public static function get_user_contexts_with_script(userlist $userlist, \context $context, string $script,
                                                         int $instance = null) {
        if (!$context instanceof \context_user) {
            return;
        }

        $params = [
            'userid' => $context->instanceid,
            'script' => $script
        ];

        $whereinstance = '';

        if (!empty($instance)) {
            $params['instance'] = $instance;
            $whereinstance = ' AND k.instance = :instance';
        }

        $sql = "SELECT k.userid
                  FROM {user_private_key} k
                 WHERE k.script = :script
                       AND k.userid = :userid
                       {$whereinstance}";

        $userlist->add_from_sql('userid', $sql, $params);
    }