You are here

public function ClozeQuestion::evaluateAnswer in Quiz 8.4

Evaluate the correctness of an answer based on the correct answer and evaluation method.

File

question_types/cloze/lib/Drupal/cloze/ClozeQuestion.php, line 278
The main classes for the multichoice question type.

Class

ClozeQuestion
Extension of QuizQuestion.

Namespace

Drupal\cloze

Code

public function evaluateAnswer($user_answer) {
  $body = $this->node->body
    ->getValue();
  $correct_answer = _cloze_get_correct_answer_chunks($body['0']['value']);
  $total_answer = count($correct_answer);
  $correct_answer_count = 0;
  if ($total_answer == 0) {
    return $this
      ->getMaximumScore();
  }
  foreach ($correct_answer as $key => $value) {
    if (_cloze_get_clean_text($correct_answer[$key]) == _cloze_get_clean_text($user_answer[$key])) {
      $correct_answer_count++;
    }
  }
  $score = $correct_answer_count / $total_answer * $this
    ->getMaximumScore();
  return round($score);
}