//Make sure that all answers pertain to the intended quiz and are all for the same question. public IEnumerable <Answer> ValidateAnswers(Data.Entities.Quizzes.Quiz quiz, IEnumerable <Answer> allAnswers, IEnumerable <long> answerIds) { var setAnswerIds = new HashSet <long>(answerIds); var answers = allAnswers.Where(a => setAnswerIds.Contains(a.Id)).ToList(); long questionId = -1; foreach (var answer in answers) { if (questionId == -1) { questionId = answer.QuestionId; if (answer.Question.QuizId != quiz.Id) { throw new QuizAnswersNotForQuizException(answer.Id, quiz.Id, answer.Question.QuizId); } } else if (questionId != answer.QuestionId) { throw new QuizAnswersNotSameQuestionException(answer.Id, questionId, answer.QuestionId); } } return(answers); }
//Determines the next question to provide for the person based on the questions which they have not yet answered. public Question NextQuestion(Data.Entities.Quizzes.Quiz quiz, Person person, IEnumerable <Question> allQuestions, IEnumerable <PersonAnswer> allPersonAnswers) { var questions = allQuestions.Where(q => q.QuizId == quiz.Id); var questionIdsOfPersonAnswers = new HashSet <long>(allPersonAnswers.Select(a => a.Answer.QuestionId)); //We only are concerned with unanswered questions. var availableQuestions = questions.Where(q => !questionIdsOfPersonAnswers.Contains(q.Id)); if (quiz.RandomizeQuestions) { availableQuestions = availableQuestions.Shuffle(_randomizer); } var question = availableQuestions.FirstOrDefault(); if (question != null && question.RandomizeAnswers) { question.Answers = question.Answers.Shuffle(_randomizer).ToList(); } return(question); }