You are here

public function MultichoiceQuestion::saveNodeProperties in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceQuestion::saveNodeProperties()

Implementation of saveNodeProperties().

See also

QuizQuestion::saveNodeProperties()

File

question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php, line 129

Class

MultichoiceQuestion
@QuizQuestion ( id = "multichoice", label = Plugin annotation @Translation("Multiple choice question"), handlers = { "response" = "\Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse" } )

Namespace

Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion

Code

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

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