You are here

public function MultichoiceQuestion::getAnsweringForm in Quiz 8.4

Generates the question form.

This is called whenever a question is rendered, either to an administrator or to a quiz taker.

Overrides QuizQuestion::getAnsweringForm

File

question_types/multichoice/lib/Drupal/multichoice/MultichoiceQuestion.php, line 398
The main classes for the multichoice question type.

Class

MultichoiceQuestion
Extension of QuizQuestion.

Namespace

Drupal\multichoice

Code

public function getAnsweringForm(array $form_state = NULL, $rid) {
  $form = parent::getAnsweringForm($form_state, $rid);

  //$form['#theme'] = 'multichoice_answering_form';

  /* We use an array looking key to be able to store multiple answers in tries.
   * At the moment all user answers have to be stored in tries. This is something we plan
   * to fix in quiz 5.x.
   */
  $form['tries[answer]'] = array(
    '#options' => array(),
    '#theme' => 'multichoice_alternative',
  );
  if (isset($rid)) {

    // This question has already been answered. We load the answer.
    $response = new MultichoiceResponse($rid, $this->node);
  }
  for ($i = 0; isset($this->node->alternatives[$i]); $i++) {
    $short = $this->node->alternatives[$i];
    $answer_markup = check_markup($short['answer'], $short['answer_format']);
    if (drupal_strlen($answer_markup) > 0) {
      $form['tries[answer]']['#options'][$short['id']] = $answer_markup;
    }
  }
  if ($this->node->choice_random) {

    // We save the choice order so that the order will be the same in the answer report
    $form['tries[choice_order]'] = array(
      '#type' => 'hidden',
      '#value' => implode(',', $this
        ->shuffle($form['tries[answer]']['#options'])),
    );
  }
  if ($this->node->choice_multi) {
    $form['tries[answer]']['#type'] = 'checkboxes';
    $form['tries[answer]']['#title'] = t('Choose');
    if (isset($response)) {
      if (is_array($response
        ->getResponse())) {
        $form['tries[answer]']['#default_value'] = $response
          ->getResponse();
      }
    }
  }
  else {
    $form['tries[answer]']['#type'] = 'radios';
    $form['tries[answer]']['#title'] = t('Choose one');
    if (isset($response)) {
      $selection = $response
        ->getResponse();
      if (is_array($selection)) {
        $form['tries[answer]']['#default_value'] = array_pop($selection);
      }
    }
  }
  return $form;
}