MDL-75119 reportbuilder: preload context data for column callbacks.
This prevents additional requests for context instances in the callback methods, by preloading all required data in the initial field select.
This commit is contained in:
@@ -19,6 +19,7 @@ declare(strict_types=1);
|
|||||||
namespace core_badges\reportbuilder\local\entities;
|
namespace core_badges\reportbuilder\local\entities;
|
||||||
|
|
||||||
use context_course;
|
use context_course;
|
||||||
|
use context_helper;
|
||||||
use context_system;
|
use context_system;
|
||||||
use html_writer;
|
use html_writer;
|
||||||
use lang_string;
|
use lang_string;
|
||||||
@@ -48,7 +49,10 @@ class badge extends base {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function get_default_table_aliases(): array {
|
protected function get_default_table_aliases(): array {
|
||||||
return ['badge' => 'b'];
|
return [
|
||||||
|
'badge' => 'b',
|
||||||
|
'context' => 'bctx',
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -89,6 +93,7 @@ class badge extends base {
|
|||||||
*/
|
*/
|
||||||
protected function get_all_columns(): array {
|
protected function get_all_columns(): array {
|
||||||
$badgealias = $this->get_table_alias('badge');
|
$badgealias = $this->get_table_alias('badge');
|
||||||
|
$contextalias = $this->get_table_alias('context');
|
||||||
|
|
||||||
// Name.
|
// Name.
|
||||||
$columns[] = (new column(
|
$columns[] = (new column(
|
||||||
@@ -101,7 +106,7 @@ class badge extends base {
|
|||||||
->add_field("{$badgealias}.name")
|
->add_field("{$badgealias}.name")
|
||||||
->set_is_sortable(true);
|
->set_is_sortable(true);
|
||||||
|
|
||||||
// Description.
|
// Description (note, this column contains plaintext so requires no post-processing).
|
||||||
$columns[] = (new column(
|
$columns[] = (new column(
|
||||||
'description',
|
'description',
|
||||||
new lang_string('description', 'core_badges'),
|
new lang_string('description', 'core_badges'),
|
||||||
@@ -137,13 +142,20 @@ class badge extends base {
|
|||||||
$this->get_entity_name()
|
$this->get_entity_name()
|
||||||
))
|
))
|
||||||
->add_joins($this->get_joins())
|
->add_joins($this->get_joins())
|
||||||
|
->add_join("LEFT JOIN {context} {$contextalias}
|
||||||
|
ON {$contextalias}.contextlevel = " . CONTEXT_COURSE . "
|
||||||
|
AND {$contextalias}.instanceid = {$badgealias}.courseid")
|
||||||
->set_type(column::TYPE_INTEGER)
|
->set_type(column::TYPE_INTEGER)
|
||||||
->add_fields("{$badgealias}.id, {$badgealias}.type, {$badgealias}.courseid, {$badgealias}.imagecaption")
|
->add_fields("{$badgealias}.id, {$badgealias}.type, {$badgealias}.courseid, {$badgealias}.imagecaption")
|
||||||
|
->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
|
||||||
->set_disabled_aggregation_all()
|
->set_disabled_aggregation_all()
|
||||||
->add_callback(static function(int $badgeid, stdClass $badge): string {
|
->add_callback(static function(int $badgeid, stdClass $badge): string {
|
||||||
$context = $badge->type == BADGE_TYPE_SITE
|
if ($badge->type == BADGE_TYPE_SITE) {
|
||||||
? context_system::instance()
|
$context = context_system::instance();
|
||||||
: context_course::instance($badge->courseid);
|
} else {
|
||||||
|
context_helper::preload_from_record($badge);
|
||||||
|
$context = context_course::instance($badge->courseid);
|
||||||
|
}
|
||||||
|
|
||||||
$badgeimage = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $badgeid, '/', 'f2');
|
$badgeimage = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $badgeid, '/', 'f2');
|
||||||
return html_writer::img($badgeimage, $badge->imagecaption);
|
return html_writer::img($badgeimage, $badge->imagecaption);
|
||||||
|
|||||||
@@ -45,7 +45,10 @@ class cohort extends base {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function get_default_table_aliases(): array {
|
protected function get_default_table_aliases(): array {
|
||||||
return ['cohort' => 'c'];
|
return [
|
||||||
|
'cohort' => 'c',
|
||||||
|
'context' => 'chctx',
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -86,6 +89,7 @@ class cohort extends base {
|
|||||||
*/
|
*/
|
||||||
protected function get_all_columns(): array {
|
protected function get_all_columns(): array {
|
||||||
$tablealias = $this->get_table_alias('cohort');
|
$tablealias = $this->get_table_alias('cohort');
|
||||||
|
$contextalias = $this->get_table_alias('context');
|
||||||
|
|
||||||
// Category/context column.
|
// Category/context column.
|
||||||
$columns[] = (new column(
|
$columns[] = (new column(
|
||||||
@@ -94,11 +98,13 @@ class cohort extends base {
|
|||||||
$this->get_entity_name()
|
$this->get_entity_name()
|
||||||
))
|
))
|
||||||
->add_joins($this->get_joins())
|
->add_joins($this->get_joins())
|
||||||
|
->add_join("JOIN {context} {$contextalias} ON {$contextalias}.id = {$tablealias}.contextid")
|
||||||
->set_type(column::TYPE_TEXT)
|
->set_type(column::TYPE_TEXT)
|
||||||
->add_fields("{$tablealias}.contextid")
|
->add_fields("{$tablealias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias))
|
||||||
->set_is_sortable(true)
|
->set_is_sortable(true)
|
||||||
->add_callback(static function($contextid): string {
|
->add_callback(static function($contextid, stdClass $cohort): string {
|
||||||
return context::instance_by_id((int) $contextid)->get_context_name(false);
|
context_helper::preload_from_record($cohort);
|
||||||
|
return context::instance_by_id($cohort->contextid)->get_context_name(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Name column.
|
// Name column.
|
||||||
@@ -130,8 +136,10 @@ class cohort extends base {
|
|||||||
$this->get_entity_name()
|
$this->get_entity_name()
|
||||||
))
|
))
|
||||||
->add_joins($this->get_joins())
|
->add_joins($this->get_joins())
|
||||||
->set_type(column::TYPE_TEXT)
|
->add_join("JOIN {context} {$contextalias} ON {$contextalias}.id = {$tablealias}.contextid")
|
||||||
|
->set_type(column::TYPE_LONGTEXT)
|
||||||
->add_fields("{$tablealias}.description, {$tablealias}.descriptionformat, {$tablealias}.id, {$tablealias}.contextid")
|
->add_fields("{$tablealias}.description, {$tablealias}.descriptionformat, {$tablealias}.id, {$tablealias}.contextid")
|
||||||
|
->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
|
||||||
->add_callback(static function(?string $description, stdClass $cohort): string {
|
->add_callback(static function(?string $description, stdClass $cohort): string {
|
||||||
global $CFG;
|
global $CFG;
|
||||||
require_once("{$CFG->libdir}/filelib.php");
|
require_once("{$CFG->libdir}/filelib.php");
|
||||||
@@ -140,12 +148,14 @@ class cohort extends base {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $cohort->contextid, 'cohort',
|
context_helper::preload_from_record($cohort);
|
||||||
|
$context = context::instance_by_id($cohort->contextid);
|
||||||
|
|
||||||
|
$description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'cohort',
|
||||||
'description', $cohort->id);
|
'description', $cohort->id);
|
||||||
|
|
||||||
return format_text($description, $cohort->descriptionformat, ['context' => $cohort->contextid]);
|
return format_text($description, $cohort->descriptionformat, ['context' => $context->id]);
|
||||||
})
|
});
|
||||||
->set_is_sortable(false);
|
|
||||||
|
|
||||||
// Visible column.
|
// Visible column.
|
||||||
$columns[] = (new column(
|
$columns[] = (new column(
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace core_course\local\entities;
|
namespace core_course\local\entities;
|
||||||
|
|
||||||
|
use context_coursecat;
|
||||||
|
use context_helper;
|
||||||
use lang_string;
|
use lang_string;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
use core_course_category;
|
use core_course_category;
|
||||||
@@ -135,12 +137,12 @@ class course_category extends base {
|
|||||||
$this->get_entity_name()
|
$this->get_entity_name()
|
||||||
))
|
))
|
||||||
->add_joins($this->get_joins())
|
->add_joins($this->get_joins())
|
||||||
->add_join("
|
->add_join("LEFT JOIN {context} {$tablealiascontext}
|
||||||
JOIN {context} {$tablealiascontext}
|
ON {$tablealiascontext}.contextlevel = " . CONTEXT_COURSECAT . "
|
||||||
ON {$tablealiascontext}.instanceid = {$tablealias}.id
|
AND {$tablealiascontext}.instanceid = {$tablealias}.id")
|
||||||
AND {$tablealiascontext}.contextlevel = " . CONTEXT_COURSECAT)
|
->set_type(column::TYPE_LONGTEXT)
|
||||||
->set_type(column::TYPE_TEXT)
|
->add_fields("{$tablealias}.description, {$tablealias}.descriptionformat, {$tablealias}.id")
|
||||||
->add_fields("{$tablealias}.description, {$tablealias}.descriptionformat, {$tablealiascontext}.id AS contextid")
|
->add_fields(context_helper::get_preload_record_columns_sql($tablealiascontext))
|
||||||
->add_callback(static function(?string $description, stdClass $category): string {
|
->add_callback(static function(?string $description, stdClass $category): string {
|
||||||
global $CFG;
|
global $CFG;
|
||||||
require_once("{$CFG->libdir}/filelib.php");
|
require_once("{$CFG->libdir}/filelib.php");
|
||||||
@@ -149,12 +151,14 @@ class course_category extends base {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $category->contextid, 'coursecat',
|
context_helper::preload_from_record($category);
|
||||||
|
$context = context_coursecat::instance($category->id);
|
||||||
|
|
||||||
|
$description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'coursecat',
|
||||||
'description', null);
|
'description', null);
|
||||||
|
|
||||||
return format_text($description, $category->descriptionformat, ['context' => $category->contextid]);
|
return format_text($description, $category->descriptionformat, ['context' => $context->id]);
|
||||||
})
|
});
|
||||||
->set_is_sortable(false);
|
|
||||||
|
|
||||||
return $columns;
|
return $columns;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user