You are here

public function MatchingResponse::getFeedbackValues in Quiz 6.x

Same name and namespace in other branches
  1. 8.6 question_types/quiz_matching/src/Plugin/quiz/QuizQuestion/MatchingResponse.php \Drupal\quiz_matching\Plugin\quiz\QuizQuestion\MatchingResponse::getFeedbackValues()
  2. 8.5 question_types/quiz_matching/src/Plugin/quiz/QuizQuestion/MatchingResponse.php \Drupal\quiz_matching\Plugin\quiz\QuizQuestion\MatchingResponse::getFeedbackValues()

Implementation of getFeedbackValues().

Overrides QuizResultAnswerEntityTrait::getFeedbackValues

See also

QuizQuestionResponse::getFeedbackValues()

File

question_types/quiz_matching/src/Plugin/quiz/QuizQuestion/MatchingResponse.php, line 82

Class

MatchingResponse
Extension of QuizQuestionResponse

Namespace

Drupal\quiz_matching\Plugin\quiz\QuizQuestion

Code

public function getFeedbackValues() : array {
  $data = [];
  $answers = $this
    ->getQuizQuestion()
    ->getCorrectAnswer();
  foreach ($this
    ->get('matching_user_answer')
    ->referencedEntities() as $pair) {
    if ($pair
      ->get('matching_user_answer')
      ->getValue()) {
      $response[$pair
        ->get('matching_user_question')
        ->getValue()[0]['value']] = $pair
        ->get('matching_user_answer')
        ->getValue()[0]['value'];
    }
  }
  $penalty = $this
    ->getQuizQuestion()
    ->get('choice_penalty')->value;
  foreach ($this
    ->getQuizQuestion()
    ->get('quiz_matching')
    ->referencedEntities() as $paragraph) {
    $vid = $paragraph
      ->getRevisionId();
    $match = ($response[$vid] ?? -1) == $vid;
    $score = 0;
    if ($match) {
      $score = 1;
    }
    elseif ($penalty) {
      $score = -1;
    }
    $attempt_index = $response[$paragraph
      ->getRevisionId()];
    $data[] = [
      'choice' => $paragraph
        ->get('matching_question')
        ->getString(),
      'attempt' => isset($attempt_index) && $answers[$attempt_index] ? $answers[$attempt_index]
        ->get('matching_answer')
        ->getString() : '',
      'correct' => ($response[$vid] ?? -1) == $vid ? QuizUtil::icon('correct') : QuizUtil::icon('incorrect'),
      'score' => $score,
      'answer_feedback' => 'placeholder',
      // @todo: $answer->get('matching_feedback')->getString(),
      'solution' => $paragraph
        ->get('matching_answer')
        ->getString(),
    ];
  }
  return $data;
}