Exemplo n.º 1
0
        public void RenderVisualizeTestResultView_WhenModelStateIsValid()
        {
            // Arange
            var quizServiceMock = new Mock <IQuizService>();

            var username = "******";

            var stringRepresentingViewContent = "_renderPartial";

            var httpContext  = new Mock <HttpContextBase>();
            var mockIdentity = new Mock <IIdentity>();

            httpContext.SetupGet(x => x.User.Identity).Returns(mockIdentity.Object);
            mockIdentity.Setup(x => x.Name).Returns(username);

            var quizMock   = new Quiz();
            var quizResult = new QuizResultViewModel();

            quizServiceMock.Setup(m => m.SubmitTest(quizMock)).Returns(Task.FromResult(quizResult));

            var controller = new TestQuizControllerMock(quizServiceMock.Object,
                                                        stringRepresentingViewContent);

            // Act && Assert
            controller
            .WithCallTo(c => c.SubmitTest(quizMock))
            .ShouldReturnJson()
            .Data
            .Equals(new { error = "true", renderedView = stringRepresentingViewContent });
        }
Exemplo n.º 2
0
        public static QuizResultViewModel ToQuizResultViewModel(this QuizResult result)
        {
            var vm = new QuizResultViewModel()
            {
                EllapsedTime = result.EllapsedTime,
                Score        = result.Score,
                UserId       = result.UserId,
            };

            return(vm);
        }
Exemplo n.º 3
0
        public async Task <QuizResultViewModel> SubmitTest(Quiz quiz)
        {
            var shouldSaveChanges = false;

            var questionsCount      = quiz.Questions.Count;
            var correctAnswersCount = CountCorrectAnswers(quiz);

            double score = Math.Round((correctAnswersCount / (double)questionsCount) * 100, 2);

            var quizResult = new QuizResultViewModel();

            double requiredScore = this.context.Assignments
                                   .First(a => a.Id == quiz.AssignmentId)
                                   .Course
                                   .RequiredScore;

            quizResult.Passed = "Failed";
            if (score >= requiredScore)
            {
                quizResult.Passed = "Passed";

                shouldSaveChanges = true;
                this.context.Assignments
                .First(a => a.Id == quiz.AssignmentId)
                .State = AssignmentState.Completed;
            }

            var assignment = this.context.Assignments
                             .First(a => a.Id == quiz.AssignmentId);

            if (assignment.TestResult < score && assignment.State != AssignmentState.Completed)
            {
                shouldSaveChanges = true;

                this.context.Assignments
                .First(a => a.Id == quiz.AssignmentId)
                .TestResult = score;
            }

            quizResult.Score          = score;
            quizResult.CorrectAnswers = correctAnswersCount;
            quizResult.RequiredScore  = requiredScore;

            if (shouldSaveChanges)
            {
                await SaveToDb();
            }

            return(quizResult);
        }
