Пример #1
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 }));
        }
Пример #2
0
        public async Task <JsonResult> SubmitAnswer(int questionId, int answerId)
        {
            var currentUserId = User.Identity.GetUserId();

            var result = await TriviaService.RecordAnsweredQuestionAsync(
                currentUserId,
                questionId,
                answerId,
                (int)QuestionTypeValues.Random);

            if (result.Succeeded)
            {
                return(Json(new { success = true, correctAnswerId = result.CorrectAnswerId }));
            }

            return(Json(new { success = false, error = ErrorMessages.AnswerNotSubmitted }));
        }
Пример #3
0
        public async Task <JsonResult> SubmitTimedAnswer(int questionId, int answerId)
        {
            DateTime utcNow          = DateTime.UtcNow;
            string   responseMessage = String.Empty;

            if (QuestionStartTime.HasValue)
            {
                TimeSpan timeSpentOnQuestion = utcNow.Subtract(QuestionStartTime.Value);
                bool     didUserAnswerInTime = timeSpentOnQuestion.TotalSeconds <= 10 ? true : false;
                QuestionStartTime = null;

                if (didUserAnswerInTime)
                {
                    var currentUserId = User.Identity.GetUserId();

                    // log the answer for this user's question history
                    var result = await TriviaService.RecordAnsweredQuestionAsync(
                        currentUserId,
                        questionId,
                        answerId,
                        (int)QuestionTypeValues.Timed);

                    if (result.Succeeded)
                    {
                        return(Json(new { success = true, correctAnswerId = result.CorrectAnswerId }));
                    }
                }
                else
                {
                    responseMessage = ErrorMessages.TimedQuestionOutOfTime;
                }
            }
            else
            {
                responseMessage = ErrorMessages.TimedQuestionNoStart;
            }

            return(Json(new { success = false, error = responseMessage }));
        }