You are here

function quiz_set_questions in Quiz 6.6

Same name and namespace in other branches
  1. 8.4 quiz.module \quiz_set_questions()
  2. 6.3 quiz.module \quiz_set_questions()
  3. 6.4 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 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.

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

File

./quiz.module, line 1866
Quiz Module

Code

function quiz_set_questions(&$quiz, $questions, $set_new_revision = FALSE) {
  if ($set_new_revision) {

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

  // XXX: Should we be verifying here that the nodes submitted as $questions are actually
  // recognized question types? It adds quite a performance hit, but could be beneficial.
  // 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.)
  $sql = "INSERT INTO {quiz_node_relationship} (parent_nid, parent_vid, child_nid, child_vid, question_status, weight)\n      VALUES (%d, %d, %d, (SELECT vid FROM {node} WHERE nid = %d), %d, %d)";
  foreach ($questions as $question) {
    if ($question->state != QUESTION_NEVER) {

      //drupal_set_message(t("Doing insert for %nid-%vid-%cnid-%cvid: %stat", array('%nid' => $quiz->nid, '%vid' => $quiz->vid, '%cnid' => $question->nid, '%cvid' => $question->vid, '%stat' => $question->status)));
      $result = db_query($sql, $quiz->nid, $quiz->vid, $question->nid, $question->nid, $question->state, $question->weight);
    }
  }
  return TRUE;
}