You are here

function multichoice_update in Quiz 6.5

Same name and namespace in other branches
  1. 5.2 multichoice.module \multichoice_update()
  2. 5 multichoice.module \multichoice_update()
  3. 6.6 question_types/multichoice/multichoice.module \multichoice_update()
  4. 6.2 multichoice.module \multichoice_update()
  5. 6.3 question_types/multichoice/multichoice.module \multichoice_update()

Implementation of hook_update().

File

question_types/multichoice/multichoice.module, line 569
Multiple choice question type for the Quiz module.

Code

function multichoice_update($node) {
  $question_vid = $node->vid;
  $node->number_of_answers = multichoice_get_number_of_corrects($node->answers);
  if ($node->revision) {
    db_query("INSERT INTO {quiz_node_question_properties} (nid, vid, number_of_answers)\n      VALUES(%d, %d, %d)", $node->nid, $question_vid, $node->number_of_answers);
  }
  else {
    db_query("UPDATE {quiz_node_question_properties}\n      SET number_of_answers = %d WHERE nid = %d AND vid = %d", $node->number_of_answers, $node->nid, $node->vid);
  }

  // Use foreach on small lists...

  //while (list($key, $value) = each($node->answers)) {

  //print_r($node->answers);exit;
  foreach ($node->answers as $key => $value) {

    // Avoid E_NOTICE messages by assigning default
    // values.
    if (!isset($value['correct'])) {
      $value['correct'] = 0;
    }
    if (!isset($value['result_option'])) {
      $value['result_option'] = 0;
    }
    if ($value['answer_id']) {
      $value['answer'] = trim($value['answer']);
      if ($value['delete'] == 1 || !isset($value['answer']) || $value['answer'] == '') {

        // Delete this entry.
        db_query("DELETE FROM {quiz_multichoice_answers} WHERE answer_id = %d", $value['answer_id']);
      }
      else {

        // Update this entry.
        db_query("UPDATE {quiz_multichoice_answers} SET answer = '%s', feedback = '%s', is_correct = %d, result_option = %d WHERE answer_id = %d", $value['answer'], $value['feedback'], $value['correct'], $value['result_option'], $value['answer_id']);
      }
    }
    elseif (trim($value['answer']) != '') {

      // If there is an answer, insert a new row. // Removed answer_id
      db_query("INSERT INTO {quiz_multichoice_answers} (nid, vid, answer, feedback, is_correct, result_option) " . "VALUES(%d, %d, '%s', '%s', %d, %d)", $node->nid, $question_vid, $value['answer'], $value['feedback'], $value['correct'], $value['result_option']);
    }
  }

  // Quiz node vid (revision) was updated.
  if ($node->revision) {

    // Gather all quiz node vids from quiz_node_relationship table that contain
    // the question being updated and create a new revision of the quizzes.
    $sql = "SELECT DISTINCT parent_vid FROM {quiz_node_relationship} WHERE child_vid = %d AND question_status != %d";
    $result = db_query($sql, $node->old_vid, QUESTION_NEVER);
    while ($quiz = db_fetch_object($result)) {

      // Create new revision of the quiz.
      // This will also create new quiz-question relation entries in the quiz_node_relationship table.
      $sql = "SELECT * FROM {node} WHERE vid = %d";
      $quiz_old = db_fetch_object(db_query($sql, $quiz->parent_vid));
      $rev = array(
        'revision' => '1',
      );
      drupal_execute('node_form', $rev, $quiz_old);

      // Update question vid in quiz_node_relationship table for each row that
      // contains the question vid prior to the increment (new revision).
      $sql = "SELECT vid FROM {node} WHERE nid = %d";
      $quiz_new = db_fetch_object(db_query($sql, $quiz_old->nid));
      $sql = "UPDATE {quiz_node_relationship} SET child_vid = %d WHERE parent_vid = %d AND child_vid = %d";
      db_query($sql, $node->vid, $quiz_new->vid, $node->old_vid);
    }
  }
}