protected function get_users_with_capability_sql($capability, $musthavesubmission, $groupid) {
        global $CFG;
        /** @var int static counter used to generate unique parameter holders */
        static $inc = 0;
        $inc++;

        // If the caller requests all groups and we are using a selected grouping,
        // recursively call this function for each group in the grouping (this is
        // needed because get_enrolled_sql only supports a single group).
        if (empty($groupid) and $this->cm->groupingid) {
            $groupingid = $this->cm->groupingid;
            $groupinggroupids = array_keys(groups_get_all_groups($this->cm->course, 0, $this->cm->groupingid, 'g.id'));
            $sql = array();
            $params = array();
            foreach ($groupinggroupids as $groupinggroupid) {
                if ($groupinggroupid > 0) { // just in case in order not to fall into the endless loop
                    list($gsql, $gparams) = $this->get_users_with_capability_sql($capability, $musthavesubmission, $groupinggroupid);
                    $sql[] = $gsql;
                    $params = array_merge($params, $gparams);
                }
            }
            $sql = implode(PHP_EOL." UNION ".PHP_EOL, $sql);
            return array($sql, $params);
        }

        list($esql, $params) = get_enrolled_sql($this->context, $capability, $groupid, true);

        $userfields = user_picture::fields('u');

        $sql = "SELECT $userfields
                  FROM {user} u
                  JOIN ($esql) je ON (je.id = u.id AND u.deleted = 0) ";

        if ($musthavesubmission) {
            $sql .= " JOIN {workshop_submissions} ws ON (ws.authorid = u.id AND ws.example = 0 AND ws.workshopid = :workshopid{$inc}) ";
            $params['workshopid'.$inc] = $this->id;
        }

        // If the activity is restricted so that only certain users should appear
        // in user lists, integrate this into the same SQL.
        $info = new \core_availability\info_module($this->cm);
        list ($listsql, $listparams) = $info->get_user_list_sql(false);
        if ($listsql) {
            $sql .= " JOIN ($listsql) restricted ON restricted.id = u.id ";
            $params = array_merge($params, $listparams);
        }

        return array($sql, $params);
    }