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
namespace core_enrol\reportbuilder\local\entities;
18
 
19
use enrol_plugin;
20
use lang_string;
21
use stdClass;
22
use core_reportbuilder\local\entities\base;
23
use core_reportbuilder\local\filters\{boolean_select, date, duration, select, text};
24
use core_reportbuilder\local\helpers\format;
25
use core_reportbuilder\local\report\{column, filter};
26
 
27
/**
28
 * Enrolment method entity
29
 *
30
 * @package     core_enrol
31
 * @copyright   2023 Paul Holden <paulh@moodle.com>
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class enrol extends base {
35
 
36
    /**
37
     * Database tables that this entity uses
38
     *
39
     * @return string[]
40
     */
41
    protected function get_default_tables(): array {
42
        return [
43
            'enrol',
44
        ];
45
    }
46
 
47
    /**
48
     * The default title for this entity
49
     *
50
     * @return lang_string
51
     */
52
    protected function get_default_entity_title(): lang_string {
53
        return new lang_string('enrolmentmethod', 'core_enrol');
54
    }
55
 
56
    /**
57
     * Initialise the entity
58
     *
59
     * @return base
60
     */
61
    public function initialise(): base {
62
        $columns = $this->get_all_columns();
63
        foreach ($columns as $column) {
64
            $this->add_column($column);
65
        }
66
 
67
        // All the filters defined by the entity can also be used as conditions.
68
        $filters = $this->get_all_filters();
69
        foreach ($filters as $filter) {
70
            $this
71
                ->add_filter($filter)
72
                ->add_condition($filter);
73
        }
74
 
75
        return $this;
76
    }
77
 
78
    /**
79
     * Returns list of all available columns
80
     *
81
     * @return column[]
82
     */
83
    protected function get_all_columns(): array {
84
        global $DB;
85
 
86
        $enrolalias = $this->get_table_alias('enrol');
87
 
88
        // Plugin column.
89
        $columns[] = (new column(
90
            'plugin',
91
            new lang_string('plugin'),
92
            $this->get_entity_name()
93
        ))
94
            ->add_joins($this->get_joins())
95
            ->set_type(column::TYPE_TEXT)
96
            ->add_fields("{$enrolalias}.enrol")
97
            ->set_is_sortable(true)
98
            ->set_callback(static function(?string $enrol): string {
99
                if ($enrol === null || !$plugin = enrol_get_plugin($enrol)) {
100
                    return '';
101
                }
102
 
103
                return $plugin->get_instance_name(null);
104
            });
105
 
106
        // Name column.
107
        $columns[] = (new column(
108
            'name',
109
            new lang_string('name'),
110
            $this->get_entity_name()
111
        ))
112
            ->add_joins($this->get_joins())
113
            ->set_type(column::TYPE_TEXT)
114
            ->add_fields("{$enrolalias}.enrol, {$enrolalias}.name, {$enrolalias}.courseid, " .
115
                "{$enrolalias}.roleid, {$enrolalias}.customint1")
116
            ->set_is_sortable(true)
117
            ->set_callback(static function(?string $enrol, stdClass $instance): string {
118
                if ($enrol === null || !$plugin = enrol_get_plugin($enrol)) {
119
                    return '';
120
                }
121
 
122
                return $plugin->get_instance_name($instance);
123
            });
124
 
125
        // Enabled column.
126
        $columns[] = (new column(
127
            'enabled',
128
            new lang_string('enabled', 'core_admin'),
129
            $this->get_entity_name()
130
        ))
131
            ->add_joins($this->get_joins())
132
            ->set_type(column::TYPE_BOOLEAN)
133
            // For accurate aggregation, we need to return boolean enabled = true by xor'ing the field value.
134
            ->add_field($DB->sql_bitxor("{$enrolalias}.status", 1), 'status')
135
            ->set_is_sortable(true)
136
            ->set_callback([format::class, 'boolean_as_text']);
137
 
138
        // Period column.
139
        $columns[] = (new column(
140
            'period',
141
            new lang_string('enrolperiod', 'core_enrol'),
142
            $this->get_entity_name()
143
        ))
144
            ->add_joins($this->get_joins())
145
            ->set_type(column::TYPE_TIMESTAMP)
146
            ->add_fields("{$enrolalias}.enrolperiod")
147
            ->set_is_sortable(true)
1441 ariadna 148
            ->set_callback(static function(?int $enrolperiod, stdClass $row): string {
149
                if ($enrolperiod === 0) {
1 efrain 150
                    return '';
151
                }
1441 ariadna 152
                return format::format_time($enrolperiod, $row);
1 efrain 153
            });
154
 
155
        // Start date column.
156
        $columns[] = (new column(
157
            'startdate',
158
            new lang_string('enroltimestart', 'core_enrol'),
159
            $this->get_entity_name()
160
        ))
161
            ->add_joins($this->get_joins())
162
            ->set_type(column::TYPE_TIMESTAMP)
163
            ->add_fields("{$enrolalias}.enrolstartdate")
164
            ->set_is_sortable(true)
165
            ->set_callback([format::class, 'userdate']);
166
 
167
        // End date column.
168
        $columns[] = (new column(
169
            'enddate',
170
            new lang_string('enroltimeend', 'core_enrol'),
171
            $this->get_entity_name()
172
        ))
173
            ->add_joins($this->get_joins())
174
            ->set_type(column::TYPE_TIMESTAMP)
175
            ->add_fields("{$enrolalias}.enrolenddate")
176
            ->set_is_sortable(true)
177
            ->set_callback([format::class, 'userdate']);
178
 
179
        return $columns;
180
    }
