Skip to content

Commit d53bc02

Browse files
committed
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.
1 parent 9a528fb commit d53bc02

5 files changed

Lines changed: 50 additions & 9 deletions

File tree

grade/lib.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,11 +2687,12 @@ public function get_cell_action_menu(array $element, string $mode, grade_plugin_
26872687
}
26882688

26892689
if (($element['type'] == 'userfield') && ($element['name'] == 'fullname')) {
2690-
$sortlink->param('sortitemid', 'firstname');
2690+
$usernamefields = core_user::get_user_full_name_fields();
2691+
$sortlink->param('sortitemid', reset($usernamefields));
26912692
$context->ascendingfirstnameurl = $this->get_sorting_link($sortlink, $gpr);
26922693
$context->descendingfirstnameurl = $this->get_sorting_link($sortlink, $gpr, 'desc');
26932694

2694-
$sortlink->param('sortitemid', 'lastname');
2695+
$sortlink->param('sortitemid', end($usernamefields));
26952696
$context->ascendinglastnameurl = $this->get_sorting_link($sortlink, $gpr);
26962697
$context->descendinglastnameurl = $this->get_sorting_link($sortlink, $gpr, 'desc');
26972698
} else {

grade/report/grader/lib.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ private function setup_sortitemid(string $sort = '') {
352352
$SESSION->gradeuserreport = new stdClass();
353353
}
354354

355+
$namefields = core_user::get_user_full_name_fields();
355356
if ($this->sortitemid) {
356357
if (!isset($SESSION->gradeuserreport->sort)) {
357358
$this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
@@ -374,10 +375,10 @@ private function setup_sortitemid(string $sort = '') {
374375
} else {
375376
// not requesting sort, use last setting (for paging)
376377

377-
if (isset($SESSION->gradeuserreport->sortitemid)) {
378+
if (isset($SESSION->gradeuserreport->sortitemid) && in_array($SESSION->gradeuserreport->sortitemid, $namefields)) {
378379
$this->sortitemid = $SESSION->gradeuserreport->sortitemid;
379380
} else {
380-
$this->sortitemid = 'lastname';
381+
$this->sortitemid = end($namefields);
381382
}
382383

383384
if (isset($SESSION->gradeuserreport->sort)) {

grade/report/lib.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,13 +466,14 @@ public function setup_users() {
466466
$filtersurnamekey = "filtersurname-{$this->context->id}";
467467

468468
$this->userwheresql = "";
469+
$usernamefields = core_user::get_user_full_name_fields();
469470
$this->userwheresql_params = array();
470471
if (!empty($SESSION->gradereport[$filterfirstnamekey])) {
471-
$this->userwheresql .= ' AND '.$DB->sql_like('u.firstname', ':firstname', false, false);
472+
$this->userwheresql .= ' AND '.$DB->sql_like('u.' . reset($usernamefields), ':firstname', false, false);
472473
$this->userwheresql_params['firstname'] = $SESSION->gradereport[$filterfirstnamekey] . '%';
473474
}
474475
if (!empty($SESSION->gradereport[$filtersurnamekey])) {
475-
$this->userwheresql .= ' AND '.$DB->sql_like('u.lastname', ':lastname', false, false);
476+
$this->userwheresql .= ' AND '.$DB->sql_like('u.' . end($usernamefields), ':lastname', false, false);
476477
$this->userwheresql_params['lastname'] = $SESSION->gradereport[$filtersurnamekey] . '%';
477478
}
478479

lib/classes/user.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,4 +1489,42 @@ public static function get_initials(stdClass $user): string {
14891489
return $initials;
14901490
}
14911491

1492+
/**
1493+
* Retrieves the fields used for displaying a user's full name in the system.
1494+
*
1495+
* This function extracts the placeholder fields from the configured full name display format and
1496+
* returns them as an array.
1497+
*
1498+
* @param array $options optional array for override.
1499+
* @return array List of field names used in the full name display format.
1500+
*/
1501+
public static function get_user_full_name_fields(array $options = []): array {
1502+
global $CFG;
1503+
$override = $options["override"] ?? false;
1504+
1505+
// Get all of the name fields.
1506+
$template = null;
1507+
// If the fullnamedisplay setting is available, set the template to that.
1508+
if (isset($CFG->fullnamedisplay)) {
1509+
$template = $CFG->fullnamedisplay;
1510+
}
1511+
// If the template is empty, or set to language, return the language string.
1512+
if ((empty($template) || $template == 'language') && !$override) {
1513+
$fullnamedisplay = get_string('fullnamedisplay');
1514+
}
1515+
1516+
// Check to see if we are displaying according to the alternative full name format.
1517+
if ($override) {
1518+
if (empty($CFG->alternativefullnameformat) || $CFG->alternativefullnameformat == 'language') {
1519+
// Default to show just the user names according to the fullnamedisplay string.
1520+
$fullnamedisplay = get_string('fullnamedisplay');
1521+
} else {
1522+
// If the override is true, then change the template to use the complete name.
1523+
$fullnamedisplay = $CFG->alternativefullnameformat;
1524+
}
1525+
}
1526+
preg_match_all('/{\$a->(.*?)}/', $fullnamedisplay, $matches);
1527+
return $matches[1];
1528+
}
1529+
14921530
}

lib/tablelib.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,13 @@ function get_sql_where() {
731731
if ($this->contains_fullname_columns()) {
732732
static $i = 0;
733733
$i++;
734-
734+
$usernamefields = core_user::get_user_full_name_fields();
735735
if (!empty($this->prefs['i_first'])) {
736-
$conditions[] = $DB->sql_like('firstname', ':ifirstc'.$i, false, false);
736+
$conditions[] = $DB->sql_like(reset($usernamefields), ':ifirstc'.$i, false, false);
737737
$params['ifirstc'.$i] = $this->prefs['i_first'].'%';
738738
}
739739
if (!empty($this->prefs['i_last'])) {
740-
$conditions[] = $DB->sql_like('lastname', ':ilastc'.$i, false, false);
740+
$conditions[] = $DB->sql_like(end($usernamefields), ':ilastc'.$i, false, false);
741741
$params['ilastc'.$i] = $this->prefs['i_last'].'%';
742742
}
743743
}

0 commit comments

Comments
 (0)