public static function get_contexts_for_group_member(int $userid, string $component, int $itemid = 0) {
        $contextlist = new contextlist();

        $sql = "SELECT ctx.id
                  FROM {groups_members} gm
                  JOIN {groups} g ON gm.groupid = g.id
                  JOIN {context} ctx ON g.courseid = ctx.instanceid AND ctx.contextlevel = :contextcourse
                 WHERE gm.userid = :userid AND gm.component = :component";

        $params = [
            'contextcourse' => CONTEXT_COURSE,
            'userid'        => $userid,
            'component'     => $component
        ];

        if ($itemid) {
            $sql .= ' AND gm.itemid = :itemid';
            $params['itemid'] = $itemid;
        }

        $contextlist->add_from_sql($sql, $params);

        // Get the contexts where the userid has group conversations.
        \core_message\privacy\provider::add_contexts_for_conversations($contextlist, $userid, 'core_group', 'groups', $itemid);

        return $contextlist;
    }