コード例 #1
0
ファイル: QuizController.cs プロジェクト: epeshk/uLearn
        public ActionResult Quiz(QuizSlide slide, string courseId, string userId, bool isGuest, bool isLti = false, ManualQuizChecking manualQuizCheckQueueItem = null)
        {
            if (isGuest)
            {
                return(PartialView(GuestQuiz(slide, courseId)));
            }
            var slideId          = slide.Id;
            var maxDropCount     = GetMaxDropCount(slide);
            var state            = GetQuizState(courseId, userId, slideId, maxDropCount);
            var quizState        = state.Item1;
            var tryNumber        = state.Item2;
            var resultsForQuizes = GetResultForQuizes(courseId, userId, slideId, state.Item1);

            log.Info($"Создаю тест для пользователя {userId} в слайде {courseId}:{slide.Id}, isLti = {isLti}");

            var quizVersion = quizzesRepo.GetLastQuizVersion(courseId, slideId);

            if (quizState != QuizState.NotPassed)
            {
                quizVersion = userQuizzesRepo.FindQuizVersionFromUsersAnswer(courseId, slideId, userId);
            }

            /* If we haven't quiz version in database, create it */
            if (quizVersion == null)
            {
                quizVersion = quizzesRepo.AddQuizVersionIfNeeded(courseId, slide);
            }

            /* Restore quiz slide from version stored in the database */
            var quiz = quizVersion.RestoredQuiz;

            slide = new QuizSlide(slide.Info, quiz);

            var userAnswers     = userQuizzesRepo.GetAnswersForShowOnSlide(courseId, slide, userId);
            var canUserFillQuiz = CanUserFillQuiz(quizState);

            var model = new QuizModel
            {
                CourseId                 = courseId,
                Slide                    = slide,
                QuizState                = quizState,
                TryNumber                = tryNumber,
                MaxDropCount             = maxDropCount,
                ResultsForQuizes         = resultsForQuizes,
                AnswersToQuizes          = userAnswers,
                IsLti                    = isLti,
                ManualQuizCheckQueueItem = manualQuizCheckQueueItem,
                CanUserFillQuiz          = canUserFillQuiz,
                GroupsIds                = Request.GetMultipleValues("group"),
            };

            if (model.QuizState == QuizState.Subtotal && model.Score == quiz.MaxScore)
            {
                model.QuizState = QuizState.Total;
            }

            return(PartialView(model));
        }
コード例 #2
0
ファイル: QuizController.cs プロジェクト: FuryThrue/Ulearn
        public ActionResult Quiz(QuizSlide slide, string courseId, string userId, bool isGuest, bool isLti = false, ManualQuizChecking manualQuizCheckQueueItem = null, int?send = null)
        {
            metricSender.SendCount("quiz.show");
            if (isLti)
            {
                metricSender.SendCount("quiz.show.lti");
            }
            metricSender.SendCount($"quiz.show.{courseId}");
            metricSender.SendCount($"quiz.show.{courseId}.{slide.Id}");

            var course = courseManager.FindCourse(courseId);

            if (course == null)
            {
                return(HttpNotFound());
            }

            if (isGuest)
            {
                metricSender.SendCount("quiz.show.to.guest");
                return(PartialView(GuestQuiz(course, slide)));
            }
            var slideId          = slide.Id;
            var maxTriesCount    = GetMaxTriesCount(courseId, slide);
            var state            = GetQuizState(courseId, userId, slideId);
            var quizState        = state.Item1;
            var tryNumber        = state.Item2;
            var resultsForQuizes = GetResultForQuizes(courseId, userId, slideId, state.Item1);

            log.Info($"Создаю тест для пользователя {userId} в слайде {courseId}:{slide.Id}, isLti = {isLti}");

            var quizVersion = quizzesRepo.GetLastQuizVersion(courseId, slideId);

            if (quizState != QuizState.NotPassed)
            {
                quizVersion = userQuizzesRepo.FindQuizVersionFromUsersAnswer(courseId, slideId, userId);
            }

            /* If we haven't quiz version in database, create it */
            if (quizVersion == null)
            {
                quizVersion = quizzesRepo.AddQuizVersionIfNeeded(courseId, slide);
            }

            /* Restore quiz slide from version stored in the database */
            var quiz = quizVersion.GetRestoredQuiz(course, course.FindUnitBySlideId(slide.Id));

            slide = new QuizSlide(slide.Info, quiz);

            if (quizState == QuizState.Subtotal)
            {
                var score = resultsForQuizes?.AsEnumerable().Sum(res => res.Value) ?? 0;
                /* QuizState.Subtotal is partially obsolete. If user fully solved quiz, then show answers. Else show empty quiz for the new try... */
                if (score == quiz.MaxScore)
                {
                    quizState = QuizState.Total;
                }
                /* ... and show last try's answers only if argument `send` has been passed in query */
                else if (!send.HasValue)
                {
                    quizState = QuizState.NotPassed;
                    /* ... if we will show answers from last try then drop quiz */
                    userQuizzesRepo.DropQuiz(courseId, userId, slideId);
                }
            }

            var userAnswers     = userQuizzesRepo.GetAnswersForShowOnSlide(courseId, slide, userId);
            var canUserFillQuiz = CanUserFillQuiz(quizState);

            var questionAnswersFrequency = new DefaultDictionary <string, DefaultDictionary <string, int> >();

            if (User.HasAccessFor(courseId, CourseRole.CourseAdmin))
            {
                questionAnswersFrequency = quiz.Blocks.OfType <ChoiceBlock>().ToDictionary(
                    block => block.Id,
                    block => userQuizzesRepo.GetAnswersFrequencyForChoiceBlock(courseId, slide.Id, block.Id).ToDefaultDictionary()
                    ).ToDefaultDictionary();
            }

            var model = new QuizModel
            {
                Course                   = course,
                Slide                    = slide,
                QuizState                = quizState,
                TryNumber                = tryNumber,
                MaxTriesCount            = maxTriesCount,
                ResultsForQuizes         = resultsForQuizes,
                AnswersToQuizes          = userAnswers,
                IsLti                    = isLti,
                ManualQuizCheckQueueItem = manualQuizCheckQueueItem,
                CanUserFillQuiz          = canUserFillQuiz,
                GroupsIds                = Request.GetMultipleValuesFromQueryString("group"),
                QuestionAnswersFrequency = questionAnswersFrequency,
            };

            return(PartialView(model));
        }