You are here

public function ChoiceQuestion::getCreationForm in Quiz 6.6

Implementation of getCreation form

(non-PHPdoc)

Overrides QuizQuestion::getCreationForm

See also

sites/all/modules/quiz-HEAD/question_types/quiz_question/QuizQuestion#getCreationForm()

File

question_types/choice/choice.classes.inc, line 339
The main classes for the choice question type.

Class

ChoiceQuestion
Implementation of QuizQuestion.

Code

public function getCreationForm($form_state) {
  $form = array();
  $type = node_get_types('type', $this->node);
  $action = '/node/add/' . $type->type;
  if ($node->nid) {
    $action = '/node/' . $this->node->nid . '/edit';
  }
  $form['#action'] = "?q={$action}";

  /*
   * Getting presets from the database
   */
  $form['#theme'][] = 'choice_creation_form';
  $form['alternatives'] = array(
    '#type' => 'fieldset',
    '#title' => t('Alternatives'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );
  $form['alternatives']['settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $default_settings = $this
    ->getDefaultAltSettings();
  $form['alternatives']['settings']['choice_multi'] = array(
    '#type' => 'checkbox',
    '#title' => t('Multiple answers'),
    '#description' => t('Allow any number of answers. If this box is not checked, one, and only one answer is allowed.'),
    '#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 one point if everything is correct. Zero points otherwise.'),
    '#default_value' => $default_settings['choice_boolean'],
    '#parents' => array(
      'choice_boolean',
    ),
  );
  $form['alternatives']['settings']['desc'] = array(
    '#type' => 'markup',
    '#value' => t('Your settings will be remembered.'),
  );
  $form['alternatives']['#theme'][] = 'choice_creation_form';
  $i = 0;
  if (isset($form_state['choice_count'])) {
    $choice_count = $form_state['choice_count'];
  }
  else {
    $choice_count = max(variable_get('choice_max_num_of_alts', 2), is_array($this->node->alternatives) ? count($this->node->alternatives) : 0);
  }
  for (; $i < $choice_count; $i++) {
    $short = $this->node->alternatives[$i];
    $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'][] = 'choice_alternative_creation';
    $form['alternatives'][$i]['correct'] = array(
      '#type' => 'checkbox',
      '#title' => t('Correct'),
      '#default_value' => $short['score_if_chosen'] > $short['score_if_not_chosen'],
      '#attributes' => array(
        'onchange' => 'refreshScores(this)',
      ),
    );
    $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',
    ));
    $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 = $this->node->alternatives[$i]['score_if_chosen'];
    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. Only used if multiple answers are allowed.'),
      '#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',
      ),
    );
  }
  $form['alternatives']["placeholder"] = array(
    '#type' => 'markup',
    '#value' => '<DIV id=\'placeholder\'></DIV>',
  );
  $form['alternatives']['choice_add_alternative'] = array(
    '#type' => 'submit',
    '#value' => t('Add more alternatives'),
    '#submit' => array(
      'choice_more_choices_submit',
    ),
    // If no javascript action.
    '#ahah' => array(
      'path' => "choice/add_alternative_js",
      'wrapper' => 'placeholder',
      'effect' => 'slide',
      'method' => 'before',
    ),
  );
  return $form;
}