This commit is contained in:
Sara Arjona
2024-01-10 16:10:08 +01:00
4 changed files with 42 additions and 8 deletions
@@ -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(
+12 -1
View File
@@ -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}
@@ -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());
}
/**
+1
View File
@@ -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: