You are here

function quiz_update_questions in Quiz 6.6

Same name and namespace in other branches
  1. 5.2 quiz.module \quiz_update_questions()
  2. 5 quiz.module \quiz_update_questions()
  3. 6.2 quiz.module \quiz_update_questions()
  4. 6.3 quiz.module \quiz_update_questions()
  5. 6.5 quiz.module \quiz_update_questions()

Updates the status of questions assigned to the quiz. Possible statuses are 'random', 'always', 'never'.

@access public

Parameters

&$quiz: The quiz node. This is modified internally.

$submitted_questions: Array of submitted question statuses indexed (keyed) by the question nid. If this is empty, all of the quiz questions will be deleted from this quiz.

Return value

boolean True if update was a success, false if there was a problem.

Deprecated

See quiz_set_questions() for the new version.

File

./quiz.module, line 1915
Quiz Module

Code

function quiz_update_questions(&$quiz, $submitted_questions, $revision = FALSE) {

  // No questions to update, so return now.
  if (empty($submitted_questions)) {
    return FALSE;

    // This will cause an error message.
  }

  // Loop through all questions and determine whether an update needs to be made.
  // As we go, we store the questions that will need to be updated.
  $existing_questions = _quiz_get_questions($quiz->nid, $quiz->vid, TRUE, TRUE);
  $existing_question_nids = array_keys($existing_questions);
  $i_am_different = FALSE;
  $questions = array();

  // These are the questions that will be put into the Quiz.
  foreach ($submitted_questions as $nid => $stat) {
    $existing = in_array($nid, $existing_question_nids);

    // $existing_questions[$nid];
    if ($stat != QUESTION_NEVER) {
      if ($existing) {
        $existing_question = $existing_questions[$nid];

        // This appears to be comparing publishing status with question_status?
        if ($existing_question->question_status != $stat) {

          // Question's status has been changed.
          $existing_question->question_status = $stat;
          $i_am_different = TRUE;
        }

        // else This question is the same. Do nothing.
        $questions[] = $existing_question;
      }
      else {
        $new_question = node_load($nid);
        $new_question->question_status = $stat;
        $questions[] = $new_question;

        // A new question was added to the quiz.
        $i_am_different = TRUE;
      }
    }
    elseif (!empty($existing) && $stat != $existing->question_status) {

      // Delete items moved from ALWAYS or RANDOM to NEVER
      $existing->question_status = $stat;
      $questions[] = $existing;
      $i_am_different = TRUE;
    }

    // else Question isn't on the quiz, and isn't marked for inclusion.

    //else {

    // Question isn't part of the quiz.

    //}
  }
  if (!$i_am_different) {

    // Nothing else to do.
    return TRUE;
  }

  // If we get here, then we (may) need to create a new VID and then store the questions.
  if ($revision) {

    // Create a new Quiz VID
    $quiz->revision = 1;

    // Need new vid.
    node_save($quiz);

    // This updates the $quiz referent.
  }

  // 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);

  // Now we do an insert of everything in the quiz.
  $sql = "INSERT INTO {quiz_node_relationship} (parent_nid, parent_vid, child_nid, child_vid, question_status)\n      VALUES (%d, %d, %d, %d, %d)";
  foreach ($questions as $question) {
    if ($question->question_status != 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->vid, $question->question_status);
    }
  }
  return TRUE;
}