public function find_users($search) {
        global $CFG, $DB;

        $systemcontext = context_system::instance();
        $userids = get_users_by_capability($systemcontext, 'moodle/site:mnetlogintoremote', 'u.id');

        if (empty($userids)) {
            return array();
        }

        list($usql, $uparams) = $DB->get_in_or_equal(array_keys($userids), SQL_PARAMS_NAMED, 'uid');

        list($wherecondition, $params) = $this->search_sql($search, 'u');

        $params = array_merge($params, $uparams);
        $params['hostid'] = $this->hostid;
        $params['remotecourseid'] = $this->remotecourseid;
        $params['mnetlocalhostid'] = $CFG->mnet_localhost_id;

        $fields      = "SELECT ".$this->required_fields_sql("u");
        $countfields = "SELECT COUNT(1)";

        $sql = "          FROM {user} u
                         WHERE $wherecondition
                               AND u.mnethostid = :mnetlocalhostid
                               AND u.id $usql
                               AND u.id NOT IN (SELECT e.userid
                                                  FROM {mnetservice_enrol_enrolments} e
                                                 WHERE (e.hostid = :hostid AND e.remotecourseid = :remotecourseid))";

        list($sort, $sortparams) = users_order_by_sql('u');
        $order = "    ORDER BY $sort";

        if (!$this->is_validating()) {
            $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
            if ($potentialmemberscount > 100) {
                return $this->too_many_results($search, $potentialmemberscount);
            }
        }

        $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));

        if (empty($availableusers)) {
            return array();
        }

        if ($search) {
            $groupname = get_string('enrolcandidatesmatching', 'enrol', $search);
        } else {
            $groupname = get_string('enrolcandidates', 'enrol');
        }

        return array($groupname => $availableusers);
    }