/// <summary>
    /// On btnVote click event handler.
    /// </summary>
    protected void btnVote_OnClick(object sender, EventArgs e)
    {
        // Check banned ip
        if (!BannedIPInfoProvider.IsAllowed(SiteContext.CurrentSiteName, BanControlEnum.AllNonComplete))
        {
            lblInfo.CssClass = "ErrorMessage";
            lblInfo.Text     = GetString("General.BannedIP");
            return;
        }

        if (pi != null)
        {
            // Indicates whether user voted or not
            bool voted = false;

            // Indicates wheter all forms of all open-ended answers can be saved
            bool       formsAreValid   = true;
            List <int> selectedAnswers = new List <int>();

            // Check if user has already voted
            if ((CheckVoted) && (PollInfoProvider.HasVoted(pi.PollID)))
            {
                errMessage = GetString("Polls.UserHasVoted");
                voted      = true;
            }
            else if (isOpened)
            {
                // Get poll answers
                DataSet ds = Answers;
                if (!DataHelper.DataSourceIsEmpty(ds))
                {
                    DataRowCollection rows = ds.Tables[0].Rows;

                    CMSCheckBox    chkItem  = null;
                    CMSRadioButton radItem  = null;
                    bool           selected = false;
                    PollAnswerInfo pai      = null;
                    BizForm        bizItem  = null;

                    foreach (DataRow row in rows)
                    {
                        pai = new PollAnswerInfo(row);

                        if ((pai != null) && (pai.AnswerEnabled))
                        {
                            selected = false;

                            // Find specific controls and update pollanswerinfo if controls are checked
                            if (pi.PollAllowMultipleAnswers)
                            {
                                // Find checkbox
                                chkItem = (CMSCheckBox)pnlAnswer.FindControl("chk" + pai.AnswerID);

                                if (chkItem != null)
                                {
                                    selected = chkItem.Checked;
                                }
                            }
                            else
                            {
                                // Find radiobutton
                                radItem = (CMSRadioButton)pnlAnswer.FindControl("rad" + pai.AnswerID);

                                if (radItem != null)
                                {
                                    selected = radItem.Checked;
                                }
                            }

                            if ((selected) && (pai.AnswerCount < Int32.MaxValue))
                            {
                                bool canBeSaved = false;
                                bizItem = (BizForm)pnlAnswer.FindControl("frm" + pai.AnswerID);

                                if (bizItem == null)
                                {
                                    canBeSaved = true;
                                }
                                else if (bizItem != null)
                                {
                                    // Validate form data
                                    canBeSaved = bizItem.ValidateData();
                                }

                                if (canBeSaved)
                                {
                                    selectedAnswers.Add(pai.AnswerID);
                                }
                                else
                                {
                                    formsAreValid = false;
                                }
                            }
                        }
                    }

                    if (formsAreValid)
                    {
                        if (selectedAnswers.Count > 0)
                        {
                            foreach (int aid in selectedAnswers)
                            {
                                // Set the vote
                                PollAnswerInfoProvider.Vote(aid);
                                // Save the bizform data
                                bizItem = (BizForm)pnlAnswer.FindControl("frm" + aid);
                                if (bizItem != null)
                                {
                                    if (bizItem != null)
                                    {
                                        bizItem.SaveData(null, false);
                                    }
                                }
                            }
                            voted = true;
                        }
                        else
                        {
                            // Set error message if no answer selected
                            lblInfo.CssClass = "ErrorMessage";
                            lblInfo.Text     = GetString("Polls.DidNotVoted");
                        }
                    }

                    if (voted)
                    {
                        LogActivity(pi, selectedAnswers);
                    }

                    if ((CheckVoted) && (voted))
                    {
                        // Create cookie about user's voting
                        PollInfoProvider.SetVoted(pi.PollID);
                    }
                }
            }

            if (voted)
            {
                // Clear cache if it's used
                Answers = null;
                // Reload poll control
                ReloadData(true);

                if (OnAfterVoted != null)
                {
                    OnAfterVoted(this, EventArgs.Empty);
                }
            }
        }
    }
示例#2
0
    /// <summary>
    /// On btnVote click event handler.
    /// </summary>
    protected void btnVote_OnClick(object sender, EventArgs e)
    {
        // Check banned ip
        if (!BannedIPInfoProvider.IsAllowed(CMSContext.CurrentSiteName, BanControlEnum.AllNonComplete))
        {
            lblInfo.CssClass = "ErrorMessage";
            lblInfo.Text     = GetString("General.BannedIP");
            return;
        }

        if (pi != null)
        {
            // Indicates whether user voted or not
            bool voted = false;

            // Check if user has already voted
            if ((this.CheckVoted) && (PollInfoProvider.HasVoted(pi.PollID)))
            {
                errMessage = GetString("Polls.UserHasVoted");
                voted      = true;
            }
            else if (isOpened)
            {
                // Get poll answers
                DataSet ds = Answers;
                if (!DataHelper.DataSourceIsEmpty(ds))
                {
                    DataRowCollection rows = ds.Tables[0].Rows;

                    LocalizedCheckBox    chkItem = null;
                    LocalizedRadioButton radItem = null;
                    bool           selected      = false;
                    PollAnswerInfo pai           = null;

                    // List of poll answers (in case of multiple answers) for activity logging
                    StringBuilder pollAnswerIDs = new StringBuilder();

                    foreach (DataRow row in rows)
                    {
                        pai = new PollAnswerInfo(row);

                        if ((pai != null) && (pai.AnswerEnabled))
                        {
                            selected = false;

                            // Find specific controls and update pollanswerinfo if controls are checked
                            if (pi.PollAllowMultipleAnswers)
                            {
                                // Find checkbox
                                chkItem = (LocalizedCheckBox)this.pnlAnswer.FindControl("chk" + pai.AnswerID);

                                if (chkItem != null)
                                {
                                    selected = chkItem.Checked;
                                }
                            }
                            else
                            {
                                // Find radiobutton
                                radItem = (LocalizedRadioButton)this.pnlAnswer.FindControl("rad" + pai.AnswerID);

                                if (radItem != null)
                                {
                                    selected = radItem.Checked;
                                }
                            }

                            if ((selected) && (pai.AnswerCount < Int32.MaxValue))
                            {
                                // Set the vote
                                PollAnswerInfoProvider.Vote(pai.AnswerID);
                                voted = true;

                                // Save all selected answers (for activity logging)
                                pollAnswerIDs.Append(pai.AnswerID);
                                pollAnswerIDs.Append(ActivityLogProvider.POLL_ANSWER_SEPARATOR);
                            }
                        }
                    }

                    if (voted)
                    {
                        LogActivity(pi, pollAnswerIDs.ToString());
                    }

                    if ((this.CheckVoted) && (voted))
                    {
                        // Create cookie about user's voting
                        PollInfoProvider.SetVoted(pi.PollID);
                    }
                }
            }

            if (voted)
            {
                // Clear cache if it's used
                Answers = null;
                // Reload poll control
                ReloadData(true);

                if (OnAfterVoted != null)
                {
                    OnAfterVoted(this, EventArgs.Empty);
                }
            }
            else
            {
                lblInfo.CssClass = "ErrorMessage";
                lblInfo.Text     = GetString("Polls.DidNotVoted");
            }
        }
    }