diff --git a/blog/classes/reportbuilder/local/entities/blog.php b/blog/classes/reportbuilder/local/entities/blog.php index 0c4af13975d..71051f0f2ec 100644 --- a/blog/classes/reportbuilder/local/entities/blog.php +++ b/blog/classes/reportbuilder/local/entities/blog.php @@ -305,17 +305,6 @@ class blog extends base { * @return string[] */ public function get_tag_joins(): array { - $postalias = $this->get_table_alias('post'); - $taginstancealias = $this->get_table_alias('tag_instance'); - $tagalias = $this->get_table_alias('tag'); - - return [ - "LEFT JOIN {tag_instance} {$taginstancealias} - ON {$taginstancealias}.component = 'core' - AND {$taginstancealias}.itemtype = 'post' - AND {$taginstancealias}.itemid = {$postalias}.id", - "LEFT JOIN {tag} {$tagalias} - ON {$tagalias}.id = {$taginstancealias}.tagid", - ]; + return $this->get_tag_joins_for_entity('core', 'post', $this->get_table_alias('post') . '.id'); } } diff --git a/reportbuilder/classes/local/entities/base.php b/reportbuilder/classes/local/entities/base.php index a32dfc02729..1dfcacefe94 100644 --- a/reportbuilder/classes/local/entities/base.php +++ b/reportbuilder/classes/local/entities/base.php @@ -215,6 +215,30 @@ abstract class base { return array_values($this->joins); } + /** + * Helper method for returning joins necessary for retrieving tags related to the current entity + * + * Both 'tag' and 'tag_instance' aliases must be returned by the entity {@see get_default_table_aliases} method + * + * @param string $component + * @param string $itemtype + * @param string $itemidfield + * @return string[] + */ + final protected function get_tag_joins_for_entity(string $component, string $itemtype, string $itemidfield): array { + $taginstancealias = $this->get_table_alias('tag_instance'); + $tagalias = $this->get_table_alias('tag'); + + return [ + "LEFT JOIN {tag_instance} {$taginstancealias} + ON {$taginstancealias}.component = '{$component}' + AND {$taginstancealias}.itemtype = '{$itemtype}' + AND {$taginstancealias}.itemid = {$itemidfield}", + "LEFT JOIN {tag} {$tagalias} + ON {$tagalias}.id = {$taginstancealias}.tagid", + ]; + } + /** * Add a column to the entity * diff --git a/reportbuilder/classes/local/entities/course.php b/reportbuilder/classes/local/entities/course.php index 5532f209f1f..787264c6360 100644 --- a/reportbuilder/classes/local/entities/course.php +++ b/reportbuilder/classes/local/entities/course.php @@ -206,18 +206,7 @@ class course extends base { * @return string[] */ public function get_tag_joins(): array { - $course = $this->get_table_alias('course'); - $taginstance = $this->get_table_alias('tag_instance'); - $tag = $this->get_table_alias('tag'); - - return [ - "LEFT JOIN {tag_instance} {$taginstance} - ON {$taginstance}.component = 'core' - AND {$taginstance}.itemtype = 'course' - AND {$taginstance}.itemid = {$course}.id", - "LEFT JOIN {tag} {$tag} - ON {$tag}.id = {$taginstance}.tagid", - ]; + return $this->get_tag_joins_for_entity('core', 'course', $this->get_table_alias('course') . '.id'); } /** diff --git a/reportbuilder/classes/local/entities/user.php b/reportbuilder/classes/local/entities/user.php index 881fd02d713..4e26f92d439 100644 --- a/reportbuilder/classes/local/entities/user.php +++ b/reportbuilder/classes/local/entities/user.php @@ -144,18 +144,7 @@ class user extends base { * @return string[] */ public function get_tag_joins(): array { - $user = $this->get_table_alias('user'); - $taginstance = $this->get_table_alias('tag_instance'); - $tag = $this->get_table_alias('tag'); - - return [ - "LEFT JOIN {tag_instance} {$taginstance} - ON {$taginstance}.component = 'core' - AND {$taginstance}.itemtype = 'user' - AND {$taginstance}.itemid = {$user}.id", - "LEFT JOIN {tag} {$tag} - ON {$tag}.id = {$taginstance}.tagid", - ]; + return $this->get_tag_joins_for_entity('core', 'user', $this->get_table_alias('user') . '.id'); } /** diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt index f38b06c36d1..384a7c2970a 100644 --- a/reportbuilder/upgrade.txt +++ b/reportbuilder/upgrade.txt @@ -3,6 +3,7 @@ Information provided here is intended especially for developers. === 4.3 === +* New `get_tag_joins_for_entity` helper in base entity class, for returning SQL joins necessary for retrieving tags * New `set_is_deprecated` method in base `local\report\[column|filter]` classes to deprecate report entity columns and filters * The following report entity columns have been deprecated, with replacements as follows: - `enrolment:method` => `enrol:name` (plus enrolment formatter `enrolment_name` method)