You are here

public function ClozeQuestion::evaluateAnswer in Cloze 7

Same name and namespace in other branches
  1. 6 cloze.classes.inc \ClozeQuestion::evaluateAnswer()

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

File

./cloze.classes.inc, line 247
The main classes for the cloze question type. These inherit or implement code found in quiz_question.classes.inc.

Class

ClozeQuestion
Extension of QuizQuestion.

Code

public function evaluateAnswer($user_answer) {
  $correct_answer = _cloze_get_correct_answer_chunks($this->node->body[LANGUAGE_NONE]['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);
}