Exemplo n.º 1
0
        public async Task <JsonResult> AutoResult(CreateResultRequest request)
        {
            var sessionModel = (SessionModel)Session["Answers"];

            request.AnswerIds = sessionModel.Answers.Select(x => x.SelectedAnswerId).ToList();

            TempData["testTimeLineExpired"] = "The time for passing the test is over.";

            // Clear session
            Session["Token"]       = null;
            Session["TokenExpire"] = null;
            Session["Answers"]     = null;

            var userId  = GetCurrentUserId();
            var isAdmin = IsUserExistInRole("Admin");
            var profile = await _profileService.GetProfileByUserIdAsync(userId);

            request.UserProfileId = profile.Id;
            request.IsAdmin       = isAdmin;

            var theme = await _themeAdminService.GetThemeByIdAsync(request.ThemeId);

            var result = await _resultAdminService.CreateResultAsync(request);

            var model = new ResultIndexViewModel
            {
                Id = result.Id,
                AllQuestionsCount   = result.AllQuestionsCount,
                CorrectAnswersCount = result.CorrectAnswersCount,
                Created             = result.Created,
                GroupId             = profile.GroupId,
                Number     = profile.Group?.Number,
                ThemeId    = result.ThemeId,
                ThemeTitle = theme.Title,
                Mark       = Convert.ToInt32(Math.Round((double)result.CorrectAnswersCount / result.AllQuestionsCount * 10, MidpointRounding.AwayFromZero))
            };

            TempData["directions"] = result.Directions;

            return(Json(Url.Action("Result", "Result", new
            {
                id = model.Id,
                allQuestionsCount = model.AllQuestionsCount,
                correctAnswersCount = model.CorrectAnswersCount,
                created = model.Created,
                groupId = model.GroupId,
                number = model.Number,
                themeId = model.ThemeId,
                themeTitle = model.ThemeTitle,
                mark = model.Mark
            })));
        }
Exemplo n.º 2
0
        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));
        }