public ActionResult CancelVotePost(int forumPostID)
        {
            var db = new ZkDataContext();
            AccountForumVote existingVote = db.AccountForumVotes.SingleOrDefault(x => x.ForumPostID == forumPostID && x.AccountID == Global.AccountID);

            if (existingVote == null)
            {
                return(Content("No existing vote to remove"));
            }

            ForumPost post   = db.ForumPosts.First(x => x.ForumPostID == forumPostID);
            Account   author = post.Account;

            int delta = existingVote.Vote;

            // reverse vote effects
            if (delta > 0)
            {
                author.ForumTotalUpvotes = author.ForumTotalUpvotes - delta;
                post.Upvotes             = post.Upvotes - delta;
            }
            else if (delta < 0)
            {
                author.ForumTotalDownvotes = author.ForumTotalDownvotes + delta;
                post.Downvotes             = post.Downvotes + delta;
            }
            db.AccountForumVotes.DeleteOnSubmit(existingVote);

            db.SubmitChanges();

            return(RedirectToAction("Thread", new { id = post.ForumThreadID, postID = forumPostID }));
        }
Пример #2
0
        public static void DeleteUserVote(int accountID, int forumPostID, ZkDataContext db)
        {
            AccountForumVote existingVote = db.AccountForumVotes.SingleOrDefault(x => x.ForumPostID == forumPostID && x.AccountID == accountID);

            if (existingVote == null)
            {
                return;
            }

            ForumPost post   = db.ForumPosts.First(x => x.ForumPostID == forumPostID);
            Account   author = post.Account;

            int delta = existingVote.Vote;

            // reverse vote effects
            if (delta > 0)
            {
                author.ForumTotalUpvotes = author.ForumTotalUpvotes - delta;
                post.Upvotes             = post.Upvotes - delta;
            }
            else if (delta < 0)
            {
                author.ForumTotalDownvotes = author.ForumTotalDownvotes + delta;
                post.Downvotes             = post.Downvotes + delta;
            }
            db.AccountForumVotes.DeleteOnSubmit(existingVote);
        }
