You are here

function multichoice_render_question_form in Quiz 6.2

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

Print question to screen.

This assumes that node_prepare() has already been called on the $node.

Parameters

$context: Form processing context.

$node: Question node.

Return value

HTML output.

1 string reference to 'multichoice_render_question_form'
multichoice_render_question in ./multichoice.module

File

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

Code

function multichoice_render_question_form($context, $node) {
  $quiz = menu_get_object();

  // Get the quiz object.
  // Radio buttons for single selection questions, checkboxes for multiselect.
  if ($node->multiple_answers == 0) {
    $type = 'radios';
  }
  else {
    $type = 'checkboxes';
  }

  // Get options.
  $options = array();
  $fake_answer_id = 0;
  while (list($key, $answer) = each($node->answers)) {
    if (empty($answer['correct']) && !isset($answer['answer']) && empty($answer['feedback'])) {
      unset($node->answers[$key]);
    }
    else {
      if (!isset($answer['answer_id'])) {

        // We are probably in a preview. Generate fake answer IDs. Later we will disabled submit, too.
        $answer['answer_id'] = ++$fake_answer_id;
      }
      $options[$answer['answer_id']] = '<span class="multichoice_answer_text">' . check_markup($answer['answer'], $node->format, FALSE) . '</span>';
    }
  }

  // Why is this here and where is the ending tag?

  //$form['start'] = array('#type' => 'markup', '#value' => '<div class="multichoice_form">');

  // $node->body has already been escaped by node_prepare()
  $form['question'] = array(
    '#type' => 'markup',
    '#value' => check_markup($node->body, $node->format),
  );

  // Create form.
  $form['tries'] = array(
    '#type' => $type,
    '#options' => $options,
  );

  // Find out if there is already an answer (e.g. if Back has been clicked).
  // If there is an answer or answers, then put this into the $form['tries']
  // field.
  $rid = $_SESSION['quiz_' . $quiz->nid]['result_id'];
  $answers = _multichoice_get_response_answers($node, $rid);
  if (count($answers) > 0) {
    if ($type == 'radios') {

      // Only one for radio
      $form['tries']['#default_value'] = $answers[0];
    }
    elseif ($type == 'checkboxes') {

      // Array for checkboxes
      $form['tries']['#default_value'] = $answers;
    }
  }

  // Print a back button if necessary.
  if ($quiz->backwards_navigation == 1 && $node->question_number) {
    $form['back'] = array(
      '#type' => 'submit',
      '#value' => t('Back'),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  if ($fake_answer_id > 0) {

    // This is not a real quiz question
    $form['submit']['#disabled'] = TRUE;
    $form['submit']['#description'] = "Preview only. Submission is disabled.";
  }
  return $form;
}