You are here

function course_course_access in Course 8.3

Same name and namespace in other branches
  1. 8.2 course.module \course_course_access()
  2. 7.2 course.module \course_course_access()
  3. 7 course.module \course_course_access()
  4. 3.x 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 708
course.module Core functionality for Courses.

Code

function course_course_access(Course $entity, $operation, AccountInterface $account) {
  $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']) && REQUEST_TIME > $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' => format_date($entity->course['live_from_date'], 'long'),
          )),
        ),
      );
    }
    if ($account
      ->isAnonymous()) {
      $options = array(
        'query' => drupal_get_destination(),
      );
      return array(
        'course_noanon' => array(
          'success' => FALSE,
          'header' => '',
          'message' => t('Please !login or !register to take this course.', array(
            '!login' => l(t('login'), 'user/login', $options),
            '!register' => l(t('register'), 'user/register', $options),
          )),
          'weight' => 300,
        ),
      );
    }
    if (!$entity
      ->access('view', $account)) {
      return [
        'course_noperm' => AccessResult::forbidden('You are not allowed to take courses.'),
      ];
    }
  }
  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) {

      // Check if there are any required, unfilled fields on the enrollment.
      $instances = $enrollment
        ->getFieldDefinitions();
      foreach ($instances as $field_name => $instance) {
        if ($instance instanceof FieldConfig) {
          if ($instance
            ->get('required') && $enrollment
            ->get($field_name)
            ->isEmpty()) {
            $button = [
              '#theme' => 'course_take_course_button',
              '#node' => $entity,
            ];
            $entity->content['course']['#markup'] = Drupal::service('renderer')
              ->render($button);
            return [
              'course_enrollment' => AccessResult::forbidden('You must fill out required enrollment fields. Click here to whatever'),
            ];
          }
        }
      }
      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.'),
        ];
      }
    }
    else {
      return [
        'course_not_enrolled' => AccessResult::forbidden('Sorry, you must first enroll in this course.'),
      ];
    }
  }
}