Пример #3
0
        /// <summary>
        ///     <para>Returns the sum of the + and - votes on the specified <see cref="ForumPost"/></para>
        ///     <para>The + and - numbers serve as links to vote on the post</para>
        ///     <para>Also includes a link to cancel an existing vote</para>
        ///     <para>The tooltip displays the people who voted for each option</para>
        /// </summary>
        /// <param name="blockPost">Removes the vote links; is true if the viewer's <see cref="Account"/> is banned or has too many net downvotes</param>
        public static MvcHtmlString PrintPostRating(this HtmlHelper helper, ForumPost post, bool blockPost = false)
        {
            var              url           = Global.UrlHelper();
            bool             noLink        = (Global.Account == null || Global.AccountID == post.AuthorAccountID || Global.Account.Level < GlobalConst.MinLevelForForumVote || Global.Account.VotesAvailable <= 0 || blockPost);
            AccountForumVote previousVote  = post.AccountForumVotes.SingleOrDefault(x => x.AccountID == Global.AccountID);
            bool             upvoted       = (previousVote != null && previousVote.Vote > 0);
            bool             downvoted     = (previousVote != null && previousVote.Vote < 0);
            bool             votersVisible = (!GlobalConst.OnlyAdminsSeePostVoters || (Global.Account?.AdminLevel >= AdminLevel.Moderator));

            /*
             * return new MvcHtmlString(string.Format("<input type='' name='upvote' value='{3}{0}{4}' title='Upvote'> / <input type='submit' name='downvote' value='{5}{1}{6}'> {2}",
             *      string.Format("<font {0}>+{1}</font>", post.Upvotes > 0 ? "color='LawnGreen'" : "", post.Upvotes),
             *      string.Format("<font {0}>-{1}</font>", post.Downvotes > 0 ? "color='Tomato'" : "", post.Downvotes),
             *      previousVote != null ? string.Format("(<input type='submit' name='clearvote' value='clear'>)") : "",
             *      upvoted ? "<strong>" : "",
             *      upvoted ? "</strong>" : "",
             *      downvoted ? "<strong>" : "",
             *      downvoted ? "</strong>" : ""));
             */

            string upvote = string.Format("<{0} nicetitle='{1}'>{2}{3}{4}{5}",
                                          !noLink? string.Format("a href='{0}'", url.Action("VotePost", "Forum", new { forumPostID = post.ForumPostID, delta = 1 })) : "span",
                                          votersVisible? string.Format("$forumVotes${0}", post.ForumPostID) : "Upvote",
                                          upvoted ? "<strong>" : "",
                                          string.Format("<font {0}>+{1}</font>", post.Upvotes > 0 ? "color='LawnGreen'" : "", post.Upvotes),
                                          upvoted ? "</strong>" : "",
                                          !noLink? "</a>" : "</span>"
                                          );
            string downvote = string.Format("<{0} nicetitle='{1}'>{2}{3}{4}{5}",
                                            !noLink? string.Format("a href='{0}'", url.Action("VotePost", "Forum", new { forumPostID = post.ForumPostID, delta = -1 })) : "span",
                                            votersVisible? string.Format("$forumVotes${0}", post.ForumPostID) : "Downvote",
                                            downvoted ? "<strong>" : "",
                                            string.Format("<font {0}>-{1}</font>", post.Downvotes > 0 ? "color='Tomato'" : "", post.Downvotes),
                                            downvoted ? "</strong>" : "",
                                            !noLink? "</a>" : "</span>"
                                            );

            return(new MvcHtmlString(string.Format("{0} / {1} {2}",
                                                   upvote,
                                                   downvote,
                                                   previousVote != null ? string.Format("(<a href='{0}'>cancel</a>)", url.Action("CancelVotePost", "Forum", new { forumPostID = post.ForumPostID })) : ""
                                                   )));
        }
        public ActionResult VotePost(int forumPostID, int delta)
        {
            var db    = new ZkDataContext();
            var myAcc = Global.Account;

            var penalty = Punishment.GetActivePunishment(Global.AccountID, Request.UserHostAddress, 0, null, x => x.BanForum);

            if (penalty != null)
            {
                return(Content(string.Format("You cannot vote while banned from forum!\nExpires: {0} UTC\nReason: {1}", penalty.BanExpires, penalty.Reason)));
            }

            if (myAcc.Level < GlobalConst.MinLevelForForumVote)
            {
                return(Content(string.Format("You cannot vote until you are level {0} or higher", GlobalConst.MinLevelForForumVote)));
            }
            if ((Global.Account.ForumTotalUpvotes - Global.Account.ForumTotalDownvotes) < GlobalConst.MinNetKarmaToVote)
            {
                return(Content("Your net karma is too low to vote"));
            }

            if (delta > 1)
            {
                delta = 1;
            }
            else if (delta < -1)
            {
                delta = -1;
            }

            var post   = db.ForumPosts.First(x => x.ForumPostID == forumPostID);
            var author = post.Account;

            if (author.AccountID == Global.AccountID)
            {
                return(Content("Cannot vote for your own posts"));
            }
            if (myAcc.VotesAvailable <= 0)
            {
                return(Content("Out of votes"));
            }

            var existingVote = db.AccountForumVotes.SingleOrDefault(x => x.ForumPostID == forumPostID && x.AccountID == Global.AccountID);

            if (existingVote != null) // clear existing vote
            {
                var oldDelta = existingVote.Vote;
                // reverse vote effects
                if (oldDelta > 0)
                {
                    author.ForumTotalUpvotes = author.ForumTotalUpvotes - oldDelta;
                    post.Upvotes             = post.Upvotes - oldDelta;
                }
                else if (oldDelta < 0)
                {
                    author.ForumTotalDownvotes = author.ForumTotalDownvotes + oldDelta;
                    post.Downvotes             = post.Downvotes + oldDelta;
                }
                db.AccountForumVotes.DeleteOnSubmit(existingVote);
            }
            if (delta > 0)
            {
                author.ForumTotalUpvotes = author.ForumTotalUpvotes + delta;
                post.Upvotes             = post.Upvotes + delta;
            }
            else if (delta < 0)
            {
                author.ForumTotalDownvotes = author.ForumTotalDownvotes - delta;
                post.Downvotes             = post.Downvotes - delta;
            }

            if (delta != 0)
            {
                var voteEntry = new AccountForumVote {
                    AccountID = Global.AccountID, ForumPostID = forumPostID, Vote = delta
                };
                db.AccountForumVotes.InsertOnSubmit(voteEntry);
                myAcc.VotesAvailable--;
            }

            db.SaveChanges();

            return(RedirectToAction("Thread", new { id = post.ForumThreadID, postID = forumPostID }));
        }
        public ActionResult VotePost(int forumPostID, int delta)
        {
            var     db    = new ZkDataContext();
            Account myAcc = Global.Account;

            if (myAcc.Level < GlobalConst.MinLevelForForumVote)
            {
                return(Content(string.Format("You cannot vote until you are level {0} or higher", GlobalConst.MinLevelForForumVote)));
            }
            if ((Global.Account.ForumTotalUpvotes - Global.Account.ForumTotalDownvotes) < GlobalConst.MinNetKarmaToVote)
            {
                return(Content("Your net karma is too low to vote"));
            }

            if (delta > 1)
            {
                delta = 1;
            }
            else if (delta < -1)
            {
                delta = -1;
            }

            ForumPost post   = db.ForumPosts.First(x => x.ForumPostID == forumPostID);
            Account   author = post.Account;

            if (author.AccountID == Global.AccountID)
            {
                return(Content("Cannot vote for your own posts"));
            }
            if (myAcc.VotesAvailable <= 0)
            {
                return(Content("Out of votes"));
            }

            AccountForumVote existingVote = db.AccountForumVotes.SingleOrDefault(x => x.ForumPostID == forumPostID && x.AccountID == Global.AccountID);

            if (existingVote != null)   // clear existing vote
            {
                int oldDelta = existingVote.Vote;
                // reverse vote effects
                if (oldDelta > 0)
                {
                    author.ForumTotalUpvotes = author.ForumTotalUpvotes - oldDelta;
                    post.Upvotes             = post.Upvotes - oldDelta;
                }
                else if (oldDelta < 0)
                {
                    author.ForumTotalDownvotes = author.ForumTotalDownvotes + oldDelta;
                    post.Downvotes             = post.Downvotes + oldDelta;
                }
                db.AccountForumVotes.DeleteOnSubmit(existingVote);
            }
            if (delta > 0)
            {
                author.ForumTotalUpvotes = author.ForumTotalUpvotes + delta;
                post.Upvotes             = post.Upvotes + delta;
            }
            else if (delta < 0)
            {
                author.ForumTotalDownvotes = author.ForumTotalDownvotes - delta;
                post.Downvotes             = post.Downvotes - delta;
            }

            if (delta != 0)
            {
                AccountForumVote voteEntry = new AccountForumVote {
                    AccountID = Global.AccountID, ForumPostID = forumPostID, Vote = delta
                };
                db.AccountForumVotes.InsertOnSubmit(voteEntry);
                myAcc.VotesAvailable--;
            }

            db.SubmitChanges();

            return(RedirectToAction("Thread", new { id = post.ForumThreadID, postID = forumPostID }));
        }