You are here

function _quiz_build_categorized_question_list in Quiz 7

Same name and namespace in other branches
  1. 6.4 quiz.module \_quiz_build_categorized_question_list()
  2. 7.6 quiz.module \_quiz_build_categorized_question_list()
  3. 7.4 quiz.module \_quiz_build_categorized_question_list()
  4. 7.5 quiz.module \_quiz_build_categorized_question_list()

Builds the questionlist for quizzes with categorized random questions

See also

quiz_build_question_list()

Related topics

1 call to _quiz_build_categorized_question_list()
quiz_build_question_list in ./quiz.module
Retrieves a list of questions (to be taken) for a given quiz.

File

./quiz.module, line 2844
Quiz Module

Code

function _quiz_build_categorized_question_list($quiz) {
  $terms = _quiz_get_terms($quiz->vid);
  $questions = array();
  $nids = array();
  $question_types = array_keys(_quiz_get_question_types());
  if (empty($question_types)) {
    return array();
  }
  $total_count = 0;
  foreach ($terms as $term) {
    $sql = "SELECT n.nid, n.vid, tn.tid\n            FROM {node} n\n            JOIN {taxonomy_term_node} tn USING(vid)\n            WHERE n.status = 1\n            AND n.type IN('" . implode("','", $question_types) . "')\n            AND tn.tid = %d";

    // $question_types isn't user input. It is considered safe.
    if (!empty($nids)) {
      $sql .= " AND n.nid NOT IN(" . implode(', ', $nids) . ")";
    }
    $sql .= " ORDER BY RAND()";
    $res = db_query_range($sql, $term->tid);
    $count = 0;
    while ($question = db_fetch_array($res)) {
      $count++;
      $question['tid'] = $term->tid;
      $question['number'] = $count + $total_count;
      $questions[] = $question;
      $nids[] = $question['nid'];
    }
    $total_count += $count;
    if ($count < $term->number) {

      // Not enough questions
      return FALSE;
    }
  }
  return $questions;
}