You are here

function quiz_get_questions in Quiz 6.4

Same name and namespace in other branches
  1. 8.4 quiz.module \quiz_get_questions()
  2. 7.6 quiz.module \quiz_get_questions()
  3. 7 quiz.module \quiz_get_questions()
  4. 7.4 quiz.module \quiz_get_questions()
  5. 7.5 quiz.module \quiz_get_questions()

Retrieve list of published questions assigned to quiz.

This function should be used for question browsers and similiar... It should not be used to decide what questions a user should answer when taking a quiz. quiz_build_question_list is written for that purpose.

Parameters

$quiz_nid: Quiz node id.

$quiz_vid: Quiz node version id.

$include_all_types: Should the results be filtered on available question types? @todo: review this.

$nid_keys: Should nid be used as keys in the array we return?

$include_question: Should the question(the node body) be included for the questions in the returned array?

Return value

An array of questions.

1 call to quiz_get_questions()
quiz_questions_form in ./quiz.admin.inc
Handles "manage questions" tab.

File

./quiz.module, line 3348
Quiz Module

Code

function quiz_get_questions($quiz_nid = NULL, $quiz_vid = NULL, $include_all_types = TRUE, $nid_keys = FALSE, $include_question = TRUE, $include_random = TRUE) {
  $filter_types = '';
  $questions = array();
  $wheres = array();
  $where_sql = '';
  if ($include_all_types === TRUE) {
    $types = array_keys(_quiz_get_question_types());
    if (count($types) > 0) {
      $str_types = "'" . implode("','", $types) . "'";
      $wheres[] = 'n.type IN (' . $str_types . ')';
    }
  }
  if (!is_null($quiz_vid)) {
    $wheres[] = 'qnr.parent_vid = ' . intval($quiz_vid);
    $wheres[] = 'qnr.parent_nid = ' . intval($quiz_nid);
  }

  // Only include published questions.
  $wheres[] = 'n.status = 1';

  // Create where filter to be added.
  if (count($wheres)) {
    $where_sql = ' WHERE ';
    $where_sql .= implode(' AND ', $wheres);
  }
  if ($include_random) {
    $on_filter = 'AND qnr.question_status != ' . QUESTION_NEVER;
  }
  else {
    $on_filter = 'AND qnr.question_status = ' . QUESTION_ALWAYS;
  }
  $result = db_query('SELECT n.nid, nr.vid, n.vid AS latest_vid, n.type, nr.title, nr.body, nr.format, qnr.question_status, qnr.weight, qnr.max_score
    FROM {node} n
    INNER JOIN {node_revisions} nr ON n.nid = nr.nid
    LEFT JOIN {quiz_node_relationship} qnr
    ON nr.vid = qnr.child_vid
    AND qnr.parent_vid = %d
    ' . $on_filter . '
    ' . $where_sql . ' ORDER BY weight', $quiz_vid);

  // Create questions array.
  if ($nid_keys === FALSE) {
    while ($node = db_fetch_object($result)) {
      $questions[] = quiz_node_map($node, $include_question);
    }
  }
  else {
    while ($node = db_fetch_object($result)) {
      $n = quiz_node_map($node, $include_question);
      $questions[$n->nid] = $n;
    }
  }
  return $questions;
}