You are here

public function ClozeQuestion::getAnsweringForm in Cloze 6

Same name and namespace in other branches
  1. 7 cloze.classes.inc \ClozeQuestion::getAnsweringForm()

Implementation of getAnsweringForm

See also

QuizQuestion#getAnsweringForm($form_state, $rid)

File

./cloze.classes.inc, line 113
The main classes for the short answer question type.

Class

ClozeQuestion
Extension of QuizQuestion.

Code

public function getAnsweringForm(array $form_state = NULL, $rid) {
  $form = parent::getAnsweringForm($form_state, $rid);
  $form['#theme'] = 'cloze_answering_form';
  drupal_add_css(drupal_get_path('module', 'cloze') . '/theme/cloze.css');
  $form['open_wrapper'] = array(
    '#type' => 'markup',
    '#value' => '<div class="cloze-question">',
  );
  foreach (_cloze_get_question_chunks($this->node->body) 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(
        '#type' => 'markup',
        '#value' => 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',
          ),
        );
      }
    }
  }
  $form['close_wrapper'] = array(
    '#type' => 'markup',
    '#value' => '</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;
}