public IActionResult AutoCheck(string formId) { // Try to get the quiz form Form form = _formsRepository.GetById(formId); if (form == null) { return(NotFound()); } // Loop through the answer sets foreach (FormAnswersSet answerSet in _answersRepository.GetAll(formId)) { // Get the stored scores Dictionary <string, int> scores = _scoresRepository.GetScore(formId, answerSet.Id); // Combine the data into a extended form answers model ExtendedFormAnswersSet model = AnswerChecking.ConstructExtendedFormAnswersSet(answerSet, form, scores); // Save the score if no manual checking is required if (model.ManualCheckingRequiredCount == 0) { // Extract the scores scores = model.Answers.ToDictionary(x => x.QuestionId, x => x.AssignedPoints.Value); // Update the score _scoresRepository.UpdateScore(formId, answerSet.Id, scores); } } // Redirect to the overview return(RedirectToAction("Index", new { formId })); }
public IActionResult View(string formId, Guid answersSetId) { // Try to get the quiz form Form form = _formsRepository.GetById(formId); if (form == null) { return(NotFound()); } // Try to get the answers set FormAnswersSet answerSet = _answersRepository.Get(formId, answersSetId); if (answerSet == null) { return(NotFound()); } // Try to get the existing scores Dictionary <string, int> scores = _scoresRepository.GetScore(formId, answersSetId); // Combine the data into a extended form answers model ExtendedFormAnswersSet model = AnswerChecking.ConstructExtendedFormAnswersSet(answerSet, form, scores); return(View(model)); }
public static ExtendedFormAnswersSet ConstructExtendedFormAnswersSet(FormAnswersSet answerSet, Form form, Dictionary <string, int> scores) { // Create the model, it will be updated later ExtendedFormAnswersSet model = new ExtendedFormAnswersSet(answerSet); // Loop through the questions of the form foreach (Question question in form.Questions) { // Increase the total points counter model.TotalPoints += question.Points; // Get the given answer for the question if any string given = null; if (answerSet.Answers.ContainsKey(question.Id)) { given = answerSet.Answers[question.Id]; } // Try to get the existing score; // Try to perform an automated check otherwise. int?score; if (scores != null && scores.ContainsKey(question.Id)) { score = scores[question.Id]; } else { score = TryCheckAnswer(question, given); } // Add the score to the total points if it's set; // Otherwise, increase the manual checking required. if (score.HasValue) { model.Points += score.Value; } else { model.ManualCheckingRequiredCount++; } // Add the answer to the list model.Answers.Add(new ExtendedAnswer(question, given, score)); } return(model); }