You are here

function course_course_enrollment_delete in Course 7

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

Implements hook_course_enrollment_delete().

Clean up after an enrollment deletion.

File

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

Code

function course_course_enrollment_delete($enrollment) {
  $report = course_report_load($enrollment->nid, $enrollment->uid);

  /** @var $course Course */
  $course = entity_load_single('course', $enrollment->nid);

  // @todo deprecated call
  $course
    ->setUser(user_load($enrollment->uid));
  $course
    ->unenroll();

  // Also delete the course report.
  $report
    ->delete();

  // Find all course objects in this course and delete the fulfillment.
  $coids = array();
  $result = db_query("SELECT coid FROM {course_outline} WHERE nid = :nid", array(
    ':nid' => $enrollment->nid,
  ));
  while ($row = $result
    ->fetch()) {
    $coids[] = $row->coid;
  }
  if (count($coids)) {
    $sql = "SELECT cofid FROM {course_outline_fulfillment} WHERE coid IN (:coids) AND uid = :uid";
    $cofid = db_query($sql, array(
      ':coids' => $coids,
      ':uid' => $enrollment->uid,
    ))
      ->fetchAllKeyed(0, 0);
    entity_delete_multiple('course_object_fulfillment', $cofid);
  }
}