Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
declare(strict_types=1);
18
 
19
namespace core_cohort\reportbuilder\local\entities;
20
 
21
use context;
22
use context_helper;
23
use lang_string;
24
use stdClass;
25
use theme_config;
26
use core_reportbuilder\local\entities\base;
27
use core_reportbuilder\local\filters\boolean_select;
28
use core_reportbuilder\local\filters\cohort as cohort_filter;
29
use core_reportbuilder\local\filters\date;
30
use core_reportbuilder\local\filters\select;
31
use core_reportbuilder\local\filters\text;
32
use core_reportbuilder\local\helpers\custom_fields;
33
use core_reportbuilder\local\helpers\format;
34
use core_reportbuilder\local\report\column;
35
use core_reportbuilder\local\report\filter;
36
 
37
/**
38
 * Cohort entity
39
 *
40
 * @package     core_cohort
41
 * @copyright   2021 Paul Holden <paulh@moodle.com>
42
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class cohort extends base {
45
 
46
    /**
47
     * Database tables that this entity uses
48
     *
49
     * @return string[]
50
     */
51
    protected function get_default_tables(): array {
52
        return [
53
            'cohort',
54
            'context',
55
        ];
56
    }
57
 
58
    /**
59
     * The default title for this entity
60
     *
61
     * @return lang_string
62
     */
63
    protected function get_default_entity_title(): lang_string {
64
        return new lang_string('cohort', 'core_cohort');
65
    }
66
 
67
    /**
68
     * Initialise the entity
69
     *
70
     * @return base
71
     */
72
    public function initialise(): base {
73
        $tablealias = $this->get_table_alias('cohort');
74
 
75
        $customfields = (new custom_fields(
76
            "{$tablealias}.id",
77
            $this->get_entity_name(),
78
            'core_cohort',
79
            'cohort',
80
        ))
81
            ->add_joins($this->get_joins());
82
 
83
        $columns = array_merge($this->get_all_columns(), $customfields->get_columns());
84
        foreach ($columns as $column) {
85
            $this->add_column($column);
86
        }
87
 
88
        // All the filters defined by the entity can also be used as conditions.
89
        $filters = array_merge($this->get_all_filters(), $customfields->get_filters());
90
        foreach ($filters as $filter) {
91
            $this
92
                ->add_filter($filter)
93
                ->add_condition($filter);
94
        }
95
 
96
        return $this;
97
    }
98
 
99
    /**
100
     * Returns list of all available columns
101
     *
102
     * @return column[]
103
     */
