diff --git a/badges/classes/reportbuilder/datasource/badges.php b/badges/classes/reportbuilder/datasource/badges.php
index 707f77351cb..379d4cbc646 100644
--- a/badges/classes/reportbuilder/datasource/badges.php
+++ b/badges/classes/reportbuilder/datasource/badges.php
@@ -122,4 +122,17 @@ class badges extends datasource {
'badge:name',
];
}
+
+ /**
+ * Return the default sorting that will be added to the report once it is created
+ *
+ * @return array|int[]
+ */
+ public function get_default_column_sorting(): array {
+ return [
+ 'badge:name' => SORT_ASC,
+ 'user:fullname' => SORT_ASC,
+ 'badge_issued:issued' => SORT_ASC,
+ ];
+ }
}
diff --git a/cohort/classes/reportbuilder/datasource/cohorts.php b/cohort/classes/reportbuilder/datasource/cohorts.php
index 6e49015cd48..de0f11c4200 100644
--- a/cohort/classes/reportbuilder/datasource/cohorts.php
+++ b/cohort/classes/reportbuilder/datasource/cohorts.php
@@ -81,8 +81,8 @@ class cohorts extends datasource {
*/
public function get_default_columns(): array {
return [
- 'cohort:context',
'cohort:name',
+ 'cohort:context',
'cohort:idnumber',
'cohort:description',
];
@@ -105,4 +105,15 @@ class cohorts extends datasource {
public function get_default_conditions(): array {
return [];
}
+
+ /**
+ * Return the default sorting that will be added to the report once it is created
+ *
+ * @return array|int[]
+ */
+ public function get_default_column_sorting(): array {
+ return [
+ 'cohort:name' => SORT_ASC,
+ ];
+ }
}
diff --git a/cohort/tests/reportbuilder/datasource/cohorts_test.php b/cohort/tests/reportbuilder/datasource/cohorts_test.php
index e0a92720a12..ce5765d50aa 100644
--- a/cohort/tests/reportbuilder/datasource/cohorts_test.php
+++ b/cohort/tests/reportbuilder/datasource/cohorts_test.php
@@ -67,8 +67,8 @@ class cohorts_test extends core_reportbuilder_testcase {
$contentrow = array_values(reset($content));
$this->assertEquals([
- 'System', // Context.
'Legends', // Name.
+ 'System', // Context.
'C101', // ID number.
'
Cohort for the legends
', // Description.
'Lionel Richards', // User.
diff --git a/course/classes/reportbuilder/datasource/courses.php b/course/classes/reportbuilder/datasource/courses.php
index 7a8e169577b..881f546c6c4 100644
--- a/course/classes/reportbuilder/datasource/courses.php
+++ b/course/classes/reportbuilder/datasource/courses.php
@@ -111,4 +111,17 @@ class courses extends datasource {
public function get_default_conditions(): array {
return ['course_category:name'];
}
+
+ /**
+ * Return the default sorting that will be added to the report once it is created
+ *
+ * @return array|int[]
+ */
+ public function get_default_column_sorting(): array {
+ return [
+ 'course_category:name' => SORT_ASC,
+ 'course:shortname' => SORT_ASC,
+ 'course:fullname' => SORT_ASC,
+ ];
+ }
}
diff --git a/reportbuilder/classes/datasource.php b/reportbuilder/classes/datasource.php
index bb8cfc51fc8..d726f9985b1 100644
--- a/reportbuilder/classes/datasource.php
+++ b/reportbuilder/classes/datasource.php
@@ -83,9 +83,27 @@ abstract class datasource extends base {
*/
public function add_default_columns(): void {
$reportid = $this->get_report_persistent()->get('id');
+
+ // Retrieve default column sorting, and track index of both sorted/non-sorted columns.
$columnidentifiers = $this->get_default_columns();
+ $defaultcolumnsorting = array_intersect_key($this->get_default_column_sorting(),
+ array_fill_keys($columnidentifiers, 1));
+ $columnnonsortingindex = count($defaultcolumnsorting) + 1;
+
foreach ($columnidentifiers as $uniqueidentifier) {
- report::add_report_column($reportid, $uniqueidentifier);
+ $column = report::add_report_column($reportid, $uniqueidentifier);
+
+ // After adding the column, toggle sorting according to defaults provided by the datasource.
+ $sortorder = array_search($uniqueidentifier, array_keys($defaultcolumnsorting));
+ if ($sortorder !== false) {
+ $column->set_many([
+ 'sortenabled' => true,
+ 'sortdirection' => $defaultcolumnsorting[$uniqueidentifier],
+ 'sortorder' => $sortorder + 1,
+ ])->update();
+ } else if (!empty($defaultcolumnsorting)) {
+ $column->set('sortorder', $columnnonsortingindex++)->update();
+ }
}
}
@@ -96,6 +114,15 @@ abstract class datasource extends base {
*/
abstract public function get_default_columns(): array;
+ /**
+ * Return the default sorting that will be added to the report once it is created
+ *
+ * @return int[] array [column identifier => SORT_ASC/SORT_DESC]
+ */
+ public function get_default_column_sorting(): array {
+ return [];
+ }
+
/**
* Return all configured report columns
*
diff --git a/reportbuilder/tests/behat/customreports.feature b/reportbuilder/tests/behat/customreports.feature
index fd5d83f60a5..0be0833214f 100644
--- a/reportbuilder/tests/behat/customreports.feature
+++ b/reportbuilder/tests/behat/customreports.feature
@@ -23,6 +23,11 @@ Feature: Manage custom reports
And I should see "Full name" in the "reportbuilder-table" "table"
And I should see "Username" in the "reportbuilder-table" "table"
And I should see "Email address" in the "reportbuilder-table" "table"
+ # Confirm we see the default sorting in the report
+ And "Admin User" "table_row" should appear before "User 2" "table_row"
+ And I click on "Show/hide 'Sorting'" "button"
+ And "Disable sorting for column 'Full name'" "checkbox" should exist in the "#settingssorting" "css_element"
+ And I click on "Show/hide 'Sorting'" "button"
# Confirm we only see not suspended users in the report.
And I should see "Admin User" in the "reportbuilder-table" "table"
And I should see "User 2" in the "reportbuilder-table" "table"
diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt
index e9e3c8ce688..35c38c6fb7f 100644
--- a/reportbuilder/upgrade.txt
+++ b/reportbuilder/upgrade.txt
@@ -56,3 +56,5 @@ Information provided here is intended especially for developers.
- `datasource_stress_test_columns_aggregation`
- `datasource_stress_test_conditions`
* The test helper method `get_custom_report_content()` now accepts a list of filter values and applies them to the report
+* New method `get_default_column_sorting` in base datasource class, to be overridden by sources that wish to
+ define default columns sort order upon report creation.
diff --git a/user/classes/reportbuilder/datasource/users.php b/user/classes/reportbuilder/datasource/users.php
index 5e1aeb5daa4..e8784fe0dd3 100644
--- a/user/classes/reportbuilder/datasource/users.php
+++ b/user/classes/reportbuilder/datasource/users.php
@@ -123,17 +123,13 @@ class users extends datasource {
}
/**
- * Set default columns and the sortorder
+ * Return the default sorting that will be added to the report once it is created
+ *
+ * @return array|int[]
*/
- public function add_default_columns(): void {
- parent::add_default_columns();
-
- $persistent = $this->get_report_persistent();
- $report = manager::get_report_from_persistent($persistent);
- foreach ($report->get_active_columns() as $column) {
- if ($column->get_unique_identifier() === 'user:fullname') {
- report::toggle_report_column_sorting($persistent->get('id'), $column->get_persistent()->get('id'), true);
- }
- }
+ public function get_default_column_sorting(): array {
+ return [
+ 'user:fullname' => SORT_ASC,
+ ];
}
}
diff --git a/user/tests/reportbuilder/datasource/users_test.php b/user/tests/reportbuilder/datasource/users_test.php
index b05ff552888..363c7bda5ac 100644
--- a/user/tests/reportbuilder/datasource/users_test.php
+++ b/user/tests/reportbuilder/datasource/users_test.php
@@ -44,24 +44,22 @@ class users_test extends core_reportbuilder_testcase {
public function test_datasource_default(): void {
$this->resetAfterTest();
- $user = $this->getDataGenerator()->create_user(['email' => 'test@example.com']);
+ $user2 = $this->getDataGenerator()->create_user(['firstname' => 'Charles']);
+ $user3 = $this->getDataGenerator()->create_user(['firstname' => 'Brian']);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
- $this->assertCount(2, $content);
+ $this->assertCount(3, $content);
- // Consistent order by email, just in case.
- core_collator::asort_array_of_arrays_by_key($content, 'c2_email');
- $content = array_values($content);
-
- // Default columns are fullname, username, email.
- [$adminrow, $userrow] = array_map('array_values', $content);
+ // Default columns are fullname, username, email. Results are sorted by the fullname.
+ [$adminrow, $userrow1, $userrow2] = array_map('array_values', $content);
$this->assertEquals(['Admin User', 'admin', 'admin@example.com'], $adminrow);
- $this->assertEquals([fullname($user), $user->username, $user->email], $userrow);
+ $this->assertEquals([fullname($user3), $user3->username, $user3->email], $userrow1);
+ $this->assertEquals([fullname($user2), $user2->username, $user2->email], $userrow2);
}
/**