Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * This file contains the setting user interface classes that all backup/restore
19
 * settings use to represent the UI they have.
20
 *
21
 * @package   core_backup
22
 * @copyright 2010 Sam Hemelryk
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
/**
27
 * Abstract class used to represent the user interface that a setting has.
28
 *
29
 * @todo extend as required for restore
30
 * @package core_backup
31
 * @copyright 2010 Sam Hemelryk
32
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class base_setting_ui {
35
    /**
36
     * Prefix applied to all inputs/selects
37
     */
38
    const NAME_PREFIX = 'setting_';
39
    /**
40
     * The name of the setting
41
     * @var string
42
     */
43
    protected $name;
44
    /**
45
     * The label for the setting
46
     * @var string
47
     */
48
    protected $label;
49
    /**
1441 ariadna 50
     * The optional accessible label for the setting.
51
     * @var string
52
     */
53
    protected string $altlabel = '';
54
    /**
1 efrain 55
     * An array of HTML attributes to apply to this setting
56
     * @var array
57
     */
58
    protected $attributes = array();
59
    /**
60
     * The backup_setting UI type this relates to. One of backup_setting::UI_*;
61
     * @var int
62
     */
63
    protected $type;
64
    /**
65
     * An icon to display next to this setting in the UI
66
     * @var pix_icon
67
     */
68
    protected $icon = false;
69
    /**
70
     * The setting this UI belongs to (parent reference)
71
     * @var base_setting|backup_setting
72
     */
73
    protected $setting;
74
 
75
    /**
76
     * Constructors are sooooo cool
77
     * @param base_setting $setting
78
     */
79
    public function __construct(base_setting $setting) {
80
        $this->setting = $setting;
81
    }
82
 
83
    /**
84
     * Destroy all circular references. It helps PHP 5.2 a lot!
85
     */
86
    public function destroy() {
87
        // No need to destroy anything recursively here, direct reset.
88
        $this->setting = null;
89
    }
90
 
91
    /**
92
     * Gets the name of this item including its prefix
93
     * @return string
94
     */
95
    public function get_name() {
96
        return self::NAME_PREFIX.$this->name;
97
    }
98
 
99
    /**
100
     * Gets the name of this item including its prefix
101
     * @return string
102
     */
103
    public function get_label() {
104
        return $this->label;
105
    }
106
 
107
    /**
1441 ariadna 108
     * Get the visually hidden label for the UI setting.
109
     *
110
     * @return string
111
     */
112
    public function get_visually_hidden_label(): ?string {
113
        global $PAGE;
114
        if ($this->altlabel === '') {
115
            return null;
116
        }
117
        $renderer = $PAGE->get_renderer('core_backup');
118
        return $renderer->visually_hidden_text($this->altlabel);
119
    }
120
 
121
    /**
1 efrain 122
     * Gets the type of this element
123
     * @return int
124
     */
125
    public function get_type() {
126
        return $this->type;
127
    }
128
 
129
    /**
130
     * Gets the HTML attributes for this item
131
     * @return array
132
     */
133
    public function get_attributes() {
134
        return $this->attributes;
135
    }
136
 
137
    /**
138
     * Gets the value of this setting
139
     * @return mixed
140
     */
141
    public function get_value() {
142
        return $this->setting->get_value();
143
    }
144
 
145
    /**
146
     * Gets the value to display in a static quickforms element
147
     * @return mixed
148
     */
149
    public function get_static_value() {
150
        return $this->setting->get_value();
151
    }
152
 
153
    /**
154
     * Gets the the PARAM_XXXX validation to be applied to the setting
155
     *
156
     * return string The PARAM_XXXX constant of null if the setting type is not defined
157
     */
158
    public function get_param_validation() {
159
        return $this->setting->get_param_validation();
160
    }
161
 
162
    /**
163
     * Sets the label.
164
     *
165
     * @throws base_setting_ui_exception when the label is not valid.
166
     * @param string $label
167
     */
168
    public function set_label(string $label): void {
169
        // Let's avoid empty/whitespace-only labels, so the html clean (that makes trim()) doesn't fail.
170
        if (trim($label) === '') {
171
            $label = '&nbsp;'; // Will be converted to non-breaking utf-8 char 0xc2a0 by PARAM_CLEANHTML.
172
        }
173
 
174
        $label = clean_param($label, PARAM_CLEANHTML);
175
 
176
        if ($label === '') {
177
            throw new base_setting_ui_exception('setting_invalid_ui_label');
178
        }
179
 
180
        $this->label = $label;
181
    }
