diff --git a/grade/report/grader/index.php b/grade/report/grader/index.php index 785e2efbe21..5146f6b6301 100644 --- a/grade/report/grader/index.php +++ b/grade/report/grader/index.php @@ -32,7 +32,7 @@ $courseid = required_param('id', PARAM_INT); // course id $page = optional_param('page', 0, PARAM_INT); // active page $edit = optional_param('edit', -1, PARAM_BOOL); // sticky editting mode -$sortitemid = optional_param('sortitemid', 0, PARAM_ALPHANUM); // sort by which grade item +$sortitemid = optional_param('sortitemid', 0, PARAM_ALPHANUMEXT); $action = optional_param('action', 0, PARAM_ALPHAEXT); $move = optional_param('move', 0, PARAM_INT); $type = optional_param('type', 0, PARAM_ALPHA); diff --git a/grade/report/grader/lib.php b/grade/report/grader/lib.php index c7ee6659a0c..9f0217a5625 100644 --- a/grade/report/grader/lib.php +++ b/grade/report/grader/lib.php @@ -442,20 +442,17 @@ class grade_report_grader extends grade_report { $sort = "g.finalgrade $this->sortorder, u.idnumber, u.lastname, u.firstname, u.email"; } else { $sortjoin = ''; - switch($this->sortitemid) { - case 'lastname': - $sort = "u.lastname $this->sortorder, u.firstname $this->sortorder, u.idnumber, u.email"; - break; - case 'firstname': - $sort = "u.firstname $this->sortorder, u.lastname $this->sortorder, u.idnumber, u.email"; - break; - case 'email': - $sort = "u.email $this->sortorder, u.firstname, u.lastname, u.idnumber"; - break; - case 'idnumber': - default: - $sort = "u.idnumber $this->sortorder, u.firstname, u.lastname, u.email"; - break; + + // The default sort will be that provided by the site for users, unless a valid user field is requested, + // the value of which takes precedence. + [$sort] = users_order_by_sql('u', null, $this->context, $userfieldssql->mappings); + if (array_key_exists($this->sortitemid, $userfieldssql->mappings)) { + + // Ensure user sort field doesn't duplicate one of the default sort fields. + $usersortfield = $userfieldssql->mappings[$this->sortitemid]; + $defaultsortfields = array_diff(explode(', ', $sort), [$usersortfield]); + + $sort = "{$usersortfield} {$this->sortorder}, " . implode(', ', $defaultsortfields); } $params = array_merge($gradebookrolesparams, $this->userwheresql_params, $this->groupwheresql_params, $enrolledparams, $relatedctxparams); diff --git a/grade/report/grader/tests/behat/ajax_grader.feature b/grade/report/grader/tests/behat/ajax_grader.feature index 7ca442a864c..de8115dab72 100644 --- a/grade/report/grader/tests/behat/ajax_grader.feature +++ b/grade/report/grader/tests/behat/ajax_grader.feature @@ -264,3 +264,9 @@ Feature: Using the AJAX grading feature of Grader report to update grades and fe When I navigate to "View > Grader report" in the course gradebook Then I should see "litle yellow frog" in the "student1" "table_row" And I should see "prince frog" in the "student2" "table_row" + # Sort by the custom profile field. + And I click on "Favourite frog" "link" in the "gradereport-grader-table" "table" + And "student1" "table_row" should appear before "student2" "table_row" + # Now sort by descending. + And I click on "Favourite frog" "link" in the "gradereport-grader-table" "table" + And "student2" "table_row" should appear before "student1" "table_row"