You are here

function quiz_set_questions in Quiz 7.5

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 quiz.module \quiz_set_questions()
  8. 7.4 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

3 calls to quiz_set_questions()
quiz_insert in ./quiz.module
Implements hook_insert().
quiz_question_revision_actions_form_submit in question_types/quiz_question/quiz_question.pages.inc
Submit callback for the revision actions page.
_quiz_update_items in ./quiz.admin.inc
Update a quiz set of items with new weights and membership.

File

./quiz.module, line 2556
quiz.module Main file for the 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);
  }

  // Clear the existing relationships for this version.
  db_delete('quiz_node_relationship')
    ->condition('parent_nid', $quiz->nid)
    ->condition('parent_vid', $quiz->vid)
    ->execute();
  if (empty($questions)) {

    // This is not an error condition.
    return TRUE;
  }
  foreach ($questions as $quiz_question_relationship) {
    $old_qnr_id = $quiz_question_relationship->qnr_id;
    unset($quiz_question_relationship->qnr_id);
    $quiz_question_relationship->parent_nid = $quiz->nid;
    $quiz_question_relationship->parent_vid = $quiz->vid;
    entity_save('quiz_question_relationship', $quiz_question_relationship);
    $quiz_question_relationship->old_qnr_id = $old_qnr_id;
    $quiz_question_relationships[] = $quiz_question_relationship;
  }

  // Update the parentage when a new revision is created.
  // @todo this is copy pasta from quiz_update_quiz_question_relationship
  foreach ($quiz_question_relationships as $quiz_question_relationship) {
    db_update('quiz_node_relationship')
      ->condition('qnr_pid', $quiz_question_relationship->old_qnr_id)
      ->condition('parent_vid', $quiz->vid)
      ->condition('parent_nid', $quiz->nid)
      ->fields(array(
      'qnr_pid' => $quiz_question_relationship->qnr_id,
    ))
      ->execute();
  }
  quiz_update_max_score_properties(array(
    $quiz->vid,
  ));
  return TRUE;
}