From 4bc38ba96a30b8821cd86a49376dcd6ae379879a Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 16 Nov 2015 18:27:01 +0100 Subject: [PATCH] MDL-51925 enrol: Support ordering in get_enrolled_users WS --- enrol/externallib.php | 31 +++++++++++++++++++++++++++++-- enrol/upgrade.txt | 3 +++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/enrol/externallib.php b/enrol/externallib.php index 7e64f2197c1f3..331ee3dbeba0d 100644 --- a/enrol/externallib.php +++ b/enrol/externallib.php @@ -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()), ) ); } @@ -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': @@ -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; } } @@ -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) { diff --git a/enrol/upgrade.txt b/enrol/upgrade.txt index f98bcbbc8a50a..38a41e110c41d 100644 --- a/enrol/upgrade.txt +++ b/enrol/upgrade.txt @@ -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