This commit is contained in:
Andrew Nicols
2025-03-06 09:09:06 +08:00
6 changed files with 128 additions and 11 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ notes:
- `enrolment:method` (plus enrolment formatter `enrolment_name` method)
- 'enrolment:role`
- `enrolment:role`
- `file:context`
@@ -0,0 +1,7 @@
issueNumber: MDL-84135
notes:
core_reportbuilder:
- message: >-
Use of the `course_completion` table is deprecated in the `completion`
entity, please use `course_completions` instead
type: deprecated
@@ -132,7 +132,7 @@ class participants extends datasource {
'course' => $course,
'user' => $user,
]);
$completion = $completionentity->get_table_alias('course_completion');
$completion = $completionentity->get_table_alias('course_completions');
$this->add_entity($completionentity
->add_joins($userentity->get_joins())
->add_join("
@@ -48,14 +48,25 @@ class completion extends base {
*/
protected function get_default_tables(): array {
return [
'course_completion',
'course',
'course_completions',
'grade_grades' ,
'grade_items',
'user',
];
}
/**
* Database tables that this entity no longer uses
*
* @return string[]
*/
protected function get_deprecated_tables(): array {
return [
'course_completions' => 'course_completion',
];
}
/**
* The default title for this entity in the list of columns/conditions/filters in the report builder
*
@@ -92,8 +103,8 @@ class completion extends base {
*/
protected function get_all_columns(): array {
[
'course_completion' => $coursecompletion,
'course' => $course,
'course_completions' => $coursecompletion,
'grade_grades' => $grade,
'grade_items' => $gradeitem,
'user' => $user,
@@ -295,7 +306,7 @@ class completion extends base {
* @return filter[]
*/
protected function get_all_filters(): array {
$coursecompletion = $this->get_table_alias('course_completion');
$coursecompletion = $this->get_table_alias('course_completions');
// Completed status filter.
$filters[] = (new filter(
+18 -4
View File
@@ -75,6 +75,9 @@ abstract class base {
* Database tables that the entity once used but now no longer does. To prevent errors in third-party code, rather than
* simply removing the table from {@see get_default_tables} you can override this method, which will emit developer debug
*
* Returns a simple list of table names, ['t1', 't2'] if they have no replacement; or ['t3' => 't1'] if an equivalent
* replacement table name exists, where 't3' replaces 't1'
*
* @return string[]
*/
protected function get_deprecated_tables(): array {
@@ -154,19 +157,30 @@ abstract class base {
/**
* Validate the given table is expected by the entity
*
* Emits developer debugging for deprecated tables, will return replacement for deprecated table if specified
* by the entity
*
* @param string $tablename
* @return string
* @throws coding_exception For invalid table name
*/
private function validate_table_name(string $tablename): void {
private function validate_table_name(string $tablename): string {
$deprecatedtables = $this->get_deprecated_tables();
if (!in_array($tablename, array_merge($this->get_default_tables(), $deprecatedtables))) {
throw new coding_exception('Invalid table name', $tablename);
}
// Emit debugging if table is marked as deprecated by the entity.
if (in_array($tablename, $deprecatedtables)) {
if (($tablenamereplacement = array_search($tablename, $deprecatedtables)) !== false) {
debugging("The table '{$tablename}' is deprecated, please do not use it any more.", DEBUG_DEVELOPER);
// An associative array contains the replacement table name as the key, so return that.
if (!array_is_list($deprecatedtables)) {
return $tablenamereplacement;
}
}
return $tablename;
}
/**
@@ -178,7 +192,7 @@ abstract class base {
* @return self
*/
final public function set_table_alias(string $tablename, string $alias): self {
$this->validate_table_name($tablename);
$tablename = $this->validate_table_name($tablename);
$this->tablealiases[$tablename] = $alias;
return $this;
@@ -204,7 +218,7 @@ abstract class base {
* @return string
*/
final public function get_table_alias(string $tablename): string {
$this->validate_table_name($tablename);
$tablename = $this->validate_table_name($tablename);
// We don't have the alias yet, generate a new one.
if (!array_key_exists($tablename, $this->tablealiases)) {
@@ -65,6 +65,34 @@ final class base_test extends advanced_testcase {
$this->assertEquals($myothertablealias, $entity->get_table_alias('myothertable'));
}
/**
* Test for deprecated get table alias
*/
public function test_get_table_alias_deprecated(): void {
$entity = new base_test_entity();
$entity->get_table_alias('mydeprecatedtable');
// Debugging called twice, as get_table_alias internally calls set_table_alias for undefined alias.
$this->assertDebuggingCalledCount(2, [
'The table \'mydeprecatedtable\' is deprecated, please do not use it any more.',
'The table \'mydeprecatedtable\' is deprecated, please do not use it any more.',
]);
}
/**
* Test for deprecated get table alias replacement
*/
public function test_get_table_alias_deprecated_replacement(): void {
$entity = new base_test_entity_second();
$mydeprecatedtable = $entity->get_table_alias('mydeprecatedtable');
$this->assertDebuggingCalled();
// We should get back the same alias for the replacement table.
$this->assertEquals($mydeprecatedtable, $entity->get_table_alias('mytable'));
}
/**
* Test for invalid get table alias
*/
@@ -105,6 +133,35 @@ final class base_test extends advanced_testcase {
$this->assertEquals('newalias', $entity->get_table_alias('mytable'));
}
/**
* Test for deprecated set table alias
*/
public function test_set_table_alias_deprecated(): void {
$entity = new base_test_entity();
$entity->set_table_alias('mydeprecatedtable', 'newalias');
$this->assertEquals('newalias', $entity->get_table_alias('mydeprecatedtable'));
// Debugging called twice, once for set_table_alias and once for subsequent get_table_alias.
$this->assertDebuggingCalledCount(2, [
'The table \'mydeprecatedtable\' is deprecated, please do not use it any more.',
'The table \'mydeprecatedtable\' is deprecated, please do not use it any more.',
]);
}
/**
* Test for deprecated get table alias replacement
*/
public function test_set_table_alias_deprecated_replacement(): void {
$entity = new base_test_entity_second();
$entity->set_table_alias('mydeprecatedtable', 'newalias');
$this->assertDebuggingCalled();
// We should get back the same alias for the replacement table.
$this->assertEquals('newalias', $entity->get_table_alias('mytable'));
}
/**
* Test invalid entity set table alias
*/
@@ -298,9 +355,9 @@ final class base_test extends advanced_testcase {
class base_test_entity extends base {
/**
* Table aliases
* Database tables that this entity uses
*
* @return array
* @return string[]
*/
protected function get_default_tables(): array {
return [
@@ -309,6 +366,17 @@ class base_test_entity extends base {
];
}
/**
* Database tables that this entity no longer uses
*
* @return string[]
*/
protected function get_deprecated_tables(): array {
return [
'mydeprecatedtable',
];
}
/**
* Entity title
*
@@ -345,3 +413,20 @@ class base_test_entity extends base {
->add_condition($filter);
}
}
/**
* Another simple implementation of the base entity
*/
class base_test_entity_second extends base_test_entity {
/**
* Database tables that this entity no longer uses
*
* @return string[]
*/
protected function get_deprecated_tables(): array {
return [
'mytable' => 'mydeprecatedtable',
];
}
}