protected void EditImage_Click(object sender, EventArgs e) { DnnImageButton editImage = (DnnImageButton)sender; SurveyOptionsInfo surveyOption = SurveyOptionsController.Get(Convert.ToInt32(editImage.CommandArgument)); if (surveyOption != null) { SurveyOptionID = surveyOption.SurveyOptionID; SurveyOption = surveyOption; AnswerTextBox.Text = surveyOption.OptionName; CorrectAnswerCheckBox.Checked = surveyOption.IsCorrect; AddAnswerButton.Visible = false; UpdateAnswerButton.Visible = true; } }
protected override void OnLoad(EventArgs e) { if (!(Page.IsPostBack)) { CreateQuestionTypeEntries(); CreateRepeatDirectionEntries(); CreateChartTypeEntries(); CorrectAnswerPanel.Visible = (SurveyType == SurveyType.Quiz); if (SurveyID > 0) { Survey = SurveysController.Get(SurveyID); Answers = SurveyOptionsController.GetAll(SurveyID); QuestionTextBox.Text = Survey.Question; QuestionTypeDropDownList.SelectedValue = Convert.ToInt32(Survey.OptionType).ToString(); ChartTypeDropDownList.SelectedValue = Convert.ToInt32(Survey.ChartType).ToString(); IsStatisticalCheckBox.Checked = (Survey.IsStatistical.HasValue ? Survey.IsStatistical.Value : false); if (Survey.OptionType == QuestionType.RadioButtons || Survey.OptionType == QuestionType.CheckBoxes) { RepeatDirectionDropDownList.SelectedValue = Convert.ToInt32(Survey.RepeatDirection).ToString(); RepeatColumnsTextBox.Text = Survey.RepeatColumns.ToString(); } else { NumberOfRowsTextBox.Text = Survey.NumberOfRows.ToString(); IsStatisticalCheckBox.Enabled = false; RepeatDirectionPanel.Visible = false; AnswersPanel.Visible = false; TextAnswerPanel.Visible = true; } AnswersGrid.DataSource = Answers; AnswersGrid.DataBind(); MaxViewOrder = Answers.Count; ChartTypeChanged = true; } if (SurveyType == SurveyType.Quiz) { IsStatisticalPanel.Visible = true; } } }
private void DisplayQuizResults(List <SurveysInfo> surveys, List <SurveyResultsInfo> surveyResults) { int score = 0; List <SurveysInfo> quizQuestions = surveys.FindAll(s => ((!(s.IsStatistical.HasValue))) || (!(s.IsStatistical.Value))); List <SurveysInfo> statisticalQuestions = surveys.FindAll(s => ((s.IsStatistical.HasValue) && (s.IsStatistical.Value))); foreach (SurveysInfo survey in quizQuestions) { SurveyPlaceHolder.Controls.Add(new LiteralControl(string.Format("<h2>{0}</h2>", survey.Question))); List <SurveyOptionsInfo> answers = SurveyOptionsController.GetAll(survey.SurveyID); int correctAnswersCount = answers.FindAll(a => a.IsCorrect).Count; StringBuilder yourAnswers = new StringBuilder(); StringBuilder correctAnswers = new StringBuilder(); int yourAnswersCount = 0; int yourCorrectAnswers = 0; List <SurveyResultsInfo> yourAnswersList = new List <SurveyResultsInfo>(); foreach (SurveyOptionsInfo answer in answers) { yourAnswersList = surveyResults.FindAll(r => r.SurveyOptionID == answer.SurveyOptionID); yourAnswersCount += yourAnswersList.Count; foreach (SurveyResultsInfo yourAnswer in yourAnswersList) { if (yourAnswer.IsCorrect) { yourCorrectAnswers++; yourAnswers.Append(string.Format("<img src=\"{0}images/correct.png\" class=\"surveyImageLeft\" />{1}<br />", ControlPath, answer.OptionName)); } else { yourAnswers.Append(string.Format("<img src=\"{0}images/not-correct.png\" class=\"surveyImageLeft\" />{1}<br />", ControlPath, answer.OptionName)); } } if (answer.IsCorrect) { correctAnswers.Append(string.Format("<img src=\"{0}images/correct.png\" class=\"surveyImageLeft\" />{1}<br />", ControlPath, answer.OptionName)); } } string answerClass = (yourCorrectAnswers == correctAnswersCount ? "dnnFormSuccess" : "dnnFormValidationSummary"); SurveyPlaceHolder.Controls.Add(new LiteralControl(string.Format("<h3>{0}:</h3><div class=\"dnnFormMessage {1}\">{2}</div>", Localization.GetString((yourAnswersCount == 1 ? "YourAnswer.Text" : "YourAnswers.Text"), LocalResourceFile), answerClass, yourAnswers.Remove(yourAnswers.Length - 6, 6).ToString()))); if ((yourCorrectAnswers < correctAnswersCount)) { SurveyPlaceHolder.Controls.Add(new LiteralControl(string.Format("<h4>{0}:</h4><div class=\"dnnFormMessage dnnFrormSuccess\">{1}</div>", Localization.GetString((correctAnswersCount == 1 ? "CorrectAnswer.Text" : "CorrectAnswers.Text"), LocalResourceFile), correctAnswers.Remove(correctAnswers.Length - 6, 6).ToString()))); } else { score++; } } string scoreClass = (score == surveys.Count ? "dnnFormSuccess" : (score == 0 ? "dnnFormValidationSummary" : "dnnFormWarning")); SurveyPlaceHolder.Controls.Add(new LiteralControl(string.Format("<div class=\"dnnFormMessage {0} surveyQuizResult\">{1}: {2}/{3} - {4:0.00}%</div>", scoreClass, Localization.GetString("YourResult.Text", LocalResourceFile), score, quizQuestions.Count, Convert.ToDouble(score) / Convert.ToDouble(quizQuestions.Count) * 100.00))); if (statisticalQuestions.Count > 0) { StringBuilder yourStatisticalAnswers = new StringBuilder(); yourStatisticalAnswers.Append(string.Format("<h2>{0}</h2>", Localization.GetString("StatisticalAnswers.Text", LocalResourceFile))); foreach (SurveysInfo survey in statisticalQuestions) { yourStatisticalAnswers.Append(string.Format("<div class=\"dnnFormMessage dnnFormInfo\"><h3>{0}</h3>", survey.Question)); if ((survey.OptionType == QuestionType.RadioButtons) || (survey.OptionType == QuestionType.CheckBoxes)) { List <SurveyOptionsInfo> answers = SurveyOptionsController.GetAll(survey.SurveyID); List <SurveyResultsInfo> yourAnswersList = new List <SurveyResultsInfo>(); foreach (SurveyOptionsInfo answer in answers) { yourAnswersList = surveyResults.FindAll(r => r.SurveyOptionID == answer.SurveyOptionID); foreach (SurveyResultsInfo yourAnswer in yourAnswersList) { yourStatisticalAnswers.Append(string.Format("<img src =\"{0}images/info.png\" class=\"surveyImageLeft\" />{1}<br />", ControlPath, answer.OptionName)); } } } else { yourStatisticalAnswers.Append(string.Format("<img src =\"{0}images/info.png\" class=\"surveyImageLeft\" />{1}<br />", ControlPath, surveyResults[0].TextAnswer)); } yourStatisticalAnswers.Append("</div>"); } SurveyPlaceHolder.Controls.Add(new LiteralControl(yourStatisticalAnswers.ToString())); } }
private void CreateSurveyItems(List <SurveysInfo> surveys) { foreach (SurveysInfo survey in surveys) { List <SurveyOptionsInfo> surveyOptions = SurveyOptionsController.GetAll(survey.SurveyID); switch (survey.OptionType) { case QuestionType.RadioButtons: SurveyRadioButtons surveyRadioButtons = (SurveyRadioButtons)LoadControl(string.Format("{0}Controls/SurveyRadioButtons.ascx", ControlPath)); surveyRadioButtons.ID = string.Format("SurveyRadiobutton_{0}", survey.SurveyID); surveyRadioButtons.Label = survey.Question; surveyRadioButtons.RepeatDirection = survey.RepeatDirection; surveyRadioButtons.RepeatColumns = (((survey.RepeatColumns == null) || (survey.RepeatColumns <= 1)) ? 1 : survey.RepeatColumns.Value); surveyRadioButtons.EditUrl = EditUrl("SurveyID", survey.SurveyID.ToString()); surveyRadioButtons.IsEditable = IsEditable; surveyRadioButtons.ErrorMessage = string.Format(Localization.GetString("RadioButtonRequired.ErrorMessage", LocalResourceFile), survey.Question); surveyRadioButtons.ValidationGroup = string.Format("Survey_{0}_ValidationGroup", ModuleId); surveyRadioButtons.DataSource = surveyOptions; surveyRadioButtons.DataTextField = "OptionName"; surveyRadioButtons.DataValueField = "SurveyOptionID"; surveyRadioButtons.DataBind(); SurveyPlaceHolder.Controls.Add(surveyRadioButtons); break; case QuestionType.CheckBoxes: SurveyCheckBoxes surveyCheckBoxes = (SurveyCheckBoxes)LoadControl(string.Format("{0}Controls/SurveyCheckBoxes.ascx", ControlPath)); surveyCheckBoxes.ID = string.Format("SurveyCheckbox_{0}", survey.SurveyID); surveyCheckBoxes.Label = survey.Question; surveyCheckBoxes.RepeatDirection = survey.RepeatDirection; surveyCheckBoxes.RepeatColumns = (((survey.RepeatColumns == null) || (survey.RepeatColumns <= 1)) ? 1 : survey.RepeatColumns.Value); surveyCheckBoxes.EditUrl = EditUrl("SurveyID", survey.SurveyID.ToString()); surveyCheckBoxes.IsEditable = IsEditable; surveyCheckBoxes.ErrorMessage = string.Format(Localization.GetString("CheckBoxRequired.ErrorMessage", LocalResourceFile), survey.Question); surveyCheckBoxes.ValidationGroup = string.Format("Survey_{0}_ValidationGroup", ModuleId); surveyCheckBoxes.DataSource = surveyOptions; surveyCheckBoxes.DataTextField = "OptionName"; surveyCheckBoxes.DataValueField = "SurveyOptionID"; surveyCheckBoxes.DataBind(); SurveyPlaceHolder.Controls.Add(surveyCheckBoxes); break; case QuestionType.Text: SurveyText surveyTextBox = (SurveyText)LoadControl(string.Format("{0}Controls/SurveyText.ascx", ControlPath)); surveyTextBox.ID = string.Format("SurveyTextBox_{0}", survey.SurveyID); surveyTextBox.Label = survey.Question; surveyTextBox.NumberOfRows = (((survey.NumberOfRows.HasValue) && (survey.NumberOfRows.Value > 1)) ? survey.NumberOfRows.Value : 1); surveyTextBox.EditUrl = EditUrl("SurveyID", survey.SurveyID.ToString()); surveyTextBox.IsEditable = IsEditable; surveyTextBox.ErrorMessage = string.Format(Localization.GetString("TextBoxRequired.ErrorMessage", LocalResourceFile), survey.Question); surveyTextBox.ValidationGroup = string.Format("Survey_{0}_ValidationGroup", ModuleId); surveyTextBox.SurveyOptionID = surveyOptions[0].SurveyOptionID; SurveyPlaceHolder.Controls.Add(surveyTextBox); break; default: break; } } if (PrivacyConfirmation) { // This is DNN 9.2.2 code... string privacyUrl = Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "Privacy"); string termsUrl = Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "Terms"); // For DNN 9.3.0 use this code then... //string privacyUrl = (PortalSettings.PrivacyTabId == Null.NullInteger ? Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "Privacy") : Globals.NavigateURL(PortalSettings.PrivacyTabId)); //string termsUrl = (PortalSettings.TermsTabId == Null.NullInteger ? Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "Terms") : Globals.NavigateURL(PortalSettings.TermsTabId)); PrivacyConfirmationCheckBox privacyConfirmation = (PrivacyConfirmationCheckBox)LoadControl(string.Format("{0}Controls/PrivacyConfirmationCheckBox.ascx", ControlPath)); privacyConfirmation.ID = string.Format("PrivacyConfirmationCheckBox_{0}", ModuleId); privacyConfirmation.Label = string.Format(Localization.GetString("PrivacyConfirmation.Text", LocalResourceFile), privacyUrl, termsUrl); privacyConfirmation.ErrorMessage = Localization.GetString("PrivacyConfirmation.ErrorMessage", LocalResourceFile); privacyConfirmation.ValidationGroup = string.Format("Survey_{0}_ValidationGroup", ModuleId); SurveyPlaceHolder.Controls.Add(privacyConfirmation); } if ((UseCaptcha == UseCaptcha.Always) || ((UseCaptcha == UseCaptcha.UnauthorizedUsersOnly) && (UserId < 1))) { CaptchaControl captcha = new CaptchaControl(); captcha.ID = string.Format("Captcha_{0}", ModuleId); captcha.Text = Localization.GetString("Captcha.Text", LocalResourceFile); captcha.CaptchaLength = 8; captcha.ErrorMessage = Localization.GetString("Captcha.ErrorMessage", LocalResourceFile); captcha.CaptchaChars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789"; captcha.ErrorStyle.CssClass = "dnnFormMessage dnnFormError"; SurveyPlaceHolder.Controls.Add(captcha); } }
protected void SubmitSurveyButton_Click(object sender, EventArgs e) { // First, check CAPTCHA CaptchaControl captcha = (CaptchaControl)FindControl(string.Format("Captcha_{0}", ModuleId)); if (((captcha != null) && (captcha.IsValid)) || (captcha == null)) { // Then validate page... Page.Validate(string.Format("Survey_{0}_ValidationGroup", ModuleId)); if (Page.IsValid) { if (ContactByFaxOnlyCheckBox.Checked) { // if someone activates this checkbox send him home :-) Response.Redirect("http://localhost/"); } List <SurveysInfo> surveys = SurveysController.GetAll(ModuleId); List <SurveyResultsInfo> surveyResults = new List <SurveyResultsInfo>(); Guid resultUserID = Guid.NewGuid(); foreach (SurveysInfo survey in surveys) { SurveyResultsInfo surveyResult; switch (survey.OptionType) { case QuestionType.RadioButtons: SurveyRadioButtons surveyRadioButtons = (SurveyRadioButtons)FindControl(string.Format("SurveyRadiobutton_{0}", survey.SurveyID)); surveyResult = new SurveyResultsInfo(); surveyResult.SurveyOptionID = Convert.ToInt32(surveyRadioButtons.SelectedValue); surveyResult.UserID = (UserId < 1 ? (int?)null : UserId); surveyResult.IPAddress = Request.ServerVariables["REMOTE_ADDR"]; surveyResult.IsCorrect = SurveyOptionsController.GetAll(survey.SurveyID).Find(x => x.SurveyOptionID == surveyResult.SurveyOptionID).IsCorrect; surveyResult.ResultUserID = resultUserID; surveyResults.Add(surveyResult); break; case QuestionType.CheckBoxes: SurveyCheckBoxes surveyCheckBoxes = (SurveyCheckBoxes)FindControl(string.Format("SurveyCheckbox_{0}", survey.SurveyID)); foreach (int surveyOptionID in surveyCheckBoxes.SelectedItems) { surveyResult = new SurveyResultsInfo(); surveyResult.SurveyOptionID = surveyOptionID; surveyResult.UserID = (UserId < 1 ? (int?)null : UserId); surveyResult.IPAddress = Request.ServerVariables["REMOTE_ADDR"]; surveyResult.IsCorrect = SurveyOptionsController.GetAll(survey.SurveyID).Find(x => x.SurveyOptionID == surveyResult.SurveyOptionID).IsCorrect; surveyResult.ResultUserID = resultUserID; surveyResults.Add(surveyResult); } break; case QuestionType.Text: SurveyText surveyTextBox = (SurveyText)FindControl(string.Format("SurveyTextBox_{0}", survey.SurveyID)); surveyResult = new SurveyResultsInfo(); surveyResult.SurveyOptionID = surveyTextBox.SurveyOptionID; surveyResult.UserID = (UserId < 1 ? (int?)null : UserId); surveyResult.IPAddress = Request.ServerVariables["REMOTE_ADDR"]; surveyResult.TextAnswer = PortalSecurity.InputFilter(surveyTextBox.Text, PortalSecurity.FilterFlag.MultiLine | PortalSecurity.FilterFlag.NoAngleBrackets | PortalSecurity.FilterFlag.NoMarkup | PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoSQL); surveyResult.IsCorrect = true; surveyResult.ResultUserID = resultUserID; surveyResults.Add(surveyResult); break; default: break; } } if (PortalSecurity.IsInRole("Administrators")) { // This is just to force the SQL Script SurveyResults_Add to add the result if the user is an administrator SurveyResultsController.Add(surveyResults, false); } else { SurveyResultsController.Add(surveyResults, AuthorizedUsersOnly); } HttpCookie cookie = new HttpCookie(_cookie); cookie.Value = "True"; cookie.Expires = (SurveyClosingDate == DateTime.MinValue ? DateTime.MaxValue : SurveyClosingDate.AddDays(1)); Response.AppendCookie(cookie); SubmitSurveyButton.Visible = false; if (SurveyType == SurveyType.Survey) { SurveyPlaceHolder.Visible = false; if (HasViewResultsPermission) { Response.Redirect(EditUrl("SurveyResults"), false); } else { SurveyMessageLabel.Text = Localization.GetString("HasVoted.Text", LocalResourceFile); SurveyMessageLabel.CssClass = "dnnFormMessage dnnFormSuccess"; SurveyMessageLabel.Visible = true; } } else { SurveyMessageLabel.Text = Localization.GetString("QuizResults.Text", LocalResourceFile); SurveyMessageLabel.CssClass = "dnnFormMessage dnnFormSuccess"; SurveyMessageLabel.Visible = true; SurveyPlaceHolder.Controls.Clear(); DisplayQuizResults(surveys, surveyResults); } } } }
protected void UpdateButton_Click(object sender, EventArgs e) { if (Page.IsValid) { try { SurveysInfo survey; if (SurveyID == 0) { survey = new SurveysInfo(); } else { survey = Survey; } survey.SurveyID = SurveyID; survey.ModuleID = ModuleId; survey.Question = QuestionTextBox.Text; survey.OptionType = (QuestionType)Convert.ToInt32(QuestionTypeDropDownList.SelectedValue); survey.IsStatistical = (SurveyType == SurveyType.Quiz ? IsStatisticalCheckBox.Checked : (bool?)null); if (survey.OptionType == QuestionType.Text) { int surveyOptionID = 0; if (SurveyID > 0) { surveyOptionID = SurveyOptionsController.GetAll(SurveyID)[0].SurveyOptionID; } Answers = DummyData.LoadDummyForTextAnswer(SurveyID, surveyOptionID, UserId); } else { if (Request.Form["SurveyOptionID"] == null) { // You can't add a single or multiple choice question with no answers... ErrorMessagePanel.Visible = true; ErrorMessageLabel.Text = Localization.GetString("NoAnswersProvided.Text", LocalResourceFile); } else { //if (SurveyID > 0) //{ List <SurveyOptionsInfo> answers = Answers; int[] surveyOptionIDs = (from p in Request.Form["SurveyOptionID"].Split(',') select int.Parse(p)).ToArray(); int viewOrder = 1; foreach (int surveyOptionID in surveyOptionIDs) { SurveyOptionsInfo answer = answers.Find(x => x.SurveyOptionID == surveyOptionID); answer.ViewOrder = viewOrder; viewOrder++; } Answers = answers; //} int correctAnswers = Answers.Where(a => a.IsCorrect).Count(); if ((SurveyType == SurveyType.Quiz) && (!(IsStatisticalCheckBox.Checked)) && (correctAnswers == 0)) { ErrorMessagePanel.Visible = true; ErrorMessageLabel.Text = Localization.GetString("NoCorrectAnswersProvided.Text", LocalResourceFile); } if ((SurveyType == SurveyType.Quiz) && (!(IsStatisticalCheckBox.Checked)) && (survey.OptionType == QuestionType.RadioButtons) && (correctAnswers > 1)) { ErrorMessagePanel.Visible = true; ErrorMessageLabel.Text = Localization.GetString("OnlyOneCorrectAnswerAllowed.Text", LocalResourceFile); } } } if (!(ErrorMessagePanel.Visible)) { survey.RepeatDirection = (RepeatDirection)Convert.ToInt32(RepeatDirectionDropDownList.SelectedValue); survey.RepeatColumns = (String.IsNullOrEmpty(RepeatColumnsTextBox.Text) ? (int?)null : Convert.ToInt32(RepeatColumnsTextBox.Text)); survey.NumberOfRows = (((String.IsNullOrEmpty(NumberOfRowsTextBox.Text)) || (NumberOfRowsTextBox.Text == "1")) ? (int?)null : Convert.ToInt32(NumberOfRowsTextBox.Text)); survey.ChartType = (ChartType)Convert.ToInt32(ChartTypeDropDownList.SelectedValue); SurveysController.AddOrChange(survey, XmlDataProvider.SurveyOptionsToXml(Answers), UserId); Response.Redirect(Globals.NavigateURL(), false); } } catch (Exception ex) { Exceptions.ProcessModuleLoadException(this, ex); } } }