Beispiel #1
0
        public async Task <ActionResult <TestResultsDto> > CheckAnswers(TestAnswersDto answers, CancellationToken token)
        {
            var test = await testStorage.FindTest(answers.TestId, token);

            if (test == null)
            {
                return(UnprocessableEntity("test with this id does not exist"));
            }
            if (!User.OwnsResource(test))
            {
                return(Forbid());
            }

            var userAnswers = new Dictionary <Guid, IAnswer>();

            foreach (var answer in answers.Answers)
            {
                if (!userAnswers.ContainsKey(answer.Id))
                {
                    userAnswers.Add(answer.Id, answer.Answer);
                }
            }
            var verdicts = TestChecker.Check(userAnswers, test);

            var results = new TestResultsDto();

            foreach (var(key, _) in verdicts)
            {
                var exercise = test.Exercises.First(e => e.Id == key);
                results.Answers.Add(key, new ExerciseVerdictDto(verdicts[key], exercise.Answer));
                await storage.UpdateCardsAwareness(exercise.UsedCardsIds, verdicts[key]? 3 : -3, token);
            }

            return(Ok(results));
        }
Beispiel #2
0
        public IActionResult GetTestResults([FromQuery] string ids)
        {
            //java version: List<AdditionalInfo> additionalInfos = noKillSwitch(() -> sutController.getAdditionalInfoList());
            IList <AdditionalInfo> additionalInfos = _sutController.GetAdditionalInfoList();

            //Fake data here
            var dto = new TestResultsDto {
                AdditionalInfoList = Enumerable.Repeat(new AdditionalInfoDto {
                    LastExecutedStatement = "\"TODO: LastExecutedStatement\""
                }, additionalInfos.Count).ToList()
            };

            return(Ok(WrappedResponseDto <TestResultsDto> .WithData(dto)));
        }
Beispiel #3
0
        public async Task <OperationResult <Dtos.Test.TestResultsDto> > GetTestResultsDto(int testId, int userId)
        {
            var testAndQuestions = await dbContext.ScheduledTests
                                   .Include(x => x.TestTemplate)
                                   .ThenInclude(x => x.TestItems)
                                   .ThenInclude(x => x.Question)
                                   .ThenInclude(x => x.Answer)
                                   .ThenInclude(x => ((ChoiceAnswer)x).Choices)
                                   .Join(dbContext.UserAnswers, x => x.Id, y => y.ScheduledTestId, (x, y) => new { TestId = x.Id, x.TestTemplate, UserAnswer = y })
                                   .Where(x => x.UserAnswer.UserId == userId && x.TestId == testId)
                                   .ToListAsync();

            if (testAndQuestions.Count == 0)
            {
                return(new BadRequestError());
            }
            var test        = testAndQuestions[0].TestTemplate;
            var userAnswers = testAndQuestions.Select(x => x.UserAnswer).ToDictionary(x => x.QuestionId);
            var questions   = test.TestItems.Select(x => x.Question).ToList();

            var questionsWithResult = new List <QuestionWithResultDto>();

            foreach (var question in questions)
            {
                QuestionWithResultDto questionWithResultDto = questionMapper.MapToTestQuestionWithResultDto(question, userAnswers[question.Id]);
                questionsWithResult.Add(questionWithResultDto);
            }

            var testResultsDto = new TestResultsDto
            {
                Name = test.Name,
                QuestionsWithResult = questionsWithResult
            };

            return(testResultsDto);
        }
Beispiel #4
0
        public async Task StudentShouldGetTheirResult()
        {
            var scheduledTest         = controllerFixture.ScheduledTest;
            var testTemplate          = controllerFixture.TestTemplate;
            var testAuthorId          = testTemplate.AuthorId;
            var student               = fixture.OrganizationOwnerMembers[testAuthorId].First();
            var questions             = controllerFixture.Questions;
            var QuestionIdUserAnswers = controllerFixture.UserAnswers
                                        .Where(x => x.UserId == student.Id)
                                        .ToDictionary(x => x.QuestionId, x => x);

            var questionsWithResult = new List <QuestionWithResultDto>
            {
                new QuestionWithChoiceAnswerResultDto
                {
                    QuestionId = questions[0].Id,
                    Question   = questions[0].Content,
                    Choices    = questions[0]
                                 .Answer.As <ChoiceAnswer>()
                                 .Choices.Select((x, i) => new Dtos.Test.ChoiceDto
                    {
                        Value      = x.Content,
                        Correct    = x.Valid,
                        UserAnswer = (QuestionIdUserAnswers[questions[0].Id].As <ChoiceUserAnswer>().Value & (1 << i)) != 0
                    }).ToList()
                },
                new QuestionWithChoiceAnswerResultDto
                {
                    QuestionId = questions[1].Id,
                    Question   = questions[1].Content,
                    Choices    = questions[1]
                                 .Answer.As <ChoiceAnswer>()
                                 .Choices.Select((x, i) => new Dtos.Test.ChoiceDto
                    {
                        Value      = x.Content,
                        Correct    = x.Valid,
                        UserAnswer = (QuestionIdUserAnswers[questions[1].Id].As <ChoiceUserAnswer>().Value & (1 << i)) != 0
                    }).ToList()
                },
                new QuestionWithWrittenResultDto
                {
                    QuestionId    = questions[2].Id,
                    Question      = questions[2].Content,
                    CorrectAnswer = questions[2].Answer.As <WrittenAnswer>().Value,
                    UserAnswer    = QuestionIdUserAnswers[questions[2].Id].As <WrittenUserAnswer>().Value
                },
            };

            var expectedDto = new TestResultsDto
            {
                Name = testTemplate.Name,
                QuestionsWithResult = questionsWithResult
            };

            var token    = fixture.GenerateToken(student);
            var response = await fixture.RequestSender.GetAsync($"tests/{scheduledTest.Id}/result", token);

            response.StatusCode.Should().Be(HttpStatusCode.OK);
            var responseData = await response.Content.ReadAsStringAsync();

            var result = fixture.Deserialize <TestResultsDto>(responseData);

            result.Should().BeEquivalentTo(expectedDto);
        }