You are here

private function ScaleQuestion::existingCollection in Quiz 7.6

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

Finds out if a collection already exists.

Parameters

$alternatives: This is the collection that will be compared with the database.

$answer_collection_id: If we are matching a set of alternatives with a given collection that exists in the database.

$last_id - The id of the last alternative we compared with.:

Return value

TRUE if the collection exists FALSE otherwise

1 call to ScaleQuestion::existingCollection()
ScaleQuestion::saveAnswerCollection in question_types/scale/scale.classes.inc
Stores the answer collection to the database, or identifies an existing collection.

File

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

Class

ScaleQuestion
Extension of QuizQuestion.

Code

private function existingCollection(array $alternatives, $answer_collection_id = NULL, $last_id = NULL) {
  $my_alts = isset($answer_collection_id) ? $alternatives : array_reverse($alternatives);

  // Find all answers identical to the next answer in $alternatives
  $sql = 'SELECT id, answer_collection_id FROM {quiz_scale_answer} WHERE answer = :answer';
  $args[':answer'] = array_pop($my_alts);

  // Filter on collection id
  if (isset($answer_collection_id)) {
    $sql .= ' AND answer_collection_id = :acid';
    $args[':acid'] = $answer_collection_id;
  }

  // Filter on alternative id(If we are investigating a specific collection, the alternatives needs to be in a correct order)
  if (isset($last_id)) {
    $sql .= ' AND id = :id';
    $args[':id'] = $last_id + 1;
  }
  $res = db_query($sql, $args);
  if (!($res_o = $res
    ->fetch())) {
    return FALSE;
  }

  /*
   * If all alternatives has matched make sure the collection we are comparing against in the database
   * doesn't have more alternatives.
   */
  if (count($my_alts) == 0) {
    $res_o2 = db_query('SELECT * FROM {quiz_scale_answer}
              WHERE answer_collection_id = :answer_collection_id
              AND id = :id', array(
      ':answer_collection_id' => $answer_collection_id,
      ':id' => $last_id + 2,
    ))
      ->fetch();
    return $res_o2 ? FALSE : $answer_collection_id;
  }

  // Do a recursive call to this function on all answer collection candidates
  do {
    $col_id = $this
      ->existingCollection($my_alts, $res_o->answer_collection_id, $res_o->id);
    if ($col_id) {
      return $col_id;
    }
  } while ($res_o = $res
    ->fetch());
  return FALSE;
}