You are here

public function MultichoiceQuestion::getAnsweringForm in Quiz 7.6

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

Generates the question element.

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

Overrides QuizQuestion::getAnsweringForm

File

question_types/multichoice/multichoice.classes.inc, line 423
The main classes for the multichoice question type.

Class

MultichoiceQuestion
Extension of QuizQuestion.

Code

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

  //$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.
   */
  $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['#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;
}