MDL-29476 web service improvements

1. moodle_enrol_get_users_courses should return number of participants,
2. moodle_user_get_users_by_courseid should limit the fields to be returned
This commit is contained in:
Dongsheng Cai 2011-11-15 16:14:07 +08:00
parent 6be90ce05f
commit ad7612f597
3 changed files with 116 additions and 57 deletions

View File

@ -57,7 +57,7 @@ class core_enrol_external extends external_api {
* @return array of courses
*/
public static function get_users_courses($userid) {
global $USER;
global $USER, $DB;
// Do basic automatic PARAM checks on incoming data, using params description
// If any problems are found then exceptions are thrown with helpful error messages
@ -74,12 +74,17 @@ class core_enrol_external extends external_api {
// current user can not access this course, sorry we can not disclose who is enrolled in this course!
continue;
}
if ($userid != $USER->id and !has_capability('moodle/course:viewparticipants', $context)) {
// we need capability to view participants
continue;
}
$result[] = array('id'=>$course->id, 'shortname'=>$course->shortname, 'fullname'=>$course->fullname, 'idnumber'=>$course->idnumber,'visible'=>$course->visible);
list($enrolledsqlselect, $enrolledparams) = get_enrolled_sql($context);
$enrolledsql = "SELECT COUNT(*) FROM ($enrolledsqlselect) AS enrolleduserids";
$enrolledusercount = $DB->count_records_sql($enrolledsql, $enrolledparams);
$result[] = array('id'=>$course->id, 'shortname'=>$course->shortname, 'fullname'=>$course->fullname, 'idnumber'=>$course->idnumber,'visible'=>$course->visible, 'enrolledusercount'=>$enrolledusercount);
}
return $result;
@ -96,6 +101,7 @@ class core_enrol_external extends external_api {
'id' => new external_value(PARAM_INT, 'id of course'),
'shortname' => new external_value(PARAM_RAW, 'short name of course'),
'fullname' => new external_value(PARAM_RAW, 'long name of course'),
'enrolledusercount' => new external_value(PARAM_INT, 'Number of enrolled users in this course'),
'idnumber' => new external_value(PARAM_RAW, 'id number of course'),
'visible' => new external_value(PARAM_INT, '1 means visible, 0 means hidden course'),
)
@ -145,6 +151,7 @@ class core_enrol_external extends external_api {
$withcapability = '';
$groupid = 0;
$onlyactive = false;
$userfields = array();
foreach ($options as $option) {
switch ($option['name']) {
case 'withcapability':
@ -156,6 +163,12 @@ class core_enrol_external extends external_api {
case 'onlyactive':
$onlyactive = !empty($option['value']);
break;
case 'userfields':
$thefields = explode(',', $option['value']);
foreach ($thefields as $f) {
$userfields[] = clean_param($f, PARAM_ALPHANUMEXT);
}
break;
}
}
@ -208,7 +221,7 @@ class core_enrol_external extends external_api {
continue;
}
context_instance_preload($user);
if ($userdetails = user_get_user_details($user, $course)) {
if ($userdetails = user_get_user_details($user, $course, $userfields)) {
$users[] = $userdetails;
}
}
@ -249,8 +262,8 @@ class core_enrol_external extends external_api {
'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version'),
'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version'),
'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version', VALUE_OPTIONAL),
'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version', VALUE_OPTIONAL),
'customfields' => new external_multiple_structure(
new external_single_structure(
array(

View File

@ -319,6 +319,7 @@ $string['invalidstatedetected'] = 'Something has gone wrong: {$a}. This should n
$string['invalidurl'] = 'Invalid URL';
$string['invaliduser'] = 'Invalid user';
$string['invaliduserid'] = 'Invalid user id';
$string['invaliduserfield'] = 'Invalid user field: {$a}';
$string['invalidxmlfile'] = '"{$a}" is not a valid XML file';
$string['iplookupfailed'] = 'Cannot find geo information about this IP address {$a}';
$string['iplookupprivate'] = 'Cannot display lookup of private IP address';

View File

@ -104,13 +104,42 @@ function user_get_users_by_id($userids) {
* @param stdClass $user user record from mdl_user
* @param stdClass $context context object
* @param stdClass $course moodle course
* @param array $userfields required fields
* @return array
*/
function user_get_user_details($user, $course = null) {
function user_get_user_details($user, $course = null, array $userfields = array()) {
global $USER, $DB, $CFG;
require_once($CFG->dirroot . "/user/profile/lib.php"); //custom field library
require_once($CFG->dirroot . "/lib/filelib.php"); // file handling on description and friends
$defaultfields = array( 'id', 'username', 'fullname', 'firstname', 'lastname', 'email',
'address', 'phone1', 'phone2', 'icq', 'skype', 'yahoo', 'aim', 'msn', 'department',
'institution', 'interests', 'firstaccess', 'lastaccess', 'auth', 'confirmed',
'idnumber', 'lang', 'theme', 'timezone', 'mailformat', 'description', 'descriptionformat',
'city', 'url', 'country', 'profileimageurlsmall', 'profileimageurl', 'customfields',
'groups', 'roles', 'preferences', 'enrolledcourses'
);
if (empty($userfields)) {
$userfields = $defaultfields;
}
foreach ($userfields as $thefield) {
if (!in_array($thefield, $defaultfields)) {
throw new moodle_exception('invaliduserfield', 'error', '', $thefield);
}
}
// Make sure id and fullname are included
if (!in_array('id', $userfields)) {
$userfields[] = 'id';
}
if (!in_array('fullname', $userfields)) {
$userfields[] = 'fullname';
}
if (!empty($course)) {
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$usercontext = get_context_instance(CONTEXT_USER, $user->id);
@ -150,42 +179,52 @@ function user_get_user_details($user, $course = null) {
$userdetails = array();
$userdetails['id'] = $user->id;
if ($isadmin or $currentuser) {
if (($isadmin or $currentuser) and in_array('username', $userfields)) {
$userdetails['username'] = $user->username;
}
if ($isadmin or $canviewfullnames) {
$userdetails['firstname'] = $user->firstname;
$userdetails['lastname'] = $user->lastname;
if (in_array('firstname', $userfields)) {
$userdetails['firstname'] = $user->firstname;
}
if (in_array('lastname', $userfields)) {
$userdetails['lastname'] = $user->lastname;
}
}
$userdetails['fullname'] = fullname($user);
$fields = $DB->get_recordset_sql("SELECT f.*
FROM {user_info_field} f
JOIN {user_info_category} c
ON f.categoryid=c.id
ORDER BY c.sortorder ASC, f.sortorder ASC");
$userdetails['customfields'] = array();
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $user->id);
if ($formfield->is_visible() and !$formfield->is_empty()) {
$userdetails['customfields'][] =
array('name' => $formfield->field->name, 'value' => $formfield->data,
'type' => $field->datatype, 'shortname' => $formfield->field->shortname);
if (in_array('customfields', $userfields)) {
$fields = $DB->get_recordset_sql("SELECT f.*
FROM {user_info_field} f
JOIN {user_info_category} c
ON f.categoryid=c.id
ORDER BY c.sortorder ASC, f.sortorder ASC");
$userdetails['customfields'] = array();
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $user->id);
if ($formfield->is_visible() and !$formfield->is_empty()) {
$userdetails['customfields'][] =
array('name' => $formfield->field->name, 'value' => $formfield->data,
'type' => $field->datatype, 'shortname' => $formfield->field->shortname);
}
}
$fields->close();
// unset customfields if it's empty
if (empty($userdetails['customfields'])) {
unset($userdetails['customfields']);
}
}
$fields->close();
// unset customfields if it's empty
if (empty($userdetails['customfields'])) {
unset($userdetails['customfields']);
}
// profile image
$profileimageurl = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', NULL, '/', 'f1');
$userdetails['profileimageurl'] = $profileimageurl->out(false);
$profileimageurlsmall = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', NULL, '/', 'f2');
$userdetails['profileimageurlsmall'] = $profileimageurlsmall->out(false);
if (in_array('profileimageurl', $userfields)) {
$profileimageurl = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', NULL, '/', 'f1');
$userdetails['profileimageurl'] = $profileimageurl->out(false);
}
if (in_array('profileimageurlsmall', $userfields)) {
$profileimageurlsmall = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', NULL, '/', 'f2');
$userdetails['profileimageurlsmall'] = $profileimageurlsmall->out(false);
}
//hidden user field
if ($canviewhiddenuserfields) {
@ -193,13 +232,13 @@ function user_get_user_details($user, $course = null) {
// address, phone1 and phone2 not appears in hidden fields list
// but require viewhiddenfields capability
// according to user/profile.php
if ($user->address) {
if ($user->address && in_array('address', $userfields)) {
$userdetails['address'] = $user->address;
}
if ($user->phone1) {
if ($user->phone1 && in_array('phone1', $userfields)) {
$userdetails['phone1'] = $user->phone1;
}
if ($user->phone2) {
if ($user->phone2 && in_array('phone2', $userfields)) {
$userdetails['phone2'] = $user->phone2;
}
} else {
@ -208,21 +247,26 @@ function user_get_user_details($user, $course = null) {
if (isset($user->description) && (!isset($hiddenfields['description']) or $isadmin)) {
if (!$cannotviewdescription) {
$user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
$userdetails['description'] = $user->description;
$userdetails['descriptionformat'] = $user->descriptionformat;
if (in_array('description', $userfields)) {
$user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
$userdetails['description'] = $user->description;
}
if (in_array('descriptionformat', $userfields)) {
$userdetails['descriptionformat'] = $user->descriptionformat;
}
}
}
if ((!isset($hiddenfields['country']) or $isadmin) && $user->country) {
if (in_array('country', $userfields) && (!isset($hiddenfields['country']) or $isadmin) && $user->country) {
$userdetails['country'] = $user->country;
}
if ((!isset($hiddenfields['city']) or $isadmin) && $user->city) {
if (in_array('city', $userfields) && (!isset($hiddenfields['city']) or $isadmin) && $user->city) {
$userdetails['city'] = $user->city;
}
if ($user->url && (!isset($hiddenfields['webpage']) or $isadmin)) {
if (in_array('url', $userfields) && $user->url && (!isset($hiddenfields['webpage']) or $isadmin)) {
$url = $user->url;
if (strpos($user->url, '://') === false) {
$url = 'http://'. $url;
@ -231,31 +275,31 @@ function user_get_user_details($user, $course = null) {
$userdetails['url'] = $user->url;
}
if ($user->icq && (!isset($hiddenfields['icqnumber']) or $isadmin)) {
if (in_array('icq', $userfields) && $user->icq && (!isset($hiddenfields['icqnumber']) or $isadmin)) {
$userdetails['icq'] = $user->icq;
}
if ($user->skype && (!isset($hiddenfields['skypeid']) or $isadmin)) {
if (in_array('skype', $userfields) && $user->skype && (!isset($hiddenfields['skypeid']) or $isadmin)) {
$userdetails['skype'] = $user->skype;
}
if ($user->yahoo && (!isset($hiddenfields['yahooid']) or $isadmin)) {
if (in_array('yahoo', $userfields) && $user->yahoo && (!isset($hiddenfields['yahooid']) or $isadmin)) {
$userdetails['yahoo'] = $user->yahoo;
}
if ($user->aim && (!isset($hiddenfields['aimid']) or $isadmin)) {
if (in_array('aim', $userfields) && $user->aim && (!isset($hiddenfields['aimid']) or $isadmin)) {
$userdetails['aim'] = $user->aim;
}
if ($user->msn && (!isset($hiddenfields['msnid']) or $isadmin)) {
if (in_array('msn', $userfields) && $user->msn && (!isset($hiddenfields['msnid']) or $isadmin)) {
$userdetails['msn'] = $user->msn;
}
if (!isset($hiddenfields['firstaccess']) or $isadmin) {
if (in_array('firstaccess', $userfields) && (!isset($hiddenfields['firstaccess']) or $isadmin)) {
if ($user->firstaccess) {
$userdetails['firstaccess'] = $user->firstaccess;
} else {
$userdetails['firstaccess'] = 0;
}
}
if (!isset($hiddenfields['lastaccess']) or $isadmin) {
if (in_array('lastaccess', $userfields) && (!isset($hiddenfields['lastaccess']) or $isadmin)) {
if ($user->lastaccess) {
$userdetails['lastaccess'] = $user->lastaccess;
} else {
@ -263,14 +307,14 @@ function user_get_user_details($user, $course = null) {
}
}
if ($currentuser
if (in_array('email', $userfields) && ($currentuser
or $canviewuseremail // this is a capability in course context, it will be false in usercontext
or $user->maildisplay == 1
or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER))) {
or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER)))) {
$userdetails['email'] = $user->email;;
}
if (!empty($CFG->usetags)) {
if (in_array('interests', $userfields) && !empty($CFG->usetags)) {
require_once($CFG->dirroot . '/tag/lib.php');
if ($interests = tag_get_tags_csv('user', $user->id, TAG_RETURN_TEXT) ) {
$userdetails['interests'] = $interests;
@ -279,15 +323,15 @@ function user_get_user_details($user, $course = null) {
//Departement/Institution are not displayed on any profile, however you can get them from editing profile.
if ($isadmin or $currentuser) {
if ($user->institution) {
if (in_array('institution', $userfields) && $user->institution) {
$userdetails['institution'] = $user->institution;
}
if (isset($user->department)) { //isset because it's ok to have department 0
if (in_array('department', $userfields) && isset($user->department)) { //isset because it's ok to have department 0
$userdetails['department'] = $user->department;
}
}
if (!empty($course)) {
if (in_array('roles', $userfields) && !empty($course)) {
// not a big secret
$roles = get_user_roles($context, $user->id, false);
$userdetails['roles'] = array();
@ -302,7 +346,7 @@ function user_get_user_details($user, $course = null) {
}
// If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group
if (!empty($course) && $canaccessallgroups) {
if (in_array('groups', $userfields) && !empty($course) && $canaccessallgroups) {
$usergroups = groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid, 'g.id, g.name,g.description');
$userdetails['groups'] = array();
foreach ($usergroups as $group) {
@ -311,7 +355,7 @@ function user_get_user_details($user, $course = null) {
}
}
//list of courses where the user is enrolled
if (!isset($hiddenfields['mycourses'])) {
if (in_array('enrolledcourses', $userfields) && !isset($hiddenfields['mycourses'])) {
$enrolledcourses = array();
if ($mycourses = enrol_get_users_courses($user->id, true)) {
foreach ($mycourses as $mycourse) {
@ -329,7 +373,7 @@ function user_get_user_details($user, $course = null) {
}
//user preferences
if ($currentuser) {
if (in_array('preferences', $userfields) && $currentuser) {
$preferences = array();
$userpreferences = get_user_preferences();
foreach($userpreferences as $prefname => $prefvalue) {
@ -337,6 +381,7 @@ function user_get_user_details($user, $course = null) {
}
$userdetails['preferences'] = $preferences;
}
return $userdetails;
}