From 224b3ab51f1e76aa48666f739c115dfb2b811372 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 7 Nov 2022 22:04:27 +0000 Subject: [PATCH] MDL-76154 blog: add files/attachments to custom report source. --- .../reportbuilder/datasource/blogs.php | 19 ++++++++++++++++++- .../reportbuilder/datasource/blogs_test.php | 14 +++++++++++++- lang/en/blog.php | 1 + 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/blog/classes/reportbuilder/datasource/blogs.php b/blog/classes/reportbuilder/datasource/blogs.php index bbfc88071c5..c706d2799e9 100644 --- a/blog/classes/reportbuilder/datasource/blogs.php +++ b/blog/classes/reportbuilder/datasource/blogs.php @@ -22,6 +22,7 @@ use lang_string; use core_reportbuilder\datasource; use core_reportbuilder\local\entities\{course, user}; use core_blog\reportbuilder\local\entities\blog; +use core_files\reportbuilder\local\entities\file; use core_tag\reportbuilder\local\entities\tag; /** @@ -54,6 +55,18 @@ class blogs extends datasource { $this->add_entity($blogentity); + // Join the files entity. + $fileentity = (new file()) + ->set_entity_title(new lang_string('blogattachment', 'core_blog')); + $filesalias = $fileentity->get_table_alias('files'); + $this->add_entity($fileentity + ->add_join("LEFT JOIN {files} {$filesalias} + ON {$filesalias}.contextid = " . SYSCONTEXTID . " + AND {$filesalias}.component = 'blog' + AND {$filesalias}.filearea = 'attachment' + AND {$filesalias}.itemid = {$postalias}.id + AND {$filesalias}.filename != '.'")); + // Join the tag entity. $tagentity = (new tag()) ->set_entity_title(new lang_string('blogtags', 'core_blog')) @@ -76,7 +89,11 @@ class blogs extends datasource { // Add report elements from each of the entities we added to the report. $this->add_all_from_entity($blogentity->get_entity_name()); - // Add specific tag entity elements. + // Add specific file/tag entity elements. + $this->add_columns_from_entity($fileentity->get_entity_name(), ['name', 'size', 'type', 'timecreated']); + $this->add_filters_from_entity($fileentity->get_entity_name(), ['name', 'size', 'timecreated']); + $this->add_conditions_from_entity($fileentity->get_entity_name(), ['name', 'size', 'timecreated']); + $this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']); $this->add_filter($tagentity->get_filter('name')); $this->add_condition($tagentity->get_condition('name')); diff --git a/blog/tests/reportbuilder/datasource/blogs_test.php b/blog/tests/reportbuilder/datasource/blogs_test.php index b6b42425901..60a026c32d2 100644 --- a/blog/tests/reportbuilder/datasource/blogs_test.php +++ b/blog/tests/reportbuilder/datasource/blogs_test.php @@ -124,15 +124,27 @@ class blogs_test extends core_reportbuilder_testcase { // Tag entity (course/user presence already checked by default columns). $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']); + // File entity. + $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:size']); + $content = $this->get_custom_report_content($report->get('id')); $this->assertCount(1, $content); - [$body, $attachment, $publishstate, $timemodified, $tags] = array_values($content[0]); + [ + $body, + $attachment, + $publishstate, + $timemodified, + $tags, + $filesize, + ] = array_values($content[0]); + $this->assertStringContainsString('Horses', $body); $this->assertStringContainsString('hello.txt', $attachment); $this->assertEquals('Yourself (draft)', $publishstate); $this->assertEquals(userdate($blog->lastmodified), $timemodified); $this->assertEquals('horse', $tags); + $this->assertEquals("5\xc2\xa0bytes", $filesize); } /** diff --git a/lang/en/blog.php b/lang/en/blog.php index d3cdd9e32c7..2e6d538e608 100644 --- a/lang/en/blog.php +++ b/lang/en/blog.php @@ -40,6 +40,7 @@ $string['blogaboutthis'] = 'Blog about this {$a->type}'; $string['blogaboutthiscourse'] = 'Add an entry about this course'; $string['blogaboutthismodule'] = 'Add an entry about this {$a}'; $string['blogadministration'] = 'Blog administration'; +$string['blogattachment'] = 'Blog attachment'; $string['blogdeleteconfirm'] = 'Delete the blog entry \'{$a}\'?'; $string['blogdisable'] = 'Blogging is disabled!'; $string['blogentries'] = 'Blog entries';