mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-46921 lib: Update get_all_user_name_fields() plus unit tests.
This commit is contained in:
parent
143aacaea0
commit
e622b1a14b
@ -3672,9 +3672,12 @@ function fullname($user, $override=false) {
|
||||
* @param string $tableprefix table query prefix to use in front of each field.
|
||||
* @param string $prefix prefix added to the name fields e.g. authorfirstname.
|
||||
* @param string $fieldprefix sql field prefix e.g. id AS userid.
|
||||
* @param bool $order moves firstname and lastname to the top of the array / start of the string.
|
||||
* @return array|string All name fields.
|
||||
*/
|
||||
function get_all_user_name_fields($returnsql = false, $tableprefix = null, $prefix = null, $fieldprefix = null) {
|
||||
function get_all_user_name_fields($returnsql = false, $tableprefix = null, $prefix = null, $fieldprefix = null, $order = false) {
|
||||
// This array is provided in this order because when called by fullname() (above) if firstname is before
|
||||
// firstnamephonetic str_replace() will change the wrong placeholder.
|
||||
$alternatenames = array('firstnamephonetic' => 'firstnamephonetic',
|
||||
'lastnamephonetic' => 'lastnamephonetic',
|
||||
'middlename' => 'middlename',
|
||||
@ -3689,6 +3692,19 @@ function get_all_user_name_fields($returnsql = false, $tableprefix = null, $pref
|
||||
}
|
||||
}
|
||||
|
||||
// If we want the end result to have firstname and lastname at the front / top of the result.
|
||||
if ($order) {
|
||||
// Move the last two elements (firstname, lastname) off the array and put them at the top.
|
||||
for ($i = 0; $i < 2; $i++) {
|
||||
// Get the last element.
|
||||
$lastelement = end($alternatenames);
|
||||
// Remove it from the array.
|
||||
unset($alternatenames[$lastelement]);
|
||||
// Put the element back on the top of the array.
|
||||
$alternatenames = array_merge(array($lastelement => $lastelement), $alternatenames);
|
||||
}
|
||||
}
|
||||
|
||||
// Create an sql field snippet if requested.
|
||||
if ($returnsql) {
|
||||
if ($tableprefix) {
|
||||
|
@ -2486,6 +2486,21 @@ class core_moodlelib_testcase extends advanced_testcase {
|
||||
// Additional name fields with an alias and a title - string.
|
||||
$teststring = 'u.firstnamephonetic AS authorfirstnamephonetic,u.lastnamephonetic AS authorlastnamephonetic,u.middlename AS authormiddlename,u.alternatename AS authoralternatename,u.firstname AS authorfirstname,u.lastname AS authorlastname';
|
||||
$this->assertEquals($teststring, get_all_user_name_fields(true, 'u', null, 'author'));
|
||||
|
||||
// Test the order parameter of the function.
|
||||
// Returning an array.
|
||||
$testarray = array('firstname' => 'firstname',
|
||||
'lastname' => 'lastname',
|
||||
'firstnamephonetic' => 'firstnamephonetic',
|
||||
'lastnamephonetic' => 'lastnamephonetic',
|
||||
'middlename' => 'middlename',
|
||||
'alternatename' => 'alternatename'
|
||||
);
|
||||
$this->assertEquals($testarray, get_all_user_name_fields(false, null, null, null, true));
|
||||
|
||||
// Returning a string.
|
||||
$teststring = 'firstname,lastname,firstnamephonetic,lastnamephonetic,middlename,alternatename';
|
||||
$this->assertEquals($teststring, get_all_user_name_fields(true, null, null, null, true));
|
||||
}
|
||||
|
||||
public function test_order_in_string() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user