You are here

function multichoice_form in Quiz 6.2

Same name and namespace in other branches
  1. 5.2 multichoice.module \multichoice_form()
  2. 5 multichoice.module \multichoice_form()
  3. 6.6 question_types/multichoice/multichoice.module \multichoice_form()
  4. 6.3 question_types/multichoice/multichoice.module \multichoice_form()
  5. 6.5 question_types/multichoice/multichoice.module \multichoice_form()

Implementation of hook_form(). Admin for create/update of a multichoice question.

File

./multichoice.module, line 95
Multiple choice question type for the Quiz module.

Code

function multichoice_form(&$node) {

  // Find out if this is a personality test.
  $is_personality = _multichoice_is_personality_question($node);
  $quiz_id = arg(3);
  if ($is_personality && !isset($quiz_id) || intval($quiz_id) == 0) {

    // We have to actually look up the Quiz because personality tests are linked to ONE SINGLE quiz. Not a great
    // design.
    $result = db_result(db_query("SELECT nid FROM {quiz_node_result_options} WHERE option_id = %d", $node->answers[0]['result_option']));
    if ($result) {
      $quiz_id = $result;
    }

    // else: This means the quiz that this question originally belonged to has been deleted. Let user fix it.
  }

  // Quiz ID used here to tie creation of a question to a specific quiz.
  if (!empty($quiz_id)) {
    $quiz = node_load((int) $quiz_id);
    $form['quiz_id'] = array(
      '#type' => 'value',
      '#value' => $quiz_id,
    );
    $form['quiz_vid'] = array(
      '#type' => 'value',
      '#value' => $quiz->vid,
    );
  }

  // Display the multichoice form.
  // Allow user to set title?
  if (user_access('allow user titles')) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => $node->title,
      '#required' => FALSE,
      '#description' => t('Add a title that will help distinguish this question from other questions. This will not be seen during the quiz.'),
    );
  }
  else {
    $form['title'] = array(
      '#type' => 'value',
      '#value' => $node->title,
    );
  }
  $form['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Question'),
    '#default_value' => $node->body,
    '#required' => TRUE,
  );
  $form['body_filter']['format'] = filter_form($node->format);
  if (user_access('allow multiple correct answers')) {
    $form['multiple_answers'] = array(
      '#type' => 'checkbox',
      '#title' => t('Multiple answers'),
      '#description' => t('Should the quiz-taker be allowed to select multiple correct answers?'),
      '#default_value' => $node->multiple_answers,
    );
  }
  else {
    $form['multiple_answers'] = array(
      '#type' => 'value',
      '#value' => FALSE,
    );
  }

  // Determine number of answer rows to display.
  if (!isset($node->rows)) {
    $node->rows = max(2, $node->answers ? count($node->answers) : variable_get('multichoice_default_answers', 5));
  }
  if ($_POST['more']) {
    $node->rows += 5;
  }
  $answers = $node->answers;

  // Display answer rows.
  $form['answers'] = array(
    '#type' => 'fieldset',
    '#title' => t('Choices'),
    '#tree' => TRUE,
    '#theme' => 'multichoice_form',
  );
  $form['scored_quiz'] = array(
    '#type' => 'value',
    '#value' => !empty($quiz_id) ? $quiz->pass_rate > 0 : TRUE,
  );
  for ($i = 0; $i < $node->rows; $i++) {

    // This is not a scored quiz, therefore no correct answers
    // so creator must assign answers to result options.
    if (isset($quiz_id) && $quiz->pass_rate == 0) {
      if (empty($result_options)) {
        $result_options = array(
          0 => 'None',
        );
        if (is_array($quiz->resultoptions)) {
          foreach ($quiz->resultoptions as $r_option) {
            $result_options[$r_option['option_id']] = $r_option['option_name'];
          }
        }
      }
      $form['answers'][$i]['result_option'] = array(
        '#type' => 'select',
        '#options' => $result_options,
        '#default_value' => $answers[$i]['result_option'],
      );
    }
    else {
      if (user_access('allow multiple correct answers')) {

        // Preview mode has $answers[$i]['correct']. A saved form
        // stores the flag in $answers[$i]['is_correct'].
        $correct = $answers[$i]['is_correct'] || $answers[$i]['correct'];
        $form['answers'][$i]['correct'] = array(
          '#type' => 'checkbox',
          '#default_value' => $correct,
        );
      }
    }
    $form['answers'][$i]['answer'] = array(
      '#type' => 'textarea',
      '#default_value' => $answers[$i]['answer'],
      '#cols' => 30,
      '#rows' => 2,
    );
    if (user_access('allow feedback')) {
      $form['answers'][$i]['feedback'] = array(
        '#type' => 'textarea',
        '#default_value' => $answers[$i]['feedback'],
        '#cols' => 30,
        '#rows' => 2,
      );
    }
    else {
      $form['answers'][$i]['feedback'] = array(
        '#type' => 'value',
        '#value' => '',
      );
    }
    if ($answers[$i]['answer_id']) {
      $form['answers'][$i]['delete'] = array(
        '#type' => 'checkbox',
        '#default_value' => 0,
      );
      $form['answers'][$i]['answer_id'] = array(
        '#type' => 'hidden',
        '#value' => $answers[$i]['answer_id'],
      );
    }
  }
  if (user_access('allow any number of answers')) {
    $form['more'] = array(
      '#type' => 'checkbox',
      '#title' => t('I need more answers'),
    );
  }
  if (!user_access('allow multiple correct answers')) {
    $form['correct'] = array(
      '#type' => 'select',
      '#title' => t('Correct Answer'),
      '#description' => t('Which of the answer choices is correct?'),
      '#required' => TRUE,
      '#size' => 1,
      '#options' => range(1, $node->rows),
      '#default_value' => _multichoice_find_correct($node->answers),
    );
  }

  // If coming from quiz view, go back there on submit.
  if (!empty($quiz_id)) {
    $form['#redirect'] = 'node/' . $quiz_id . '/questions';
  }
  return $form;
}