// // DotNetNuke - http://www.dotnetnuke.com // Copyright (c) 2002-2005 // by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca ) // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated // documentation files (the "Software"), to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and // to permit persons to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or substantial portions // of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // using DotNetNuke; using DotNetNuke.Security.Roles; using System.Collections.Generic; using System.Web.UI.WebControls; using System; using DotNetNuke.Services.Localization; using DotNetNuke.Security; using DotNetNuke.Services.Exceptions; using System.Web; using Microsoft.VisualBasic; namespace DotNetNuke.Modules.Survey { public partial class Survey : DotNetNuke.Entities.Modules.PortalModuleBase, Entities.Modules.IActionable { private string strCookie; private bool blnSurveyExpired; private bool blnPersonalVoteTracking; private bool blnVoted; private bool blnPrivateResults; private void Page_Load(object sender, System.EventArgs e) { try { strCookie = ("_Module" + (ModuleId.ToString() + "_Survey")); blnSurveyExpired = SurveyExpired(); blnPersonalVoteTracking = SurveyTracking(); blnPrivateResults = SurveyResultsType(); blnVoted = HasVoted(); if (!Page.IsPostBack) { DisplaySurveyPage(); } } catch (Exception exc) { // Module failed to load Exceptions.ProcessModuleLoadException(this, exc); } } private bool SurveyExpired() { if (!(Settings["surveyclosingdate"] == null)) { try { // Using VB DateAndTime rather than using a C# method to keep the code close to the VB version if (DateAndTime.DateDiff(DateInterval.Day, DateAndTime.Now, DateTime.Parse(Settings["surveyclosingdate"].ToString()), Microsoft.VisualBasic.FirstDayOfWeek.System, FirstWeekOfYear.System) > -1) { return false; } else { return true; } } catch (System.Exception) { return false; } } else { return false; } } private bool SurveyTracking() { // Check the settings to see if this module is using Personalization for vote tracking if (!(Settings["surveytracking"] == null)) { return (Convert.ToBoolean(int.Parse(Settings["surveytracking"].ToString()))); } else { return false; } } private bool SurveyResultsType() { // If the user is able to edit the module then they are always able to see the results if (DotNetNuke.Security.PortalSecurity.IsInRoles(this.ModuleConfiguration.AuthorizedEditRoles)) { return false; } // Check the settings to see if this module is using Personalization for vote tracking if (!(Settings["surveyresultstype"] == null)) { return (Convert.ToBoolean(int.Parse(Settings["surveyresultstype"].ToString()))); } else { return false; } } private bool HasVoted() { if ((blnPersonalVoteTracking == true)) { if ((UserId != -1)) { // Check if the user has voted before if (!(DotNetNuke.Services.Personalization.Personalization.GetProfile(ModuleId.ToString(), "Voted") == null)) { return ((bool)(DotNetNuke.Services.Personalization.Personalization.GetProfile(ModuleId.ToString(), "Voted"))); } else { return false; } } else { // The module is using Personalization but the user is not logged in return true; } } else { // The Module is not using personalization so look for a cookie if ((Request.Cookies[strCookie] == null)) { return false; } else { return true; } } } private void DisplaySurveyPage() { // Only show Survey if it is not expired if (!blnSurveyExpired) { if ((blnPersonalVoteTracking && (UserId == -1))) { // The User is not logged in and the module is using personalization DisplaySurvey(); cmdSubmit.Visible = false; cmdResults.Visible = false; Message_Label.Text = Localization.GetString("Survey_MustBeSignedIn", this.LocalResourceFile); } else if ((blnVoted == false)) { // They haven't voted so show the Survey DisplaySurvey(); // If the results are private, hide the results link if (blnPrivateResults) { cmdResults.Visible = false; } } else { // They have voted so display the results DisplayResults(); } } else { // Survey is expired so display the results DisplayResults(); } } private void DisplaySurvey() { pnlResults.Visible = false; List Surveylist = SurveyController.GetSurveys(ModuleId); if ((Surveylist.Count > 0)) { lstSurvey.DataSource = Surveylist; lstSurvey.DataBind(); pnlSurvey.Visible = true; } else { pnlSurvey.Visible = false; } } private void DisplayResults() { lstResults.DataSource = SurveyController.GetSurveys(ModuleId); lstResults.DataBind(); // Hide the survey items, show the results pnlSurvey.Visible = false; pnlResults.Visible = true; if ((DotNetNuke.Security.PortalSecurity.IsInRoles(this.ModuleConfiguration.AuthorizedEditRoles) && (UserId != -1))) { cmdSurvey.Visible = true; } else { // If the results are private, hide the results and the results link if (blnPrivateResults) { lstResults.Visible = false; cmdResults.Visible = false; Message_Label.Text = Localization.GetString("AlreadyVoted", this.LocalResourceFile); } // If they have voted or the Survey has expired, hide the button to show the Survey items if ((!blnVoted && !blnSurveyExpired)) { cmdSurvey.Visible = true; } else { cmdSurvey.Visible = false; } } } public string FormatQuestion(string strQuestion, int ItemNumber) { return (ItemNumber.ToString() + (". " + strQuestion)); } private string FormatSurveyOption(string OptionName, bool IsCorrect) { if (IsCorrect) { if (((OptionName.StartsWith(">") == true) && (OptionName.EndsWith("<") == true))) { return OptionName; } else { return (">" + OptionName + "<"); } } else if (((OptionName.StartsWith(">") == true) && (OptionName.EndsWith("<") == true))) { return OptionName.Substring(1, (OptionName.Length - 2)); } else { return OptionName; } } protected void lstSurvey_ItemDataBound(object sender, DataListItemEventArgs e) { SurveyInfo objSurvey = SurveyController.GetSurvey(Int32.Parse(lstSurvey.DataKeys[e.Item.ItemIndex].ToString()), ModuleId); if (!(objSurvey == null)) { List SurveyOptionList = new List(); SurveyOptionList = SurveyOptionController.GetSurveyOptions(objSurvey.SurveyId); switch (objSurvey.OptionType) { case "R": RadioButtonList optOptions = ((RadioButtonList)(e.Item.FindControl("optOptions"))); optOptions.DataSource = SurveyOptionList; optOptions.DataTextField = "OptionName"; optOptions.DataValueField = "SurveyOptionId"; optOptions.DataBind(); optOptions.SelectedValue = SurveyOptionList[0].SurveyOptionId.ToString(); optOptions.Visible = true; break; case "C": CheckBoxList chkOptions = ((CheckBoxList)(e.Item.FindControl("chkOptions"))); chkOptions.DataSource = SurveyOptionList; chkOptions.DataTextField = "OptionName"; chkOptions.DataValueField = "SurveyOptionId"; chkOptions.DataBind(); chkOptions.Visible = true; break; } } } protected void cmdSubmit_Click(object sender, System.EventArgs e) { RadioButtonList objRadioButtonList; CheckBoxList objCheckBoxList; SurveyInfo objSurvey; int intSurvey; SurveyOptionInfo objSurveyOption = new SurveyOptionInfo(); int intSurveyOption; int intQuestion; int intOption; bool blnValid = true; bool blntmpValid = false; intQuestion = -1; List arrSurveys = SurveyController.GetSurveys(ModuleId); for (intSurvey = 0; (intSurvey <= (arrSurveys.Count - 1)); intSurvey++) { objSurvey = arrSurveys[intSurvey]; intQuestion++; switch (objSurvey.OptionType) { case "R": objRadioButtonList = (RadioButtonList)this.FindControl("lstSurvey").Controls[intQuestion].FindControl("optOptions"); if ((objRadioButtonList.SelectedIndex == -1)) { blnValid = false; } break; case "C": objCheckBoxList = (CheckBoxList)this.FindControl("lstSurvey").Controls[intQuestion].FindControl("chkOptions"); blntmpValid = false; intOption = -1; List arrSurveyOptions = SurveyOptionController.GetSurveyOptions(objSurvey.SurveyId); for (intSurveyOption = 0; (intSurveyOption <= (arrSurveyOptions.Count - 1)); intSurveyOption++) { intOption++; if ((objCheckBoxList.Items[intOption].Selected == true)) { blntmpValid = true; } } if ((blntmpValid == false)) { blnValid = false; } break; } } if (blnValid) { intQuestion = -1; for (intSurvey = 0; (intSurvey <= (arrSurveys.Count - 1)); intSurvey++) { objSurvey = ((SurveyInfo)(arrSurveys[intSurvey])); intQuestion++; switch (objSurvey.OptionType) { case "R": if (!(this.FindControl("lstSurvey").Controls[intQuestion].FindControl("optOptions") == null)) { objRadioButtonList = (RadioButtonList)this.FindControl("lstSurvey").Controls[intQuestion].FindControl("optOptions"); objSurveyOption.SurveyOptionId = Convert.ToInt32(objRadioButtonList.SelectedValue); SurveyOptionController.AddSurveyResult(objSurveyOption, UserId); } break; case "C": intOption = -1; objCheckBoxList = (CheckBoxList)this.FindControl("lstSurvey").Controls[intQuestion].FindControl("chkOptions"); List arrSurveyOptions = SurveyOptionController.GetSurveyOptions(objSurvey.SurveyId); for (intSurveyOption = 0; (intSurveyOption <= (arrSurveyOptions.Count - 1)); intSurveyOption++) { objSurveyOption = ((SurveyOptionInfo)(arrSurveyOptions[intSurveyOption])); intOption++; if ((objCheckBoxList.Items[intOption].Selected == true)) { SurveyOptionController.AddSurveyResult(objSurveyOption, UserId); } } break; } } Message_Label.Text = Localization.GetString("SurveyComplete", this.LocalResourceFile); if ((blnPersonalVoteTracking == true)) { // This means the module vote tracking is using personalization, so set the profile to show they have voted DotNetNuke.Services.Personalization.Personalization.SetProfile(ModuleId.ToString(), "Voted", true); blnVoted = true; DisplayResults(); } else { // Store a cookie to show the chart after the submit HttpCookie objCookie = new HttpCookie(("_Module" + (ModuleId.ToString() + "_Survey"))); objCookie.Value = "True"; objCookie.Expires = DateTime.MaxValue; // never expires Response.AppendCookie(objCookie); blnVoted = true; DisplayResults(); } } else { Message_Label.Text = Localization.GetString("SurveyIncomplete", this.LocalResourceFile); } } protected void cmdResults_Click(object sender, System.EventArgs e) { DisplayResults(); } protected void lstResults_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) { SurveyInfo objSurvey; SurveyOptionInfo objSurveyOption; int intSurveyOption; string strHTML; int intGraphWidth = 100; if ((Settings["surveygraphwidth"]) != null) { try { intGraphWidth = Int32.Parse((string)(Settings["surveygraphwidth"])); } catch { intGraphWidth = 100; } } strHTML = "
"; objSurvey = SurveyController.GetSurvey(Int32.Parse(lstResults.DataKeys[e.Item.ItemIndex].ToString()), ModuleId); if (!(objSurvey == null)) { List arrSurveyOptions = SurveyOptionController.GetSurveyOptions(objSurvey.SurveyId); for (intSurveyOption = 0; (intSurveyOption <= (arrSurveyOptions.Count - 1)); intSurveyOption++) { objSurveyOption = arrSurveyOptions[intSurveyOption]; float dblPercent = 0; int intPercent = 0; if ((objSurvey.Votes != 0)) { dblPercent = ((float)objSurveyOption.Votes / (float)objSurvey.Votes); intPercent = Convert.ToInt16(dblPercent * 100); } strHTML += ""; strHTML = (strHTML + (""))))); strHTML = (strHTML + (""))))))); strHTML += ""; } } strHTML += "
" + (FormatSurveyOption(objSurveyOption.OptionName, objSurveyOption.IsCorrect) + (" (" + (objSurveyOption.Votes.ToString() + ") " + (intPercent.ToString() + "%
"; Label lblResults = ((Label)(e.Item.FindControl("lblResults"))); lblResults.Text = strHTML; } protected void cmdSurvey_Click(object sender, System.EventArgs e) { DisplaySurvey(); } public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions { get { Entities.Modules.Actions.ModuleActionCollection Actions = new Entities.Modules.Actions.ModuleActionCollection(); Actions.Add(GetNextActionID(), Localization.GetString(Entities.Modules.Actions.ModuleActionType.AddContent, LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false); return Actions; } } } }