public static function get_users_by_field($field, $values) {
        global $CFG, $USER, $DB;
        require_once($CFG->dirroot . "/user/lib.php");

        $params = self::validate_parameters(self::get_users_by_field_parameters(),
                array('field' => $field, 'values' => $values));

        // This array will keep all the users that are allowed to be searched,
        // according to the current user's privileges.
        $cleanedvalues = array();

        switch ($field) {
            case 'id':
                $paramtype = core_user::get_property_type('id');
                break;
            case 'idnumber':
                $paramtype = core_user::get_property_type('idnumber');
                break;
            case 'username':
                $paramtype = core_user::get_property_type('username');
                break;
            case 'email':
                $paramtype = core_user::get_property_type('email');
                break;
            default:
                throw new coding_exception('invalid field parameter',
                        'The search field \'' . $field . '\' is not supported, look at the web service documentation');
        }

        // Clean the values.
        foreach ($values as $value) {
            $cleanedvalue = clean_param($value, $paramtype);
            if ( $value != $cleanedvalue) {
                throw new invalid_parameter_exception('The field \'' . $field .
                        '\' value is invalid: ' . $value . '(cleaned value: '.$cleanedvalue.')');
            }
            $cleanedvalues[] = $cleanedvalue;
        }

        // Retrieve the users.
        $users = $DB->get_records_list('user', $field, $cleanedvalues, 'id');

        $context = context_system::instance();
        self::validate_context($context);

        // Finally retrieve each users information.
        $returnedusers = array();
        foreach ($users as $user) {
            $userdetails = user_get_user_details_courses($user);

            // Return the user only if the searched field is returned.
            // Otherwise it means that the $USER was not allowed to search the returned user.
            if (!empty($userdetails) and !empty($userdetails[$field])) {
                $returnedusers[] = $userdetails;
            }
        }

        return $returnedusers;
    }
public static function get_users_by_field($field, $values) {
        global $CFG, $USER, $DB;
        require_once($CFG->dirroot . "/user/lib.php");

        $params = self::validate_parameters(self::get_users_by_field_parameters(),
                array('field' => $field, 'values' => $values));

        // This array will keep all the users that are allowed to be searched,
        // according to the current user's privileges.
        $cleanedvalues = array();

        switch ($field) {
            case 'id':
                $paramtype = core_user::get_property_type('id');
                break;
            case 'idnumber':
                $paramtype = core_user::get_property_type('idnumber');
                break;
            case 'username':
                $paramtype = core_user::get_property_type('username');
                break;
            case 'email':
                $paramtype = core_user::get_property_type('email');
                break;
            default:
                throw new coding_exception('invalid field parameter',
                        'The search field \'' . $field . '\' is not supported, look at the web service documentation');
        }

        // Clean the values.
        foreach ($values as $value) {
            $cleanedvalue = clean_param($value, $paramtype);
            if ( $value != $cleanedvalue) {
                throw new invalid_parameter_exception('The field \'' . $field .
                        '\' value is invalid: ' . $value . '(cleaned value: '.$cleanedvalue.')');
            }
            $cleanedvalues[] = $cleanedvalue;
        }

        // Retrieve the users.
        $users = $DB->get_records_list('user', $field, $cleanedvalues, 'id');

        $context = context_system::instance();
        self::validate_context($context);

        // Finally retrieve each users information.
        $returnedusers = array();
        foreach ($users as $user) {
            $userdetails = user_get_user_details_courses($user);

            // Return the user only if the searched field is returned.
            // Otherwise it means that the $USER was not allowed to search the returned user.
            if (!empty($userdetails) and !empty($userdetails[$field])) {
                $returnedusers[] = $userdetails;
            }
        }

        return $returnedusers;
    }
public static function get_users_by_field ( $ field , $ values ) { global $ CFG , $ USER , $ DB ; require_once ( $ CFG -> dirroot . "/user/lib.php" ) ; $ params = self :: validate_parameters ( self :: get_users_by_field_parameters ( ) , array ( 'field' => $ field , 'values' => $ values ) ) ; // This array will keep all the users that are allowed to be searched, // according to the current user's privileges. $ cleanedvalues = array ( ) ; switch ( $ field ) { case 'id' : $ paramtype = core_user :: get_property_type ( 'id' ) ; break ; case 'idnumber' : $ paramtype = core_user :: get_property_type ( 'idnumber' ) ; break ; case 'username' : $ paramtype = core_user :: get_property_type ( 'username' ) ; break ; case 'email' : $ paramtype = core_user :: get_property_type ( 'email' ) ; break ; default : throw new coding_exception ( 'invalid field parameter' , 'The search field \'' . $ field . '\' is not supported, look at the web service documentation' ) ; } // Clean the values. foreach ( $ values as $ value ) { $ cleanedvalue = clean_param ( $ value , $ paramtype ) ; if ( $ value != $ cleanedvalue ) { throw new invalid_parameter_exception ( 'The field \'' . $ field . '\' value is invalid: ' . $ value . '(cleaned value: ' . $ cleanedvalue . ')' ) ; } $ cleanedvalues [ ] = $ cleanedvalue ; } // Retrieve the users. $ users = $ DB -> get_records_list ( 'user' , $ field , $ cleanedvalues , 'id' ) ; $ context = context_system :: instance ( ) ; self :: validate_context ( $ context ) ; // Finally retrieve each users information. $ returnedusers = array ( ) ; foreach ( $ users as $ user ) { $ userdetails = user_get_user_details_courses ( $ user ) ; // Return the user only if the searched field is returned. // Otherwise it means that the $USER was not allowed to search the returned user. if ( ! empty ( $ userdetails ) and ! empty ( $ userdetails [ $ field ] ) ) { $ returnedusers [ ] = $ userdetails ; } } return $ returnedusers ; }
Get user information for a unique field.

@throws coding_exception
@throws invalid_parameter_exception
@param string $field
@param array $values
@return array An array of arrays containg user profiles.
@since Moodle 2.4
Get user information for a unique field .