You are here

function Quiz::isLastQuestion in Quiz 6.x

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

Show the finish button?

File

src/Entity/Quiz.php, line 724

Class

Quiz
Defines the Quiz entity class.

Namespace

Drupal\quiz\Entity

Code

function isLastQuestion() {

  /* @var $quiz_session \Drupal\quiz\Services\QuizSessionInterface */
  $quiz_session = \Drupal::service('quiz.session');
  $quiz_result = $quiz_session
    ->getResult($this);
  $current = $quiz_session
    ->getCurrentQuestion($this);
  $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[$current + 1]);
}