You are here

public function ChoiceQuestion::save in Quiz 6.6

Implementation of save

Stores the question in the database.

Parameters

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

Overrides QuizQuestion::save

See also

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

File

question_types/choice/choice.classes.inc, line 83
The main classes for the choice question type.

Class

ChoiceQuestion
Implementation of QuizQuestion.

Code

public function save($is_new = FALSE) {
  $is_new_node = $is_new || $this->node->revision == 1;
  $this
    ->forgive();
  if ($is_new_node) {
    $sql = 'INSERT INTO {quiz_choice_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; is_array($this->node->alternatives[$i]); $i++) {
      if (strlen(strip_tags($this->node->alternatives[$i]['answer'])) > 0) {
        $this
          ->insertAlternative($i);
      }
    }
  }
  else {
    $sql = 'UPDATE {quiz_choice_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);
    $sql = 'SELECT id FROM {quiz_choice_answer}
              WHERE question_nid = %d AND question_vid = %d';
    $res = db_query($sql, $this->node->nid, $this->node->vid);
    $ids_to_delete = array();
    while ($res_o = db_fetch_object($res)) {
      $ids_to_delete[] = $res_o->id;
    }
    for ($i = 0; is_array($this->node->alternatives[$i]); $i++) {
      $short = $this->node->alternatives[$i];
      if (strlen(strip_tags($short['answer'])) > 0) {
        if (!is_numeric($short['id'])) {
          $this
            ->insertAlternative($i);
        }
        else {
          $this
            ->updateAlternative($i);
          $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_choice_answer} WHERE id = %d', $id_to_delete);
      }
    }
  }
  $this
    ->saveUserSettings();
}