MDL-30982 Restore moodle2 files - phpDocs categorization
[moodle.git] / backup / util / settings / backup_setting.class.php
CommitLineData
69dd0c8c
EL
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 * @package moodlecore
20 * @subpackage backup-settings
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * This abstract class defines one backup_setting
27 *
28 * TODO: Finish phpdocs
29 */
30abstract class backup_setting extends base_setting implements checksumable {
31
32 // Some constants defining levels of setting
33 const ROOT_LEVEL = 1;
34 const COURSE_LEVEL = 5;
35 const SECTION_LEVEL = 9;
36 const ACTIVITY_LEVEL = 13;
37
17f04f53
SH
38 /**
39 * One of the above constants
40 * @var {int}
41 */
69dd0c8c
EL
42 protected $level; // level of the setting
43
17f04f53
SH
44 public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
45 parent::__construct($name, $vtype, $value, $visibility, $status);
46 // Generate a default ui
47 $this->uisetting = new backup_setting_ui_checkbox($this, $name);
48 }
49
50 /**
51 * Returns the level of the setting
fb95ef9e 52 *
17f04f53
SH
53 * @return {int} One of the above constants
54 */
69dd0c8c
EL
55 public function get_level() {
56 return $this->level;
57 }
58
17f04f53
SH
59 /**
60 * Creates and sets a user interface for this setting given appropriate arguments
61 *
62 * @param int $type
63 * @param string $label
64 * @param array $attributes
65 * @param array $options
66 */
67 public function make_ui($type, $label, array $attributes = null, array $options = null) {
17f04f53
SH
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' :
73 // text
74 if (array_key_exists('text', $options)) {
75 $this->uisetting->set_text($options['text']);
76 }
77 case 'backup_setting_ui_checkbox' :
78 // value
79 if (array_key_exists('value', $options)) {
80 $this->uisetting->set_value($options['value']);
81 }
82 break;
83 case 'backup_setting_ui_select' :
84 // options
85 if (array_key_exists('options', $options)) {
86 $this->uisetting->set_values($options['options']);
87 }
88 break;
89 }
90 }
91 }
92
1904e9b3 93 public function add_dependency(backup_setting $dependentsetting, $type=setting_dependency::DISABLED_VALUE, $options=array()) {
69dd0c8c 94 // Check the dependency level is >= current level
1904e9b3 95 if ($dependentsetting->get_level() < $this->level) {
69dd0c8c
EL
96 throw new backup_setting_exception('cannot_add_upper_level_dependency');
97 }
1904e9b3 98 parent::add_dependency($dependentsetting, $type, $options);
69dd0c8c
EL
99 }
100
101// checksumable interface methods
102
103 public function calculate_checksum() {
104 // Checksum is a simple md5 hash of name, value, level
105 // Not following dependencies at all. Each setting will
106 // calculate its own checksum
107 return md5($this->name . '-' . $this->value . '-' . $this->level);
108 }
109
110 public function is_checksum_correct($checksum) {
111 return $this->calculate_checksum() === $checksum;
112 }
113}
114
115/*
116 * Exception class used by all the @backup_setting stuff
117 */
118class backup_setting_exception extends base_setting_exception {
119}