Beispiel #1
0
        private void CreateAnswers(QuizQuestionResultDTO dto, QuizQuestionResult result)
        {
            foreach (var answerString in dto.answers)
            {
                var answer = new QuizQuestionResultAnswer();
                try
                {
                    answer.QuizQuestionResult = result;
                    answer.Value = answerString;

                    switch (result.QuestionType.Id)
                    {
                    case (int)QuestionTypeEnum.SingleMultipleChoiceText:
                        if (int.TryParse(answerString, out var distractorId))
                        {
                            var distractor = result.QuestionRef.Distractors.FirstOrDefault(x => x.Id == distractorId);
                            if (distractor != null)
                            {
                                answer.QuizDistractorAnswer = distractor;
                                answer.Value = distractor.DistractorName;
                            }
                            else
                            {
                                answer.Value = answerString;
                            }
                        }
                        else
                        {
                            answer.Value = answerString;
                        }

                        break;

                    default:
                        answer.Value = answerString;
                        break;
                    }

                    QuizQuestionResultAnswerModel.RegisterSave(answer);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("Saving answers:" + ex);
                }
            }
        }
Beispiel #2
0
        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);
        }