-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcondition_base.php
275 lines (231 loc) · 6.6 KB
/
condition_base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
namespace tool_dynamic_cohorts;
/**
* Abstract class that all conditions must extend.
*
* @package tool_dynamic_cohorts
* @copyright 2024 Catalyst IT
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class condition_base {
/**
* Value for text field types.
*/
public const FIELD_DATA_TYPE_TEXT = 'text';
/**
* Value for menu field types.
*/
public const FIELD_DATA_TYPE_MENU = 'menu';
/**
* Value for select field types.
*/
public const FIELD_DATA_TYPE_SELECT = 'select';
/**
* Value for checkbox field types.
*/
public const FIELD_DATA_TYPE_CHECKBOX = 'checkbox';
/**
* Value for date field types.
*/
public const FIELD_DATA_TYPE_DATE = 'date';
/**
* Value for date field types.
*/
public const FIELD_DATA_TYPE_DATETIME = 'datetime';
/**
* Value for autocomplete field types.
*/
public const FIELD_DATA_TYPE_AUTOCOMPLETE = 'autocomplete';
/**
* Value for operator text contains.
*/
public const TEXT_CONTAINS = 1;
/**
* Value for operator text doesn't contain.
*/
public const TEXT_DOES_NOT_CONTAIN = 2;
/**
* Value for operator text is equal to.
*/
public const TEXT_IS_EQUAL_TO = 3;
/**
* Value for operator text starts with.
*/
public const TEXT_STARTS_WITH = 4;
/**
* Value for operator text ends with.
*/
public const TEXT_ENDS_WITH = 5;
/**
* Value for operator text is empty.
*/
public const TEXT_IS_EMPTY = 6;
/**
* Value for operator text is not empty.
*/
public const TEXT_IS_NOT_EMPTY = 7;
/**
* Value for operator text is not equal to.
*/
public const TEXT_IS_NOT_EQUAL_TO = 8;
/**
* Value for operator date is after.
*/
public const DATE_IS_AFTER = 1;
/**
* Value for operator date is before.
*/
public const DATE_IS_BEFORE = 2;
/**
* Value for operator date is in the past.
*/
public const DATE_IN_THE_PAST = 30;
/**
* Value for operator date is in the future.
*/
public const DATE_IN_THE_FUTURE = 40;
/**
* Condition persistent object.
*
* @var condition $condition
*/
protected $condition;
/**
* Protected constructor.
*/
protected function __construct() {
}
/**
* Return an instance of condition object.
*
* @param int $id
* @param \stdClass|null $record
* @return \tool_dynamic_cohorts\condition_base|null
*/
final public static function get_instance(int $id = 0, ?\stdClass $record = null): ?condition_base {
$condition = new condition($id, $record);
// In case we are getting the instance without underlying persistent data.
if (!$classname = $condition->get('classname')) {
$classname = get_called_class();
$condition->set('classname', $classname);
}
if (!class_exists($classname) || !is_subclass_of($classname, self::class)) {
return null;
}
$instance = new $classname();
$instance->condition = $condition;
return $instance;
}
/**
* Gets required config data from submitted condition form data.
*
* @param \stdClass $formdata Form data generated via $mform->get_data()
* @return array
*/
public static function retrieve_config_data(\stdClass $formdata): array {
$configdata = (array)$formdata;
// Everything except these fields is considered as config data.
unset($configdata['id']);
unset($configdata['ruleid']);
unset($configdata['sortorder']);
return $configdata;
}
/**
* A config data for that condition.
*
* @return array
*/
public function get_config_data(): array {
return json_decode($this->condition->get('configdata'), true);
}
/**
* Sets config data.
*
* @param array $configdata
*/
public function set_config_data(array $configdata): void {
$this->condition->set('configdata', json_encode($configdata));
}
/**
* Gets condition persistent record.
*
* @return condition
*/
public function get_record(): condition {
return $this->condition;
}
/**
* Returns a rule record for the given condition.
*
* @return rule|null
*/
public function get_rule(): ?rule {
return rule::get_record(['id' => $this->get_record()->get('ruleid')]) ?: null;
}
/**
* Gets a list of event classes the condition will be triggered on.
*
* @return array
*/
public function get_events(): array {
return [];
}
/**
* Human readable description of the broken condition.
*
* @return string
*/
public function get_broken_description(): string {
return $this->get_record()->get('configdata');
}
/**
* Returns the name of the condition
*
* @return string
*/
abstract public function get_name(): string;
/**
* Add condition config form elements.
*
* @param \MoodleQuickForm $mform The form to add elements to
*/
abstract public function config_form_add(\MoodleQuickForm $mform): void;
/**
* Validates conditions form elements.
*
* @param array $data Form data.
* @return array Errors array.
*/
abstract public function config_form_validate(array $data): array;
/**
* Returns elements to extend SQL for filtering users.
* @return condition_sql
*/
abstract public function get_sql(): condition_sql;
/**
* Human-readable description of the configured condition.
*
* @return string
*/
abstract public function get_config_description(): string;
/**
* Indicate that condition is broken.
*
* @return bool
*/
abstract public function is_broken(): bool;
}