MDL-82132 user: re-factor code for generating dummy user fullname.

This commit is contained in:
Paul Holden
2025-04-15 16:00:52 +01:00
parent f22eed6b67
commit 3df91d98f4
5 changed files with 54 additions and 10 deletions
+19 -3
View File
@@ -1401,6 +1401,22 @@ class core_user {
return $displayname;
}
/**
* Return fullname of a dummy user comprised of configured name fields only
*
* @param context|null $context
* @param array $options
* @return string
*/
public static function get_dummy_fullname(?context $context = null, array $options = []): string {
// Create a dummy user object containing all name fields.
$namefields = \core_user\fields::get_name_fields();
$user = (object) array_combine($namefields, $namefields);
return static::get_fullname($user, $context, $options);
}
/**
* Return profile url depending on context.
*
@@ -1471,10 +1487,10 @@ class core_user {
public static function get_initials(stdClass $user): string {
// Get the available name fields.
$namefields = \core_user\fields::get_name_fields();
// Build a dummy user to determine the name format.
$dummyuser = array_combine($namefields, $namefields);
// Determine the name format by using fullname() and passing the dummy user.
$nameformat = fullname((object) $dummyuser);
$nameformat = static::get_dummy_fullname();
// Fetch all the available username fields.
$availablefields = order_in_string($namefields, $nameformat);
// We only want the first and last name fields.