This commit is contained in:
Ilya Tregubov
2023-12-12 09:28:53 +08:00
33 changed files with 243 additions and 175 deletions
@@ -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',
];
}
/**
@@ -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',
];
}
@@ -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',
];
}
@@ -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',
];
}
/**
@@ -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());
@@ -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',
];
}
@@ -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',
];
}
@@ -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',
];
}
/**
@@ -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',
];
}
@@ -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())
@@ -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',
];
}
/**
@@ -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',
];
}
@@ -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',
];
}
@@ -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',
];
}
/**
@@ -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',
];
}
@@ -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',
];
}
@@ -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',
];
}
/**
@@ -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',
];
}
@@ -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',
];
}
@@ -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',
];
}
@@ -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',
];
}
/**
@@ -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',
];
}
/**
@@ -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.
@@ -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',
];
}
/**
@@ -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',
];
}
/**
+50 -18
View File
@@ -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
@@ -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',
];
}
@@ -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',
];
}
@@ -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',
];
}
+2
View File
@@ -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:
@@ -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',
];
}
/**
@@ -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',
];
}
@@ -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',
];
}
/**