You are here

function course_take_course in Course 7

Same name and namespace in other branches
  1. 6 course.module \course_take_course()
  2. 7.2 course.module \course_take_course()

Take a course.

  • Enroll the user, if allowed.
  • Block the user if not allowed.
  • Fire the outline handler.
2 calls to course_take_course()
CourseEnrollmentTestCase::testCourseDuration in tests/CourseEnrollmentTestCase.test
Test for course duration being set properly on enrollment.
CourseEnrollmentTestCase::testCourseEnrollment in tests/CourseEnrollmentTestCase.test
Test for enrollment access and timestamping.
1 string reference to 'course_take_course'
course_menu in ./course.module
Implements hook_menu().

File

./course.module, line 1256
course.module Core functionality for Courses.

Code

function course_take_course($node) {
  global $user;
  drupal_set_title($node->title);
  $enroll_access = course_enroll_access($node);
  $enrollment = course_enrollment_load($node, $user);
  if ($enroll_access['success'] === TRUE && empty($enrollment->timestamp)) {

    // User can enroll in this course and user is not enrolled. Check for
    // enrollment fields or enroll the user.
    $instances = field_info_instances('course_enrollment', $node->course['enrollment_type']);
    foreach ($instances as $field_name => $instance) {
      $field = field_info_field($field_name);
      if ($instance['settings']['course_enrollment_user_field']) {

        // At least one field must be shown when enrolling. Display the user
        // enrollment form.
        if (empty($enrollment->type)) {
          $enrollment = entity_create('course_enrollment', array(
            'nid' => $node->nid,
            'uid' => $user->uid,
            'type' => $node->course['enrollment_type'],
          ));
        }
        $form = entity_ui_get_form('course_enrollment', $enrollment, 'edit');
        return $form;
      }
    }

    // No fields to show. Check for enrollment. If it does not exist, create it.
    if (empty($enrollment)) {
      $enrollment = entity_create('course_enrollment', array(
        'nid' => $node->nid,
        'uid' => $user->uid,
        'type' => $node->course['enrollment_type'],
      ));
      $enrollment
        ->save();
    }
  }
  $take_access = course_take_course_access($node);
  if ($take_access['success']) {

    // User has access to take this course.
    if (empty($enrollment->timestamp)) {

      // If user hasn't started course, mark start of enrollment.
      $enrollment->timestamp = REQUEST_TIME;
      course_enrollment_save($enrollment);
      drupal_set_message(t('Your enrollment in this course has been recorded.'));
    }

    // Display the configured outline handler output.
    $key = isset($node->course['outline']) ? $node->course['outline'] : 'course';
    $handlers = course_get_handlers('outline');
    foreach ($handlers as $module => $outline_handlers) {
      if ($outline_handlers) {
        foreach ($outline_handlers as $key2 => $outline_handler) {
          if ($key == $key2 && !empty($outline_handler['callback'])) {
            $callback = $outline_handler['callback'];
          }
        }
      }
    }
    if (!empty($callback) && function_exists($callback)) {
      $outline = $callback($node, $user);
    }
    else {
      $outline = t('Outline not provided.');
    }
    if (!$outline) {
      $outline = t('No learning objects are available this time.');
    }
    return $outline;
  }
  else {
    drupal_add_http_header('Status', '403 Forbidden');
    drupal_set_title(t('Access denied'));
    if (empty($take_access['message'])) {
      return t('Sorry, you do not have access to take this course. (No message provided by module).');
    }
    return "<h2>" . $take_access['header'] . "</h2>" . $take_access['message'];
  }
}