You are here

function course_unenrol in Course 6

Un-enroll the user.

Deletes course report entries, course enrollments, and object fulfillment records.

Parameters

object $node: A course node.

object $user: A user.

Return value

bool TRUE if user is un-enrolled, FALSE if node is not a course.

5 calls to course_unenrol()
course_remove_enrollment_action in ./course.module
Action to unenrol a user.
course_signup_signup_cancel in modules/course_signup/course_signup.module
Implements hook_signup_cancel().
course_signup_signup_delete in modules/course_signup/course_signup.module
Implements hook_signup_delete().
course_user in ./course.module
Implements hook_user().
_course_enrollment_delete in services/course_enrollment_resource.inc
Delete an enrollment by eid.

File

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

Code

function course_unenrol(&$node, &$user) {
  if (course_node_is_course($node)) {
    $course = course_get_course($node, $user);
    $course
      ->unenroll();
    $sql = "DELETE FROM {course_report} WHERE nid = %d AND uid = %d";
    db_query($sql, $node->nid, $user->uid);
    $sql = "DELETE FROM {course_enrolment} WHERE nid = %d AND uid = %d";
    db_query($sql, $node->nid, $user->uid);

    // Find all course objects in this course and delete the fulfillment.
    $sql = "SELECT coid FROM {course_outline} WHERE nid = %d";
    $values = array();
    $result = db_query($sql, $node->nid);
    while ($row = db_fetch_object($result)) {
      $values[] = $row->coid;
    }
    if (count($values)) {
      $values[] = $user->uid;
      $placeholders = db_placeholders($values);
      $sql = "DELETE FROM {course_outline_fulfillment} WHERE coid IN ({$placeholders}) AND uid = %d";
      db_query($sql, $values);
    }

    // Notify other modules after course unenrollment.
    module_invoke_all('course_unenrol', $node, $user);
    $watchdog_variables = array(
      '!uid' => $user->uid,
      '!nid' => $node->nid,
    );
    watchdog('course_enrol', 'Removed user !uid from !nid', $watchdog_variables);
    return TRUE;
  }
  else {
    return FALSE;
  }
}