/// <summary> /// Creates poll answer section. /// </summary> /// <param name="reload">Indicates postback</param> /// <param name="hasVoted">Indicates if user has voted</param> protected void CreateAnswerSection(bool reload, bool hasVoted) { pnlAnswer.Controls.Clear(); if (pi != null) { // Get poll's answers DataSet ds = Answers; if (!DataHelper.DataSourceIsEmpty(ds)) { int count = 0; int maxCount = 0; long sumCount = 0; // Sum answer counts and get highest foreach (DataRow row in ds.Tables[0].Rows) { if (ValidationHelper.GetBoolean(row["AnswerEnabled"], true)) { count = ValidationHelper.GetInteger(row["AnswerCount"], 0); sumCount += count; if (count > maxCount) { maxCount = count; } } } LocalizedCheckBox chkItem = null; LocalizedRadioButton radItem = null; LocalizedLabel lblItem = null; int index = 0; bool enabled = false; pnlAnswer.Controls.Add(new LiteralControl("<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">")); // Create the answers foreach (DataRow row in ds.Tables[0].Rows) { enabled = ValidationHelper.GetBoolean(row["AnswerEnabled"], true); if (enabled) { pnlAnswer.Controls.Add(new LiteralControl("<tr><td class=\"PollAnswer\" colspan=\"2\">")); if (((reload) && (this.ShowResultsAfterVote)) || (!hasPermission && !this.HideWhenNotAuthorized) || (!isOpened && !this.HideWhenNotOpened) || (this.CheckVoted && PollInfoProvider.HasVoted(pi.PollID))) { // Add label lblItem = new LocalizedLabel(); lblItem.ID = "lbl" + ValidationHelper.GetInteger(row["AnswerID"], 0); lblItem.EnableViewState = false; lblItem.Text = HTMLHelper.HTMLEncode(ValidationHelper.GetString(row["AnswerText"], "")); lblItem.CssClass = "PollAnswerText"; pnlAnswer.Controls.Add(lblItem); } else { if (pi.PollAllowMultipleAnswers) { // Add checkboxes for multiple answers chkItem = new LocalizedCheckBox(); chkItem.ID = "chk" + ValidationHelper.GetInteger(row["AnswerID"], 0); chkItem.AutoPostBack = false; chkItem.Text = HTMLHelper.HTMLEncode(ValidationHelper.GetString(row["AnswerText"], "")); chkItem.Checked = false; chkItem.CssClass = "PollAnswerCheck"; pnlAnswer.Controls.Add(chkItem); } else { // Add radiobuttons radItem = new LocalizedRadioButton(); radItem.ID = "rad" + ValidationHelper.GetInteger(row["AnswerID"], 0); radItem.AutoPostBack = false; radItem.GroupName = pi.PollCodeName + "Group"; radItem.Text = HTMLHelper.HTMLEncode(ValidationHelper.GetString(row["AnswerText"], "")); radItem.Checked = false; radItem.CssClass = "PollAnswerRadio"; pnlAnswer.Controls.Add(radItem); } } pnlAnswer.Controls.Add(new LiteralControl("</td></tr>")); if (this.ShowGraph || (hasVoted || reload) && this.ShowResultsAfterVote) { // Create graph under the answer CreateGraph(maxCount, ValidationHelper.GetInteger(row["AnswerCount"], 0), sumCount, index); } index++; } } pnlAnswer.Controls.Add(new LiteralControl("</table>")); } } }
/// <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; // Indicates wheter all forms of all open-ended answers can be saved bool formsAreValid = true; List <int> selectedAnswers = new List <int>(); // List of poll answers (in case of multiple answers) for activity logging StringBuilder pollAnswerIDs = new StringBuilder(); // 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; LocalizedCheckBox chkItem = null; LocalizedRadioButton 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 = (LocalizedCheckBox)pnlAnswer.FindControl("chk" + pai.AnswerID); if (chkItem != null) { selected = chkItem.Checked; } } else { // Find radiobutton radItem = (LocalizedRadioButton)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.BasicForm != null) { // Validate form data canBeSaved = bizItem.BasicForm.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.BasicForm != null) { bizItem.BasicForm.SaveData(null, false); } } // Save all selected answers (for activity logging) pollAnswerIDs.Append(aid); pollAnswerIDs.Append(ActivityLogProvider.POLL_ANSWER_SEPARATOR); } voted = true; } else { // Set error message if no answer selected lblInfo.CssClass = "ErrorMessage"; lblInfo.Text = GetString("Polls.DidNotVoted"); } } if (voted) { LogActivity(pi, pollAnswerIDs.ToString()); } 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); } } } }
/// <summary> /// Creates poll answer section. /// </summary> /// <param name="reload">Indicates postback</param> /// <param name="hasVoted">Indicates if user has voted</param> protected void CreateAnswerSection(bool reload, bool hasVoted) { pnlAnswer.Controls.Clear(); if (pi != null) { // Get poll's answers DataSet ds = Answers; if (!DataHelper.DataSourceIsEmpty(ds)) { int count = 0; int maxCount = 0; long sumCount = 0; bool hideSomeForm = false; foreach (DataRow row in ds.Tables[0].Rows) { // Sum answer counts and get highest if (ValidationHelper.GetBoolean(row["AnswerEnabled"], true)) { count = ValidationHelper.GetInteger(row["AnswerCount"], 0); sumCount += count; if (count > maxCount) { maxCount = count; } } // Check if any open-ended answer form should be hidden if (ValidationHelper.GetBoolean(row["AnswerHideForm"], false)) { hideSomeForm = true; } } LocalizedCheckBox chkItem = null; LocalizedRadioButton radItem = null; LocalizedLabel lblItem = null; BizForm viewBiz = null; string formName = null; int index = 0; bool enabled = false; bool bizFormsAvailable = ModuleEntry.IsModuleLoaded(ModuleEntry.BIZFORM) && ResourceSiteInfoProvider.IsResourceOnSite(ModuleEntry.BIZFORM, CMSContext.CurrentSiteName); pnlAnswer.Controls.Add(new LiteralControl("<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">")); // Create the answers foreach (DataRow row in ds.Tables[0].Rows) { enabled = ValidationHelper.GetBoolean(row["AnswerEnabled"], true); if (enabled) { pnlAnswer.Controls.Add(new LiteralControl("<tr><td class=\"PollAnswer\" colspan=\"2\">")); if (((reload) && (ShowResultsAfterVote)) || (!hasPermission && !HideWhenNotAuthorized) || (!isOpened && !HideWhenNotOpened) || (CheckVoted && PollInfoProvider.HasVoted(pi.PollID))) { // Add label lblItem = new LocalizedLabel(); lblItem.ID = "lbl" + ValidationHelper.GetInteger(row["AnswerID"], 0); lblItem.EnableViewState = false; lblItem.Text = HTMLHelper.HTMLEncode(ValidationHelper.GetString(row["AnswerText"], string.Empty)); lblItem.CssClass = "PollAnswerText"; pnlAnswer.Controls.Add(lblItem); } else { if (pi.PollAllowMultipleAnswers) { // Add checkboxes for multiple answers chkItem = new LocalizedCheckBox(); chkItem.ID = "chk" + ValidationHelper.GetInteger(row["AnswerID"], 0); chkItem.AutoPostBack = false; chkItem.Text = HTMLHelper.HTMLEncode(ValidationHelper.GetString(row["AnswerText"], string.Empty)); chkItem.Checked = false; chkItem.CssClass = "PollAnswerCheck"; if (hideSomeForm) { chkItem.AutoPostBack = true; chkItem.CheckedChanged += AnswerSelectionChanged; } pnlAnswer.Controls.Add(chkItem); } else { // Add radiobuttons radItem = new LocalizedRadioButton(); radItem.ID = "rad" + ValidationHelper.GetInteger(row["AnswerID"], 0); radItem.AutoPostBack = false; radItem.GroupName = pi.PollCodeName + "Group"; radItem.Text = HTMLHelper.HTMLEncode(ValidationHelper.GetString(row["AnswerText"], string.Empty)); radItem.Checked = false; radItem.CssClass = "PollAnswerRadio"; if (hideSomeForm) { radItem.AutoPostBack = true; radItem.CheckedChanged += AnswerSelectionChanged; } pnlAnswer.Controls.Add(radItem); } formName = ValidationHelper.GetString(row["AnswerForm"], string.Empty); if (!string.IsNullOrEmpty(formName) && bizFormsAvailable) { // Add forms for open ended answers viewBiz = new BizForm(); viewBiz.FormName = formName; viewBiz.SiteName = CMSContext.CurrentSiteName; viewBiz.AlternativeFormFullName = ValidationHelper.GetString(row["AnswerAlternativeForm"], string.Empty); viewBiz.ID = "frm" + ValidationHelper.GetInteger(row["AnswerID"], 0); viewBiz.IsLiveSite = IsLiveSite; viewBiz.OnAfterDataLoad += Form_AfterDataLoad; viewBiz.CssClass = "PollAnswerForm"; viewBiz.Visible = !ValidationHelper.GetBoolean(row["AnswerHideForm"], false); viewBiz.FormClearAfterSave = true; viewBiz.FormRedirectToUrl = String.Empty; viewBiz.FormDisplayText = String.Empty; pnlAnswer.Controls.Add(viewBiz); } } pnlAnswer.Controls.Add(new LiteralControl("</td></tr>")); if (ShowGraph || (hasVoted || reload) && ShowResultsAfterVote) { // Create graph under the answer CreateGraph(maxCount, ValidationHelper.GetInteger(row["AnswerCount"], 0), sumCount, index); } index++; } } pnlAnswer.Controls.Add(new LiteralControl("</table>")); } } }