This commit is contained in:
Jun Pataleta
2024-10-31 11:21:41 +07:00
committed by Huong Nguyen
7 changed files with 86 additions and 235 deletions
@@ -0,0 +1,32 @@
issueNumber: MDL-78118
notes:
core_reportbuilder:
- message: >-
New `get_deprecated_tables` method in base entity, to be overridden when
an entity no longer uses a table (due to column/filter re-factoring,
etc) in order to avoid breaking third-party reports
type: improved
- message: >-
The following deprecated report entity elements have been removed:
- `comment:context`
- `comment:contexturl`
- `enrolment:method` (plus enrolment formatter `enrolment_name` method)
- 'enrolment:role`
- `file:context`
- `file:contexturl`
- `instance:context` (tag)
- `instance:contexturl` (tag)
Use of the `context` table is also deprecated in the `file` and
`instance` (tag) entities
type: removed
@@ -20,7 +20,6 @@ namespace core_comment\reportbuilder\local\entities;
use context;
use context_helper;
use html_writer;
use lang_string;
use stdClass;
use core_reportbuilder\local\entities\base;
@@ -118,52 +117,6 @@ class comment extends base {
return format_text($content, $comment->format, ['context' => $context]);
});
// Context.
$columns[] = (new column(
'context',
new lang_string('context'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_join($this->get_context_join())
->add_fields("{$commentalias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias))
// Sorting may not order alphabetically, but will at least group contexts together.
->set_is_sortable(true)
->set_is_deprecated('See \'context:name\' for replacement')
->add_callback(static function($contextid, stdClass $context): string {
if ($contextid === null) {
return '';
}
context_helper::preload_from_record($context);
return context::instance_by_id($contextid)->get_context_name();
});
// Context URL.
$columns[] = (new column(
'contexturl',
new lang_string('contexturl'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_join($this->get_context_join())
->add_fields("{$commentalias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias))
// Sorting may not order alphabetically, but will at least group contexts together.
->set_is_sortable(true)
->set_is_deprecated('See \'context:link\' for replacement')
->add_callback(static function($contextid, stdClass $context): string {
if ($contextid === null) {
return '';
}
context_helper::preload_from_record($context);
$context = context::instance_by_id($contextid);
return html_writer::link($context->get_url(), $context->get_context_name());
});
// Component.
$columns[] = (new column(
'component',
@@ -89,20 +89,6 @@ class enrolment extends base {
*/
protected function get_all_columns(): array {
$userenrolments = $this->get_table_alias('user_enrolments');
$enrol = $this->get_table_alias('enrol');
// Enrolment method column (Deprecated since Moodle 4.3, to remove in MDL-78118).
$columns[] = (new column(
'method',
new lang_string('method', 'enrol'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_fields("{$enrol}.enrol, {$enrol}.id")
->set_is_sortable(true)
->set_is_deprecated('See \'enrol:name\' for replacement')
->add_callback([enrolment_formatter::class, 'enrolment_name']);
// Enrolment time created.
$columns[] = (new column(
@@ -156,33 +142,6 @@ class enrolment extends base {
->set_is_sortable(true)
->add_callback([enrolment_formatter::class, 'enrolment_status']);
// Role column (Deprecated since Moodle 4.3, to remove in MDL-78118).
$ctx = database::generate_alias();
$ra = database::generate_alias();
$r = database::generate_alias();
$columns[] = (new column(
'role',
new lang_string('role', 'moodle'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->add_join("LEFT JOIN {context} {$ctx}
ON {$ctx}.instanceid = {$enrol}.courseid AND {$ctx}.contextlevel = " . CONTEXT_COURSE)
->add_join("LEFT JOIN {role_assignments} {$ra}
ON {$ra}.contextid = {$ctx}.id AND {$ra}.userid = {$userenrolments}.userid")
->add_join("LEFT JOIN {role} {$r} ON {$r}.id = {$ra}.roleid")
->set_type(column::TYPE_TEXT)
->add_fields("{$r}.id, {$r}.name, {$r}.shortname, {$ctx}.instanceid")
->set_is_sortable(true, ["{$r}.shortname"])
->set_is_deprecated('See \'role:name\' for replacement')
->add_callback(static function(?string $value, stdClass $row): string {
if (!$row->id) {
return '';
}
$context = context_course::instance($row->instanceid);
return role_get_name($row, $context, ROLENAME_ALIAS);
});
return $columns;
}
@@ -215,24 +174,6 @@ class enrolment extends base {
*/
protected function get_all_filters(): array {
$userenrolments = $this->get_table_alias('user_enrolments');
$enrol = $this->get_table_alias('enrol');
// Enrolment method (Deprecated since Moodle 4.3, to remove in MDL-78118).
$enrolmentmethods = static function(): array {
return array_map(static function(enrol_plugin $plugin): string {
return get_string('pluginname', 'enrol_' . $plugin->get_name());
}, enrol_get_plugins(true));
};
$filters[] = (new filter(
select::class,
'method',
new lang_string('method', 'enrol'),
$this->get_entity_name(),
"{$enrol}.enrol"
))
->add_joins($this->get_joins())
->set_is_deprecated('See \'enrol:plugin\' for replacement')
->set_options_callback($enrolmentmethods);
// Enrolment time created.
$filters[] = (new filter(
@@ -20,7 +20,6 @@ namespace core_course\reportbuilder\local\formatters;
use core_user\output\status_field;
use lang_string;
use stdClass;
/**
* Formatters for the course enrolment entity
@@ -32,25 +31,11 @@ use stdClass;
class enrolment {
/**
* Return enrolment plugin instance name
*
* @param string|null $value
* @param stdClass $row
* @return string
*
* @deprecated since Moodle 4.3 - please do not use this function any more (to remove in MDL-78118)
*/
public static function enrolment_name(?string $value, stdClass $row): string {
global $DB;
if (empty($value)) {
return '';
}
$instance = $DB->get_record('enrol', ['id' => $row->id, 'enrol' => $row->enrol], '*', MUST_EXIST);
$plugin = enrol_get_plugin($row->enrol);
return $plugin ? $plugin->get_instance_name($instance) : '-';
#[\core\attribute\deprecated(null, reason: 'It is no longer used', since: '4.3', mdl: 'MDL-76900', final: true)]
public static function enrolment_name(): void {
\core\deprecation::emit_deprecation_if_present([self::class, __FUNCTION__]);
}
/**
@@ -18,11 +18,8 @@ declare(strict_types=1);
namespace core_files\reportbuilder\local\entities;
use context;
use context_helper;
use core_collator;
use core_filetypes;
use html_writer;
use lang_string;
use license_manager;
use stdClass;
@@ -48,6 +45,16 @@ class file extends base {
protected function get_default_tables(): array {
return [
'files',
];
}
/**
* Database tables that this entity no longer uses
*
* @return string[]
*/
protected function get_deprecated_tables(): array {
return [
'context',
];
}
@@ -90,7 +97,6 @@ class file extends base {
*/
protected function get_all_columns(): array {
$filesalias = $this->get_table_alias('files');
$contextalias = $this->get_table_alias('context');
// Name.
$columns[] = (new column(
@@ -220,52 +226,6 @@ class file extends base {
return $licenses[$license]->fullname;
});
// Context.
$columns[] = (new column(
'context',
new lang_string('context'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_join("LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$filesalias}.contextid")
->add_fields("{$filesalias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias))
// Sorting may not order alphabetically, but will at least group contexts together.
->set_is_sortable(true)
->set_is_deprecated('See \'context:name\' for replacement')
->add_callback(static function($contextid, stdClass $context): string {
if ($contextid === null) {
return '';
}
context_helper::preload_from_record($context);
return context::instance_by_id($contextid)->get_context_name();
});
// Context link.
$columns[] = (new column(
'contexturl',
new lang_string('contexturl'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_join("LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$filesalias}.contextid")
->add_fields("{$filesalias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias))
// Sorting may not order alphabetically, but will at least group contexts together.
->set_is_sortable(true)
->set_is_deprecated('See \'context:link\' for replacement')
->add_callback(static function($contextid, stdClass $context): string {
if ($contextid === null) {
return '';
}
context_helper::preload_from_record($context);
$context = context::instance_by_id($contextid);
return html_writer::link($context->get_url(), $context->get_context_name());
});
// Content hash.
$columns[] = (new column(
'contenthash',
+31 -11
View File
@@ -93,6 +93,16 @@ abstract class base {
return [];
}
/**
* Database tables that the entity once used but now no longer does. To prevent errors in third-party code, rather than
* simply removing the table from {@see get_default_tables} you can override this method, which will emit developer debug
*
* @return string[]
*/
protected function get_deprecated_tables(): array {
return [];
}
/**
* The default title for this entity
*
@@ -163,6 +173,24 @@ abstract class base {
return $this->entitytitle ?? $this->get_default_entity_title();
}
/**
* Validate the given table is expected by the entity
*
* @param string $tablename
* @throws coding_exception For invalid table name
*/
private function validate_table_name(string $tablename): void {
$deprecatedtables = $this->get_deprecated_tables();
if (!in_array($tablename, array_merge($this->get_default_tables(), $deprecatedtables))) {
throw new coding_exception('Invalid table name', $tablename);
}
// Emit debugging if table is marked as deprecated by the entity.
if (in_array($tablename, $deprecatedtables)) {
debugging("The table '{$tablename}' is deprecated, please do not use it any more.", DEBUG_DEVELOPER);
}
}
/**
* Override the default alias for given database table used in entity queries, for instance when the same table is used
* by multiple entities and you want them each to refer to it by the same alias
@@ -170,15 +198,11 @@ abstract class base {
* @param string $tablename One of the tables set by {@see get_default_tables}
* @param string $alias
* @return self
* @throws coding_exception For invalid table name
*/
final public function set_table_alias(string $tablename, string $alias): self {
$tablenames = $this->get_default_tables();
if (!in_array($tablename, $tablenames)) {
throw new coding_exception('Invalid table name', $tablename);
}
$this->validate_table_name($tablename);
$this->tablealiases[$tablename] = $alias;
return $this;
}
@@ -200,13 +224,9 @@ abstract class base {
*
* @param string $tablename One of the tables set by {@see get_default_tables}
* @return string
* @throws coding_exception For invalid table name
*/
final public function get_table_alias(string $tablename): string {
$tablenames = $this->get_default_tables();
if (!in_array($tablename, $tablenames)) {
throw new coding_exception('Invalid table name', $tablename);
}
$this->validate_table_name($tablename);
// We don't have the alias yet, generate a new one.
if (!array_key_exists($tablename, $this->tablealiases)) {
@@ -18,11 +18,8 @@ declare(strict_types=1);
namespace core_tag\reportbuilder\local\entities;
use context;
use context_helper;
use core_collator;
use core_tag_area;
use html_writer;
use lang_string;
use stdClass;
use core_reportbuilder\local\entities\base;
@@ -47,6 +44,16 @@ class instance extends base {
protected function get_default_tables(): array {
return [
'tag_instance',
];
}
/**
* Database tables that this entity no longer uses
*
* @return string[]
*/
protected function get_deprecated_tables(): array {
return [
'context',
];
}
@@ -89,7 +96,6 @@ class instance extends base {
*/
protected function get_all_columns(): array {
$instancealias = $this->get_table_alias('tag_instance');
$contextalias = $this->get_table_alias('context');
// Area.
$columns[] = (new column(
@@ -109,52 +115,6 @@ class instance extends base {
return (string) core_tag_area::display_name($area->component, $area->itemtype);
});
// Context.
$columns[] = (new column(
'context',
new lang_string('context'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_join("LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$instancealias}.contextid")
->add_fields("{$instancealias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias))
// Sorting may not order alphabetically, but will at least group contexts together.
->set_is_sortable(true)
->set_is_deprecated('See \'context:name\' for replacement')
->add_callback(static function($contextid, stdClass $context): string {
if ($contextid === null) {
return '';
}
context_helper::preload_from_record($context);
return context::instance_by_id($contextid)->get_context_name();
});
// Context URL.
$columns[] = (new column(
'contexturl',
new lang_string('contexturl'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_join("LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$instancealias}.contextid")
->add_fields("{$instancealias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias))
// Sorting may not order alphabetically, but will at least group contexts together.
->set_is_sortable(true)
->set_is_deprecated('See \'context:link\' for replacement')
->add_callback(static function($contextid, stdClass $context): string {
if ($contextid === null) {
return '';
}
context_helper::preload_from_record($context);
$context = context::instance_by_id($contextid);
return html_writer::link($context->get_url(), $context->get_context_name());
});
// Component.
$columns[] = (new column(
'component',