Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
        $contextalias = $this->get_table_alias('context');
106
        $groupsalias = $this->get_table_alias('groups');
107
 
108
        // Name column.
109
        $columns[] = (new column(
110
            'name',
111
            new lang_string('name'),
112
            $this->get_entity_name()
113
        ))
114
            ->add_joins($this->get_joins())
115
            ->set_type(column::TYPE_TEXT)
116
            ->add_fields("{$groupsalias}.name, {$groupsalias}.courseid")
117
            ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
118
            ->set_is_sortable(true)
119
            ->set_callback(static function($name, stdClass $group): string {
120
                if ($name === null) {
121
                    return '';
122
                }
123
 
124
                context_helper::preload_from_record($group);
125
                $context = context_course::instance($group->courseid);
126
 
127
                return format_string($group->name, true, ['context' => $context]);
128
            });
129
 
130
        // ID number column.
131
        $columns[] = (new column(
132
            'idnumber',
133
            new lang_string('idnumber'),
134
            $this->get_entity_name()
135
        ))
136
            ->add_joins($this->get_joins())
137
            ->set_type(column::TYPE_TEXT)
138
            ->add_fields("{$groupsalias}.idnumber")
139
            ->set_is_sortable(true);
140
 
141
        // Description column.
142
        $columns[] = (new column(
143
            'description',
144
            new lang_string('description'),
145
            $this->get_entity_name()
146
        ))
147
            ->add_joins($this->get_joins())
148
            ->set_type(column::TYPE_LONGTEXT)
1441 ariadna 149
            ->add_fields("{$groupsalias}.description, {$groupsalias}.descriptionformat, {$groupsalias}.id, {$groupsalias}.courseid")
1 efrain 150
            ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
1441 ariadna 151
            ->set_is_sortable(true)
1 efrain 152
            ->set_callback(static function(?string $description, stdClass $group): string {
153
                global $CFG;
154
 
155
                if ($description === null) {
156
                    return '';
157
                }
158
 
159
                require_once("{$CFG->libdir}/filelib.php");
160
 
161
                context_helper::preload_from_record($group);
162
                $context = context_course::instance($group->courseid);
163
 
164
                $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'group',
165
                    'description', $group->id);
166
 
167
                return format_text($description, $group->descriptionformat, ['context' => $context]);
168
            });
169
 
170
        // Enrolment key column.
171
        $columns[] = (new column(
172
            'enrolmentkey',
173
            new lang_string('enrolmentkey', 'core_group'),
174
            $this->get_entity_name()
175
        ))
176
            ->add_joins($this->get_joins())
177
            ->set_type(column::TYPE_TEXT)
178
            ->add_fields("{$groupsalias}.enrolmentkey")
179
            ->set_is_sortable(true);
180
 
181
        // Visibility column.
182
        $columns[] = (new column(
183
            'visibility',
184
            new lang_string('visibilityshort', 'core_group'),
185
            $this->get_entity_name()
186
        ))
187
            ->add_joins($this->get_joins())
188
            ->add_fields("{$groupsalias}.visibility")
189
            ->set_is_sortable(true)
1441 ariadna 190
            ->set_callback(static function(?string $visibility): string {
1 efrain 191
                if ($visibility === null) {
192
                    return '';
193
                }
194
 
195
                $options = [
196
                    GROUPS_VISIBILITY_ALL => new lang_string('visibilityall', 'core_group'),
197
                    GROUPS_VISIBILITY_MEMBERS => new lang_string('visibilitymembers', 'core_group'),
198
                    GROUPS_VISIBILITY_OWN => new lang_string('visibilityown', 'core_group'),
199
                    GROUPS_VISIBILITY_NONE => new lang_string('visibilitynone', 'core_group'),
200
                ];
201
 
1441 ariadna 202
                return (string) ($options[(int) $visibility] ?? $visibility);
1 efrain 203
            });
204
 
205
        // Participation column.
206
        $columns[] = (new column(
207
            'participation',
208
            new lang_string('participationshort', 'core_group'),
209
            $this->get_entity_name()
210
        ))
211
            ->add_joins($this->get_joins())
212
            ->set_type(column::TYPE_BOOLEAN)
213
            ->add_fields("{$groupsalias}.participation")
