diff --git a/course/classes/reportbuilder/local/entities/completion.php b/course/classes/reportbuilder/local/entities/completion.php index 38e9629598e..e3fa21df4ce 100644 --- a/course/classes/reportbuilder/local/entities/completion.php +++ b/course/classes/reportbuilder/local/entities/completion.php @@ -91,11 +91,13 @@ class completion extends base { * @return column[] */ protected function get_all_columns(): array { - $coursecompletion = $this->get_table_alias('course_completion'); - $course = $this->get_table_alias('course'); - $grade = $this->get_table_alias('grade_grades'); - $gradeitem = $this->get_table_alias('grade_items'); - $user = $this->get_table_alias('user'); + [ + 'course_completion' => $coursecompletion, + 'course' => $course, + 'grade_grades' => $grade, + 'grade_items' => $gradeitem, + 'user' => $user, + ] = $this->get_table_aliases(); // Completed column. $columns[] = (new column( diff --git a/reportbuilder/classes/local/entities/base.php b/reportbuilder/classes/local/entities/base.php index 356353022fa..a24ddaaee27 100644 --- a/reportbuilder/classes/local/entities/base.php +++ b/reportbuilder/classes/local/entities/base.php @@ -205,7 +205,7 @@ abstract class base { */ final public function get_table_alias(string $tablename): string { $tablenames = $this->get_default_tables(); - if (array_search($tablename, $tablenames) === false) { + if (!in_array($tablename, $tablenames)) { throw new coding_exception('Invalid table name', $tablename); } @@ -217,6 +217,17 @@ abstract class base { return $this->tablealiases[$tablename]; } + /** + * Returns aliases used in the queries for all tables + * + * @return string[] + */ + final public function get_table_aliases(): array { + $tablenames = $this->get_default_tables(); + + return array_combine($tablenames, array_map([$this, 'get_table_alias'], $tablenames)); + } + /** * Set the alias for given database table that has already been added to the report. Enables entities to avoid additional * joins on the same table by allowing re-use of existing table aliases in their own queries, {@see has_table_join_alias} diff --git a/reportbuilder/tests/local/entities/base_test.php b/reportbuilder/tests/local/entities/base_test.php index ae5d5bf69d6..58330dbeb72 100644 --- a/reportbuilder/tests/local/entities/base_test.php +++ b/reportbuilder/tests/local/entities/base_test.php @@ -77,6 +77,24 @@ class base_test extends advanced_testcase { $entity->get_table_alias('nonexistingalias'); } + /** + * Test getting all table aliases + */ + public function test_get_table_aliases(): void { + $entity = new base_test_entity(); + + [ + 'mytable' => $mytablealias, + 'myothertable' => $myothertablealias, + ] = $entity->get_table_aliases(); + + $this->assertMatchesRegularExpression('/^rbalias(\d+)$/', $mytablealias); + $this->assertMatchesRegularExpression('/^rbalias(\d+)$/', $myothertablealias); + + // They must differ. + $this->assertNotEquals($mytablealias, $myothertablealias); + } + /** * Test setting table alias */ @@ -108,8 +126,10 @@ class base_test extends advanced_testcase { 'mytable' => 'newalias', 'myothertable' => 'newalias2', ]); - $this->assertEquals('newalias', $entity->get_table_alias('mytable')); - $this->assertEquals('newalias2', $entity->get_table_alias('myothertable')); + $this->assertEquals([ + 'mytable' => 'newalias', + 'myothertable' => 'newalias2', + ], $entity->get_table_aliases()); } /** diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt index 6e80fc1fe70..3e5aef3fc6a 100644 --- a/reportbuilder/upgrade.txt +++ b/reportbuilder/upgrade.txt @@ -6,6 +6,7 @@ Information provided here is intended especially for developers. * 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 +* New method `get_table_aliases` in base entity class, for retrieving all table aliases in a single call * 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: