public static function delete_subscription($subscriptionorid, $checkuser = true) {
        global $DB, $USER;
        if (is_object($subscriptionorid)) {
            $subscription = $subscriptionorid;
        } else {
            $subscription = self::get_subscription($subscriptionorid);
        }
        if ($checkuser && $subscription->userid != $USER->id) {
            throw new \coding_exception('Invalid subscription supplied');
        }

        // Store the subscription before we delete it.
        $subscription = $DB->get_record('tool_monitor_subscriptions', array('id' => $subscription->id));

        $success = $DB->delete_records('tool_monitor_subscriptions', array('id' => $subscription->id));

        // If successful trigger a subscription_deleted event.
        if ($success) {
            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);
        }

        return $success;
    }