MDL-72959 reportbuilder: format course/category names in entities.

This commit is contained in:
Paul Holden 2021-11-01 07:58:31 +00:00
parent 8af7bec81e
commit c7a7a54495
3 changed files with 59 additions and 3 deletions

View File

@ -96,7 +96,10 @@ class course_category extends base {
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_fields("{$tablealias}.name")
->add_fields("{$tablealias}.name, {$tablealias}.id")
->add_callback(static function(string $name, stdClass $category): string {
return core_course_category::get($category->id, MUST_EXIST, true)->get_formatted_name();
})
->set_is_sortable(true);
// ID number column.

View File

@ -66,4 +66,42 @@ class datasource_test extends core_reportbuilder_testcase {
'CAT101', // Course ID number.
], $contentrow);
}
/**
* Tests courses datasource using multilang filters
*/
public function test_courses_datasource_multilang_filters(): void {
$this->resetAfterTest();
filter_set_global_state('multilang', TEXTFILTER_ON);
filter_set_applies_to_strings('multilang', true);
// Test subject.
$category = $this->getDataGenerator()->create_category([
'name' => '<span class="multilang" lang="en">Cat (en)</span><span class="multilang" lang="es">Cat (es)</span>',
]);
$course = $this->getDataGenerator()->create_course([
'category' => $category->id,
'fullname' => '<span class="multilang" lang="en">Crs (en)</span><span class="multilang" lang="es">Crs (es)</span>',
]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create a report containing columns that support multilang content.
$report = $generator->create_report(['name' => 'Courses', 'source' => courses::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course_category:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:coursefullnamewithlink']);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);
$contentrow = array_values(reset($content));
$this->assertEquals([
'Cat (en)',
'Crs (en)',
'<a href="' . (string) course_get_url($course->id) . '">Crs (en)</a>',
], $contentrow);
}
}

View File

@ -216,7 +216,7 @@ class course extends base {
'courseidnumberewithlink' => 'idnumber',
];
foreach ($fields as $key => $field) {
$columns[] = (new column(
$column = (new column(
$key,
new lang_string($key, 'core_reportbuilder'),
$this->get_entity_name()
@ -230,8 +230,23 @@ class course extends base {
return '';
}
return html_writer::link(course_get_url($row->id), $value);
context_helper::preload_from_record($row);
return html_writer::link(course_get_url($row->id),
format_string($value, true, ['context' => context_course::instance($row->id)]));
});
// Join on the context table so that we can use it for formatting these columns later.
if ($key === 'coursefullnamewithlink') {
$join = "LEFT JOIN {context} {$contexttablealias}
ON {$contexttablealias}.contextlevel = " . CONTEXT_COURSE . "
AND {$contexttablealias}.instanceid = {$tablealias}.id";
$column->add_join($join)
->add_fields(context_helper::get_preload_record_columns_sql($contexttablealias));
}
$columns[] = $column;
}
foreach ($coursefields as $coursefield => $coursefieldlang) {