Commit | Line | Data |
---|---|---|
69dd0c8c EL |
1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | /** | |
19 | * @package moodlecore | |
20 | * @subpackage backup-settings | |
21 | * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
25 | /** | |
26 | * This abstract class defines one basic setting | |
27 | * | |
28 | * Each setting will be able to control its name, value (from a list), ui | |
29 | * representation (check box, drop down, text field...), visibility, status | |
30 | * (editable/locked...) and its hierarchy with other settings (using one | |
31 | * like-observer pattern. | |
32 | * | |
33 | * TODO: Finish phpdocs | |
34 | */ | |
35 | abstract class base_setting { | |
36 | ||
37 | // Some constants defining different ui representations for the setting | |
38 | const UI_NONE = 0; | |
39 | const UI_HTML_CHECKBOX = 10; | |
40 | const UI_HTML_RADIOBUTTON = 20; | |
41 | const UI_HTML_DROPDOWN = 30; | |
42 | const UI_HTML_TEXTFIELD = 40; | |
43 | ||
44 | // Type of validation to perform against the value (relaying in PARAM_XXX validations) | |
45 | const IS_BOOLEAN = 'bool'; | |
46 | const IS_INTEGER = 'int'; | |
47 | const IS_FILENAME= 'file'; | |
48 | const IS_PATH = 'path'; | |
49 | ||
50 | // Visible/hidden | |
51 | const VISIBLE = 1; | |
52 | const HIDDEN = 0; | |
53 | ||
54 | // Editable/locked (by different causes) | |
ce937f99 EL |
55 | const NOT_LOCKED = 3; |
56 | const LOCKED_BY_CONFIG = 5; | |
69dd0c8c | 57 | const LOCKED_BY_HIERARCHY = 7; |
ce937f99 | 58 | const LOCKED_BY_PERMISSION = 9; |
69dd0c8c EL |
59 | |
60 | // Type of change to inform dependencies | |
61 | const CHANGED_VALUE = 1; | |
62 | const CHANGED_VISIBILITY = 2; | |
63 | const CHANGED_STATUS = 3; | |
64 | ||
65 | protected $name; // name of the setting | |
66 | protected $value; // value of the setting | |
67 | protected $vtype; // type of value (setting_base::IS_BOOLEAN/setting_base::IS_INTEGER...) | |
68 | ||
69 | protected $visibility; // visibility of the setting (setting_base::VISIBLE/setting_base::HIDDEN) | |
70 | protected $status; // setting_base::NOT_LOCKED/setting_base::LOCKED_BY_PERMISSION... | |
71 | ||
72d5e574 SH |
72 | protected $dependencies = array(); // array of dependent (observer) objects (usually setting_base ones) |
73 | protected $dependenton = array(); | |
69dd0c8c | 74 | |
1904e9b3 | 75 | /** |
f6ae3f01 | 76 | * The user interface for this setting |
1904e9b3 SH |
77 | * @var backup_setting_ui|backup_setting_ui_checkbox|backup_setting_ui_radio|backup_setting_ui_select|backup_setting_ui_text |
78 | */ | |
79 | protected $uisetting; | |
80 | ||
1a83fcb5 | 81 | /** |
f6ae3f01 SH |
82 | * An array that contains the identifier and component of a help string if one |
83 | * has been set | |
1a83fcb5 SH |
84 | * @var array |
85 | */ | |
86 | protected $help = array(); | |
87 | ||
69dd0c8c EL |
88 | public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) { |
89 | // Check vtype | |
90 | if ($vtype !== self::IS_BOOLEAN && $vtype !== self::IS_INTEGER && | |
91 | $vtype !== self::IS_FILENAME && $vtype !== self::IS_PATH) { | |
92 | throw new base_setting_exception('setting_invalid_type'); | |
93 | } | |
94 | ||
95 | // Validate value | |
96 | $value = $this->validate_value($vtype, $value); | |
97 | ||
98 | // Check visibility | |
99 | $visibility = $this->validate_visibility($visibility); | |
100 | ||
101 | // Check status | |
102 | $status = $this->validate_status($status); | |
103 | ||
104 | $this->name = $name; | |
105 | $this->vtype = $vtype; | |
106 | $this->value = $value; | |
107 | $this->visibility = $visibility; | |
108 | $this->status = $status; | |
69dd0c8c | 109 | |
1904e9b3 SH |
110 | // Generate a default ui |
111 | $this->uisetting = new backup_setting_ui_checkbox($this, $name); | |
69dd0c8c EL |
112 | } |
113 | ||
114 | public function get_name() { | |
115 | return $this->name; | |
116 | } | |
117 | ||
118 | public function get_value() { | |
119 | return $this->value; | |
120 | } | |
121 | ||
122 | public function get_visibility() { | |
123 | return $this->visibility; | |
124 | } | |
125 | ||
126 | public function get_status() { | |
127 | return $this->status; | |
128 | } | |
129 | ||
130 | public function set_value($value) { | |
131 | // Validate value | |
132 | $value = $this->validate_value($this->vtype, $value); | |
133 | // Only can change value if setting is not locked | |
134 | if ($this->status != self::NOT_LOCKED) { | |
135 | switch ($this->status) { | |
136 | case self::LOCKED_BY_PERMISSION: | |
137 | throw new base_setting_exception('setting_locked_by_permission'); | |
cd0034d8 EL |
138 | case self::LOCKED_BY_CONFIG: |
139 | throw new base_setting_exception('setting_locked_by_config'); | |
69dd0c8c EL |
140 | } |
141 | } | |
142 | $oldvalue = $this->value; | |
143 | $this->value = $value; | |
144 | if ($value !== $oldvalue) { // Value has changed, let's inform dependencies | |
145 | $this->inform_dependencies(self::CHANGED_VALUE, $oldvalue); | |
146 | } | |
147 | } | |
148 | ||
149 | public function set_visibility($visibility) { | |
150 | $visibility = $this->validate_visibility($visibility); | |
72d5e574 SH |
151 | |
152 | // If this setting is dependent on other settings first check that all | |
153 | // of those settings are visible | |
154 | if (count($this->dependenton) > 0 && $visibility == base_setting::VISIBLE) { | |
155 | foreach ($this->dependenton as $dependency) { | |
156 | if ($dependency->get_setting()->get_visibility() != base_setting::VISIBLE) { | |
157 | $visibility = base_setting::HIDDEN; | |
158 | break; | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
69dd0c8c EL |
163 | $oldvisibility = $this->visibility; |
164 | $this->visibility = $visibility; | |
165 | if ($visibility !== $oldvisibility) { // Visibility has changed, let's inform dependencies | |
166 | $this->inform_dependencies(self::CHANGED_VISIBILITY, $oldvisibility); | |
167 | } | |
168 | } | |
169 | ||
170 | public function set_status($status) { | |
171 | $status = $this->validate_status($status); | |
72d5e574 SH |
172 | |
173 | // If this setting is dependent on other settings first check that all | |
174 | // of those settings are not locked | |
175 | if (count($this->dependenton) > 0 && $status == base_setting::NOT_LOCKED) { | |
176 | foreach ($this->dependenton as $dependency) { | |
177 | if ($dependency->get_setting()->get_status() != base_setting::NOT_LOCKED) { | |
178 | $status = base_setting::LOCKED_BY_HIERARCHY; | |
179 | break; | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
69dd0c8c EL |
184 | $oldstatus = $this->status; |
185 | $this->status = $status; | |
186 | if ($status !== $oldstatus) { // Status has changed, let's inform dependencies | |
187 | $this->inform_dependencies(self::CHANGED_STATUS, $oldstatus); | |
188 | } | |
189 | } | |
190 | ||
1904e9b3 SH |
191 | public function set_ui(backup_setting_ui $ui) { |
192 | $this->uisetting = $ui; | |
69dd0c8c EL |
193 | } |
194 | ||
1904e9b3 SH |
195 | public function make_ui($type, $label, array $attributes = null, array $options = null) { |
196 | $type = $this->validate_ui_type($type); | |
197 | $label = $this->validate_ui_label($label); | |
198 | $this->uisetting = backup_setting_ui::make($this, $type, $label, $attributes, $options); | |
199 | if (is_array($options) || is_object($options)) { | |
200 | $options = (array)$options; | |
201 | switch (get_class($this->uisetting)) { | |
202 | case 'backup_setting_ui_radio' : | |
203 | // text | |
204 | if (array_key_exists('text', $options)) { | |
205 | $this->uisetting->set_text($options['text']); | |
206 | } | |
207 | case 'backup_setting_ui_checkbox' : | |
208 | // value | |
209 | if (array_key_exists('value', $options)) { | |
210 | $this->uisetting->set_value($options['value']); | |
211 | } | |
212 | break; | |
213 | case 'backup_setting_ui_select' : | |
214 | // options | |
215 | if (array_key_exists('options', $options)) { | |
216 | $this->uisetting->set_values($options['options']); | |
217 | } | |
218 | break; | |
219 | } | |
220 | } | |
69dd0c8c EL |
221 | } |
222 | ||
1904e9b3 SH |
223 | public function get_ui() { |
224 | return $this->uisetting; | |
69dd0c8c EL |
225 | } |
226 | ||
72d5e574 SH |
227 | /** |
228 | * Adds a dependency where another setting depends on this setting. | |
229 | * @param setting_dependency $dependency | |
230 | */ | |
231 | public function register_dependency(setting_dependency $dependency) { | |
232 | if ($this->is_circular_reference($dependency->get_dependant_setting())) { | |
233 | $a = new stdclass(); | |
234 | $a->alreadydependent = $this->name; | |
235 | $a->main = $dependentsetting->get_name(); | |
236 | throw new base_setting_exception('setting_circular_reference', $a); | |
237 | } | |
238 | $this->dependencies[$dependency->get_dependant_setting()->get_name()] = $dependency; | |
239 | $dependency->get_dependant_setting()->register_dependent_dependency($dependency); | |
240 | } | |
241 | /** | |
242 | * Adds a dependency where this setting is dependent on another. | |
243 | * | |
244 | * This should only be called internally once we are sure it is not cicrular. | |
245 | * | |
246 | * @param setting_dependency $dependency | |
247 | */ | |
248 | protected function register_dependent_dependency(setting_dependency $dependency) { | |
249 | $this->dependenton[$dependency->get_setting()->get_name()] = $dependency; | |
250 | } | |
251 | ||
1904e9b3 SH |
252 | public function add_dependency(base_setting $dependentsetting, $type=null, $options=array()) { |
253 | if ($this->is_circular_reference($dependentsetting)) { | |
69dd0c8c EL |
254 | $a = new stdclass(); |
255 | $a->alreadydependent = $this->name; | |
1904e9b3 | 256 | $a->main = $dependentsetting->get_name(); |
69dd0c8c EL |
257 | throw new base_setting_exception('setting_circular_reference', $a); |
258 | } | |
259 | // Check the settings hasn't been already added | |
1904e9b3 | 260 | if (array_key_exists($dependentsetting->get_name(), $this->dependencies)) { |
69dd0c8c EL |
261 | throw new base_setting_exception('setting_already_added'); |
262 | } | |
1904e9b3 SH |
263 | |
264 | $options = (array)$options; | |
265 | ||
266 | if (!array_key_exists('defaultvalue', $options)) { | |
267 | $options['defaultvalue'] = false; | |
268 | } | |
269 | ||
270 | if ($type == null) { | |
271 | switch ($this->vtype) { | |
272 | case self::IS_BOOLEAN : | |
273 | if ($this->value) { | |
274 | $type = setting_dependency::DISABLED_FALSE; | |
275 | } else { | |
276 | $type = setting_dependency::DISABLED_TRUE; | |
277 | } | |
278 | break; | |
279 | case self::IS_FILENAME : | |
280 | case self::IS_PATH : | |
281 | case self::IS_INTEGER : | |
282 | default : | |
283 | $type = setting_dependency::DISABLED_VALUE; | |
284 | break; | |
285 | } | |
286 | } | |
287 | ||
288 | switch ($type) { | |
289 | case setting_dependency::DISABLED_VALUE : | |
290 | if (!array_key_exists('value', $options)) { | |
291 | throw new base_setting_exception('dependency_needs_value'); | |
292 | } | |
293 | $dependency = new setting_dependency_disabledif_equals($this, $dependentsetting, $options['value'], $options['defaultvalue']); | |
294 | break; | |
295 | case setting_dependency::DISABLED_TRUE : | |
296 | case setting_dependency::DISABLED_CHECKED : | |
297 | $dependency = new setting_dependency_disabledif_equals($this, $dependentsetting, true, $options['defaultvalue']); | |
298 | break; | |
299 | case setting_dependency::DISABLED_FALSE : | |
300 | case setting_dependency::DISABLED_NOT_CHECKED : | |
301 | $dependency = new setting_dependency_disabledif_equals($this, $dependentsetting, false, $options['defaultvalue']); | |
302 | break; | |
303 | } | |
304 | $this->dependencies[$dependentsetting->get_name()] = $dependency; | |
72d5e574 | 305 | $dependency->get_dependant_setting()->register_dependent_dependency($dependency); |
69dd0c8c EL |
306 | } |
307 | ||
308 | // Protected API starts here | |
309 | ||
310 | protected function validate_value($vtype, $value) { | |
311 | if (is_null($value)) { // Nulls aren't validated | |
312 | return null; | |
313 | } | |
314 | $oldvalue = $value; | |
315 | switch ($vtype) { | |
316 | case self::IS_BOOLEAN: | |
317 | $value = clean_param($oldvalue, PARAM_BOOL); // Just clean | |
318 | break; | |
319 | case self::IS_INTEGER: | |
320 | $value = clean_param($oldvalue, PARAM_INT); | |
321 | if ($value != $oldvalue) { | |
322 | throw new base_setting_exception('setting_invalid_integer', $oldvalue); | |
323 | } | |
324 | break; | |
325 | case self::IS_FILENAME: | |
326 | $value = clean_param($oldvalue, PARAM_FILE); | |
327 | if ($value != $oldvalue) { | |
328 | throw new base_setting_exception('setting_invalid_filename', $oldvalue); | |
329 | } | |
330 | break; | |
331 | case self::IS_PATH: | |
332 | $value = clean_param($oldvalue, PARAM_PATH); | |
333 | if ($value != $oldvalue) { | |
334 | throw new base_setting_exception('setting_invalid_path', $oldvalue); | |
335 | } | |
336 | break; | |
337 | } | |
338 | return $value; | |
339 | } | |
340 | ||
341 | protected function validate_visibility($visibility) { | |
342 | if (is_null($visibility)) { | |
343 | $visibility = self::VISIBLE; | |
344 | } | |
345 | if ($visibility !== self::VISIBLE && $visibility !== self::HIDDEN) { | |
346 | throw new base_setting_exception('setting_invalid_visibility'); | |
347 | } | |
348 | return $visibility; | |
349 | } | |
350 | ||
351 | protected function validate_status($status) { | |
352 | if (is_null($status)) { | |
353 | $status = self::NOT_LOCKED; | |
354 | } | |
ce937f99 EL |
355 | if ($status !== self::NOT_LOCKED && $status !== self::LOCKED_BY_CONFIG && |
356 | $status !== self::LOCKED_BY_PERMISSION && $status !== self::LOCKED_BY_HIERARCHY) { | |
357 | throw new base_setting_exception('setting_invalid_status', $status); | |
69dd0c8c EL |
358 | } |
359 | return $status; | |
360 | } | |
361 | ||
362 | protected function validate_ui_type($type) { | |
363 | if ($type !== self::UI_HTML_CHECKBOX && $type !== self::UI_HTML_RADIOBUTTON && | |
364 | $type !== self::UI_HTML_DROPDOWN && $type !== self::UI_HTML_TEXTFIELD) { | |
365 | throw new base_setting_exception('setting_invalid_ui_type'); | |
366 | } | |
367 | return $type; | |
368 | } | |
369 | ||
370 | protected function validate_ui_label($label) { | |
371 | if (empty($label) || $label !== clean_param($label, PARAM_ALPHAEXT)) { | |
372 | throw new base_setting_exception('setting_invalid_ui_label'); | |
373 | } | |
374 | return $label; | |
375 | } | |
376 | ||
377 | protected function inform_dependencies($ctype, $oldv) { | |
378 | foreach ($this->dependencies as $dependency) { | |
1904e9b3 | 379 | $dependency->process_change($ctype, $oldv); |
69dd0c8c EL |
380 | } |
381 | } | |
382 | ||
383 | protected function is_circular_reference($obj) { | |
384 | // Get object dependencies recursively and check (by name) if $this is already there | |
385 | $dependencies = $obj->get_dependencies(); | |
386 | if (array_key_exists($this->name, $dependencies) || $obj == $this) { | |
387 | return true; | |
388 | } | |
389 | return false; | |
390 | } | |
391 | ||
392 | protected function get_dependencies() { | |
393 | $dependencies = array(); | |
394 | foreach ($this->dependencies as $dependency) { | |
1904e9b3 | 395 | $dependencies[$dependency->get_dependant_setting()->get_name()] = $dependency->get_dependant_setting(); |
69dd0c8c EL |
396 | $dependencies = array_merge($dependencies, $dependency->get_dependencies()); |
397 | } | |
398 | return $dependencies; | |
399 | } | |
400 | ||
1a83fcb5 SH |
401 | /** |
402 | * Sets a help string for this setting | |
403 | * | |
404 | * @param string $identifier | |
405 | * @param string $component | |
406 | */ | |
407 | public function set_help($identifier, $component='moodle') { | |
408 | $this->help = array($identifier, $component); | |
409 | } | |
410 | ||
411 | /** | |
412 | * Gets the help string params for this setting if it has been set | |
413 | * @return array|false An array (identifier, component) or false if not set | |
414 | */ | |
415 | public function get_help() { | |
416 | if ($this->has_help()) { | |
417 | return $this->help; | |
418 | } | |
419 | return false; | |
420 | } | |
421 | ||
422 | /** | |
423 | * Returns true if help has been set for this setting | |
424 | * @return cool | |
425 | */ | |
426 | public function has_help() { | |
427 | return (!empty($this->help)); | |
428 | } | |
69dd0c8c EL |
429 | } |
430 | ||
431 | /* | |
432 | * Exception class used by all the @setting_base stuff | |
433 | */ | |
434 | class base_setting_exception extends backup_exception { | |
435 | ||
436 | public function __construct($errorcode, $a=NULL, $debuginfo=null) { | |
437 | parent::__construct($errorcode, $a, $debuginfo); | |
438 | } | |
439 | } |