mirror of
https://github.com/moodle/moodle.git
synced 2025-08-06 17:36:38 +02:00
MDL-75525 reportbuilder: specify initial sorting for datasources
This commit is contained in:
parent
6fae54c10e
commit
064eccd4fc
9 changed files with 88 additions and 23 deletions
|
@ -122,4 +122,17 @@ class badges extends datasource {
|
||||||
'badge:name',
|
'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,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,8 +81,8 @@ class cohorts extends datasource {
|
||||||
*/
|
*/
|
||||||
public function get_default_columns(): array {
|
public function get_default_columns(): array {
|
||||||
return [
|
return [
|
||||||
'cohort:context',
|
|
||||||
'cohort:name',
|
'cohort:name',
|
||||||
|
'cohort:context',
|
||||||
'cohort:idnumber',
|
'cohort:idnumber',
|
||||||
'cohort:description',
|
'cohort:description',
|
||||||
];
|
];
|
||||||
|
@ -105,4 +105,15 @@ class cohorts extends datasource {
|
||||||
public function get_default_conditions(): array {
|
public function get_default_conditions(): array {
|
||||||
return [];
|
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,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,8 +67,8 @@ class cohorts_test extends core_reportbuilder_testcase {
|
||||||
|
|
||||||
$contentrow = array_values(reset($content));
|
$contentrow = array_values(reset($content));
|
||||||
$this->assertEquals([
|
$this->assertEquals([
|
||||||
'System', // Context.
|
|
||||||
'Legends', // Name.
|
'Legends', // Name.
|
||||||
|
'System', // Context.
|
||||||
'C101', // ID number.
|
'C101', // ID number.
|
||||||
'<div class="text_to_html">Cohort for the legends</div>', // Description.
|
'<div class="text_to_html">Cohort for the legends</div>', // Description.
|
||||||
'Lionel Richards', // User.
|
'Lionel Richards', // User.
|
||||||
|
|
|
@ -111,4 +111,17 @@ class courses extends datasource {
|
||||||
public function get_default_conditions(): array {
|
public function get_default_conditions(): array {
|
||||||
return ['course_category:name'];
|
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,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,9 +83,27 @@ abstract class datasource extends base {
|
||||||
*/
|
*/
|
||||||
public function add_default_columns(): void {
|
public function add_default_columns(): void {
|
||||||
$reportid = $this->get_report_persistent()->get('id');
|
$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();
|
$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) {
|
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;
|
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
|
* Return all configured report columns
|
||||||
*
|
*
|
||||||
|
|
|
@ -23,6 +23,11 @@ Feature: Manage custom reports
|
||||||
And I should see "Full name" in the "reportbuilder-table" "table"
|
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 "Username" in the "reportbuilder-table" "table"
|
||||||
And I should see "Email address" 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.
|
# 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 "Admin User" in the "reportbuilder-table" "table"
|
||||||
And I should see "User 2" in the "reportbuilder-table" "table"
|
And I should see "User 2" in the "reportbuilder-table" "table"
|
||||||
|
|
|
@ -56,3 +56,5 @@ Information provided here is intended especially for developers.
|
||||||
- `datasource_stress_test_columns_aggregation`
|
- `datasource_stress_test_columns_aggregation`
|
||||||
- `datasource_stress_test_conditions`
|
- `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
|
* 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.
|
||||||
|
|
|
@ -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 {
|
public function get_default_column_sorting(): array {
|
||||||
parent::add_default_columns();
|
return [
|
||||||
|
'user:fullname' => SORT_ASC,
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,24 +44,22 @@ class users_test extends core_reportbuilder_testcase {
|
||||||
public function test_datasource_default(): void {
|
public function test_datasource_default(): void {
|
||||||
$this->resetAfterTest();
|
$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 */
|
/** @var core_reportbuilder_generator $generator */
|
||||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
||||||
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 1]);
|
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 1]);
|
||||||
|
|
||||||
$content = $this->get_custom_report_content($report->get('id'));
|
$content = $this->get_custom_report_content($report->get('id'));
|
||||||
$this->assertCount(2, $content);
|
$this->assertCount(3, $content);
|
||||||
|
|
||||||
// Consistent order by email, just in case.
|
// Default columns are fullname, username, email. Results are sorted by the fullname.
|
||||||
core_collator::asort_array_of_arrays_by_key($content, 'c2_email');
|
[$adminrow, $userrow1, $userrow2] = array_map('array_values', $content);
|
||||||
$content = array_values($content);
|
|
||||||
|
|
||||||
// Default columns are fullname, username, email.
|
|
||||||
[$adminrow, $userrow] = array_map('array_values', $content);
|
|
||||||
|
|
||||||
$this->assertEquals(['Admin User', 'admin', 'admin@example.com'], $adminrow);
|
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue