public QuizQuestionModel GetRandomQuestionModel(Int32 userID, Int32 quizID) { QuizQuestionModel model = new QuizQuestionModel(); StatsRepository statsRepository = new StatsRepository(userID, quizID); QuizQuestion question; List<QuizOption> questionOptions; List<Int32> answeredIDs; answeredIDs = context.QuizAnswer.Where(y => y.UserID == userID) .Select(y => y.AnsweredOption.QuizQuestion.QuizQuestionID).ToList(); question = context.QuizQuestion .Where(x => !answeredIDs.Contains(x.QuizQuestionID)) .OrderBy(x => Guid.NewGuid()).Take(1).FirstOrDefault(); if (question != null) { questionOptions = question.Options.ToList(); model.StatsModel = statsRepository.FillStatsModel(); model.Options = new List<QuizOptionModel>(); model.Caption = question.Text; model.ID = question.QuizQuestionID; questionOptions.ForEach(x => model.Options.Add(new QuizOptionModel() { ID = x.QuizOptionID.ToString(), Text = x.Text })); } else { model.IsQuizFinished = true; } return model; }
public ActionResult Question(Int32? quizID, Int32? questionID) { QuizQuestionModel model = new QuizQuestionModel(); QuizRepository repository = new QuizRepository(); if(quizID.HasValue) { HttpCookie cookie = new HttpCookie("QuizID"); cookie.Value = quizID.Value.ToString(); this.ControllerContext.HttpContext.Response.Cookies.Add(cookie); } else if(this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("QuizID")) { quizID = Convert.ToInt32(this.ControllerContext.HttpContext.Request.Cookies["QuizID"].Value); } if(questionID.HasValue) { //Do we even need this?? } else { model = repository.GetRandomQuestionModel(WebSecurity.GetUserId(User.Identity.Name), quizID.Value); } if (model.IsQuizFinished) return RedirectToAction("QuizComplete", "QuizController"); return View(model); }
public ActionResult CheckAnswer(Int32 ID, FormCollection collection) { QuizQuestionModel model = new QuizQuestionModel(); QuizRepository repository = new QuizRepository(); model = repository.CheckAnswer(ID, collection, WebSecurity.GetUserId(User.Identity.Name)); return View(model); }
public QuizQuestionModel CheckAnswer(Int32 questionID, FormCollection collection, Int32 userID) { QuizQuestionModel model = new QuizQuestionModel(); QuizQuestion question; question = context.QuizQuestion.FirstOrDefault(x => x.QuizQuestionID == questionID); if (question == null) return model; model.Options = new List<QuizOptionModel>(); model.Caption = question.Text; model.ID = question.QuizQuestionID; String[] answeredOptions = collection.AllKeys.Where(x => x.IsNumeric()).ToArray(); foreach (String option in answeredOptions) { QuizAnswer qa = new QuizAnswer(); qa.UserID = userID; Int32 value = Convert.ToInt32(option); qa.AnsweredOption = context.QuizOption.FirstOrDefault(x => x.QuizOptionID == value); context.QuizAnswer.Add(qa); } context.SaveChanges(); List<QuizOption> options = question.Options.ToList(); foreach (QuizOption op in options) { QuizOptionModel optionModel = new QuizOptionModel(); optionModel.ID = op.QuizOptionID.ToString(); optionModel.Text = op.Text; optionModel.IsCheckedByUser = answeredOptions.Contains(op.QuizOptionID.ToString()); optionModel.AnswerType = QuizEnums.CheckType.Neutral; if (op.IsCorrectAnswer && optionModel.IsCheckedByUser.Value) { optionModel.AnswerType = QuizEnums.CheckType.CorrectCheck; } if ( (op.IsCorrectAnswer && !answeredOptions.Contains(optionModel.ID)) || (!op.IsCorrectAnswer && answeredOptions.Contains(optionModel.ID)) ) { optionModel.AnswerType = QuizEnums.CheckType.NotCorrectCheck; } model.Options.Add(optionModel); } return model; }