diff --git a/admin/classes/reportbuilder/local/entities/task_log.php b/admin/classes/reportbuilder/local/entities/task_log.php index b276d591496..8cc6cdf2bfc 100644 --- a/admin/classes/reportbuilder/local/entities/task_log.php +++ b/admin/classes/reportbuilder/local/entities/task_log.php @@ -46,12 +46,14 @@ class task_log extends base { protected const FAILED = 1; /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['task_log' => 'tl']; + protected function get_default_tables(): array { + return [ + 'task_log', + ]; } /** diff --git a/admin/roles/classes/reportbuilder/local/entities/role.php b/admin/roles/classes/reportbuilder/local/entities/role.php index 34a699349c6..e83516c6f70 100644 --- a/admin/roles/classes/reportbuilder/local/entities/role.php +++ b/admin/roles/classes/reportbuilder/local/entities/role.php @@ -34,14 +34,14 @@ use core_reportbuilder\local\report\{column, filter}; class role extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'context' => 'rctx', - 'role' => 'r', + 'context', + 'role', ]; } diff --git a/badges/classes/reportbuilder/local/entities/badge.php b/badges/classes/reportbuilder/local/entities/badge.php index 4f5f42b1b2b..820a5c2ffcb 100644 --- a/badges/classes/reportbuilder/local/entities/badge.php +++ b/badges/classes/reportbuilder/local/entities/badge.php @@ -44,16 +44,16 @@ require_once("{$CFG->libdir}/badgeslib.php"); class badge extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'badge' => 'b', - 'context' => 'bctx', - 'tag_instance' => 'bti', - 'tag' => 'bt', + 'badge', + 'context', + 'tag_instance', + 'tag', ]; } diff --git a/badges/classes/reportbuilder/local/entities/badge_issued.php b/badges/classes/reportbuilder/local/entities/badge_issued.php index fd5df1d0bd8..4fb3a73463d 100644 --- a/badges/classes/reportbuilder/local/entities/badge_issued.php +++ b/badges/classes/reportbuilder/local/entities/badge_issued.php @@ -34,12 +34,14 @@ use core_reportbuilder\local\report\{column, filter}; class badge_issued extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['badge_issued' => 'bi']; + protected function get_default_tables(): array { + return [ + 'badge_issued', + ]; } /** diff --git a/blog/classes/reportbuilder/datasource/blogs.php b/blog/classes/reportbuilder/datasource/blogs.php index e6dac6a5858..ee89075d86b 100644 --- a/blog/classes/reportbuilder/datasource/blogs.php +++ b/blog/classes/reportbuilder/datasource/blogs.php @@ -88,23 +88,21 @@ class blogs extends datasource { $this->add_entity($courseentity ->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.id = {$postalias}.courseid")); - // Join the comment entity (ensure differing alias from that used by course entity). - $commententity = (new comment()) - ->set_table_alias('comments', 'bcmt'); + // Join the comment entity. + $commententity = new comment(); + $commentalias = $commententity->get_table_alias('comments'); $this->add_entity($commententity - ->add_join("LEFT JOIN {comments} bcmt ON bcmt.component = 'blog' AND bcmt.itemid = {$postalias}.id")); + ->add_join("LEFT JOIN {comments} {$commentalias} ON {$commentalias}.component = 'blog' + AND {$commentalias}.itemid = {$postalias}.id")); - // Join the user entity to represent the comment author. Override table aliases to avoid clash with first instance. + // Join the user entity to represent the comment author. $commenterentity = (new user()) ->set_entity_name('commenter') - ->set_entity_title(new lang_string('commenter', 'core_comment')) - ->set_table_aliases([ - 'user' => 'cu', - 'context' => 'cuctx', - ]); + ->set_entity_title(new lang_string('commenter', 'core_comment')); + $commenteralias = $commenterentity->get_table_alias('user'); $this->add_entity($commenterentity ->add_joins($commententity->get_joins()) - ->add_join("LEFT JOIN {user} cu ON cu.id = bcmt.userid")); + ->add_join("LEFT JOIN {user} {$commenteralias} ON {$commenteralias}.id = {$commentalias}.userid")); // Add report elements from each of the entities we added to the report. $this->add_all_from_entity($blogentity->get_entity_name()); diff --git a/blog/classes/reportbuilder/local/entities/blog.php b/blog/classes/reportbuilder/local/entities/blog.php index fa771641171..43ceb049a5e 100644 --- a/blog/classes/reportbuilder/local/entities/blog.php +++ b/blog/classes/reportbuilder/local/entities/blog.php @@ -38,15 +38,15 @@ use core_reportbuilder\local\report\{column, filter}; class blog extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'post' => 'bp', - 'tag_instance' => 'bti', - 'tag' => 'bt', + 'post', + 'tag_instance', + 'tag', ]; } diff --git a/cohort/classes/reportbuilder/local/entities/cohort.php b/cohort/classes/reportbuilder/local/entities/cohort.php index aae80cc9e36..d3e5b5517c5 100644 --- a/cohort/classes/reportbuilder/local/entities/cohort.php +++ b/cohort/classes/reportbuilder/local/entities/cohort.php @@ -42,14 +42,14 @@ use core_reportbuilder\local\report\filter; class cohort extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'cohort' => 'c', - 'context' => 'chctx', + 'cohort', + 'context', ]; } diff --git a/cohort/classes/reportbuilder/local/entities/cohort_member.php b/cohort/classes/reportbuilder/local/entities/cohort_member.php index 3d82cad7a28..3e16e994420 100644 --- a/cohort/classes/reportbuilder/local/entities/cohort_member.php +++ b/cohort/classes/reportbuilder/local/entities/cohort_member.php @@ -35,12 +35,14 @@ use core_reportbuilder\local\report\filter; class cohort_member extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['cohort_members' => 'cm']; + protected function get_default_tables(): array { + return [ + 'cohort_members', + ]; } /** diff --git a/comment/classes/reportbuilder/local/entities/comment.php b/comment/classes/reportbuilder/local/entities/comment.php index 8ae7d1d7443..659035e8b8d 100644 --- a/comment/classes/reportbuilder/local/entities/comment.php +++ b/comment/classes/reportbuilder/local/entities/comment.php @@ -38,14 +38,14 @@ use core_reportbuilder\local\report\{column, filter}; class comment extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'comments' => 'c', - 'context' => 'cmctx', + 'comments', + 'context', ]; } diff --git a/course/classes/reportbuilder/datasource/categories.php b/course/classes/reportbuilder/datasource/categories.php index 6957d759de9..4189c6cf035 100644 --- a/course/classes/reportbuilder/datasource/categories.php +++ b/course/classes/reportbuilder/datasource/categories.php @@ -60,13 +60,13 @@ class categories extends datasource { $this->add_entity($courseentity ->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.category = {$categoryalias}.id")); - // Join cohort entity (amend alias to avoid clash with course entity, indicate context table join alias). + // Join cohort entity (indicate context table join alias). $cohortentity = (new cohort()) - ->set_table_alias('cohort', 'ch') ->set_table_join_alias('context', $contextalias); + $cohort = $cohortentity->get_table_alias('cohort'); $this->add_entity($cohortentity ->add_join($categoryentity->get_context_join()) - ->add_join("LEFT JOIN {cohort} ch ON ch.contextid = {$contextalias}.id")); + ->add_join("LEFT JOIN {cohort} {$cohort} ON {$cohort}.contextid = {$contextalias}.id")); // Join role entity. $roleentity = (new role()) diff --git a/course/classes/reportbuilder/local/entities/access.php b/course/classes/reportbuilder/local/entities/access.php index 3b38073bb56..ee493be2f4c 100644 --- a/course/classes/reportbuilder/local/entities/access.php +++ b/course/classes/reportbuilder/local/entities/access.php @@ -36,12 +36,15 @@ use stdClass; class access extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['user_lastaccess' => 'ula', 'user' => 'u']; + protected function get_default_tables(): array { + return [ + 'user_lastaccess', + 'user', + ]; } /** diff --git a/course/classes/reportbuilder/local/entities/completion.php b/course/classes/reportbuilder/local/entities/completion.php index 0c21c4ab70d..38e9629598e 100644 --- a/course/classes/reportbuilder/local/entities/completion.php +++ b/course/classes/reportbuilder/local/entities/completion.php @@ -42,17 +42,17 @@ use stdClass; class completion extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'course_completion' => 'ccomp', - 'course' => 'c', - 'grade_grades' => 'gg', - 'grade_items' => 'gi', - 'user' => 'u', + 'course_completion', + 'course', + 'grade_grades' , + 'grade_items', + 'user', ]; } diff --git a/course/classes/reportbuilder/local/entities/course_category.php b/course/classes/reportbuilder/local/entities/course_category.php index 54e02e52210..965baf83035 100644 --- a/course/classes/reportbuilder/local/entities/course_category.php +++ b/course/classes/reportbuilder/local/entities/course_category.php @@ -40,14 +40,14 @@ use core_reportbuilder\local\report\filter; class course_category extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'context' => 'ccctx', - 'course_categories' => 'cc', + 'context', + 'course_categories', ]; } diff --git a/course/classes/reportbuilder/local/entities/enrolment.php b/course/classes/reportbuilder/local/entities/enrolment.php index 0d2ee75f7ac..b0ba4a47291 100644 --- a/course/classes/reportbuilder/local/entities/enrolment.php +++ b/course/classes/reportbuilder/local/entities/enrolment.php @@ -42,12 +42,15 @@ use stdClass; class enrolment extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['user_enrolments' => 'ue', 'enrol' => 'e']; + protected function get_default_tables(): array { + return [ + 'user_enrolments', + 'enrol', + ]; } /** diff --git a/enrol/classes/reportbuilder/local/entities/enrol.php b/enrol/classes/reportbuilder/local/entities/enrol.php index 7d6f4eff505..e3d8236029f 100644 --- a/enrol/classes/reportbuilder/local/entities/enrol.php +++ b/enrol/classes/reportbuilder/local/entities/enrol.php @@ -34,13 +34,13 @@ use core_reportbuilder\local\report\{column, filter}; class enrol extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'enrol' => 'e', + 'enrol', ]; } diff --git a/files/classes/reportbuilder/local/entities/file.php b/files/classes/reportbuilder/local/entities/file.php index a598567668c..489fe73ef29 100644 --- a/files/classes/reportbuilder/local/entities/file.php +++ b/files/classes/reportbuilder/local/entities/file.php @@ -41,14 +41,14 @@ use core_reportbuilder\local\report\{column, filter}; class file extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'files' => 'f', - 'context' => 'fctx', + 'files', + 'context', ]; } diff --git a/grade/report/summary/classes/local/entities/grade_items.php b/grade/report/summary/classes/local/entities/grade_items.php index 1733a0c0df3..38c7951f625 100644 --- a/grade/report/summary/classes/local/entities/grade_items.php +++ b/grade/report/summary/classes/local/entities/grade_items.php @@ -59,12 +59,14 @@ class grade_items extends base { } /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['grade_items' => 'gi']; + protected function get_default_tables(): array { + return [ + 'grade_items', + ]; } /** diff --git a/group/classes/reportbuilder/local/entities/group.php b/group/classes/reportbuilder/local/entities/group.php index caeb2825969..ad0097ca1c5 100644 --- a/group/classes/reportbuilder/local/entities/group.php +++ b/group/classes/reportbuilder/local/entities/group.php @@ -44,14 +44,14 @@ require_once("{$CFG->libdir}/grouplib.php"); class group extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'context' => 'gctx', - 'groups' => 'g', + 'context', + 'groups', ]; } diff --git a/group/classes/reportbuilder/local/entities/group_member.php b/group/classes/reportbuilder/local/entities/group_member.php index 0139bee11ef..8eea70e2d4d 100644 --- a/group/classes/reportbuilder/local/entities/group_member.php +++ b/group/classes/reportbuilder/local/entities/group_member.php @@ -34,13 +34,13 @@ use core_reportbuilder\local\report\{column, filter}; class group_member extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'groups_members' => 'gm', + 'groups_members', ]; } diff --git a/group/classes/reportbuilder/local/entities/grouping.php b/group/classes/reportbuilder/local/entities/grouping.php index 5dcea958553..39570820bc0 100644 --- a/group/classes/reportbuilder/local/entities/grouping.php +++ b/group/classes/reportbuilder/local/entities/grouping.php @@ -37,14 +37,14 @@ use core_reportbuilder\local\report\{column, filter}; class grouping extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'context' => 'ggctx', - 'groupings' => 'gg', + 'context', + 'groupings', ]; } diff --git a/lib/classes/reportbuilder/local/entities/context.php b/lib/classes/reportbuilder/local/entities/context.php index 2e085109a66..9acf790ee68 100644 --- a/lib/classes/reportbuilder/local/entities/context.php +++ b/lib/classes/reportbuilder/local/entities/context.php @@ -36,12 +36,14 @@ use stdClass; class context extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['context' => 'ctx']; + protected function get_default_tables(): array { + return [ + 'context', + ]; } /** diff --git a/mod/lti/classes/reportbuilder/local/entities/tool_types.php b/mod/lti/classes/reportbuilder/local/entities/tool_types.php index 82d719cdb6b..4780f4156e6 100644 --- a/mod/lti/classes/reportbuilder/local/entities/tool_types.php +++ b/mod/lti/classes/reportbuilder/local/entities/tool_types.php @@ -35,12 +35,15 @@ use core_reportbuilder\local\report\filter; class tool_types extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['lti_types' => 'tt', 'lti' => 'ti']; + protected function get_default_tables(): array { + return [ + 'lti_types', + 'lti', + ]; } /** diff --git a/notes/classes/reportbuilder/datasource/notes.php b/notes/classes/reportbuilder/datasource/notes.php index c209b9cc2c1..ff1b68f60fa 100644 --- a/notes/classes/reportbuilder/datasource/notes.php +++ b/notes/classes/reportbuilder/datasource/notes.php @@ -68,17 +68,14 @@ class notes extends datasource { ON {$recipientalias}.id = {$postalias}.userid") ); - // Join the user entity to represent the note author. Override all entity table aliases to avoid clash with first instance. + // Join the user entity to represent the note author. $authorentity = (new user()) ->set_entity_name('author') - ->set_entity_title(new lang_string('author', 'core_notes')) - ->set_table_aliases([ - 'user' => 'au', - 'context' => 'auctx', - ]); + ->set_entity_title(new lang_string('author', 'core_notes')); + $authoralias = $authorentity->get_table_alias('user'); $this->add_entity($authorentity->add_join(" - LEFT JOIN {user} au - ON au.id = {$postalias}.usermodified") + LEFT JOIN {user} {$authoralias} + ON {$authoralias}.id = {$postalias}.usermodified") ); // Join the course entity for course notes. diff --git a/notes/classes/reportbuilder/local/entities/note.php b/notes/classes/reportbuilder/local/entities/note.php index d8b421c45e6..0bc05b30fb1 100644 --- a/notes/classes/reportbuilder/local/entities/note.php +++ b/notes/classes/reportbuilder/local/entities/note.php @@ -40,12 +40,14 @@ require_once("{$CFG->dirroot}/notes/lib.php"); class note extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['post' => 'np']; + protected function get_default_tables(): array { + return [ + 'post', + ]; } /** diff --git a/report/configlog/classes/reportbuilder/local/entities/config_change.php b/report/configlog/classes/reportbuilder/local/entities/config_change.php index 5d3665c131f..adfcc9870e9 100644 --- a/report/configlog/classes/reportbuilder/local/entities/config_change.php +++ b/report/configlog/classes/reportbuilder/local/entities/config_change.php @@ -36,12 +36,14 @@ use core_reportbuilder\local\filters\text; class config_change extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['config_log' => 'cl']; + protected function get_default_tables(): array { + return [ + 'config_log', + ]; } /** diff --git a/reportbuilder/classes/local/entities/base.php b/reportbuilder/classes/local/entities/base.php index 29a56665547..356353022fa 100644 --- a/reportbuilder/classes/local/entities/base.php +++ b/reportbuilder/classes/local/entities/base.php @@ -19,9 +19,10 @@ declare(strict_types=1); namespace core_reportbuilder\local\entities; use coding_exception; -use lang_string; +use core_reportbuilder\local\helpers\database; use core_reportbuilder\local\report\column; use core_reportbuilder\local\report\filter; +use lang_string; /** * Base class for all report entities @@ -57,14 +58,41 @@ abstract class base { private $conditions = []; /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * * Must be overridden by the entity to list all database tables that it expects to be present in the main * SQL or in JOINs added to this entity * - * @return string[] Array of $tablename => $alias + * @todo in Moodle 4.8 - make abstract when support for {@see get_default_table_aliases} is finally removed + * + * @return string[] */ - abstract protected function get_default_table_aliases(): array; + protected function get_default_tables(): array { + static $debuggingshown; + + // The default implementation falls back to retrieving deprecated table aliases to determine our table names. + $tablenamealiases = $this->get_default_table_aliases(); + if (!empty($tablenamealiases) && !$debuggingshown) { + debugging('The function get_default_table_aliases() is deprecated, please define the entity' . + ' tables with get_default_tables() in ' . static::class, DEBUG_DEVELOPER); + + // Don't be too spammy with the debugging, this method is called multiple times per entity load. + $debuggingshown = true; + } + + return array_keys($tablenamealiases); + } + + /** + * Database tables that this entity uses and their default aliases (note that these aliases are now ignored) + * + * @return string[] Array of $tablename => $alias + * + * @deprecated since Moodle 4.4 - aliases are now autogenerated, please implement {@see get_default_tables} instead + */ + protected function get_default_table_aliases(): array { + return []; + } /** * The default title for this entity @@ -137,16 +165,17 @@ abstract class base { } /** - * Override the default alias for given database table used in entity queries, to avoid table alias clashes that may occur - * if multiple entities of a report each define the same default alias for one of their tables + * 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 * - * @param string $tablename + * @param string $tablename One of the tables set by {@see get_default_tables} * @param string $alias * @return self - * @throws coding_exception + * @throws coding_exception For invalid table name */ final public function set_table_alias(string $tablename, string $alias): self { - if (!array_key_exists($tablename, $this->get_default_table_aliases())) { + $tablenames = $this->get_default_tables(); + if (!in_array($tablename, $tablenames)) { throw new coding_exception('Invalid table name', $tablename); } @@ -155,9 +184,7 @@ abstract class base { } /** - * Override multiple default database table aliases used in entity queries as per {@see set_table_alias}, typically when - * you're adding an entity multiple times to a report you'd want to override the table aliases in the second instance to - * avoid clashes with the first + * Override multiple default database table aliases used in entity queries as per {@see set_table_alias} * * @param array $aliases Array of tablename => alias values * @return self @@ -172,17 +199,22 @@ abstract class base { /** * Returns an alias used in the queries for a given table * - * @param string $tablename + * @param string $tablename One of the tables set by {@see get_default_tables} * @return string - * @throws coding_exception + * @throws coding_exception For invalid table name */ final public function get_table_alias(string $tablename): string { - $defaulttablealiases = $this->get_default_table_aliases(); - if (!array_key_exists($tablename, $defaulttablealiases)) { + $tablenames = $this->get_default_tables(); + if (array_search($tablename, $tablenames) === false) { throw new coding_exception('Invalid table name', $tablename); } - return $this->tablealiases[$tablename] ?? $defaulttablealiases[$tablename]; + // We don't have the alias yet, generate a new one. + if (!array_key_exists($tablename, $this->tablealiases)) { + $this->set_table_alias($tablename, database::generate_alias()); + } + + return $this->tablealiases[$tablename]; } /** @@ -246,7 +278,7 @@ abstract class base { /** * 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 + * Both 'tag' and 'tag_instance' aliases must be returned by the entity {@see get_default_tables} method * * @param string $component * @param string $itemtype diff --git a/reportbuilder/classes/local/entities/course.php b/reportbuilder/classes/local/entities/course.php index 787264c6360..20f8b67b5e3 100644 --- a/reportbuilder/classes/local/entities/course.php +++ b/reportbuilder/classes/local/entities/course.php @@ -50,16 +50,16 @@ require_once($CFG->dirroot . '/course/lib.php'); class course extends base { /** - * Database tables that this entity uses and their default aliases. + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'course' => 'c', - 'context' => 'cctx', - 'tag_instance' => 'cti', - 'tag' => 'ct', + 'course', + 'context', + 'tag_instance', + 'tag', ]; } diff --git a/reportbuilder/classes/local/entities/user.php b/reportbuilder/classes/local/entities/user.php index 5b7c3299a04..dc5ff943322 100644 --- a/reportbuilder/classes/local/entities/user.php +++ b/reportbuilder/classes/local/entities/user.php @@ -50,16 +50,16 @@ use core_reportbuilder\local\report\filter; class user extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'user' => 'u', - 'context' => 'uctx', - 'tag_instance' => 'uti', - 'tag' => 'ut', + 'user', + 'context', + 'tag_instance', + 'tag', ]; } diff --git a/reportbuilder/tests/local/entities/base_test.php b/reportbuilder/tests/local/entities/base_test.php index 38e9ff784a8..ae5d5bf69d6 100644 --- a/reportbuilder/tests/local/entities/base_test.php +++ b/reportbuilder/tests/local/entities/base_test.php @@ -50,7 +50,19 @@ class base_test extends advanced_testcase { */ public function test_get_table_alias(): void { $entity = new base_test_entity(); - $this->assertEquals('m', $entity->get_table_alias('mytable')); + + $mytablealias = $entity->get_table_alias('mytable'); + $this->assertMatchesRegularExpression('/^rbalias(\d+)$/', $mytablealias); + + $myothertablealias = $entity->get_table_alias('myothertable'); + $this->assertMatchesRegularExpression('/^rbalias(\d+)$/', $myothertablealias); + + // They must differ. + $this->assertNotEquals($mytablealias, $myothertablealias); + + // Re-request both, ensure they are identical to what we previously received. + $this->assertEquals($mytablealias, $entity->get_table_alias('mytable')); + $this->assertEquals($myothertablealias, $entity->get_table_alias('myothertable')); } /** @@ -314,10 +326,10 @@ class base_test_entity extends base { * * @return array */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'mytable' => 'm', - 'myothertable' => 'o', + 'mytable', + 'myothertable', ]; } diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt index efb95580059..6e80fc1fe70 100644 --- a/reportbuilder/upgrade.txt +++ b/reportbuilder/upgrade.txt @@ -4,6 +4,8 @@ Information provided here is intended especially for developers. === 4.4 === * New methods `get_identity_[columns|filters]` in user entity, for retrieving all user identity field report elements +* Entity table aliases are now auto-generated, hence usage of the `get_default_table_aliases` method is now deprecated. Instead, + entities should implement the `get_default_tables` method to define the tables they use * The database helper `generate_alias[es]` and `generate_param_name[s]` methods now accept an optional `$suffix` argument for appending additional string to the generated value * New report filter types: diff --git a/tag/classes/reportbuilder/local/entities/collection.php b/tag/classes/reportbuilder/local/entities/collection.php index 05c8f81ea7e..4bfb55de4a6 100644 --- a/tag/classes/reportbuilder/local/entities/collection.php +++ b/tag/classes/reportbuilder/local/entities/collection.php @@ -36,12 +36,14 @@ use core_reportbuilder\local\report\{column, filter}; class collection extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['tag_coll' => 'tc']; + protected function get_default_tables(): array { + return [ + 'tag_coll', + ]; } /** diff --git a/tag/classes/reportbuilder/local/entities/instance.php b/tag/classes/reportbuilder/local/entities/instance.php index d1e89cd44cf..46cceeaa8be 100644 --- a/tag/classes/reportbuilder/local/entities/instance.php +++ b/tag/classes/reportbuilder/local/entities/instance.php @@ -40,14 +40,14 @@ use core_reportbuilder\local\report\{column, filter}; class instance extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { + protected function get_default_tables(): array { return [ - 'tag_instance' => 'ti', - 'context' => 'tictx', + 'tag_instance', + 'context', ]; } diff --git a/tag/classes/reportbuilder/local/entities/tag.php b/tag/classes/reportbuilder/local/entities/tag.php index ebc9395e5cf..e132c6a0104 100644 --- a/tag/classes/reportbuilder/local/entities/tag.php +++ b/tag/classes/reportbuilder/local/entities/tag.php @@ -38,12 +38,14 @@ use core_reportbuilder\local\report\{column, filter}; class tag extends base { /** - * Database tables that this entity uses and their default aliases + * Database tables that this entity uses * - * @return array + * @return string[] */ - protected function get_default_table_aliases(): array { - return ['tag' => 't']; + protected function get_default_tables(): array { + return [ + 'tag', + ]; } /**