You are here

function multichoice_form in Quiz 5

Same name and namespace in other branches
  1. 5.2 multichoice.module \multichoice_form()
  2. 6.6 question_types/multichoice/multichoice.module \multichoice_form()
  3. 6.2 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().

File

./multichoice.module, line 67
Multiple choice question type for quiz module

Code

function multichoice_form(&$node) {

  // quiz id here to tie creating a question to a specific quiz
  $quiz_id = arg(3);
  if (!empty($quiz_id)) {
    $form['quiz_id'] = array(
      '#type' => 'value',
      '#value' => $quiz_id,
    );
  }

  // Display multichoice form
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $node->title,
    '#required' => TRUE,
    '#description' => t('Add a title that will help distinguish this question from other questions. This will not be seen during the quiz.'),
  );
  $form['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Question'),
    '#default_value' => $node->body,
    '#required' => TRUE,
  );
  $form['body_filter']['format'] = filter_form($node->format);
  $form['multiple_answers'] = array(
    '#type' => 'checkbox',
    '#title' => t('Multiple answers'),
    '#default_value' => $node->multiple_answers,
  );

  // Determine number of answer rows to display
  if (!isset($node->rows)) {
    $node->rows = max(2, $node->answers ? count($node->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',
  );
  for ($i = 0; $i < $node->rows; $i++) {
    $form['answers'][$i]['correct'] = array(
      '#type' => 'checkbox',
      '#default_value' => $answers[$i]['points'],
    );
    $form['answers'][$i]['answer'] = array(
      '#type' => 'textarea',
      '#default_value' => $answers[$i]['answer'],
      '#cols' => 30,
      '#rows' => 2,
    );
    $form['answers'][$i]['feedback'] = array(
      '#type' => 'textarea',
      '#default_value' => $answers[$i]['feedback'],
      '#cols' => 30,
      '#rows' => 2,
    );
    if ($answers[$i]['aid']) {
      $form['answers'][$i]['delete'] = array(
        '#type' => 'checkbox',
        '#default_value' => 0,
      );
      $form['answers'][$i]['aid'] = array(
        '#type' => 'hidden',
        '#value' => $answers[$i]['aid'],
      );
    }
  }
  $form['more'] = array(
    '#type' => 'checkbox',
    '#title' => t('I need more answers'),
  );

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