Merge branch 'wip-MDL-46921-master' of git://github.com/abgreeve/moodle

This commit is contained in:
Damyon Wiese 2014-10-09 09:57:58 +08:00
commit e6b00f032d
9 changed files with 145 additions and 44 deletions

View file

@ -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) {

View file

@ -448,7 +448,8 @@ class flexible_table {
if (($sortcol = optional_param($this->request[TABLE_VAR_SORT], '', PARAM_ALPHANUMEXT)) &&
$this->is_sortable($sortcol) && empty($this->sess->collapse[$sortcol]) &&
(isset($this->columns[$sortcol]) || in_array($sortcol, array('firstname', 'lastname')) && isset($this->columns['fullname']))) {
(isset($this->columns[$sortcol]) || in_array($sortcol, get_all_user_name_fields())
&& isset($this->columns['fullname']))) {
if (array_key_exists($sortcol, $this->sess->sortby)) {
// This key already exists somewhere. Change its sortorder and bring it to the top.
@ -571,7 +572,7 @@ class flexible_table {
if (isset($this->columns[$column])) {
continue; // This column is OK.
}
if (in_array($column, array('firstname', 'lastname')) &&
if (in_array($column, get_all_user_name_fields()) &&
isset($this->columns['fullname'])) {
continue; // This column is OK.
}
@ -1186,7 +1187,7 @@ class flexible_table {
if ($nameformat == 'language') {
$nameformat = get_string('fullnamedisplay');
}
$requirednames = order_in_string(array('firstname', 'lastname'), $nameformat);
$requirednames = order_in_string(get_all_user_name_fields(), $nameformat);
if (!empty($requirednames)) {
if ($this->is_sortable($column)) {

View file

@ -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() {