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
 
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
 * Defines backup_setting class
20
 *
21
 * @package     core_backup
22
 * @category    backup
23
 * @copyright   2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
24
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
/**
30
 * Parent class for all backup settings
31
 */
32
abstract class backup_setting extends base_setting implements checksumable {
33
 
34
    // Some constants defining levels of setting
35
    const ROOT_LEVEL     = 1;
36
    const COURSE_LEVEL   = 5;
37
    const SECTION_LEVEL  = 9;
38
    const ACTIVITY_LEVEL = 13;
39
 
1441 ariadna 40
    /** @var int the subsection level. */
41
    const SUBSECTION_LEVEL = 17;
42
 
43
    /** @var int the activity inside a subsection level. */
44
    const SUBACTIVITY_LEVEL = 21;
45
 
1 efrain 46
    /** @var int Level of the setting, eg {@link self::ROOT_LEVEL} */
47
    protected $level;
48
 
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
53
        parent::__construct($name, $vtype, $value, $visibility, $status);
54
        // Generate a default ui
55
        $this->uisetting = new backup_setting_ui_checkbox($this, $name);
56
    }
57
 
58
    /**
59
     * @return int Level of the setting, eg {@link self::ROOT_LEVEL}
60
     */
61
    public function get_level() {
62
        return $this->level;
63
    }
64
 
65
    /**
66
     * Creates and sets a user interface for this setting given appropriate arguments
67
     *
68
     * @param int $type
69
     * @param string $label
70
     * @param array $attributes
71
     * @param array $options
72
     */
1441 ariadna 73
    public function make_ui($type, $label, ?array $attributes = null, ?array $options = null) {
1 efrain 74
        $this->uisetting = backup_setting_ui::make($this, $type, $label, $attributes, $options);
75
        if (is_array($options) || is_object($options)) {
76
            $options = (array)$options;
77
            switch (get_class($this->uisetting)) {
78
                case 'backup_setting_ui_radio' :
79
                    // text
80
                    if (array_key_exists('text', $options)) {
81
                        $this->uisetting->set_text($options['text']);
82
                    }
83
                case 'backup_setting_ui_checkbox' :
84
                    // value
85
                    if (array_key_exists('value', $options)) {
86
                        $this->uisetting->set_value($options['value']);
87
                    }
88
                    break;
89
                case 'backup_setting_ui_select' :
90
                    // options
91
                    if (array_key_exists('options', $options)) {
92
                        $this->uisetting->set_values($options['options']);
93
                    }
94
                    break;
95
            }
96
        }
97
    }
98
 
99
    public function add_dependency(base_setting $dependentsetting, $type=setting_dependency::DISABLED_VALUE, $options=array()) {
100
        if (!($dependentsetting instanceof backup_setting)) {
101
            throw new backup_setting_exception('invalid_backup_setting_parameter');
102
        }
103
        // Check the dependency level is >= current level
104
        if ($dependentsetting->get_level() < $this->level) {
1441 ariadna 105
            throw new backup_setting_exception('cannot_add_upper_level_dependency', [
106
                $dependentsetting->get_level(),
107
                $dependentsetting->get_name(),
108
                $this->level,
109
                $this->get_name(),
110
            ]);
1 efrain 111
        }
112
        parent::add_dependency($dependentsetting, $type, $options);
113
    }
114
 
115
// checksumable interface methods
116
 
117
    public function calculate_checksum() {
118
        // Checksum is a simple md5 hash of name, value, level
119
        // Not following dependencies at all. Each setting will
120
        // calculate its own checksum
121
        return md5($this->name . '-' . $this->value . '-' . $this->level);
122
    }
123
 
124
    public function is_checksum_correct($checksum) {
125
        return $this->calculate_checksum() === $checksum;
126
    }
127
}
128
 
129
/**
130
 * Exception class used by all the @backup_setting stuff
131
 */
132
class backup_setting_exception extends base_setting_exception {
133
}