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_course\reportbuilder\local\entities;
20
 
21
use context_course;
22
use core_course\reportbuilder\local\formatters\enrolment as enrolment_formatter;
23
use core_reportbuilder\local\entities\base;
24
use core_reportbuilder\local\filters\date;
25
use core_reportbuilder\local\filters\select;
26
use core_reportbuilder\local\helpers\database;
27
use core_reportbuilder\local\helpers\format;
28
use core_reportbuilder\local\report\column;
29
use core_reportbuilder\local\report\filter;
30
use core_user\output\status_field;
31
use enrol_plugin;
32
use lang_string;
33
use stdClass;
34
 
35
/**
36
 * Course enrolment entity implementation
37
 *
38
 * @package     core_course
39
 * @copyright   2022 David Matamoros <davidmc@moodle.com>
40
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class enrolment extends base {
43
 
44
    /**
45
     * Database tables that this entity uses
46
     *
47
     * @return string[]
48
     */
49
    protected function get_default_tables(): array {
50
        return [
51
            'user_enrolments',
52
            'enrol',
53
        ];
54
    }
55
 
56
    /**
57
     * The default title for this entity in the list of columns/conditions/filters in the report builder
58
     *
59
     * @return lang_string
60
     */
61
    protected function get_default_entity_title(): lang_string {
62
        return new lang_string('enrolment', 'enrol');
63
    }
64
 
65
    /**
66
     * Initialise the entity
67
     *
68
     * @return base
69
     */
70
    public function initialise(): base {
71
        foreach ($this->get_all_columns() as $column) {
72
            $this->add_column($column);
73
        }
74
 
75
        // All the filters defined by the entity can also be used as conditions.
76
        foreach ($this->get_all_filters() as $filter) {
77
            $this
78
                ->add_filter($filter)
79
                ->add_condition($filter);
80
        }
81
 
82
        return $this;
83
    }
84
 
85
    /**
86
     * Returns list of all available columns
87
     *
88
     * @return column[]
89
     */
