private void GetDragAndDropResult(Question _question, DragAndDropAnswer dndAnswer, ref uint count) { var question = _question as DragAndDropQuestion; _context.Entry(question).Collection(x => x.Options).Load(); _context.Entry(dndAnswer).Collection(x => x.DragAndDropAnswerOptions).Load(); if (dndAnswer.DragAndDropAnswerOptions == null || dndAnswer.DragAndDropAnswerOptions.Count == 0) { dndAnswer.Result = null; dndAnswer.Score = 0; } else { var dndOptions = dndAnswer.DragAndDropAnswerOptions; int optionsCount = dndOptions.Count(), wrongOrderCount = 0; foreach (var dndOption in dndOptions) { if (dndOption.RightOptionId != dndOption.OptionId) { wrongOrderCount++; } } var score = question.Score * (optionsCount - wrongOrderCount) / (float)optionsCount; dndAnswer.Score = score > 0 ? score : 0; if (Math.Abs(dndAnswer.Score - question.Score) < EPSILON) { count++; dndAnswer.Result = AnswerResult.Right; } else { dndAnswer.Result = score == 0 ? AnswerResult.Wrong : AnswerResult.PartiallyRight; } } _context.Update(dndAnswer); }
public async Task <IActionResult> Start(int testResultId, ErrorViewModel model) { var user = await _userManager.GetUserAsync(HttpContext.User); var testResult = await _context.TestResults .Include(tr => tr.Test) .ThenInclude(t => t.Questions) .SingleAsync(t => t.Id == testResultId); if (testResult == null) { return(NotFound()); } if (!testResult.Test.IsEnabled) { return(Forbid()); } var questions = testResult.Test.Questions.Where(q => !q.IsDeleted).ToList(); if (questions.Count() == 0) { return(NotFound()); } if (_context.Answers.Any(a => a.TestResult == testResult)) { return(RedirectToAction("Answer", "Answer", new { testResultId = testResult.Id, answerId = _context.Answers.Where(a => a.TestResult == testResult) .SingleOrDefault(a => a.Order == 1).Id })); } var answers = new List <Answer>(); Answer answer = null; if (testResult.Test.Count != 0 && testResult.Test.Count < questions.Count && testResult.Test.Shuffled) { var order = new ushort[testResult.Test.Count]; for (var i = 0; i < order.Length; i++) { order[i] = (ushort)(i + 1); } if (testResult.Test.Shuffled) { Shuffle(order); } var j = 0; Shuffle(questions); for (var k = 0; k < order.Length; k++) { var question = questions[k]; switch (question.QuestionType) { case "SingleChoiceQuestion": answer = new SingleChoiceAnswer(); break; case "MultiChoiceQuestion": answer = new MultiChoiceAnswer(); break; case "TextQuestion": answer = new TextAnswer(); break; case "DragAndDropQuestion": answer = new DragAndDropAnswer(); break; case "CodeQuestion": answer = new CodeAnswer(); break; } if (answer == null) { throw new NullReferenceException(); } answer.Result = null; answer.Question = question; answer.Score = 0; answer.TestResult = testResult; answer.Order = order[j++]; await _context.Answers.AddAsync(answer); answers.Add(answer); await _context.SaveChangesAsync(); } } else { var order = new ushort[questions.Count()]; for (var i = 0; i < order.Length; i++) { order[i] = (ushort)(i + 1); } if (testResult.Test.Shuffled) { Shuffle(order); } var j = 0; foreach (var question in questions) { switch (question.QuestionType) { case "SingleChoiceQuestion": answer = new SingleChoiceAnswer(); break; case "MultiChoiceQuestion": answer = new MultiChoiceAnswer(); break; case "TextQuestion": answer = new TextAnswer(); break; case "DragAndDropQuestion": answer = new DragAndDropAnswer(); break; case "CodeQuestion": answer = new CodeAnswer(); break; } if (answer == null) { throw new NullReferenceException(); } answer.Question = question; answer.Score = 0; answer.TestResult = testResult; answer.Order = order[j++]; await _context.Answers.AddAsync(answer); answers.Add(answer); await _context.SaveChangesAsync(); } } testResult.StartedOn = DateTime.UtcNow; _context.TestResults.Update(testResult); await _context.SaveChangesAsync(); // TODO: redirect to first answer (question) //throw new NotImplementedException(); return(RedirectToAction("Answer", "Answer", new { testResultId = testResult.Id, answerId = answers.SingleOrDefault(a => a.Order == 1).Id })); }
private void StartTest(User user, TestResult testResult, Test test) { var answers = new List <Answer>(); Answer answer = null; var questions = testResult.Test.Questions.Where(q => !q.IsDeleted).ToList(); testResult.TotalQuestions = (uint)questions.Count; if (test.Count != 0 && test.Count < test.Questions.Count && testResult.Test.Shuffled) { var order = new ushort[testResult.Test.Count]; for (var i = 0; i < order.Length; i++) { order[i] = (ushort)(i + 1); } var j = 0; for (var k = 0; k < order.Length; k++) { var question = questions[k]; switch (question.QuestionType) { case "SingleChoiceQuestion": answer = new SingleChoiceAnswer(); break; case "MultiChoiceQuestion": answer = new MultiChoiceAnswer(); break; case "TextQuestion": answer = new TextAnswer(); break; case "DragAndDropQuestion": answer = new DragAndDropAnswer(); break; case "CodeQuestion": answer = new CodeAnswer(); break; } if (answer == null) { throw new NullReferenceException(); } answer.Result = null; answer.Question = question; answer.Score = 0; answer.TestResult = testResult; answer.Order = order[j++]; _context.Answers.Add(answer); answers.Add(answer); _context.SaveChanges(); } } else { var order = new ushort[questions.Count()]; for (var i = 0; i < order.Length; i++) { order[i] = (ushort)(i + 1); } var j = 0; foreach (var question in questions) { switch (question.QuestionType) { case "SingleChoiceQuestion": answer = new SingleChoiceAnswer(); break; case "MultiChoiceQuestion": answer = new MultiChoiceAnswer(); break; case "TextQuestion": answer = new TextAnswer(); break; case "DragAndDropQuestion": answer = new DragAndDropAnswer(); break; case "CodeQuestion": answer = new CodeAnswer(); break; } if (answer == null) { throw new NullReferenceException(); } answer.Question = question; answer.Score = 0; answer.TestResult = testResult; answer.Order = order[j++]; _context.Answers.Add(answer); answers.Add(answer); _context.SaveChanges(); } } testResult.StartedOn = DateTime.UtcNow; _context.TestResults.Update(testResult); _context.SaveChanges(); }