public ActionResult TakeTest(int id) { var testTmp = testFacade.GetTestTemplateById(id); var time2 = testTmp.Date + testTmp.CompletionTime; if (DateTime.Now > time2 || DateTime.Now < testTmp.Date) { TempData["error"] = "Cannot start the test!"; return(RedirectToAction("Index")); } var que = new QuestionFacade().GetNumOfRandQuestionsFromThematicAreas(testTmp.NumOfQuestions, testTmp.ThematicAreas); var answs = que.SelectMany(s => s.Answers) .Select(tmp => new CheckboxModel { Id = tmp.Id, Selected = false }) .ToArray(); var model = new TestActiveModel { Id = testTmp.Id, Name = testTmp.Name, CompletionTime = testTmp.CompletionTime, Date = testTmp.Date, NumOfQuestions = testTmp.NumOfQuestions, Questions = que, Answers = answs }; return(View(model)); }
public ActionResult TakeTest(int id, TestActiveModel testActive) { var tmp = testActive.Answers .ToDictionary(checkboxModel => checkboxModel.Id, checkboxModel => checkboxModel.Selected); var str = new StringBuilder(); double respts = 0; foreach (var questionDto in testActive.Questions) { var correctResult = 0; var correct = 0; var wrong = 0; var question = questionFacade.GetQuestionById(questionDto.Id); foreach (var answerDto in question.Answers) { if (answerDto.Correct && tmp.FirstOrDefault(s => s.Key == answerDto.Id).Value == answerDto.Correct) { correctResult++; } else if (!answerDto.Correct && tmp.FirstOrDefault(s => s.Key == answerDto.Id).Value) { wrong++; } if (answerDto.Correct) { correct++; } } if (correct == 0) { continue; } var count = (double)(correctResult - wrong) / correct; if (count < 0) { count = 0; } count *= question.Points; respts += (Math.Round((double)(count * 4), MidpointRounding.ToEven) / 4); } foreach (var b in tmp) { str.Append(b.Key + "," + b.Value + ";"); } resultFacade.CreateResult(new ResultDTO { TestTemplateId = id, UserId = Convert.ToInt32(User.Identity.GetUserId()), ResultString = str.ToString(), ResultPoints = respts, TestTemplateName = testActive.Name, Time = DateTime.Now }); return(RedirectToAction("Index")); }