Skip to content

Commit

Permalink
MDL-51925 enrol: Support ordering in get_enrolled_users WS
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Nov 17, 2015
1 parent 26653c8 commit 4bc38ba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
31 changes: 29 additions & 2 deletions enrol/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,10 @@ public static function get_enrolled_users_parameters() {
* onlyactive (integer) return only users with active enrolments and matching time restrictions. This option requires \'moodle/course:enrolreview\' on the course context.
* userfields (\'string, string, ...\') return only the values of these user fields.
* limitfrom (integer) sql limit from.
* limitnumber (integer) maximum number of returned users.', VALUE_DEFAULT, array()),
* limitnumber (integer) maximum number of returned users.
* sortby (string) sort by id, firstname or lastname. For ordering like the site does, use siteorder.
* sortdirection (string) ASC or DESC',
VALUE_DEFAULT, array()),
)
);
}
Expand Down Expand Up @@ -417,6 +420,9 @@ public static function get_enrolled_users($courseid, $options = array()) {
$userfields = array();
$limitfrom = 0;
$limitnumber = 0;
$sortby = 'us.id';
$sortparams = array();
$sortdirection = 'ASC';
foreach ($options as $option) {
switch ($option['name']) {
case 'withcapability':
Expand All @@ -440,6 +446,26 @@ public static function get_enrolled_users($courseid, $options = array()) {
case 'limitnumber' :
$limitnumber = clean_param($option['value'], PARAM_INT);
break;
case 'sortby':
$sortallowedvalues = array('id', 'firstname', 'lastname', 'siteorder');
if (!in_array($option['value'], $sortallowedvalues)) {
throw new invalid_parameter_exception('Invalid value for sortby parameter (value: ' . $option['value'] . '),' .
'allowed values are: ' . implode(',', $sortallowedvalues));
}
if ($option['value'] == 'siteorder') {
list($sortby, $sortparams) = users_order_by_sql('us');
} else {
$sortby = 'us.' . $option['value'];
}
break;
case 'sortdirection':
$sortdirection = strtoupper($option['value']);
$directionallowedvalues = array('ASC', 'DESC');
if (!in_array($sortdirection, $directionallowedvalues)) {
throw new invalid_parameter_exception('Invalid value for sortdirection parameter
(value: ' . $sortdirection . '),' . 'allowed values are: ' . implode(',', $directionallowedvalues));
}
break;
}
}

Expand Down Expand Up @@ -503,7 +529,8 @@ public static function get_enrolled_users($courseid, $options = array()) {
FROM {user} u $ctxjoin $groupjoin
WHERE u.id IN ($enrolledsql)
) q ON q.id = us.id
ORDER BY us.id ASC";
ORDER BY $sortby $sortdirection";
$enrolledparams = array_merge($enrolledparams, $sortparams);
$enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber);
$users = array();
foreach ($enrolledusers as $user) {
Expand Down
3 changes: 3 additions & 0 deletions enrol/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
This files describes API changes in /enrol/* - plugins,
information provided here is intended especially for developers.

=== 3.1 ===
* core_enrol_external::get_enrolled_users now supports two additional parameters for ordering: sortby and sortdirection.

=== 3.0 ===

* Added new events enrol_instance_created, enrol_instance_updated and
Expand Down

0 comments on commit 4bc38ba

Please sign in to comment.