182
 
183
    /**
1441 ariadna 184
     * Adds a visually hidden label to the UI setting.
185
     *
186
     * Some backup fields labels have unaccessible labels for screen readers. For example,
187
     * all schema activity user data uses '-' as label. This method adds extra information
188
     * for screen readers.
189
     *
190
     * @param string $label The accessible label to be added.
191
     * @return void
192
     */
193
    public function set_visually_hidden_label(string $label): void {
194
        $this->altlabel = clean_param($label, PARAM_CLEANHTML);
195
    }
196
 
197
    /**
1 efrain 198
     * Disables the UI for this element
199
     */
200
    public function disable() {
201
        $this->attributes['disabled'] = 'disabled';
202
    }
203
 
204
    /**
205
     * Sets the icon to display next to this item
206
     *
207
     * @param pix_icon $icon
208
     */
209
    public function set_icon(pix_icon $icon) {
210
        $this->icon = $icon;
211
    }
212
 
213
    /**
214
     * Returns the icon to display next to this item, or false if there isn't one.
215
     *
216
     * @return pix_icon|false
217
     */
218
    public function get_icon() {
219
        if (!empty($this->icon)) {
220
            return $this->icon;
221
        }
222
        return false;
223
    }
224
}
225
 
226
/**
227
 * Abstract class to represent the user interface backup settings have
228
 *
229
 * @package core_backup
230
 * @copyright 2010 Sam Hemelryk
231
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
232
 */
