You are here

function Quiz::isLastQuestion in Quiz 8.5

Same name and namespace in other branches
  1. 8.6 src/Entity/Quiz.php \Drupal\quiz\Entity\Quiz::isLastQuestion()
  2. 6.x src/Entity/Quiz.php \Drupal\quiz\Entity\Quiz::isLastQuestion()

Show the finish button?

File

src/Entity/Quiz.php, line 717

Class

Quiz
Defines the Quiz entity class.

Namespace

Drupal\quiz\Entity

Code

function isLastQuestion() {
  $quiz_result = QuizResult::load($_SESSION['quiz'][$this
    ->id()]['result_id']);
  $current = $_SESSION['quiz'][$this
    ->id()]['current'];
  $layout = $quiz_result
    ->getLayout();
  foreach ($layout as $idx => $qra) {
    if ($qra
      ->get('question_id')
      ->referencedEntities()[0]
      ->bundle() == 'page') {
      if ($current == $idx) {

        // Found a page that we are on.
        $in_page = TRUE;
        $last_page = TRUE;
      }
      else {

        // Found a quiz page that we are not on.
        $last_page = FALSE;
      }
    }
    elseif (empty($qra->qqr_pid)) {

      // A question without a parent showed up.
      $in_page = FALSE;
      $last_page = FALSE;
    }
  }
  return $last_page || !isset($layout[$_SESSION['quiz'][$this
    ->id()]['current'] + 1]);
}