diff --git a/classes/observer.php b/classes/observer.php new file mode 100644 index 0000000..16804a8 --- /dev/null +++ b/classes/observer.php @@ -0,0 +1,57 @@ +. + +/** + * Event observers supported by this module + * + * @package mod_attendance + * @copyright 2017 Dan Marsden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Event observers supported by this module + * + * @package mod_attendance + * @copyright 2017 Dan Marsden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_attendance_observer { + + /** + * Observer for the event course_content_deleted - delete all attendance stuff. + * + * @param \core\event\course_content_deleted $event + */ + public static function course_content_deleted(\core\event\course_content_deleted $event) { + global $DB; + + $attids = array_keys($DB->get_records('attendance', array('course' => $event->objectid), '', 'id')); + $sessids = array_keys($DB->get_records_list('attendance_sessions', 'attendanceid', $attids, '', 'id')); + if (attendance_existing_calendar_events_ids($sessids)) { + attendance_delete_calendar_events($sessids); + } + if ($sessids) { + $DB->delete_records_list('attendance_log', 'sessionid', $sessids); + } + if ($attids) { + $DB->delete_records_list('attendance_statuses', 'attendanceid', $attids); + $DB->delete_records_list('attendance_sessions', 'attendanceid', $attids); + } + } +} diff --git a/db/events.php b/db/events.php new file mode 100644 index 0000000..afe87a6 --- /dev/null +++ b/db/events.php @@ -0,0 +1,34 @@ +. + +/** + * Attendance event handler definition. + * + * @package mod_attendance + * @category event + * @copyright 2017 Dan Marsden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +// List of observers. +$observers = array( + + array( + 'eventname' => '\core\event\course_content_deleted', + 'callback' => 'mod_attendance_observer::course_content_deleted', + ), + +); diff --git a/lib.php b/lib.php index e5ab12c..e3d6163 100644 --- a/lib.php +++ b/lib.php @@ -120,26 +120,6 @@ function attendance_delete_instance($id) { return true; } -function attendance_delete_course($course, $feedback=true) { - global $DB; - - $attids = array_keys($DB->get_records('attendance', array('course' => $course->id), '', 'id')); - $sessids = array_keys($DB->get_records_list('attendance_sessions', 'attendanceid', $attids, '', 'id')); - if (attendance_existing_calendar_events_ids($sessids)) { - attendance_delete_calendar_events($sessids); - } - if ($sessids) { - $DB->delete_records_list('attendance_log', 'sessionid', $sessids); - } - if ($attids) { - $DB->delete_records_list('attendance_statuses', 'attendanceid', $attids); - $DB->delete_records_list('attendance_sessions', 'attendanceid', $attids); - } - $DB->delete_records('attendance', array('course' => $course->id)); - - return true; -} - /** * Called by course/reset.php * @param $mform form passed by reference diff --git a/version.php b/version.php index b9ce0a8..79d9f6f 100644 --- a/version.php +++ b/version.php @@ -23,7 +23,7 @@ */ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2017020202; +$plugin->version = 2017020203; $plugin->requires = 2016111800; $plugin->release = '3.2.3'; $plugin->maturity = MATURITY_STABLE;