public async Task <IActionResult> PassTest(CraftedTest craftedTest)
        {
            craftedTest.User = await _userManager.GetUserAsync(HttpContext.User);

            await _studentTestService.SubmitTestAsync(craftedTest);

            return(RedirectToAction(nameof(GetTests)));
        }
Example #2
0
        public async Task SubmitTestAsync_AddsNewTestResultElement()
        {
            var user = await _context.Users.FindAsync(1);

            var craftedTest = new CraftedTest()
            {
                Date        = DateTime.Now,
                Questions   = new List <QuestionDto>(),
                Score       = 10,
                TestBaseDto = new TestBaseDto(),
                User        = user
            };

            await _service.SubmitTestAsync(craftedTest);

            _context.TestResults.Count().Should().Be(3);
        }
Example #3
0
        public async Task SubmitTestAsync(CraftedTest craftedTest)
        {
            var numOfQuestions      = craftedTest.TestBaseDto.NumOfQuestions;
            var numOfCorrectAnswers = 0;

            foreach (var questionDto in craftedTest.Questions)
            {
                var currentQuestionIsAnsweredCorrect = true;
                foreach (var answerDto in questionDto.Answers)
                {
                    var correctAnswer = await _unitOfWork.AnswerRepository.GetByIdAsync(answerDto.Id);

                    if (answerDto.IsCorrect != correctAnswer.IsCorrect)
                    {
                        currentQuestionIsAnsweredCorrect = false;
                        break;
                    }
                }

                if (currentQuestionIsAnsweredCorrect)
                {
                    numOfCorrectAnswers++;
                }
            }
            var score = (float)numOfCorrectAnswers / (float)numOfQuestions * 100;

            score = (float)Math.Round(score, 2);

            var testResultDto = new TestResultDto
            {
                Date          = craftedTest.Date,
                TestBaseId    = craftedTest.TestBaseDto.Id,
                StudentUserId = craftedTest.User.Id,
                Score         = score
            };

            var testResultEntity = TestResultMapper.Map(testResultDto);

            await _unitOfWork.TestResultRepository.InsertAsync(testResultEntity);

            await _unitOfWork.CommitAsync();
        }
Example #4
0
        public async Task <CraftedTest> CraftRandomTestFromTestBaseAsync(User user, int testBaseId)
        {
            var testBaseEntity = await _unitOfWork.TestBaseRepository.GetByIdAsync(testBaseId);

            var testResultsForThisStudent = await _unitOfWork.TestResultRepository
                                            .GetAllAsync(testResult => testResult.StudentUser == user);

            var testResultsForThisTest = testResultsForThisStudent
                                         .Where(x => x.TestBaseId == testBaseId);
            int numOfTriesLeft = testBaseEntity.NumOfTries - testResultsForThisTest.Count();

            if (numOfTriesLeft <= 0)
            {
                return(null);
            }

            testBaseEntity.Topic = await _unitOfWork.TopicRepository.GetByIdAsync(testBaseEntity.TopicId);

            var testBaseDto = TestBaseMapper.Map(testBaseEntity);

            var craftedTest = new CraftedTest(user, testBaseDto);

            var topicEntity = await _unitOfWork.TopicRepository.GetByIdAsync(testBaseDto.TopicId);

            topicEntity.Subject = await _unitOfWork.SubjectRepository.GetByIdAsync(topicEntity.SubjectId);


            var topicDto          = TopicMapper.Map(topicEntity);
            var questionsEntities = await _unitOfWork.QuestionRepository
                                    .GetAllAsync(question => question.TopicId == testBaseDto.TopicId, null, new[] { "Topic" });

            topicDto.Questions = (ICollection <QuestionDto>)QuestionMapper.Map(questionsEntities);
            foreach (var questionDto in topicDto.Questions)
            {
                var answersEntities = await _unitOfWork.AnswerRepository
                                      .GetAllAsync(answer => answer.QuestionId == questionDto.Id);

                ;
                questionDto.Answers = (ICollection <AnswerDto>)AnswerMapper.Map(answersEntities);
                foreach (var answerDto in questionDto.Answers)
                {
                    answerDto.IsCorrect = false;
                }
            }

            var questionListForTopic = topicDto.Questions.ToList();

            questionListForTopic.Shuffle();

            var questionListIndex = 0;

            for (var numOfQuestionIndex = 0; numOfQuestionIndex < testBaseDto.NumOfQuestions; numOfQuestionIndex++)
            {
                if (numOfQuestionIndex >= questionListForTopic.Count)
                {
                    questionListIndex = 0;
                    questionListForTopic.Shuffle();
                }

                var question = new QuestionDto
                {
                    Id        = questionListForTopic[questionListIndex].Id,
                    TaskInfo  = questionListForTopic[questionListIndex].TaskInfo,
                    TopicId   = questionListForTopic[questionListIndex].TopicId,
                    TopicName = questionListForTopic[questionListIndex].TopicName
                };
                var answers = questionListForTopic[questionListIndex].Answers.ToList();
                answers.Shuffle();
                question.Answers      = answers;
                craftedTest.Questions = craftedTest.Questions.Append(question);

                questionListIndex++;
            }

            return(craftedTest);
        }