Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 17... Línea 17...
17
declare(strict_types=1);
17
declare(strict_types=1);
Línea 18... Línea 18...
18
 
18
 
Línea 19... Línea 19...
19
namespace core_reportbuilder\local\entities;
19
namespace core_reportbuilder\local\entities;
20
 
20
 
21
use coding_exception;
21
use coding_exception;
22
use core_reportbuilder\local\helpers\database;
22
use core_reportbuilder\local\helpers\{database, join_trait};
23
use core_reportbuilder\local\report\column;
23
use core_reportbuilder\local\report\column;
Línea 24... Línea 24...
24
use core_reportbuilder\local\report\filter;
24
use core_reportbuilder\local\report\filter;
Línea 31... Línea 31...
31
 * @copyright   2019 Marina Glancy <marina@moodle.com>
31
 * @copyright   2019 Marina Glancy <marina@moodle.com>
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
33
 */
34
abstract class base {
34
abstract class base {
Línea -... Línea 35...
-
 
35
 
-
 
36
    use join_trait;
35
 
37
 
36
    /** @var string $entityname Internal reference to name of entity */
38
    /** @var string $entityname Internal reference to name of entity */
Línea 37... Línea 39...
37
    private $entityname = null;
39
    private $entityname = null;
38
 
40
 
Línea 43... Línea 45...
43
    private $tablealiases = [];
45
    private $tablealiases = [];
Línea 44... Línea 46...
44
 
46
 
45
    /** @var array $tablejoinaliases Database tables that have already been joined to the report and their aliases */
47
    /** @var array $tablejoinaliases Database tables that have already been joined to the report and their aliases */
Línea 46... Línea -...
46
    private $tablejoinaliases = [];
-
 
47
 
-
 
48
    /** @var string[] $joins List of SQL joins for the entity */
-
 
49
    private $joins = [];
48
    private $tablejoinaliases = [];
50
 
49
 
Línea 51... Línea 50...
51
    /** @var column[] $columns List of columns for the entity */
50
    /** @var column[] $columns List of columns for the entity */
52
    private $columns = [];
51
    private $columns = [];
Línea 53... Línea 52...
53
 
52
 
54
    /** @var filter[] $filters List of filters for the entity */
53
    /** @var filter[] $filters List of filters for the entity */
Línea 55... Línea 54...
55
    private $filters = [];
54
    private $filters = [];
56
 
-
 
57
    /** @var filter[] $conditions List of conditions for the entity */
-
 
58
    private $conditions = [];
55
 
59
 
-
 
60
    /**
-
 
61
     * Database tables that this entity uses
-
 
62
     *
56
    /** @var filter[] $conditions List of conditions for the entity */
63
     * Must be overridden by the entity to list all database tables that it expects to be present in the main
57
    private $conditions = [];
64
     * SQL or in JOINs added to this entity
58
 
65
     *
59
    /**
66
     * @todo in Moodle 4.8 - make abstract when support for {@see get_default_table_aliases} is finally removed
-
 
Línea -... Línea 60...
-
 
60
     * Database tables that the entity expects to be present in the main SQL or in JOINs added to it
67
     *
61
     *
-
 
62
     * @return string[]
68
     * @return string[]
63
     */
69
     */
-
 
70
    protected function get_default_tables(): array {
64
    abstract protected function get_default_tables(): array;
71
        static $debuggingshown;
65
 
72
 
-
 
73
        // The default implementation falls back to retrieving deprecated table aliases to determine our table names.
-
 
74
        $tablenamealiases = $this->get_default_table_aliases();
-
 
75
        if (!empty($tablenamealiases) && !$debuggingshown) {
66
    /**
76
            debugging('The function get_default_table_aliases() is deprecated, please define the entity' .
-
 
77
                ' tables with get_default_tables() in ' . static::class, DEBUG_DEVELOPER);
-
 
78
 
67
     * @deprecated since Moodle 4.4 - aliases are now autogenerated, please implement {@see get_default_tables} instead
Línea 79... Línea 68...
79
            // Don't be too spammy with the debugging, this method is called multiple times per entity load.
68
     */
80
            $debuggingshown = true;
69
    #[\core\attribute\deprecated('::get_default_tables', since: '4.4', mdl: 'MDL-79397', final: true)]
-
 
70
    protected function get_default_table_aliases(): array {
81
        }
71
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
-
 
72
        return [];
82
 
73
    }
83
        return array_keys($tablenamealiases);
74
 
84
    }
75
    /**
85
 
76
     * Database tables that the entity once used but now no longer does. To prevent errors in third-party code, rather than
86
    /**
77
     * simply removing the table from {@see get_default_tables} you can override this method, which will emit developer debug
87
     * Database tables that this entity uses and their default aliases (note that these aliases are now ignored)
78
     *
88
     *
79
     * Returns a simple list of table names, ['t1', 't2'] if they have no replacement; or ['t3' => 't1'] if an equivalent
Línea 89... Línea 80...
89
     * @return string[] Array of $tablename => $alias
80
     * replacement table name exists, where 't3' replaces 't1'
90
     *
81
     *
Línea 163... Línea 154...
163
    final public function get_entity_title(): lang_string {
154
    final public function get_entity_title(): lang_string {
164
        return $this->entitytitle ?? $this->get_default_entity_title();
155
        return $this->entitytitle ?? $this->get_default_entity_title();
165
    }
156
    }
Línea 166... Línea 157...
166
 
157
 
-
 
158
    /**
-
 
159
     * Validate the given table is expected by the entity
-
 
160
     *
-
 
161
     * Emits developer debugging for deprecated tables, will return replacement for deprecated table if specified
-
 
162
     * by the entity
-
 
163
     *
-
 
164
     * @param string $tablename
-
 
165
     * @return string
-
 
166
     * @throws coding_exception For invalid table name
-
 
167
     */
-
 
168
    private function validate_table_name(string $tablename): string {
-
 
169
        $deprecatedtables = $this->get_deprecated_tables();
-
 
170
        if (!in_array($tablename, array_merge($this->get_default_tables(), $deprecatedtables))) {
-
 
171
            throw new coding_exception('Invalid table name', $tablename);
-
 
172
        }
-
 
173
 
-
 
174
        // Emit debugging if table is marked as deprecated by the entity.
-
 
175
        if (($tablenamereplacement = array_search($tablename, $deprecatedtables)) !== false) {
-
 
176
            debugging("The table '{$tablename}' is deprecated, please do not use it any more.", DEBUG_DEVELOPER);
-
 
177
 
-
 
178
            // An associative array contains the replacement table name as the key, so return that.
-
 
179
            if (!array_is_list($deprecatedtables)) {
-
 
180
                return $tablenamereplacement;
-
 
181
            }
-
 
182
        }
-
 
183
 
-
 
184
        return $tablename;
-
 
185
    }
-
 
186
 
167
    /**
187
    /**
168
     * Override the default alias for given database table used in entity queries, for instance when the same table is used
188
     * Override the default alias for given database table used in entity queries, for instance when the same table is used
169
     * by multiple entities and you want them each to refer to it by the same alias
189
     * by multiple entities and you want them each to refer to it by the same alias
170
     *
190
     *
171
     * @param string $tablename One of the tables set by {@see get_default_tables}
191
     * @param string $tablename One of the tables set by {@see get_default_tables}
172
     * @param string $alias
192
     * @param string $alias
173
     * @return self
-
 
174
     * @throws coding_exception For invalid table name
193
     * @return self
175
     */
194
     */
176
    final public function set_table_alias(string $tablename, string $alias): self {
195
    final public function set_table_alias(string $tablename, string $alias): self {
177
        $tablenames = $this->get_default_tables();
-
 
178
        if (!in_array($tablename, $tablenames)) {
-
 
179
            throw new coding_exception('Invalid table name', $tablename);
-
 
180
        }
-
 
181
 
196
        $tablename = $this->validate_table_name($tablename);
-
 
197
        $this->tablealiases[$tablename] = $alias;
182
        $this->tablealiases[$tablename] = $alias;
198
 
183
        return $this;
199
        return $this;
Línea 184... Línea 200...
184
    }
200
    }
185
 
201
 
Línea 199... Línea 215...
199
    /**
215
    /**
200
     * Returns an alias used in the queries for a given table
216
     * Returns an alias used in the queries for a given table
201
     *
217
     *
202
     * @param string $tablename One of the tables set by {@see get_default_tables}
218
     * @param string $tablename One of the tables set by {@see get_default_tables}
203
     * @return string
219
     * @return string
204
     * @throws coding_exception For invalid table name
-
 
205
     */
220
     */
206
    final public function get_table_alias(string $tablename): string {
221
    final public function get_table_alias(string $tablename): string {
207
        $tablenames = $this->get_default_tables();
222
        $tablename = $this->validate_table_name($tablename);
208
        if (!in_array($tablename, $tablenames)) {
-
 
209
            throw new coding_exception('Invalid table name', $tablename);
-
 
210
        }
-
 
Línea 211... Línea 223...
211
 
223
 
212
        // We don't have the alias yet, generate a new one.
224
        // We don't have the alias yet, generate a new one.
213
        if (!array_key_exists($tablename, $this->tablealiases)) {
225
        if (!array_key_exists($tablename, $this->tablealiases)) {
214
            $this->set_table_alias($tablename, database::generate_alias());
226
            $this->set_table_alias($tablename, database::generate_alias());
Línea 252... Línea 264...
252
    final public function has_table_join_alias(string $tablename): bool {
264
    final public function has_table_join_alias(string $tablename): bool {
253
        return array_key_exists($tablename, $this->tablejoinaliases);
265
        return array_key_exists($tablename, $this->tablejoinaliases);
254
    }
266
    }
Línea 255... Línea 267...
255
 
267
 
256
    /**
-
 
257
     * Add join clause required for this entity to join to existing tables/entities
-
 
258
     *
-
 
259
     * @param string $join
-
 
260
     * @return self
-
 
261
     */
-
 
262
    final public function add_join(string $join): self {
-
 
263
        $this->joins[trim($join)] = trim($join);
-
 
264
        return $this;
-
 
265
    }
-
 
266
 
-
 
267
    /**
-
 
268
     * Add multiple join clauses required for this entity {@see add_join}
-
 
269
     *
-
 
270
     * @param string[] $joins
-
 
271
     * @return self
-
 
272
     */
-
 
273
    final public function add_joins(array $joins): self {
-
 
274
        foreach ($joins as $join) {
-
 
275
            $this->add_join($join);
-
 
276
        }
-
 
277
        return $this;
-
 
278
    }
-
 
279
 
-
 
280
    /**
-
 
281
     * Return entity joins
-
 
282
     *
-
 
283
     * @return string[]
-
 
284
     */
-
 
285
    final public function get_joins(): array {
-
 
286
        return array_values($this->joins);
-
 
287
    }
-
 
288
 
-
 
289
    /**
268
    /**
290
     * Helper method for returning joins necessary for retrieving tags related to the current entity
269
     * Helper method for returning joins necessary for retrieving tags related to the current entity
291
     *
270
     *
292
     * Both 'tag' and 'tag_instance' aliases must be returned by the entity {@see get_default_tables} method
271
     * Both 'tag' and 'tag_instance' aliases must be returned by the entity {@see get_default_tables} method
293
     *
272
     *