| 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 |
namespace core_adminpresets\local\setting;
|
|
|
18 |
|
|
|
19 |
use admin_setting;
|
|
|
20 |
use moodle_exception;
|
|
|
21 |
use stdClass;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Admin tool presets plugin to load some settings.
|
|
|
25 |
*
|
|
|
26 |
* @package core_adminpresets
|
|
|
27 |
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
|
|
|
28 |
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class adminpresets_setting {
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* @var admin_setting
|
|
|
35 |
*/
|
|
|
36 |
protected $settingdata;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* @var delegation
|
|
|
40 |
*/
|
|
|
41 |
protected $delegation;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* The setting DB value
|
|
|
45 |
*
|
|
|
46 |
* @var mixed
|
|
|
47 |
*/
|
|
|
48 |
protected $value;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Stores the visible value of the setting DB value
|
|
|
52 |
*
|
|
|
53 |
* @var string
|
|
|
54 |
*/
|
|
|
55 |
protected $visiblevalue;
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* For multiple value settings, used to look for the other values
|
|
|
59 |
*
|
|
|
60 |
* @var string
|
|
|
61 |
*/
|
|
|
62 |
protected $attributes = false;
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* To store the setting attributes
|
|
|
66 |
*
|
|
|
67 |
* @var array
|
|
|
68 |
*/
|
|
|
69 |
protected $attributesvalues;
|
|
|
70 |
|
|
|
71 |
/** @var array To store the behaviors. */
|
|
|
72 |
protected array $behaviors = [];
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Stores the setting data and the selected value
|
|
|
76 |
*
|
|
|
77 |
* @param admin_setting $settingdata admin_setting subclass
|
|
|
78 |
* @param mixed $dbsettingvalue Actual value
|
|
|
79 |
*/
|
|
|
80 |
public function __construct(admin_setting $settingdata, $dbsettingvalue) {
|
|
|
81 |
$this->settingdata = $settingdata;
|
|
|
82 |
$this->delegation = new delegation();
|
|
|
83 |
|
|
|
84 |
if ($this->settingdata->plugin == '') {
|
|
|
85 |
$this->settingdata->plugin = 'none';
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// Applies specific children behaviors.
|
|
|
89 |
$this->set_behaviors();
|
|
|
90 |
$this->apply_behaviors();
|
|
|
91 |
|
|
|
92 |
// Cleaning value.
|
|
|
93 |
$this->set_value($dbsettingvalue);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Each class can overwrite this method to specify extra processes
|
|
|
98 |
*/
|
|
|
99 |
protected function set_behaviors() {
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Applies the children class specific behaviors
|
|
|
104 |
*
|
|
|
105 |
* See delegation class for the available extra behaviors
|
|
|
106 |
*/
|
|
|
107 |
protected function apply_behaviors() {
|
|
|
108 |
if (!empty($this->behaviors)) {
|
|
|
109 |
|
|
|
110 |
foreach ($this->behaviors as $behavior => $arguments) {
|
|
|
111 |
|
|
|
112 |
// The arguments of the behavior depends on the caller.
|
|
|
113 |
$methodname = 'extra_' . $behavior;
|
|
|
114 |
$this->delegation->{$methodname}($arguments);
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Gets the setting value.
|
|
|
121 |
*
|
|
|
122 |
* @return mixed The setting value
|
|
|
123 |
*/
|
|
|
124 |
public function get_value() {
|
|
|
125 |
return $this->value;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Sets the setting value cleaning it
|
|
|
130 |
*
|
|
|
131 |
* Child classes should overwrite method to clean more acurately
|
|
|
132 |
*
|
|
|
133 |
* @param mixed $value Setting value
|
|
|
134 |
* @return mixed Returns false if wrong param value
|
|
|
135 |
*/
|
|
|
136 |
protected function set_value($value) {
|
|
|
137 |
$this->value = $value;
|
|
|
138 |
$this->set_visiblevalue();
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
public function get_visiblevalue() {
|
|
|
142 |
return $this->visiblevalue;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Sets the visible name for the setting selected value
|
|
|
147 |
*
|
|
|
148 |
* In most cases the child classes will overwrite
|
|
|
149 |
*/
|
|
|
150 |
protected function set_visiblevalue() {
|
|
|
151 |
$this->visiblevalue = $this->value;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
public function get_attributes() {
|
|
|
155 |
return $this->attributes;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
public function get_attributes_values() {
|
|
|
159 |
return $this->attributesvalues;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
public function get_settingdata() {
|
|
|
163 |
return $this->settingdata;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
public function set_attribute_value($name, $value) {
|
|
|
167 |
$this->attributesvalues[$name] = $value;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
/**
|
|
|
171 |
* Saves the setting attributes values
|
|
|
172 |
*
|
|
|
173 |
* @return array|false Array of inserted ids (in config_log) or false if nothing was inserted
|
|
|
174 |
*/
|
|
|
175 |
public function save_attributes_values() {
|
|
|
176 |
// Plugin name or null.
|
|
|
177 |
$plugin = $this->settingdata->plugin;
|
|
|
178 |
if ($plugin == 'none' || $plugin == '') {
|
|
|
179 |
$plugin = null;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
if (!$this->attributesvalues) {
|
|
|
183 |
return false;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
// To store inserted ids.
|
|
|
187 |
$ids = [];
|
|
|
188 |
foreach ($this->attributesvalues as $name => $value) {
|
|
|
189 |
|
|
|
190 |
// Getting actual setting.
|
|
|
191 |
$actualsetting = get_config($plugin, $name);
|
|
|
192 |
|
|
|
193 |
// If it's the actual setting get off.
|
|
|
194 |
if ($value == $actualsetting) {
|
|
|
195 |
return false;
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
if ($id = $this->save_value($name, $value)) {
|
|
|
199 |
$ids[] = $id;
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
return $ids;
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Stores the setting into database, logs the change and returns the config_log inserted id
|
|
|
208 |
*
|
|
|
209 |
* @param bool $name Setting name to store.
|
|
|
210 |
* @param mixed $value Setting value to store.
|
|
|
211 |
* @return int|false config_log inserted id or false whenever the new value is the same as old value.
|
|
|
212 |
*/
|
|
|
213 |
public function save_value($name = false, $value = null) {
|
|
|
214 |
// Object values if no arguments.
|
|
|
215 |
if ($value === null) {
|
|
|
216 |
$value = $this->value;
|
|
|
217 |
}
|
|
|
218 |
if (!$name) {
|
|
|
219 |
$name = $this->settingdata->name;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
// Plugin name or null.
|
|
|
223 |
$plugin = $this->settingdata->plugin;
|
|
|
224 |
if ($plugin == 'none' || $plugin == '') {
|
|
|
225 |
$plugin = null;
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
// Getting the actual value.
|
|
|
229 |
$actualvalue = get_config($plugin, $name);
|
|
|
230 |
|
|
|
231 |
// If it's the same it's not necessary.
|
|
|
232 |
if ($actualvalue == $value) {
|
|
|
233 |
return false;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
set_config($name, $value, $plugin);
|
|
|
237 |
|
|
|
238 |
return $this->to_log($plugin, $name, $value, $actualvalue);
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* Copy of config_write method of the admin_setting class
|
|
|
243 |
*
|
|
|
244 |
* @param string $plugin
|
|
|
245 |
* @param string $name
|
|
|
246 |
* @param mixed $value
|
|
|
247 |
* @param mixed $actualvalue
|
|
|
248 |
* @return integer The stored config_log id
|
|
|
249 |
*/
|
|
|
250 |
protected function to_log($plugin, $name, $value, $actualvalue) {
|
|
|
251 |
global $DB, $USER;
|
|
|
252 |
|
|
|
253 |
// Log the change (pasted from admin_setting class).
|
|
|
254 |
$log = new stdClass();
|
|
|
255 |
$log->userid = during_initial_install() ? 0 : $USER->id; // 0 as user id during install.
|
|
|
256 |
$log->timemodified = time();
|
|
|
257 |
$log->plugin = $plugin;
|
|
|
258 |
$log->name = $name;
|
|
|
259 |
$log->value = $value;
|
|
|
260 |
$log->oldvalue = $actualvalue;
|
|
|
261 |
|
|
|
262 |
// Getting the inserted config_log id.
|
|
|
263 |
if (!$id = $DB->insert_record('config_log', $log)) {
|
|
|
264 |
throw new moodle_exception('errorinserting', 'core_adminpresets');
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
return $id;
|
|
|
268 |
}
|
|
|
269 |
}
|