/// <summary> /// evaluate answer with multiple answer choices /// </summary> /// <param name="answer">answer to evaluate</param> /// <returns>calculated number of points for the answer</returns> private int EvaluateMultipleChoiceAnswer(TestSubmissionAnswer answer) { var correctAnswers = QuestionWithChoicesTools.GetCorrectAnswersLetters(answer.Question); var submittedAnswers = QuestionWithChoicesTools.GetSubmittedAnswersLetters(answer); var allPossibleAnswers = QuestionWithChoicesTools.GetAnswerChoicesLetters(answer.Question); int correct = 0; foreach (var possibleAnswer in allPossibleAnswers) { if (IsAnswerLetterCorrect(possibleAnswer, correctAnswers, submittedAnswers)) { correct++; } } double correctRatio = (double)correct / allPossibleAnswers.Length; return((int)Math.Round(correctRatio * answer.Question.Points)); }
/// <summary> /// check if value of <see cref="TestQuestion.CorrectAnswer"/> property is valid /// </summary> /// <param name="question">question to check</param> /// <returns>TRUE - valid, FALSE - invalid</returns> public bool HasValidCorrectAnswer(TestQuestion question) { if (question.Type == QuestionType.TextAnswer) { return(true); } else { var correctLetters = QuestionWithChoicesTools.GetCorrectAnswersLetters(question); var allChoicesLetters = QuestionWithChoicesTools.GetAnswerChoicesLetters(question); bool allAnswersValid = correctLetters.All(letter => allChoicesLetters.Contains(letter)); bool choicesValid = AreChoicesValid(allChoicesLetters); if (question.Type == QuestionType.SingleChoice) { return(allAnswersValid && choicesValid && correctLetters.Length == 1); } else { return(allAnswersValid && choicesValid); } } }