90
    protected function get_all_columns(): array {
91
        $userenrolments = $this->get_table_alias('user_enrolments');
92
        $enrol = $this->get_table_alias('enrol');
93
 
94
        // Enrolment method column (Deprecated since Moodle 4.3, to remove in MDL-78118).
95
        $columns[] = (new column(
96
            'method',
97
            new lang_string('method', 'enrol'),
98
            $this->get_entity_name()
99
        ))
100
            ->add_joins($this->get_joins())
101
            ->set_type(column::TYPE_TEXT)
102
            ->add_fields("{$enrol}.enrol, {$enrol}.id")
103
            ->set_is_sortable(true)
104
            ->set_is_deprecated('See \'enrol:name\' for replacement')
105
            ->add_callback([enrolment_formatter::class, 'enrolment_name']);
106
 
107
        // Enrolment time created.
108
        $columns[] = (new column(
109
            'timecreated',
110
            new lang_string('timecreated', 'moodle'),
111
            $this->get_entity_name()
112
        ))
113
            ->add_joins($this->get_joins())
114
            ->set_type(column::TYPE_TIMESTAMP)
115
            ->add_field("{$userenrolments}.timecreated")
116
            ->set_is_sortable(true)
117
            ->add_callback([format::class, 'userdate']);
118
 
119
        // Enrolment time started.
120
        $columns[] = (new column(
121
            'timestarted',
122
            new lang_string('timestarted', 'enrol'),
123
            $this->get_entity_name()
124
        ))
125
            ->add_joins($this->get_joins())
126
            ->set_type(column::TYPE_TIMESTAMP)
127
            ->add_field("
128
                CASE WHEN {$userenrolments}.timestart = 0
129
                     THEN {$userenrolments}.timecreated
130
                     ELSE {$userenrolments}.timestart
131
                 END", 'timestarted')
132
            ->set_is_sortable(true)
133
            ->add_callback([format::class, 'userdate']);
134
 
135
        // Enrolment time ended.
136
        $columns[] = (new column(
137
            'timeended',
138
            new lang_string('timeended', 'enrol'),
139
            $this->get_entity_name()
140
        ))
141
            ->add_joins($this->get_joins())
142
            ->set_type(column::TYPE_TIMESTAMP)
143
            ->add_field("{$userenrolments}.timeend")
144
            ->set_is_sortable(true)
145
            ->add_callback([format::class, 'userdate']);
146
 
147
        // Enrolment status.
148
        $columns[] = (new column(
149
            'status',
150
            new lang_string('status', 'moodle'),
151
            $this->get_entity_name()
152
        ))
153
            ->add_joins($this->get_joins())
154
            ->set_type(column::TYPE_TEXT)
155
            ->add_field($this->get_status_field_sql(), 'status')
156
            ->set_is_sortable(true)
157
            ->add_callback([enrolment_formatter::class, 'enrolment_status']);
158
 
159
        // Role column (Deprecated since Moodle 4.3, to remove in MDL-78118).
160
        $ctx = database::generate_alias();
161
        $ra = database::generate_alias();
162
        $r = database::generate_alias();
163
        $columns[] = (new column(
164
            'role',
165
            new lang_string('role', 'moodle'),
166
            $this->get_entity_name()
167
        ))
168
            ->add_joins($this->get_joins())
169
            ->add_join("LEFT JOIN {context} {$ctx}
170
                ON {$ctx}.instanceid = {$enrol}.courseid AND {$ctx}.contextlevel = " . CONTEXT_COURSE)
171
            ->add_join("LEFT JOIN {role_assignments} {$ra}
172
                ON {$ra}.contextid = {$ctx}.id AND {$ra}.userid = {$userenrolments}.userid")
173
            ->add_join("LEFT JOIN {role} {$r} ON {$r}.id = {$ra}.roleid")
174
            ->set_type(column::TYPE_TEXT)
175
            ->add_fields("{$r}.id, {$r}.name, {$r}.shortname, {$ctx}.instanceid")
176
            ->set_is_sortable(true, ["{$r}.shortname"])
177
            ->set_is_deprecated('See \'role:name\' for replacement')
178
            ->add_callback(static function(?string $value, stdClass $row): string {
179
                if (!$row->id) {
180
                    return '';
181
                }
182
                $context = context_course::instance($row->instanceid);
183
                return role_get_name($row, $context, ROLENAME_ALIAS);
184
            });
185
 
186
        return $columns;
187
    }
188
 
189
    /**
190
     * Generate SQL snippet suitable for returning enrolment status field
191
     *
192
     * @return string
193
     */
194
    private function get_status_field_sql(): string {
195
        $time = time();
196
        $userenrolments = $this->get_table_alias('user_enrolments');
197
        $enrol = $this->get_table_alias('enrol');
198
 
199
        return "
200
            CASE WHEN {$userenrolments}.status = " . ENROL_USER_ACTIVE . "
201
                 THEN CASE WHEN ({$userenrolments}.timestart > {$time})
202
                             OR ({$userenrolments}.timeend > 0 AND {$userenrolments}.timeend < {$time})
203
                             OR ({$enrol}.status = " . ENROL_INSTANCE_DISABLED . ")
204
                           THEN " . status_field::STATUS_NOT_CURRENT . "
205
                           ELSE " . status_field::STATUS_ACTIVE . "
206
                      END
207
                 ELSE {$userenrolments}.status
208
            END";
209
    }
210
 
211
    /**
212
     * Return list of all available filters
213
     *
214
     * @return filter[]
215
     */
216
    protected function get_all_filters(): array {
217
        $userenrolments = $this->get_table_alias('user_enrolments');
218
        $enrol = $this->get_table_alias('enrol');
219
 
220
        // Enrolment method (Deprecated since Moodle 4.3, to remove in MDL-78118).
221
        $enrolmentmethods = static function(): array {
222
            return array_map(static function(enrol_plugin $plugin): string {
223
                return get_string('pluginname', 'enrol_' . $plugin->get_name());
224
            }, enrol_get_plugins(true));
225
        };
226
        $filters[] = (new filter(
227
            select::class,
228
            'method',
229
            new lang_string('method', 'enrol'),
230
            $this->get_entity_name(),
231
            "{$enrol}.enrol"
232
        ))
233
            ->add_joins($this->get_joins())
234
            ->set_is_deprecated('See \'enrol:plugin\' for replacement')
235
            ->set_options_callback($enrolmentmethods);
236
 
237
        // Enrolment time created.
238
        $filters[] = (new filter(
239
            date::class,
240
            'timecreated',
241
            new lang_string('timecreated', 'moodle'),
242
            $this->get_entity_name(),
243
            "{$userenrolments}.timecreated"
244
        ))
245
            ->add_joins($this->get_joins())
246
            ->set_limited_operators([
247
                date::DATE_ANY,
248
                date::DATE_NOT_EMPTY,
249
                date::DATE_EMPTY,
250
                date::DATE_RANGE,
251
                date::DATE_LAST,
252
                date::DATE_CURRENT,
253
            ]);
254
 
255
        // Enrolment time started.
256
        $filters[] = (new filter(
257
            date::class,
258
            'timestarted',
259
            new lang_string('timestarted', 'enrol'),
260
            $this->get_entity_name(),
261
            "CASE WHEN {$userenrolments}.timestart = 0
262
                          THEN {$userenrolments}.timecreated
263
                          ELSE {$userenrolments}.timestart
264
                      END"
265
        ))
266
            ->add_joins($this->get_joins())
267
            ->set_limited_operators([
268
                date::DATE_ANY,
269
                date::DATE_NOT_EMPTY,
270
                date::DATE_EMPTY,
271
                date::DATE_RANGE,
272
                date::DATE_LAST,
273
                date::DATE_CURRENT,
274
            ]);
275
 
276
        // Enrolment time ended.
277
        $filters[] = (new filter(
278
            date::class,
279
            'timeended',
280
            new lang_string('timeended', 'enrol'),
281
            $this->get_entity_name(),
282
            "{$userenrolments}.timeend"
283
        ))
284
            ->add_joins($this->get_joins())
285
            ->set_limited_operators([
286
                date::DATE_ANY,
287
                date::DATE_NOT_EMPTY,
288
                date::DATE_EMPTY,
289
                date::DATE_RANGE,
290
                date::DATE_LAST,
291
                date::DATE_CURRENT,
292
            ]);
293
 
294
        // Enrolment status.
295
        $filters[] = (new filter(
296
            select::class,
297
            'status',
298
            new lang_string('status', 'moodle'),
299
            $this->get_entity_name(),
300
            $this->get_status_field_sql()
301
        ))
302
            ->add_joins($this->get_joins())
303
            ->set_options(enrolment_formatter::enrolment_values());
304
 
305
        return $filters;
306
    }
307
}