public function CertificateTabController::accessTab in Course 8.2
Same name and namespace in other branches
- 8.3 modules/course_certificate/src/Controller/CertificateTabController.php \Drupal\course_certificate\Controller\CertificateTabController::accessTab()
Parameters
EntityInterface $course: The entity this belongs to
AccountInterface $account: The user account to check
Return value
\Drupal\Core\Access\AccessResultInterface An access result
2 calls to CertificateTabController::accessTab()
- CertificateTabController::accessDefaultTab in modules/
course_certificate/ src/ Controller/ CertificateTabController.php - Helper to plugin current user when not provided in path
- CertificateTabController::accessPdf in modules/
course_certificate/ src/ Controller/ CertificateTabController.php - Downloads
1 string reference to 'CertificateTabController::accessTab'
- course_certificate.routing.yml in modules/
course_certificate/ course_certificate.routing.yml - modules/course_certificate/course_certificate.routing.yml
File
- modules/
course_certificate/ src/ Controller/ CertificateTabController.php, line 51
Class
- CertificateTabController
- An example controller.
Namespace
Drupal\course_certificate\ControllerCode
public function accessTab(Course $course, AccountInterface $account = NULL) {
$has_object = FALSE;
$account = $account ?? Drupal::currentUser();
$admin = $account
->hasPermission('administer certificates');
$view_all = $account
->hasPermission('view all user certificates');
$access_result = AccessResult::forbidden("Access not granted");
if (!$account
->id()) {
return AccessResult::forbidden("Not a valid user");
}
// Does the course have a certificate object?
foreach ($course
->getObjects() as $courseObject) {
if ($courseObject
->getComponent() == 'certificate') {
$has_object = TRUE;
break;
}
}
if (!$has_object) {
return AccessResult::forbidden("No certificate object provided");
}
// Are they enrolled?
$enrollments = $this
->entityTypeManager()
->getStorage('course_enrollment')
->loadByProperties([
'uid' => $account
->id(),
'cid' => $course
->id(),
]);
if (empty($enrollments)) {
$access_result = AccessResult::forbidden();
}
else {
$enrollment = reset($enrollments);
// Are they an admin or have they completed the course?
if ($admin || $view_all || $enrollment
->isComplete()) {
$access_result = AccessResult::allowed();
}
}
return $access_result;
}