You are here

public function ScaleQuestion::saveAnswerCollection in Quiz 8.4

Stores the answer collection to the database, or identifies an existing collection.

We try to reuse answer collections as much as possible to minimize the amount of rows in the database, and thereby improving performance when surveys are beeing taken.

Parameters

$is_new_node - the question is beeing inserted(not updated):

$alt_input - the alternatives array to be saved.:

$preset - 1 | 0 = preset | not preset:

Return value

Answer collection id

1 call to ScaleQuestion::saveAnswerCollection()
ScaleQuestion::saveNodeProperties in question_types/scale/lib/Drupal/scale/ScaleQuestion.php
Implementation of saveNodeProperties

File

question_types/scale/lib/Drupal/scale/ScaleQuestion.php, line 98
The main classes for the short answer question type.

Class

ScaleQuestion
Extension of QuizQuestion.

Namespace

Drupal\scale

Code

public function saveAnswerCollection($is_new_node, array $alt_input = NULL, $preset = NULL) {
  $user = \Drupal::currentUser();
  $config = \Drupal::config('scale.settings');
  if (!isset($preset)) {
    $preset = $this->node->save;
  }
  $alternatives = array();
  if ($alt_input) {
    for ($i = 0; $i < $config
      ->get('scale_max_num_of_alts'); $i++) {
      if (isset($alt_input['alternative' . $i]) && drupal_strlen($alt_input['alternative' . $i]) > 0) {
        $alternatives[] = $alt_input['alternative' . $i];
      }
    }
  }
  else {
    for ($i = 0; $i < $config
      ->get('scale_max_num_of_alts'); $i++) {
      $alternative = 'alternative' . $i;
      if (isset($this->node->{$alternative}) && drupal_strlen($this->node->{$alternative}) > 0) {
        $alternatives[] = $this->node->{$alternative};
      }
    }
  }

  // If an identical answer collection already exists
  if ($answer_collection_id = $this
    ->existingCollection($alternatives)) {
    if ($preset == 1) {
      $this
        ->setPreset($answer_collection_id);
    }
    if (!$is_new_node || $this->util) {
      $col_to_delete = $this->util ? $this->col_id : $this->node->{0}->answer_collection_id;
      if ($col_to_delete != $answer_collection_id) {

        // We try to delete the old answer collection
        $this
          ->deleteCollectionIfNotUsed($col_to_delete, 1);
      }
    }
    return $answer_collection_id;
  }

  // Register a new answer collection
  $answer_collection_id = db_insert('quiz_scale_answer_collection')
    ->fields(array(
    'for_all' => 1,
  ))
    ->execute();

  // Save as preset if checkbox for preset has been checked
  if ($preset == 1) {
    $id = db_insert('quiz_scale_user')
      ->fields(array(
      'uid' => $user
        ->id(),
      'answer_collection_id' => $answer_collection_id,
    ))
      ->execute();
  }

  // Save the alternatives in the answer collection

  //db_lock_table('quiz_scale_answer');
  for ($i = 0; $i < count($alternatives); $i++) {
    $this
      ->saveAlternative($alternatives[$i], $answer_collection_id);
  }

  //db_unlock_tables();
  return $answer_collection_id;
}