From 0e5fd1299a49a55bbf7d81fcaebe8aa7bf47f9e4 Mon Sep 17 00:00:00 2001 From: Jerome Mouneyrac Date: Tue, 7 Jun 2011 18:20:07 +0800 Subject: [PATCH] MDL-27564 most parameter should have been optional - return more common information so no extra web service required - note that it could be good to return role and group for each user in each course (it would make sens and save some web service call) --- enrol/externallib.php | 46 ++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/enrol/externallib.php b/enrol/externallib.php index 84cac5389d6..a9dad34d43e 100644 --- a/enrol/externallib.php +++ b/enrol/externallib.php @@ -43,9 +43,9 @@ class moodle_enrol_external extends external_api { return new external_function_parameters( array( 'courseid' => new external_value(PARAM_INT, 'Course id'), - 'withcapability' => new external_value(PARAM_CAPABILITY, 'User should have this capability'), - 'groupid' => new external_value(PARAM_INT, 'Group id, null means all groups'), - 'onlyactive' => new external_value(PARAM_INT, 'True means only active, false means all participants'), + 'withcapability' => new external_value(PARAM_CAPABILITY, 'User should have this capability', VALUE_DEFAULT, null), + 'groupid' => new external_value(PARAM_INT, 'Group id, null means all groups', VALUE_DEFAULT, null), + 'onlyactive' => new external_value(PARAM_INT, 'True means only active, false means all participants', VALUE_DEFAULT, 0), ) ); } @@ -59,8 +59,8 @@ class moodle_enrol_external extends external_api { * @param bool $onlyactive * @return array of course participants */ - public static function get_enrolled_users($courseid, $withcapability, $groupid, $onlyactive) { - global $DB; + public static function get_enrolled_users($courseid, $withcapability = null, $groupid = null, $onlyactive = false) { + global $DB, $CFG, $USER; // Do basic automatic PARAM checks on incoming data, using params description // If any problems are found then exceptions are thrown with helpful error messages @@ -101,19 +101,35 @@ class moodle_enrol_external extends external_api { require_capability('moodle/course:enrolreview', $coursecontext); } - list($sql, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive); - $sql = "SELECT DISTINCT ue.userid, e.courseid + list($sqlparams, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive); + $sql = "SELECT ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id as usercontextid FROM {user_enrolments} ue JOIN {enrol} e ON (e.id = ue.enrolid) - WHERE e.courseid = :courseid AND ue.userid IN ($sql)"; + JOIN {user} u ON (ue.userid = u.id) + JOIN {context} c ON (u.id = c.instanceid AND contextlevel = " . CONTEXT_USER . ") + WHERE e.courseid = :courseid AND ue.userid IN ($sqlparams) + GROUP BY ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id"; $params['courseid'] = $courseid; - $enrolledusers = $DB->get_records_sql($sql, $params); - $result = array(); foreach ($enrolledusers as $enrolleduser) { - $result[] = array('courseid' => $enrolleduser->courseid, - 'userid' => $enrolleduser->userid); + $profilimgurl = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f1'); + $profilimgurlsmall = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f2'); + $resultuser = array('courseid' => $enrolleduser->courseid, + 'userid' => $enrolleduser->userid, 'fullname' => fullname($enrolleduser), + 'profileimgurl' => $profilimgurl->out(false), + 'profileimgurlsmall' => $profilimgurlsmall->out(false)); + //check if we can return username + $isadmin = is_siteadmin($USER); + if ($isadmin) { + $resultuser['username'] = $enrolleduser->username; + } + //check if we can return first and last name + if ($isadmin or has_capability('moodle/site:viewfullnames', $context)) { + $resultuser['firstname'] = $enrolleduser->firstname; + $resultuser['lastname'] = $enrolleduser->lastname; + } + $result[] = $resultuser; } return $result; @@ -129,6 +145,12 @@ class moodle_enrol_external extends external_api { array( 'courseid' => new external_value(PARAM_INT, 'id of course'), 'userid' => new external_value(PARAM_INT, 'id of user'), + 'firstname' => new external_value(PARAM_RAW, 'first name of user', VALUE_OPTIONAL), + 'lastname' => new external_value(PARAM_RAW, 'last name of user', VALUE_OPTIONAL), + 'fullname' => new external_value(PARAM_RAW, 'fullname of user'), + 'username' => new external_value(PARAM_RAW, 'username of user', VALUE_OPTIONAL), + 'profileimgurl' => new external_value(PARAM_URL, 'url of the profile image'), + 'profileimgurlsmall' => new external_value(PARAM_URL, 'url of the profile image (small version)') ) ) );