예제 #1
0
        public ActionResult AnswerQuestion(Question question)
        {
            Option optionSelected = null;

            try
            {
                optionSelected = Newtonsoft.Json.JsonConvert.DeserializeObject <Option>(question.SelectedOptionJSON);
            }
            catch (Exception ex)
            {
                return(Redirect(Request.UrlReferrer.ToString()));
            }

            if (optionSelected == null)
            {
                return(new EmptyResult());
            }

            var parentQuestion = ViewModel.Exam.Questions.FirstOrDefault(x => x.Id == optionSelected.ParentQuestionId) ??
                                 ViewModel.Exam.Questions.SelectMany(t => t.Options)
                                 .SelectMany(x => x.Questions)
                                 .FirstOrDefault(x => x.Id == optionSelected.ParentQuestionId);


            var answer = new Answer {
                AnswerText = optionSelected.Text, Exam = ViewModel.Exam, Id = Guid.NewGuid(), Level = parentQuestion.Level, Option = optionSelected, ScorePoint = parentQuestion.ScorePoint, Text = optionSelected.Text
            };

            _examService.AddAnswer(answer);

            parentQuestion.SelectedOption = optionSelected;

            if (ViewModel.Exam.Questions.All(x => x.SelectedOption != null))
            {
                return(CompleteExamInternal());
            }

            var nextUnanweredQuestion = ViewModel.Exam.Questions.Where(x => x.SelectedOption == null && !x.IsUnsure).OrderBy(x => x.Sequence).FirstOrDefault() ?? ViewModel.Exam.Questions.Where(x => x.SelectedOption == null).OrderBy(x => x.Sequence).FirstOrDefault();


            ViewModel.Exam.CurrentQuestionNumber = (nextUnanweredQuestion != null) ? nextUnanweredQuestion.Sequence : 1;

            //return Redirect(Request.UrlReferrer.ToString());
            return(RedirectToAction("Exam", "Exam", new { questionNumber = ViewModel.Exam.CurrentQuestionNumber }));
        }