View source
<?php
namespace Drupal\quiz\View;
use Drupal;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\quiz\Entity\Quiz;
use function _quiz_format_duration;
use function _quiz_get_quiz_name;
class QuizViewBuilder extends EntityViewBuilder {
public function alterBuild(array &$build, Drupal\Core\Entity\EntityInterface $entity, Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
$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);
if ($available
->isAllowed()) {
if (is_a($available, \Drupal\Core\Access\AccessResultReasonInterface::class)) {
if (\Drupal::routeMatch() == 'entity.quiz.canonical') {
Drupal::messenger()
->addMessage($available
->getReason());
}
}
$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,
);
}
}
}