public async Task <IActionResult> QuizStart([FromForm] QuizStartViewModel model) { var requiredQuiz = await _dbContext.Quizzes .Where(e => e.Id == model.QuizId) .FirstOrDefaultAsync() .ConfigureAwait(false); if (requiredQuiz == null) { return(StatusCode(404, $"Тест с заданным идентификатором ({model.QuizId}) не найден.")); } var currentUser = await _userManager.GetUserAsync(HttpContext.User); if (currentUser == null) { ModelState.AddModelError("UnknownUser", "Невозможно определить пользователя в этой сессии"); return(View(model)); } var generatedQuiz = new GeneratedQuizDto { Tag = Guid.NewGuid().ToString().Replace("-", string.Empty), StartTime = DateTime.Now, SourceQuiz = requiredQuiz, RespondentUser = currentUser, UserAnswers = new List <UserAnswerDto>() }; await _dbContext.UserQuizzes.AddAsync(generatedQuiz).ConfigureAwait(false); await _dbContext.SaveChangesAsync().ConfigureAwait(false); return(RedirectToAction("QuizProcess", "Testing", new { token = generatedQuiz.Tag })); }
/// <summary> /// Выполняет преобразование <see cref="GeneratedQuizDto"/> в <see cref="GeneratedQuizModel"/>. /// </summary> /// <param name="dto">Исходный объект.</param> /// <returns>Преобразованный объект.</returns> public static GeneratedQuizModel Translate(GeneratedQuizDto dto) { var model = new GeneratedQuizModel { Id = dto.Id, StartTime = dto.StartTime, SourceQuiz = Translate(dto.SourceQuiz), }; return(model); }
/// <summary> /// Выполняет преобразование результатов теста в модель. /// </summary> /// <param name="dto">DTO результата тестирования.</param> /// <returns>Перечисление моделей блоков теста.</returns> private IEnumerable <ResultQuizBlockViewModel> TranslateSessionResultModel(GeneratedQuizDto dto) { foreach (var quizBlockDto in dto.SourceQuiz.QuizBlocks) { var quizBlockModel = new ResultQuizBlockViewModel(); quizBlockModel.BlockId = quizBlockDto.Id; quizBlockModel.Question = new ResultQuestionBlockViewModel(); quizBlockModel.Question.Text = quizBlockDto.Question.Text; quizBlockModel.Question.QuestionType = quizBlockDto.Question.QuestionType; foreach (var answerBlockDto in quizBlockDto.Answers) { var answerBlockModel = new ResultAnswerBlockViewModel(); answerBlockModel.Text = answerBlockDto.Text; answerBlockModel.AnswerType = answerBlockDto.AnswerType; answerBlockModel.IsCorrect = answerBlockDto.IsCorrect; answerBlockModel.IsAnswered = false; quizBlockModel.Answers.Add(answerBlockModel); var userAnswerDto = dto.UserAnswers.FirstOrDefault(e => e.QuizBlockId == quizBlockDto.Id); if (userAnswerDto != null) { var userAnswerList = userAnswerDto.UserAnswer.Split(new[] { Environment.NewLine }, StringSplitOptions.None); switch (answerBlockDto.AnswerType) { case AnswerBlockType.Text: var firstUserAnswer = userAnswerList.First().Trim(); var firstBlockAnswer = answerBlockDto.Text.Trim(); answerBlockModel.IsAnswered = firstBlockAnswer.Equals(firstUserAnswer, StringComparison.InvariantCultureIgnoreCase); answerBlockModel.Text += " / " + firstUserAnswer; break; case AnswerBlockType.Checkbox: case AnswerBlockType.Radio: answerBlockModel.IsAnswered = userAnswerList.Contains(answerBlockDto.Id.ToString()); break; default: throw new ArgumentOutOfRangeException(nameof(answerBlockDto.AnswerType)); } } answerBlockModel.AdditionalStyle = answerBlockModel.IsCorrect == answerBlockModel.IsAnswered ? answerBlockModel.IsCorrect ? "is-valid" : "" : "is-invalid"; } yield return(quizBlockModel); } }