You are here

function course_uc_course_access in Course 3.x

Same name and namespace in other branches
  1. 8.3 modules/course_uc/course_uc.module \course_uc_course_access()
  2. 8.2 modules/course_uc/course_uc.module \course_uc_course_access()
  3. 7.2 modules/course_uc/course_uc.module \course_uc_course_access()
  4. 7 modules/course_uc/course_uc.module \course_uc_course_access()

Implements hook_course_access().

Block the user from enrolling in a paid course.

File

modules/course_uc/course_uc.module, line 441

Code

function course_uc_course_access(Course $course, $op, AccountProxy $account) {
  if ($op == 'enroll') {
    if ($node = course_get_attached($course)) {

      // Block enrollment if course is free and bypass checkout is not enabled.
      if (!$node
        ->get('price')
        ->isEmpty() && !Drupal::config('course_uc.settings')
        ->get('bypass_checkout', 1) && !course_uc_check_purchased($account
        ->id(), $node
        ->id())) {
        return [
          'course_must_purchase' => AccessResult::forbidden('You must checkout to enroll in this course.'),
        ];
        return array(
          'course_must_purchase' => array(
            'success' => FALSE,
            'header' => t('Checkout required'),
            'message' => t('You must checkout to enroll in this course.'),
            'weight' => 1000,
          ),
        );
      }

      // Now we have to check that even if the course has a price, that the
      // payment object is first (to block enrollments). If the course is $0 and
      // there are no payment objects, we assume the enrollment is blocked.
      $courseObjects = $course
        ->getObjects();
      $first = reset($courseObjects);
      foreach ($courseObjects as $courseObject) {
        $first_object = $courseObject
          ->id() == $first
          ->id();
        $is_payment = $courseObject
          ->getComponent() == 'ubercart';
        if ($is_payment && $first_object) {
          $product = course_get_attached($courseObject
            ->getCourse());
          $complete = $courseObject
            ->getFulfillment($account)
            ->isComplete();
          if ((!Drupal::config('course_uc.settings')
            ->get('bypass_checkout', 1) || $product
            ->get('price')->value > 0) && !$complete) {

            // Force purchase.
            if (!$course
              ->isEnrolled($account)) {
              return [
                'course_must_purchase' => AccessResult::forbidden('You must purchase this course.'),
              ];
            }
          }
        }
      }
    }
  }
}