You are here

public function ScaleQuestion::saveAnswerCollection in Quiz 7.5

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. 6.4 question_types/scale/scale.classes.inc \ScaleQuestion::saveAnswerCollection()
  5. 7.6 question_types/scale/scale.classes.inc \ScaleQuestion::saveAnswerCollection()
  6. 7 question_types/scale/scale.classes.inc \ScaleQuestion::saveAnswerCollection()
  7. 7.4 question_types/scale/scale.classes.inc \ScaleQuestion::saveAnswerCollection()

Stores|Identifies the answer collection.

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 being taken.

Parameters

bool $is_new_node: TRUE if the question is being inserted, or FALSE for update.

array $alt_input: The alternatives array to be saved.

int $preset: Use 1 for preset, 0 for not a preset.

Return value

int 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 106
Scale classes.

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) && isset($this->node->save)) {
    $preset = $this->node->save;
  }
  $alternatives = array();
  for ($i = 0; $i < variable_get('scale_max_num_of_alts', 10); $i++) {
    if (isset($alt_input['alternative' . $i]) && drupal_strlen($alt_input['alternative' . $i]) > 0) {

      // Handle the form.
      $alternatives[] = $alt_input['alternative' . $i];
    }
    elseif (isset($alt_input['scale'][$i]->answer) && drupal_strlen($alt_input['scale'][$i]->answer) > 0) {

      // Handle programmatic save.
      $alternatives[] = $alt_input['scale'][$i]->answer;
    }
  }

  // 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->scale[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->uid,
      '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;
}