You are here

function quiz_update_questions in Quiz 5.2

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

$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.module
Submit function for quiz_questions.

File

./quiz.module, line 1803

Code

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

  // Load quiz node.
  $quiz = node_load(arg(1));
  $return = true;

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

  // Get array of all questions with a valid (enabled) question type (in database).
  $questions_in_db = _quiz_get_unused_questions($quiz->vid, TRUE) + _quiz_get_questions($quiz->vid, TRUE, TRUE);

  // Create an array of question statuses (in database), keyed by question vid.
  $status_in_db = array();
  foreach ($questions_in_db as $question) {
    $status_in_db[$question->nid] = $question->status;
  }

  // Determine which questions have had their status changed;
  // e.g. unassigned questions assigned to either 'always' or 'random', etc.
  $changes = array_diff_assoc($submitted_questions, $status_in_db);

  // Create new revision of current quiz (if any question status has been changed).
  if (!empty($changes)) {
    drupal_execute('node_form', array(
      'revision' => '1',
    ), $quiz);
    drupal_set_message(t('A new revision of the @quiz has been created.', array(
      '@quiz' => QUIZ_NAME,
    )));
  }

  // Separate all questions that have had their status changed
  //  into corresponding arrays (inserts or updates).
  $inserts = array();
  $updates = array();
  foreach ($changes as $nid => $status) {
    if ($status_in_db[$nid] == QUESTION_NEVER) {
      $inserts[$nid] = $status;
    }
    else {
      $updates[$nid] = $status;
    }
  }

  // Insert question(s) into quiz; status will be either 'always' or 'random'.
  foreach ($inserts as $nid => $status) {
    $child_vid = $questions_in_db[$nid]->vid;
    $sql = "INSERT INTO {quiz_node_relationship} (parent_nid, parent_vid, child_nid, child_vid, question_status) " . "SELECT src.nid, src.vid, %d, %d, %d FROM {node} AS src WHERE src.nid = %d";
    $result = db_query($sql, $nid, $child_vid, $status, $quiz->nid);
    if (!$result) {
      return FALSE;
    }
  }

  // Update question status in quiz; i.e. change status from 'always' or 'random' to another status.
  foreach ($updates as $nid => $status) {
    $child_vid = $questions_in_db[$nid]->vid;
    $sql = "UPDATE {quiz_node_relationship} SET question_status = %d " . "WHERE parent_vid = (SELECT src.vid FROM {node} AS src WHERE src.nid = %d) AND parent_nid = %d AND child_vid = %d";
    $result = db_query($sql, $status, $quiz->nid, $quiz->nid, $child_vid);
    if (!$result) {
      return FALSE;
    }
  }
  return TRUE;
}