You are here

function _quiz_get_random_questions in Quiz 6.3

Same name and namespace in other branches
  1. 8.4 quiz.module \_quiz_get_random_questions()
  2. 5.2 quiz.module \_quiz_get_random_questions()
  3. 6.6 quiz.module \_quiz_get_random_questions()
  4. 6.2 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()
1 call to _quiz_get_random_questions()
quiz_build_question_list in ./quiz.module
Retrieves a question list for a given quiz.

File

./quiz.module, line 2244
Quiz Module

Code

function _quiz_get_random_questions($quiz) {
  if (!is_object($quiz)) {
    drupal_set_message('The question pool cannot be generated.', 'error');
    watchdog('quiz', '_quiz_get_random_questions was called incorrectly.', array(), WATCHDOG_ERROR);
    return FALSE;
  }
  $num_random = $quiz->number_of_random_questions;
  $tid = $quiz->tid;
  $questions = array();
  if ($num_random > 0) {
    if ($tid > 0) {
      $questions = _quiz_get_random_taxonomy_question_ids($tid, $num_random);

      /*
      // 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;
}