You are here

function QuizResult::canReview in Quiz 8.5

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

Can the quiz taker view the requested review?

There's a workaround in here: @kludge

When review for the question is enabled, and it is the last question, technically it is the end of the quiz, and the "end of quiz" review settings apply. So we check to make sure that we are in question taking and the feedback is viewed within 5 seconds of completing the question/quiz.

Parameters

string $option: An option key.

QuizResult $quiz_result: A Quiz result.

Return value

bool TRUE if the quiz taker can view this quiz option at this time, FALSE otherwise.

1 call to QuizResult::canReview()
QuizResult::hasReview in src/Entity/QuizResult.php
Can the quiz taker view any reviews right now?

File

src/Entity/QuizResult.php, line 394

Class

QuizResult
Defines the Quiz entity class.

Namespace

Drupal\quiz\Entity

Code

function canReview($option) {
  $config = Drupal::config('quiz.settings');

  // Load quiz associated with this result.
  $quiz = \Drupal::entityTypeManager()
    ->getStorage('quiz')
    ->loadRevision($this
    ->get('vid')
    ->getString());
  $admin = $quiz
    ->access('update');
  if ($config
    ->get('override_admin_feedback') && $admin) {

    // Admin user uses the global feedback options.
    $review_options['end'] = $config
      ->get('admin_review_options_end');
    $review_options['question'] = $config
      ->get('admin_review_options_question');
  }
  else {

    // Use this Quiz's feedback options.
    if ($quiz
      ->get('review_options')
      ->get(0)) {
      $review_options = $quiz
        ->get('review_options')
        ->get(0)
        ->getValue();
    }
    else {
      $review_options = [];
    }
  }

  // Hold combined review options from each feedback type.
  $all_shows = array();
  $rules = \Drupal::moduleHandler()
    ->moduleExists('rules');
  $feedbackTypes = QuizFeedbackType::loadMultiple();
  foreach ($review_options as $time_key => $shows) {

    /* @var $component RulesComponent */
    $component = $feedbackTypes[$time_key]
      ->getComponent();
    $component
      ->setContextValue('quiz_result', $this);
    if ($component
      ->getExpression()
      ->executeWithState($component
      ->getState())) {

      // Add selected feedbacks to the show list.
      $all_shows += array_filter($shows);
    }
  }
  return !empty($all_shows[$option]);
}