Exemplo n.º 1
0
        public async Task <string> SubmitQuiz(string courseId, string slideIndex, string answer, bool isLti)
        {
            var intSlideIndex = int.Parse(slideIndex);
            var course        = courseManager.GetCourse(courseId);
            var userId        = User.Identity.GetUserId();
            var slide         = course.Slides[intSlideIndex];
            var slideId       = slide.Id;

            if (visitsRepo.IsPassed(userId, slideId))
            {
                return("already answered");
            }
            var time    = DateTime.Now;
            var answers = JsonConvert.DeserializeObject <List <QuizAnswer> >(answer).GroupBy(x => x.QuizId);
            var quizBlockWithTaskCount = course.Slides[intSlideIndex].Blocks.Count(x => x is AbstractQuestionBlock);
            var incorrectQuizzes       = new List <string>();
            var fillInBlockType        = typeof(FillInBlock);
            var allQuizInfos           = new List <QuizInfoForDb>();

            foreach (var ans in answers)
            {
                var quizInfos = CreateQuizInfo(course, intSlideIndex, ans);
                foreach (var quizInfo in quizInfos)
                {
                    allQuizInfos.Add(quizInfo);
                    if (!quizInfo.IsRightAnswer && quizInfo.QuizType == fillInBlockType)
                    {
                        incorrectQuizzes.Add(ans.Key);
                    }
                }
            }
            var blocksInAnswerCount = allQuizInfos.Select(x => x.QuizId).Distinct().Count();

            if (blocksInAnswerCount != quizBlockWithTaskCount)
            {
                return("has empty blocks");
            }
            var score = allQuizInfos
                        .Where(forDb => forDb.IsRightQuizBlock)
                        .Select(forDb => forDb.QuizId)
                        .Distinct()
                        .Count();

            foreach (var quizInfoForDb in allQuizInfos)
            {
                await userQuizzesRepo.AddUserQuiz(courseId, quizInfoForDb.IsRightAnswer, quizInfoForDb.ItemId, quizInfoForDb.QuizId,
                                                  slideId, quizInfoForDb.Text, userId, time, quizInfoForDb.IsRightQuizBlock);
            }

            await visitsRepo.AddAttempt(slideId, userId, score);

            if (isLti)
            {
                LtiUtils.SubmitScore(slide, userId);
            }

            return(string.Join("*", incorrectQuizzes.Distinct()));
        }