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_group\reportbuilder\local\entities;
20
 
21
use context_course;
22
use context_helper;
23
use html_writer;
24
use lang_string;
25
use moodle_url;
26
use stdClass;
27
use core_reportbuilder\local\entities\base;
28
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
29
use core_reportbuilder\local\helpers\{custom_fields, format};
30
use core_reportbuilder\local\report\{column, filter};
31
 
32
defined('MOODLE_INTERNAL') || die();
33
 
34
global $CFG;
35
require_once("{$CFG->libdir}/grouplib.php");
36
 
37
/**
38
 * Group entity
39
 *
40
 * @package     core_group
41
 * @copyright   2022 Paul Holden <paulh@moodle.com>
42
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class group 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
            'context',
54
            'groups',
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('group', 'core_group');
65
    }
66
 
67
    /**
68
     * Initialise the entity
69
     *
70
     * @return base
71
     */
72
    public function initialise(): base {
73
        $groupsalias = $this->get_table_alias('groups');
74
 
75
        $customfields = (new custom_fields(
76
            "{$groupsalias}.id",
77
            $this->get_entity_name(),
78
            'core_group',
79
            'group',
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
        $contextalias = $this->get_table_alias('context');
108
        $groupsalias = $this->get_table_alias('groups');
109
 
110
        // Name column.
111
        $columns[] = (new column(
112
            'name',
113
            new lang_string('name'),
114
            $this->get_entity_name()
115
        ))
116
            ->add_joins($this->get_joins())
117
            ->set_type(column::TYPE_TEXT)
118
            ->add_fields("{$groupsalias}.name, {$groupsalias}.courseid")
119
            ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
120
            ->set_is_sortable(true)
121
            ->set_callback(static function($name, stdClass $group): string {
122
                if ($name === null) {
123
                    return '';
124
                }
125
 
126
                context_helper::preload_from_record($group);
127
                $context = context_course::instance($group->courseid);
128
 
129
                return format_string($group->name, true, ['context' => $context]);
130
            });
131
 
132
        // ID number column.
133
        $columns[] = (new column(
134
            'idnumber',
135
            new lang_string('idnumber'),
136
            $this->get_entity_name()
137
        ))
138
            ->add_joins($this->get_joins())
139
            ->set_type(column::TYPE_TEXT)
140
            ->add_fields("{$groupsalias}.idnumber")
141
            ->set_is_sortable(true);
142
 
143
        // Description column.
144
        $descriptionfieldsql = "{$groupsalias}.description";
145
        if ($DB->get_dbfamily() === 'oracle') {
146
            $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
147
        }
148
        $columns[] = (new column(
149
            'description',
150
            new lang_string('description'),
151
            $this->get_entity_name()
152
        ))
153
            ->add_joins($this->get_joins())
154
            ->set_type(column::TYPE_LONGTEXT)
155
            ->add_field($descriptionfieldsql, 'description')
156
            ->add_fields("{$groupsalias}.descriptionformat, {$groupsalias}.id, {$groupsalias}.courseid")
157
            ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
158
            ->set_is_sortable(false)
159
            ->set_callback(static function(?string $description, stdClass $group): string {
160
                global $CFG;
161
 
162
                if ($description === null) {
163
                    return '';
164
                }
165
 
166
                require_once("{$CFG->libdir}/filelib.php");
167
 
168
                context_helper::preload_from_record($group);
169
                $context = context_course::instance($group->courseid);
170
 
171
                $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'group',
172
                    'description', $group->id);
173
 
174
                return format_text($description, $group->descriptionformat, ['context' => $context]);
175
            });
176
 
177
        // Enrolment key column.
178
        $columns[] = (new column(
179
            'enrolmentkey',
180
            new lang_string('enrolmentkey', 'core_group'),
181
            $this->get_entity_name()
182
        ))
183
            ->add_joins($this->get_joins())
184
            ->set_type(column::TYPE_TEXT)
185
            ->add_fields("{$groupsalias}.enrolmentkey")
186
            ->set_is_sortable(true);
187
 
188
        // Visibility column.
189
        $columns[] = (new column(
190
            'visibility',
191
            new lang_string('visibilityshort', 'core_group'),
192
            $this->get_entity_name()
193
        ))
194
            ->add_joins($this->get_joins())
195
            ->set_type(column::TYPE_INTEGER)
196
            ->add_fields("{$groupsalias}.visibility")
197
            ->set_is_sortable(true)
198
            // It doesn't make sense to offer integer aggregation methods for this column.
199
            ->set_disabled_aggregation(['avg', 'max', 'min', 'sum'])
200
            ->set_callback(static function(?int $visibility): string {
201
                if ($visibility === null) {
202
                    return '';
203
                }
204
 
205
                $options = [
206
                    GROUPS_VISIBILITY_ALL => new lang_string('visibilityall', 'core_group'),
207
                    GROUPS_VISIBILITY_MEMBERS => new lang_string('visibilitymembers', 'core_group'),
208
                    GROUPS_VISIBILITY_OWN => new lang_string('visibilityown', 'core_group'),
209
                    GROUPS_VISIBILITY_NONE => new lang_string('visibilitynone', 'core_group'),
210
                ];
211
 
212
                return (string) ($options[$visibility] ?? $visibility);
213
            });
214
 
215
        // Participation column.
216
        $columns[] = (new column(
217
            'participation',
218
            new lang_string('participationshort', 'core_group'),
219
            $this->get_entity_name()
220
        ))
221
            ->add_joins($this->get_joins())
222
            ->set_type(column::TYPE_BOOLEAN)
223
            ->add_fields("{$groupsalias}.participation")
224
            ->set_is_sortable(true)
225
            ->set_callback([format::class, 'boolean_as_text']);
226
 
227
        // Picture column.
228
        $columns[] = (new column(
229
            'picture',
230
            new lang_string('picture'),
231
            $this->get_entity_name()
232
        ))
233
            ->add_joins($this->get_joins())
234
            ->set_type(column::TYPE_INTEGER)
235
            ->add_fields("{$groupsalias}.picture, {$groupsalias}.id, {$contextalias}.id AS contextid")
236
            ->set_is_sortable(false)
237
            // It doesn't make sense to offer integer aggregation methods for this column.
238
            ->set_disabled_aggregation(['avg', 'max', 'min', 'sum'])
239
            ->set_callback(static function ($picture, stdClass $group): string {
240
                if (empty($group->picture)) {
241
                    return '';
242
                }
243
 
244
                $pictureurl = moodle_url::make_pluginfile_url($group->contextid, 'group', 'icon', $group->id, '/', 'f2');
245
                $pictureurl->param('rev', $group->picture);
246
 
247
                return html_writer::img($pictureurl, '');
248
            });
249
 
250
        // Time created column.
251
        $columns[] = (new column(
252
            'timecreated',
253
            new lang_string('timecreated', 'core_reportbuilder'),
254
            $this->get_entity_name()
255
        ))
256
            ->add_joins($this->get_joins())
257
            ->set_type(column::TYPE_TIMESTAMP)
258
            ->add_fields("{$groupsalias}.timecreated")
259
            ->set_is_sortable(true)
260
            ->set_callback([format::class, 'userdate']);
261
 
262
        // Time modified column.
263
        $columns[] = (new column(
264
            'timemodified',
265
            new lang_string('timemodified', 'core_reportbuilder'),
266
            $this->get_entity_name()
267
        ))
268
            ->add_joins($this->get_joins())
269
            ->set_type(column::TYPE_TIMESTAMP)
270
            ->add_fields("{$groupsalias}.timemodified")
271
            ->set_is_sortable(true)
272
            ->set_callback([format::class, 'userdate']);
273
 
274
        return $columns;
275
    }
276
 
277
    /**
278
     * Return list of all available filters
279
     *
280
     * @return filter[]
281
     */
282
    protected function get_all_filters(): array {
283
        $groupsalias = $this->get_table_alias('groups');
284
 
285
        // Name filter.
286
        $filters[] = (new filter(
287
            text::class,
288
            'name',
289
            new lang_string('name'),
290
            $this->get_entity_name(),
291
            "{$groupsalias}.name"
292
        ))
293
            ->add_joins($this->get_joins());
294
 
295
        // ID number filter.
296
        $filters[] = (new filter(
297
            text::class,
298
            'idnumber',
299
            new lang_string('idnumber'),
300
            $this->get_entity_name(),
301
            "{$groupsalias}.idnumber"
302
        ))
303
            ->add_joins($this->get_joins());
304
 
305
        // Visibility filter.
306
        $filters[] = (new filter(
307
            select::class,
308
            'visibility',
309
            new lang_string('visibilityshort', 'core_group'),
310
            $this->get_entity_name(),
311
            "{$groupsalias}.visibility"
312
        ))
313
            ->add_joins($this->get_joins())
314
            ->set_options([
315
                GROUPS_VISIBILITY_ALL => new lang_string('visibilityall', 'core_group'),
316
                GROUPS_VISIBILITY_MEMBERS => new lang_string('visibilitymembers', 'core_group'),
317
                GROUPS_VISIBILITY_OWN => new lang_string('visibilityown', 'core_group'),
318
                GROUPS_VISIBILITY_NONE => new lang_string('visibilitynone', 'core_group'),
319
            ]);
320
 
321
        // Participation filter.
322
        $filters[] = (new filter(
323
            boolean_select::class,
324
            'participation',
325
            new lang_string('participationshort', 'core_group'),
326
            $this->get_entity_name(),
327
            "{$groupsalias}.participation"
328
        ))
329
            ->add_joins($this->get_joins());
330
 
331
        // Time created filter.
332
        $filters[] = (new filter(
333
            date::class,
334
            'timecreated',
335
            new lang_string('timecreated', 'core_reportbuilder'),
336
            $this->get_entity_name(),
337
            "{$groupsalias}.timecreated"
338
        ))
339
            ->add_joins($this->get_joins());
340
 
341
        return $filters;
342
    }
343
}