You are here

function quiz_set_questions in Quiz 6.4

Same name and namespace in other branches
  1. 8.4 quiz.module \quiz_set_questions()
  2. 6.6 quiz.module \quiz_set_questions()
  3. 6.3 quiz.module \quiz_set_questions()
  4. 6.5 quiz.module \quiz_set_questions()
  5. 7.6 quiz.module \quiz_set_questions()
  6. 7 quiz.module \quiz_set_questions()
  7. 7.4 quiz.module \quiz_set_questions()
  8. 7.5 quiz.module \quiz_set_questions()

Sets the questions that are assigned to a quiz.

Parameters

$quiz: The quiz(node) to modify.

$questions: An array of questions.

$set_new_revision: If TRUE, a new revision will be generated. Note that saving quiz questions unmodified will still generate a new revision of the quiz if this is set to TRUE. Why? For a few reasons:

  • All of the questions are updated to their latest VID. That is supposed to be a feature.
  • All weights are updated.
  • All status flags are updated.

Return value

Boolean TRUE if update was successful, FALSE otherwise.

Related topics

1 call to quiz_set_questions()
_quiz_update_items in ./quiz.admin.inc
Update a quiz set of items with new weights and membership

File

./quiz.module, line 2836
Quiz Module

Code

function quiz_set_questions(&$quiz, $questions, $set_new_revision = FALSE) {
  $old_vid = $quiz->vid;
  if ($set_new_revision) {

    // Create a new Quiz VID, even if nothing changed.
    $quiz->revision = 1;
  }
  node_save($quiz);

  // When node_save() calls all of the node API hooks, old quiz info is
  // automatically inserted into quiz_node_relationship. We could get clever and
  // try to do strategic updates/inserts/deletes, but that method has already
  // proven error prone as the module has gained complexity (See 5.x-2.0-RC2).
  // So we go with the brute force method:
  db_query('DELETE FROM {quiz_node_relationship} WHERE parent_nid = %d AND parent_vid = %d', $quiz->nid, $quiz->vid);
  if (empty($questions)) {
    return TRUE;

    // This is not an error condition.
  }

  // Now we do an insert of everything in the quiz. (Note that we are using a
  // subselect to get the most recent vid.)
  $refresh_sql = "INSERT INTO {quiz_node_relationship}\n                  (parent_nid, parent_vid, child_nid, child_vid, question_status, weight, max_score)\n                  VALUES (%d, %d, %d, (SELECT vid FROM {node} WHERE nid = %d), %d, %d, %d)";
  $norefresh_sql = "INSERT INTO {quiz_node_relationship}\n                    (parent_nid, parent_vid, child_nid, child_vid, question_status, weight, max_score)\n                    VALUES (%d, %d, %d, %d, %d, %d, %d)";
  foreach ($questions as $question) {
    if ($question->state != QUESTION_NEVER) {
      if ($question->refresh) {
        $result = db_query($refresh_sql, $quiz->nid, $quiz->vid, $question->nid, $question->nid, $question->state, $question->weight, $question->max_score);
      }
      else {
        $result = db_query($norefresh_sql, $quiz->nid, $quiz->vid, $question->nid, $question->vid, $question->state, $question->weight, $question->max_score);
      }
    }
  }
  quiz_update_max_score_properties(array(
    $quiz->vid,
  ));
  return TRUE;
}