Ejemplo n.º 1
0
        public async Task <IActionResult> QuizProcess([FromForm] GeneratedQuizModel model)
        {
            var quizDto = await _dbContext.UserQuizzes
                          .Where(e => e.Id == model.Id)
                          .Include(e => e.UserAnswers)
                          .FirstOrDefaultAsync()
                          .ConfigureAwait(false);

            if (quizDto == null)
            {
                return(StatusCode(404, $"Тест с заданным идентификатором ({model.Id}) не найден."));
            }

            quizDto.UserAnswers = new List <UserAnswerDto>();
            quizDto.IsEnded     = true;
            quizDto.EndTime     = DateTime.Now;

            if (model.SourceQuiz.QuizBlocks != null)
            {
                foreach (var quizBlock in model.SourceQuiz.QuizBlocks)
                {
                    var userAnswerString = "";

                    if (quizBlock.UserAnswer != null)
                    {
                        foreach (var userAnswer in quizBlock.UserAnswer)
                        {
                            if (!string.IsNullOrWhiteSpace(userAnswerString))
                            {
                                userAnswerString += Environment.NewLine;
                            }

                            int userAnswerInt;
                            if (int.TryParse(userAnswer, out userAnswerInt))
                            {
                                var answerBlock = quizBlock.Answers[userAnswerInt];
                                userAnswerString += answerBlock.Id;
                            }
                            else
                            {
                                userAnswerString += userAnswer;
                            }
                        }
                    }

                    quizDto.UserAnswers.Add(new UserAnswerDto
                    {
                        GeneratedQuizId = model.Id,
                        QuizBlockId     = quizBlock.Id,
                        UserAnswer      = userAnswerString,
                    });
                }
            }

            _dbContext.UserQuizzes.Update(quizDto);
            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

            return(RedirectToAction("Results", "Testing", new { token = quizDto.Tag }));
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Выполняет перемешивание вопросов и ответов в тесте.
        /// </summary>
        /// <param name="quiz">Исходный тест.</param>
        /// <returns>Тест с перемешанными данными.</returns>
        public static GeneratedQuizModel ShuffleQuizData(GeneratedQuizModel quiz)
        {
            var rnd = new Random(quiz.StartTime.Millisecond);

            quiz.SourceQuiz.QuizBlocks = quiz.SourceQuiz.QuizBlocks.Shuffle(rnd).ToList();
            foreach (var quizBlock in quiz.SourceQuiz.QuizBlocks)
            {
                quizBlock.Answers = quizBlock.Answers.Shuffle(rnd).ToList();
            }

            return(quiz);
        }