181
 
182
    /**
183
     * Return list of all available filters
184
     *
185
     * @return filter[]
186
     */
187
    protected function get_all_filters(): array {
188
        global $DB;
189
 
190
        $enrolalias = $this->get_table_alias('enrol');
191
 
192
        // Plugin filter.
193
        $filters[] = (new filter(
194
            select::class,
195
            'plugin',
196
            new lang_string('plugin'),
197
            $this->get_entity_name(),
198
            "{$enrolalias}.enrol"
199
        ))
200
            ->add_joins($this->get_joins())
201
            ->set_options_callback(static function(): array {
202
                return array_map(static function(enrol_plugin $plugin): string {
203
                    return $plugin->get_instance_name(null);
204
                }, enrol_get_plugins(true));
205
            });
206
 
207
        // Custom name filter.
208
        $filters[] = (new filter(
209
            text::class,
210
            'customname',
211
            new lang_string('custominstancename', 'core_enrol'),
212
            $this->get_entity_name(),
213
            "{$enrolalias}.name"
214
        ))
215
            ->add_joins($this->get_joins());
216
 
217
        // Enabled filter.
218
        $filters[] = (new filter(
219
            boolean_select::class,
220
            'enabled',
221
            new lang_string('enabled', 'core_admin'),
222
            $this->get_entity_name(),
223
            $DB->sql_bitxor("{$enrolalias}.status", 1)
224
        ))
225
            ->add_joins($this->get_joins());
226
 
227
        // Period filter.
228
        $filters[] = (new filter(
229
            duration::class,
230
            'period',
231
            new lang_string('enrolperiod', 'core_enrol'),
232
            $this->get_entity_name(),
233
            "{$enrolalias}.enrolperiod"
234
        ))
235
            ->add_joins($this->get_joins());
236
 
237
        // Start date filter.
238
        $filters[] = (new filter(
239
            date::class,
240
            'startdate',
241
            new lang_string('enroltimestart', 'core_enrol'),
242
            $this->get_entity_name(),
243
            "{$enrolalias}.enrolstartdate"
244
        ))
245
            ->add_joins($this->get_joins());
246
 
247
        // End date filter.
248
        $filters[] = (new filter(
249
            date::class,
250
            'enddate',
251
            new lang_string('enroltimeend', 'core_enrol'),
252
            $this->get_entity_name(),
253
            "{$enrolalias}.enrolenddate"
254
        ))
255
            ->add_joins($this->get_joins());
256
 
257
        return $filters;
258
    }
259
}