Skip to content

Commit

Permalink
MDL-76295 reportbuilder: implement default datasource sorting.
Browse files Browse the repository at this point in the history
Update all existing report sources to use the new default sorting
API from 064eccd, updating existing tests to assert behaviour.
  • Loading branch information
paulholden committed Aug 23, 2023
1 parent b3c2a9f commit 33a63ca
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 108 deletions.
11 changes: 11 additions & 0 deletions admin/classes/reportbuilder/datasource/task_logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ public function get_default_columns(): array {
];
}

/**
* Return the column sorting that will be added to the report upon creation
*
* @return int[]
*/
public function get_default_column_sorting(): array {
return [
'task_log:starttime' => SORT_DESC,
];
}

/**
* Return the filters that will be added to the report upon creation
*
Expand Down
11 changes: 9 additions & 2 deletions admin/tests/reportbuilder/datasource/task_logs_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,24 @@ public function test_datasource_default(): void {
$this->resetAfterTest();

$this->generate_task_log_data(true, 3, 2, 1654038000, 1654038060);
$this->generate_task_log_data(false, 5, 1, 1654556400, 1654556700);

/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Tasks', 'source' => task_logs::class, 'default' => 1]);

$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);
$this->assertCount(2, $content);

// Default columns are name, starttime, duration, result.
// Default columns are name, start time, duration, result. Sorted by start time descending.
[$name, $timestart, $duration, $result] = array_values($content[0]);
$this->assertStringContainsString(send_schedules::class, $name);
$this->assertEquals('7/06/22, 07:00:00', $timestart);
$this->assertEquals('5 mins', $duration);
$this->assertEquals('Fail', $result);

[$name, $timestart, $duration, $result] = array_values($content[1]);
$this->assertStringContainsString(send_schedules::class, $name);
$this->assertEquals('1/06/22, 07:00:00', $timestart);
$this->assertEquals('1 min', $duration);
$this->assertEquals('Success', $result);
Expand Down
12 changes: 12 additions & 0 deletions blog/classes/reportbuilder/datasource/blogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ public function get_default_columns(): array {
];
}

/**
* Return the column sorting that will be added to the report upon creation
*
* @return int[]
*/
public function get_default_column_sorting(): array {
return [
'user:fullname' => SORT_ASC,
'blog:timecreated' => SORT_ASC,
];
}

/**
* Return the filters that will be added to the report upon creation
*
Expand Down
28 changes: 12 additions & 16 deletions blog/tests/reportbuilder/datasource/blogs_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use context_system;
use context_user;
use core_blog_generator;
use core_collator;
use core_comment_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
Expand Down Expand Up @@ -51,35 +50,32 @@ public function test_datasource_default(): void {
/** @var core_blog_generator $blogsgenerator */
$blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');