104
    protected function get_all_columns(): array {
105
        global $DB;
106
 
107
        $tablealias = $this->get_table_alias('cohort');
108
        $contextalias = $this->get_table_alias('context');
109
 
110
        // Category/context column.
111
        $columns[] = (new column(
112
            'context',
113
            new lang_string('category'),
114
            $this->get_entity_name()
115
        ))
116
            ->add_joins($this->get_joins())
117
            ->add_join($this->get_context_join())
118
            ->set_type(column::TYPE_TEXT)
119
            ->add_fields("{$tablealias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias))
120
            ->set_is_sortable(true)
121
            ->add_callback(static function($contextid, stdClass $cohort): string {
122
                if ($contextid === null) {
123
                    return '';
124
                }
125
 
126
                context_helper::preload_from_record($cohort);
127
                return context::instance_by_id($cohort->contextid)->get_context_name(false);
128
            });
129
 
130
        // Name column.
131
        $columns[] = (new column(
132
            'name',
133
            new lang_string('name', 'core_cohort'),
134
            $this->get_entity_name()
135
        ))
136
            ->add_joins($this->get_joins())
137
            ->set_type(column::TYPE_TEXT)
138
            ->add_fields("{$tablealias}.name")
139
            ->set_is_sortable(true);
140
 
141
        // ID number column.
142
        $columns[] = (new column(
143
            'idnumber',
144
            new lang_string('idnumber', 'core_cohort'),
145
            $this->get_entity_name()
146
        ))
147
            ->add_joins($this->get_joins())
148
            ->set_type(column::TYPE_TEXT)
149
            ->add_fields("{$tablealias}.idnumber")
150
            ->set_is_sortable(true);
151
 
152
        // Description column.
153
        $descriptionfieldsql = "{$tablealias}.description";
154
        if ($DB->get_dbfamily() === 'oracle') {
155
            $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
156
        }
157
        $columns[] = (new column(
158
            'description',
159
            new lang_string('description'),
160
            $this->get_entity_name()
161
        ))
162
            ->add_joins($this->get_joins())
163
            ->add_join($this->get_context_join())
164
            ->set_type(column::TYPE_LONGTEXT)
165
            ->add_field($descriptionfieldsql, 'description')
166
            ->add_fields("{$tablealias}.descriptionformat, {$tablealias}.id, {$tablealias}.contextid")
167
            ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
168
            ->add_callback(static function(?string $description, stdClass $cohort): string {
169
                global $CFG;
170
                require_once("{$CFG->libdir}/filelib.php");
171
 
172
                if ($description === null) {
173
                    return '';
174
                }
175
 
176
                context_helper::preload_from_record($cohort);
177
                $context = context::instance_by_id($cohort->contextid);
178
 
179
                $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'cohort',
180
                    'description', $cohort->id);
181
 
182
                return format_text($description, $cohort->descriptionformat, ['context' => $context->id]);
183
            });
184
 
185
        // Visible column.
186
        $columns[] = (new column(
187
            'visible',
188
            new lang_string('visible', 'core_cohort'),
189
            $this->get_entity_name()
190
        ))
191
            ->add_joins($this->get_joins())
192
            ->set_type(column::TYPE_BOOLEAN)
193
            ->add_fields("{$tablealias}.visible")
194
            ->set_is_sortable(true)
195
            ->set_callback([format::class, 'boolean_as_text']);
196
 
197
        // Time created column.
198
        $columns[] = (new column(
199
            'timecreated',
200
            new lang_string('timecreated', 'core_reportbuilder'),
201
            $this->get_entity_name()
202
        ))
203
            ->add_joins($this->get_joins())
204
            ->set_type(column::TYPE_TIMESTAMP)
205
            ->add_fields("{$tablealias}.timecreated")
206
            ->set_is_sortable(true)
207
            ->set_callback([format::class, 'userdate']);
208
 
209
        // Time modified column.
210
        $columns[] = (new column(
211
            'timemodified',
212
            new lang_string('timemodified', 'core_reportbuilder'),
213
            $this->get_entity_name()
214
        ))
215
            ->add_joins($this->get_joins())
216
            ->set_type(column::TYPE_TIMESTAMP)
217
            ->add_fields("{$tablealias}.timemodified")
218
            ->set_is_sortable(true)
219
            ->set_callback([format::class, 'userdate']);
220
 
221
        // Component column.
222
        $columns[] = (new column(
223
            'component',
224
            new lang_string('component', 'core_cohort'),
225
            $this->get_entity_name()
226
        ))
227
            ->add_joins($this->get_joins())
228
            ->set_type(column::TYPE_TEXT)
229
            ->add_fields("{$tablealias}.component")
230
            ->set_is_sortable(true)
231
            ->add_callback(static function(?string $component): string {
232
                if ($component === null) {
233
                    return '';
234
                }
235
 
236
                return $component === ''
237
                    ? get_string('nocomponent', 'cohort')
238
                    : get_string('pluginname', $component);
239
            });
240
 
241
        // Theme column.
242
        $columns[] = (new column(
243
            'theme',
244
            new lang_string('theme'),
245
            $this->get_entity_name()
246
        ))
247
            ->add_joins($this->get_joins())
248
            ->set_type(column::TYPE_TEXT)
249
            ->add_fields("{$tablealias}.theme")
250
            ->set_is_sortable(true)
251
            ->add_callback(static function (?string $theme): string {
252
                if ((string) $theme === '') {
253
                    return '';
254
                }
255
 
256
                return get_string('pluginname', "theme_{$theme}");
257
            });
258
 
259
        return $columns;
260
    }
261
 
262
    /**
263
     * Return list of all available filters
264
     *
265
     * @return filter[]
266
     */
