You are here

function course_course_access in Course 7

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

Code

function course_course_access($op, $node, $user) {
  $enrollment = course_enrollment_load($node->nid, $user->uid);
  if ($op == 'enroll') {
    if (!node_access('view', $node)) {
      return array(
        'course_denied' => array(
          'success' => FALSE,
          'header' => t('Access denied'),
          'message' => t('You do not have permission to enroll into this course'),
        ),
      );
    }
    if (!empty($node->course['live_from_date']) && REQUEST_TIME > $node->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($node->course['live_from_date'], 'long'),
          )),
        ),
      );
    }
    if ($user->uid == 0) {
      $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 (!user_access('access course', $user)) {
      return array(
        'course_noperm' => array(
          'success' => FALSE,
          'header' => '',
          'message' => t('You are not allowed to take courses.'),
          'weight' => 300,
        ),
      );
    }
  }
  if ($op == 'take') {
    if (!node_access('view', $node)) {
      return array(
        'course_node_access' => array(
          'success' => FALSE,
          'header' => t('Access denied'),
          'message' => t('You do not have permission to take this course.'),
          'weight' => 300,
        ),
      );
    }
    if ($enrollment) {

      // Check if there are any required, unfilled fields on the enrollment.
      $instances = field_info_instances('course_enrollment', $node->course['enrollment_type']);
      foreach ($instances as $field_name => $instance) {
        if ($instance['required'] && !field_get_items('course_enrollment', $enrollment, $field_name)) {
          return array(
            'course_enrollment' => array(
              'success' => FALSE,
              'header' => t(''),
              'message' => theme('course_take_course_button', array(
                'node' => $node,
              )),
            ),
          );
        }
      }
      if ($enrollment->enroll_end > 0 && REQUEST_TIME > $enrollment->enroll_end) {
        return array(
          'course_enrollment_expired' => array(
            'success' => FALSE,
            'header' => t('Enrollment expired'),
            'message' => t('Sorry, your enrollment has expired for this course.'),
          ),
        );
      }
    }
    else {
      return array(
        'course_not_enrolled' => array(
          'success' => FALSE,
          'header' => t('Not enrolled.'),
          'message' => t('Sorry, you must first enroll in this course.'),
          'weight' => 200,
        ),
      );
    }
  }

  // Both enroll and take course blockers.
  if (!empty($node->course['open']) && REQUEST_TIME < $node->course['open']) {
    return array(
      'course_notopen' => array(
        'success' => FALSE,
        'header' => t('Course not open'),
        'message' => t('This course opens on %date.', array(
          '%date' => format_date($node->course['open'], 'long'),
        )),
      ),
    );
  }
  if (!empty($node->course['close']) && REQUEST_TIME > $node->course['close']) {
    return array(
      'course_closed' => array(
        'success' => FALSE,
        'header' => t('Course closed'),
        'message' => t('This course closed on %date.', array(
          '%date' => format_date($node->course['close'], 'long'),
        )),
      ),
    );
  }
}