hub MDL-19313 remove not used define values
[moodle.git] / backup / util / settings / base_setting.class.php
CommitLineData
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 */
35abstract 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
84e546e0
SH
191 /**
192 * Returns an array of all dependencies this setting has as well as the dependencies
193 * of its dependencies.... another words recursivily
194 * @return array
195 */
196 public function get_all_dependencies() {
197 $dependencies = array_values($this->dependencies);
198 foreach ($this->dependencies as &$dependency) {
e35fb479 199 $childdependencies = $dependency->get_dependent_setting()->get_all_dependencies();
84e546e0
SH
200 foreach ($childdependencies as $name=>&$childdependency) {
201 $dependencies[] = $childdependency;
202 }
203 }
204 return $dependencies;
205 }
206
6ed9a7da
SH
207 /**
208 * Gets an array of properties for all of the dependencies that will affect
209 * this setting.
210 *
211 * This method returns and array rather than the dependencies in order to
212 * minimise the memory footprint of for the potentially huge recursive
213 * dependency structure that we may be dealing with.
214 *
215 * This method also ensures that all dependencies are transmuted to affect
216 * the setting in question and that we don't provide any duplicates.
217 *
218 * @param string|null $settingname
219 * @return array
220 */
221 public function get_my_dependency_properties($settingname=null) {
222 if ($settingname == null) {
223 $settingname = $this->get_ui_name();
224 }
225 $dependencies = array();
226 foreach ($this->dependenton as $dependenton) {
227 $properties = $dependenton->get_moodleform_properties();
228 $properties['setting'] = $settingname;
229 $dependencies[$properties['setting'].'-'.$properties['dependenton']] = $properties;
230 $dependencies = array_merge($dependencies, $dependenton->get_setting()->get_my_dependency_properties($settingname));
231 }
232 return $dependencies;
233 }
234
1904e9b3
SH
235 public function set_ui(backup_setting_ui $ui) {
236 $this->uisetting = $ui;
69dd0c8c
EL
237 }
238
1904e9b3
SH
239 public function make_ui($type, $label, array $attributes = null, array $options = null) {
240 $type = $this->validate_ui_type($type);
241 $label = $this->validate_ui_label($label);
242 $this->uisetting = backup_setting_ui::make($this, $type, $label, $attributes, $options);
243 if (is_array($options) || is_object($options)) {
244 $options = (array)$options;
245 switch (get_class($this->uisetting)) {
246 case 'backup_setting_ui_radio' :
247 // text
248 if (array_key_exists('text', $options)) {
249 $this->uisetting->set_text($options['text']);
250 }
251 case 'backup_setting_ui_checkbox' :
252 // value
253 if (array_key_exists('value', $options)) {
254 $this->uisetting->set_value($options['value']);
255 }
256 break;
257 case 'backup_setting_ui_select' :
258 // options
259 if (array_key_exists('options', $options)) {
260 $this->uisetting->set_values($options['options']);
261 }
262 break;
263 }
264 }
69dd0c8c
EL
265 }
266
1904e9b3
SH
267 public function get_ui() {
268 return $this->uisetting;
69dd0c8c
EL
269 }
270
72d5e574
SH
271 /**
272 * Adds a dependency where another setting depends on this setting.
273 * @param setting_dependency $dependency
274 */
275 public function register_dependency(setting_dependency $dependency) {
e35fb479 276 if ($this->is_circular_reference($dependency->get_dependent_setting())) {
72d5e574
SH
277 $a = new stdclass();
278 $a->alreadydependent = $this->name;
279 $a->main = $dependentsetting->get_name();
280 throw new base_setting_exception('setting_circular_reference', $a);
281 }
e35fb479
SH
282 $this->dependencies[$dependency->get_dependent_setting()->get_name()] = $dependency;
283 $dependency->get_dependent_setting()->register_dependent_dependency($dependency);
72d5e574
SH
284 }
285 /**
286 * Adds a dependency where this setting is dependent on another.
287 *
288 * This should only be called internally once we are sure it is not cicrular.
289 *
290 * @param setting_dependency $dependency
291 */
292 protected function register_dependent_dependency(setting_dependency $dependency) {
293 $this->dependenton[$dependency->get_setting()->get_name()] = $dependency;
294 }
295
1904e9b3
SH
296 public function add_dependency(base_setting $dependentsetting, $type=null, $options=array()) {
297 if ($this->is_circular_reference($dependentsetting)) {
69dd0c8c
EL
298 $a = new stdclass();
299 $a->alreadydependent = $this->name;
1904e9b3 300 $a->main = $dependentsetting->get_name();
69dd0c8c
EL
301 throw new base_setting_exception('setting_circular_reference', $a);
302 }
303 // Check the settings hasn't been already added
1904e9b3 304 if (array_key_exists($dependentsetting->get_name(), $this->dependencies)) {
69dd0c8c
EL
305 throw new base_setting_exception('setting_already_added');
306 }
1904e9b3
SH
307
308 $options = (array)$options;
309
310 if (!array_key_exists('defaultvalue', $options)) {
311 $options['defaultvalue'] = false;
312 }
313
314 if ($type == null) {
315 switch ($this->vtype) {
316 case self::IS_BOOLEAN :
84e546e0
SH
317 if ($this->get_ui_type() == self::UI_HTML_CHECKBOX) {
318 if ($this->value) {
319 $type = setting_dependency::DISABLED_NOT_CHECKED;
320 } else {
321 $type = setting_dependency::DISABLED_CHECKED;
322 }
1904e9b3 323 } else {
84e546e0
SH
324 if ($this->value) {
325 $type = setting_dependency::DISABLED_FALSE;
326 } else {
327 $type = setting_dependency::DISABLED_TRUE;
328 }
1904e9b3
SH
329 }
330 break;
331 case self::IS_FILENAME :
332 case self::IS_PATH :
333 case self::IS_INTEGER :
334 default :
335 $type = setting_dependency::DISABLED_VALUE;
336 break;
337 }
338 }
339
340 switch ($type) {
341 case setting_dependency::DISABLED_VALUE :
342 if (!array_key_exists('value', $options)) {
343 throw new base_setting_exception('dependency_needs_value');
344 }
345 $dependency = new setting_dependency_disabledif_equals($this, $dependentsetting, $options['value'], $options['defaultvalue']);
346 break;
347 case setting_dependency::DISABLED_TRUE :
1904e9b3
SH
348 $dependency = new setting_dependency_disabledif_equals($this, $dependentsetting, true, $options['defaultvalue']);
349 break;
350 case setting_dependency::DISABLED_FALSE :
1904e9b3
SH
351 $dependency = new setting_dependency_disabledif_equals($this, $dependentsetting, false, $options['defaultvalue']);
352 break;
84e546e0
SH
353 case setting_dependency::DISABLED_CHECKED :
354 $dependency = new setting_dependency_disabledif_checked($this, $dependentsetting, $options['defaultvalue']);
355 break;
356 case setting_dependency::DISABLED_NOT_CHECKED :
357 $dependency = new setting_dependency_disabledif_not_checked($this, $dependentsetting, $options['defaultvalue']);
358 break;
1904e9b3
SH
359 }
360 $this->dependencies[$dependentsetting->get_name()] = $dependency;
e35fb479 361 $dependency->get_dependent_setting()->register_dependent_dependency($dependency);
69dd0c8c
EL
362 }
363
364// Protected API starts here
365
366 protected function validate_value($vtype, $value) {
367 if (is_null($value)) { // Nulls aren't validated
368 return null;
369 }
370 $oldvalue = $value;
371 switch ($vtype) {
372 case self::IS_BOOLEAN:
373 $value = clean_param($oldvalue, PARAM_BOOL); // Just clean
374 break;
375 case self::IS_INTEGER:
376 $value = clean_param($oldvalue, PARAM_INT);
377 if ($value != $oldvalue) {
378 throw new base_setting_exception('setting_invalid_integer', $oldvalue);
379 }
380 break;
381 case self::IS_FILENAME:
382 $value = clean_param($oldvalue, PARAM_FILE);
383 if ($value != $oldvalue) {
384 throw new base_setting_exception('setting_invalid_filename', $oldvalue);
385 }
386 break;
387 case self::IS_PATH:
388 $value = clean_param($oldvalue, PARAM_PATH);
389 if ($value != $oldvalue) {
390 throw new base_setting_exception('setting_invalid_path', $oldvalue);
391 }
392 break;
393 }
394 return $value;
395 }
396
397 protected function validate_visibility($visibility) {
398 if (is_null($visibility)) {
399 $visibility = self::VISIBLE;
400 }
401 if ($visibility !== self::VISIBLE && $visibility !== self::HIDDEN) {
402 throw new base_setting_exception('setting_invalid_visibility');
403 }
404 return $visibility;
405 }
406
407 protected function validate_status($status) {
408 if (is_null($status)) {
409 $status = self::NOT_LOCKED;
410 }
ce937f99
EL
411 if ($status !== self::NOT_LOCKED && $status !== self::LOCKED_BY_CONFIG &&
412 $status !== self::LOCKED_BY_PERMISSION && $status !== self::LOCKED_BY_HIERARCHY) {
413 throw new base_setting_exception('setting_invalid_status', $status);
69dd0c8c
EL
414 }
415 return $status;
416 }
417
418 protected function validate_ui_type($type) {
419 if ($type !== self::UI_HTML_CHECKBOX && $type !== self::UI_HTML_RADIOBUTTON &&
420 $type !== self::UI_HTML_DROPDOWN && $type !== self::UI_HTML_TEXTFIELD) {
421 throw new base_setting_exception('setting_invalid_ui_type');
422 }
423 return $type;
424 }
425
426 protected function validate_ui_label($label) {
427 if (empty($label) || $label !== clean_param($label, PARAM_ALPHAEXT)) {
428 throw new base_setting_exception('setting_invalid_ui_label');
429 }
430 return $label;
431 }
432
433 protected function inform_dependencies($ctype, $oldv) {
434 foreach ($this->dependencies as $dependency) {
1904e9b3 435 $dependency->process_change($ctype, $oldv);
69dd0c8c
EL
436 }
437 }
438
439 protected function is_circular_reference($obj) {
440 // Get object dependencies recursively and check (by name) if $this is already there
441 $dependencies = $obj->get_dependencies();
442 if (array_key_exists($this->name, $dependencies) || $obj == $this) {
443 return true;
444 }
445 return false;
446 }
447
84e546e0
SH
448 public function get_dependencies() {
449 return $this->dependencies;
450 }
451
452 public function get_ui_name() {
453 return $this->uisetting->get_name();
454 }
455
456 public function get_ui_type() {
457 return $this->uisetting->get_type();
69dd0c8c
EL
458 }
459
1a83fcb5
SH
460 /**
461 * Sets a help string for this setting
462 *
463 * @param string $identifier
464 * @param string $component
465 */
466 public function set_help($identifier, $component='moodle') {
467 $this->help = array($identifier, $component);
468 }
469
470 /**
471 * Gets the help string params for this setting if it has been set
472 * @return array|false An array (identifier, component) or false if not set
473 */
474 public function get_help() {
475 if ($this->has_help()) {
476 return $this->help;
477 }
478 return false;
479 }
480
481 /**
482 * Returns true if help has been set for this setting
483 * @return cool
484 */
485 public function has_help() {
486 return (!empty($this->help));
487 }
69dd0c8c
EL
488}
489
490/*
491 * Exception class used by all the @setting_base stuff
492 */
493class base_setting_exception extends backup_exception {
494
495 public function __construct($errorcode, $a=NULL, $debuginfo=null) {
496 parent::__construct($errorcode, $a, $debuginfo);
497 }
498}