From beb48617bb98c7e1928e1a2c64d85a58e54acb11 Mon Sep 17 00:00:00 2001 From: Eli Date: Sat, 30 Nov 2024 22:20:39 +0200 Subject: [PATCH] MDL-78729 lib: Support dynamic names in grade library and queries Improved the grade library to handle dynamic names for non-English languages. Added a function for name handling and updated query conditions to use it. --- grade/lib.php | 5 +++-- grade/report/grader/lib.php | 5 +++-- grade/report/lib.php | 5 +++-- lib/classes/user.php | 38 +++++++++++++++++++++++++++++++++++++ lib/tablelib.php | 6 +++--- 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/grade/lib.php b/grade/lib.php index 6cd05f3c797e5..bad777f9203bc 100644 --- a/grade/lib.php +++ b/grade/lib.php @@ -2687,11 +2687,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 969c0532df5b6..3f0e409e31d2b 100644 --- a/grade/report/grader/lib.php +++ b/grade/report/grader/lib.php @@ -352,6 +352,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'; @@ -374,10 +375,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 7e952ee83ffae..792931959c43c 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 a125173e170ad..eb5780878a8c8 100644 --- a/lib/classes/user.php +++ b/lib/classes/user.php @@ -1489,4 +1489,42 @@ public static function get_initials(stdClass $user): string { return $initials; } + /** + * 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]; + } + } diff --git a/lib/tablelib.php b/lib/tablelib.php index 0ed7f137b98b1..e578a496585a7 100644 --- a/lib/tablelib.php +++ b/lib/tablelib.php @@ -731,13 +731,13 @@ function get_sql_where() { if ($this->contains_fullname_columns()) { 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'].'%'; } }