You are here

public function MultichoiceQuestion::getAnsweringForm in Quiz 7.5

Same name and namespace in other branches
  1. 6.4 question_types/multichoice/multichoice.classes.inc \MultichoiceQuestion::getAnsweringForm()
  2. 7.6 question_types/multichoice/multichoice.classes.inc \MultichoiceQuestion::getAnsweringForm()
  3. 7 question_types/multichoice/multichoice.classes.inc \MultichoiceQuestion::getAnsweringForm()
  4. 7.4 question_types/multichoice/multichoice.classes.inc \MultichoiceQuestion::getAnsweringForm()

Implementation of getAnsweringForm().

Overrides QuizQuestion::getAnsweringForm

See also

QuizQuestion::getAnsweringForm()

File

question_types/multichoice/multichoice.classes.inc, line 400
Multichoice classes.

Class

MultichoiceQuestion
Extension of QuizQuestion.

Code

public function getAnsweringForm(array $form_state = NULL, $result_id) {
  $element = parent::getAnsweringForm($form_state, $result_id);

  // 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.
  $element['#theme'] = 'multichoice_alternative';
  if (isset($result_id)) {

    // This question has already been answered. We load the answer.
    $response = new MultichoiceResponse($result_id, $this->node);
  }
  for ($i = 0; isset($this->node->alternatives[$i]); $i++) {
    $short = $this->node->alternatives[$i];
    $answer_markup = check_markup($short['answer']['value'], $short['answer']['format']);
    if (drupal_strlen($answer_markup) > 0) {
      $element['user_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.
    $element['choice_order'] = array(
      '#type' => 'hidden',
      '#value' => implode(',', $this
        ->shuffle($element['user_answer']['#options'])),
    );
  }
  if ($this->node->choice_multi) {
    $element['user_answer']['#type'] = 'checkboxes';
    $element['user_answer']['#title'] = t('Choose all that apply');
    if (isset($response)) {
      if (is_array($response
        ->getResponse())) {
        $element['user_answer']['#default_value'] = $response
          ->getResponse();
      }
    }
  }
  else {
    $element['user_answer']['#type'] = 'radios';
    $element['user_answer']['#title'] = t('Choose one');
    if (isset($response)) {
      $selection = $response
        ->getResponse();
      if (is_array($selection)) {
        $element['user_answer']['#default_value'] = array_pop($selection);
      }
    }
  }
  $element['#attached']['js'] = array(
    drupal_get_path('module', 'multichoice') . '/multichoice.js',
  );
  return $element;
}