You are here

function course_enrol in Course 6

Enrols a user in a course.

Timestamp is by design 0, so a user may purchase a course but start taking it later.

Parameters

object $node: By reference. The course node.

object $user: By reference. The enrolling user.

string $from: The type of enrollment, if applicable. {course_enrolment}.enrollmenttype.

string $code: The access code used to enroll. {course_enrolment}.code.

integer $status: The enrolment status. {course_enrolment}.status.

14 calls to course_enrol()
CourseAccessTestCase::testDurationExpiration in tests/CourseAccessTestCase.test
Test the enrollment duration. This does not test the enrollment end date being set correctly.
CourseAccessTestCase::testReleaseExpiration in tests/CourseAccessTestCase.test
Test the open/close date functionality.
CourseEnrollmentTestCase::testCourseEnrollment in tests/CourseEnrollmentTestCase.test
Test for enrollment access and timestamping.
CourseObjectAccessConditionalTestCase::testTimeAfterCompletion in tests/CourseObjectAccessConditionalTestCase.test
Test the completion based trigger for object access.
CourseObjectAccessConditionalTestCase::testTimeAfterStart in tests/CourseObjectAccessConditionalTestCase.test
Test the time based trigger for object access.

... See full list

1 string reference to 'course_enrol'
course_unenrol in ./course.module
Un-enroll the user.

File

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

Code

function course_enrol($node, $account = NULL, $from = NULL, $code = NULL, $status = 1) {
  if (!$account) {
    global $user;
    $account = $user;
  }
  if (course_node_is_course($node)) {
    $enroll = array(
      'nid' => $node->nid,
      'uid' => $account->uid,
      'enrollmenttype' => $from,
      'status' => $status,
      'code' => $code,
    );
    if (isset($node->course['duration']) && $node->course['duration'] > 0) {

      // Set enrolment end to now + the duration of the course.
      $enroll['enrol_end'] = time() + $node->course['duration'] * 86400;
    }
    $enroll = (object) $enroll;
    $watchdog_variables = array(
      '!uid' => $account->uid,
      '!nid' => $node->nid,
    );
    if (!course_enrolment_check($node->nid, $account->uid)) {

      // User is not enrolled yet.
      watchdog('course_enrol', 'Enrolling user !uid into !nid', $watchdog_variables);
      course_enrolment_save($enroll);
      $op = 'insert';
    }
    else {
      watchdog('course_enrol', 'Re-enrolling user !uid into !nid', $watchdog_variables);

      // return drupal_write_record('course_enrolment', $enroll, array('nid', 'uid'));
      $op = 'update';
    }

    // @todo figure $op out.
    // Notify modules about a course enrollment.
    module_invoke_all('course_enrol', $node, $account, $from, $code, $status);
    return $enroll;
  }
  else {
    return FALSE;
  }
}