/// <summary> /// Answers specified quiz question. /// </summary> /// <param name="question">The question to answer.</param> /// <param name="answerTemplateId">The answer template identifier.</param> public void AnswerQuestion(Question question, int answerTemplateId) { ThrowIf.Null(question, nameof(question)); ThrowIf.Completed(question.Quiz); if (question.IsAnswered) { throw new QuizFlowException( QuizFlowErrorCodes.QuestionAlreadyAnswered, "Question already answered."); } QuestionTemplate questionTemplate = this.Uow.QuestionTemplateRepository .GetByID(question.TemplateId); ThrowIf.NotFound(questionTemplate, question.TemplateId); AnswerTemplate answerTemplate = questionTemplate.GetAnswer(answerTemplateId); ThrowIf.NotFound(answerTemplate, answerTemplateId); var answer = new Answer() { TemplateId = answerTemplateId, IsCorrect = answerTemplate.IsCorrect }; question.Answers.Add(answer); question.DateEnd = DateTime.Now; this.Uow.Save(); }
/// <summary> /// Completes the quiz. /// </summary> /// <param name="quiz">The quiz to complete.</param> public void CompleteQuiz(Quiz quiz) { ThrowIf.Null(quiz, nameof(quiz)); ThrowIf.Completed(quiz); IScoreCalculationStrategy scoreCalculation = this.ScoreCalculationFactory.CreateStrategy(quiz); var score = scoreCalculation.CalculateScore(quiz); this.Uow.ScoreRepository.Insert(score); quiz.DateEnd = DateTime.Now; this.Uow.Save(); }
/// <summary> /// Gets next question of the specified quiz. /// </summary> /// <param name="quizId">The quiz to get question from.</param> /// <returns>Quiz flow command to manage quiz flow.</returns> public QuizFlowCommandContract GetNextQuestion(Quiz quiz) { ThrowIf.Null(quiz, nameof(quiz)); ThrowIf.Completed(quiz); Question currentQuestion = quiz.CurrentQuestion; QuizFlowCommandContract command; if (currentQuestion != null && !currentQuestion.IsAnswered) { command = this.GetCommandFromCurrentQuestion(quiz, currentQuestion); } else { command = this.CreateCommandFromNextQuestion(quiz, currentQuestion); } this.Uow.Save(); command.HideAnswerCorrectness(); return(command); }