/// <summary> /// The get quiz data by quiz id. /// </summary> /// <param name="quizId"> /// The quiz id. /// </param> /// <returns> /// The <see cref="QuizDataDTO"/>. /// </returns> public QuizDataDTO getQuizDataByQuizID(int quizId) { var result = new QuizDataDTO { quizVO = new QuizDTO(this.GetOneById(quizId).Value) }; if (result.quizVO != null && result.quizVO.subModuleItemId.HasValue) { result.questions = this.GetQuizQuestionsBySMIId(result.quizVO.subModuleItemId.Value).ToArray(); result.distractors = this.GetQuizDistractorsBySMIId(result.quizVO.subModuleItemId.Value).ToArray(); } return(result); }
public QuizDataDTO getQuizDataByQuizGuid(Guid key) { var queryOver = new DefaultQueryOver <Quiz, int>().GetQueryOver().Where(x => x.Guid == key).Take(1); var quiz = Repository.FindOne(queryOver).Value; var result = new QuizDataDTO { quizVO = new QuizDTO(quiz) }; if (result.quizVO != null && result.quizVO.subModuleItemId.HasValue) { result.questions = this.GetQuizQuestionsBySMIId(result.quizVO.subModuleItemId.Value).ToArray(); result.distractors = this.GetQuizDistractorsBySMIId(result.quizVO.subModuleItemId.Value).ToArray(); } return(result); }
private int SaveQuestionResult(OfflineQuizAnswerDTO[] answers, QuizDataDTO quizData, QuizResult postQuizResult) { var score = 0; foreach (var answer in answers) { var questionResult = new QuizQuestionResult(); var question = quizData.questions.First(x => x.questionId == answer.questionId); //var distractors = quizData.questions.Where(x => x.questionId == answer.questionId).ToList(); var distractors = DistractorModel.GetAllByQuestionId(answer.questionId).ToList(); questionResult.Question = question.question; var questionObj = QuestionModel.GetOneById(question.questionId).Value; questionResult.QuestionRef = questionObj; questionResult.QuestionType = questionObj.QuestionType; questionResult.QuizResult = postQuizResult; if (answer.trueFalseAnswer != null) { var shouldBeOneDistractor = distractors.FirstOrDefault(); if (shouldBeOneDistractor == null) { throw new InvalidOperationException("There should be a distractor for true/false question"); } var isCorrect = answer.trueFalseAnswer.answer == (shouldBeOneDistractor.IsCorrect ?? false); if (isCorrect) { score++; } questionResult.IsCorrect = isCorrect; QuizQuestionResultModel.RegisterSave(questionResult, true); } if (answer.singleChoiceAnswer != null) { var quizDistractorAnswer = DistractorModel.GetOneById(answer.singleChoiceAnswer.answeredDistractorId).Value; var quizQuestionResultAnswer = new QuizQuestionResultAnswer { QuizQuestionResult = questionResult, Value = quizDistractorAnswer.DistractorName, QuizDistractorAnswer = quizDistractorAnswer, }; var distractor = distractors.FirstOrDefault(x => x.Id == quizDistractorAnswer.Id); if (distractor == null) { throw new InvalidOperationException("How come answered distractor id is not present in question distractors???"); } var isCorrect = distractor.IsCorrect ?? false; if (isCorrect) { score++; } questionResult.IsCorrect = isCorrect; QuizQuestionResultModel.RegisterSave(questionResult, true); QuizQuestionResultAnswerModel.RegisterSave(quizQuestionResultAnswer); } if (answer.multiChoiceAnswer != null) { var correctDistractors = distractors.Where(x => x.IsCorrect ?? false).Select(x => x.Id); var isCorrect = correctDistractors.SequenceEqual(answer.multiChoiceAnswer.answeredDistractorIds); if (isCorrect) { score++; } questionResult.IsCorrect = isCorrect; QuizQuestionResultModel.RegisterSave(questionResult, true); foreach (var answeredDistractorId in answer.multiChoiceAnswer.answeredDistractorIds) { var distrator = distractors.First(x => x.Id == answeredDistractorId); var quizQuestionResultAnswer = new QuizQuestionResultAnswer { QuizQuestionResult = questionResult, Value = distrator.DistractorName, QuizDistractorAnswer = distrator, }; QuizQuestionResultAnswerModel.RegisterSave(quizQuestionResultAnswer); } } } return(score); }