コード例 #1
0
        public ActionResult Pass(long id)
        {
            var test = testsRepo.FindOne(new ByIdSpecify<Test>(id));

            var model = new PassTestModel { Id = test.Id, Name = test.Name, Questions = new List<QuestionTest>() };

            foreach (var q in test.Questions)
            {
                model.Questions.Add(new QuestionTest
                {
                    Id = q.Id,
                    QuestionValue = q.QuestionText,
                    TimeToAnswer = q.TimeToAnswer,
                    Answers = new List<AnswerTest>(q.Answers
                        .Select(a => new AnswerTest
                        {
                            Id = a.Id,
                            AnswerValue = a.AnswerText,
                            IsCorrect = a.IsCorrect
                        }).ToList())
                });
            }

            return View(model);
        }
コード例 #2
0
        public ActionResult PassResult(PassTestModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new { isValid = false });
            }

            var test = testsRepo.FindOne(new ByIdSpecify<Test>(model.Id));

            var result = new TestResult
                {
                    UserId = this.usersRepo.FindOne(new UserByLoginSpecify(User.Identity.Name)).Id,
                    TestId = test.Id,
                    PassDate = DateTime.Now
                };

            testsResRepo.SaveOrUpdate(result);

            var correctAnswers = 0;

            foreach (var question in model.Questions)
            {
                var resultQuestion = new ResultQuestion
                    {
                        QuestionText = question.QuestionValue,
                        TestResultId = result.Id
                    };

                qResRepo.SaveOrUpdate(resultQuestion);

                foreach (var answer in question.Answers)
                {
                    if (answer.IsCorrect && answer.IsChoisen)
                        correctAnswers++;
                    var resultAnswer = new ResultAnswer
                        {
                            AnswerText = answer.AnswerValue,
                            ResultQuestionId = resultQuestion.Id,
                            IsCorrect = answer.IsCorrect,
                            IsChoisen = answer.IsChoisen
                        };

                    aResRepo.SaveOrUpdate(resultAnswer);

                    //resultQuestion.ResultAnswers.Add(resultAnswer);
                }

                //result.ResultQuestions.Add(resultQuestion);
            }

            ViewBag.correctAnswers = correctAnswers;
            ViewBag.totalQuestios = model.Questions.Count;
            return View();
        }