function quiz_quiz_access in Quiz 8.6
Same name and namespace in other branches
- 8.5 quiz.module \quiz_quiz_access()
- 7.5 quiz.module \quiz_quiz_access()
- 6.x quiz.module \quiz_quiz_access()
Implements hook_quiz_access().
Can a user take this quiz?
Related topics
File
- ./
quiz.module, line 441 - Contains quiz.module
Code
function quiz_quiz_access(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation == 'take') {
$user_is_admin = $entity
->access('update');
// Make sure this is available.
if (!$entity
->get('quiz_date')
->isEmpty()) {
// Compare current GMT time to the open and close dates (which should still
// be in GMT time).
$quiz_open = Drupal::time()
->getRequestTime() >= strtotime($entity
->get('quiz_date')
->get(0)
->getValue()['value']);
$quiz_closed = Drupal::time()
->getRequestTime() >= strtotime($entity
->get('quiz_date')
->get(0)
->getValue()['end_value']);
if (!$quiz_open || $quiz_closed) {
if ($user_is_admin) {
$hooks['admin_ignore_date'] = array(
'success' => TRUE,
'message' => (string) t('You are marked as an administrator or owner for this @quiz. While you can take this @quiz, the open/close times prohibit other users from taking this @quiz.', array(
'@quiz' => _quiz_get_quiz_name(),
)),
);
}
else {
if ($quiz_closed) {
return AccessResultForbidden::forbidden((string) t('This @quiz is closed.', array(
'@quiz' => _quiz_get_quiz_name(),
)));
}
if (!$quiz_open) {
return AccessResultForbidden::forbidden((string) t('This @quiz is not yet open.', array(
'@quiz' => _quiz_get_quiz_name(),
)));
}
}
}
}
// Check to see if this user is allowed to take the quiz again:
if ($entity
->get('takes')
->getString() > 0) {
$taken = db_query("SELECT COUNT(*) AS takes FROM {quiz_result} WHERE uid = :uid AND qid = :qid", array(
':uid' => $account
->id(),
':qid' => $entity
->id(),
))
->fetchField();
$t = Drupal::translation();
$allowed_times = $t
->formatPlural($entity
->get('takes')
->getString(), '1 time', '@count times');
$taken_times = $t
->formatPlural($taken, '1 time', '@count times');
// The user has already taken this quiz.
if ($taken) {
if (false && $user_is_admin) {
$hooks['owner_limit'] = array(
'success' => TRUE,
'message' => (string) t('You have taken this @quiz already. You are marked as an owner or administrator for this quiz, so you can take this quiz as many times as you would like.', array(
'@quiz' => _quiz_get_quiz_name(),
)),
);
}
elseif ($taken >= $entity
->get('takes')
->getString()) {
if ($entity->allow_resume && $entity
->getResumeableResult($account)) {
// Quiz is resumable and there is an active attempt, so we should
// allow them to finish it as it won't be creating a new attempt. This
// is the blocker, so we do nothing here. The resume handles in the
// take function.
}
elseif (!isset($_SESSION['quiz'][$entity
->id()])) {
// If result is in session, don't check the attempt limit. @todo would
// be to split up "take" into something like "start" and "continue" an
// attempt.
$hooks['attempt_limit'] = array(
'success' => FALSE,
'message' => (string) t('You have already taken this @quiz @really. You may not take it again.', array(
'@quiz' => _quiz_get_quiz_name(),
'@really' => $taken_times,
)),
);
}
}
elseif ($entity->show_attempt_stats) {
$hooks['attempt_limit'] = array(
'success' => TRUE,
'message' => (string) t("You can only take this @quiz @allowed. You have taken it @really.", array(
'@quiz' => _quiz_get_quiz_name(),
'@allowed' => $allowed_times,
'@really' => $taken_times,
)),
'weight' => -10,
);
}
}
}
// Check to see if the user is registered, and user alredy passed this quiz.
if ($entity->show_passed && $account
->id() && quiz_is_passed($account
->id(), $entity
->id(), $entity
->getRevisionId())) {
$hooks['already_passed'] = array(
'success' => TRUE,
'message' => (string) t('You have already passed this @quiz.', array(
'@quiz' => _quiz_get_quiz_name(),
)),
'weight' => 10,
);
}
if (!empty($hooks)) {
foreach ($hooks as $hook) {
if (!$hook['success']) {
return AccessResultForbidden::forbidden($hook['message'], array(
'@quiz' => _quiz_get_quiz_name(),
));
}
}
}
if (!empty($hooks)) {
foreach ($hooks as $hook) {
if ($hook['success']) {
if (Drupal::routeMatch()
->getRouteName() == 'entity.quiz.canonical') {
// Only display if we are viewing the quiz.
Drupal::messenger()
->addWarning($hook['message']);
}
return [
AccessResultAllowed::allowed($hook['message'], array(
'@quiz' => _quiz_get_quiz_name(),
)),
];
}
}
}
// Check permission and node access.
if (!Drupal::currentUser()
->hasPermission('access quiz') || !$entity
->access('view')) {
return [
AccessResultForbidden::forbidden((string) t('You are not allowed to take this @quiz.', array(
'@quiz' => _quiz_get_quiz_name(),
))),
];
}
}
}