public async Task <IActionResult> Results([FromQuery] string token) { var generatedQuiz = await _dbContext.UserQuizzes .Where(e => e.Tag == token) .Include(e => e.SourceQuiz) .ThenInclude(e => e.QuizBlocks) .ThenInclude(e => e.Question) .Include(e => e.SourceQuiz) .ThenInclude(e => e.QuizBlocks) .ThenInclude(e => e.Answers) .Include(e => e.UserAnswers) .FirstOrDefaultAsync().ConfigureAwait(false); if (generatedQuiz == null) { return(StatusCode(404, $"Тест с заданным идентификатором ({token}) не найден.")); } var generatedQuizIsEnded = DateTime.Now > generatedQuiz.StartTime.Add(TimeSpan.FromSeconds(generatedQuiz.SourceQuiz.TotalTimeSecs)) || generatedQuiz.IsEnded; if (!generatedQuizIsEnded) { return(RedirectToAction("QuizProcess", new { token })); } if (!generatedQuiz.IsEnded) { generatedQuiz.IsEnded = true; generatedQuiz.EndTime = generatedQuiz.StartTime.AddSeconds(generatedQuiz.SourceQuiz.TotalTimeSecs); _dbContext.UserQuizzes.Update(generatedQuiz); await _dbContext.SaveChangesAsync().ConfigureAwait(false); } var model = new TestResultsViewModel(); model.TestTitle = generatedQuiz.SourceQuiz.Title; model.StartTime = generatedQuiz.StartTime; model.CorrectAnswersCount = 0; if (generatedQuiz.SourceQuiz.MaxQuizBlocksCount > 0 && generatedQuiz.SourceQuiz.MaxQuizBlocksCount <= generatedQuiz.SourceQuiz.QuizBlocks.Count) { if (generatedQuiz.SourceQuiz.ShowResultsToUser) { model.QuizBlocks = TranslateSessionResultModel(generatedQuiz).ToList(); var tempQuiz = Translation.Translate(generatedQuiz); tempQuiz = CommonHelpers.ShuffleQuizData(tempQuiz); var quizBlockIds = tempQuiz.SourceQuiz.QuizBlocks.Take(tempQuiz.SourceQuiz.MaxQuizBlocks).Select(e => e.Id).ToList(); model.QuizBlocks = model.QuizBlocks.Where(e => quizBlockIds.Contains(e.BlockId)).ToList(); } model.QuestionCount = generatedQuiz.SourceQuiz.MaxQuizBlocksCount; } else { model.QuestionCount = generatedQuiz.SourceQuiz.QuizBlocks.Count; } foreach (var quizBlock in generatedQuiz.SourceQuiz.QuizBlocks) { var quizUserAnswer = generatedQuiz.UserAnswers.FirstOrDefault(e => e.QuizBlockId == quizBlock.Id); if (quizUserAnswer == null) { continue; } if (CommonHelpers.CheckQuizAnswer(quizBlock, quizUserAnswer)) { model.CorrectAnswersCount++; } } return(View(model)); }
public async Task <IActionResult> ShowResults(ShowResultsViewModel model) { await UpdateQuizStatuses().ConfigureAwait(false); var generatedQuizIds = model.Answers.Where(e => e.IsSelected).Select(e => e.GeneratedQuizId).ToList(); var generatedQuizzes = await _dbContext.UserQuizzes .Where(e => generatedQuizIds.Contains(e.Id)) .Include(e => e.RespondentUser) .Include(e => e.SourceQuiz) .ThenInclude(e => e.QuizBlocks) .ThenInclude(e => e.Question) .Include(e => e.SourceQuiz) .ThenInclude(e => e.QuizBlocks) .ThenInclude(e => e.Answers) .Include(e => e.UserAnswers) .ToListAsync().ConfigureAwait(false); var resultSb = new StringBuilder(); resultSb.Append("Пользователь;").Append("Правильных ответов;").Append("Всего вопросов;").Append("% правильных;").AppendLine(); foreach (var generatedQuiz in generatedQuizzes) { var questionCount = generatedQuiz.SourceQuiz.MaxQuizBlocksCount > 0 && generatedQuiz.SourceQuiz.MaxQuizBlocksCount <= generatedQuiz.SourceQuiz.QuizBlocks.Count ? generatedQuiz.SourceQuiz.MaxQuizBlocksCount : generatedQuiz.SourceQuiz.QuizBlocks.Count; var correctAnswerCount = 0; foreach (var quizBlock in generatedQuiz.SourceQuiz.QuizBlocks) { var quizUserAnswer = generatedQuiz.UserAnswers.FirstOrDefault(e => e.QuizBlockId == quizBlock.Id); if (quizUserAnswer == null) { continue; } if (CommonHelpers.CheckQuizAnswer(quizBlock, quizUserAnswer)) { correctAnswerCount++; } } var correctPercent = correctAnswerCount * 100 / questionCount; resultSb .Append(generatedQuiz.RespondentUser.Name).Append(";") .Append(correctAnswerCount).Append(";") .Append(questionCount).Append(";") .Append(correctPercent).Append(";") .AppendLine(); } var fileName = $"QuizResults_{model.SourceQuizId}_{DateTimeOffset.Now.ToUnixTimeSeconds()}.csv"; var stream = new MemoryStream(); var csvWriter = new StreamWriter(stream, Encoding.UTF8) { AutoFlush = true }; await csvWriter.WriteAsync(resultSb.ToString()).ConfigureAwait(false); stream.Seek(0, SeekOrigin.Begin); return(File(stream, "text/csv", fileName)); }