You are here

function quiz_set_questions in Quiz 7

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.4 quiz.module \quiz_set_questions()
  5. 6.5 quiz.module \quiz_set_questions()
  6. 7.6 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 2948
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_delete('quiz_node_relationship')
    ->condition('parent_nid', $quiz->nid)
    ->condition('parent_vid', $quiz->vid)
    ->execute();
  if (empty($questions)) {
    return TRUE;

    // This is not an error condition.
  }
  $qnr_insert = db_insert('quiz_node_relationship')
    ->fields(array(
    'parent_nid',
    'parent_vid',
    'child_nid',
    'child_vid',
    'question_status',
    'weight',
    'max_score',
  ));
  foreach ($questions as $question) {
    if ($question->state != QUESTION_NEVER) {
      $qnr_insert
        ->values(array(
        'parent_nid' => $quiz->nid,
        'parent_vid' => $quiz->vid,
        'child_nid' => $question->nid,
        'child_vid' => $question->refresh ? db_query('SELECT vid FROM {node} WHERE nid = :nid', array(
          ':nid' => $question->nid,
        ))
          ->fetchField() : $question->vid,
        'question_status' => $question->state,
        'weight' => $question->weight,
        'max_score' => (int) $question->max_score,
      ));
    }
  }
  if (count($questions)) {
    $qnr_insert
      ->execute();
  }
  quiz_update_max_score_properties(array(
    $quiz->vid,
  ));
  return TRUE;
}