public static function delete_stale_subscriptions($userid = 0) {
        global $DB;
        // Get the expiry duration, in days.
        $cutofftime = strtotime("-" . self::INACTIVE_SUBSCRIPTION_LIFESPAN_IN_DAYS . " days", time());

        if (!empty($userid)) {
            // Remove any stale subscriptions for the desired user only.
            $success = $DB->delete_records_select('tool_monitor_subscriptions',
                                                  'userid = ? AND inactivedate < ? AND inactivedate <> 0',
                                                  array($userid, $cutofftime));

        } else {
            // Remove all stale subscriptions.
            $success = $DB->delete_records_select('tool_monitor_subscriptions',
                                                  'inactivedate < ? AND inactivedate <> 0',
                                                  array($cutofftime));
        }
        return $success;
    }