You are here

function quiz_build_question_list in Quiz 7.6

Same name and namespace in other branches
  1. 8.4 quiz.module \quiz_build_question_list()
  2. 5.2 quiz.module \quiz_build_question_list()
  3. 5 quiz.module \quiz_build_question_list()
  4. 6.6 quiz.module \quiz_build_question_list()
  5. 6.2 quiz.module \quiz_build_question_list()
  6. 6.3 quiz.module \quiz_build_question_list()
  7. 6.4 quiz.module \quiz_build_question_list()
  8. 6.5 quiz.module \quiz_build_question_list()
  9. 7 quiz.module \quiz_build_question_list()
  10. 7.4 quiz.module \quiz_build_question_list()
  11. 7.5 quiz.module \quiz_build_question_list()

Retrieves a list of questions (to be taken) for a given quiz.

If the quiz has random questions this function only returns a random selection of those questions. This function should be used to decide what questions a quiz taker should answer.

This question list is stored in the user's result, and may be different when called multiple times. It should only be used to generate the layout for a quiz attempt and NOT used to do operations on the questions inside of a quiz.

Parameters

$quiz: Quiz node.

Return value

Array of question node IDs.

Related topics

5 calls to quiz_build_question_list()
QuizRandomTestCase::testCategorizedRandomQuestions in tests/QuizRandomTestCase.test
Test pulling questions from categories.
QuizRandomTestCase::testRandomOrder in tests/QuizRandomTestCase.test
Test random order of questions.
QuizRandomTestCase::testRandomOrderInPages in tests/QuizRandomTestCase.test
Test that questions inside of pages are shuffled.
QuizRandomTestCase::testRandomQuestions in tests/QuizRandomTestCase.test
Test random plus required questions from a pool.
QuizResultController::save in includes/QuizResultController.class.inc
Save the Quiz result and do any post-processing to the result.

File

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

Code

function quiz_build_question_list($quiz) {
  $questions = array();
  if ($quiz->randomization == 3) {
    $questions = _quiz_build_categorized_question_list($quiz);
  }
  else {

    // Get required questions first.
    $query = db_query('SELECT qnr.child_nid as nid, qnr.child_vid as vid, n.type, qnr.qnr_id, qnr.qnr_pid
    FROM {quiz_node_relationship} qnr
    JOIN {node} n ON qnr.child_nid = n.nid
    LEFT JOIN {quiz_node_relationship} qnr2 ON (qnr.qnr_pid = qnr2.qnr_id OR (qnr.qnr_pid IS NULL AND qnr.qnr_id = qnr2.qnr_id))
    WHERE qnr.parent_vid = :parent_vid
    AND qnr.question_status = :question_status
    AND n.status = 1
    ORDER BY qnr2.weight, qnr.weight', array(
      ':parent_vid' => $quiz->vid,
      ':question_status' => QUIZ_QUESTION_ALWAYS,
    ));
    $i = 0;
    while ($question_node = $query
      ->fetchAssoc()) {

      // Just to make it easier on us, let's use a 1-based index.
      $i++;
      $questions[$i] = $question_node;
    }

    // Get random questions for the remainder.
    if ($quiz->number_of_random_questions > 0) {
      $random_questions = _quiz_get_random_questions($quiz);
      $questions = array_merge($questions, $random_questions);
      if ($quiz->number_of_random_questions > count($random_questions)) {

        // Unable to find enough requested random questions.
        return FALSE;
      }
    }

    // Shuffle questions if required.
    if ($quiz->randomization > 0) {
      $question_to_shuffle = array();
      $mark = NULL;
      foreach ($questions as $qidx => $question) {
        if ($mark) {
          if ($question['type'] == 'quiz_page') {

            // Found another page.
            shuffle($question_to_shuffle);
            array_splice($questions, $mark, $qidx - $mark - 1, $question_to_shuffle);
            $mark = 0;
            $question_to_shuffle = array();
          }
          else {
            $question_to_shuffle[] = $question;
          }
        }
        if ($question['type'] == 'quiz_page') {
          $mark = $qidx;
        }
      }
      if ($mark) {
        shuffle($question_to_shuffle);
        array_splice($questions, $mark, $qidx - $mark, $question_to_shuffle);
      }
      else {
        if (is_null($mark)) {
          shuffle($questions);
        }
      }
    }
  }
  $count = 0;
  $display_count = 0;
  $questions_out = array();
  foreach ($questions as &$question) {
    $question_node = node_load($question['nid'], $question['vid']);
    $count++;
    $display_count++;
    $question['number'] = $count;
    if ($question['type'] != 'quiz_page') {
      $question['display_number'] = $display_count;
    }
    $questions_out[$count] = $question;
  }
  return $questions_out;
}