You are here

public function MultichoiceQuestion::saveNodeProperties in Quiz 7

Same name and namespace in other branches
  1. 6.4 question_types/multichoice/multichoice.classes.inc \MultichoiceQuestion::saveNodeProperties()
  2. 7.6 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 124
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) {
    $id = db_insert('quiz_multichoice_properties')
      ->fields(array(
      'nid' => $this->node->nid,
      'vid' => $this->node->vid,
      'choice_multi' => $this->node->choice_multi,
      'choice_random' => $this->node->choice_random,
      'choice_boolean' => $this->node->choice_boolean,
    ))
      ->execute();

    // TODO: utilize the benefit of multiple insert of DBTNG
    for ($i = 0; isset($this->node->alternatives[$i]); $i++) {
      if (drupal_strlen($this->node->alternatives[$i]['answer']['value']) > 0) {
        $this
          ->insertAlternative($i);
      }
    }
  }
  else {
    db_update('quiz_multichoice_properties')
      ->fields(array(
      'choice_multi' => $this->node->choice_multi,
      'choice_random' => $this->node->choice_random,
      'choice_boolean' => $this->node->choice_boolean,
    ))
      ->condition('nid', $this->node->nid)
      ->condition('vid', $this->node->vid)
      ->execute();

    // 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.
    $res = db_query('SELECT id FROM {quiz_multichoice_answers}
              WHERE question_nid = :nid AND question_vid = :vid', array(
      ':nid' => $this->node->nid,
      ':vid' => $this->node->vid,
    ));

    // We start by assuming that all existing alternatives needs to be deleted
    $ids_to_delete = array();
    while ($res_o = $res
      ->fetch()) {
      $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']['value']) > 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_delete('quiz_multichoice_answers')
          ->condition('id', $id_to_delete)
          ->execute();
      }
    }
  }
  $this
    ->saveUserSettings();
}