You are here

public function MultichoiceQuestion::getCreationForm in Quiz 7.4

Same name and namespace in other branches
  1. 6.4 question_types/multichoice/multichoice.classes.inc \MultichoiceQuestion::getCreationForm()
  2. 7.6 question_types/multichoice/multichoice.classes.inc \MultichoiceQuestion::getCreationForm()
  3. 7 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 502
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_type_get_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;
  }

  // TODO The second parameter to this function call should be an array.
  $action = url('node/add/' . $type->type, $options);
  if (isset($this->node->nid)) {

    // TODO The second parameter to this function call should be an array.
    $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 being 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(
    '#markup' => '<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' => FALSE,
    );
    $form['alternatives'][$i]['#theme'][] = 'multichoice_alternative_creation';
    if (is_array($short)) {
      if ($short['score_if_chosen'] == $short['score_if_not_chosen']) {
        $correct_default = isset($short['correct']) ? $short['correct'] : FALSE;
      }
      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' => 'Multichoice.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' => 'text_format',
      '#base_type' => 'textarea',
      '#title' => t('Alternative !i', array(
        '!i' => $i + 1,
      )),
      '#default_value' => $short['answer']['value'],
      '#required' => $i < 2,
      '#format' => isset($short['answer']['format']) ? $short['answer']['format'] : NULL,
      '#rows' => 3,
    );
    $form['alternatives'][$i]['advanced'] = array(
      '#type' => 'fieldset',
      '#title' => t('Advanced options'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['alternatives'][$i]['advanced']['feedback_if_chosen'] = array(
      '#type' => 'text_format',
      '#base_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']['value'],
      '#format' => isset($short['feedback_if_chosen']['format']) ? $short['feedback_if_chosen']['format'] : NULL,
      '#rows' => 3,
    );

    // 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' => 'text_format',
      '#base_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']['value'],
      '#format' => isset($short['feedback_if_not_chosen']['format']) ? $short['feedback_if_not_chosen']['format'] : NULL,
      '#rows' => 3,
    );
    $default_value = isset($this->node->alternatives[$i]['score_if_chosen']) ? $this->node->alternatives[$i]['score_if_chosen'] : 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' => 'Multichoice.refreshCorrect(this)',
        'onkeyup' => 'Multichoice.refreshCorrect(this)',
        'onchange' => 'Multichoice.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' => 'Multichoice.refreshCorrect(this)',
        'onkeyup' => 'Multichoice.refreshCorrect(this)',
        'onchange' => 'Multichoice.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(
    '#markup' => '<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.
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => 'multichoice_add_alternative_ajax_callback',
      'wrapper' => 'placeholder',
      'effect' => 'slide',
      'method' => 'before',
    ),
  );
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'multichoice') . '/multichoice.js',
  );
  return $form;
}