You are here

public function MatchingQuestion::getAnsweringForm in Quiz 8.5

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. 6.x question_types/quiz_matching/src/Plugin/quiz/QuizQuestion/MatchingQuestion.php \Drupal\quiz_matching\Plugin\quiz\QuizQuestion\MatchingQuestion::getAnsweringForm()

Implementation of getAnsweringForm().

Overrides QuizQuestionEntityTrait::getAnsweringForm

See also

QuizQuestion::getAnsweringForm()

File

question_types/quiz_matching/src/Plugin/quiz/QuizQuestion/MatchingQuestion.php, line 41
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) {
  $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')
        ->referencedEntities()) {
        $form['user_answer'][$paragraph
          ->get('matching_user_question')
          ->referencedEntities()[0]
          ->id()]['#default_value'] = $paragraph
          ->get('matching_user_answer')
          ->referencedEntities()[0]
          ->id();
      }
    }
  }
  if (Drupal::config('quiz.settings')
    ->get('matching_shuffle_options', TRUE)) {
    $form = $this
      ->customShuffle($form);
  }
  $form['scoring_info'] = array(
    '#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;
}