You are here

function _quiz_maintain_results in Quiz 7

Same name and namespace in other branches
  1. 8.4 quiz.module \_quiz_maintain_results()
  2. 6.4 quiz.module \_quiz_maintain_results()
  3. 7.6 quiz.module \_quiz_maintain_results()
  4. 7.4 quiz.module \_quiz_maintain_results()
  5. 7.5 quiz.module \_quiz_maintain_results()

Deletes results for a quiz according to the keep results setting

Parameters

$quiz: The quiz node to be maintained

$rid: The result id of the latest result for the current user

Return value

TRUE if results where deleted.

3 calls to _quiz_maintain_results()
quiz_report_form_submit in ./quiz.pages.inc
Submit the report form
quiz_take_quiz in ./quiz.module
Handles quiz taking.
quiz_update_total_score in ./quiz.module
Update a score for a quiz.

File

./quiz.module, line 3912
Quiz Module

Code

function _quiz_maintain_results($quiz, $rid) {
  global $user;

  // Do not delete results for anonymous users
  if ($user->uid == 0) {
    return;
  }
  switch ($quiz->keep_results) {
    case QUIZ_KEEP_ALL:
      return FALSE;
    case QUIZ_KEEP_BEST:
      $best_result_id = db_query('SELECT result_id FROM {quiz_node_results}
          WHERE nid = :nid AND uid = :uid AND is_evaluated = :is_evaluated
          ORDER BY score DESC', array(
        ':nid' => $quiz->nid,
        ':uid' => $user->uid,
        ':is_evaluated' => 1,
      ))
        ->fetchField();
      if (!$best_result_id) {
        return;
      }
      $res = db_query('SELECT result_id FROM {quiz_node_results}
          WHERE nid = :nid AND uid = :uid AND result_id != :best_rid AND is_evaluated = :is_evaluated', array(
        ':nid' => $quiz->nid,
        ':uid' => $user->uid,
        ':is_evaluated' => 1,
        ':best_rid' => $best_result_id,
      ));
      $rids = array();
      while ($rid2 = $res
        ->fetchField()) {
        $rids[] = $rid2;
      }
      quiz_delete_results($rids);
      return !empty($rids);
    case QUIZ_KEEP_LATEST:
      $res = db_query('SELECT result_id FROM {quiz_node_results}
              WHERE nid = :nid AND uid = :uid AND is_evaluated = :is_evaluated AND result_id != :result_id', array(
        ':nid' => $quiz->nid,
        ':uid' => $user->uid,
        ':is_evaluated' => 1,
        ':result_id' => $rid,
      ));
      $rids = array();
      while ($rid2 = $res
        ->fetchField()) {
        $rids[] = $rid2;
      }
      quiz_delete_results($rids);
      return !empty($rids);
  }
}