You are here

function course_enroll in Course 7

Same name and namespace in other branches
  1. 7.2 course.module \course_enroll()

Enrolls a user in a course.

Parameters

object $node: By reference. The course node.

object $user: By reference. The enrolling user.

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

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

integer $status: The enrollment status. {course_enrollment}.status.

23 calls to course_enroll()
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.
CourseGradeTestCase::testCourseFinalGrade in tests/CourseGradeTestCase.test
Test that the final grade is calculated correctly.
CourseGradeTestCase::testCourseGradeAccess in tests/CourseGradeTestCase.test
Test that the course grade access plugin functions properly.

... See full list

File

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

Code

function course_enroll($node, $account, $from = NULL, $code = NULL, $status = 1) {
  if (!$account) {
    global $user;
    $account = $user;
  }
  if (course_node_is_course($node)) {
    $enrollment = entity_create('course_enrollment', array(
      'nid' => $node->nid,
      'uid' => $account->uid,
      'enrollmenttype' => $from,
      'status' => $status,
      'code' => $code,
      'type' => $node->course['enrollment_type'],
    ));
    $watchdog_variables = array(
      '!uid' => $account->uid,
      '!nid' => $node->nid,
    );
    if (!course_enrollment_check($node->nid, $account->uid)) {

      // User is not enrolled yet.
      watchdog('course_enroll', 'Enrolling user !uid into !nid.', $watchdog_variables);
      $enrollment
        ->save();
    }

    // Flush caches.
    course_access_course('take', $node, $account, TRUE);
    return $enrollment;
  }
  else {
    return FALSE;
  }
}