mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 00:16:46 +02:00
MDL-82132 user: re-factor code for generating dummy user fullname.
This commit is contained in:
parent
8f0c7bb53c
commit
d6b81786ee
5 changed files with 56 additions and 10 deletions
7
.upgradenotes/MDL-82132-2025041514493372.yml
Normal file
7
.upgradenotes/MDL-82132-2025041514493372.yml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
issueNumber: MDL-82132
|
||||||
|
notes:
|
||||||
|
core_user:
|
||||||
|
- message: >-
|
||||||
|
New method `\core_user::get_dummy_fullname(...)` for returning dummy
|
||||||
|
user fullname comprised of configured name fields only
|
||||||
|
type: improved
|
|
@ -1518,6 +1518,22 @@ class user {
|
||||||
return $displayname;
|
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.
|
* Return profile url depending on context.
|
||||||
*
|
*
|
||||||
|
@ -1588,10 +1604,10 @@ class user {
|
||||||
public static function get_initials(stdClass $user): string {
|
public static function get_initials(stdClass $user): string {
|
||||||
// Get the available name fields.
|
// Get the available name fields.
|
||||||
$namefields = \core_user\fields::get_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.
|
// 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.
|
// Fetch all the available username fields.
|
||||||
$availablefields = order_in_string($namefields, $nameformat);
|
$availablefields = order_in_string($namefields, $nameformat);
|
||||||
// We only want the first and last name fields.
|
// We only want the first and last name fields.
|
||||||
|
|
|
@ -876,6 +876,32 @@ final class user_test extends \advanced_testcase {
|
||||||
$this->assertEquals('John Doe', \core_user::get_fullname($user, $context, $options));
|
$this->assertEquals('John Doe', \core_user::get_fullname($user, $context, $options));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test retrieving dummy user fullname
|
||||||
|
*
|
||||||
|
* @covers \core_user::get_dummy_fullname
|
||||||
|
*/
|
||||||
|
public function test_get_dummy_fullname(): void {
|
||||||
|
$context = \context_system::instance();
|
||||||
|
|
||||||
|
// Show real name as the force names config are not set.
|
||||||
|
$this->assertEquals('firstname lastname', \core_user::get_dummy_fullname($context));
|
||||||
|
|
||||||
|
// With override, still show real name.
|
||||||
|
$options = ['override' => true];
|
||||||
|
$this->assertEquals('firstname lastname', \core_user::get_dummy_fullname($context, $options));
|
||||||
|
|
||||||
|
// Set the alternative names config.
|
||||||
|
set_config('alternativefullnameformat', 'alternatename lastname firstname');
|
||||||
|
|
||||||
|
// Show default name format.
|
||||||
|
$this->assertEquals('firstname lastname', \core_user::get_dummy_fullname($context));
|
||||||
|
|
||||||
|
// With override, show alternative name format.
|
||||||
|
$options = ['override' => true];
|
||||||
|
$this->assertEquals('alternatename lastname firstname', \core_user::get_dummy_fullname($context, $options));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for function to get user details.
|
* Test for function to get user details.
|
||||||
*
|
*
|
||||||
|
|
|
@ -24,6 +24,7 @@ use context_user;
|
||||||
use core\context;
|
use core\context;
|
||||||
use core_component;
|
use core_component;
|
||||||
use core_date;
|
use core_date;
|
||||||
|
use core_user;
|
||||||
use html_writer;
|
use html_writer;
|
||||||
use lang_string;
|
use lang_string;
|
||||||
use moodle_url;
|
use moodle_url;
|
||||||
|
@ -397,10 +398,8 @@ class user extends base {
|
||||||
|
|
||||||
$namefields = fields::get_name_fields(true);
|
$namefields = fields::get_name_fields(true);
|
||||||
|
|
||||||
// Create a dummy user object containing all name fields.
|
|
||||||
$dummyuser = (object) array_combine($namefields, $namefields);
|
|
||||||
$viewfullnames = has_capability('moodle/site:viewfullnames', context_system::instance());
|
$viewfullnames = has_capability('moodle/site:viewfullnames', context_system::instance());
|
||||||
$dummyfullname = fullname($dummyuser, $viewfullnames);
|
$dummyfullname = core_user::get_dummy_fullname(null, ['override' => $viewfullnames]);
|
||||||
|
|
||||||
// Extract any name fields from the fullname format in the order that they appear.
|
// Extract any name fields from the fullname format in the order that they appear.
|
||||||
$matchednames = array_values(order_in_string($namefields, $dummyfullname));
|
$matchednames = array_values(order_in_string($namefields, $dummyfullname));
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
namespace core_user;
|
namespace core_user;
|
||||||
|
|
||||||
use core_text;
|
use core_text;
|
||||||
|
use core_user;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for retrieving information about user fields that are needed for displaying user identity.
|
* Class for retrieving information about user fields that are needed for displaying user identity.
|
||||||
|
@ -585,10 +586,7 @@ class fields {
|
||||||
$unique = self::$uniqueidentifier++;
|
$unique = self::$uniqueidentifier++;
|
||||||
|
|
||||||
$namefields = self::get_name_fields();
|
$namefields = self::get_name_fields();
|
||||||
|
$dummyfullname = core_user::get_dummy_fullname(null, ['override' => $override]);
|
||||||
// Create a dummy user object containing all name fields.
|
|
||||||
$dummyuser = (object) array_combine($namefields, $namefields);
|
|
||||||
$dummyfullname = fullname($dummyuser, $override);
|
|
||||||
|
|
||||||
// Extract any name fields from the fullname format in the order that they appear.
|
// Extract any name fields from the fullname format in the order that they appear.
|
||||||
$matchednames = array_values(order_in_string($namefields, $dummyfullname));
|
$matchednames = array_values(order_in_string($namefields, $dummyfullname));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue