You are here

public function ShortAnswerResponse::score in Quiz 8.5

Same name and namespace in other branches
  1. 8.6 question_types/quiz_short_answer/src/Plugin/quiz/QuizQuestion/ShortAnswerResponse.php \Drupal\quiz_short_answer\Plugin\quiz\QuizQuestion\ShortAnswerResponse::score()
  2. 6.x question_types/quiz_short_answer/src/Plugin/quiz/QuizQuestion/ShortAnswerResponse.php \Drupal\quiz_short_answer\Plugin\quiz\QuizQuestion\ShortAnswerResponse::score()

Calculate the unscaled score in points for this question response.

Parameters

array $values: A part of form state values with the question input from the user.

Return value

int The unscaled point value of the answer.

Overrides QuizResultAnswerEntityTrait::score

File

question_types/quiz_short_answer/src/Plugin/quiz/QuizQuestion/ShortAnswerResponse.php, line 16

Class

ShortAnswerResponse
Extension of QuizResultAnswer.

Namespace

Drupal\quiz_short_answer\Plugin\quiz\QuizQuestion

Code

public function score(array $values) {
  $question = $this
    ->getQuizQuestion();
  $correct = $question
    ->get('short_answer_correct')
    ->getString();
  $this
    ->set('short_answer', $values['answer']);
  switch ($question
    ->get('short_answer_evaluation')
    ->getString()) {
    case ShortAnswerQuestion::ANSWER_MANUAL:
      $this
        ->set('is_evaluated', 0);
      break;
    case ShortAnswerQuestion::ANSWER_MATCH:
      $this
        ->set('is_evaluated', 1);
      if ($values['answer'] == $correct) {
        return $question
          ->getMaximumScore();
      }
      break;
    case ShortAnswerQuestion::ANSWER_INSENSITIVE_MATCH:
      $this
        ->set('is_evaluated', 1);
      if (strtolower($values['answer']) == strtolower($correct)) {
        return $question
          ->getMaximumScore();
      }
      break;
    case ShortAnswerQuestion::ANSWER_REGEX:
      $this
        ->set('is_evaluated', 1);
      if (preg_match($correct, $values['answer']) > 0) {
        return $question
          ->getMaximumScore();
      }
      break;
  }
}