コード例 #1
0
        public void CheckAnswers()
        {
            for (uint i = 1; i <= Questions.Count; i++)
            {
                List <string> userAnswers = new List <string>();
                if (UserAnswers.ContainsKey(i))
                {
                    userAnswers = UserAnswers[i];
                }
                else
                {
                    UserAnswers.Add(i, new List <string>());
                }
                List <bool> correctAnswers         = CorrectAnswers[i];
                List <bool> userCorrectnessAnswers = new List <bool>();

                for (int j = 0; j < userAnswers.Count; j++)
                {
                    if (correctAnswers[GetIdAnswer(userAnswers[j], i)])
                    {
                        userCorrectnessAnswers.Add(true);
                    }
                    else
                    {
                        userCorrectnessAnswers.Add(false);
                    }
                }
                UserCorrectnessAnswers.Add(i, userCorrectnessAnswers);
            }
        }
コード例 #2
0
 public void AddToUserAnswers(uint key, string val)
 {
     if (UserAnswers.ContainsKey(key))
     {
         UserAnswers[key].Add(val);
     }
     else
     {
         UserAnswers.Add(key, new List <string>());
         UserAnswers[key].Add(val);
     }
 }
コード例 #3
0
        /// <summary>
        /// Saves the answer for the current question
        /// </summary>
        /// <param name="answer">The answer itself</param>
        public void SaveAnswer(Answer answer)
        {
            // Save the answer
            UserAnswers.Add(answer);

            DI.Logger.Log($"User answer saved for question nr {CurrentQuestionString}.");

            // Create view data from the results page if it is allowed to be shown
            if (AreResultsAllowed)
            {
                QuestionViewModels.Add(Questions[mCurrentQuestion - 1].ToViewModel(answer, QuestionViewModels.Count));
            }
        }
コード例 #4
0
        private void OnChange(Answer answer)
        {
            var IsChecked = UserAnswers.FirstOrDefault(p => p.AnswerId == answer.AnswerId);

            if (IsChecked == null)
            {
                UserAnswers.Add(answer);
            }
            else
            {
                UserAnswers.Remove(IsChecked);
            }
            StateHasChanged();
        }
コード例 #5
0
        private async Task AddPublishedTestWithAnswers()
        {
            var questionWithSingleValidChoice = Questions[0];
            var questionWith3ValidChoices     = Questions[1];
            var questionWithWrittenAnswer     = Questions[2];
            var testTemplateQuestions         = new Dictionary <int, Question>
            {
                [questionWithSingleValidChoice.Id] = questionWithSingleValidChoice,
                [questionWith3ValidChoices.Id]     = questionWith3ValidChoices,
                [questionWithWrittenAnswer.Id]     = questionWithWrittenAnswer
            };

            var student1 = fixture.OrganizationOwnerMembers[fixture.UserId][0];
            var student2 = fixture.OrganizationOwnerMembers[fixture.UserId][1];

            var testPublishDate           = new DateTime(2021, 1, 7, 6, 0, 0, DateTimeKind.Utc);
            var startDate                 = testPublishDate.AddDays(1);
            var endDate                   = startDate.AddHours(3);
            var durationInMinutes         = 30;
            IEnumerable <int> studentsIds = new List <int> {
                student1.Id, student2.Id
            };

            ScheduledTest = new ScheduledTest(TestTemplate.Id, testPublishDate, startDate, endDate, durationInMinutes, studentsIds);
            var userTestStartDate = startDate.AddMinutes(10);

            foreach (var userTest in ScheduledTest.UserTests)
            {
                userTest.SetStartDate(userTestStartDate);
                userTest.SetEndDate(userTestStartDate.AddMinutes(5));
            }

            await fixture.ExecuteDbContext(db =>
            {
                db.ScheduledTests.Add(ScheduledTest);
                return(db.SaveChangesAsync());
            });

            int scheduledTestId = ScheduledTest.Id;

            //student1:
            UserAnswers.Add(
                UserAnswerGenerator.GenerateValidAnswer(questionWithSingleValidChoice, scheduledTestId, student1.Id));
            UserAnswers.Add(
                UserAnswerGenerator.GenerateValidAnswer(questionWith3ValidChoices, scheduledTestId, student1.Id));
            UserAnswers.Add(
                UserAnswerGenerator.GenerateValidAnswer(questionWithWrittenAnswer, scheduledTestId, student1.Id));

            //student2:
            UserAnswers.Add(
                UserAnswerGenerator.GenerateInvalidAnswer(questionWithSingleValidChoice, scheduledTestId, student2.Id));
            UserAnswers.Add(
                UserAnswerGenerator.GenerateUserAnswerWithNValidChoices(questionWith3ValidChoices, 1, scheduledTestId, student2.Id));
            UserAnswers.Add(
                UserAnswerGenerator.GenerateInvalidAnswer(questionWithWrittenAnswer, scheduledTestId, student2.Id));

            await fixture.ExecuteDbContext(db =>
            {
                db.UserAnswers.AddRange(UserAnswers);
                return(db.SaveChangesAsync());
            });

            foreach (var userTest in ScheduledTest.UserTests)
            {
                float studentScore = 0f;
                foreach (var studentAnswer in UserAnswers.Where(x => x.UserId == userTest.UserId))
                {
                    var   question      = testTemplateQuestions[studentAnswer.QuestionId];
                    float questionScore = question.Answer.GetScore(studentAnswer);
                    studentScore += questionScore;
                }
                StudentIdTestScore.Add(userTest.UserId, studentScore);
            }

            TestMaxScore = testTemplateQuestions.Values.Select(x => x.Answer.MaxScore).Aggregate((x, y) => x + y);
        }