Exemplo n.º 4
0
        public ActionResult Result(SubmittedQuizViewModel quiz)
        {
            if (ModelState.IsValid)
            {
                Quiz passedQuiz = quizService.GetById(quiz.Id);

                QuizResultViewModel result = new QuizResultViewModel()
                {
                    Title = passedQuiz.Title, Questions = new List <QuizQuestionResultViewModel>()
                };

                result.Total = quiz.Answers.Count;

                for (int i = 0; i < quiz.Answers.Count; i++)
                {
                    QuizQuestion testQuestion = passedQuiz.Questions.ElementAt(i);

                    QuizQuestionResultViewModel questionResult = new QuizQuestionResultViewModel()
                    {
                        Text     = testQuestion.Text,
                        Answers  = testQuestion.AnswerOptions.ToList(),
                        Correct  = testQuestion.CorrectAnswer,
                        Selected = quiz.Answers[i]
                    };

                    if (questionResult.Selected == testQuestion.CorrectAnswer)
                    {
                        result.Correct++;
                    }

                    result.Questions.Add(questionResult);
                }

                User user = userService.GetByEmail(User.Identity.Name);

                if (user != null)
                {
                    QuizStatistic statistic = new QuizStatistic()
                    {
                        DatePassed = DateTime.Now, QuizId = passedQuiz.Id, Correct = result.Correct, UserId = user.Id
                    };
                    statisticService.Create(statistic);
                }

                return(View(result));
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 5
0
        public JsonResult PostResult(QuizResultViewModel vm)
        {
            var result = _viewModelConverter.ToQuizResultModel(vm);

            var userId = String.IsNullOrEmpty(User.Identity.Name) ? Session.SessionID : User.Identity.Name;

            result.UserId = userId;

            _quizResultService.Insert(result);


            _userActivityService.Add(userId, ActivityItem.Quiz, ActivityAction.EndQuiz, result.QuizId);

            return(Json(result.Id));
        }
Exemplo n.º 6
0
        public QuizResult ToQuizResultModel(QuizResultViewModel vm)
        {
            var model = new QuizResult();

            model.EllapsedTime = vm.EllapsedTime;
            model.QuizId       = vm.QuizId;
            model.Score        = vm.Score;
            model.UserId       = vm.UserId;
            model.UserAnswers  = vm.UserAnswers.Select(x => new UserAnswer
            {
                AnswerId   = x.AnswerId,
                QuestionId = x.QuestionId
            }).ToList();

            return(model);
        }
Exemplo n.º 7
0
        public QuizResultViewModel ToQuizResultViewModel(QuizResult result)
        {
            var vm = new QuizResultViewModel
            {
                EllapsedTime = result.EllapsedTime,
                Score        = result.Score,
                UserId       = result.UserId,
                QuizId       = result.QuizId,
            };

            vm.UserAnswers = result.UserAnswers.Select(x => new UserAnswerViewModel
            {
                AnswerId   = x.AnswerId,
                QuestionId = x.QuestionId
            }).ToList();

            return(vm);
        }
Exemplo n.º 8
0
        public ActionResult GetStudentQuizResultDetails(int resultId)
        {
            var result = UserService.GetStudentQuizResult(resultId);

            if (result.IsOK)
            {
                var viewModel = new QuizResultViewModel(User.Identity.Name, result.GetData());
                viewModel.IsOk = true;
                return(Json(viewModel));
            }
            else
            {
                var viewModel = new QuizResultViewModel(User.Identity.Name, result.GetData());
                viewModel.IsOk    = false;
                viewModel.Message = result.Messages[0];
                return(Json(viewModel));
            }
        }
Exemplo n.º 9
0
        public void ThrowArgumentNullException_WhenModelStateIsValidAndQuizResultIsNull()
        {
            // Arange
            var quizServiceMock = new Mock <IQuizService>();

            var stringRepresentingViewContent = "_renderPartial";

            var quizMock = new Quiz();
            QuizResultViewModel quizResult = null;

            quizServiceMock.Setup(m => m.SubmitTest(quizMock)).Returns(Task.FromResult(quizResult));

            var controller = new TestQuizControllerMock(quizServiceMock.Object,
                                                        stringRepresentingViewContent);

            // Act && Assert
            Assert
            .ThrowsException <ArgumentNullException>(() => controller.SubmitTest(quizMock));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> Submit(AttemtedQuizViewModel model)
        {
            var userId            = this.userManager.GetUserId(this.User);
            var originalQuestions = await this.questionsService.GetAllByQuizIdAsync <QuestionViewModel>(model.Id);

            var points    = this.resultHelper.CalculateResult(originalQuestions, model.Questions);
            var creatorId = await this.quizService.GetCreatorIdByQuizIdAsync(model.Id);

            if (creatorId != userId)
            {
                await this.resultService.CreateResultAsync(userId, points, originalQuestions.Count, model.Id);
            }

            var resultModel = new QuizResultViewModel()
            {
                QuizName  = model.Name,
                MaxPoints = originalQuestions.Count,
                Points    = points,
            };

            return(this.View(resultModel));
        }
Exemplo n.º 11
0
        public ActionResult SubmitQuiz(TakeQuizDisplayViewModel model)
        {
            var studentName = User.Identity.Name;
            var quiz        = model.GetQuizFromModel();

            var result = _takeQuizService.ProcessResults(quiz, studentName);

            if (result.IsOK)
            {
                var quizResult = result.GetData();
                var viewModel  = new QuizResultViewModel(studentName, quizResult);
                viewModel.IsOk = true;
                return(Json(viewModel));
            }
            else
            {
                var quizResult = result.GetData();
                var viewModel  = new QuizResultViewModel(studentName, quizResult);
                viewModel.IsOk    = false;
                viewModel.Message = result.Messages.FirstOrDefault();
                return(Json(viewModel));
            }
        }
Exemplo n.º 12
0
        public IActionResult QuizResult(QuizViewModel result)
        {
            List <int> colorInteger             = new List <int>();
            Dictionary <string, int> colorScore = new Dictionary <string, int>();
            List <string>            colors     = new List <string>();
            QuizResultViewModel      quizResult = new QuizResultViewModel();

            string[] desserializeResult = result.ColorScore.Split('|');
            foreach (string color in desserializeResult)
            {
                colorScore.Add(color.Substring(0, 1), int.Parse(color.Substring(1)));
            }

            var sortedDict = from entry in colorScore orderby entry.Value descending select entry;


            quizResult.PrimaryColor = colorScore.Aggregate((x, y) => x.Value > y.Value ? x : y).Key;



            quizResult.SecondaryColor = sortedDict.Skip(1).Aggregate((x, y) => x.Value > y.Value ? x : y).Key;

            if (quizResult.PrimaryColor == quizResult.SecondaryColor)
            {
                quizResult.PrimaryColor = sortedDict.OrderByDescending(i => i.Value).FirstOrDefault().Key;
            }

            quizResult.ColorScore = colorScore;
            AspNetUsers user = _context.AspNetUsers.Find(FindUserId());

            user.Playertype            = quizResult.PrimaryColor + quizResult.SecondaryColor;
            _context.Entry(user).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            _context.Update(user);
            _context.SaveChanges();

            return(View(quizResult));
        }
Exemplo n.º 13
0
 public IActionResult Submit(QuizResultViewModel model)
 {
     return(this.View(model));
 }
 public QuizResultPage(QuizResultViewModel model) : this()
 {
     this.BindingContext = model;
 }