233
abstract class backup_setting_ui extends base_setting_ui {
234
    /**
235
     * An array of options relating to this setting
236
     * @var array
237
     */
238
    protected $options = array();
239
 
240
    /**
241
     * JAC... Just Another Constructor
242
     *
243
     * @param backup_setting $setting
244
     * @param string $label The label to display with the setting ui
245
     * @param array $attributes Array of HTML attributes to apply to the element
246
     * @param array $options Array of options to apply to the setting ui object
247
     */
1441 ariadna 248
    public function __construct(backup_setting $setting, $label = null, ?array $attributes = null, ?array $options = null) {
1 efrain 249
        parent::__construct($setting);
250
        // Improve the inputs name by appending the level to the name.
251
        switch ($setting->get_level()) {
252
            case backup_setting::ROOT_LEVEL :
253
                $this->name = 'root_'.$setting->get_name();
254
                break;
255
            case backup_setting::COURSE_LEVEL :
256
                $this->name = 'course_'.$setting->get_name();
257
                break;
258
            case backup_setting::SECTION_LEVEL :
1441 ariadna 259
            case backup_setting::SUBSECTION_LEVEL:
1 efrain 260
                $this->name = 'section_'.$setting->get_name();
261
                break;
262
            case backup_setting::ACTIVITY_LEVEL :
1441 ariadna 263
            case backup_setting::SUBACTIVITY_LEVEL:
1 efrain 264
                $this->name = 'activity_'.$setting->get_name();
265
                break;
266
        }
267
        $this->label = $label;
268
        if (is_array($attributes)) {
269
            $this->attributes = $attributes;
270
        }
271
        if (is_array($options)) {
272
            $this->options = $options;
273
        }
274
    }
275
 
276
    /**
277
     * Creates a new backup setting ui based on the setting it is given
278
     *
279
     * @throws backup_setting_ui_exception if the setting type is not supported,
280
     * @param backup_setting $setting
281
     * @param int $type The backup_setting UI type. One of backup_setting::UI_*;
282
     * @param string $label The label to display with the setting ui
283
     * @param array $attributes Array of HTML attributes to apply to the element
284
     * @param array $options Array of options to apply to the setting ui object
285
     * @return backup_setting_ui_text|backup_setting_ui_checkbox|backup_setting_ui_select|backup_setting_ui_radio
286
     */
1441 ariadna 287
    final public static function make(backup_setting $setting, $type, $label, ?array $attributes = null, ?array $options = null) {
1 efrain 288
        // Base the decision we make on the type that was sent.
289
        switch ($type) {
290
            case backup_setting::UI_HTML_CHECKBOX :
291
                return new backup_setting_ui_checkbox($setting, $label, null, (array)$attributes, (array)$options);
292
            case backup_setting::UI_HTML_DROPDOWN :
293
                return new backup_setting_ui_select($setting, $label, null, (array)$attributes, (array)$options);
294
            case backup_setting::UI_HTML_RADIOBUTTON :
295
                return new backup_setting_ui_radio($setting, $label, null, null, (array)$attributes, (array)$options);
296
            case backup_setting::UI_HTML_TEXTFIELD :
297
                return new backup_setting_ui_text($setting, $label, $attributes, $options);
298
            default:
299
                throw new backup_setting_ui_exception('setting_invalid_ui_type');
300
        }
301
    }
302
 
303
    /**
304
     * Get element properties that can be used to make a quickform element
305
     *
306
     * @param base_task $task
307
     * @param renderer_base $output
308
     * @return array
309
     */
1441 ariadna 310
    abstract public function get_element_properties(?base_task $task = null, ?renderer_base $output = null);
1 efrain 311
 
312
    /**
313
     * Applies config options to a given properties array and then returns it
314
     * @param array $properties
315
     * @return array
316
     */
317
    public function apply_options(array $properties) {
318
        if (!empty($this->options['size'])) {
319
            $properties['attributes']['size'] = $this->options['size'];
320
        }
321
        return $properties;
322
    }
323
 
324
    /**
325
     * Gets the label for this item
326
     * @param base_task $task Optional, if provided and the setting is an include
327
     *          $task is used to set the setting label
328
     * @return string
329
     */
1441 ariadna 330
    public function get_label(?base_task $task = null) {
1 efrain 331
        // If a task has been provided and the label is not already set meaningfully
332
        // we will attempt to improve it.
333
        if (!is_null($task) && $this->label == $this->setting->get_name() && strpos($this->setting->get_name(), '_include') !== false) {
1441 ariadna 334
            $level = $this->setting->get_level();
335
            if ($level == backup_setting::SECTION_LEVEL || $level == backup_setting::SUBSECTION_LEVEL) {
1 efrain 336
                $this->label = get_string('includesection', 'backup', $task->get_name());
1441 ariadna 337
            } else if ($level == backup_setting::ACTIVITY_LEVEL || $level == backup_setting::SUBACTIVITY_LEVEL) {
1 efrain 338
                $this->label = $task->get_name();
339
            }
340
        }
341
        return $this->label;
342
    }
343
 
344
    /**
345
     * Returns true if the setting is changeable.
346
     *
347
     * A setting is changeable if it meets either of the two following conditions.
348
     *
349
     * 1. The setting is not locked
350
     * 2. The setting is locked but only by settings that are of the same level (same page)
351
     *
352
     * Condition 2 is really why we have this function
353
     * @param int $level Optional, if provided only depedency_settings below or equal to this level are considered,
354
     *          when checking if the ui_setting is changeable. Although dependencies might cause a lock on this setting,
355
     *          they could be changeable in the same view.
356
     * @return bool
357
     */
358
    public function is_changeable($level = null) {
359
        if ($this->setting->get_status() === backup_setting::NOT_LOCKED) {
360
            // Its not locked so its chanegable.
361
            return true;
362
        } else if ($this->setting->get_status() !== backup_setting::LOCKED_BY_HIERARCHY) {
363
            // Its not changeable because its locked by permission or config.
364
            return false;
365
        } else if ($this->setting->has_dependencies_on_settings()) {
366
            foreach ($this->setting->get_settings_depended_on() as $dependency) {
367
                if ($level && $dependency->get_setting()->get_level() >= $level) {
368
                    continue;
369
                }
370
                if ($dependency->is_locked() && $dependency->get_setting()->get_level() !== $this->setting->get_level()) {
371
                    // Its not changeable because one or more dependancies arn't changeable.
372
                    return false;
373
                }
374
            }
375
            // Its changeable because all dependencies are changeable.
376
            return true;
377
        }
378
        // We should never get here but if we do return false to be safe.
379
        // The setting would need to be locked by hierarchy and not have any deps.
380
        return false;
381
    }
382
 
383
}
384
 
385
/**
386
 * A text input user interface element for backup settings
387
 *
388
 * @package core_backup
389
 * @copyright 2010 Sam Hemelryk
390
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
391
 */
