You are here

public function ScaleQuestion::saveAnswerCollection in Quiz 6.4

Same name and namespace in other branches
  1. 8.6 question_types/quiz_scale/src/Plugin/quiz/QuizQuestion/ScaleQuestion.php \ScaleQuestion::saveAnswerCollection()
  2. 8.5 question_types/quiz_scale/src/Plugin/quiz/QuizQuestion/ScaleQuestion.php \ScaleQuestion::saveAnswerCollection()
  3. 6.6 question_types/scale/scale.classes.inc \ScaleQuestion::saveAnswerCollection()
  4. 7.6 question_types/scale/scale.classes.inc \ScaleQuestion::saveAnswerCollection()
  5. 7 question_types/scale/scale.classes.inc \ScaleQuestion::saveAnswerCollection()
  6. 7.4 question_types/scale/scale.classes.inc \ScaleQuestion::saveAnswerCollection()
  7. 7.5 question_types/scale/scale.classes.inc \ScaleQuestion::saveAnswerCollection()

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/scale.classes.inc
Implementation of saveNodeProperties

File

question_types/scale/scale.classes.inc, line 93
The main classes for the scale question type.

Class

ScaleQuestion
Extension of QuizQuestion.

Code

public function saveAnswerCollection($is_new_node, array $alt_input = NULL, $preset = NULL) {
  global $user;
  if (!isset($alt_input)) {
    $alt_input = get_object_vars($this->node);
  }
  if (!isset($preset)) {
    $preset = $this->node->save;
  }
  $alternatives = array();
  for ($i = 0; $i < variable_get('scale_max_num_of_alts', 10); $i++) {
    if (drupal_strlen($alt_input['alternative' . $i]) > 0) {
      $alternatives[] = $alt_input['alternative' . $i];
    }
  }

  // If an identical answer collection already exists
  if ($answer_collection_id = $this
    ->excistingCollection($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
  db_query('INSERT INTO {quiz_scale_answer_collection} () VALUES ()');
  $answer_collection_id = db_last_insert_id('quiz_scale_answer_collection', 'id');

  // Save as preset if checkbox for preset has been checked
  if ($preset == 1) {
    $sql = 'INSERT INTO {quiz_scale_user}
              (uid, answer_collection_id)
              VALUES (%d, %d)';
    db_query($sql, $user->uid, $answer_collection_id);
  }

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