You are here

public function MatchingQuestion::getAnsweringForm in Quiz 6.x

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

Overrides QuizQuestionEntityTrait::getAnsweringForm

File

question_types/quiz_matching/src/Plugin/quiz/QuizQuestion/MatchingQuestion.php, line 39
Matching classes.

Class

MatchingQuestion
@QuizQuestion ( id = "matching", label = Plugin annotation @Translation("Matching question"), handlers = { "response" = "\Drupal\quiz_matching\Plugin\quiz\QuizQuestion\MatchingResponse" } )

Namespace

Drupal\quiz_matching\Plugin\quiz\QuizQuestion

Code

public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) : array {
  $form = parent::getAnsweringForm($form_state, $quizQuestionResultAnswer);
  $answers[''] = '';
  foreach ($this
    ->get('quiz_matching')
    ->referencedEntities() as $alternative) {

    /* @var $alternative Paragraph */
    $questions[$alternative
      ->id()] = $alternative
      ->get('matching_question')
      ->getString();
    $answers[$alternative
      ->id()] = $alternative
      ->get('matching_answer')
      ->getString();
  }
  foreach ($questions as $id => $question) {

    // Build options list.
    $form['user_answer'][$id] = [
      '#title' => $question,
      '#type' => 'select',
      '#options' => $answers,
    ];
  }
  if ($paragraphs = $quizQuestionResultAnswer
    ->getResponse()) {

    // If this question already has been answered.
    foreach ($paragraphs as $paragraph) {
      if ($paragraph
        ->get('matching_user_answer')->value) {
        $form['user_answer'][$paragraph
          ->get('matching_user_question')->value]['#default_value'] = $paragraph
          ->get('matching_user_answer')->value;
      }
    }
  }
  if (Drupal::config('quiz_matching.settings')
    ->get('shuffle')) {
    $form['user_answer'] = $this
      ->customShuffle($form['user_answer']);
  }
  $form['scoring_info'] = [
    '#access' => !empty($this
      ->get('choice_penalty')
      ->getString()),
    '#markup' => '<p><em>' . t('You lose points by selecting incorrect options. You may leave an option blank to avoid losing points.') . '</em></p>',
  ];
  return $form;
}