|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00030 abstract class backup_setting extends base_setting implements checksumable { 00031 00032 // Some constants defining levels of setting 00033 const ROOT_LEVEL = 1; 00034 const COURSE_LEVEL = 5; 00035 const SECTION_LEVEL = 9; 00036 const ACTIVITY_LEVEL = 13; 00037 00042 protected $level; // level of the setting 00043 00044 public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) { 00045 parent::__construct($name, $vtype, $value, $visibility, $status); 00046 // Generate a default ui 00047 $this->uisetting = new backup_setting_ui_checkbox($this, $name); 00048 } 00049 00055 public function get_level() { 00056 return $this->level; 00057 } 00058 00067 public function make_ui($type, $label, array $attributes = null, array $options = null) { 00068 $this->uisetting = backup_setting_ui::make($this, $type, $label, $attributes, $options); 00069 if (is_array($options) || is_object($options)) { 00070 $options = (array)$options; 00071 switch (get_class($this->uisetting)) { 00072 case 'backup_setting_ui_radio' : 00073 // text 00074 if (array_key_exists('text', $options)) { 00075 $this->uisetting->set_text($options['text']); 00076 } 00077 case 'backup_setting_ui_checkbox' : 00078 // value 00079 if (array_key_exists('value', $options)) { 00080 $this->uisetting->set_value($options['value']); 00081 } 00082 break; 00083 case 'backup_setting_ui_select' : 00084 // options 00085 if (array_key_exists('options', $options)) { 00086 $this->uisetting->set_values($options['options']); 00087 } 00088 break; 00089 } 00090 } 00091 } 00092 00093 public function add_dependency(backup_setting $dependentsetting, $type=setting_dependency::DISABLED_VALUE, $options=array()) { 00094 // Check the dependency level is >= current level 00095 if ($dependentsetting->get_level() < $this->level) { 00096 throw new backup_setting_exception('cannot_add_upper_level_dependency'); 00097 } 00098 parent::add_dependency($dependentsetting, $type, $options); 00099 } 00100 00101 // checksumable interface methods 00102 00103 public function calculate_checksum() { 00104 // Checksum is a simple md5 hash of name, value, level 00105 // Not following dependencies at all. Each setting will 00106 // calculate its own checksum 00107 return md5($this->name . '-' . $this->value . '-' . $this->level); 00108 } 00109 00110 public function is_checksum_correct($checksum) { 00111 return $this->calculate_checksum() === $checksum; 00112 } 00113 } 00114 00115 /* 00116 * Exception class used by all the @backup_setting stuff 00117 */ 00118 class backup_setting_exception extends base_setting_exception { 00119 }