You are here

public function ClozeQuestion::getAnsweringForm in Quiz 8.4

Implementation of getAnsweringForm

Overrides QuizQuestion::getAnsweringForm

See also

QuizQuestion#getAnsweringForm($form_state, $rid)

File

question_types/cloze/lib/Drupal/cloze/ClozeQuestion.php, line 176
The main classes for the multichoice question type.

Class

ClozeQuestion
Extension of QuizQuestion.

Namespace

Drupal\cloze

Code

public function getAnsweringForm(array $form_state = NULL, $rid) {
  $form = parent::getAnsweringForm($form_state, $rid);
  $form['#theme'] = 'cloze_answering_form';
  $module_path = drupal_get_path('module', 'cloze');
  if (isset($this->node->learning_mode) && $this->node->learning_mode) {
    $form['#attached']['js'][] = $module_path . '/css/cloze.js';
    $question = $form['question']['#markup'];
    $this
      ->_answerJs($question);
  }
  $form['#attached']['css'][] = $module_path . '/css/cloze.css';
  $form['open_wrapper'] = array(
    '#markup' => '<div class="cloze-question">',
  );
  $body = $this->node->body
    ->getValue();
  foreach (_cloze_get_question_chunks($body['0']['value']) as $position => $chunk) {
    if (strpos($chunk, '[') === FALSE) {

      // this "tries[foobar]" hack is needed becaues question handler engine checks for input field
      // with name tries
      $form['tries[' . $position . ']'] = array(
        '#markup' => str_replace("\n", "<br/>", $chunk),
        '#prefix' => '<div class="form-item">',
        '#suffix' => '</div>',
      );
    }
    else {
      $chunk = str_replace(array(
        '[',
        ']',
      ), '', $chunk);
      $choices = explode(',', $chunk);
      if (count($choices) > 1) {
        $form['tries[' . $position . ']'] = array(
          '#type' => 'select',
          '#title' => '',
          '#options' => _cloze_shuffle_choices(drupal_map_assoc($choices)),
          '#required' => FALSE,
        );
      }
      else {
        $form['tries[' . $position . ']'] = array(
          '#type' => 'textfield',
          '#title' => '',
          '#size' => 32,
          '#required' => FALSE,
          '#attributes' => array(
            'autocomplete' => 'off',
            'class' => array(
              'answer-' . $position,
            ),
          ),
        );
      }
    }
  }
  $form['close_wrapper'] = array(
    '#markup' => '</div>',
  );
  if (isset($rid)) {
    $cloze_esponse = new ClozeResponse($rid, $this->node);
    $response = $cloze_esponse
      ->getResponse();
    if (is_array($response)) {
      foreach ($response as $key => $value) {
        $form["tries[{$key}]"]['#default_value'] = $value;
      }
    }
  }
  return $form;
}