You are here

function multichoice_render_question_form in Quiz 6.6

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.2 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 question_types/multichoice/multichoice.module

File

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

Code

function multichoice_render_question_form($context, $node) {

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

  // 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;
      }
      if (!$taking_quiz && $answer['is_correct'] == 1) {
        $options[$answer['answer_id']] = '<span class="multichoice_answer_correct">' . check_markup($answer['answer'], $node->format, FALSE) . ' <strong>(correct)</strong></span>';
      }
      else {
        $options[$answer['answer_id']] = '<span class="multichoice_answer_text">' . check_markup($answer['answer'], $node->format, FALSE) . '</span>';
      }
    }
  }
  $form['question'] = array(
    '#type' => 'markup',
    '#value' => check_markup($node->body, $node->format, FALSE),
  );

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

    // 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('Next'),
    );

    /* TODO should this be deleted?
       $form['op'] = array(
       '#type' => 'submit',
       '#value' => t('Skip'),
       );
       */
  }
  else {

    // reconstruct form so type is first (at top)
    // TODO is there a better way?  like unshift an assoc tuple?
    $type = node_get_types('type', $node);
    $newform['question_type'] = array(
      '#type' => 'markup',
      '#value' => '<div class="question_type_name">' . $type->name . '</div>',
    );
    $newform += $form;
    $form = $newform;
    if (user_access('view quiz question solutions')) {

      // not in quiz context so no ability to process answer
      $form['tries']['#disabled'] = TRUE;
    }
    else {
      $form['tries'] = array(
        '#type' => 'markup',
        '#value' => '<em>Answers hidden</em>.',
      );
    }
  }
  if ($fake_answer_id > 0) {

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