// Our first user will create a course blog.
$course = $this->getDataGenerator()->create_course();
$usercourseblog = $this->getDataGenerator()->create_and_enrol($course);
$courseblog = $blogsgenerator->create_entry(['publishstate' => 'site', 'userid' => $usercourseblog->id,
$userone = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
$courseblog = $blogsgenerator->create_entry(['publishstate' => 'site', 'userid' => $userone->id,
'subject' => 'Course', 'summary' => 'Course summary', 'courseid' => $course->id]);

$userpersonalblog = $this->getDataGenerator()->create_user();
$personalblog = $blogsgenerator->create_entry(['publishstate' => 'draft', 'userid' => $userpersonalblog->id,
// Our second user will create a personal and site blog.
$usertwo = $this->getDataGenerator()->create_user(['firstname' => 'Amy']);
$personalblog = $blogsgenerator->create_entry(['publishstate' => 'draft', 'userid' => $usertwo->id,
'subject' => 'Personal', 'summary' => 'Personal summary']);

$usersiteblog = $this->getDataGenerator()->create_user();
$siteblog = $blogsgenerator->create_entry(['publishstate' => 'public', 'userid' => $usersiteblog->id,
$this->waitForSecond(); // For consistent ordering we need distinct time for second user blogs.
$siteblog = $blogsgenerator->create_entry(['publishstate' => 'public', 'userid' => $usertwo->id,
'subject' => 'Site', 'summary' => 'Site summary']);

/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Blogs', 'source' => blogs::class, 'default' => 1]);

$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(3, $content);

// Consistent order (course, personal, site), just in case.
core_collator::asort_array_of_arrays_by_key($content, 'c2_subject');
$content = array_values($content);

// Default columns are user, course, title, timecreated.
// Default columns are user, course, title, time created. Sorted by user and time created.
$this->assertEquals([
[fullname($usercourseblog), $course->fullname, $courseblog->subject, userdate($courseblog->created)],
[fullname($userpersonalblog), '', $personalblog->subject, userdate($personalblog->created)],
[fullname($usersiteblog), '', $siteblog->subject, userdate($siteblog->created)],
[fullname($usertwo), '', $personalblog->subject, userdate($personalblog->created)],
[fullname($usertwo), '', $siteblog->subject, userdate($siteblog->created)],
[fullname($userone), $course->fullname, $courseblog->subject, userdate($courseblog->created)],
], array_map('array_values', $content));
}

Expand Down
14 changes: 13 additions & 1 deletion comment/classes/reportbuilder/datasource/comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,25 @@ protected function initialise(): void {
*/
public function get_default_columns(): array {
return [
'user:fullname',
'context:name',
'comment:content',
'user:fullname',
'comment:timecreated',
];
}

/**
* Return the column sorting that will be added to the report upon creation
*
* @return int[]
*/
public function get_default_column_sorting(): array {
return [
'user:fullname' => SORT_ASC,
'comment:timecreated' => SORT_ASC,
];
}

/**
* Return the filters that will be added to the report upon creation
*
Expand Down
7 changes: 5 additions & 2 deletions comment/classes/reportbuilder/local/entities/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ protected function get_all_columns(): array {
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_fields("{$commentalias}.component");
->add_fields("{$commentalias}.component")
->set_is_sortable(true);

// Area.
$columns[] = (new column(
Expand All @@ -182,7 +183,8 @@ protected function get_all_columns(): array {
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_fields("{$commentalias}.commentarea");
->add_fields("{$commentalias}.commentarea")
->set_is_sortable(true);

// Item ID.
$columns[] = (new column(
Expand All @@ -193,6 +195,7 @@ protected function get_all_columns(): array {
->add_joins($this->get_joins())
->set_type(column::TYPE_INTEGER)
->add_fields("{$commentalias}.itemid")
->set_is_sortable(true)
->set_disabled_aggregation_all();

// Time created.
Expand Down
7 changes: 5 additions & 2 deletions comment/tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ class core_comment_generator extends component_generator_base {
* Create comment
*
* @param array|stdClass $record
* @return comment
*/
public function create_comment($record): comment {
$record = (array) $record;

$content = $record['content'] ?? '';
$content = (string) ($record['content'] ?? '');
unset($record['content']);

$comment = new comment((object) $record);
$comment->add($content);
if ($content !== '') {
$comment->add($content);
}

return $comment;
}
Expand Down
41 changes: 29 additions & 12 deletions comment/tests/reportbuilder/datasource/comments_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,51 @@ class comments_test extends core_reportbuilder_testcase {
*/
public function test_datasource_default(): void {
$this->resetAfterTest();
$this->setAdminUser();

$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);

/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
$generator->create_comment([

// Our first user will create a single comment.
$userone = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
$this->setUser($userone);
$useronecomment = $generator->create_comment([
'context' => $coursecontext,
'component' => 'block_comments',
'area' => 'page_comments',
'content' => 'Cool',
]);
])->add('Cool');

// Our second user will create a couple of comments.
$usertwo = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Amy']);
$this->setUser($usertwo);
$usertwocommentfirst = $generator->create_comment([
'context' => $coursecontext,
'component' => 'block_comments',
'area' => 'page_comments',
])->add('Super');

$this->waitForSecond(); // For consistent ordering we need distinct time for second user comments.
$usertwocommentsecond = $generator->create_comment([
'context' => $coursecontext,
'component' => 'block_comments',
'area' => 'page_comments',
])->add('Awesome');

/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Blogs', 'source' => comments::class, 'default' => 1]);

$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);

// Default columns are context, content, user, time created.
[$contextname, $content, $userfullname, $timecreated] = array_values($content[0]);

$this->assertEquals($coursecontext->get_context_name(), $contextname);
$this->assertEquals(format_text('Cool'), $content);
$this->assertEquals(fullname(get_admin()), $userfullname);
$this->assertNotEmpty($timecreated);
// Default columns are user, context, content, time created. Sorted by user and time created.
$contextname = $coursecontext->get_context_name();
$this->assertEquals([
[fullname($usertwo), $contextname, format_text('Super'), userdate($usertwocommentfirst->timecreated)],
[fullname($usertwo), $contextname, format_text('Awesome'), userdate($usertwocommentsecond->timecreated)],
[fullname($userone), $contextname, format_text('Cool'), userdate($useronecomment->timecreated)],
], array_map('array_values', $content));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion course/tests/reportbuilder/datasource/categories_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function test_datasource_default(): void {
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(2, $content);

// Default columns are name, idnumber, coursecount. Sorted by name descending.
// Default columns are name, idnumber, coursecount. Sorted by name ascending.
$this->assertEquals([
[get_string('defaultcategoryname'), '', 0],
[$category->get_formatted_name(), $category->idnumber, 1],
Expand Down
20 changes: 11 additions & 9 deletions course/tests/reportbuilder/datasource/courses_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ public function test_datasource_default(): void {

// Test subject.
$category = $this->getDataGenerator()->create_category(['name' => 'My cats']);
$course = $this->getDataGenerator()->create_course([
$courseone = $this->getDataGenerator()->create_course([
'category' => $category->id,
'fullname' => 'Feline fine',
'shortname' => 'C102',
'idnumber' => 'CAT102'
]);
$coursetwo = $this->getDataGenerator()->create_course([
'category' => $category->id,
'fullname' => 'All about cats',
'shortname' => 'C101',
Expand All @@ -63,16 +69,12 @@ public function test_datasource_default(): void {
$report = $generator->create_report(['name' => 'Courses', 'source' => courses::class, 'default' => 1]);

$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);

$contentrow = array_values($content[0]);

// Default columns are category, shortname, fullname, idnumber. Sorted by category, shortname, fullname.
$this->assertEquals([
$category->get_formatted_name(),
$course->shortname,
$course->fullname,
$course->idnumber,
], $contentrow);
[$category->name, $coursetwo->shortname, $coursetwo->fullname, $coursetwo->idnumber],
[$category->name, $courseone->shortname, $courseone->fullname, $courseone->idnumber],
], array_map('array_values', $content));
}

/**
Expand Down
12 changes: 12 additions & 0 deletions files/classes/reportbuilder/datasource/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public function get_default_columns(): array {
];
}

/**
* Return the column sorting that will be added to the report upon creation
*
* @return int[]
*/
public function get_default_column_sorting(): array {
return [
'context:name' => SORT_ASC,
'file:timecreated' => SORT_ASC,
];
}

/**
* Return the filters that will be added to the report upon creation
*
Expand Down
12 changes: 2 additions & 10 deletions files/tests/reportbuilder/datasource/files_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

use context_course;
use context_user;
use core_collator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, number, select, text};
Expand Down Expand Up @@ -67,23 +66,16 @@ public function test_datasource_default(): void {

$this->assertCount(2, $content);

// Consistent order (course, user), just in case.
core_collator::asort_array_of_arrays_by_key($content, 'c0_ctxid');
$content = array_values($content);

// First row (course summary file).
// Default columns are context, user, name, type, size, time created. Sorted by context and time created.
[$contextname, $userfullname, $filename, $mimetype, $filesize, $timecreated] = array_values($content[0]);

$this->assertEquals($coursecontext->get_context_name(), $contextname);
$this->assertEquals(fullname($user), $userfullname);
$this->assertEquals('Hello.txt', $filename);
$this->assertEquals('Text file', $mimetype);
$this->assertEquals("5\xc2\xa0bytes", $filesize);
$this->assertNotEmpty($timecreated);

// Second row (user draft file).
[$contextname, $userfullname, $filename, $mimetype, $filesize, $timecreated] = array_values($content[1]);

$this->assertEquals($usercontext->get_context_name(), $contextname);
$this->assertEquals(fullname($user), $userfullname);
$this->assertEquals('Hello.txt', $filename);
Expand Down Expand Up @@ -332,7 +324,7 @@ protected function filter_custom_report_content(array $content, callable $callba
}

/**
* Helper method to generate some test files for reporting on
* Helper method to generate some test files (a user draft and course summary file) for reporting on
*
* @param context_course $context
* @return int Draft item ID
Expand Down
13 changes: 13 additions & 0 deletions group/classes/reportbuilder/datasource/groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ public function get_default_columns(): array {
];
}

/**
* Return the column sorting that will be added to the report upon creation
*
* @return int[]
*/
public function get_default_column_sorting(): array {
return [
'course:coursefullnamewithlink' => SORT_ASC,
'group:name' => SORT_ASC,
'user:fullname' => SORT_ASC,
];
}

/**
* Return the filters that will be added to the report as part of default setup
*
Expand Down
Loading

0 comments on commit 33a63ca

Please sign in to comment.