Merge branch 'MDL-75525-master-2' of https://github.com/marinaglancy/moodle

This commit is contained in:
Shamim Rezaie 2022-11-11 12:48:22 +11:00
commit 535e997ac3
9 changed files with 88 additions and 23 deletions

View File

@ -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,
];
}
}

View File

@ -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,
];
}
}

View File

@ -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.
'<div class="text_to_html">Cohort for the legends</div>', // Description.
'Lionel Richards', // User.

View File

@ -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,
];
}
}

View File

@ -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
*

View File

@ -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"

View File

@ -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.

View File

@ -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,
];
}
}

View File

@ -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);
}
/**