You are here

public function ScaleQuestion::deleteCollectionIfNotUsed in Quiz 8.4

Deletes an answer collection if it isn't beeing used.

Parameters

$answer_collection_id:

$accept: If collection is used more than this many times we keep it.

Return value

true if deleted, false if not deleted.

2 calls to ScaleQuestion::deleteCollectionIfNotUsed()
ScaleQuestion::delete in question_types/scale/lib/Drupal/scale/ScaleQuestion.php
Implementation of delete
ScaleQuestion::saveAnswerCollection in question_types/scale/lib/Drupal/scale/ScaleQuestion.php
Stores the answer collection to the database, or identifies an existing collection.

File

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

Class

ScaleQuestion
Extension of QuizQuestion.

Namespace

Drupal\scale

Code

public function deleteCollectionIfNotUsed($answer_collection_id, $accept = 0) {

  // Check if the collection is someones preset. If it is we can't delete it.
  $count = db_query('SELECT COUNT(*) FROM {quiz_scale_user} WHERE answer_collection_id = :acid', array(
    ':acid' => $answer_collection_id,
  ))
    ->fetchField();
  if ($count > 0) {
    return FALSE;
  }

  // Check if the collection is a global preset. If it is we can't delete it.
  $for_all = db_query('SELECT for_all FROM {quiz_scale_answer_collection} WHERE id = :id', array(
    ':id' => $answer_collection_id,
  ))
    ->fetchField();
  if ($for_all == 1) {
    return FALSE;
  }

  // Check if the collection is used in an existing question. If it is we can't delete it.
  $count = db_query('SELECT COUNT(*) FROM {quiz_scale_node_properties} WHERE answer_collection_id = :acid', array(
    ':acid' => $answer_collection_id,
  ))
    ->fetchField();

  // We delete the answer collection if it isnt beeing used by enough questions
  if ($count <= $accept) {
    db_delete('quiz_scale_answer_collection')
      ->condition('id', $answer_collection_id)
      ->execute();
    db_delete('quiz_scale_answer')
      ->condition('answer_collection_id', $answer_collection_id)
      ->execute();
    return TRUE;
  }
  return FALSE;
}