| 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 |
|
|
|
93 |
// Enrolment time created.
|
|
|
94 |
$columns[] = (new column(
|
|
|
95 |
'timecreated',
|
|
|
96 |
new lang_string('timecreated', 'moodle'),
|
|
|
97 |
$this->get_entity_name()
|
|
|
98 |
))
|
|
|
99 |
->add_joins($this->get_joins())
|
|
|
100 |
->set_type(column::TYPE_TIMESTAMP)
|
|
|
101 |
->add_field("{$userenrolments}.timecreated")
|
|
|
102 |
->set_is_sortable(true)
|
|
|
103 |
->add_callback([format::class, 'userdate']);
|
|
|
104 |
|
|
|
105 |
// Enrolment time started.
|
|
|
106 |
$columns[] = (new column(
|
|
|
107 |
'timestarted',
|
|
|
108 |
new lang_string('timestarted', 'enrol'),
|
|
|
109 |
$this->get_entity_name()
|
|
|
110 |
))
|
|
|
111 |
->add_joins($this->get_joins())
|
|
|
112 |
->set_type(column::TYPE_TIMESTAMP)
|
|
|
113 |
->add_field("
|
|
|
114 |
CASE WHEN {$userenrolments}.timestart = 0
|
|
|
115 |
THEN {$userenrolments}.timecreated
|
|
|
116 |
ELSE {$userenrolments}.timestart
|
|
|
117 |
END", 'timestarted')
|
|
|
118 |
->set_is_sortable(true)
|
|
|
119 |
->add_callback([format::class, 'userdate']);
|
|
|
120 |
|
|
|
121 |
// Enrolment time ended.
|
|
|
122 |
$columns[] = (new column(
|
|
|
123 |
'timeended',
|
|
|
124 |
new lang_string('timeended', 'enrol'),
|
|
|
125 |
$this->get_entity_name()
|
|
|
126 |
))
|
|
|
127 |
->add_joins($this->get_joins())
|
|
|
128 |
->set_type(column::TYPE_TIMESTAMP)
|
|
|
129 |
->add_field("{$userenrolments}.timeend")
|
|
|
130 |
->set_is_sortable(true)
|
|
|
131 |
->add_callback([format::class, 'userdate']);
|
|
|
132 |
|
|
|
133 |
// Enrolment status.
|
|
|
134 |
$columns[] = (new column(
|
|
|
135 |
'status',
|
|
|
136 |
new lang_string('status', 'moodle'),
|
|
|
137 |
$this->get_entity_name()
|
|
|
138 |
))
|
|
|
139 |
->add_joins($this->get_joins())
|
|
|
140 |
->set_type(column::TYPE_TEXT)
|
|
|
141 |
->add_field($this->get_status_field_sql(), 'status')
|
|
|
142 |
->set_is_sortable(true)
|
|
|
143 |
->add_callback([enrolment_formatter::class, 'enrolment_status']);
|
|
|
144 |
|
|
|
145 |
return $columns;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Generate SQL snippet suitable for returning enrolment status field
|
|
|
150 |
*
|
|
|
151 |
* @return string
|
|
|
152 |
*/
|
|
|
153 |
private function get_status_field_sql(): string {
|
|
|
154 |
$time = time();
|
|
|
155 |
$userenrolments = $this->get_table_alias('user_enrolments');
|
|
|
156 |
$enrol = $this->get_table_alias('enrol');
|
|
|
157 |
|
|
|
158 |
return "
|
|
|
159 |
CASE WHEN {$userenrolments}.status = " . ENROL_USER_ACTIVE . "
|
|
|
160 |
THEN CASE WHEN ({$userenrolments}.timestart > {$time})
|
|
|
161 |
OR ({$userenrolments}.timeend > 0 AND {$userenrolments}.timeend < {$time})
|
|
|
162 |
OR ({$enrol}.status = " . ENROL_INSTANCE_DISABLED . ")
|
|
|
163 |
THEN " . status_field::STATUS_NOT_CURRENT . "
|
|
|
164 |
ELSE " . status_field::STATUS_ACTIVE . "
|
|
|
165 |
END
|
|
|
166 |
ELSE {$userenrolments}.status
|
|
|
167 |
END";
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
/**
|
|
|
171 |
* Return list of all available filters
|
|
|
172 |
*
|
|
|
173 |
* @return filter[]
|
|
|
174 |
*/
|
|
|
175 |
protected function get_all_filters(): array {
|
|
|
176 |
$userenrolments = $this->get_table_alias('user_enrolments');
|
|
|
177 |
|
|
|
178 |
// Enrolment time created.
|
|
|
179 |
$filters[] = (new filter(
|
|
|
180 |
date::class,
|
|
|
181 |
'timecreated',
|
|
|
182 |
new lang_string('timecreated', 'moodle'),
|
|
|
183 |
$this->get_entity_name(),
|
|
|
184 |
"{$userenrolments}.timecreated"
|
|
|
185 |
))
|
|
|
186 |
->add_joins($this->get_joins())
|
|
|
187 |
->set_limited_operators([
|
|
|
188 |
date::DATE_ANY,
|
|
|
189 |
date::DATE_NOT_EMPTY,
|
|
|
190 |
date::DATE_EMPTY,
|
|
|
191 |
date::DATE_RANGE,
|
|
|
192 |
date::DATE_LAST,
|
|
|
193 |
date::DATE_CURRENT,
|
|
|
194 |
]);
|
|
|
195 |
|
|
|
196 |
// Enrolment time started.
|
|
|
197 |
$filters[] = (new filter(
|
|
|
198 |
date::class,
|
|
|
199 |
'timestarted',
|
|
|
200 |
new lang_string('timestarted', 'enrol'),
|
|
|
201 |
$this->get_entity_name(),
|
|
|
202 |
"CASE WHEN {$userenrolments}.timestart = 0
|
|
|
203 |
THEN {$userenrolments}.timecreated
|
|
|
204 |
ELSE {$userenrolments}.timestart
|
|
|
205 |
END"
|
|
|
206 |
))
|
|
|
207 |
->add_joins($this->get_joins())
|
|
|
208 |
->set_limited_operators([
|
|
|
209 |
date::DATE_ANY,
|
|
|
210 |
date::DATE_NOT_EMPTY,
|
|
|
211 |
date::DATE_EMPTY,
|
|
|
212 |
date::DATE_RANGE,
|
|
|
213 |
date::DATE_LAST,
|
|
|
214 |
date::DATE_CURRENT,
|
|
|
215 |
]);
|
|
|
216 |
|
|
|
217 |
// Enrolment time ended.
|
|
|
218 |
$filters[] = (new filter(
|
|
|
219 |
date::class,
|
|
|
220 |
'timeended',
|
|
|
221 |
new lang_string('timeended', 'enrol'),
|
|
|
222 |
$this->get_entity_name(),
|
|
|
223 |
"{$userenrolments}.timeend"
|
|
|
224 |
))
|
|
|
225 |
->add_joins($this->get_joins())
|
|
|
226 |
->set_limited_operators([
|
|
|
227 |
date::DATE_ANY,
|
|
|
228 |
date::DATE_NOT_EMPTY,
|
|
|
229 |
date::DATE_EMPTY,
|
|
|
230 |
date::DATE_RANGE,
|
|
|
231 |
date::DATE_LAST,
|
|
|
232 |
date::DATE_CURRENT,
|
|
|
233 |
]);
|
|
|
234 |
|
|
|
235 |
// Enrolment status.
|
|
|
236 |
$filters[] = (new filter(
|
|
|
237 |
select::class,
|
|
|
238 |
'status',
|
|
|
239 |
new lang_string('status', 'moodle'),
|
|
|
240 |
$this->get_entity_name(),
|
|
|
241 |
$this->get_status_field_sql()
|
|
|
242 |
))
|
|
|
243 |
->add_joins($this->get_joins())
|
|
|
244 |
->set_options(enrolment_formatter::enrolment_values());
|
|
|
245 |
|
|
|
246 |
return $filters;
|
|
|
247 |
}
|
|
|
248 |
}
|