public IActionResult Quiz(string answer, int taskId) { var user = _userData.GetUserWithStats(User.GetUserId()); var task = _taskData.GetQuizTask(taskId); QuizTaskStatistic currentStat = null; foreach (var stat in user.CourseStatistics) { foreach (var statQuestionTaskStatistic in stat.QuizTaskStatistics) { if (statQuestionTaskStatistic.TaskId == taskId) { currentStat = statQuestionTaskStatistic; break; } } } if (currentStat != null) { currentStat.IsComplete = true; currentStat.CorrectAnswer = task.CorrectAnswer.Text; currentStat.UserVariant = answer; if (string.Equals(task.CorrectAnswer.Text, answer, StringComparison.CurrentCultureIgnoreCase)) { currentStat.IsCorrect = true; } else { currentStat.IsCorrect = false; } _statisticData.UpdateQuizTaskStatistic(currentStat); return(Ok(new { isCompleted = currentStat.IsComplete, rezult = currentStat.IsCorrect, answer = currentStat.CorrectAnswer, userAnswer = currentStat.UserVariant })); } return(NotFound()); }
public IActionResult CheckCompleteQuiz(int taskId) { var user = _userData.GetUserWithStats(User.GetUserId()); QuizTaskStatistic currentStat = null; foreach (var stat in user.CourseStatistics) { foreach (var quizTaskStatistic in stat.QuizTaskStatistics) { if (quizTaskStatistic.TaskId == taskId) { currentStat = quizTaskStatistic; break; } } } if (currentStat != null) { return(Ok(new { isCompleted = currentStat.IsComplete, isCorect = currentStat.CorrectAnswer == currentStat.UserVariant, answer = currentStat.CorrectAnswer, userAnswer = currentStat.UserVariant })); } return(NotFound()); }
public void UpdateQuizTaskStatistic(QuizTaskStatistic statistic) { _context.QuizTaskStatistics.Update(statistic); _context.SaveChanges(); }