Esempio n. 1
0
        public async Task <ActionResult> Quiz(int id, string seoName)
        {
            await SetNotificationsAsync();

            var currentUserId = User.Identity.GetUserId();

            // if there is no quiz by this id, return not found
            var quiz = await TriviaService.GetQuizAsync(id);

            if (quiz == null)
            {
                return(new HttpNotFoundResult());
            }

            // redirect to the SEO name if not provided
            string expectedSeoName = quiz.ToSEOName();

            if (seoName != expectedSeoName)
            {
                return(RedirectToAction("quiz", new { id = id, seoName = expectedSeoName }));
            }

            bool isAlreadyCompleted = false;
            List <QuestionViewModel>   questionListViewModel = new List <QuestionViewModel>();
            MinefieldQuestionViewModel minefieldQuestion     = new MinefieldQuestionViewModel();

            if (User.Identity.IsAuthenticated)
            {
                // check if the quiz has already been completed by the user
                isAlreadyCompleted = await TriviaService.IsQuizCompletedByUserAsync(currentUserId, quiz.Id);

                // if it has already been completed, get answered questions for quiz and user
                if (isAlreadyCompleted)
                {
                    if (quiz.QuizTypeId == (int)QuizTypeValues.Individual)
                    {
                        questionListViewModel = await GetQuestionsForIndividualQuizAsync(currentUserId, quiz.Id, quiz.Questions);
                    }
                    else
                    {
                        minefieldQuestion = await GetQuestionForMinefieldQuizAsync(currentUserId, quiz.Id, quiz.MinefieldQuestion);
                    }
                }
                // if it hasn't been completed, get unanswered questions for quiz
                else
                {
                    if (quiz.QuizTypeId == (int)QuizTypeValues.Individual)
                    {
                        questionListViewModel = Mapper.Map <ICollection <Question>, List <QuestionViewModel> >(quiz.Questions);
                    }
                    else
                    {
                        minefieldQuestion = Mapper.Map <MinefieldQuestion, MinefieldQuestionViewModel>(quiz.MinefieldQuestion);
                    }
                }
            }
            // user isn't authenticated, so anonymous users the questions
            else
            {
                if (quiz.QuizTypeId == (int)QuizTypeValues.Individual)
                {
                    questionListViewModel = Mapper.Map <ICollection <Question>, List <QuestionViewModel> >(quiz.Questions);
                }
                else
                {
                    minefieldQuestion = Mapper.Map <MinefieldQuestion, MinefieldQuestionViewModel>(quiz.MinefieldQuestion);
                }
            }

            var usersCompletedQuiz = await TriviaService.GetUsersCompletedQuizAsync(id, currentUserId);

            var tagsForQuiz = await GetTagsForQuizAsync(id);

            var questionViolationViewModel = await GetQuestionViolationViewModelAsync();

            var similarQuizzes = await TriviaService.GetSimilarQuizzesAsync(id, quiz.QuizCategoryId);

            var quizCategories = await TriviaService.GetQuizCategoriesAsync();

            var usersWithSimilarScores = await TriviaService.GetUsersWithSimilarScoresAsync(currentUserId, id, 4);

            QuizViewModel viewModel = new QuizViewModel()
            {
                Questions          = questionListViewModel,
                MinefieldQuestion  = minefieldQuestion,
                QuizName           = quiz.Name,
                QuizId             = id,
                QuizTypeId         = quiz.QuizTypeId,
                IsAlreadyCompleted = isAlreadyCompleted,
                QuizDescription    = quiz.Description,
                UsersCompletedQuiz = usersCompletedQuiz,
                Tags = tagsForQuiz,
                QuestionViolation      = questionViolationViewModel,
                ThumbnailImageUrl      = quiz.GetThumbnailImagePath(),
                ImageUrl               = quiz.GetImagePath(),
                SimilarQuizzes         = similarQuizzes,
                QuizCategories         = quizCategories,
                UsersWithSimilarScores = usersWithSimilarScores
            };

            await SetupAchievementProgressDetails(currentUserId, viewModel);

            return(View(viewModel));
        }