392
class backup_setting_ui_text extends backup_setting_ui {
393
    /**
394
     * @var int
395
     */
396
    protected $type = backup_setting::UI_HTML_TEXTFIELD;
397
 
398
    /**
399
     * Returns an array of properties suitable for generating a quickforms element
400
     * @param base_task $task
401
     * @param renderer_base $output
402
     * @return array (element, name, label, attributes)
403
     */
1441 ariadna 404
    public function get_element_properties(?base_task $task = null, ?renderer_base $output = null) {
1 efrain 405
        $icon = $this->get_icon();
406
        $context = context_course::instance($task->get_courseid());
407
        $label = format_string($this->get_label($task), true, array('context' => $context));
408
        if (!empty($icon)) {
409
            $label .= $output->render($icon);
410
        }
411
        // Name, label, attributes.
412
        return $this->apply_options(array(
413
            'element' => 'text',
414
            'name' => self::NAME_PREFIX.$this->name,
415
            'label' => $label,
416
            'attributes' => $this->attributes)
417
        );
418
    }
419
 
420
}
421
 
422
/**
423
 * A checkbox user interface element for backup settings (default)
424
 *
425
 * @package core_backup
426
 * @copyright 2010 Sam Hemelryk
427
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
428
 */
429
class backup_setting_ui_checkbox extends backup_setting_ui {
430
 
431
    /**
432
     * @var int
433
     */
434
    protected $type = backup_setting::UI_HTML_CHECKBOX;
435
 
436
    /**
437
     * @var bool
438
     */
439
    protected $changeable = true;
440
 
441
    /**
442
     * The text to show next to the checkbox
443
     * @var string
444
     */
445
    protected $text;
446
 
447
    /**
448
     * Overridden constructor so we can take text argument
449
     *
450
     * @param backup_setting $setting
451
     * @param string $label
452
     * @param string $text
453
     * @param array $attributes
454
     * @param array $options
455
     */
456
    public function __construct(backup_setting $setting, $label = null, $text = null, array $attributes = array(), array $options = array()) {
457
        parent::__construct($setting, $label, $attributes, $options);
458
        $this->text = $text;
459
    }
460
 
461
    /**
462
     * Returns an array of properties suitable for generating a quickforms element
463
     * @param base_task $task
464
     * @param renderer_base $output
465
     * @return array (element, name, label, text, attributes);
466
     */
1441 ariadna 467
    public function get_element_properties(?base_task $task = null, ?renderer_base $output = null) {
1 efrain 468
        // Name, label, text, attributes.
469
        $icon = $this->get_icon();
470
        $context = context_course::instance($task->get_courseid());
471
        $label = format_string($this->get_label($task), true, array('context' => $context));
472
        if (!empty($icon)) {
473
            $label .= $output->render($icon);
474
        }
1441 ariadna 475
        $altlabel = $this->get_visually_hidden_label();
476
        if (!empty($altlabel)) {
477
            $label = $altlabel . $label;
478
        }
1 efrain 479
        return $this->apply_options(array(
480
            'element' => 'checkbox',
481
            'name' => self::NAME_PREFIX.$this->name,
482
            'label' => $label,
483
            'text' => $this->text,
484
            'attributes' => $this->attributes
485
        ));
486
    }
487
 
488
    /**
489
     * Sets the text for the element
490
     * @param string $text
491
     */
492
    public function set_text($text) {
493
        $this->text = $text;
494
    }
495
 
496
    /**
497
     * Gets the static value for the element
498
     * @global core_renderer $OUTPUT
499
     * @return string
500
     */
501
    public function get_static_value() {
502
        global $OUTPUT;
503
        // Checkboxes are always yes or no.
504
        if ($this->get_value()) {
505
            return $OUTPUT->pix_icon('i/valid', get_string('yes'));
506
        } else {
507
            return $OUTPUT->pix_icon('i/invalid', get_string('no'));
508
        }
509
    }
510
 
511
    /**
512
     * Returns true if the setting is changeable
513
     * @param int $level Optional, if provided only depedency_settings below or equal to this level are considered,
514
     *          when checking if the ui_setting is changeable. Although dependencies might cause a lock on this setting,
515
     *          they could be changeable in the same view.
516
     * @return bool
517
     */
518
    public function is_changeable($level = null) {
519
        if ($this->changeable === false) {
520
            return false;
521
        } else {
522
            return parent::is_changeable($level);
523
        }
524
    }
525
 
526
    /**
527
     * Sets whether the setting is changeable,
528
     * Note dependencies can still mark this setting changeable or not
529
     * @param bool $newvalue
530
     */
531
    public function set_changeable($newvalue) {
532
        $this->changeable = ($newvalue);
533
    }
534
}
535
 
536
/**
537
 * Radio button user interface element for backup settings
538
 *
539
 * @package core_backup
540
 * @copyright 2010 Sam Hemelryk
541
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
542
 */
