You are here

function quiz_take_question in Quiz 7.5

Same name and namespace in other branches
  1. 7.6 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 call to quiz_take_question()
quiz_quiztake_pane_content_type_render in plugins/content_types/quiztake_pane.inc
Run-time rendering of the body of the block (content type).
2 string references to 'quiz_take_question'
quiz_menu in ./quiz.module
Implements hook_menu().
quiz_quiztake_menu_alter in plugins/page_manager/tasks/quiztake.inc
Callback defined by quiz_quiztake_page_manager_tasks().

File

./quiz.module, line 1894
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']);
    $layout = $quiz_result
      ->getLayout();
    $question = $layout[$question_number];
    if (!empty($question['qnr_pid'])) {

      // Find the parent.
      foreach ($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;
  $found_pages = 0;
  foreach ($quiz_result
    ->getLayout() as $idx => $question) {
    if (empty($question['qnr_pid'])) {

      // Question has no parent. Show it in the jumper.
      $questions[$idx] = ++$i;
      $found_pages++;
    }
    if ($question_node->nid == $question['nid']) {

      // Found our question.
      $current_page = $found_pages;
    }
  }
  $content = array();
  $content['progress']['#markup'] = theme('quiz_progress', array(
    'questions' => $questions,
    'current' => $current_page,
    'allow_jumping' => $quiz->allow_jumping,
    'pager' => count($questions) >= variable_get('quiz_pager_start', 100),
    'time_limit' => $quiz->time_limit,
  ));
  $content['progress']['#weight'] = -50;
  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' => 'quiz_finished',
      'compact' => FALSE,
      '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 quiz_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, 'inline');

    // Add div to be used by jQuery countdown.
    $content['body']['countdown']['#markup'] = '<div class="countdown"></div>';
  }
  $question_form = drupal_get_form('quiz_question_answering_form', $question_node, $_SESSION['quiz'][$quiz->nid]['result_id']);
  $content['body']['question']['#markup'] = drupal_render($question_form);
  return $content;
}