MDL-45758 tool_monitor: Add observers to handle user and module deletion
authorAnkit Agarwal <ankit@moodle.com>
Thu, 2 Oct 2014 06:45:51 +0000 (12:15 +0530)
committerAnkit Agarwal <ankit@moodle.com>
Wed, 15 Oct 2014 02:11:06 +0000 (07:41 +0530)
Original issue - MDL-47376

admin/tool/monitor/classes/eventobservers.php
admin/tool/monitor/classes/subscription_manager.php
admin/tool/monitor/db/events.php

index a63dc0d..27d3d29 100644 (file)
@@ -187,4 +187,24 @@ class eventobservers {
         $this->count = 0;
         return $events;
     }
+
+    /**
+     * Observer that monitors user deleted event and delete user subscriptions.
+     *
+     * @param \core\event\user_deleted $event the event object.
+     */
+    public static function user_deleted(\core\event\user_deleted $event) {
+        $userid = $event->objectid;
+        subscription_manager::delete_user_subscriptions($userid);
+    }
+
+    /**
+     * Observer that monitors course module deleted event and delete user subscriptions.
+     *
+     * @param \core\event\course_module_deleted $event the event object.
+     */
+    public static function course_module_deleted(\core\event\course_module_deleted $event) {
+        $cmid = $event->contextinstanceid;
+        subscription_manager::delete_cm_subscriptions($cmid);
+    }
 }
index c7b67c5..3d4d0f3 100644 (file)
@@ -84,6 +84,30 @@ class subscription_manager {
         return $DB->delete_records('tool_monitor_subscriptions', array('id' => $subscription->id));
     }
 
+    /**
+     * Delete all subscriptions for a user.
+     *
+     * @param int $userid user id.
+     *
+     * @return mixed
+     */
+    public static function delete_user_subscriptions($userid) {
+        global $DB;
+        return $DB->delete_records('tool_monitor_subscriptions', array('userid' => $userid));
+    }
+
+    /**
+     * Delete all subscriptions for a course module.
+     *
+     * @param int $cmid cm id.
+     *
+     * @return mixed
+     */
+    public static function delete_cm_subscriptions($cmid) {
+        global $DB;
+        return $DB->delete_records('tool_monitor_subscriptions', array('cmid' => $cmid));
+    }
+
     /**
      * Delete all subscribers for a given rule.
      *
index 4589c58..0b1859f 100644 (file)
 $observers = array(
     array(
         'eventname'   => '\core\event\course_deleted',
+        'priority'    => 1,
         'callback'    => '\tool_monitor\eventobservers::course_deleted',
     ),
     array(
         'eventname'   => '*',
         'callback'    => '\tool_monitor\eventobservers::process_event',
+    ),
+    array(
+        'eventname'   => '\core\event\user_deleted',
+        'callback'    => '\tool_monitor\eventobservers::user_deleted',
+    ),
+    array(
+        'eventname'   => '\core\event\course_module_deleted',
+        'callback'    => '\tool_monitor\eventobservers::course_module_deleted',
     )
 );