From c4ba598a0bdd9f4cff86dfa67e57ebeea7c16a7d Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Wed, 7 Sep 2022 20:40:53 +0800 Subject: [PATCH] MDL-75283 core: Fetch proper column mapping for sort 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. --- lib/datalib.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/datalib.php b/lib/datalib.php index 30c53983563..74028155197 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -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