diff --git a/grade/lib.php b/grade/lib.php index e241c7ae9879c..007921ec0858b 100644 --- a/grade/lib.php +++ b/grade/lib.php @@ -2489,11 +2489,12 @@ public function get_cell_action_menu(array $element, string $mode, grade_plugin_ } if (($element['type'] == 'userfield') && ($element['name'] == 'fullname')) { - $sortlink->param('sortitemid', 'firstname'); + $usernamefields = core_user::get_user_full_name_fields(); + $sortlink->param('sortitemid', reset($usernamefields)); $context->ascendingfirstnameurl = $this->get_sorting_link($sortlink, $gpr); $context->descendingfirstnameurl = $this->get_sorting_link($sortlink, $gpr, 'desc'); - $sortlink->param('sortitemid', 'lastname'); + $sortlink->param('sortitemid', end($usernamefields)); $context->ascendinglastnameurl = $this->get_sorting_link($sortlink, $gpr); $context->descendinglastnameurl = $this->get_sorting_link($sortlink, $gpr, 'desc'); } else { diff --git a/grade/report/grader/lib.php b/grade/report/grader/lib.php index c0b1a5b7bd57f..5738bee56ca5e 100644 --- a/grade/report/grader/lib.php +++ b/grade/report/grader/lib.php @@ -356,6 +356,7 @@ private function setup_sortitemid(string $sort = '') { $SESSION->gradeuserreport = new stdClass(); } + $namefields = core_user::get_user_full_name_fields(); if ($this->sortitemid) { if (!isset($SESSION->gradeuserreport->sort)) { $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC'; @@ -378,10 +379,10 @@ private function setup_sortitemid(string $sort = '') { } else { // not requesting sort, use last setting (for paging) - if (isset($SESSION->gradeuserreport->sortitemid)) { + if (isset($SESSION->gradeuserreport->sortitemid) && in_array($SESSION->gradeuserreport->sortitemid, $namefields)) { $this->sortitemid = $SESSION->gradeuserreport->sortitemid; } else { - $this->sortitemid = 'lastname'; + $this->sortitemid = end($namefields); } if (isset($SESSION->gradeuserreport->sort)) { diff --git a/grade/report/lib.php b/grade/report/lib.php index a49effe5746dd..99e805d7ab184 100644 --- a/grade/report/lib.php +++ b/grade/report/lib.php @@ -466,13 +466,14 @@ public function setup_users() { $filtersurnamekey = "filtersurname-{$this->context->id}"; $this->userwheresql = ""; + $usernamefields = core_user::get_user_full_name_fields(); $this->userwheresql_params = array(); if (!empty($SESSION->gradereport[$filterfirstnamekey])) { - $this->userwheresql .= ' AND '.$DB->sql_like('u.firstname', ':firstname', false, false); + $this->userwheresql .= ' AND '.$DB->sql_like('u.' . reset($usernamefields), ':firstname', false, false); $this->userwheresql_params['firstname'] = $SESSION->gradereport[$filterfirstnamekey] . '%'; } if (!empty($SESSION->gradereport[$filtersurnamekey])) { - $this->userwheresql .= ' AND '.$DB->sql_like('u.lastname', ':lastname', false, false); + $this->userwheresql .= ' AND '.$DB->sql_like('u.' . end($usernamefields), ':lastname', false, false); $this->userwheresql_params['lastname'] = $SESSION->gradereport[$filtersurnamekey] . '%'; } diff --git a/lib/classes/user.php b/lib/classes/user.php index 06fa5f4c15819..0521694c3148e 100644 --- a/lib/classes/user.php +++ b/lib/classes/user.php @@ -1716,6 +1716,42 @@ public static function get_name_placeholders(stdClass $user): array { } return $namefields; } + + /** + * Retrieves the fields used for displaying a user's full name in the system. + * + * This function extracts the placeholder fields from the configured full name display format and + * returns them as an array. + * + * @param array $options optional array for override. + * @return array List of field names used in the full name display format. + */ + public static function get_user_full_name_fields(array $options = []): array { + global $CFG; + $override = $options["override"] ?? false; + // Get all of the name fields. + $template = null; + // If the fullnamedisplay setting is available, set the template to that. + if (isset($CFG->fullnamedisplay)) { + $template = $CFG->fullnamedisplay; + } + // If the template is empty, or set to language, return the language string. + if ((empty($template) || $template == 'language') && !$override) { + $fullnamedisplay = get_string('fullnamedisplay'); + } + // Check to see if we are displaying according to the alternative full name format. + if ($override) { + if (empty($CFG->alternativefullnameformat) || $CFG->alternativefullnameformat == 'language') { + // Default to show just the user names according to the fullnamedisplay string. + $fullnamedisplay = get_string('fullnamedisplay'); + } else { + // If the override is true, then change the template to use the complete name. + $fullnamedisplay = $CFG->alternativefullnameformat; + } + } + preg_match_all('/{\$a->(.*?)}/', $fullnamedisplay, $matches); + return $matches[1]; + } } // Alias this class to the old name. diff --git a/lib/table/classes/flexible_table.php b/lib/table/classes/flexible_table.php index 9d52cad337374..71725cdf95ea4 100644 --- a/lib/table/classes/flexible_table.php +++ b/lib/table/classes/flexible_table.php @@ -20,6 +20,7 @@ use core_table\local\filter\filterset; use core\exception\coding_exception; use core\output\renderable; +use core_user; use html_writer; use moodle_url; use paging_bar; @@ -688,12 +689,13 @@ public function get_sql_where() { static $i = 0; $i++; + $usernamefields = core_user::get_user_full_name_fields(); if (!empty($this->prefs['i_first'])) { - $conditions[] = $DB->sql_like('firstname', ':ifirstc' . $i, false, false); + $conditions[] = $DB->sql_like(reset($usernamefields), ':ifirstc'.$i, false, false); $params['ifirstc' . $i] = $this->prefs['i_first'] . '%'; } if (!empty($this->prefs['i_last'])) { - $conditions[] = $DB->sql_like('lastname', ':ilastc' . $i, false, false); + $conditions[] = $DB->sql_like(end($usernamefields), ':ilastc'.$i, false, false); $params['ilastc' . $i] = $this->prefs['i_last'] . '%'; } }