543
class backup_setting_ui_radio extends backup_setting_ui {
544
    /**
545
     * @var int
546
     */
547
    protected $type = backup_setting::UI_HTML_RADIOBUTTON;
548
 
549
    /**
550
     * The string shown next to the input
551
     * @var string
552
     */
553
    protected $text;
554
 
555
    /**
556
     * The value for the radio input
557
     * @var string
558
     */
559
    protected $value;
560
 
561
    /**
562
     * Constructor
563
     *
564
     * @param backup_setting $setting
565
     * @param string $label
566
     * @param string $text
567
     * @param string $value
568
     * @param array $attributes
569
     * @param array $options
570
     */
571
    public function __construct(backup_setting $setting, $label = null, $text = null, $value = null, array $attributes = array(), array $options = array()) {
572
        parent::__construct($setting, $label, $attributes, $options);
573
        $this->text = $text;
574
        $this->value = (string)$value;
575
    }
576
 
577
    /**
578
     * Returns an array of properties suitable for generating a quickforms element
579
     * @param base_task $task
580
     * @param renderer_base $output
581
     * @return array (element, name, label, text, value, attributes)
582
     */
1441 ariadna 583
    public function get_element_properties(?base_task $task = null, ?renderer_base $output = null) {
1 efrain 584
        $icon = $this->get_icon();
585
        $context = context_course::instance($task->get_courseid());
586
        $label = format_string($this->get_label($task), true, array('context' => $context));
587
        if (!empty($icon)) {
588
            $label .= $output->render($icon);
589
        }
590
        // Name, label, text, value, attributes.
591
        return $this->apply_options(array(
592
            'element' => 'radio',
593
            'name' => self::NAME_PREFIX.$this->name,
594
            'label' => $label,
595
            'text' => $this->text,
596
            'value' => $this->value,
597
            'attributes' => $this->attributes
598
        ));
599
    }
600
    /**
601
     * Sets the text next to this input
602
     * @param text $text
603
     */
604
    public function set_text($text) {
605
        $this->text = $text;
606
    }
607
    /**
608
     * Sets the value for the input
609
     * @param string $value
610
     */
611
    public function set_value($value) {
612
        $this->value = (string)$value;
613
    }
614
    /**
615
     * Gets the static value to show for the element
616
     */
617
    public function get_static_value() {
618
        return $this->value;
619
    }
620
}
621
 
622
/**
623
 * A select box, drop down user interface for backup settings
624
 *
625
 * @package core_backup
626
 * @copyright 2010 Sam Hemelryk
627
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
628
 */
629
class backup_setting_ui_select extends backup_setting_ui {
630
    /**
631
     * @var int
632
     */
633
    protected $type = backup_setting::UI_HTML_DROPDOWN;
634
 
635
    /**
636
     * An array of options to display in the select
637
     * @var array
638
     */
639
    protected $values;
640
 
641
    /**
642
     * Constructor
643
     *
644
     * @param backup_setting $setting
645
     * @param string $label
646
     * @param array $values
647
     * @param array $attributes
648
     * @param array $options
649
     */
650
    public function __construct(backup_setting $setting, $label = null, $values = null, array $attributes = array(), array $options = array()) {
651
        parent::__construct($setting, $label, $attributes, $options);
652
        $this->values = $values;
653
    }
654
 
655
    /**
656
     * Returns an array of properties suitable for generating a quickforms element
657
     * @param base_task $task
658
     * @param renderer_base $output
659
     * @return array (element, name, label, options, attributes)
660
     */
1441 ariadna 661
    public function get_element_properties(?base_task $task = null, ?renderer_base $output = null) {
1 efrain 662
        $icon = $this->get_icon();
663
        $context = context_course::instance($task->get_courseid());
664
        $label = format_string($this->get_label($task), true, array('context' => $context));
665
        if (!empty($icon)) {
666
            $label .= $output->render($icon);
667
        }
668
        // Name, label, options, attributes.
669
        return $this->apply_options(array(
670
            'element' => 'select',
671
            'name' => self::NAME_PREFIX.$this->name,
672
            'label' => $label,
673
            'options' => $this->values,
674
            'attributes' => $this->attributes
675
        ));
676
    }
677
 
678
    /**
679
     * Sets the options for the select box
680
     * @param array $values Associative array of value => text options
681
     */
682
    public function set_values(array $values) {
683
        $this->values = $values;
684
    }
685
 
686
    /**
687
     * Gets the static value for this select element
688
     * @return string
689
     */
690
    public function get_static_value() {
691
        return $this->values[$this->get_value()];
692
    }
693
 
694
    /**
695
     * Returns true if the setting is changeable, false otherwise
696
     *
697
     * @param int $level Optional, if provided only depedency_settings below or equal to this level are considered,
698
     *          when checking if the ui_setting is changeable. Although dependencies might cause a lock on this setting,
699
     *          they could be changeable in the same view.
700
     * @return bool
701
     */
702
    public function is_changeable($level = null) {
703
        if (count($this->values) == 1) {
704
            return false;
705
        } else {
706
            return parent::is_changeable($level);
707
        }
708
    }
709
 
710
    /**
711
     * Returns the list of available values
712
     * @return array
713
     */
714
    public function get_values() {
715
        return $this->values;
716
    }
717
}
718
 
