public function add_from_sql(string $fieldname, string $sql, array $params) : userlist {
        global $DB;

        // Able to guess a field name.
        $wrapper = "
            SELECT DISTINCT u.id
            FROM {user} u
            JOIN ({$sql}) target ON u.id = target.{$fieldname}";

        $users = $DB->get_records_sql($wrapper, $params);
        $this->add_userids(array_keys($users));

        return $this;
    }
public function add_from_sql(string $fieldname, string $sql, array $params) : userlist {
        global $DB;

        // Able to guess a field name.
        $wrapper = "
            SELECT DISTINCT u.id
            FROM {user} u
            JOIN ({$sql}) target ON u.id = target.{$fieldname}";

        $users = $DB->get_records_sql($wrapper, $params);
        $this->add_userids(array_keys($users));

        return $this;
    }
public function add_from_sql ( string $ fieldname , string $ sql , array $ params ) : userlist { global $ DB ; // Able to guess a field name. $ wrapper = "
            SELECT DISTINCT u.id
            FROM {user} u
            JOIN ({$sql}) target ON u.id = target.{$fieldname}" ; $ users = $ DB -> get_records_sql ( $ wrapper , $ params ) ; $ this -> add_userids ( array_keys ( $ users ) ) ; return $ this ; }
Add a set of users from  SQL.

The SQL should only return a list of user IDs.

@param   string  $fieldname The name of the field which holds the user id
@param   string  $sql    The SQL which will fetch the list of * user IDs
@param   array   $params The set of SQL parameters
@return  $this
Add a set of users from SQL .