You are here

public function TrueFalseQuestion::getAnsweringForm in Quiz 8.5

Same name and namespace in other branches
  1. 8.6 question_types/quiz_truefalse/src/Plugin/quiz/QuizQuestion/TrueFalseQuestion.php \Drupal\quiz_truefalse\Plugin\quiz\QuizQuestion\TrueFalseQuestion::getAnsweringForm()
  2. 6.x question_types/quiz_truefalse/src/Plugin/quiz/QuizQuestion/TrueFalseQuestion.php \Drupal\quiz_truefalse\Plugin\quiz\QuizQuestion\TrueFalseQuestion::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_truefalse/src/Plugin/quiz/QuizQuestion/TrueFalseQuestion.php, line 42

Class

TrueFalseQuestion
@QuizQuestion ( id = "truefalse", label = Plugin annotation @Translation("True/false question"), handlers = { "response" = "\Drupal\quiz_truefalse\Plugin\quiz\QuizQuestion\TrueFalseResponse" } )

Namespace

Drupal\quiz_truefalse\Plugin\quiz\QuizQuestion

Code

public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) {
  $element = parent::getAnsweringForm($form_state, $quizQuestionResultAnswer);
  $element += array(
    '#type' => 'radios',
    '#title' => t('Choose one'),
    '#options' => array(
      1 => t('True'),
      0 => t('False'),
    ),
  );
  if ($quizQuestionResultAnswer
    ->isAnswered()) {
    if ($quizQuestionResultAnswer
      ->getResponse() != '') {
      $element['#default_value'] = $quizQuestionResultAnswer
        ->getResponse();
    }
  }
  return $element;
}