214
            ->set_is_sortable(true)
215
            ->set_callback([format::class, 'boolean_as_text']);
216
 
217
        // Picture column.
218
        $columns[] = (new column(
219
            'picture',
220
            new lang_string('picture'),
221
            $this->get_entity_name()
222
        ))
223
            ->add_joins($this->get_joins())
224
            ->add_fields("{$groupsalias}.picture, {$groupsalias}.id, {$contextalias}.id AS contextid")
225
            ->set_is_sortable(false)
1441 ariadna 226
            ->set_callback(static function($value, stdClass $group): string {
1 efrain 227
                if (empty($group->picture)) {
228
                    return '';
229
                }
230
 
231
                $pictureurl = moodle_url::make_pluginfile_url($group->contextid, 'group', 'icon', $group->id, '/', 'f2');
232
                $pictureurl->param('rev', $group->picture);
233
 
234
                return html_writer::img($pictureurl, '');
235
            });
236
 
237
        // Time created column.
238
        $columns[] = (new column(
239
            'timecreated',
240
            new lang_string('timecreated', 'core_reportbuilder'),
241
            $this->get_entity_name()
242
        ))
243
            ->add_joins($this->get_joins())
244
            ->set_type(column::TYPE_TIMESTAMP)
245
            ->add_fields("{$groupsalias}.timecreated")
246
            ->set_is_sortable(true)
247
            ->set_callback([format::class, 'userdate']);
248
 
249
        // Time modified column.
250
        $columns[] = (new column(
251
            'timemodified',
252
            new lang_string('timemodified', 'core_reportbuilder'),
253
            $this->get_entity_name()
254
        ))
255
            ->add_joins($this->get_joins())
256
            ->set_type(column::TYPE_TIMESTAMP)
257
            ->add_fields("{$groupsalias}.timemodified")
258
            ->set_is_sortable(true)
259
            ->set_callback([format::class, 'userdate']);
260
 
261
        return $columns;
262
    }
263
 
264
    /**
265
     * Return list of all available filters
266
     *
267
     * @return filter[]
268
     */
269
    protected function get_all_filters(): array {
270
        $groupsalias = $this->get_table_alias('groups');
271
 
272
        // Name filter.
273
        $filters[] = (new filter(
274
            text::class,
275
            'name',
276
            new lang_string('name'),
277
            $this->get_entity_name(),
278
            "{$groupsalias}.name"
279
        ))
280
            ->add_joins($this->get_joins());
281
 
282
        // ID number filter.
283
        $filters[] = (new filter(
284
            text::class,
285
            'idnumber',
286
            new lang_string('idnumber'),
287
            $this->get_entity_name(),
288
            "{$groupsalias}.idnumber"
289
        ))
290
            ->add_joins($this->get_joins());
291
 
292
        // Visibility filter.
293
        $filters[] = (new filter(
294
            select::class,
295
            'visibility',
296
            new lang_string('visibilityshort', 'core_group'),
297
            $this->get_entity_name(),
298
            "{$groupsalias}.visibility"
299
        ))
300
            ->add_joins($this->get_joins())
301
            ->set_options([
302
                GROUPS_VISIBILITY_ALL => new lang_string('visibilityall', 'core_group'),
303
                GROUPS_VISIBILITY_MEMBERS => new lang_string('visibilitymembers', 'core_group'),
304
                GROUPS_VISIBILITY_OWN => new lang_string('visibilityown', 'core_group'),
305
                GROUPS_VISIBILITY_NONE => new lang_string('visibilitynone', 'core_group'),
306
            ]);
307
 
308
        // Participation filter.
309
        $filters[] = (new filter(
310
            boolean_select::class,
311
            'participation',
312
            new lang_string('participationshort', 'core_group'),
313
            $this->get_entity_name(),
314
            "{$groupsalias}.participation"
315
        ))
316
            ->add_joins($this->get_joins());
317
 
318
        // Time created filter.
319
        $filters[] = (new filter(
320
            date::class,
321
            'timecreated',
322
            new lang_string('timecreated', 'core_reportbuilder'),
323
            $this->get_entity_name(),
324
            "{$groupsalias}.timecreated"
325
        ))
326
            ->add_joins($this->get_joins());
327
 
328
        return $filters;
329
    }
330
}