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