mirrored from git://git.moodle.org/moodle.git
/
backup_ui_setting.class.php
593 lines (573 loc) · 19.3 KB
/
backup_ui_setting.class.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<?php
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
/**
* This file contains the setting user interface classes that all backup/restore
* settings use to represent the UI they have.
*
* @package moodlecore
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Abstract class used to represent the user interface that a setting has.
*
* @todo extend as required for restore
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class base_setting_ui {
/**
* Prefix applied to all inputs/selects
*/
const NAME_PREFIX = 'setting_';
/**
* The name of the setting
* @var string
*/
protected $name;
/**
* The label for the setting
* @var string
*/
protected $label;
/**
* An array of HTML attributes to apply to this setting
* @var array
*/
protected $attributes = array();
/**
* The backup_setting UI type this relates to. One of backup_setting::UI_*;
* @var int
*/
protected $type;
/**
* An icon to display next to this setting in the UI
* @var pix_icon
*/
protected $icon = false;
/**
* The setting this UI belongs to (parent reference)
* @var base_setting|backup_setting
*/
protected $setting;
/**
* Constructors are sooooo cool
* @param base_setting $setting
*/
public function __construct(base_setting $setting) {
$this->setting = $setting;
}
/**
* Destroy all circular references. It helps PHP 5.2 a lot!
*/
public function destroy() {
// No need to destroy anything recursively here, direct reset
$this->setting = null;
}
/**
* Gets the name of this item including its prefix
* @return string
*/
public function get_name() {
return self::NAME_PREFIX.$this->name;
}
/**
* Gets the name of this item including its prefix
* @return string
*/
public function get_label() {
return $this->label;
}
/**
* Gets the type of this element
* @return int
*/
public function get_type() {
return $this->type;
}
/**
* Gets the HTML attributes for this item
* @return array
*/
public function get_attributes() {
return $this->attributes;
}
/**
* Gets the value of this setting
* @return mixed
*/
public function get_value() {
return $this->setting->get_value();
}
/**
* Gets the value to display in a static quickforms element
* @return mixed
*/
public function get_static_value() {
return $this->setting->get_value();
}
/**
* Sets the label
* @param string $label
*/
public function set_label($label) {
if ((string)$label === '' || $label !== clean_param($label, PARAM_TEXT)) {
throw new base_setting_ui_exception('setting_invalid_ui_label');
}
$this->label = $label;
}
/**
* Disables the UI for this element
*/
public function disable() {
$this->attributes['disabled'] = 'disabled';
}
/**
* Sets the icon to display next to this item
*
* @param pix_icon $icon
*/
public function set_icon(pix_icon $icon) {
$this->icon = $icon;
}
/**
* Returns the icon to display next to this item, or false if there isn't one.
*
* @return pix_icon|false
*/
public function get_icon() {
if (!empty($this->icon)) {
return $this->icon;
}
return false;
}
}
/**
* Abstract class to represent the user interface backup settings have
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class backup_setting_ui extends base_setting_ui {
/**
* An array of options relating to this setting
* @var array
*/
protected $options = array();
/**
* JAC... Just Another Constructor
*
* @param backup_setting $setting
* @param string|null $label The label to display with the setting ui
* @param array|null $attributes Array of HTML attributes to apply to the element
* @param array|null $options Array of options to apply to the setting ui object
*/
public function __construct(backup_setting $setting, $label = null, array $attributes = null, array $options = null) {
parent::__construct($setting);
// Improve the inputs name by appending the level to the name
switch ($setting->get_level()) {
case backup_setting::ROOT_LEVEL :
$this->name = 'root_'.$setting->get_name();
break;
case backup_setting::COURSE_LEVEL :
$this->name = 'course_'.$setting->get_name();
break;
case backup_setting::SECTION_LEVEL :
$this->name = 'section_'.$setting->get_name();
break;
case backup_setting::ACTIVITY_LEVEL :
$this->name = 'activity_'.$setting->get_name();
break;
}
$this->label = $label;
if (is_array($attributes)) {
$this->attributes = $attributes;
}
if (is_array($options)) {
$this->options = $options;
}
}
/**
* Creates a new backup setting ui based on the setting it is given
*
* Throws an exception if an invalid type is provided.
*
* @param backup_setting $setting
* @param int $type The backup_setting UI type. One of backup_setting::UI_*;
* @param string $label The label to display with the setting ui
* @param array $attributes Array of HTML attributes to apply to the element
* @param array $options Array of options to apply to the setting ui object
*
* @return backup_setting_ui_text|backup_setting_ui_checkbox|backup_setting_ui_select|backup_setting_ui_radio
*/
final public static function make(backup_setting $setting, $type, $label, array $attributes = null, array $options=null) {
// Base the decision we make on the type that was sent
switch ($type) {
case backup_setting::UI_HTML_CHECKBOX :
return new backup_setting_ui_checkbox($setting, $label, null, (array)$attributes, (array)$options);
case backup_setting::UI_HTML_DROPDOWN :
return new backup_setting_ui_select($setting, $label, null, (array)$attributes, (array)$options);
case backup_setting::UI_HTML_RADIOBUTTON :
return new backup_setting_ui_radio($setting, $label, null, null, (array)$attributes, (array)$options);
case backup_setting::UI_HTML_TEXTFIELD :
return new backup_setting_ui_text($setting, $label, $attributes, $options);
default:
throw new backup_setting_ui_exception('setting_invalid_ui_type');
}
}
/**
* Get element properties that can be used to make a quickform element
* @return array
*/
abstract public function get_element_properties(base_task $task=null, renderer_base $output=null);
/**
* Applies config options to a given properties array and then returns it
* @param array $properties
* @return array
*/
public function apply_options(array $properties) {
if (!empty($this->options['size'])) {
$properties['attributes']['size'] = $this->options['size'];
}
return $properties;
}
/**
* Gets the label for this item
* @param backup_task|null $task Optional, if provided and the setting is an include
* $task is used to set the setting label
* @return string
*/
public function get_label(base_task $task=null) {
// If a task has been provided and the label is not already set meaniningfully
// we will attempt to improve it.
if (!is_null($task) && $this->label == $this->setting->get_name() && strpos($this->setting->get_name(), '_include')!==false) {
if ($this->setting->get_level() == backup_setting::SECTION_LEVEL) {
$this->label = get_string('includesection', 'backup', $task->get_name());
} else if ($this->setting->get_level() == backup_setting::ACTIVITY_LEVEL) {
$this->label = $task->get_name();
}
}
return $this->label;
}
/**
* Returns true if the setting is changeable.
*
* A setting is changeable if it meets either of the two following conditions.
*
* 1. The setting is not locked
* 2. The setting is locked but only by settings that are of the same level (same page)
*
* Condition 2 is really why we have this function
*
* @return bool
*/
public function is_changeable() {
if ($this->setting->get_status() === backup_setting::NOT_LOCKED) {
// Its not locked so its chanegable
return true;
} else if ($this->setting->get_status() !== backup_setting::LOCKED_BY_HIERARCHY) {
// Its not changeable because its locked by permission or config
return false;
} else if ($this->setting->has_dependencies_on_settings()) {
foreach ($this->setting->get_settings_depended_on() as $dependency) {
if ($dependency->is_locked() && $dependency->get_setting()->get_level() !== $this->setting->get_level()) {
// Its not changeable because one or more dependancies arn't
// changeable.
return false;
}
}
// Its changeable because all dependencies are changeable.
return true;
}
// We should never get here but if we do return false to be safe.
// The setting would need to be locked by hierarchy and not have any deps
return false;
}
}
/**
* A text input user interface element for backup settings
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_setting_ui_text extends backup_setting_ui {
/**
* @var int
*/
protected $type = backup_setting::UI_HTML_TEXTFIELD;
/**
* Returns an array of properties suitable for generating a quickforms element
* @param backup_task|null $task
* @return array (element, name, label, attributes)
*/
public function get_element_properties(base_task $task=null, renderer_base $output=null) {
// name, label, text, attributes
$icon = $this->get_icon();
$label = $this->get_label($task);
if (!empty($icon)) {
$label .= $output->render($icon);
}
// name, label, attributes
return $this->apply_options(array('element'=>'text','name'=>self::NAME_PREFIX.$this->name, 'label'=>$label, 'attributes'=>$this->attributes));
}
}
/**
* A checkbox user interface element for backup settings (default)
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_setting_ui_checkbox extends backup_setting_ui {
/**
* @var int
*/
protected $type = backup_setting::UI_HTML_CHECKBOX;
/**
* @var bool
*/
protected $changeable = true;
/**
* The text to show next to the checkbox
* @var string
*/
protected $text;
/**
* Overridden constructor so we can take text argument
* @param backup_setting $setting
* @param string $label
* @param string $text
* @param array $attributes
* @param array $options
*/
public function __construct(backup_setting $setting, $label = null, $text=null, array $attributes = array(), array $options = array()) {
parent::__construct($setting, $label, $attributes, $options);
$this->text = $text;
}
/**
* Returns an array of properties suitable for generating a quickforms element
* @param backup_task|null $task
* @return array (element, name, label, text, attributes);
*/
public function get_element_properties(base_task $task=null, renderer_base $output=null) {
// name, label, text, attributes
$icon = $this->get_icon();
$label = $this->get_label($task);
if (!empty($icon)) {
$label .= $output->render($icon);
}
return $this->apply_options(array('element'=>'checkbox','name'=>self::NAME_PREFIX.$this->name, 'label'=>$label, 'text'=>$this->text, 'attributes'=>$this->attributes));
}
/**
* Sets the text for the element
* @param string $text
*/
public function set_text($text) {
$this->text = $text;
}
/**
* Gets the static value for the element
* @global core_renderer $OUTPUT
* @return string
*/
public function get_static_value() {
global $OUTPUT;
// Checkboxes are always yes or no
if ($this->get_value()) {
return $OUTPUT->pix_icon('i/valid', get_string('yes'));
} else {
return $OUTPUT->pix_icon('i/invalid', get_string('no'));
}
}
/**
* Returns true if the setting is changeable
* @return bool
*/
public function is_changeable() {
if ($this->changeable===false) {
return false;
} else {
return parent::is_changeable();
}
}
/**
* Sets whether the setting is changeable,
* Note dependencies can still mark this setting changeable or not
* @param bool $newvalue
*/
public function set_changeable($newvalue) {
$this->changeable = ($newvalue);
}
}
/**
* Radio button user interface element for backup settings
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_setting_ui_radio extends backup_setting_ui {
/**
* @var int
*/
protected $type = backup_setting::UI_HTML_RADIOBUTTON;
/**
* The string shown next to the input
* @var string
*/
protected $text;
/**
* The value for the radio input
* @var string
*/
protected $value;
/**
*
* @param backup_setting $setting
* @param string $label
* @param string $text
* @param string $value
* @param array $attributes
* @param array $options
*/
public function __construct(backup_setting $setting, $label = null, $text=null, $value=null, array $attributes = array(), array $options = array()) {
parent::__construct($setting, $label, $attributes, $options);
$this->text = $text;
$this->value = (string)$value;
}
/**
* Returns an array of properties suitable for generating a quickforms element
* @param backup_task|null $task
* @return array (element, name, label, text, value, attributes)
*/
public function get_element_properties(base_task $task=null, renderer_base $output=null) {
// name, label, text, attributes
$icon = $this->get_icon();
$label = $this->get_label($task);
if (!empty($icon)) {
$label .= $output->render($icon);
}
// name, label, text, value, attributes
return $this->apply_options(array('element'=>'radio','name'=>self::NAME_PREFIX.$this->name, 'label'=>$label, 'text'=>$this->text, 'value'=>$this->value, 'attributes'=>$this->attributes));
}
/**
* Sets the text next to this input
* @param text $text
*/
public function set_text($text) {
$this->text = $text;
}
/**
* Sets the value for the input
* @param string $value
*/
public function set_value($value) {
$this->value = (string)$value;
}
/**
* Gets the static value to show for the element
*/
public function get_static_value() {
return $this->value;
}
}
/**
* A select box, drop down user interface for backup settings
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_setting_ui_select extends backup_setting_ui {
/**
* @var int
*/
protected $type = backup_setting::UI_HTML_DROPDOWN;
/**
* An array of options to display in the select
* @var array
*/
protected $values;
/**
*
* @param backup_setting $setting
* @param string $label
* @param array $values
* @param array $attributes
* @param array $options
*/
public function __construct(backup_setting $setting, $label = null, $values=null, array $attributes = array(), array $options = array()) {
parent::__construct($setting, $label, $attributes, $options);
$this->values = $values;
}
/**
* Returns an array of properties suitable for generating a quickforms element
* @param backup_task|null $task
* @return array (element, name, label, options, attributes)
*/
public function get_element_properties(base_task $task = null, renderer_base $output=null) {
// name, label, text, attributes
$icon = $this->get_icon();
$label = $this->get_label($task);
if (!empty($icon)) {
$label .= $output->render($icon);
}
// name, label, options, attributes
return $this->apply_options(array('element'=>'select','name'=>self::NAME_PREFIX.$this->name, 'label'=>$label, 'options'=>$this->values, 'attributes'=>$this->attributes));
}
/**
* Sets the options for the select box
* @param array $values Associative array of value=>text options
*/
public function set_values(array $values) {
$this->values = $values;
}
/**
* Gets the static value for this select element
* @return string
*/
public function get_static_value() {
return $this->values[$this->get_value()];
}
/**
* Returns true if the setting is changeable, false otherwise
*
* @return bool
*/
public function is_changeable() {
if (count($this->values) == 1) {
return false;
} else {
return parent::is_changeable();
}
}
}
class backup_setting_ui_dateselector extends backup_setting_ui_text {
public function get_element_properties(base_task $task = null, renderer_base $output=null) {
if (!array_key_exists('optional', $this->attributes)) {
$this->attributes['optional'] = false;
}
$properties = parent::get_element_properties($task, $output);
$properties['element'] = 'date_selector';
return $properties;
}
public function get_static_value() {
$value = $this->get_value();
if (!empty($value)) {
return userdate($value);
}
return parent::get_static_value();
}
}
class base_setting_ui_exception extends base_setting_exception {}
class backup_setting_ui_exception extends base_setting_ui_exception {};