You are here

function QuizResult::maintainResults in Quiz 6.x

Same name and namespace in other branches
  1. 8.6 src/Entity/QuizResult.php \Drupal\quiz\Entity\QuizResult::maintainResults()
  2. 8.5 src/Entity/QuizResult.php \Drupal\quiz\Entity\QuizResult::maintainResults()

Mark results as invalid for a quiz according to the keep results setting.

This function will only mark the results as invalid. The actual delete action happens based on a cron run. If we would have deleted the results in this function the user might not have been able to view the result screen of the quiz he just finished.

Parameters

QuizResult $quiz_result: The result of the latest result for the current user.

Return value

bool TRUE if results were marked as invalid, FALSE otherwise.

1 call to QuizResult::maintainResults()
QuizResult::save in src/Entity/QuizResult.php
Save the Quiz result and do any post-processing to the result.

File

src/Entity/QuizResult.php, line 307

Class

QuizResult
Defines the Quiz entity class.

Namespace

Drupal\quiz\Entity

Code

function maintainResults() {
  $db = \Drupal::database();
  $quiz = $this
    ->getQuiz();
  $user = $this
    ->getAccount();

  // Do not delete results for anonymous users.
  if ($user
    ->id() == 0) {
    return FALSE;
  }
  $result_ids = [];
  switch ((int) $quiz
    ->get('keep_results')
    ->getString()) {
    case Quiz::KEEP_ALL:
      break;
    case Quiz::KEEP_BEST:
      $best_result_id = $db
        ->select('quiz_result', 'qnr')
        ->fields('qnr', [
        'result_id',
      ])
        ->condition('qnr.qid', $quiz
        ->id())
        ->condition('qnr.uid', $user
        ->id())
        ->condition('qnr.is_evaluated', 1)
        ->condition('qnr.is_invalid', 0)
        ->orderBy('score', 'DESC')
        ->execute()
        ->fetchField();
      if ($best_result_id) {
        $result_ids = $db
          ->select('quiz_result', 'qnr')
          ->fields('qnr', [
          'result_id',
        ])
          ->condition('qnr.qid', $quiz
          ->id())
          ->condition('qnr.uid', $user
          ->id())
          ->condition('qnr.is_evaluated', 1)
          ->condition('qnr.is_invalid', 0)
          ->condition('qnr.result_id', $best_result_id, '!=')
          ->execute()
          ->fetchCol('result_id');
      }
      break;
    case Quiz::KEEP_LATEST:
      $result_ids = $db
        ->select('quiz_result', 'qnr')
        ->fields('qnr', [
        'result_id',
      ])
        ->condition('qnr.qid', $quiz
        ->id())
        ->condition('qnr.uid', $user
        ->id())
        ->condition('qnr.is_evaluated', 1)
        ->condition('qnr.is_invalid', 0)
        ->condition('qnr.result_id', $this
        ->id(), '!=')
        ->execute()
        ->fetchCol('result_id');
      break;
  }
  if ($result_ids) {
    $db
      ->update('quiz_result')
      ->fields([
      'is_invalid' => 1,
    ])
      ->condition('result_id', $result_ids, 'IN')
      ->execute();
    return TRUE;
  }
  return FALSE;
}