Skip to content

Commit

Permalink
MDL-78729 lib: Support dynamic names in grade library and queries
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EliZard12 committed Dec 5, 2024
1 parent b819eca commit beb4861
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
5 changes: 3 additions & 2 deletions grade/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions grade/report/grader/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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)) {
Expand Down
5 changes: 3 additions & 2 deletions grade/report/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] . '%';
}

Expand Down
38 changes: 38 additions & 0 deletions lib/classes/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

}
6 changes: 3 additions & 3 deletions lib/tablelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'].'%';
}
}
Expand Down

0 comments on commit beb4861

Please sign in to comment.