Skip to content

Commit

Permalink
Merge branch 'MDL_52574_master' of https://github.com/alexandru-elise…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Mar 15, 2016
2 parents ab5c48b + b6d1286 commit e77a158
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/accesslib.php
Expand Up @@ -4110,8 +4110,7 @@ function sort_by_roleassignment_authority($users, context $context, $roles = arr
* system is more flexible. If you really need, you can to use this
* function but consider has_capability() as a possible substitute.
*
* The caller function is responsible for including all the
* $sort fields in $fields param.
* All $sort fields are added into $fields if not present there yet.
*
* If $roleid is an array or is empty (all roles) you need to set $fields
* (and $sort by extension) params according to it, as the first field
Expand Down Expand Up @@ -4209,6 +4208,22 @@ function get_role_users($roleid, context $context, $parent = false, $fields = ''
$params = array_merge($params, $sortparams);
}

// Adding the fields from $sort that are not present in $fields.
$sortarray = preg_split('/,\s*/', $sort);
$fieldsarray = preg_split('/,\s*/', $fields);
$addedfields = array();
foreach ($sortarray as $sortfield) {
if (!in_array($sortfield, $fieldsarray)) {
$fieldsarray[] = $sortfield;
$addedfields[] = $sortfield;
}
}
$fields = implode(', ', $fieldsarray);
if (!empty($addedfields)) {
$addedfields = implode(', ', $addedfields);
debugging('get_role_users() adding '.$addedfields.' to the query result because they were required by $sort but missing in $fields');
}

if ($all === null) {
// Previously null was used to indicate that parameter was not used.
$all = true;
Expand Down
10 changes: 10 additions & 0 deletions lib/tests/accesslib_test.php
Expand Up @@ -1443,6 +1443,16 @@ public function test_get_role_users() {
$this->assertArrayHasKey($user1->id, $users);
$this->assertArrayHasKey($user3->id, $users);

$users = get_role_users($teacherrole->id, $coursecontext, false, 'u.id, u.email');
$this->assertDebuggingCalled('get_role_users() adding u.lastname, u.firstname to the query result because they were required by $sort but missing in $fields');
$this->assertCount(2, $users);
$this->assertArrayHasKey($user1->id, $users);
$this->assertObjectHasAttribute('lastname', $users[$user1->id]);
$this->assertObjectHasAttribute('firstname', $users[$user1->id]);
$this->assertArrayHasKey($user3->id, $users);
$this->assertObjectHasAttribute('lastname', $users[$user3->id]);
$this->assertObjectHasAttribute('firstname', $users[$user3->id]);

$users = get_role_users($teacherrole->id, $coursecontext, false, 'u.id, u.email, u.idnumber', 'u.idnumber', null, $group->id);
$this->assertCount(1, $users);
$this->assertArrayHasKey($user3->id, $users);
Expand Down
3 changes: 3 additions & 0 deletions lib/upgrade.txt
Expand Up @@ -3,6 +3,9 @@ information provided here is intended especially for developers.

=== 3.1 ===

* The get_role_users() function will now add the $sort fields that are not part
of the requested fields to the query result and will throw a debugging message
with the added fields when that happens.
* The core_user::fill_properties_cache() static method has been introduced to be a reference
and allow standard user fields data validation. Right now only type validation is supported
checking it against the parameter (PARAM_*) type of the target user field. MDL-52781 is
Expand Down

0 comments on commit e77a158

Please sign in to comment.