You are here

function quiz_update_questions in Quiz 6.2

Same name and namespace in other branches
  1. 5.2 quiz.module \quiz_update_questions()
  2. 5 quiz.module \quiz_update_questions()
  3. 6.6 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.

Return value

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

1 call to quiz_update_questions()
quiz_questions_form_submit in ./quiz.admin.inc
Submit function for quiz_questions.

File

./quiz.module, line 1171
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->vid, TRUE, TRUE);
  $i_am_different_now = FALSE;
  $questions = array();

  // These are the questions that will be put into the Quiz.
  foreach ($submitted_questions as $nid => $stat) {
    $existing = $existing_questions[$nid];
    if ($stat != QUESTION_NEVER) {
      if ($existing) {
        if ($existing->status != $stat) {

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

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

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

      // Delete items moved from ALWAYS or RANDOM to NEVER
      $existing->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->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->status);
    }
  }
  return TRUE;
}