You are here

function course_uc_course_access in Course 8.3

Same name and namespace in other branches
  1. 8.2 modules/course_uc/course_uc.module \course_uc_course_access()
  2. 7.2 modules/course_uc/course_uc.module \course_uc_course_access()
  3. 7 modules/course_uc/course_uc.module \course_uc_course_access()
  4. 3.x 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 471

Code

function course_uc_course_access($op, $node, $user) {
  if ($op == 'enroll') {
    $course = course_get_course($node, $user);

    // Block enrollment if course is free and bypass checkout is not enabled.
    if (!empty($node->sell_price) && !variable_get('course_access_bypass_checkout', 1) && !course_uc_check_purchased($user->uid, $node
      ->id())) {
      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
        ->identifier() == $first
        ->identifier();
      $is_payment = $courseObject
        ->getComponent() == 'payment';
      if ($is_payment && $first_object) {
        $product = \Drupal\node\Entity\Node::load($courseObject
          ->getInstanceId());
        $complete = $courseObject
          ->getFulfillment($user)
          ->isComplete();
        if ((!variable_get('course_access_bypass_checkout', 1) || $product->sell_price > 0) && !$complete) {

          // Force purchase.
          if (!course_enrollment_check($node
            ->id(), $user->uid)) {
            return array(
              'course_must_purchase' => array(
                'success' => FALSE,
                'header' => t('Payment required'),
                'message' => t('You must purchase this course.'),
                'weight' => 1000,
              ),
            );
          }
        }
      }
    }
  }
}