/// <summary> /// This method will loop through each row, scoring the quiz, then re-binding the list /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSubmitAnswers_Click(object sender, EventArgs e) { //Fast exit if invalid if (!Page.IsValid) return; var oQuizInfo = QuizController.GetQuizById(_quizId, ModuleId); //Variables for scoring var correctCount = 0; var totalCount = 0; //Clear the answer list _answers = new List<UserQuestionAnswerInfo>(); //Loop through the questions foreach (RepeaterItem oCurrentRecord in rptChallengeQuestions.Items) { //Get the controls var oQuestion = (QuizQuestionInfo) oCurrentRecord.DataItem; var hfQuestionId = (HiddenField) oCurrentRecord.FindControl("hfQuestionId"); var rblQuestions = (RadioButtonList) oCurrentRecord.FindControl("rblQuestions"); //Increment the total count totalCount++; //Add the users answer to the list var oUserInfo = new UserQuestionAnswerInfo(); oUserInfo.Answer = rblQuestions.SelectedItem.Text; oUserInfo.QuestionId = int.Parse(hfQuestionId.Value); oUserInfo.UserId = UserId; //Get the question, see if they were correct if (oQuestion == null) oQuestion = QuizController.GetQuizQuestion(oUserInfo.QuestionId, ModuleId); if (oQuestion.CorrectAnswer == oUserInfo.Answer) { oUserInfo.WasCorrect = true; correctCount++; } else oUserInfo.WasCorrect = false; //Add it to the collection _answers.Add(oUserInfo); } //Prepare results var oResults = new UserQuizResultInfo { DateTaken = DateTime.Now, IsMostCurrent = true, NumberCorrect = correctCount, NumberIncorrect = totalCount - correctCount, Percentage = Math.Round( (decimal.Parse(correctCount.ToString())/ decimal.Parse(totalCount.ToString()))*100, 2), QuizId = _quizId, ReminderSent = false, UserId = UserId, UserIpAddress = Request.UserHostAddress }; //Declare placeholder for the result it int resultId; //Did they pass if (oResults.Percentage >= oQuizInfo.PassPercentage) { oResults.Passed = true; oResults.ExpirationDate = oQuizInfo.CanExpire ? DateTime.Now.AddDays(oQuizInfo.ExpireDuration) : DateTime.Now.AddYears(10); //Now save it resultId = QuizController.SaveUserQuizResults(oResults); var certificate = Globals.NavigateURL("QuizCert", "mid=" + ModuleId, "quizId=" + UrlUtils.EncryptParameter(_quizId.ToString()), "exp=" + UrlUtils.EncryptParameter( Server.UrlEncode( oResults.ExpirationDate.ToShortDateString())), "resId=" + resultId); var noSkinQsParams = "&SkinSrc=[G]Skins%2f_default%2fNo+Skin&ContainerSrc=[G]Containers%2f_default%2fNo+Container"; //Setup pass notification var notification = new StringBuilder(Localization.GetString("PassTemplate", LocalResourceFile)); notification.Append(Localization.GetString("ReturnLinkTemplate", LocalResourceFile)); notification.Replace("[YOURSCORE]", Math.Round(oResults.Percentage, 0).ToString()); notification.Replace("[REQUIREDSCORE]", Math.Round(oQuizInfo.PassPercentage, 0).ToString()); notification.Replace("[CERTLINK]", certificate); notification.Replace("[CERTLINKPRINT]", certificate + noSkinQsParams); notification.Replace("[RETURNLINK]", Globals.NavigateURL(TabId)); lblStatus.Text = notification.ToString(); //Add to role if needed if (!oQuizInfo.RoleToAdd.Equals("-1")) { var oRoleController = new RoleController(); var oRoleToAdd = oRoleController.GetRoleByName(PortalId, oQuizInfo.RoleToAdd); if (oRoleToAdd != null) { oRoleController.AddUserRole(PortalId, UserId, oRoleToAdd.RoleID, oQuizInfo.CanExpire ? DateTime.Now.AddDays(oQuizInfo.ExpireDuration) : Null.NullDate); } else Exceptions.LogException( new ArgumentException("Unable to add role, unable to find " + oQuizInfo.RoleName + " role for assignment")); } } else { oResults.Passed = false; oResults.ExpirationDate = Null.NullDate; //Now save it resultId = QuizController.SaveUserQuizResults(oResults); //Setup fail notification var takeQuizLink = Globals.NavigateURL("TakeQuiz", "mid=" + ModuleId, "quizId=" + UrlUtils.EncryptParameter(_quizId.ToString())); var notification = new StringBuilder(Localization.GetString("FailTemplate", LocalResourceFile)); notification.Append(Localization.GetString("ReturnLinkTemplate", LocalResourceFile)); notification.Replace("[YOURSCORE]", Math.Round(oResults.Percentage, 0).ToString()); notification.Replace("[REQUIREDSCORE]", Math.Round(oQuizInfo.PassPercentage, 0).ToString()); notification.Replace("[TESTLINK]", takeQuizLink); notification.Replace("[RETURNLINK]", Globals.NavigateURL(TabId)); lblStatus.Text = notification.ToString(); //Setup the re-take link btnTakeAgain.NavigateUrl = takeQuizLink; btnTakeAgain.Visible = true; } //Hide the submit button btnSubmitAnswers.Visible = false; //Now, we need to save the question answers foreach (UserQuestionAnswerInfo oAnswer in _answers) { oAnswer.ResultId = resultId; QuizController.SaveUserQuestionAnswer(oAnswer); } //Now, flag for rebinding _questionIndex = 0; _finished = true; BindAndDisplayQuestions(_quizId); }
/// <summary> /// Saves the user question answer. /// </summary> /// <param name="oInfo">The o info.</param> public static void SaveUserQuestionAnswer(UserQuestionAnswerInfo oInfo) { DataProvider.Instance().SaveUserQuestionAnswer(oInfo.ResultId, oInfo.QuestionId, oInfo.UserId, oInfo.Answer, oInfo.WasCorrect); }