function course_course_access in Course 3.x
Same name and namespace in other branches
- 8.3 course.module \course_course_access()
- 8.2 course.module \course_course_access()
- 7.2 course.module \course_course_access()
- 7 course.module \course_course_access()
Implements hook_course_access().
Block enrollments when a course has either not yet started or is expired.
File
- ./
course.module, line 617 - course.module Core functionality for Courses.
Code
function course_course_access(Course $entity, $operation, AccountInterface $account) {
if (!in_array($operation, [
'take',
'enroll',
])) {
return;
}
$request_time = Drupal::time()
->getRequestTime();
$enrollment = $entity
->getEnrollment($account);
if (!$entity
->get('course_date')
->isEmpty() && in_array($operation, [
'take',
'enroll',
])) {
$date = $entity
->get('course_date')
->getValue();
$course_open = new DrupalDateTime($date[0]['value'], 'UTC');
$course_close = new DrupalDateTime($date[0]['end_value'], 'UTC');
$now = DrupalDateTime::createFromTimestamp($request_time);
$course_not_open = $course_open
->diff($now)->invert;
$course_closed = !$course_close
->diff($now)->invert;
$formatter = Drupal::service('date.formatter');
// Both enroll and take course blockers.
if ($date[0]['value'] && $course_not_open) {
$message = t('This course opens on %date.', [
'%date' => $formatter
->format($course_open
->getTimestamp()),
]);
return [
'course_notopen' => AccessResult::forbidden((string) $message),
];
}
if ($date[0]['end_value'] && $course_closed) {
$message = t('This course closed on %date.', [
'%date' => $formatter
->format($course_close
->getTimestamp()),
]);
return [
'course_closed' => AccessResult::forbidden((string) $message),
];
}
}
if ($operation == 'enroll') {
if (!$entity
->access('view', $account)) {
return array(
'course_denied' => AccessResult::forbidden('You do not have permission to enroll into this course'),
);
}
if (!empty($entity->course['live_from_date']) && Drupal::time()
->getRequestTime() > $entity->course['live_from_date']) {
return array(
'course_live_started' => array(
'success' => FALSE,
'message' => t('This live activity started on %date and is no longer available for enrollments.', array(
'%date' => Drupal::service('date.formatter')
->format($entity->course['live_from_date'], 'long'),
)),
),
);
}
if ($account
->isAnonymous()) {
$options = \Drupal::destination()
->getAsArray();
$message = t('Please @login or @register to take this course.', array(
'@login' => Link::createFromRoute(t('login'), 'user.login', $options)
->toString(),
'@register' => Link::createFromRoute(t('register'), 'user.register', $options)
->toString(),
));
return [
'course_noanon' => AccessResult::forbidden((string) $message),
];
}
return [
'course_enroll' => AccessResult::allowedIf($account
->hasPermission('enroll course')),
];
}
if ($operation == 'take') {
if (!$entity
->access('view', $account)) {
return [
'course_node_access' => AccessResult::forbidden('You do not have permission to take this course.'),
];
}
if ($enrollment) {
if ($enrollment
->get('enroll_end')
->getString() > 0 && $request_time > $enrollment
->get('enroll_end')
->getString()) {
return [
'course_enrollment_expired' => AccessResult::forbidden('Sorry, your enrollment has expired for this course.'),
];
}
return [
'course_take' => AccessResult::allowedIf($account
->hasPermission('take course')),
];
}
else {
return [
'course_not_enrolled' => AccessResult::forbidden('Sorry, you must first enroll in this course.'),
];
}
}
}