You are here

function QuizResult::finalize in Quiz 6.x

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

Score a quiz result.

File

src/Entity/QuizResult.php, line 448

Class

QuizResult
Defines the Quiz entity class.

Namespace

Drupal\quiz\Entity

Code

function finalize() {
  $questions = $this
    ->getLayout();

  // Mark all missing answers as blank. This is essential here for when we may
  // have pages of unanswered questions. Also kills a lot of the skip code that
  // was necessary before.
  foreach ($questions as $qinfo) {

    // If the result answer has not been marked as skipped and it hasn't been
    // answered.
    if (empty($qinfo->is_skipped) && empty($qinfo->answer_timestamp)) {
      $qinfo->is_skipped = 1;
      $qinfo
        ->save();
    }
  }
  $score = $this
    ->score();
  if (!isset($score['percentage_score'])) {
    $score['percentage_score'] = 0;
  }

  // @todo Could be removed if we implement any "released" functionality.
  $this
    ->set('released', 1);
  $this
    ->set('is_evaluated', $score['is_evaluated']);
  $this
    ->set('score', $score['percentage_score']);
  $this
    ->set('time_end', \Drupal::time()
    ->getRequestTime());
  $this
    ->save();
  return $this;
}