public async Task <ActionResult> Index(Guid themeId, int page = 1, int pageSize = 10)
        {
            var questions = await _questionAdminService.GetAllQuestionsByThemeIdAsync(page, pageSize, themeId);

            var theme = await _themeAdminService.GetThemeByIdAsync(themeId);

            List <Breadcrumb> breadcrumb = new List <Breadcrumb>()
            {
                new Breadcrumb("Home", "Index", "Home", new { Area = "" }),
                new Breadcrumb("Subjects", "Index", "Subject", new { Area = "Admin" }),
                new Breadcrumb("Subject themes", "Index", "Theme", new { Area = "Admin", subjectId = theme.SubjectId }),
                new Breadcrumb("Questions of the theme")
            };

            QuestionIndexViewModel questionIndexViewModel = new QuestionIndexViewModel
            {
                PageInfo   = questions,
                ThemeId    = themeId,
                Questions  = questions.Results,
                Breadcrumb = breadcrumb
            };

            ViewBag.Title           = $"List of questions to the theme \"{theme.Title}\" of the subject \"{theme.SubjectTitle}\"";
            ViewBag.HeadFirstTitle  = $"List of questions to the theme \"{theme.Title}\"";
            ViewBag.HeadSecondTitle = $"Subject \"{theme.SubjectTitle}\"";
            return(View(questionIndexViewModel));
        }
示例#2
0
        public async Task <ActionResult> Solve(Guid themeId, int questionNumber = 1)
        {
            var theme = await _themeAdminService.GetThemeByIdAsync(themeId);

            if (theme.QuestionsCount == 0)
            {
                Session["Token"]       = null;
                Session["TokenExpire"] = null;
                Session["Answers"]     = null;
                return(RedirectToAction("Index", "Theme", new { subjectId = theme.SubjectId }));
            }

            var sessionToken       = Session["Token"];
            var sessionTokenExpire = (DateTime)Session["TokenExpiration"];

            if (sessionToken == null)
            {
                TempData["invalidToken"] = "You have an invalid token. Please try to run test again.";
                return(RedirectToAction("AutoResult", "Result", new { themeId = theme.Id }));
            }

            if (sessionTokenExpire < DateTime.UtcNow)
            {
                return(RedirectToAction("AutoResult", "Result", new { themeId = theme.Id }));
            }

            var sessionModel = (SessionModel)Session["Answers"];

            var question = (await _questionAdminService.GetAllQuestionsByThemeIdAsync(themeId))
                           .Skip(questionNumber - 1)
                           .Take(1)
                           .Select(x => new QuestionViewModel
            {
                TotalQuestionsCount = theme.QuestionsCount,
                QuestionNumber      = questionNumber,
                QuestionId          = x.Id,
                Image      = x.ImageUrl,
                Title      = x.Title,
                ThemeId    = x.ThemeId,
                ThemeTitle = x.ThemeTitle,
                Options    = x.Answers.Select(y => new ChoiceModel
                {
                    AnswerId        = y.Id,
                    Title           = y.Title,
                    IsCorrectAnswer = sessionModel.Answers.Any(z => z.SelectedAnswerId == y.Id)
                })
                             .ToList()
            })
                           .FirstOrDefault();

            question.Options.Shuffle();

            return(View(question));
        }
        public async Task <ResultResponse> CreateResultAsync(CreateResultRequest model)
        {
            var correctAnswerIds = await UnitOfWork.QuestionAnswerRepository
                                   .Query(x => x.Question.QuestionThemes.Any(y => y.ThemeId == model.ThemeId) &&
                                          x.Answer.IsCorrectAnswer)
                                   .Select(x => x.AnswerId)
                                   .ToListAsync();

            var questions = await _questionAdminService.GetAllQuestionsByThemeIdAsync(model.ThemeId);

            var userCorrectAnswerIds = model.AnswerIds.Where(x => correctAnswerIds.Any(y => y == x)).ToList();

            // Prepare directions
            var directions = new List <string>();

            foreach (var question in questions)
            {
                if (question.Answers.Any(x => x.IsCorrectAnswer && userCorrectAnswerIds.All(y => y != x.Id)))
                {
                    directions.Add(question.Explanation);
                }
            }

            // Add result to database
            var result = new Result
            {
                Id = Guid.NewGuid(),
                AllQuestionsCount   = questions.Count(),
                CorrectAnswersCount = userCorrectAnswerIds.Count,
                Created             = DateTime.UtcNow,
                UserProfileId       = model.UserProfileId,
                ThemeId             = model.ThemeId
            };

            if (model.IsAdmin)
            {
                return(result.ToResultResponse(directions));
            }

            UnitOfWork.ResultRepository.Add(result);
            await UnitOfWork.SaveChangesAsync();

            return(result.ToResultResponse(directions));
        }