3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Defines backup_setting class
21 * @package core_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
27 defined('MOODLE_INTERNAL') || die();
30 * Parent class for all backup settings
32 abstract class backup_setting extends base_setting implements checksumable {
34 // Some constants defining levels of setting
36 const COURSE_LEVEL = 5;
37 const SECTION_LEVEL = 9;
38 const ACTIVITY_LEVEL = 13;
40 /** @var int Level of the setting, eg {@link self::ROOT_LEVEL} */
46 public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
47 parent::__construct($name, $vtype, $value, $visibility, $status);
48 // Generate a default ui
49 $this->uisetting = new backup_setting_ui_checkbox($this, $name);
53 * @return int Level of the setting, eg {@link self::ROOT_LEVEL}
55 public function get_level() {
60 * Creates and sets a user interface for this setting given appropriate arguments
63 * @param string $label
64 * @param array $attributes
65 * @param array $options
67 public function make_ui($type, $label, array $attributes = null, array $options = null) {
68 $this->uisetting = backup_setting_ui::make($this, $type, $label, $attributes, $options);
69 if (is_array($options) || is_object($options)) {
70 $options = (array)$options;
71 switch (get_class($this->uisetting)) {
72 case 'backup_setting_ui_radio' :
74 if (array_key_exists('text', $options)) {
75 $this->uisetting->set_text($options['text']);
77 case 'backup_setting_ui_checkbox' :
79 if (array_key_exists('value', $options)) {
80 $this->uisetting->set_value($options['value']);
83 case 'backup_setting_ui_select' :
85 if (array_key_exists('options', $options)) {
86 $this->uisetting->set_values($options['options']);
93 public function add_dependency(base_setting $dependentsetting, $type=setting_dependency::DISABLED_VALUE, $options=array()) {
94 if (!($dependentsetting instanceof backup_setting)) {
95 throw new backup_setting_exception('invalid_backup_setting_parameter');
97 // Check the dependency level is >= current level
98 if ($dependentsetting->get_level() < $this->level) {
99 throw new backup_setting_exception('cannot_add_upper_level_dependency');
101 parent::add_dependency($dependentsetting, $type, $options);
104 // checksumable interface methods
106 public function calculate_checksum() {
107 // Checksum is a simple md5 hash of name, value, level
108 // Not following dependencies at all. Each setting will
109 // calculate its own checksum
110 return md5($this->name . '-' . $this->value . '-' . $this->level);
113 public function is_checksum_correct($checksum) {
114 return $this->calculate_checksum() === $checksum;
119 * Exception class used by all the @backup_setting stuff
121 class backup_setting_exception extends base_setting_exception {