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

This commit is contained in:
Paul Holden 2022-11-07 21:10:01 +00:00
parent 5019abfdd6
commit 27ebde570f
2 changed files with 45 additions and 1 deletions

View File

@ -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_comment\reportbuilder\local\entities\comment;
use core_tag\reportbuilder\local\entities\tag;
/**
@ -73,6 +74,12 @@ class blogs extends datasource {
$this->add_entity($courseentity
->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.
$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($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'));
}
/**

View File

@ -18,7 +18,9 @@ declare(strict_types=1);
namespace core_blog\reportbuilder\datasource;
use comment;
use context_system;
use context_user;
use core_blog_generator;
use core_collator;
use core_reportbuilder_generator;
@ -40,6 +42,14 @@ require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
*/
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
*/
@ -90,6 +100,7 @@ class blogs_test extends core_reportbuilder_testcase {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
/** @var core_blog_generator $blogsgenerator */
$blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
@ -107,6 +118,15 @@ class blogs_test extends core_reportbuilder_testcase {
'filename' => 'hello.txt',
], '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.
$blog->created = 1654038000;
$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).
$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'));
$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('hello.txt', $attachment);
$this->assertEquals('Yourself (draft)', $publishstate);
$this->assertEquals(userdate($blog->lastmodified), $timemodified);
$this->assertEquals('horse', $tags);
$this->assertEquals(format_text('Cool'), $comment);
}
/**