719
/**
720
 * A date selector user interface widget for backup settings.
721
 *
722
 * @package core_backup
723
 * @copyright 2010 Sam Hemelryk
724
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
725
 */
726
class backup_setting_ui_dateselector extends backup_setting_ui_text {
727
 
728
    /**
729
     * Returns an array of properties suitable for generating a quickforms element
730
     * @param base_task $task
731
     * @param renderer_base $output
732
     * @return array (element, name, label, options, attributes)
733
     */
1441 ariadna 734
    public function get_element_properties(?base_task $task = null, ?renderer_base $output = null) {
1 efrain 735
        if (!array_key_exists('optional', $this->attributes)) {
736
            $this->attributes['optional'] = false;
737
        }
738
        $properties = parent::get_element_properties($task, $output);
739
        $properties['element'] = 'date_selector';
740
        return $properties;
741
    }
742
 
743
    /**
744
     * Gets the static value for this select element
745
     * @return string
746
     */
747
    public function get_static_value() {
748
        $value = $this->get_value();
749
        if (!empty($value)) {
750
            return userdate($value);
751
        }
752
        return parent::get_static_value();
753
    }
754
}
755
 
756
/**
757
 * A wrapper for defaultcustom form element - can have either text or date_selector type
758
 *
759
 * @package core_backup
760
 * @copyright 2017 Marina Glancy
761
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
762
 */
763
class backup_setting_ui_defaultcustom extends backup_setting_ui_text {
764
 
765
    /**
766
     * Constructor
767
     *
768
     * @param backup_setting $setting
769
     * @param string $label The label to display with the setting ui
770
     * @param array $attributes Array of HTML attributes to apply to the element
771
     * @param array $options Array of options to apply to the setting ui object
772
     */
1441 ariadna 773
    public function __construct(backup_setting $setting, $label = null, ?array $attributes = null, ?array $options = null) {
1 efrain 774
        if (!is_array($attributes)) {
775
            $attributes = [];
776
        }
777
        $attributes += ['customlabel' => get_string('overwrite', 'backup'),
778
            'type' => 'text'];
779
        parent::__construct($setting, $label, $attributes, $options);
780
    }
781
 
782
    /**
783
     * Returns an array of properties suitable for generating a quickforms element
784
     * @param base_task $task
785
     * @param renderer_base $output
786
     * @return array (element, name, label, options, attributes)
787
     */
1441 ariadna 788
    public function get_element_properties(?base_task $task = null, ?renderer_base $output = null) {
1 efrain 789
        return ['element' => 'defaultcustom'] + parent::get_element_properties($task, $output);
790
    }
791
 
792
    /**
793
     * Gets the static value for this select element
794
     * @return string
795
     */
796
    public function get_static_value() {
797
        $value = $this->get_value();
798
        if ($value === false) {
799
            $value = $this->attributes['defaultvalue'];
800
        }
801
        if (!empty($value)) {
802
            if ($this->attributes['type'] === 'date_selector' ||
803
                    $this->attributes['type'] === 'date_time_selector') {
804
                return userdate($value);
805
            }
806
        }
807
        return $value;
808
    }
809
}
810
 
811
/**
812
 * Base setting UI exception class.
813
 *
814
 * @package core_backup
815
 * @copyright 2010 Sam Hemelryk
816
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
817
 */
818
class base_setting_ui_exception extends base_setting_exception {}
819
 
820
/**
821
 * Backup setting UI exception class.
822
 *
823
 * @package core_backup
824
 * @copyright 2010 Sam Hemelryk
825
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
826
 */
827
class backup_setting_ui_exception extends base_setting_ui_exception {};