Skip to content

Commit

Permalink
MDL-82202 course: correct last access report entity column callback.
Browse files Browse the repository at this point in the history
Ensure it works when being aggregated.
  • Loading branch information
paulholden committed Jun 17, 2024
1 parent fbed803 commit 79d97f0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
10 changes: 4 additions & 6 deletions course/classes/reportbuilder/local/entities/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,13 @@ protected function get_all_columns(): array {
->add_field("{$tablealias}.timeaccess")
->add_field("{$user}.id", 'userid')
->set_is_sortable(true)
->add_callback([format::class, 'userdate'])
->add_callback(static function(string $value, stdClass $row): string {
if (!$row->userid) {
->add_callback(static function(?int $value, stdClass $row, $arguments, ?string $aggregation): string {
if ($row->userid === null && $aggregation === null) {
return '';
}
if ($value === '') {
} else if ($value === null) {
return get_string('never');
}
return $value;
return format::userdate($value, $row);
});

return $columns;
Expand Down
37 changes: 37 additions & 0 deletions course/tests/reportbuilder/datasource/participants_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,43 @@ public function test_datasource_non_default_columns(): void {
], array_values($content[0]));
}


/**
* Test creating course report, with aggregated last access date (minimum and maximum)
*/
public function test_course_last_access_aggregation(): void {
$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();

$userone = $this->getDataGenerator()->create_and_enrol($course);
$useronelastaccess = $this->getDataGenerator()->create_user_course_lastaccess($userone, $course, 1622502000);

$usertwo = $this->getDataGenerator()->create_and_enrol($course);
$usertwolastaccess = $this->getDataGenerator()->create_user_course_lastaccess($usertwo, $course, 1622847600);

/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');

$report = $generator->create_report(['name' => 'Courses', 'source' => participants::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']);
$column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'access:timeaccess']);

// Course aggregated with "Minimum" last access.
$column->set('aggregation', 'min')->update();
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([
[$course->fullname, userdate($useronelastaccess->timeaccess)]
], array_map('array_values', $content));

// Course aggregated with "Maximum" last access.
$column->set('aggregation', 'max')->update();
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([
[$course->fullname, userdate($usertwolastaccess->timeaccess)]
], array_map('array_values', $content));
}

/**
* Data provider for {@see test_datasource_filters}
*
Expand Down

0 comments on commit 79d97f0

Please sign in to comment.