public static function remove_all_subscriptions_for_rule($ruleid, $coursecontext = null) {
        global $DB;

        // Store all the subscriptions we have to delete.
        $subscriptions = $DB->get_recordset('tool_monitor_subscriptions', array('ruleid' => $ruleid));

        // Now delete them.
        $success = $DB->delete_records('tool_monitor_subscriptions', array('ruleid' => $ruleid));

        // If successful and there were subscriptions that were deleted trigger a subscription deleted event.
        if ($success && $subscriptions) {
            foreach ($subscriptions as $subscription) {
                // It is possible that we are deleting rules associated with a deleted course, so we should be
                // passing the context as the second parameter.
                if (!is_null($coursecontext)) {
                    $context = $coursecontext;
                    $courseid = $subscription->courseid;
                } else if (!empty($subscription->courseid) && ($coursecontext =
                        \context_course::instance($subscription->courseid, IGNORE_MISSING))) {
                    $courseid = $subscription->courseid;
                    $context = $coursecontext;
                } else {
                    $courseid = 0;
                    $context = \context_system::instance();
                }

                $params = array(
                    'objectid' => $subscription->id,
                    'courseid' => $courseid,
                    'context' => $context
                );
                $event = \tool_monitor\event\subscription_deleted::create($params);
                $event->add_record_snapshot('tool_monitor_subscriptions', $subscription);
                $event->trigger();

                // Let's invalidate the cache.
                $cache = \cache::make('tool_monitor', 'eventsubscriptions');
                $cache->delete($courseid);
            }
        }

        $subscriptions->close();

        return $success;
    }