diff --git a/admin/settings/users.php b/admin/settings/users.php index 60ddda4590887..b1786b184976d 100644 --- a/admin/settings/users.php +++ b/admin/settings/users.php @@ -96,6 +96,12 @@ new lang_string('visibilitypref', 'core_contentbank'), new lang_string('visibilitypref_help', 'core_contentbank'), \core_contentbank\content::VISIBILITY_PUBLIC, $choices)); + + $choices = []; + $choices[\core_user::SORTUSERS_FIRSTNAME] = new lang_string('sortuserbyfirstname'); + $choices[\core_user::SORTUSERS_LASTNAME] = new lang_string('sortuserbylastname'); + $temp->add(new admin_setting_configselect('defaultpreference_sortuser', new lang_string('sortuser'), + '', 0, $choices)); } $ADMIN->add('accounts', $temp); diff --git a/grade/lib.php b/grade/lib.php index 02744dee562e9..ba665e1374b88 100644 --- a/grade/lib.php +++ b/grade/lib.php @@ -114,15 +114,39 @@ class graded_users_iterator { * @param string $sortfield2 The second field of the users table by which the array of users will be sorted * @param string $sortorder2 The order in which the second sorting field will be sorted (ASC or DESC) */ - public function __construct($course, $grade_items=null, $groupid=0, - $sortfield1='lastname', $sortorder1='ASC', - $sortfield2='firstname', $sortorder2='ASC') { + public function __construct( + $course, + $grade_items = null, + $groupid = 0, + $sortfield1 = '', + $sortorder1 = 'ASC', + $sortfield2 = '', + $sortorder2 = 'ASC', + ) { + global $CFG; + $this->course = $course; $this->grade_items = $grade_items; $this->groupid = $groupid; - $this->sortfield1 = $sortfield1; + if (empty($sortfield1)) { + if ($CFG->defaultpreference_sortuser == \core_user::SORTUSERS_FIRSTNAME) { + $this->sortfield1 = 'firstname'; + } else { + $this->sortfield1 = 'lastname'; + } + } else { + $this->sortfield1 = $sortfield1; + } $this->sortorder1 = $sortorder1; - $this->sortfield2 = $sortfield2; + if (empty($sortfield2)) { + if ($CFG->defaultpreference_sortuser == \core_user::SORTUSERS_FIRSTNAME) { + $this->sortfield2 = 'lastname'; + } else { + $this->sortfield2 = 'firstname'; + } + } else { + $this->sortfield2 = $sortfield2; + } $this->sortorder2 = $sortorder2; $this->gradestack = array(); diff --git a/grade/report/grader/lib.php b/grade/report/grader/lib.php index dd00b9abd83dd..d63efc96b07dc 100644 --- a/grade/report/grader/lib.php +++ b/grade/report/grader/lib.php @@ -338,8 +338,7 @@ public function process_data($data) { * @param string $sort sorting direction */ private function setup_sortitemid(string $sort = '') { - - global $SESSION; + global $SESSION, $CFG; if (!isset($SESSION->gradeuserreport)) { $SESSION->gradeuserreport = new stdClass(); @@ -370,7 +369,7 @@ private function setup_sortitemid(string $sort = '') { if (isset($SESSION->gradeuserreport->sortitemid)) { $this->sortitemid = $SESSION->gradeuserreport->sortitemid; } else { - $this->sortitemid = 'lastname'; + $this->sortitemid = $CFG->defaultpreference_sortuser; } if (isset($SESSION->gradeuserreport->sort)) { diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 10985cc5b95b5..498b7c2574aed 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -2114,6 +2114,9 @@ $string['sortbyx'] = 'Sort by {$a} ascending'; $string['sortbyxreverse'] = 'Sort by {$a} descending'; $string['sorting'] = 'Sorting'; +$string['sortuser'] = 'Default user sort order'; +$string['sortuserbyfirstname'] = 'Order by firstname, lastname'; +$string['sortuserbylastname'] = 'Order by lastname, firstname'; $string['sourcerole'] = 'Source role'; $string['specifyname'] = 'You must specify a name.'; $string['standard'] = 'Standard'; diff --git a/lib/classes/user.php b/lib/classes/user.php index 576e9800eeb9b..744b243611a7d 100644 --- a/lib/classes/user.php +++ b/lib/classes/user.php @@ -88,6 +88,11 @@ class core_user { /** @var int Indicates that user profile view should be allowed even if Moodle would prevent it */ const VIEWPROFILE_FORCE_ALLOW = 1; + /** @var int Indicates that lists of users must be sorted by firstname,lastname */ + const SORTUSERS_FIRSTNAME = 1; + /** @var int Indicates that lists of users must be sorted by lastname,firstname */ + const SORTUSERS_LASTNAME = 2; + /** @var stdClass keep record of noreply user */ public static $noreplyuser = false; diff --git a/lib/datalib.php b/lib/datalib.php index 2dbaae9cf9af6..e5d6ebc63ea33 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -362,7 +362,7 @@ function users_search_sql(string $search, string $u = 'u', int $searchtype = USE */ function users_order_by_sql(string $usertablealias = '', string $search = null, context $context = null, array $customfieldmappings = []) { - global $DB, $PAGE; + global $DB, $PAGE, $CFG; if ($usertablealias) { $tableprefix = $usertablealias . '.'; @@ -370,7 +370,13 @@ function users_order_by_sql(string $usertablealias = '', string $search = null, $tableprefix = ''; } - $sort = "{$tableprefix}lastname, {$tableprefix}firstname, {$tableprefix}id"; + if (!isset($CFG->defaultpreference_sortuser) || + ($CFG->defaultpreference_sortuser == \core_user::SORTUSERS_LASTNAME) || + ($CFG->defaultpreference_sortuser != \core_user::SORTUSERS_FIRSTNAME)) { + $sort = "{$tableprefix}lastname, {$tableprefix}firstname, {$tableprefix}id"; + } else { + $sort = "{$tableprefix}firstname, {$tableprefix}lastname, {$tableprefix}id"; + } $params = array(); if (!$search) { diff --git a/lib/tests/datalib_test.php b/lib/tests/datalib_test.php index 134176da19c35..6e0cc7efafe70 100644 --- a/lib/tests/datalib_test.php +++ b/lib/tests/datalib_test.php @@ -166,6 +166,43 @@ public function test_users_order_by_sql_table_prefix() { $this->assertEquals(array(), $params); } + /** + * Tests for user_orders_by_sql with a custom sortuser value. + * + * @dataProvider user_orders_by_sql_sortuser_provider + * @covers ::users_order_by_sql + * @param mixed $sortuser The sortuser value + * @param string $expected The expected SQL + */ + public function test_users_order_by_sql_sortuser_lastname( + mixed $sortuser, + string $expected, + ): void { + global $CFG; + + $this->resetAfterTest(true); + $CFG->defaultpreference_sortuser = $sortuser; + + [$sort, $params] = users_order_by_sql('u'); + $this->assert_same_sql($expected, $sort); + $this->assertEquals([], $params); + } + + /** + * Data provider for test_users_order_by_sql_sortuser_lastname tests. + * + * @return array + */ + public static function user_orders_by_sql_sortuser_provider(): array { + return [ + [\core_user::SORTUSERS_LASTNAME, 'u.lastname, u.firstname, u.id'], + [\core_user::SORTUSERS_FIRSTNAME, 'u.firstname, u.lastname, u.id'], + [false, 'u.lastname, u.firstname, u.id'], + [true, 'u.lastname, u.firstname, u.id'], + ['0', 'u.lastname, u.firstname, u.id'], + ]; + } + public function test_users_order_by_sql_search_no_extra_fields() { global $CFG, $DB; $this->resetAfterTest(true);