Permalink
Browse files

MDL-27564 most parameter should have been optional - return more comm…

…on 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)
  • Loading branch information...
1 parent a56c68e commit 0e5fd1299a49a55bbf7d81fcaebe8aa7bf47f9e4 @mouneyrac mouneyrac committed Jun 7, 2011
Showing with 34 additions and 12 deletions.
  1. +34 −12 enrol/externallib.php
View
@@ -43,9 +43,9 @@ public static function get_enrolled_users_parameters() {
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 @@ public static function get_enrolled_users_parameters() {
* @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 @@ public static function get_enrolled_users($courseid, $withcapability, $groupid,
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 @@ public static function get_enrolled_users_returns() {
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)')
)
)
);

0 comments on commit 0e5fd12

Please sign in to comment.