Skip to content

Commit

Permalink
MDL-75283 core: Fetch proper column mapping for sort
Browse files Browse the repository at this point in the history
Calling \core_user\fields::get_sql() incerements a static uniqueid
variable. This should be called only once and not make a separate call
for the sort mapping data as it doing so will result to an incorrect
table alias for the sort mapping data.
  • Loading branch information
junpataleta authored and ilyatregubov committed Sep 7, 2022
1 parent 7bf964c commit 5e37074
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions lib/datalib.php
Expand Up @@ -529,23 +529,19 @@ function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperp
$userfields->with_identity($extracontext, true);
}

$userfields->excluding('id');
$userfields->including('username', 'email', 'city', 'country', 'lastaccess', 'confirmed', 'mnethostid', 'suspended');
['selects' => $selects, 'joins' => $joins, 'params' => $joinparams, 'mappings' => $mappings] =
(array)$userfields->get_sql('u', true);

if ($sort) {
$possiblesortfields = \core_user\fields::for_name();
if ($extracontext) {
$possiblesortfields->with_identity($extracontext);
}
$orderbymap = $possiblesortfields->including('id', 'city', 'country', 'lastaccess', 'confirmed', 'mnethostid', 'suspended');
$orderbymap = $orderbymap->get_sql('u', true)->mappings;
$orderbymap = $mappings;
$orderbymap['default'] = 'lastaccess';
$sort = get_safe_orderby($orderbymap, $sort, $dir);
}

$userfields->excluding('id', 'username', 'email', 'city', 'country', 'lastaccess', 'confirmed', 'mnethostid');
['selects' => $selects, 'joins' => $joins, 'params' => $joinparams] =
(array)$userfields->get_sql('u', true);

// warning: will return UNCONFIRMED USERS
return $DB->get_records_sql("SELECT u.id, username, email, city, country, lastaccess, confirmed, mnethostid, suspended $selects
return $DB->get_records_sql("SELECT u.id $selects
FROM {user} u
$joins
WHERE $select
Expand Down

0 comments on commit 5e37074

Please sign in to comment.