You are here

function _quiz_get_random_questions in Quiz 5.2

Same name and namespace in other branches
  1. 8.4 quiz.module \_quiz_get_random_questions()
  2. 6.6 quiz.module \_quiz_get_random_questions()
  3. 6.2 quiz.module \_quiz_get_random_questions()
  4. 6.3 quiz.module \_quiz_get_random_questions()
  5. 6.4 quiz.module \_quiz_get_random_questions()
  6. 6.5 quiz.module \_quiz_get_random_questions()
  7. 7.6 quiz.module \_quiz_get_random_questions()
  8. 7 quiz.module \_quiz_get_random_questions()
  9. 7.4 quiz.module \_quiz_get_random_questions()
  10. 7.5 quiz.module \_quiz_get_random_questions()
2 calls to _quiz_get_random_questions()
quiz_build_question_list in ./quiz.module
Retrieves a question list for a given quiz.
quiz_questions_form_submit in ./quiz.module
Submit function for quiz_questions.

File

./quiz.module, line 1269

Code

function _quiz_get_random_questions($num_random, $tid) {
  $questions = array();
  if ($num_random > 0) {
    if ($tid > 0) {

      // Select random questions by taxonomy.
      $term = taxonomy_get_term($tid);
      $tree = taxonomy_get_tree($term->vid, $term->tid);

      // Flatten the taxonomy tree, and just keep term id's.
      $term_ids[] = $term->tid;
      if (is_array($tree)) {
        foreach ($tree as $term) {
          $term_ids[] = $term->tid;
        }
      }
      $term_ids = implode(',', $term_ids);

      // Get all published questions with one of the allowed term ids.
      $result = db_query_range("SELECT n.nid, n.vid " . "FROM {node} n " . "INNER JOIN {term_node} tn USING (nid)" . "WHERE n.status = 1 AND tn.tid IN ({$term_ids}) " . "AND n.type IN ('" . implode("','", _quiz_get_question_types()) . "') " . "ORDER BY RAND()", 0, $num_random);
    }
    else {

      // Select random question from assigned pool.
      $result = db_query_range("SELECT child_nid as nid, child_vid as vid FROM {quiz_node_relationship} WHERE parent_vid = %d AND parent_nid = %d AND question_status = %d ORDER BY RAND()", $quiz->vid, $quiz->nid, QUESTION_RANDOM, 0, $quiz->number_of_random_questions);
    }
    while ($question_node = db_fetch_array($result)) {
      $questions[] = $question_node;
    }
  }
  return $questions;
}