Created
April 30, 2012 16:15
-
-
Save fabiomsouto/2559689 to your computer and use it in GitHub Desktop.
new implementation of core_user_get_users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public static function get_users_parameters() { | |
return new external_function_parameters( | |
array( | |
'criteria' => new external_multiple_structure( | |
new external_single_structure( | |
array( | |
'key' => new external_value(PARAM_ALPHA, 'the user column to search'), | |
'value' => new external_value(PARAM_TEXT, 'the value to match') | |
) | |
) | |
), | |
'addcourseprofiles' => new external_value(PARAM_BOOL, 'return the user\'s course profiles (0 - default) | |
otherwise only the user system profile (0)', VALUE_DEFAULT, 0) | |
) | |
); | |
} | |
/** | |
* Get user information, filtered by key/value pairs. | |
* - This function is matching the permissions of /user/profile.php | |
* - It is also matching some permissions from /user/editadvanced.php for the following fields: | |
* auth, confirmed, idnumber, lang, theme, timezone, mailformat | |
* @param array $keys array of keys | |
* @return array An array of arrays describing users | |
*/ | |
public static function get_users($criteria = array(), $addcourseprofiles = false) { | |
global $CFG, $USER, $DB; | |
require_once($CFG->dirroot . "user/lib.php"); | |
require_once($CFG->dirroot . "enrol/enrollib.php"); | |
$params = self::validate_parameters(self::get_users_parameters(), array('criteria' => $criteria)); | |
$users = array(); | |
if (!empty($params['criteria'])) { | |
$conditions = array(); | |
$wheres = array(); | |
foreach ($params['criteria'] as $crit) { | |
$key = trim($crit['key']); | |
// Trying to avoid duplicate keys. | |
if (!isset($conditions[$key])) { | |
switch($key) { | |
case 'id': | |
$value = clean_param($crit['value'], PARAM_INT); | |
break; | |
case 'idnumber': | |
$value = clean_param($crit['value'], PARAM_RAW); | |
break; | |
case 'username': | |
$value = clean_param($crit['value'], PARAM_RAW); | |
break; | |
case 'fullname': | |
case 'firstname': | |
case 'lastname': | |
$value = clean_param($crit['value'], PARAM_NOTAGS); | |
break; | |
case 'email': | |
$value = clean_param($crit['value'], PARAM_EMAIL); | |
break; | |
case 'auth': | |
$value = clean_param($crit['value'], PARAM_PLUGIN); | |
break; | |
default: | |
throw new moodle_exception('invalidextparam', 'webservice', '', $key); | |
} | |
if ($value != '') { | |
$conditions[$key] = $crit['value']; | |
$wheres[] = $key . " = :" . $key; | |
} | |
} | |
} | |
list($uselect, $ujoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx'); | |
if (!empty($wheres)) { | |
$wheres = implode(" AND ", $wheres); | |
$usersql = "SELECT u.* $uselect | |
FROM {user} u $ujoin | |
WHERE $wheres | |
ORDER BY username ASC"; | |
} | |
else { | |
$usersql = "SELECT u.* $uselect | |
FROM {user} u $ujoin | |
ORDER BY username ASC"; | |
} | |
$users = $DB->get_recordset_sql($usersql, $conditions); | |
foreach ($users as $user) { | |
if ($addcourseprofiles) { | |
//get courses this user is in and add the profile info of each to the user info | |
} | |
else { | |
if ($userarray = user_get_user_details($user)) { | |
$result[] = $userarray; | |
} | |
} | |
} | |
} | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment