You are here

public function QuizViewBuilder::alterBuild in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 src/View/QuizViewBuilder.php \Drupal\quiz\View\QuizViewBuilder::alterBuild()

Specific per-entity building.

Parameters

array $build: The render array that is being created.

\Drupal\Core\Entity\EntityInterface $entity: The entity to be prepared.

\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display: The entity view display holding the display options configured for the entity components.

string $view_mode: The view mode that should be used to prepare the entity.

Overrides EntityViewBuilder::alterBuild

File

src/View/QuizViewBuilder.php, line 13

Class

QuizViewBuilder

Namespace

Drupal\quiz\View

Code

public function alterBuild(array &$build, Drupal\Core\Entity\EntityInterface $entity, Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {

  /* @var $entity Quiz */
  $stats = array(
    array(
      array(
        'header' => TRUE,
        'width' => '25%',
        'data' => t('Questions'),
      ),
      $entity
        ->getNumberOfQuestions(),
    ),
  );
  if ($entity
    ->get('show_attempt_stats')
    ->getString()) {
    $takes = $entity
      ->get('takes')
      ->getString() == 0 ? t('Unlimited') : $entity
      ->get('takes')
      ->getString();
    $stats[] = array(
      array(
        'header' => TRUE,
        'data' => t('Attempts allowed'),
      ),
      $takes,
    );
  }
  if ($entity
    ->get('quiz_date')
    ->isEmpty()) {
    $stats[] = array(
      array(
        'header' => TRUE,
        'data' => t('Available'),
      ),
      t('Always'),
    );
  }
  else {
    $stats[] = array(
      array(
        'header' => TRUE,
        'data' => t('Opens'),
      ),
      $entity
        ->get('quiz_date')->value,
    );
    $stats[] = array(
      array(
        'header' => TRUE,
        'data' => t('Closes'),
      ),
      $entity
        ->get('quiz_date')->end_value,
    );
  }
  if (!$entity
    ->get('pass_rate')
    ->isEmpty()) {
    $stats[] = array(
      array(
        'header' => TRUE,
        'data' => t('Pass rate'),
      ),
      $entity
        ->get('pass_rate')
        ->getString() . ' %',
    );
  }
  if (!$entity
    ->get('time_limit')
    ->isEmpty()) {
    $stats[] = array(
      array(
        'header' => TRUE,
        'data' => t('Time limit'),
      ),
      _quiz_format_duration($entity
        ->get('time_limit')
        ->getString()),
    );
  }
  $stats[] = array(
    array(
      'header' => TRUE,
      'data' => t('Backwards navigation'),
    ),
    $entity
      ->get('backwards_navigation') ? t('Allowed') : t('Forbidden'),
  );
  if ($display
    ->getComponent('stats')) {
    $build['stats'] = array(
      '#id' => 'quiz-view-table',
      '#theme' => 'table',
      '#rows' => $stats,
      '#weight' => -1,
    );
  }
  $available = $entity
    ->access('take', NULL, TRUE);

  // Check the permission before displaying start button.
  if ($available
    ->isAllowed()) {
    if (is_a($available, \Drupal\Core\Access\AccessResultReasonInterface::class)) {

      // There's a friendly success message available. Only display if we are
      // viewing the quiz.
      // @todo does not work because we cannot pass allowed reason, only
      // forbidden reason. The message is displayed in quiz_quiz_access().
      if (\Drupal::routeMatch() == 'entity.quiz.canonical') {
        Drupal::messenger()
          ->addMessage($available
          ->getReason());
      }
    }

    // Add a link to the take tab.
    $link = Drupal\Core\Link::createFromRoute(t('Start @quiz', array(
      '@quiz' => _quiz_get_quiz_name(),
    )), 'entity.quiz.take', [
      'quiz' => $entity
        ->id(),
    ], [
      'attributes' => [
        'class' => array(
          'quiz-start-link',
        ),
      ],
    ]);
    $build['take'] = array(
      '#cache' => [
        'max-age' => 0,
      ],
      '#markup' => $link
        ->toString(),
      '#weight' => 2,
    );
  }
  else {
    $build['take'] = array(
      '#cache' => [
        'max-age' => 0,
      ],
      '#markup' => '<div class="quiz-not-available">' . $available
        ->getReason() . '</div>',
      '#weight' => 2,
    );
  }
}