267
    protected function get_all_filters(): array {
268
        global $DB;
269
 
270
        $tablealias = $this->get_table_alias('cohort');
271
 
272
        // Cohort select filter.
273
        $filters[] = (new filter(
274
            cohort_filter::class,
275
            'cohortselect',
276
            new lang_string('selectcohort', 'core_cohort'),
277
            $this->get_entity_name(),
278
            "{$tablealias}.id"
279
        ))
280
            ->add_joins($this->get_joins());
281
 
282
        // Context filter.
283
        $filters[] = (new filter(
284
            select::class,
285
            'context',
286
            new lang_string('category'),
287
            $this->get_entity_name(),
288
            "{$tablealias}.contextid"
289
        ))
290
            ->add_joins($this->get_joins())
291
            ->set_options_callback(static function(): array {
292
                global $DB;
293
 
294
                // Load all contexts in which there are cohorts.
295
                $ctxfields = context_helper::get_preload_record_columns_sql('ctx');
296
                $contexts = $DB->get_records_sql("
297
                    SELECT DISTINCT {$ctxfields}, c.contextid
298
                      FROM {context} ctx
299
                      JOIN {cohort} c ON c.contextid = ctx.id");
300
 
301
                // Transform context record into it's name (used as the filter options).
302
                return array_map(static function(stdClass $contextrecord): string {
303
                    context_helper::preload_from_record($contextrecord);
304
 
305
                    return context::instance_by_id($contextrecord->contextid)
306
                        ->get_context_name(false);
307
                }, $contexts);
308
            });
309
 
310
        // Name filter.
311
        $filters[] = (new filter(
312
            text::class,
313
            'name',
314
            new lang_string('name', 'core_cohort'),
315
            $this->get_entity_name(),
316
            "{$tablealias}.name"
317
        ))
318
            ->add_joins($this->get_joins());
319
 
320
        // ID number filter.
321
        $filters[] = (new filter(
322
            text::class,
323
            'idnumber',
324
            new lang_string('idnumber', 'core_cohort'),
325
            $this->get_entity_name(),
326
            "{$tablealias}.idnumber"
327
        ))
328
            ->add_joins($this->get_joins());
329
 
330
        // Time created filter.
331
        $filters[] = (new filter(
332
            date::class,
333
            'timecreated',
334
            new lang_string('timecreated', 'core_reportbuilder'),
335
            $this->get_entity_name(),
336
            "{$tablealias}.timecreated"
337
        ))
338
            ->add_joins($this->get_joins());
339
 
340
        // Description filter.
341
        $filters[] = (new filter(
342
            text::class,
343
            'description',
344
            new lang_string('description'),
345
            $this->get_entity_name(),
346
            $DB->sql_cast_to_char("{$tablealias}.description")
347
        ))
348
            ->add_joins($this->get_joins());
349
 
350
        // Theme filter.
351
        $filters[] = (new filter(
352
            select::class,
353
            'theme',
354
            new lang_string('theme'),
355
            $this->get_entity_name(),
356
            "{$tablealias}.theme",
357
        ))
358
            ->set_options_callback(static function(): array {
359
                return array_map(
360
                    fn(theme_config $theme) => $theme->get_theme_name(),
361
                    get_list_of_themes(),
362
                );
363
            })
364
            ->add_joins($this->get_joins());
365
 
366
        // Visible filter.
367
        $filters[] = (new filter(
368
            boolean_select::class,
369
            'visible',
370
            new lang_string('visible', 'core_cohort'),
371
            $this->get_entity_name(),
372
            "{$tablealias}.visible"
373
        ))
374
            ->add_joins($this->get_joins());
375
 
376
        return $filters;
377
    }
378
 
379
    /**
380
     * Return context join used by columns
381
     *
382
     * @return string
383
     */
384
    private function get_context_join(): string {
385
 
386
        // If the context table is already joined, we don't need to do that again.
387
        if ($this->has_table_join_alias('context')) {
388
            return '';
389
        }
390
 
391
        $tablealias = $this->get_table_alias('cohort');
392
        $contextalias = $this->get_table_alias('context');
393
 
394
        return "LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$tablealias}.contextid";
395
    }
396
}