Skip to content

Commit

Permalink
MDL-79532 blog: add "title with link" column to report entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Jan 5, 2024
1 parent 9587029 commit f6c918a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
25 changes: 22 additions & 3 deletions blog/classes/reportbuilder/local/entities/blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use blog_entry_attachment;
use context_system;
use core_collator;
use html_writer;
use lang_string;
use moodle_url;
use stdClass;
use core_reportbuilder\local\entities\base;
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
Expand Down Expand Up @@ -102,6 +104,23 @@ protected function get_all_columns(): array {
->add_fields("{$postalias}.subject")
->set_is_sortable(true);

// Title with link.
$columns[] = (new column(
'titlewithlink',
new lang_string('entrytitlewithlink', 'core_blog'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_fields("{$postalias}.subject, {$postalias}.id")
->set_is_sortable(true)
->add_callback(static function(?string $subject, stdClass $post): string {
if ($subject === null) {
return '';
}
return html_writer::link(new moodle_url('/blog/index.php', ['entryid' => $post->id]), $subject);
});

// Body.
$summaryfieldsql = "{$postalias}.summary";
if ($DB->get_dbfamily() === 'oracle') {
Expand All @@ -117,7 +136,7 @@ protected function get_all_columns(): array {
->set_type(column::TYPE_LONGTEXT)
->add_field($summaryfieldsql, 'summary')
->add_fields("{$postalias}.summaryformat, {$postalias}.id")
->add_callback(static function(?string $summary, stdClass $blog): string {
->add_callback(static function(?string $summary, stdClass $post): string {
global $CFG;
require_once("{$CFG->libdir}/filelib.php");

Expand All @@ -127,9 +146,9 @@ protected function get_all_columns(): array {

// All blog files are stored in system context.
$context = context_system::instance();
$summary = file_rewrite_pluginfile_urls($summary, 'pluginfile.php', $context->id, 'blog', 'post', $blog->id);
$summary = file_rewrite_pluginfile_urls($summary, 'pluginfile.php', $context->id, 'blog', 'post', $post->id);

return format_text($summary, $blog->summaryformat, ['context' => $context->id]);
return format_text($summary, $post->summaryformat, ['context' => $context->id]);
});

// Attachment.
Expand Down
4 changes: 4 additions & 0 deletions blog/tests/reportbuilder/datasource/blogs_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public function test_datasource_non_default_columns(): void {
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Blogs', 'source' => blogs::class, 'default' => 0]);

$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:titlewithlink']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:body']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:attachment']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:publishstate']);
Expand All @@ -146,6 +147,7 @@ public function test_datasource_non_default_columns(): void {
$this->assertCount(1, $content);

[
$link,
$body,
$attachment,
$publishstate,
Expand All @@ -156,6 +158,8 @@ public function test_datasource_non_default_columns(): void {
$commenter,
] = array_values($content[0]);

$this->assertEquals("<a href=\"https://www.example.com/moodle/blog/index.php?entryid={$blog->id}\">{$blog->subject}</a>",
$link);
$this->assertStringContainsString('Horses', $body);
$this->assertStringContainsString('hello.txt', $attachment);
$this->assertEquals('Draft', $publishstate);
Expand Down
1 change: 1 addition & 0 deletions lang/en/blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
$string['entryerrornotyours'] = 'This entry is not yours';
$string['entrysaved'] = 'Your entry has been saved';
$string['entrytitle'] = 'Entry title';
$string['entrytitlewithlink'] = 'Entry title with link';
$string['eventblogentriesviewed'] = 'Blog entries viewed';
$string['eventblogassociationadded'] = 'Blog association created';
$string['eventblogassociationdeleted'] = 'Blog association deleted';
Expand Down

0 comments on commit f6c918a

Please sign in to comment.