You are here

public function MultichoiceQuestion::getCreationForm in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceQuestion::getCreationForm()

Implementation of getCreationForm().

See also

QuizQuestion::getCreationForm()

File

question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php, line 491

Class

MultichoiceQuestion
@QuizQuestion ( id = "multichoice", label = Plugin annotation @Translation("Multiple choice question"), handlers = { "response" = "\Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse" } )

Namespace

Drupal\quiz_multichoice\Plugin\quiz\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;
  }
  $action = url('quiz/add/' . $type->type, $options);
  if (isset($this->node->nid)) {
    $action = url('quiz/' . $this->node->nid . '/edit', $options);
  }
  $form['#action'] = $action;
  drupal_add_tabledrag('multichoice-alternatives-table', 'order', 'sibling', 'multichoice-alternative-weight');
  $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.'),
    '#weight' => 30,
  );
  $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.', array(
      '@quiz' => _quiz_get_quiz_name(),
    )),
    '#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',
    ),
  );
  $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['values']['op']) && $form_state['values']['op'] == t('Add choice')) {
    $form_state['choice_count']++;
  }
  else {
    $form_state['choice_count'] = max(variable_get('multichoice_def_num_of_alts', 2), isset($this->node->alternatives) ? count($this->node->alternatives) : 0);
  }
  $form['alternatives']['#prefix'] = '<div class="clear-block" id="multichoice-alternatives-wrapper">';
  $form['alternatives']['#suffix'] = '</div>';
  $form['alternatives']['#theme'] = array(
    'multichoice_alternative_creation_table',
  );
  for ($i = 0; $i < $form_state['choice_count']; $i++) {
    $short = isset($this->node->alternatives[$i]) ? $this->node->alternatives[$i] : NULL;
    $form['alternatives'][$i] = array(
      '#type' => 'container',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    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',
      '#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',
      '#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',
      '#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,
      '#default_value' => $default_value,
      '#description' => t("This score is added to the user's 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,
      '#default_value' => $default_value,
      '#description' => t("This score is added to the user's 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',
      ),
    );
    $form['alternatives'][$i]['weight'] = array(
      '#type' => 'textfield',
      '#size' => 2,
      '#attributes' => array(
        'class' => array(
          'multichoice-alternative-weight',
        ),
      ),
      '#default_value' => isset($this->node->alternatives[$i]['weight']) ? $this->node->alternatives[$i]['weight'] : $i,
    );

    // Add remove button.
    $form['alternatives'][$i]['remove_button'] = array(
      '#delta' => $i,
      '#name' => 'alternatives__' . $i . '__remove_button',
      '#type' => 'submit',
      '#value' => t('Remove'),
      '#validate' => array(),
      '#submit' => array(
        'multichoice_remove_alternative_submit',
      ),
      '#limit_validation_errors' => array(),
      '#ajax' => array(
        'callback' => 'multichoice_remove_alternative_ajax_callback',
        'effect' => 'fade',
        'wrapper' => 'multichoice-alternatives-wrapper',
      ),
      '#weight' => 1000,
    );
  }
  $form['alternatives']['multichoice_add_alternative'] = array(
    '#type' => 'button',
    '#value' => t('Add choice'),
    '#ajax' => array(
      'method' => 'replace',
      'wrapper' => 'multichoice-alternatives-wrapper',
      'callback' => 'multichoice_add_alternative_ajax_callback',
    ),
    '#weight' => 20,
    '#limit_validation_errors' => array(),
  );

  //$form['#attached']['js'] = array(

  // @todo not allowed

  //drupal_get_path('module', 'multichoice') . '/multichoice.js',

  //);
  return $form;
}