You are here

function quiz_take_question in Quiz 7.6

Same name and namespace in other branches
  1. 7.5 quiz.module \quiz_take_question()

Take a quiz questions.

Parameters

type $quiz: A quiz node.

type $question_number: A question number, starting at 1. Pages do not have question numbers. Quiz directions are considered part of the numbering.

Related topics

1 string reference to 'quiz_take_question'
quiz_menu in ./quiz.module
Implements hook_menu().

File

./quiz.module, line 1790
quiz.module Main file for the Quiz module.

Code

function quiz_take_question($quiz, $question_number) {

  // Preserve "Take" tab.
  $item = menu_get_item("node/{$quiz->nid}/take");
  menu_set_item(NULL, $item);
  if (!empty($_SESSION['quiz'][$quiz->nid]['result_id'])) {
    $quiz_result = quiz_result_load($_SESSION['quiz'][$quiz->nid]['result_id']);
    $question = $quiz_result->layout[$question_number];
    if (!empty($question['qnr_pid'])) {

      // Find the parent.
      foreach ($quiz_result->layout as $pquestion) {
        if ($pquestion['qnr_id'] == $question['qnr_pid']) {

          // Load the page that the requested question belongs to.
          $question_node = node_load($pquestion['nid'], $pquestion['vid']);
        }
      }
    }
    else {

      // Load the question.
      $question_node = node_load($question['nid'], $question['vid']);
    }
  }
  if (!$question_node) {

    // Question disappeared or invalid session. Start over.
    unset($_SESSION['quiz'][$quiz->nid]);
    drupal_goto("node/{$quiz->nid}");
  }

  // Mark this as the current question.
  quiz_question_goto($quiz, $question_number);

  // Added the progress info to the view.
  $quiz_result = quiz_result_load($_SESSION['quiz'][$quiz->nid]['result_id']);
  $questions = array();
  $i = 0;
  foreach ($quiz_result->layout as $idx => $question) {
    if (empty($question['qnr_pid'])) {

      // Question has no parent. Show it in the jumper.
      $questions[$idx] = ++$i;
    }
  }
  $content['progress']['#markup'] = theme('quiz_progress', array(
    'questions' => $questions,
    'current' => arg(3),
    'allow_jumping' => $quiz->allow_jumping,
    'pager' => count($questions) >= variable_get('quiz_pager_start', 100),
    'time_limit' => $quiz->time_limit,
  ));
  $content['progress']['#weight'] = -50;

  /**
  * @todo: wat do?
   if (count($_SESSION['quiz'][$quiz->nid]['quiz_questions']) + count($_SESSION['quiz'][$quiz->nid]['previous_quiz_questions']) > $number_of_questions) {
   drupal_set_message(t('At least one question have been deleted from the quiz after you started taking it. You will have to start over.'), 'warning', FALSE);
   unset($_SESSION['quiz'][$quiz->nid]);
   drupal_goto('node/' . $quiz->nid . '/take');
   }
  */
  if (function_exists('jquery_countdown_add') && variable_get('quiz_has_timer', 0) && $quiz->time_limit) {
    jquery_countdown_add('.countdown', array(
      'until' => $quiz_result->time_start + $quiz->time_limit - REQUEST_TIME,
      'onExpiry' => 'finished',
      'compact' => TRUE,
      'layout' => t('Time left') . ': {hnn}{sep}{mnn}{sep}{snn}',
    ));

    // These are the two button op values that are accepted for answering
    // questions.
    $button_op1 = drupal_json_encode(t('Finish'));
    $button_op2 = drupal_json_encode(t('Next'));
    $js = "\n            function finished() {\n              // Find all buttons with a name of 'op'.\n              var buttons = jQuery('input[type=submit][name=op], button[type=submit][name=op]');\n              // Filter out the ones that don't have the right op value.\n              buttons = buttons.filter(function() {\n                return this.value == {$button_op1} || this.value == {$button_op2};\n              });\n              if (buttons.length == 1) {\n                // Since only one button was found, this must be it.\n                buttons.click();\n              }\n              else {\n                // Zero, or more than one buttons were found; fall back on a page refresh.\n                window.location = window.location.href;\n              }\n            }\n          ";
    drupal_add_js($js, array(
      'type' => 'inline',
      'scope' => JS_DEFAULT,
    ));
  }
  $question_form = drupal_get_form('quiz_question_answering_form', $question_node, $_SESSION['quiz'][arg(1)]['result_id']);
  $content['body']['question']['#markup'] = drupal_render($question_form);
  drupal_set_title($quiz->title);
  return $content;
}