Exemple #1
0
        public async Task <ActionResult> MinefieldQuiz(QuizViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("quiz", new { id = viewModel.QuizId, seoName = viewModel.SEOName }));
            }

            string currentUserId = User.Identity.GetUserId();

            var profile = await ProfileService.GetProfileAsync(currentUserId);

            // loop through questions and record answers
            var result = await TriviaService.RecordAnsweredMinefieldQuestionAsync(
                currentUserId,
                viewModel.MinefieldQuestion.MinefieldQuestionId,
                viewModel.MinefieldQuestion.Answers);

            if (result.Succeeded)
            {
                int count = await TriviaService.SetQuizAsCompletedAsync(currentUserId, viewModel.QuizId, result.CorrectAnswerCount);
            }

            TempData["TagsAwardedCount"]        = 0;
            TempData["DidUserJustCompleteQuiz"] = true;

            return(RedirectToAction("quiz", new { id = viewModel.QuizId, seoName = viewModel.SEOName }));
        }
Exemple #2
0
        public async Task <ActionResult> Quiz(QuizViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("quiz", new { id = viewModel.QuizId, seoName = viewModel.SEOName }));
            }

            string currentUserId = User.Identity.GetUserId();

            // loop through questions and record answers
            int        numberOfCorrectAnswers = 0;
            int        numberOfTagsAwarded    = 0;
            List <int> milestoneIdsUnlocked   = new List <int>();

            foreach (var question in viewModel.Questions)
            {
                var result = await TriviaService.RecordAnsweredQuestionAsync(
                    currentUserId,
                    question.QuestionId,
                    question.SelectedAnswerId.Value,
                    (int)QuestionTypeValues.Quiz);

                if (result.Succeeded)
                {
                    if (question.SelectedAnswerId.Value == result.CorrectAnswerId)
                    {
                        numberOfCorrectAnswers++;
                    }

                    numberOfTagsAwarded += result.TagsAwardedCount;

                    // collect up any milestones that were unlocked by the user
                    foreach (var milestoneIds in result.AwardedAchievements)
                    {
                        milestoneIdsUnlocked.AddRange(milestoneIds.MilestoneIdsUnlocked);
                    }
                }
            }

            TempData["DidUserJustCompleteQuiz"] = true;
            TempData["TagsAwardedCount"]        = numberOfTagsAwarded;
            TempData["MilestoneIdsUnlocked"]    = milestoneIdsUnlocked;

            int count = await TriviaService.SetQuizAsCompletedAsync(currentUserId, viewModel.QuizId, numberOfCorrectAnswers);

            return(RedirectToAction("quiz", new { id = viewModel.QuizId, seoName = viewModel.SEOName }));
        }