You are here

public function MultichoiceQuestion::saveNodeProperties in Quiz 8.4

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/lib/Drupal/multichoice/MultichoiceQuestion.php, line 130
The main classes for the multichoice question type.

Class

MultichoiceQuestion
Extension of QuizQuestion.

Namespace

Drupal\multichoice

Code

public function saveNodeProperties($is_new = FALSE) {
  $is_new = $is_new || $this->node
    ->isNewRevision() == 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
        ->id(),
      'vid' => $this->node
        ->getRevisionId(),
      '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
      ->id())
      ->condition('vid', $this->node
      ->getRevisionId())
      ->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
        ->id(),
      ':vid' => $this->node
        ->getRevisionId(),
    ));

    // 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();
}