Skip to content

Commit

Permalink
MDL-37917 admin: Allow to sort lists by firstname or lastname
Browse files Browse the repository at this point in the history
  • Loading branch information
danielneis committed Dec 8, 2023
1 parent a891866 commit 9e6969c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 10 deletions.
6 changes: 6 additions & 0 deletions admin/settings/users.php
Expand Up @@ -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);

Expand Down
34 changes: 29 additions & 5 deletions grade/lib.php
Expand Up @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions grade/report/grader/lib.php
Expand Up @@ -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();
Expand Down Expand Up @@ -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)) {
Expand Down
3 changes: 3 additions & 0 deletions lang/en/moodle.php
Expand Up @@ -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';
Expand Down
5 changes: 5 additions & 0 deletions lib/classes/user.php
Expand Up @@ -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;

Expand Down
10 changes: 8 additions & 2 deletions lib/datalib.php
Expand Up @@ -362,15 +362,21 @@ 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 . '.';
} else {
$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) {
Expand Down
37 changes: 37 additions & 0 deletions lib/tests/datalib_test.php
Expand Up @@ -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);
Expand Down

0 comments on commit 9e6969c

Please sign in to comment.