MDL-76153 blog: add comments to blogs custom report source.

This commit is contained in:
Paul Holden
2022-12-20 09:32:11 +00:00
parent 5019abfdd6
commit 27ebde570f
2 changed files with 45 additions and 1 deletions
@@ -22,6 +22,7 @@ use lang_string;
use core_reportbuilder\datasource; use core_reportbuilder\datasource;
use core_reportbuilder\local\entities\{course, user}; use core_reportbuilder\local\entities\{course, user};
use core_blog\reportbuilder\local\entities\blog; use core_blog\reportbuilder\local\entities\blog;
use core_comment\reportbuilder\local\entities\comment;
use core_tag\reportbuilder\local\entities\tag; use core_tag\reportbuilder\local\entities\tag;
/** /**
@@ -73,6 +74,12 @@ class blogs extends datasource {
$this->add_entity($courseentity $this->add_entity($courseentity
->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.id = {$postalias}.courseid")); ->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.id = {$postalias}.courseid"));
// Join the comment entity (ensure differing alias from that used by course entity).
$commententity = (new comment())
->set_table_alias('comments', 'bcmt');
$this->add_entity($commententity
->add_join("LEFT JOIN {comments} bcmt ON bcmt.component = 'blog' AND bcmt.itemid = {$postalias}.id"));
// Add report elements from each of the entities we added to the report. // Add report elements from each of the entities we added to the report.
$this->add_all_from_entity($blogentity->get_entity_name()); $this->add_all_from_entity($blogentity->get_entity_name());
@@ -83,6 +90,11 @@ class blogs extends datasource {
$this->add_all_from_entity($userentity->get_entity_name()); $this->add_all_from_entity($userentity->get_entity_name());
$this->add_all_from_entity($courseentity->get_entity_name()); $this->add_all_from_entity($courseentity->get_entity_name());
// Add specific comment entity elements.
$this->add_columns_from_entity($commententity->get_entity_name(), ['content', 'timecreated']);
$this->add_filter($commententity->get_filter('timecreated'));
$this->add_condition($commententity->get_filter('timecreated'));
} }
/** /**
@@ -18,7 +18,9 @@ declare(strict_types=1);
namespace core_blog\reportbuilder\datasource; namespace core_blog\reportbuilder\datasource;
use comment;
use context_system; use context_system;
use context_user;
use core_blog_generator; use core_blog_generator;
use core_collator; use core_collator;
use core_reportbuilder_generator; use core_reportbuilder_generator;
@@ -40,6 +42,14 @@ require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
*/ */
class blogs_test extends core_reportbuilder_testcase { class blogs_test extends core_reportbuilder_testcase {
/**
* Required test libraries
*/
public static function setUpBeforeClass(): void {
global $CFG;
require_once("{$CFG->dirroot}/comment/lib.php");
}
/** /**
* Test default datasource * Test default datasource
*/ */
@@ -90,6 +100,7 @@ class blogs_test extends core_reportbuilder_testcase {
$this->resetAfterTest(); $this->resetAfterTest();
$user = $this->getDataGenerator()->create_user(); $user = $this->getDataGenerator()->create_user();
$this->setUser($user);
/** @var core_blog_generator $blogsgenerator */ /** @var core_blog_generator $blogsgenerator */
$blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog'); $blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
@@ -107,6 +118,15 @@ class blogs_test extends core_reportbuilder_testcase {
'filename' => 'hello.txt', 'filename' => 'hello.txt',
], 'hello'); ], 'hello');
// Add a comment.
$comment = new comment((object) [
'context' => context_user::instance($user->id),
'component' => 'blog',
'area' => 'format_blog',
'itemid' => $blog->id,
]);
$comment->add('Cool');
// Manually update the created/modified date of the blog. // Manually update the created/modified date of the blog.
$blog->created = 1654038000; $blog->created = 1654038000;
$blog->lastmodified = $blog->created + HOURSECS; $blog->lastmodified = $blog->created + HOURSECS;
@@ -124,15 +144,27 @@ class blogs_test extends core_reportbuilder_testcase {
// Tag entity (course/user presence already checked by default columns). // Tag entity (course/user presence already checked by default columns).
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']); $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']);
// Comment entity.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:content']);
$content = $this->get_custom_report_content($report->get('id')); $content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content); $this->assertCount(1, $content);
[$body, $attachment, $publishstate, $timemodified, $tags] = array_values($content[0]); [
$body,
$attachment,
$publishstate,
$timemodified,
$tags,
$comment,
] = array_values($content[0]);
$this->assertStringContainsString('Horses', $body); $this->assertStringContainsString('Horses', $body);
$this->assertStringContainsString('hello.txt', $attachment); $this->assertStringContainsString('hello.txt', $attachment);
$this->assertEquals('Yourself (draft)', $publishstate); $this->assertEquals('Yourself (draft)', $publishstate);
$this->assertEquals(userdate($blog->lastmodified), $timemodified); $this->assertEquals(userdate($blog->lastmodified), $timemodified);
$this->assertEquals('horse', $tags); $this->assertEquals('horse', $tags);
$this->assertEquals(format_text('Cool'), $comment);
} }
/** /**