You are here

public function MultichoiceQuestion::getCreationForm in Quiz 6.4

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

Implementation of getCreationForm

Overrides QuizQuestion::getCreationForm

See also

QuizQuestion#getCreationForm()

File

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

Class

MultichoiceQuestion
Extension of QuizQuestion.

Code

public function getCreationForm(array $form_state = NULL) {
  $form = array();
  $type = node_get_types('type', $this->node);

  // We add #action to the form because of the use of ajax
  $options = array();
  $get = $_GET;
  unset($get['q']);
  if (!empty($get)) {
    $options['query'] = $get;
  }
  $action = url('node/add/' . $type->type, $options);
  if (isset($this->node->nid)) {
    $action = url('node/' . $this->node->nid . '/edit', $options);
  }
  $form['#action'] = $action;
  $form['alternatives'] = array(
    '#type' => 'fieldset',
    '#title' => t('Answer'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -4,
    '#tree' => TRUE,
  );

  // Get the nodes settings, users settings or default settings
  $default_settings = $this
    ->getDefaultAltSettings();
  $form['alternatives']['settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#description' => t('Your settings will be remembered.'),
  );
  $form['alternatives']['settings']['choice_multi'] = array(
    '#type' => 'checkbox',
    '#title' => t('Multiple answers'),
    '#description' => t('Allow any number of answers(checkboxes are used). If this box is not checked, one, and only one answer is allowed(radiobuttons are used).'),
    '#default_value' => $default_settings['choice_multi'],
    '#parents' => array(
      'choice_multi',
    ),
  );
  $form['alternatives']['settings']['choice_random'] = array(
    '#type' => 'checkbox',
    '#title' => t('Random order'),
    '#description' => t('Present alternatives in random order when quiz is beeing taken.'),
    '#default_value' => $default_settings['choice_random'],
    '#parents' => array(
      'choice_random',
    ),
  );
  $form['alternatives']['settings']['choice_boolean'] = array(
    '#type' => 'checkbox',
    '#title' => t('Simple scoring'),
    '#description' => t('Give max score if everything is correct. Zero points otherwise.'),
    '#default_value' => $default_settings['choice_boolean'],
    '#parents' => array(
      'choice_boolean',
    ),
  );

  // Add helper tag where we will place the input selector for all the textareas.
  $form['alternatives']['input_format_all'] = array(
    '#type' => 'markup',
    '#value' => '<DIV id="input-all-ph"></DIV>',
  );
  $form['alternatives']['#theme'][] = 'multichoice_creation_form';
  $i = 0;

  // choice_count might be stored in the form_state after an ajax callback
  if (isset($form_state['choice_count'])) {
    $choice_count = $form_state['choice_count'];
  }
  else {
    $choice_count = max(variable_get('multichoice_def_num_of_alts', 2), isset($this->node->alternatives) ? count($this->node->alternatives) : 0);
  }
  for (; $i < $choice_count; $i++) {
    $short = isset($this->node->alternatives[$i]) ? $this->node->alternatives[$i] : NULL;
    $form['alternatives'][$i] = array(
      '#type' => 'fieldset',
      '#title' => t('Alternative !i', array(
        '!i' => $i + 1,
      )),
      '#collapsible' => TRUE,
      '#collapsed' => !($i < 2 || isset($short['answer'])),
    );
    $form['alternatives'][$i]['#theme'][] = 'multichoice_alternative_creation';
    if (is_array($short)) {
      if ($short['score_if_chosen'] == $short['score_if_not_chosen']) {
        $correct_default = $short['correct'];
      }
      else {
        $correct_default = $short['score_if_chosen'] > $short['score_if_not_chosen'];
      }
    }
    else {
      $correct_default = FALSE;
    }
    $form['alternatives'][$i]['correct'] = array(
      '#type' => 'checkbox',
      '#title' => t('Correct'),
      '#default_value' => $correct_default,
      '#attributes' => array(
        'onchange' => 'refreshScores(this, ' . variable_get('multichoice_def_scoring', 0) . ')',
      ),
    );

    // We add id to be able to update the correct alternatives if the node is updated, without destroying
    // existing answer reports
    $form['alternatives'][$i]['id'] = array(
      '#type' => 'value',
      '#value' => $short['id'],
    );
    $form['alternatives'][$i]["answer"] = array(
      '#type' => 'textarea',
      '#title' => t('Alternative !i', array(
        '!i' => $i + 1,
      )),
      '#default_value' => $short['answer'],
      '#required' => $i < 2,
    );
    $form['alternatives'][$i]['format'] = filter_form($short['answer_format'], NULL, array(
      'alternatives',
      $i,
      'answer_format',
    ));
    $form['alternatives'][$i]['advanced'] = array(
      '#type' => 'fieldset',
      '#title' => t('Advanced options'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['alternatives'][$i]['advanced']['feedback_if_chosen'] = array(
      '#type' => 'textarea',
      '#title' => t('Feedback if chosen'),
      '#description' => t('This feedback is given to users who chooses this alternative.'),
      '#parents' => array(
        'alternatives',
        $i,
        'feedback_if_chosen',
      ),
      '#default_value' => $short['feedback_if_chosen'],
    );
    $form['alternatives'][$i]['advanced']['format'] = filter_form($short['feedback_if_chosen_format'], NULL, array(
      'alternatives',
      $i,
      'feedback_if_chosen_format',
    ));

    // We add 'helper' to trick the current version of the wysiwyg module to add an editor to several
    // textareas in the same fieldset
    $form['alternatives'][$i]['advanced']['helper']['feedback_if_not_chosen'] = array(
      '#type' => 'textarea',
      '#title' => t('Feedback if not chosen'),
      '#description' => t('This feedback is given to users who doesn\'t choose this alternative.'),
      '#parents' => array(
        'alternatives',
        $i,
        'feedback_if_not_chosen',
      ),
      '#default_value' => $short['feedback_if_not_chosen'],
    );
    $form['alternatives'][$i]['advanced']['helper']['format'] = filter_form($short['feedback_if_not_chosen_format'], NULL, array(
      'alternatives',
      $i,
      'feedback_if_not_chosen_format',
    ));
    $default_value = isset($this->node->alternatives[$i]['score_if_chosen']) ? $this->node->alternatives[$i]['score_if_chosen'] : 0;
    if (!isset($default_value)) {
      $default_value = '0';
    }
    $form['alternatives'][$i]['advanced']['score_if_chosen'] = array(
      '#type' => 'textfield',
      '#title' => t('Score if chosen'),
      '#size' => 4,
      '#maxlength' => 4,
      '#default_value' => $default_value,
      '#description' => t('This score is added to the users total score if the user chooses this alternative.'),
      '#attributes' => array(
        'onkeypress' => 'refreshCorrect(this)',
        'onkeyup' => 'refreshCorrect(this)',
        'onchange' => 'refreshCorrect(this)',
      ),
      '#parents' => array(
        'alternatives',
        $i,
        'score_if_chosen',
      ),
    );
    $default_value = $short['score_if_not_chosen'];
    if (!isset($default_value)) {
      $default_value = '0';
    }
    $form['alternatives'][$i]['advanced']['score_if_not_chosen'] = array(
      '#type' => 'textfield',
      '#title' => t('Score if not chosen'),
      '#size' => 4,
      '#maxlength' => 4,
      '#default_value' => $default_value,
      '#description' => t('This score is added to the users total score if the user doesn\'t choose this alternative. Only used if multiple answers are allowed.'),
      '#attributes' => array(
        'onkeypress' => 'refreshCorrect(this)',
        'onkeyup' => 'refreshCorrect(this)',
        'onchange' => 'refreshCorrect(this)',
      ),
      '#parents' => array(
        'alternatives',
        $i,
        'score_if_not_chosen',
      ),
    );
  }

  // ahah helper tag. New questions will be inserted before this tag
  $form['alternatives']["placeholder"] = array(
    '#type' => 'markup',
    '#value' => '<DIV id=\'placeholder\'></DIV>',
  );

  // We can't send the get values to the ahah callback the normal way, so we do it like this.
  $form['get'] = array(
    '#type' => 'value',
    '#value' => $get,
  );
  $form['alternatives']['multichoice_add_alternative'] = array(
    '#type' => 'submit',
    '#value' => t('Add more alternatives'),
    '#submit' => array(
      'multichoice_more_choices_submit',
    ),
    // If no javascript action.
    '#ahah' => array(
      'path' => 'node/add/multichoice/add_alternative_ahah',
      'wrapper' => 'placeholder',
      'effect' => 'slide',
      'method' => 'before',
    ),
  );
  return $form;
}