You are here

public function MultichoiceQuestion::getAnsweringForm in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceQuestion::getAnsweringForm()
  2. 6.x question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceQuestion::getAnsweringForm()

Get the form through which the user will answer the question.

Question types should populate the form with selected values from the current result if possible.

Parameters

FormStateInterface $form_state: Form state.

QuizResultAnswer $quizQuestionResultAnswer: The quiz result answer.

Return value

array Form array.

Overrides QuizQuestionEntityTrait::getAnsweringForm

File

question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php, line 416

Class

MultichoiceQuestion
@QuizQuestion ( id = "multichoice", label = Plugin annotation @Translation("Multiple choice question"), handlers = { "response" = "\Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse" } )

Namespace

Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion

Code

public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) {
  $element = parent::getAnsweringForm($form_state, $quizQuestionResultAnswer);
  foreach ($this
    ->get('alternatives')
    ->referencedEntities() as $alternative) {

    /* @var $alternative Paragraph */
    $uuid = $alternative
      ->get('uuid')
      ->getString();
    $alternatives[$uuid] = $alternative;
  }

  // Build options list.
  $element['user_answer'] = [
    '#type' => 'tableselect',
    '#header' => [
      'answer' => t('Answer'),
    ],
    '#js_select' => FALSE,
    '#multiple' => $this
      ->get('choice_multi')
      ->getString(),
  ];

  // @todo see https://www.drupal.org/project/drupal/issues/2986517
  // There is some way to label the elements.
  foreach ($alternatives as $uuid => $alternative) {
    $vid = $alternative
      ->getRevisionId();
    $answer_markup = check_markup($alternative
      ->get('multichoice_answer')
      ->getValue()[0]['value'], $alternative
      ->get('multichoice_answer')
      ->getValue()[0]['format']);
    $element['user_answer']['#options'][$vid]['title']['data']['#title'] = $answer_markup;
    $element['user_answer']['#options'][$vid]['answer'] = $answer_markup;
  }
  if ($this
    ->get('choice_random')
    ->getString()) {

    // We save the choice order so that the order will be the same in the
    // answer report.
    $element['choice_order'] = array(
      '#type' => 'hidden',
      '#value' => implode(',', $this
        ->shuffle($element['user_answer']['#options'])),
    );
  }
  if ($quizQuestionResultAnswer
    ->isAnswered()) {
    $choices = $quizQuestionResultAnswer
      ->getResponse();
    if ($this
      ->get('choice_multi')
      ->getString()) {
      foreach ($choices as $choice) {
        $element['user_answer']['#default_value'][$choice] = TRUE;
      }
    }
    else {
      $element['user_answer']['#default_value'] = reset($choices);
    }
  }
  return $element;
}