示例#1
0
        public ActionResult UserTestAnswers(int userTestId)
        {
            UserTestService.LoadWith(x => x.Test);
            var userTest = UserTestService.GetByPK(userTestId);

            if (!userTest.ShowAnswers ||
                userTest.UserId != User.UserID)
            {
                return(null);
            }
            var qIds = UserTestAnswerService.GetAll(x => x.UserTestId == userTestId && !x.IsRight)
                       .Select(x => x.QuestionId).ToList();

            TestQuestionService.LoadWith(x => x.TestAnswers);
            var model = new UserTestAnswerVM {
                Questions = TestQuestionService.GetAll(x => qIds.Contains(x.Id)).ToList(),
                UserTest  = userTest
            };

            return(BaseViewWithModel(new UserTestAnswersView(), model));
        }
示例#2
0
        private Dictionary <int, bool> ProcessAnswers(UserTest userTest, TestResultData model,
                                                      List <TestQuestion> questions)
        {
            var userTestId = userTest.Id;
            var answers    = new Dictionary <int, bool>();
            var answerList = new List <UserTestAnswer>();

            if (model.Data == null)
            {
                Logger.Exception(new Exception("model.Data == null"), User);
            }
            foreach (var questionAnswer in model.Data)
            {
                var question = questions.First(x => x.Id == questionAnswer.QuestionId);
                var isRight  = IsRight(question, questionAnswer);
                answers.Add(questionAnswer.QuestionId, isRight);
                var answer = new UserTestAnswer {
                    QuestionId = questionAnswer.QuestionId,
                    IsRight    = isRight,
                    UserTestId = userTestId,
                    Answer     = questionAnswer.GetText(),
                };
                answerList.Add(answer);
            }

            if (userTest.IsCoursePlanned || userTest.ShowAnswers)
            {
                UserTestAnswerService.EnableTracking();
                foreach (var userTestAnswer in answerList.Where(x => !x.Answer.IsEmpty()))
                {
                    UserTestAnswerService.Insert(userTestAnswer);
                }
                UserTestAnswerService.SubmitChanges();
            }

            return(answers);
        }