Beispiel #1
0
        public void AnswerAttempConstructorTest()
        {
            answerAttempt = new AnswerAttempt();

            Assert.NotNull(answerAttempt);
            Assert.IsType <AnswerAttempt>(answerAttempt);
        }
Beispiel #2
0
        public async Task <ServiceResult <bool> > Answer(int id, AnswerAttempt answer)
        {
            var question = await dbContext.Questions.FindAsync(id);

            var correct = false;

            if (question == null)
            {
                return(NotFound <bool>());
            }

            else if (question.Type == QuestionType.Binary && answer.Correct != null)
            {
                correct = question.Answer((bool)answer.Correct);
            }

            else if (question.Type == QuestionType.Select && answer.Answers != null)
            {
                correct = question.Answer(answer.Answers);
            }

            else if (question.Type == QuestionType.Input && answer.TextAnswers != null)
            {
                correct = question.Answer(answer.TextAnswers);
            }

            return(Ok(correct));
        }
Beispiel #3
0
        public void AnswerAttemptPropertiesTest()
        {
            validApplicant = new AppUser()
            {
                Id = validApplicantID
            };
            validAnswer = new Answer()
            {
                ID = validAnswerID
            };
            validTest = new Test();

            answerAttempt = new AnswerAttempt()
            {
                ID            = validAnswerAttemptID,
                AnswerID      = validAnswer.ID,
                Applicant     = validApplicant,
                ApplicantID   = validApplicant.Id,
                SelctedAnswer = validAnswer,
                Test          = validTest
            };

            Assert.NotNull(answerAttempt.Applicant);
            Assert.NotNull(answerAttempt.SelctedAnswer);
            Assert.NotNull(answerAttempt.Test);

            Assert.Equal(validAnswerAttemptID, answerAttempt.ID);
            Assert.Equal(validApplicant, answerAttempt.Applicant);
            Assert.Equal(validApplicantID, answerAttempt.ApplicantID);
            Assert.Equal(validAnswer, answerAttempt.SelctedAnswer);
            Assert.Equal(validAnswerID, answerAttempt.AnswerID);
            Assert.Equal(validTest, answerAttempt.Test);
        }
Beispiel #4
0
        /// <summary>
        /// Add answer to subscriber game result
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="subscriberId"></param>
        /// <param name="gameId"></param>
        /// <param name="clueId"></param>
        /// <param name="answer"></param>
        /// <param name="score"></param>
        public void UpdateSubscriberGameResult(Guid tournamentId, Guid subscriberId, Guid gameId, Guid clueId, string answer, int score)
        {
            var tournament = TournamentRepositoryFake.Tournaments.FirstOrDefault(t => t.Id == tournamentId);

            if (tournament == null)
            {
                throw new Exception("Tournament not found!");
            }

            var game = TournamentRepositoryFake.TournamentGames.FirstOrDefault(tg => tg.TournamentId == tournamentId && tg.Id == gameId);

            if (game == null)
            {
                throw new Exception("Game not found!");
            }
            if (game.Clues.FirstOrDefault(c => c.Id == clueId) == null)
            {
                throw new Exception("Clue not found!");
            }

            var tournamentSubscriber = TournamentRepositoryFake.TournamentSubscribers.FirstOrDefault(ts => ts.TournamentId == tournamentId && ts.Id == subscriberId);

            if (tournamentSubscriber == null)
            {
                throw new Exception("Tournament subscriber not found!");
            }

            var subscriberGameResults = TournamentRepositoryFake.SubscriberGameResults.FirstOrDefault(sgr => sgr.TournamentId == tournamentId && sgr.GameId == gameId && sgr.SubscriberId == subscriberId);

            if (subscriberGameResults == null)
            {
                subscriberGameResults = new SubscriberGameResult
                {
                    Id             = Guid.NewGuid(),
                    TournamentId   = tournamentId,
                    GameId         = gameId,
                    SubscriberId   = subscriberId,
                    AnswerAttempts = new List <AnswerAttempt>()
                };
                // Add to repository
                TournamentRepositoryFake.SubscriberGameResults.Add(subscriberGameResults);
            }
            var answerAttempt = subscriberGameResults.AnswerAttempts.FirstOrDefault(aa => aa.ClueId == clueId);

            if (answerAttempt == null)
            {
                answerAttempt = new AnswerAttempt
                {
                    Id     = Guid.NewGuid(),
                    ClueId = clueId
                };
                subscriberGameResults.AnswerAttempts.Add(answerAttempt);
            }
            answerAttempt.Answer = answer;
            answerAttempt.Score  = score;
        }
Beispiel #5
0
        public async Task <ServiceResult <bool> > Answer(int id, AnswerAttempt attempt)
        {
            var result = new ServiceResult <bool>();

            var question = await questions.FindAsync(id);

            if (question == null)
            {
                return(result.NotFound());
            }

            return(result.Ok(question.Answer(attempt)));
        }
Beispiel #6
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>   Saves an answer attempt for each question the applicant answers. </summary>
        ///
        /// <param name="submittedAnswers"> The answers. </param>
        /// <param name="applicant">        The applicant. </param>
        /// <param name="test">             The test. </param>
        ///-------------------------------------------------------------------------------------------------
        public void SaveAnswerAttempt(List <Answer> submittedAnswers, AppUser applicant, Test test)
        {
            var tempAppUser = db.Users.SingleOrDefault(user => user.Email == applicant.Email);

            foreach (var answer in submittedAnswers)
            {
                var tempAnswer = db.Answers.SingleOrDefault(a => a.AnswerBody == answer.AnswerBody);

                AnswerAttempt answerAttempt = new AnswerAttempt
                {
                    AnswerID    = tempAnswer.ID,
                    Applicant   = tempAppUser,
                    ApplicantID = tempAppUser.Id,
                    Test        = test
                };

                db.AnswerAttempts.Add(answerAttempt);
            }
        }
        public static bool Answer(this Question question, AnswerAttempt attempt)
        {
            switch (question.Type)
            {
            case QuestionType.Binary:
                return(attempt.Correct != null && question.Correct == attempt.Correct);

            case QuestionType.Single:
            case QuestionType.Multiple:

                if (attempt.CorrectAnswers == null)
                {
                    return(false);
                }

                var correctAnswers = question.Answers
                                     .Where(a => (bool)a.Correct)
                                     .Select(a => a.Id)
                                     .ToArray();

                return(attempt.CorrectAnswers.SequenceEqual(correctAnswers));

            case QuestionType.SingleInput:
            case QuestionType.MultipleInput:

                if (attempt.CorrectInputs == null)
                {
                    return(false);
                }

                var correctInputs = question.Answers.Select(a => a.Text).ToArray();

                return(attempt.CorrectInputs.SequenceEqual(correctInputs));    // normalize
            }

            return(false);
        }
 public async Task <ActionResult <bool> > Answer(int id, AnswerAttempt answer)
 => await questionService.Answer(id, answer).MapToAction();
 public async Task <IActionResult> Answer(int id, AnswerAttempt attempt) => (await questionService.Answer(id, attempt)).Result();