Skip to content

Commit

Permalink
MDL-77700 blog: add comment author data to custom report source.
Browse files Browse the repository at this point in the history
The ability to report on blog comments was added in 27ebde5, however
it lacked the option to report on the user who added the comment(s).

AMOS BEGIN
 CPY [author,core_notes],[author,core_blog]
AMOS END
  • Loading branch information
paulholden committed May 4, 2023
1 parent 5252124 commit ea404b8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
25 changes: 20 additions & 5 deletions blog/classes/reportbuilder/datasource/blogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ protected function initialise(): void {
->add_joins($blogentity->get_tag_joins()));

// Join the user entity to represent the blog author.
$userentity = new user();
$useralias = $userentity->get_table_alias('user');
$this->add_entity($userentity
->add_join("LEFT JOIN {user} {$useralias} ON {$useralias}.id = {$postalias}.userid"));
$authorentity = (new user())
->set_entity_title(new lang_string('author', 'core_blog'));
$authoralias = $authorentity->get_table_alias('user');
$this->add_entity($authorentity
->add_join("LEFT JOIN {user} {$authoralias} ON {$authoralias}.id = {$postalias}.userid"));

// Join the course entity for course blogs.
$courseentity = new course();
Expand All @@ -93,6 +94,18 @@ protected function initialise(): void {
$this->add_entity($commententity
->add_join("LEFT JOIN {comments} bcmt ON bcmt.component = 'blog' AND bcmt.itemid = {$postalias}.id"));

// Join the user entity to represent the comment author. Override table aliases to avoid clash with first instance.
$commenterentity = (new user())
->set_entity_name('commenter')
->set_entity_title(new lang_string('commenter', 'core_comment'))
->set_table_aliases([
'user' => 'cu',
'context' => 'cuctx',
]);
$this->add_entity($commenterentity
->add_joins($commententity->get_joins())
->add_join("LEFT JOIN {user} cu ON cu.id = bcmt.userid"));

// Add report elements from each of the entities we added to the report.
$this->add_all_from_entity($blogentity->get_entity_name());

Expand All @@ -105,13 +118,15 @@ protected function initialise(): void {
$this->add_filter($tagentity->get_filter('name'));
$this->add_condition($tagentity->get_condition('name'));

$this->add_all_from_entity($userentity->get_entity_name());
$this->add_all_from_entity($authorentity->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'));

$this->add_all_from_entity($commenterentity->get_entity_name());
}

/**
Expand Down
5 changes: 5 additions & 0 deletions blog/tests/reportbuilder/datasource/blogs_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public function test_datasource_non_default_columns(): void {
// Comment entity.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:content']);

// Commenter entity.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'commenter:fullname']);

$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);

Expand All @@ -154,6 +157,7 @@ public function test_datasource_non_default_columns(): void {
$tags,
$filesize,
$comment,
$commenter,
] = array_values($content[0]);

$this->assertStringContainsString('Horses', $body);
Expand All @@ -163,6 +167,7 @@ public function test_datasource_non_default_columns(): void {
$this->assertEquals('horse', $tags);
$this->assertEquals("5\xc2\xa0bytes", $filesize);
$this->assertEquals(format_text('Cool'), $comment);
$this->assertEquals(fullname($user), $commenter);
}

/**
Expand Down
1 change: 1 addition & 0 deletions lang/en/blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
$string['association'] = 'Association';
$string['associations'] = 'Associations';
$string['associationunviewable'] = 'This entry cannot be viewed by others until a course is associated with it or the \'Publish to\' field is changed';
$string['author'] = 'Author';
$string['autotags'] = 'Add these tags';
$string['autotags_help'] = 'Enter one or more local tags (separated by commas) that you want to automatically add to each blog entry copied from the external blog into your local blog.';
$string['backupblogshelp'] = 'If enabled then blogs will be included in SITE automated backups';
Expand Down
1 change: 1 addition & 0 deletions lang/en/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

$string['comment'] = 'Comment';
$string['commenter'] = 'Commenter';
$string['comments'] = 'Comments';
$string['commentsubcontext'] = 'Comments';
$string['privacy:metadata:comment'] = 'Stores comments of users.';
Expand Down

0 comments on commit ea404b8

Please sign in to comment.