Skip to content

Commit

Permalink
Merge branch 'MDL-75119' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
vmdef committed Jul 19, 2022
2 parents 068eda3 + dc60fe6 commit 5087f56
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
22 changes: 17 additions & 5 deletions badges/classes/reportbuilder/local/entities/badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace core_badges\reportbuilder\local\entities;

use context_course;
use context_helper;
use context_system;
use html_writer;
use lang_string;
Expand Down Expand Up @@ -48,7 +49,10 @@ class badge extends base {
* @return array
*/
protected function get_default_table_aliases(): array {
return ['badge' => 'b'];
return [
'badge' => 'b',
'context' => 'bctx',
];
}

/**
Expand Down Expand Up @@ -89,6 +93,7 @@ public function initialise(): base {
*/
protected function get_all_columns(): array {
$badgealias = $this->get_table_alias('badge');
$contextalias = $this->get_table_alias('context');

// Name.
$columns[] = (new column(
Expand All @@ -101,7 +106,7 @@ protected function get_all_columns(): array {
->add_field("{$badgealias}.name")
->set_is_sortable(true);

// Description.
// Description (note, this column contains plaintext so requires no post-processing).
$columns[] = (new column(
'description',
new lang_string('description', 'core_badges'),
Expand Down Expand Up @@ -137,13 +142,20 @@ protected function get_all_columns(): array {
$this->get_entity_name()
))
->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)
->add_fields("{$badgealias}.id, {$badgealias}.type, {$badgealias}.courseid, {$badgealias}.imagecaption")
->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
->set_disabled_aggregation_all()
->add_callback(static function(int $badgeid, stdClass $badge): string {
$context = $badge->type == BADGE_TYPE_SITE
? context_system::instance()
: context_course::instance($badge->courseid);
if ($badge->type == BADGE_TYPE_SITE) {
$context = context_system::instance();
} 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');
return html_writer::img($badgeimage, $badge->imagecaption);
Expand Down
28 changes: 19 additions & 9 deletions cohort/classes/local/entities/cohort.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class cohort extends base {
* @return array
*/
protected function get_default_table_aliases(): array {
return ['cohort' => 'c'];
return [
'cohort' => 'c',
'context' => 'chctx',
];
}

/**
Expand Down Expand Up @@ -86,6 +89,7 @@ public function initialise(): base {
*/
protected function get_all_columns(): array {
$tablealias = $this->get_table_alias('cohort');
$contextalias = $this->get_table_alias('context');

// Category/context column.
$columns[] = (new column(
Expand All @@ -94,11 +98,13 @@ protected function get_all_columns(): array {
$this->get_entity_name()
))
->add_joins($this->get_joins())
->add_join("JOIN {context} {$contextalias} ON {$contextalias}.id = {$tablealias}.contextid")
->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)
->add_callback(static function($contextid): string {
return context::instance_by_id((int) $contextid)->get_context_name(false);
->add_callback(static function($contextid, stdClass $cohort): string {
context_helper::preload_from_record($cohort);
return context::instance_by_id($cohort->contextid)->get_context_name(false);
});

// Name column.
Expand Down Expand Up @@ -130,8 +136,10 @@ protected function get_all_columns(): array {
$this->get_entity_name()
))
->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(context_helper::get_preload_record_columns_sql($contextalias))
->add_callback(static function(?string $description, stdClass $cohort): string {
global $CFG;
require_once("{$CFG->libdir}/filelib.php");
Expand All @@ -140,12 +148,14 @@ protected function get_all_columns(): array {
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);

return format_text($description, $cohort->descriptionformat, ['context' => $cohort->contextid]);
})
->set_is_sortable(false);
return format_text($description, $cohort->descriptionformat, ['context' => $context->id]);
});

// Visible column.
$columns[] = (new column(
Expand Down
24 changes: 14 additions & 10 deletions course/classes/local/entities/course_category.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace core_course\local\entities;

use context_coursecat;
use context_helper;
use lang_string;
use stdClass;
use core_course_category;
Expand Down Expand Up @@ -134,12 +136,12 @@ protected function get_all_columns(): array {
$this->get_entity_name()
))
->add_joins($this->get_joins())
->add_join("
JOIN {context} {$tablealiascontext}
ON {$tablealiascontext}.instanceid = {$tablealias}.id
AND {$tablealiascontext}.contextlevel = " . CONTEXT_COURSECAT)
->set_type(column::TYPE_TEXT)
->add_fields("{$tablealias}.description, {$tablealias}.descriptionformat, {$tablealiascontext}.id AS contextid")
->add_join("LEFT JOIN {context} {$tablealiascontext}
ON {$tablealiascontext}.contextlevel = " . CONTEXT_COURSECAT . "
AND {$tablealiascontext}.instanceid = {$tablealias}.id")
->set_type(column::TYPE_LONGTEXT)
->add_fields("{$tablealias}.description, {$tablealias}.descriptionformat, {$tablealias}.id")
->add_fields(context_helper::get_preload_record_columns_sql($tablealiascontext))
->add_callback(static function(?string $description, stdClass $category): string {
global $CFG;
require_once("{$CFG->libdir}/filelib.php");
Expand All @@ -148,12 +150,14 @@ protected function get_all_columns(): array {
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);

return format_text($description, $category->descriptionformat, ['context' => $category->contextid]);
})
->set_is_sortable(false);
return format_text($description, $category->descriptionformat, ['context' => $context->id]);
});

return $columns;
}
Expand Down

0 comments on commit 5087f56

Please sign in to comment.