示例#1
0
        public async Task <ActionResult> Test(int id, int?question)
        {
            TestCourseTopicVM testCourseTopic = new TestCourseTopicVM(await db.GetTestsCourseTopicByTestIdAsync(id));
            TestVM            test            = await testCourseTopic.GetTestAsync();

            CourseVM            course    = test.GetCourse();
            UserTestVM          userTest  = new UserTestVM(await db.GetUserTestAsync(User.Identity.Name, course.Id, id, testCourseTopic.Id));
            List <TestQuestion> Questions = await db.GetTestQuestionsByTestIdAsync(id);

            if (userTest.IsStarted && userTest.ClosingIn <= DateTime.Now || userTest.IsDone || !userTest.IsStarted && question != null)
            {
                return(RedirectToAction("Error"));
            }

            ViewBag.Test       = test;
            ViewBag.UserTest   = userTest;
            ViewBag.Course     = course;
            ViewBag.Questions  = Questions;
            ViewBag.UserCourse = new UserCourseVM(await db.GetUserCourseAsync(User.Identity.Name, course.Id));

            if (!userTest.IsStarted && question == null)
            {
                ViewBag.TimeMinutes = test.TimeMinutes;
                ViewBag.Attempts    = test.Attempts;
            }
            else if (userTest.IsStarted)
            {
                question = question == null ? 1 : question;

                List <TestAnswer> answers = await db.GetTestAnswersByTestParamsAsync(id, (int)question);

                List <UserTestAnswer> userTestAllAnswers = await db.GetUserTestAnswersByTestParamsAsync(id);

                ViewBag.Question    = new TestQuestionVM(await db.GetTestQuestionByTestParamsAsync(id, (int)question));
                ViewBag.Answers     = answers.Select(x => new TestAnswerVM(x)).ToList();
                ViewBag.UserAnswers = userTestAllAnswers.Select(x => new UserTestAnswerVM(x)).ToList();

                return(View("Test", testCourseTopic));
            }
            return(View("Test", testCourseTopic));
        }