You are here

public function MultichoiceQuestion::saveNodeProperties in Quiz 6.4

Same name and namespace in other branches
  1. 7.6 question_types/multichoice/multichoice.classes.inc \MultichoiceQuestion::saveNodeProperties()
  2. 7 question_types/multichoice/multichoice.classes.inc \MultichoiceQuestion::saveNodeProperties()
  3. 7.4 question_types/multichoice/multichoice.classes.inc \MultichoiceQuestion::saveNodeProperties()
  4. 7.5 question_types/multichoice/multichoice.classes.inc \MultichoiceQuestion::saveNodeProperties()

Implementation of save

Stores the question in the database.

Parameters

is_new if - if the node is a new node...: (non-PHPdoc)

Overrides QuizQuestion::saveNodeProperties

See also

sites/all/modules/quiz-HEAD/question_types/quiz_question/QuizQuestion#save()

File

question_types/multichoice/multichoice.classes.inc, line 122
The main classes for the multichoice question type.

Class

MultichoiceQuestion
Extension of QuizQuestion.

Code

public function saveNodeProperties($is_new = FALSE) {
  $is_new = $is_new || $this->node->revision == 1;

  // Before we save we forgive some possible user errors
  $this
    ->forgive();

  // We also add warnings on other possible user errors
  $this
    ->warn();
  if ($is_new) {
    $sql = 'INSERT INTO {quiz_multichoice_properties}
              (nid, vid, choice_multi, choice_random, choice_boolean)
              VALUES (%d, %d, %d, %d, %d)';
    db_query($sql, $this->node->nid, $this->node->vid, $this->node->choice_multi, $this->node->choice_random, $this->node->choice_boolean);
    for ($i = 0; isset($this->node->alternatives[$i]); $i++) {
      if (drupal_strlen($this->node->alternatives[$i]['answer']) > 0) {
        $this
          ->insertAlternative($i);
      }
    }
  }
  else {
    $sql = 'UPDATE {quiz_multichoice_properties}
              SET choice_multi = %d, choice_random = %d, choice_boolean = %d
              WHERE nid = %d AND vid = %d';
    db_query($sql, $this->node->choice_multi, $this->node->choice_random, $this->node->choice_boolean, $this->node->nid, $this->node->vid);

    // We fetch ids for the existing answers belonging to this question
    // We need to figure out if an existing alternative has been changed or deleted.
    $sql = 'SELECT id FROM {quiz_multichoice_answers}
              WHERE question_nid = %d AND question_vid = %d';
    $res = db_query($sql, $this->node->nid, $this->node->vid);

    // We start by assuming that all existing alternatives needs to be deleted
    $ids_to_delete = array();
    while ($res_o = db_fetch_object($res)) {
      $ids_to_delete[] = $res_o->id;
    }
    for ($i = 0; isset($this->node->alternatives[$i]); $i++) {
      $short = $this->node->alternatives[$i];
      if (drupal_strlen($this->node->alternatives[$i]['answer']) > 0) {

        // If new alternative
        if (!is_numeric($short['id'])) {
          $this
            ->insertAlternative($i);
        }
        else {
          $this
            ->updateAlternative($i);

          // Make sure this alternative isn't deleted
          $key = array_search($short['id'], $ids_to_delete);
          $ids_to_delete[$key] = FALSE;
        }
      }
    }
    foreach ($ids_to_delete as $id_to_delete) {
      if ($id_to_delete) {
        db_query('DELETE FROM {quiz_multichoice_answers} WHERE id = %d', $id_to_delete);
      }
    }
  }
  $this
    ->saveUserSettings();
}