Skip to content

Commit

Permalink
MDL-75810 reportbuilder: entity method for returning tag SQL joins.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Jun 16, 2023
1 parent 697be4e commit e8ae968
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 36 deletions.
13 changes: 1 addition & 12 deletions blog/classes/reportbuilder/local/entities/blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,6 @@ protected function get_all_filters(): array {
* @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');
}
}
24 changes: 24 additions & 0 deletions reportbuilder/classes/local/entities/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,30 @@ final public function get_joins(): array {
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
*
Expand Down
13 changes: 1 addition & 12 deletions reportbuilder/classes/local/entities/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,7 @@ protected function get_course_field_type(string $coursefield): int {
* @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');
}

/**
Expand Down
13 changes: 1 addition & 12 deletions reportbuilder/classes/local/entities/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,7 @@ public function get_identity_filter(string $identityfield): filter {
* @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');
}

/**
Expand Down
1 change: 1 addition & 0 deletions reportbuilder/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e8ae968

Please sign in to comment.