/// <summary> /// Loads data. /// </summary> public void ReloadData(bool forceReload) { if (!StopProcessing) { SetContext(); lblInfo.Text = string.Empty; lblInfo.Visible = false; if (pi == null) { pi = PollInfoProvider.GetPollInfo(PollCodeName, PollSiteID, PollGroupID); hasPermission = HasPermission(); isOpened = IsOpened(); } // Show poll if current user has permission or if poll should be displayed even if user is not authorized // and if poll is opened or if poll should be opened even if it is not opened // ... and show group poll if it is poll of current group bool showPoll = (pi != null) && (hasPermission || !HideWhenNotAuthorized) && (isOpened || !HideWhenNotOpened); // Show site poll only if it is poll of current site if (showPoll && (pi.PollSiteID > 0) && (pi.PollSiteID != SiteContext.CurrentSiteID)) { showPoll = false; } // Show global poll only if it is allowed for current site if (showPoll && (pi.PollSiteID == 0)) { showPoll = SettingsKeyInfoProvider.GetBoolValue(SiteContext.CurrentSiteName + ".CMSPollsAllowGlobal"); } if (showPoll) { Visible = true; // Load title lblTitle.Text = HTMLHelper.HTMLEncode(pi.PollTitle); // Load question lblQuestion.Text = HTMLHelper.HTMLEncode(pi.PollQuestion); if ((!forceReload) || ((forceReload) && (ShowResultsAfterVote))) { // Add answer section CreateAnswerSection(forceReload, CheckVoted && PollInfoProvider.HasVoted(pi.PollID)); } else { // Hide answer panel pnlAnswer.Visible = false; } if ((forceReload) && (isOpened)) { // Hide footer with vote button pnlFooter.Visible = false; // Add poll response after voting if ((errMessage != null) && (errMessage.Trim() != "")) { // Display message if error occurs lblInfo.Text = errMessage; lblInfo.CssClass = "ErrorMessage"; } else { // Display poll response message lblResult.Text = HTMLHelper.HTMLEncode(pi.PollResponseMessage); } } else if (isOpened) { if (hasPermission && !(CheckVoted && (PollInfoProvider.HasVoted(pi.PollID)))) { // Display footer wiht vote button pnlFooter.Visible = true; btnVote.Text = ButtonText; } else { pnlFooter.Visible = false; } } else { pnlFooter.Visible = false; lblInfo.Text = GetString("Polls.Closed"); } } else { Visible = false; } ReleaseContext(); } }
/// <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; } } CMSCheckBox chkItem = null; CMSRadioButton radItem = null; LocalizedLabel lblItem = null; BizForm viewBiz = null; string formName = null; int index = 0; bool enabled = false; bool bizFormsAvailable = ModuleManager.IsModuleLoaded(ModuleName.BIZFORM) && ResourceSiteInfoProvider.IsResourceOnSite(ModuleName.BIZFORM, SiteContext.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 CMSCheckBox(); 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 CMSRadioButton(); 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 = SiteContext.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>")); } } }
/// <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>(); // 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; 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); } } // Save all selected answers (for activity logging) pollAnswerIDs.Append(aid); pollAnswerIDs.Append(ActivityPollVoting.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); } } } }