Commit | Line | Data |
---|---|---|
db26acd4 | 1 | <?php |
2 | ||
0c079f19 | 3 | // This file is part of Moodle - http://moodle.org/ |
4 | // | |
db26acd4 | 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. | |
0c079f19 | 14 | // |
db26acd4 | 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/>. | |
88a7228a | 17 | |
18 | /** | |
0c079f19 | 19 | * Functions and classes used during installation, upgrades and for admin settings. |
db26acd4 | 20 | * |
0c079f19 | 21 | * ADMIN SETTINGS TREE INTRODUCTION |
db26acd4 | 22 | * |
23 | * This file performs the following tasks: | |
24 | * -it defines the necessary objects and interfaces to build the Moodle | |
25 | * admin hierarchy | |
61ef8f9f | 26 | * -it defines the admin_externalpage_setup() |
db26acd4 | 27 | * |
28 | * ADMIN_SETTING OBJECTS | |
29 | * | |
30 | * Moodle settings are represented by objects that inherit from the admin_setting | |
31 | * class. These objects encapsulate how to read a setting, how to write a new value | |
32 | * to a setting, and how to appropriately display the HTML to modify the setting. | |
33 | * | |
34 | * ADMIN_SETTINGPAGE OBJECTS | |
35 | * | |
36 | * The admin_setting objects are then grouped into admin_settingpages. The latter | |
37 | * appear in the Moodle admin tree block. All interaction with admin_settingpage | |
38 | * objects is handled by the admin/settings.php file. | |
39 | * | |
40 | * ADMIN_EXTERNALPAGE OBJECTS | |
41 | * | |
42 | * There are some settings in Moodle that are too complex to (efficiently) handle | |
43 | * with admin_settingpages. (Consider, for example, user management and displaying | |
44 | * lists of users.) In this case, we use the admin_externalpage object. This object | |
45 | * places a link to an external PHP file in the admin tree block. | |
46 | * | |
47 | * If you're using an admin_externalpage object for some settings, you can take | |
48 | * advantage of the admin_externalpage_* functions. For example, suppose you wanted | |
49 | * to add a foo.php file into admin. First off, you add the following line to | |
50 | * admin/settings/first.php (at the end of the file) or to some other file in | |
51 | * admin/settings: | |
52 | * <code> | |
53 | * $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'), | |
54 | * $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission')); | |
55 | * </code> | |
56 | * | |
57 | * Next, in foo.php, your file structure would resemble the following: | |
58 | * <code> | |
59 | * require_once('.../config.php'); | |
60 | * require_once($CFG->libdir.'/adminlib.php'); | |
61 | * admin_externalpage_setup('foo'); | |
62 | * // functionality like processing form submissions goes here | |
01a2ce80 | 63 | * $OUTPUT->header(); |
db26acd4 | 64 | * // your HTML goes here |
01a2ce80 | 65 | * $OUTPUT->footer(); |
db26acd4 | 66 | * </code> |
67 | * | |
68 | * The admin_externalpage_setup() function call ensures the user is logged in, | |
69 | * and makes sure that they have the proper role permission to access the page. | |
01a2ce80 | 70 | * It also configures all $PAGE properties needed for navigation. |
db26acd4 | 71 | * |
72 | * ADMIN_CATEGORY OBJECTS | |
73 | * | |
74 | * Above and beyond all this, we have admin_category objects. These objects | |
75 | * appear as folders in the admin tree block. They contain admin_settingpage's, | |
76 | * admin_externalpage's, and other admin_category's. | |
77 | * | |
78 | * OTHER NOTES | |
79 | * | |
80 | * admin_settingpage's, admin_externalpage's, and admin_category's all inherit | |
81 | * from part_of_admin_tree (a pseudointerface). This interface insists that | |
82 | * a class has a check_access method for access permissions, a locate method | |
83 | * used to find a specific node in the admin tree and find parent path. | |
84 | * | |
85 | * admin_category's inherit from parentable_part_of_admin_tree. This pseudo- | |
86 | * interface ensures that the class implements a recursive add function which | |
87 | * accepts a part_of_admin_tree object and searches for the proper place to | |
88 | * put it. parentable_part_of_admin_tree implies part_of_admin_tree. | |
89 | * | |
90 | * Please note that the $this->name field of any part_of_admin_tree must be | |
91 | * UNIQUE throughout the ENTIRE admin tree. | |
92 | * | |
93 | * The $this->name field of an admin_setting object (which is *not* part_of_ | |
94 | * admin_tree) must be unique on the respective admin_settingpage where it is | |
95 | * used. | |
96 | * | |
0c079f19 | 97 | * Original author: Vincenzo K. Marcovecchio |
98 | * Maintainer: Petr Skoda | |
db26acd4 | 99 | * |
100 | * @package moodlecore | |
101 | * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com | |
102 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
88a7228a | 103 | */ |
104 | ||
598b38f7 | 105 | /// Add libraries |
106 | require_once($CFG->libdir.'/ddllib.php'); | |
b1f93b15 | 107 | require_once($CFG->libdir.'/xmlize.php'); |
108 | ||
bba0beae | 109 | define('INSECURE_DATAROOT_WARNING', 1); |
110 | define('INSECURE_DATAROOT_ERROR', 2); | |
0c079f19 | 111 | |
ed996ede | 112 | /** |
113 | * Automatically clean-up all plugin data and remove the plugin DB tables | |
114 | * | |
115 | * @param string $type The plugin type, eg. 'mod', 'qtype', 'workshopgrading' etc. | |
116 | * @param string $name The plugin name, eg. 'forum', 'multichoice', 'accumulative' etc. | |
117 | * @uses global $OUTPUT to produce notices and other messages | |
118 | * @return void | |
119 | */ | |
120 | function uninstall_plugin($type, $name) { | |
121 | global $CFG, $DB, $OUTPUT; | |
122 | ||
846e4e17 PS |
123 | // recursively uninstall all module subplugins first |
124 | if ($type === 'mod') { | |
125 | if (file_exists("$CFG->dirroot/$name/db/subplugins.php")) { | |
126 | $subplugins = array(); | |
127 | include("$moddir/db/subplugins.php"); | |
128 | foreach ($subplugins as $subplugintype=>$dir) { | |
129 | $instances = get_plugin_list($subplugintype); | |
130 | foreach ($instances as $subpluginname => $notusedpluginpath) { | |
131 | uninstall_plugin($subplugintype, $subpluginname); | |
132 | } | |
ed996ede | 133 | } |
134 | } | |
846e4e17 | 135 | |
ed996ede | 136 | } |
137 | ||
2138244c | 138 | $component = $type . '_' . $name; // eg. 'qtype_multichoice' or 'workshopgrading_accumulative' or 'mod_forum' |
139 | ||
140 | if ($type === 'mod') { | |
141 | $pluginname = $name; // eg. 'forum' | |
142 | $strpluginname = get_string('modulename', $pluginname); | |
70d35fe6 PS |
143 | if (get_string_manager()->string_exists('modulename', $component)) { |
144 | $strpluginname = get_string('modulename', $component); | |
145 | } else { | |
146 | $strpluginname = $component; | |
147 | } | |
2138244c | 148 | } else { |
9baf6825 | 149 | $pluginname = $component; |
70d35fe6 PS |
150 | if (get_string_manager()->string_exists('pluginname', $component)) { |
151 | $strpluginname = get_string('pluginname', $component); | |
152 | } else { | |
153 | $strpluginname = $component; | |
154 | } | |
2138244c | 155 | } |
ed996ede | 156 | echo $OUTPUT->heading($pluginname); |
157 | ||
158 | $plugindirectory = get_plugin_directory($type, $name); | |
159 | $uninstalllib = $plugindirectory . '/db/uninstall.php'; | |
160 | if (file_exists($uninstalllib)) { | |
161 | require_once($uninstalllib); | |
2138244c | 162 | $uninstallfunction = 'xmldb_' . $pluginname . '_uninstall'; // eg. 'xmldb_workshop_uninstall()' |
ed996ede | 163 | if (function_exists($uninstallfunction)) { |
2138244c | 164 | if (!$uninstallfunction()) { |
ed996ede | 165 | echo $OUTPUT->notification('Encountered a problem running uninstall function for '. $pluginname); |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | if ('mod' === $type) { | |
9baf6825 | 171 | // perform cleanup tasks specific for activity modules |
ed996ede | 172 | |
ed996ede | 173 | if (!$module = $DB->get_record('modules', array('name' => $name))) { |
174 | print_error('moduledoesnotexist', 'error'); | |
175 | } | |
176 | ||
177 | // delete all the relevant instances from all course sections | |
178 | if ($coursemods = $DB->get_records('course_modules', array('module' => $module->id))) { | |
179 | foreach ($coursemods as $coursemod) { | |
180 | if (!delete_mod_from_section($coursemod->id, $coursemod->section)) { | |
181 | echo $OUTPUT->notification("Could not delete the $strpluginname with id = $coursemod->id from section $coursemod->section"); | |
182 | } | |
183 | } | |
184 | } | |
185 | ||
186 | // clear course.modinfo for courses that used this module | |
187 | $sql = "UPDATE {course} | |
188 | SET modinfo='' | |
189 | WHERE id IN (SELECT DISTINCT course | |
190 | FROM {course_modules} | |
191 | WHERE module=?)"; | |
192 | $DB->execute($sql, array($module->id)); | |
193 | ||
194 | // delete all the course module records | |
2138244c | 195 | $DB->delete_records('course_modules', array('module' => $module->id)); |
ed996ede | 196 | |
197 | // delete module contexts | |
198 | if ($coursemods) { | |
199 | foreach ($coursemods as $coursemod) { | |
200 | if (!delete_context(CONTEXT_MODULE, $coursemod->id)) { | |
201 | echo $OUTPUT->notification("Could not delete the context for $strpluginname with id = $coursemod->id"); | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
206 | // delete the module entry itself | |
2138244c | 207 | $DB->delete_records('modules', array('name' => $module->name)); |
ed996ede | 208 | |
209 | // cleanup the gradebook | |
210 | require_once($CFG->libdir.'/gradelib.php'); | |
211 | grade_uninstalled_module($module->name); | |
212 | ||
213 | // Perform any custom uninstall tasks | |
214 | if (file_exists($CFG->dirroot . '/mod/' . $module->name . '/lib.php')) { | |
215 | require_once($CFG->dirroot . '/mod/' . $module->name . '/lib.php'); | |
216 | $uninstallfunction = $module->name . '_uninstall'; | |
217 | if (function_exists($uninstallfunction)) { | |
218 | debugging("{$uninstallfunction}() has been deprecated. Use the plugin's db/uninstall.php instead", DEBUG_DEVELOPER); | |
2138244c | 219 | if (!$uninstallfunction()) { |
ed996ede | 220 | echo $OUTPUT->notification('Encountered a problem running uninstall function for '. $module->name.'!'); |
221 | } | |
222 | } | |
223 | } | |
224 | } | |
225 | ||
70d35fe6 | 226 | // perform clean-up task common for all the plugin/subplugin types |
ed996ede | 227 | |
228 | // delete calendar events | |
2138244c | 229 | $DB->delete_records('event', array('modulename' => $pluginname)); |
ed996ede | 230 | |
231 | // delete all the logs | |
2138244c | 232 | $DB->delete_records('log', array('module' => $pluginname)); |
ed996ede | 233 | |
234 | // delete log_display information | |
2138244c | 235 | $DB->delete_records('log_display', array('module' => $pluginname)); |
ed996ede | 236 | |
237 | // delete the module configuration records | |
2138244c | 238 | unset_all_config_for_plugin($pluginname); |
ed996ede | 239 | |
240 | // delete the plugin tables | |
241 | $xmldbfilepath = $plugindirectory . '/db/install.xml'; | |
242 | drop_plugin_tables($pluginname, $xmldbfilepath, false); | |
243 | ||
ed996ede | 244 | // delete the capabilities that were defined by this module |
245 | capabilities_cleanup($component); | |
246 | ||
247 | // remove entent handlers and dequeue pending events | |
248 | events_uninstall($component); | |
249 | ||
250 | echo $OUTPUT->notification(get_string('success'), 'notifysuccess'); | |
251 | } | |
252 | ||
b1f93b15 | 253 | /** |
254 | * Delete all plugin tables | |
db26acd4 | 255 | * |
db26acd4 | 256 | * @param string $name Name of plugin, used as table prefix |
257 | * @param string $file Path to install.xml file | |
258 | * @param bool $feedback defaults to true | |
0c079f19 | 259 | * @return bool Always returns true |
b1f93b15 | 260 | */ |
261 | function drop_plugin_tables($name, $file, $feedback=true) { | |
262 | global $CFG, $DB; | |
263 | ||
264 | // first try normal delete | |
8b4ca8f7 | 265 | if (file_exists($file) and $DB->get_manager()->delete_tables_from_xmldb_file($file)) { |
b1f93b15 | 266 | return true; |
267 | } | |
268 | ||
269 | // then try to find all tables that start with name and are not in any xml file | |
270 | $used_tables = get_used_table_names(); | |
271 | ||
272 | $tables = $DB->get_tables(); | |
273 | ||
274 | /// Iterate over, fixing id fields as necessary | |
275 | foreach ($tables as $table) { | |
276 | if (in_array($table, $used_tables)) { | |
277 | continue; | |
278 | } | |
279 | ||
8b4ca8f7 | 280 | if (strpos($table, $name) !== 0) { |
281 | continue; | |
282 | } | |
283 | ||
b1f93b15 | 284 | // found orphan table --> delete it |
285 | if ($DB->get_manager()->table_exists($table)) { | |
286 | $xmldb_table = new xmldb_table($table); | |
eee5d9bb | 287 | $DB->get_manager()->drop_table($xmldb_table); |
b1f93b15 | 288 | } |
289 | } | |
290 | ||
291 | return true; | |
292 | } | |
293 | ||
294 | /** | |
295 | * Returns names of all known tables == tables that moodle knowns about. | |
db26acd4 | 296 | * |
297 | * @return array Array of lowercase table names | |
b1f93b15 | 298 | */ |
299 | function get_used_table_names() { | |
300 | $table_names = array(); | |
301 | $dbdirs = get_db_directories(); | |
302 | ||
303 | foreach ($dbdirs as $dbdir) { | |
304 | $file = $dbdir.'/install.xml'; | |
305 | ||
306 | $xmldb_file = new xmldb_file($file); | |
307 | ||
308 | if (!$xmldb_file->fileExists()) { | |
309 | continue; | |
310 | } | |
311 | ||
312 | $loaded = $xmldb_file->loadXMLStructure(); | |
73fa96d5 | 313 | $structure = $xmldb_file->getStructure(); |
b1f93b15 | 314 | |
315 | if ($loaded and $tables = $structure->getTables()) { | |
316 | foreach($tables as $table) { | |
317 | $table_names[] = strtolower($table->name); | |
318 | } | |
319 | } | |
320 | } | |
321 | ||
322 | return $table_names; | |
323 | } | |
324 | ||
325 | /** | |
326 | * Returns list of all directories where we expect install.xml files | |
db26acd4 | 327 | * @return array Array of paths |
b1f93b15 | 328 | */ |
329 | function get_db_directories() { | |
330 | global $CFG; | |
331 | ||
332 | $dbdirs = array(); | |
333 | ||
9baf6825 | 334 | /// First, the main one (lib/db) |
b1f93b15 | 335 | $dbdirs[] = $CFG->libdir.'/db'; |
336 | ||
9baf6825 | 337 | /// Then, all the ones defined by get_plugin_types() |
17da2e6f | 338 | $plugintypes = get_plugin_types(); |
339 | foreach ($plugintypes as $plugintype => $pluginbasedir) { | |
340 | if ($plugins = get_plugin_list($plugintype)) { | |
341 | foreach ($plugins as $plugin => $plugindir) { | |
342 | $dbdirs[] = $plugindir.'/db'; | |
76b6c644 | 343 | } |
b91b274b | 344 | } |
7cdd8b22 | 345 | } |
b1f93b15 | 346 | |
b1f93b15 | 347 | return $dbdirs; |
348 | } | |
349 | ||
eef868d1 | 350 | /** |
61460dd6 | 351 | * Try to obtain or release the cron lock. |
61460dd6 | 352 | * @param string $name name of lock |
353 | * @param int $until timestamp when this lock considered stale, null means remove lock unconditionaly | |
db26acd4 | 354 | * @param bool $ignorecurrent ignore current lock state, usually entend previous lock, defaults to false |
61460dd6 | 355 | * @return bool true if lock obtained |
f3221af9 | 356 | */ |
61460dd6 | 357 | function set_cron_lock($name, $until, $ignorecurrent=false) { |
f33e1ed4 | 358 | global $DB; |
f3221af9 | 359 | if (empty($name)) { |
61460dd6 | 360 | debugging("Tried to get a cron lock for a null fieldname"); |
f3221af9 | 361 | return false; |
362 | } | |
363 | ||
61460dd6 | 364 | // remove lock by force == remove from config table |
365 | if (is_null($until)) { | |
366 | set_config($name, null); | |
f3221af9 | 367 | return true; |
368 | } | |
369 | ||
61460dd6 | 370 | if (!$ignorecurrent) { |
9baf6825 | 371 | // read value from db - other processes might have changed it |
f33e1ed4 | 372 | $value = $DB->get_field('config', 'value', array('name'=>$name)); |
61460dd6 | 373 | |
374 | if ($value and $value > time()) { | |
9baf6825 | 375 | //lock active |
61460dd6 | 376 | return false; |
f3221af9 | 377 | } |
378 | } | |
61460dd6 | 379 | |
380 | set_config($name, $until); | |
f3221af9 | 381 | return true; |
382 | } | |
a597f8a8 | 383 | |
bba0beae | 384 | /** |
385 | * Test if and critical warnings are present | |
386 | * @return bool | |
387 | */ | |
388 | function admin_critical_warnings_present() { | |
389 | global $SESSION; | |
390 | ||
391 | if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { | |
392 | return 0; | |
393 | } | |
394 | ||
395 | if (!isset($SESSION->admin_critical_warning)) { | |
396 | $SESSION->admin_critical_warning = 0; | |
fbf2c91e | 397 | if (is_dataroot_insecure(true) === INSECURE_DATAROOT_ERROR) { |
bba0beae | 398 | $SESSION->admin_critical_warning = 1; |
399 | } | |
400 | } | |
401 | ||
402 | return $SESSION->admin_critical_warning; | |
403 | } | |
404 | ||
61f9c4b4 | 405 | /** |
db26acd4 | 406 | * Detects if float supports at least 10 decimal digits |
407 | * | |
408 | * Detects if float supports at least 10 deciman digits | |
61f9c4b4 | 409 | * and also if float-->string conversion works as expected. |
db26acd4 | 410 | * |
61f9c4b4 | 411 | * @return bool true if problem found |
412 | */ | |
413 | function is_float_problem() { | |
414 | $num1 = 2009010200.01; | |
415 | $num2 = 2009010200.02; | |
416 | ||
417 | return ((string)$num1 === (string)$num2 or $num1 === $num2 or $num2 <= (string)$num1); | |
418 | } | |
419 | ||
57e35f32 | 420 | /** |
421 | * Try to verify that dataroot is not accessible from web. | |
57e35f32 | 422 | * |
db26acd4 | 423 | * Try to verify that dataroot is not accessible from web. |
424 | * It is not 100% correct but might help to reduce number of vulnerable sites. | |
57e35f32 | 425 | * Protection from httpd.conf and .htaccess is not detected properly. |
0c079f19 | 426 | * |
db26acd4 | 427 | * @uses INSECURE_DATAROOT_WARNING |
428 | * @uses INSECURE_DATAROOT_ERROR | |
429 | * @param bool $fetchtest try to test public access by fetching file, default false | |
bba0beae | 430 | * @return mixed empty means secure, INSECURE_DATAROOT_ERROR found a critical problem, INSECURE_DATAROOT_WARNING migth be problematic |
57e35f32 | 431 | */ |
bba0beae | 432 | function is_dataroot_insecure($fetchtest=false) { |
57e35f32 | 433 | global $CFG; |
434 | ||
435 | $siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround | |
436 | ||
437 | $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1); | |
438 | $rp = strrev(trim($rp, '/')); | |
439 | $rp = explode('/', $rp); | |
440 | foreach($rp as $r) { | |
441 | if (strpos($siteroot, '/'.$r.'/') === 0) { | |
442 | $siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory | |
443 | } else { | |
444 | break; // probably alias root | |
445 | } | |
446 | } | |
447 | ||
448 | $siteroot = strrev($siteroot); | |
449 | $dataroot = str_replace('\\', '/', $CFG->dataroot.'/'); | |
450 | ||
bba0beae | 451 | if (strpos($dataroot, $siteroot) !== 0) { |
452 | return false; | |
453 | } | |
454 | ||
455 | if (!$fetchtest) { | |
456 | return INSECURE_DATAROOT_WARNING; | |
457 | } | |
458 | ||
459 | // now try all methods to fetch a test file using http protocol | |
460 | ||
461 | $httpdocroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); | |
462 | preg_match('|(https?://[^/]+)|i', $CFG->wwwroot, $matches); | |
463 | $httpdocroot = $matches[1]; | |
464 | $datarooturl = $httpdocroot.'/'. substr($dataroot, strlen($siteroot)); | |
465 | if (make_upload_directory('diag', false) === false) { | |
466 | return INSECURE_DATAROOT_WARNING; | |
467 | } | |
468 | $testfile = $CFG->dataroot.'/diag/public.txt'; | |
469 | if (!file_exists($testfile)) { | |
470 | file_put_contents($testfile, 'test file, do not delete'); | |
471 | } | |
472 | $teststr = trim(file_get_contents($testfile)); | |
473 | if (empty($teststr)) { | |
9baf6825 | 474 | // hmm, strange |
bba0beae | 475 | return INSECURE_DATAROOT_WARNING; |
476 | } | |
477 | ||
478 | $testurl = $datarooturl.'/diag/public.txt'; | |
041a4b0f | 479 | if (extension_loaded('curl') and |
480 | !(stripos(ini_get('disable_functions'), 'curl_init') !== FALSE) and | |
481 | !(stripos(ini_get('disable_functions'), 'curl_setop') !== FALSE) and | |
482 | ($ch = @curl_init($testurl)) !== false) { | |
bba0beae | 483 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
484 | curl_setopt($ch, CURLOPT_HEADER, false); | |
485 | $data = curl_exec($ch); | |
486 | if (!curl_errno($ch)) { | |
487 | $data = trim($data); | |
488 | if ($data === $teststr) { | |
489 | curl_close($ch); | |
490 | return INSECURE_DATAROOT_ERROR; | |
491 | } | |
492 | } | |
493 | curl_close($ch); | |
494 | } | |
495 | ||
496 | if ($data = @file_get_contents($testurl)) { | |
497 | $data = trim($data); | |
498 | if ($data === $teststr) { | |
499 | return INSECURE_DATAROOT_ERROR; | |
500 | } | |
501 | } | |
502 | ||
503 | preg_match('|https?://([^/]+)|i', $testurl, $matches); | |
504 | $sitename = $matches[1]; | |
505 | $error = 0; | |
506 | if ($fp = @fsockopen($sitename, 80, $error)) { | |
507 | preg_match('|https?://[^/]+(.*)|i', $testurl, $matches); | |
508 | $localurl = $matches[1]; | |
509 | $out = "GET $localurl HTTP/1.1\r\n"; | |
510 | $out .= "Host: $sitename\r\n"; | |
511 | $out .= "Connection: Close\r\n\r\n"; | |
512 | fwrite($fp, $out); | |
513 | $data = ''; | |
514 | $incoming = false; | |
515 | while (!feof($fp)) { | |
516 | if ($incoming) { | |
517 | $data .= fgets($fp, 1024); | |
518 | } else if (@fgets($fp, 1024) === "\r\n") { | |
9baf6825 | 519 | $incoming = true; |
520 | } | |
bba0beae | 521 | } |
522 | fclose($fp); | |
523 | $data = trim($data); | |
524 | if ($data === $teststr) { | |
525 | return INSECURE_DATAROOT_ERROR; | |
526 | } | |
57e35f32 | 527 | } |
bba0beae | 528 | |
529 | return INSECURE_DATAROOT_WARNING; | |
57e35f32 | 530 | } |
6e4dc10f | 531 | |
6e4dc10f | 532 | /// CLASS DEFINITIONS ///////////////////////////////////////////////////////// |
533 | ||
534 | /** | |
535 | * Pseudointerface for anything appearing in the admin tree | |
536 | * | |
537 | * The pseudointerface that is implemented by anything that appears in the admin tree | |
538 | * block. It forces inheriting classes to define a method for checking user permissions | |
539 | * and methods for finding something in the admin tree. | |
540 | * | |
db26acd4 | 541 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
6e4dc10f | 542 | */ |
73fa96d5 | 543 | interface part_of_admin_tree { |
6e4dc10f | 544 | |
9baf6825 | 545 | /** |
546 | * Finds a named part_of_admin_tree. | |
547 | * | |
548 | * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree | |
549 | * and not parentable_part_of_admin_tree, then this function should only check if | |
550 | * $this->name matches $name. If it does, it should return a reference to $this, | |
551 | * otherwise, it should return a reference to NULL. | |
552 | * | |
553 | * If a class inherits parentable_part_of_admin_tree, this method should be called | |
554 | * recursively on all child objects (assuming, of course, the parent object's name | |
555 | * doesn't match the search criterion). | |
556 | * | |
557 | * @param string $name The internal name of the part_of_admin_tree we're searching for. | |
558 | * @return mixed An object reference or a NULL reference. | |
559 | */ | |
73fa96d5 | 560 | public function locate($name); |
4672d955 | 561 | |
562 | /** | |
563 | * Removes named part_of_admin_tree. | |
564 | * | |
565 | * @param string $name The internal name of the part_of_admin_tree we want to remove. | |
a8a66c96 | 566 | * @return bool success. |
4672d955 | 567 | */ |
73fa96d5 | 568 | public function prune($name); |
4672d955 | 569 | |
220a90c5 | 570 | /** |
571 | * Search using query | |
db26acd4 | 572 | * @param string $query |
220a90c5 | 573 | * @return mixed array-object structure of found settings and pages |
574 | */ | |
73fa96d5 | 575 | public function search($query); |
220a90c5 | 576 | |
6e4dc10f | 577 | /** |
578 | * Verifies current user's access to this part_of_admin_tree. | |
579 | * | |
580 | * Used to check if the current user has access to this part of the admin tree or | |
581 | * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree, | |
582 | * then this method is usually just a call to has_capability() in the site context. | |
583 | * | |
584 | * If a class inherits parentable_part_of_admin_tree, this method should return the | |
585 | * logical OR of the return of check_access() on all child objects. | |
586 | * | |
587 | * @return bool True if the user has access, false if she doesn't. | |
588 | */ | |
73fa96d5 | 589 | public function check_access(); |
eef868d1 | 590 | |
a8a66c96 | 591 | /** |
592 | * Mostly usefull for removing of some parts of the tree in admin tree block. | |
593 | * | |
594 | * @return True is hidden from normal list view | |
595 | */ | |
73fa96d5 | 596 | public function is_hidden(); |
427649bf PS |
597 | |
598 | /** | |
599 | * Show we display Save button at the page bottom? | |
600 | * @return bool | |
601 | */ | |
602 | public function show_save(); | |
6e4dc10f | 603 | } |
604 | ||
605 | /** | |
606 | * Pseudointerface implemented by any part_of_admin_tree that has children. | |
607 | * | |
608 | * The pseudointerface implemented by any part_of_admin_tree that can be a parent | |
609 | * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart | |
eef868d1 | 610 | * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods |
6e4dc10f | 611 | * include an add method for adding other part_of_admin_tree objects as children. |
612 | * | |
db26acd4 | 613 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
6e4dc10f | 614 | */ |
73fa96d5 | 615 | interface parentable_part_of_admin_tree extends part_of_admin_tree { |
eef868d1 | 616 | |
9baf6825 | 617 | /** |
618 | * Adds a part_of_admin_tree object to the admin tree. | |
619 | * | |
620 | * Used to add a part_of_admin_tree object to this object or a child of this | |
621 | * object. $something should only be added if $destinationname matches | |
622 | * $this->name. If it doesn't, add should be called on child objects that are | |
623 | * also parentable_part_of_admin_tree's. | |
624 | * | |
625 | * @param string $destinationname The internal name of the new parent for $something. | |
626 | * @param part_of_admin_tree $something The object to be added. | |
627 | * @return bool True on success, false on failure. | |
628 | */ | |
73fa96d5 | 629 | public function add($destinationname, $something); |
eef868d1 | 630 | |
6e4dc10f | 631 | } |
632 | ||
633 | /** | |
634 | * The object used to represent folders (a.k.a. categories) in the admin tree block. | |
eef868d1 | 635 | * |
6e4dc10f | 636 | * Each admin_category object contains a number of part_of_admin_tree objects. |
637 | * | |
db26acd4 | 638 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
6e4dc10f | 639 | */ |
73fa96d5 | 640 | class admin_category implements parentable_part_of_admin_tree { |
6e4dc10f | 641 | |
9baf6825 | 642 | /** @var mixed An array of part_of_admin_tree objects that are this object's children */ |
73fa96d5 | 643 | public $children; |
0c079f19 | 644 | /** @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects */ |
73fa96d5 | 645 | public $name; |
0c079f19 | 646 | /** @var string The displayed name for this category. Usually obtained through get_string() */ |
73fa96d5 | 647 | public $visiblename; |
0c079f19 | 648 | /** @var bool Should this category be hidden in admin tree block? */ |
73fa96d5 | 649 | public $hidden; |
0c079f19 | 650 | /** @var mixed Either a string or an array or strings */ |
73fa96d5 | 651 | public $path; |
0c079f19 | 652 | /** @var mixed Either a string or an array or strings */ |
73fa96d5 | 653 | public $visiblepath; |
6e4dc10f | 654 | |
655 | /** | |
656 | * Constructor for an empty admin category | |
657 | * | |
658 | * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects | |
659 | * @param string $visiblename The displayed named for this category. Usually obtained through get_string() | |
db26acd4 | 660 | * @param bool $hidden hide category in admin tree block, defaults to false |
6e4dc10f | 661 | */ |
73fa96d5 | 662 | public function __construct($name, $visiblename, $hidden=false) { |
220a90c5 | 663 | $this->children = array(); |
664 | $this->name = $name; | |
6e4dc10f | 665 | $this->visiblename = $visiblename; |
220a90c5 | 666 | $this->hidden = $hidden; |
6e4dc10f | 667 | } |
eef868d1 | 668 | |
6e4dc10f | 669 | /** |
220a90c5 | 670 | * Returns a reference to the part_of_admin_tree object with internal name $name. |
6e4dc10f | 671 | * |
220a90c5 | 672 | * @param string $name The internal name of the object we want. |
673 | * @param bool $findpath initialize path and visiblepath arrays | |
0c079f19 | 674 | * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL. |
db26acd4 | 675 | * defaults to false |
6e4dc10f | 676 | */ |
73fa96d5 | 677 | public function locate($name, $findpath=false) { |
6e4dc10f | 678 | if ($this->name == $name) { |
220a90c5 | 679 | if ($findpath) { |
680 | $this->visiblepath[] = $this->visiblename; | |
681 | $this->path[] = $this->name; | |
682 | } | |
683 | return $this; | |
6e4dc10f | 684 | } |
eef868d1 | 685 | |
220a90c5 | 686 | $return = NULL; |
687 | foreach($this->children as $childid=>$unused) { | |
73fa96d5 | 688 | if ($return = $this->children[$childid]->locate($name, $findpath)) { |
220a90c5 | 689 | break; |
6e4dc10f | 690 | } |
691 | } | |
eef868d1 | 692 | |
220a90c5 | 693 | if (!is_null($return) and $findpath) { |
694 | $return->visiblepath[] = $this->visiblename; | |
695 | $return->path[] = $this->name; | |
696 | } | |
eef868d1 | 697 | |
220a90c5 | 698 | return $return; |
6e4dc10f | 699 | } |
700 | ||
701 | /** | |
220a90c5 | 702 | * Search using query |
db26acd4 | 703 | * |
704 | * @param string query | |
220a90c5 | 705 | * @return mixed array-object structure of found settings and pages |
6e4dc10f | 706 | */ |
73fa96d5 | 707 | public function search($query) { |
220a90c5 | 708 | $result = array(); |
709 | foreach ($this->children as $child) { | |
3cea9c55 | 710 | $subsearch = $child->search($query); |
711 | if (!is_array($subsearch)) { | |
712 | debugging('Incorrect search result from '.$child->name); | |
713 | continue; | |
714 | } | |
715 | $result = array_merge($result, $subsearch); | |
6e4dc10f | 716 | } |
220a90c5 | 717 | return $result; |
6e4dc10f | 718 | } |
719 | ||
4672d955 | 720 | /** |
721 | * Removes part_of_admin_tree object with internal name $name. | |
722 | * | |
723 | * @param string $name The internal name of the object we want to remove. | |
a8a66c96 | 724 | * @return bool success |
4672d955 | 725 | */ |
73fa96d5 | 726 | public function prune($name) { |
4672d955 | 727 | |
728 | if ($this->name == $name) { | |
729 | return false; //can not remove itself | |
730 | } | |
731 | ||
732 | foreach($this->children as $precedence => $child) { | |
733 | if ($child->name == $name) { | |
9baf6825 | 734 | // found it! |
eef868d1 | 735 | unset($this->children[$precedence]); |
4672d955 | 736 | return true; |
737 | } | |
738 | if ($this->children[$precedence]->prune($name)) { | |
739 | return true; | |
740 | } | |
741 | } | |
742 | return false; | |
743 | } | |
744 | ||
6e4dc10f | 745 | /** |
746 | * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object. | |
747 | * | |
220a90c5 | 748 | * @param string $destinationame The internal name of the immediate parent that we want for $something. |
749 | * @param mixed $something A part_of_admin_tree or setting instanceto be added. | |
750 | * @return bool True if successfully added, false if $something can not be added. | |
6e4dc10f | 751 | */ |
73fa96d5 | 752 | public function add($parentname, $something) { |
753 | $parent = $this->locate($parentname); | |
220a90c5 | 754 | if (is_null($parent)) { |
755 | debugging('parent does not exist!'); | |
6e4dc10f | 756 | return false; |
757 | } | |
758 | ||
73fa96d5 | 759 | if ($something instanceof part_of_admin_tree) { |
760 | if (!($parent instanceof parentable_part_of_admin_tree)) { | |
220a90c5 | 761 | debugging('error - parts of tree can be inserted only into parentable parts'); |
762 | return false; | |
6e4dc10f | 763 | } |
220a90c5 | 764 | $parent->children[] = $something; |
6e4dc10f | 765 | return true; |
eef868d1 | 766 | |
220a90c5 | 767 | } else { |
768 | debugging('error - can not add this element'); | |
769 | return false; | |
6e4dc10f | 770 | } |
eef868d1 | 771 | |
6e4dc10f | 772 | } |
eef868d1 | 773 | |
6e4dc10f | 774 | /** |
775 | * Checks if the user has access to anything in this category. | |
776 | * | |
777 | * @return bool True if the user has access to atleast one child in this category, false otherwise. | |
778 | */ | |
73fa96d5 | 779 | public function check_access() { |
6e4dc10f | 780 | foreach ($this->children as $child) { |
220a90c5 | 781 | if ($child->check_access()) { |
782 | return true; | |
783 | } | |
6e4dc10f | 784 | } |
220a90c5 | 785 | return false; |
6e4dc10f | 786 | } |
eef868d1 | 787 | |
a8a66c96 | 788 | /** |
789 | * Is this category hidden in admin tree block? | |
790 | * | |
791 | * @return bool True if hidden | |
792 | */ | |
73fa96d5 | 793 | public function is_hidden() { |
a8a66c96 | 794 | return $this->hidden; |
795 | } | |
427649bf PS |
796 | |
797 | /** | |
798 | * Show we display Save button at the page bottom? | |
799 | * @return bool | |
800 | */ | |
801 | public function show_save() { | |
802 | foreach ($this->children as $child) { | |
803 | if ($child->show_save()) { | |
804 | return true; | |
805 | } | |
806 | } | |
807 | return false; | |
808 | } | |
6e4dc10f | 809 | } |
810 | ||
db26acd4 | 811 | /** |
0c079f19 | 812 | * Root of admin settings tree, does not have any parent. |
db26acd4 | 813 | * |
814 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
db26acd4 | 815 | */ |
220a90c5 | 816 | class admin_root extends admin_category { |
9baf6825 | 817 | /** @var array List of errors */ |
73fa96d5 | 818 | public $errors; |
0c079f19 | 819 | /** @var string search query */ |
73fa96d5 | 820 | public $search; |
0c079f19 | 821 | /** @var bool full tree flag - true means all settings required, false onlypages required */ |
73fa96d5 | 822 | public $fulltree; |
0c079f19 | 823 | /** @var bool flag indicating loaded tree */ |
73fa96d5 | 824 | public $loaded; |
0c079f19 | 825 | /** @var mixed site custom defaults overriding defaults in setings files*/ |
cd3acbf2 | 826 | public $custom_defaults; |
827 | ||
db26acd4 | 828 | /** |
0c079f19 | 829 | * @param bool $fulltree true means all settings required, |
db26acd4 | 830 | * false only pages required |
831 | */ | |
73fa96d5 | 832 | public function __construct($fulltree) { |
cd3acbf2 | 833 | global $CFG; |
834 | ||
73fa96d5 | 835 | parent::__construct('root', get_string('administration'), false); |
220a90c5 | 836 | $this->errors = array(); |
837 | $this->search = ''; | |
73fa96d5 | 838 | $this->fulltree = $fulltree; |
839 | $this->loaded = false; | |
cd3acbf2 | 840 | |
841 | // load custom defaults if found | |
842 | $this->custom_defaults = null; | |
843 | $defaultsfile = "$CFG->dirroot/local/defaults.php"; | |
844 | if (is_readable($defaultsfile)) { | |
845 | $defaults = array(); | |
846 | include($defaultsfile); | |
847 | if (is_array($defaults) and count($defaults)) { | |
848 | $this->custom_defaults = $defaults; | |
849 | } | |
850 | } | |
73fa96d5 | 851 | } |
852 | ||
db26acd4 | 853 | /** |
854 | * Empties children array, and sets loaded to false | |
855 | * | |
856 | * @param bool $requirefulltree | |
857 | */ | |
73fa96d5 | 858 | public function purge_children($requirefulltree) { |
859 | $this->children = array(); | |
860 | $this->fulltree = ($requirefulltree || $this->fulltree); | |
861 | $this->loaded = false; | |
220a90c5 | 862 | } |
863 | } | |
864 | ||
6e4dc10f | 865 | /** |
866 | * Links external PHP pages into the admin tree. | |
867 | * | |
868 | * See detailed usage example at the top of this document (adminlib.php) | |
869 | * | |
db26acd4 | 870 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
6e4dc10f | 871 | */ |
73fa96d5 | 872 | class admin_externalpage implements part_of_admin_tree { |
6e4dc10f | 873 | |
9baf6825 | 874 | /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */ |
73fa96d5 | 875 | public $name; |
eef868d1 | 876 | |
0c079f19 | 877 | /** @var string The displayed name for this external page. Usually obtained through get_string(). */ |
73fa96d5 | 878 | public $visiblename; |
eef868d1 | 879 | |
0c079f19 | 880 | /** @var string The external URL that we should link to when someone requests this external page. */ |
73fa96d5 | 881 | public $url; |
eef868d1 | 882 | |
0c079f19 | 883 | /** @var string The role capability/permission a user must have to access this external page. */ |
73fa96d5 | 884 | public $req_capability; |
eef868d1 | 885 | |
0c079f19 | 886 | /** @var object The context in which capability/permission should be checked, default is site context. */ |
73fa96d5 | 887 | public $context; |
84c8ede0 | 888 | |
0c079f19 | 889 | /** @var bool hidden in admin tree block. */ |
73fa96d5 | 890 | public $hidden; |
a8a66c96 | 891 | |
0c079f19 | 892 | /** @var mixed either string or array of string */ |
73fa96d5 | 893 | public $path; |
894 | public $visiblepath; | |
220a90c5 | 895 | |
6e4dc10f | 896 | /** |
897 | * Constructor for adding an external page into the admin tree. | |
898 | * | |
899 | * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects. | |
900 | * @param string $visiblename The displayed name for this external page. Usually obtained through get_string(). | |
901 | * @param string $url The external URL that we should link to when someone requests this external page. | |
38d2d43b | 902 | * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'. |
92f00846 | 903 | * @param boolean $hidden Is this external page hidden in admin tree block? Default false. |
904 | * @param context $context The context the page relates to. Not sure what happens | |
905 | * if you specify something other than system or front page. Defaults to system. | |
6e4dc10f | 906 | */ |
73fa96d5 | 907 | public function __construct($name, $visiblename, $url, $req_capability='moodle/site:config', $hidden=false, $context=NULL) { |
220a90c5 | 908 | $this->name = $name; |
6e4dc10f | 909 | $this->visiblename = $visiblename; |
220a90c5 | 910 | $this->url = $url; |
38d2d43b | 911 | if (is_array($req_capability)) { |
912 | $this->req_capability = $req_capability; | |
913 | } else { | |
914 | $this->req_capability = array($req_capability); | |
915 | } | |
92f00846 | 916 | $this->hidden = $hidden; |
84c8ede0 | 917 | $this->context = $context; |
6e4dc10f | 918 | } |
eef868d1 | 919 | |
6e4dc10f | 920 | /** |
921 | * Returns a reference to the part_of_admin_tree object with internal name $name. | |
922 | * | |
923 | * @param string $name The internal name of the object we want. | |
db26acd4 | 924 | * @param bool $findpath defaults to false |
6e4dc10f | 925 | * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL. |
926 | */ | |
73fa96d5 | 927 | public function locate($name, $findpath=false) { |
220a90c5 | 928 | if ($this->name == $name) { |
929 | if ($findpath) { | |
930 | $this->visiblepath = array($this->visiblename); | |
931 | $this->path = array($this->name); | |
932 | } | |
933 | return $this; | |
934 | } else { | |
935 | $return = NULL; | |
936 | return $return; | |
937 | } | |
6e4dc10f | 938 | } |
4672d955 | 939 | |
db26acd4 | 940 | /** |
941 | * This function always returns false, required function by interface | |
942 | * | |
943 | * @param string $name | |
944 | * @return false | |
945 | */ | |
73fa96d5 | 946 | public function prune($name) { |
4672d955 | 947 | return false; |
948 | } | |
949 | ||
220a90c5 | 950 | /** |
951 | * Search using query | |
db26acd4 | 952 | * |
953 | * @param string $query | |
220a90c5 | 954 | * @return mixed array-object structure of found settings and pages |
955 | */ | |
73fa96d5 | 956 | public function search($query) { |
220a90c5 | 957 | $textlib = textlib_get_instance(); |
958 | ||
959 | $found = false; | |
960 | if (strpos(strtolower($this->name), $query) !== false) { | |
961 | $found = true; | |
962 | } else if (strpos($textlib->strtolower($this->visiblename), $query) !== false) { | |
9baf6825 | 963 | $found = true; |
964 | } | |
220a90c5 | 965 | if ($found) { |
966 | $result = new object(); | |
967 | $result->page = $this; | |
968 | $result->settings = array(); | |
969 | return array($this->name => $result); | |
970 | } else { | |
971 | return array(); | |
972 | } | |
973 | } | |
974 | ||
6e4dc10f | 975 | /** |
2ce38b70 | 976 | * Determines if the current user has access to this external page based on $this->req_capability. |
db26acd4 | 977 | * |
6e4dc10f | 978 | * @return bool True if user has access, false otherwise. |
979 | */ | |
73fa96d5 | 980 | public function check_access() { |
1caea91e | 981 | global $CFG; |
84c8ede0 | 982 | $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context; |
38d2d43b | 983 | foreach($this->req_capability as $cap) { |
4f0c2d00 | 984 | if (has_capability($cap, $context)) { |
38d2d43b | 985 | return true; |
986 | } | |
987 | } | |
988 | return false; | |
6e4dc10f | 989 | } |
990 | ||
a8a66c96 | 991 | /** |
992 | * Is this external page hidden in admin tree block? | |
993 | * | |
994 | * @return bool True if hidden | |
995 | */ | |
73fa96d5 | 996 | public function is_hidden() { |
a8a66c96 | 997 | return $this->hidden; |
998 | } | |
999 | ||
427649bf PS |
1000 | /** |
1001 | * Show we display Save button at the page bottom? | |
1002 | * @return bool | |
1003 | */ | |
1004 | public function show_save() { | |
1005 | return false; | |
1006 | } | |
6e4dc10f | 1007 | } |
1008 | ||
1009 | /** | |
1010 | * Used to group a number of admin_setting objects into a page and add them to the admin tree. | |
1011 | * | |
db26acd4 | 1012 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
6e4dc10f | 1013 | */ |
73fa96d5 | 1014 | class admin_settingpage implements part_of_admin_tree { |
6e4dc10f | 1015 | |
9baf6825 | 1016 | /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */ |
73fa96d5 | 1017 | public $name; |
eef868d1 | 1018 | |
0c079f19 | 1019 | /** @var string The displayed name for this external page. Usually obtained through get_string(). */ |
73fa96d5 | 1020 | public $visiblename; |
0c079f19 | 1021 | |
1022 | /** @var mixed An array of admin_setting objects that are part of this setting page. */ | |
73fa96d5 | 1023 | public $settings; |
eef868d1 | 1024 | |
0c079f19 | 1025 | /** @var string The role capability/permission a user must have to access this external page. */ |
73fa96d5 | 1026 | public $req_capability; |
eef868d1 | 1027 | |
0c079f19 | 1028 | /** @var object The context in which capability/permission should be checked, default is site context. */ |
73fa96d5 | 1029 | public $context; |
84c8ede0 | 1030 | |
0c079f19 | 1031 | /** @var bool hidden in admin tree block. */ |
73fa96d5 | 1032 | public $hidden; |
a8a66c96 | 1033 | |
0c079f19 | 1034 | /** @var mixed string of paths or array of strings of paths */ |
73fa96d5 | 1035 | public $path; |
1036 | public $visiblepath; | |
220a90c5 | 1037 | |
db26acd4 | 1038 | /** |
1039 | * see admin_settingpage for details of this function | |
0c079f19 | 1040 | * |
db26acd4 | 1041 | * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects. |
1042 | * @param string $visiblename The displayed name for this external page. Usually obtained through get_string(). | |
1043 | * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'. | |
1044 | * @param boolean $hidden Is this external page hidden in admin tree block? Default false. | |
1045 | * @param context $context The context the page relates to. Not sure what happens | |
1046 | * if you specify something other than system or front page. Defaults to system. | |
1047 | */ | |
73fa96d5 | 1048 | public function __construct($name, $visiblename, $req_capability='moodle/site:config', $hidden=false, $context=NULL) { |
220a90c5 | 1049 | $this->settings = new object(); |
1050 | $this->name = $name; | |
1051 | $this->visiblename = $visiblename; | |
1052 | if (is_array($req_capability)) { | |
1053 | $this->req_capability = $req_capability; | |
6e4dc10f | 1054 | } else { |
220a90c5 | 1055 | $this->req_capability = array($req_capability); |
6e4dc10f | 1056 | } |
220a90c5 | 1057 | $this->hidden = $hidden; |
1058 | $this->context = $context; | |
6e4dc10f | 1059 | } |
eef868d1 | 1060 | |
0c079f19 | 1061 | /** |
db26acd4 | 1062 | * see admin_category |
1063 | * | |
1064 | * @param string $name | |
1065 | * @param bool $findpath | |
1066 | * @return mixed Object (this) if name == this->name, else returns null | |
1067 | */ | |
73fa96d5 | 1068 | public function locate($name, $findpath=false) { |
220a90c5 | 1069 | if ($this->name == $name) { |
1070 | if ($findpath) { | |
1071 | $this->visiblepath = array($this->visiblename); | |
1072 | $this->path = array($this->name); | |
1073 | } | |
1074 | return $this; | |
1075 | } else { | |
1076 | $return = NULL; | |
1077 | return $return; | |
1078 | } | |
6e4dc10f | 1079 | } |
4672d955 | 1080 | |
0c079f19 | 1081 | /** |
1082 | * Search string in settings page. | |
1083 | * | |
db26acd4 | 1084 | * @param string $query |
1085 | * @return array | |
1086 | */ | |
73fa96d5 | 1087 | public function search($query) { |
220a90c5 | 1088 | $found = array(); |
4672d955 | 1089 | |
220a90c5 | 1090 | foreach ($this->settings as $setting) { |
1091 | if ($setting->is_related($query)) { | |
1092 | $found[] = $setting; | |
1093 | } | |
1094 | } | |
1095 | ||
1096 | if ($found) { | |
1097 | $result = new object(); | |
1098 | $result->page = $this; | |
1099 | $result->settings = $found; | |
1100 | return array($this->name => $result); | |
1101 | } | |
1102 | ||
1103 | $textlib = textlib_get_instance(); | |
1104 | ||
1105 | $found = false; | |
1106 | if (strpos(strtolower($this->name), $query) !== false) { | |
1107 | $found = true; | |
1108 | } else if (strpos($textlib->strtolower($this->visiblename), $query) !== false) { | |
9baf6825 | 1109 | $found = true; |
1110 | } | |
220a90c5 | 1111 | if ($found) { |
1112 | $result = new object(); | |
1113 | $result->page = $this; | |
1114 | $result->settings = array(); | |
1115 | return array($this->name => $result); | |
38d2d43b | 1116 | } else { |
220a90c5 | 1117 | return array(); |
38d2d43b | 1118 | } |
6e4dc10f | 1119 | } |
eef868d1 | 1120 | |
db26acd4 | 1121 | /** |
1122 | * This function always returns false, required by interface | |
1123 | * | |
1124 | * @param string $name | |
1125 | * @return bool Always false | |
1126 | */ | |
73fa96d5 | 1127 | public function prune($name) { |
6e4dc10f | 1128 | return false; |
1129 | } | |
eef868d1 | 1130 | |
220a90c5 | 1131 | /** |
db26acd4 | 1132 | * adds an admin_setting to this admin_settingpage |
1133 | * | |
220a90c5 | 1134 | * not the same as add for admin_category. adds an admin_setting to this admin_settingpage. settings appear (on the settingpage) in the order in which they're added |
1135 | * n.b. each admin_setting in an admin_settingpage must have a unique internal name | |
db26acd4 | 1136 | * |
220a90c5 | 1137 | * @param object $setting is the admin_setting object you want to add |
db26acd4 | 1138 | * @return bool true if successful, false if not |
220a90c5 | 1139 | */ |
73fa96d5 | 1140 | public function add($setting) { |
1141 | if (!($setting instanceof admin_setting)) { | |
220a90c5 | 1142 | debugging('error - not a setting instance'); |
1143 | return false; | |
1144 | } | |
1145 | ||
1146 | $this->settings->{$setting->name} = $setting; | |
1147 | return true; | |
1148 | } | |
1149 | ||
db26acd4 | 1150 | /** |
1151 | * see admin_externalpage | |
1152 | * | |
1153 | * @return bool Returns true for yes false for no | |
1154 | */ | |
73fa96d5 | 1155 | public function check_access() { |
1caea91e | 1156 | global $CFG; |
84c8ede0 | 1157 | $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context; |
38d2d43b | 1158 | foreach($this->req_capability as $cap) { |
4f0c2d00 | 1159 | if (has_capability($cap, $context)) { |
38d2d43b | 1160 | return true; |
1161 | } | |
1162 | } | |
1163 | return false; | |
6e4dc10f | 1164 | } |
eef868d1 | 1165 | |
220a90c5 | 1166 | /** |
1167 | * outputs this page as html in a table (suitable for inclusion in an admin pagetype) | |
db26acd4 | 1168 | * @return string Returns an XHTML string |
220a90c5 | 1169 | */ |
73fa96d5 | 1170 | public function output_html() { |
1171 | $adminroot = admin_get_root(); | |
220a90c5 | 1172 | $return = '<fieldset>'."\n".'<div class="clearer"><!-- --></div>'."\n"; |
6e4dc10f | 1173 | foreach($this->settings as $setting) { |
220a90c5 | 1174 | $fullname = $setting->get_full_name(); |
1175 | if (array_key_exists($fullname, $adminroot->errors)) { | |
1176 | $data = $adminroot->errors[$fullname]->data; | |
6e4dc10f | 1177 | } else { |
220a90c5 | 1178 | $data = $setting->get_setting(); |
79698344 | 1179 | // do not use defaults if settings not available - upgrade settings handles the defaults! |
6e4dc10f | 1180 | } |
220a90c5 | 1181 | $return .= $setting->output_html($data); |
6e4dc10f | 1182 | } |
220a90c5 | 1183 | $return .= '</fieldset>'; |
6e4dc10f | 1184 | return $return; |
1185 | } | |
1186 | ||
a8a66c96 | 1187 | /** |
1188 | * Is this settigns page hidden in admin tree block? | |
1189 | * | |
1190 | * @return bool True if hidden | |
1191 | */ | |
73fa96d5 | 1192 | public function is_hidden() { |
a8a66c96 | 1193 | return $this->hidden; |
1194 | } | |
1195 | ||
427649bf PS |
1196 | /** |
1197 | * Show we display Save button at the page bottom? | |
1198 | * @return bool | |
1199 | */ | |
1200 | public function show_save() { | |
1201 | foreach($this->settings as $setting) { | |
1202 | if (empty($setting->nosave)) { | |
1203 | return true; | |
1204 | } | |
1205 | } | |
1206 | return false; | |
1207 | } | |
6e4dc10f | 1208 | } |
1209 | ||
1210 | ||
220a90c5 | 1211 | /** |
1212 | * Admin settings class. Only exists on setting pages. | |
1213 | * Read & write happens at this level; no authentication. | |
db26acd4 | 1214 | * |
1215 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 1216 | */ |
301bf0b2 | 1217 | abstract class admin_setting { |
9baf6825 | 1218 | /** @var string unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. */ |
73fa96d5 | 1219 | public $name; |
0c079f19 | 1220 | /** @var string localised name */ |
73fa96d5 | 1221 | public $visiblename; |
3fa37159 | 1222 | /** @var string localised long description in Markdown format */ |
73fa96d5 | 1223 | public $description; |
0c079f19 | 1224 | /** @var mixed Can be string or array of string */ |
73fa96d5 | 1225 | public $defaultsetting; |
0c079f19 | 1226 | /** @var string */ |
73fa96d5 | 1227 | public $updatedcallback; |
0c079f19 | 1228 | /** @var mixed can be String or Null. Null means main config table */ |
73fa96d5 | 1229 | public $plugin; // null means main config table |
427649bf PS |
1230 | /** @var bool true indicates this setting does not actually save anything, just information */ |
1231 | public $nosave = false; | |
6e4dc10f | 1232 | |
220a90c5 | 1233 | /** |
1234 | * Constructor | |
0c079f19 | 1235 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, |
db26acd4 | 1236 | * or 'myplugin/mysetting' for ones in config_plugins. |
1a41e806 | 1237 | * @param string $visiblename localised name |
1238 | * @param string $description localised long description | |
220a90c5 | 1239 | * @param mixed $defaultsetting string or array depending on implementation |
1240 | */ | |
73fa96d5 | 1241 | public function __construct($name, $visiblename, $description, $defaultsetting) { |
7fb0303d | 1242 | $this->parse_setting_name($name); |
220a90c5 | 1243 | $this->visiblename = $visiblename; |
3fa37159 | 1244 | $this->description = markdown_to_html($description); |
6e4dc10f | 1245 | $this->defaultsetting = $defaultsetting; |
1246 | } | |
eef868d1 | 1247 | |
7fb0303d | 1248 | /** |
db26acd4 | 1249 | * Set up $this->name and potentially $this->plugin |
1250 | * | |
7fb0303d | 1251 | * Set up $this->name and possibly $this->plugin based on whether $name looks |
1252 | * like 'settingname' or 'plugin/settingname'. Also, do some sanity checking | |
1253 | * on the names, that is, output a developer debug warning if the name | |
1254 | * contains anything other than [a-zA-Z0-9_]+. | |
1255 | * | |
1256 | * @param string $name the setting name passed in to the constructor. | |
1257 | */ | |
1258 | private function parse_setting_name($name) { | |
1259 | $bits = explode('/', $name); | |
1260 | if (count($bits) > 2) { | |
1261 | throw new moodle_exception('invalidadminsettingname', '', '', $name); | |
1262 | } | |
1263 | $this->name = array_pop($bits); | |
1264 | if (!preg_match('/^[a-zA-Z0-9_]+$/', $this->name)) { | |
1265 | throw new moodle_exception('invalidadminsettingname', '', '', $name); | |
1266 | } | |
1267 | if (!empty($bits)) { | |
1268 | $this->plugin = array_pop($bits); | |
cd3acbf2 | 1269 | if ($this->plugin === 'moodle') { |
1270 | $this->plugin = null; | |
1271 | } else if (!preg_match('/^[a-zA-Z0-9_]+$/', $this->plugin)) { | |
9baf6825 | 1272 | throw new moodle_exception('invalidadminsettingname', '', '', $name); |
1273 | } | |
7fb0303d | 1274 | } |
1275 | } | |
1276 | ||
db26acd4 | 1277 | /** |
1278 | * Returns the fullname prefixed by the plugin | |
1279 | * @return string | |
1280 | */ | |
73fa96d5 | 1281 | public function get_full_name() { |
220a90c5 | 1282 | return 's_'.$this->plugin.'_'.$this->name; |
1283 | } | |
1284 | ||
db26acd4 | 1285 | /** |
1286 | * Returns the ID string based on plugin and name | |
1287 | * @return string | |
1288 | */ | |
73fa96d5 | 1289 | public function get_id() { |
220a90c5 | 1290 | return 'id_s_'.$this->plugin.'_'.$this->name; |
1291 | } | |
1292 | ||
db26acd4 | 1293 | /** |
1294 | * Returns the config if possible | |
1295 | * | |
db26acd4 | 1296 | * @return mixed returns config if successfull else null |
1297 | */ | |
73fa96d5 | 1298 | public function config_read($name) { |
220a90c5 | 1299 | global $CFG; |
eb6a973c | 1300 | if (!empty($this->plugin)) { |
220a90c5 | 1301 | $value = get_config($this->plugin, $name); |
1302 | return $value === false ? NULL : $value; | |
1303 | ||
1304 | } else { | |
1305 | if (isset($CFG->$name)) { | |
1306 | return $CFG->$name; | |
1307 | } else { | |
1308 | return NULL; | |
1309 | } | |
1310 | } | |
1311 | } | |
1312 | ||
301bf0b2 | 1313 | /** |
db26acd4 | 1314 | * Used to set a config pair and log change |
301bf0b2 | 1315 | * |
db26acd4 | 1316 | * @param string $name |
1317 | * @param mixed $value Gets converted to string if not null | |
1318 | * @return bool Write setting to confix table | |
301bf0b2 | 1319 | */ |
73fa96d5 | 1320 | public function config_write($name, $value) { |
301bf0b2 | 1321 | global $DB, $USER, $CFG; |
1322 | ||
427649bf PS |
1323 | if ($this->nosave) { |
1324 | return true; | |
1325 | } | |
1326 | ||
301bf0b2 | 1327 | // make sure it is a real change |
1328 | $oldvalue = get_config($this->plugin, $name); | |
1329 | $oldvalue = ($oldvalue === false) ? null : $oldvalue; // normalise | |
1330 | $value = is_null($value) ? null : (string)$value; | |
1331 | ||
1332 | if ($oldvalue === $value) { | |
1333 | return true; | |
1334 | } | |
1335 | ||
1336 | // store change | |
1337 | set_config($name, $value, $this->plugin); | |
1338 | ||
301bf0b2 | 1339 | // log change |
1340 | $log = new object(); | |
31a99877 | 1341 | $log->userid = during_initial_install() ? 0 :$USER->id; // 0 as user id during install |
301bf0b2 | 1342 | $log->timemodified = time(); |
1343 | $log->plugin = $this->plugin; | |
1344 | $log->name = $name; | |
1345 | $log->value = $value; | |
1346 | $log->oldvalue = $oldvalue; | |
1347 | $DB->insert_record('config_log', $log); | |
1348 | ||
1349 | return true; // BC only | |
220a90c5 | 1350 | } |
1351 | ||
1352 | /** | |
1353 | * Returns current value of this setting | |
1354 | * @return mixed array or string depending on instance, NULL means not set yet | |
1355 | */ | |
301bf0b2 | 1356 | public abstract function get_setting(); |
eef868d1 | 1357 | |
220a90c5 | 1358 | /** |
1359 | * Returns default setting if exists | |
1360 | * @return mixed array or string depending on instance; NULL means no default, user must supply | |
1361 | */ | |
73fa96d5 | 1362 | public function get_defaultsetting() { |
cd3acbf2 | 1363 | $adminroot = admin_get_root(false, false); |
1364 | if (!empty($adminroot->custom_defaults)) { | |
1365 | $plugin = is_null($this->plugin) ? 'moodle' : $this->plugin; | |
1366 | if (isset($adminroot->custom_defaults[$plugin])) { | |
1367 | if (array_key_exists($this->name, $adminroot->custom_defaults[$plugin])) { // null is valid valie here ;-) | |
1368 | return $adminroot->custom_defaults[$plugin][$this->name]; | |
1369 | } | |
1370 | } | |
1371 | } | |
8e5da17a | 1372 | return $this->defaultsetting; |
1373 | } | |
1374 | ||
220a90c5 | 1375 | /** |
1376 | * Store new setting | |
db26acd4 | 1377 | * |
1378 | * @param mixed $data string or array, must not be NULL | |
1379 | * @return string empty string if ok, string error message otherwise | |
220a90c5 | 1380 | */ |
301bf0b2 | 1381 | public abstract function write_setting($data); |
eef868d1 | 1382 | |
220a90c5 | 1383 | /** |
1384 | * Return part of form with setting | |
db26acd4 | 1385 | * This function should always be overwritten |
1386 | * | |
1387 | * @param mixed $data array or string depending on setting | |
1388 | * @param string $query | |
220a90c5 | 1389 | * @return string |
1390 | */ | |
73fa96d5 | 1391 | public function output_html($data, $query='') { |
9baf6825 | 1392 | // should be overridden |
220a90c5 | 1393 | return; |
1394 | } | |
1395 | ||
1396 | /** | |
db26acd4 | 1397 | * Function called if setting updated - cleanup, cache reset, etc. |
1398 | * @param string $functionname Sets the function name | |
220a90c5 | 1399 | */ |
73fa96d5 | 1400 | public function set_updatedcallback($functionname) { |
220a90c5 | 1401 | $this->updatedcallback = $functionname; |
1402 | } | |
1403 | ||
1404 | /** | |
1405 | * Is setting related to query text - used when searching | |
1406 | * @param string $query | |
1407 | * @return bool | |
1408 | */ | |
73fa96d5 | 1409 | public function is_related($query) { |
220a90c5 | 1410 | if (strpos(strtolower($this->name), $query) !== false) { |
1411 | return true; | |
1412 | } | |
1413 | $textlib = textlib_get_instance(); | |
1414 | if (strpos($textlib->strtolower($this->visiblename), $query) !== false) { | |
1415 | return true; | |
1416 | } | |
1417 | if (strpos($textlib->strtolower($this->description), $query) !== false) { | |
1418 | return true; | |
1419 | } | |
587c7040 | 1420 | $current = $this->get_setting(); |
1421 | if (!is_null($current)) { | |
1422 | if (is_string($current)) { | |
1423 | if (strpos($textlib->strtolower($current), $query) !== false) { | |
1424 | return true; | |
1425 | } | |
1426 | } | |
1427 | } | |
1428 | $default = $this->get_defaultsetting(); | |
1429 | if (!is_null($default)) { | |
1430 | if (is_string($default)) { | |
1431 | if (strpos($textlib->strtolower($default), $query) !== false) { | |
1432 | return true; | |
1433 | } | |
1434 | } | |
1435 | } | |
220a90c5 | 1436 | return false; |
6e4dc10f | 1437 | } |
220a90c5 | 1438 | } |
eef868d1 | 1439 | |
220a90c5 | 1440 | /** |
1441 | * No setting - just heading and text. | |
db26acd4 | 1442 | * |
1443 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 1444 | */ |
1445 | class admin_setting_heading extends admin_setting { | |
9baf6825 | 1446 | /** |
1447 | * not a setting, just text | |
1448 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. | |
1449 | * @param string $heading heading | |
1450 | * @param string $information text in box | |
1451 | */ | |
73fa96d5 | 1452 | public function __construct($name, $heading, $information) { |
427649bf | 1453 | $this->nosave = true; |
73fa96d5 | 1454 | parent::__construct($name, $heading, $information, ''); |
220a90c5 | 1455 | } |
1456 | ||
db26acd4 | 1457 | /** |
1458 | * Always returns true | |
1459 | * @return bool Always returns true | |
1460 | */ | |
73fa96d5 | 1461 | public function get_setting() { |
220a90c5 | 1462 | return true; |
1463 | } | |
1464 | ||
db26acd4 | 1465 | /** |
1466 | * Always returns true | |
1467 | * @return bool Always returns true | |
1468 | */ | |
73fa96d5 | 1469 | public function get_defaultsetting() { |
220a90c5 | 1470 | return true; |
1471 | } | |
1472 | ||
db26acd4 | 1473 | /** |
1474 | * Never write settings | |
1475 | * @return string Always returns an empty string | |
1476 | */ | |
73fa96d5 | 1477 | public function write_setting($data) { |
9baf6825 | 1478 | // do not write any setting |
220a90c5 | 1479 | return ''; |
1480 | } | |
0c079f19 | 1481 | |
db26acd4 | 1482 | /** |
1483 | * Returns an HTML string | |
1484 | * @return string Returns an HTML string | |
1485 | */ | |
73fa96d5 | 1486 | public function output_html($data, $query='') { |
3c159385 | 1487 | global $OUTPUT; |
220a90c5 | 1488 | $return = ''; |
1489 | if ($this->visiblename != '') { | |
79698344 | 1490 | $return .= $OUTPUT->heading($this->visiblename, 3, 'main', true); |
220a90c5 | 1491 | } |
1492 | if ($this->description != '') { | |
ea85e1ee | 1493 | $return .= $OUTPUT->box(highlight($query, $this->description), 'generalbox formsettingheading'); |
220a90c5 | 1494 | } |
1495 | return $return; | |
1496 | } | |
1497 | } | |
6e4dc10f | 1498 | |
220a90c5 | 1499 | /** |
1500 | * The most flexibly setting, user is typing text | |
db26acd4 | 1501 | * |
1502 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 1503 | */ |
6e4dc10f | 1504 | class admin_setting_configtext extends admin_setting { |
1505 | ||
9baf6825 | 1506 | /** @var mixed int means PARAM_XXX type, string is a allowed format in regex */ |
73fa96d5 | 1507 | public $paramtype; |
0c079f19 | 1508 | /** @var int default field size */ |
73fa96d5 | 1509 | public $size; |
6e4dc10f | 1510 | |
220a90c5 | 1511 | /** |
db26acd4 | 1512 | * Config text contructor |
1513 | * | |
1a41e806 | 1514 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. |
220a90c5 | 1515 | * @param string $visiblename localised |
1516 | * @param string $description long localised info | |
1517 | * @param string $defaultsetting | |
1518 | * @param mixed $paramtype int means PARAM_XXX type, string is a allowed format in regex | |
f7633b0f | 1519 | * @param int $size default field size |
220a90c5 | 1520 | */ |
73fa96d5 | 1521 | public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $size=null) { |
6e4dc10f | 1522 | $this->paramtype = $paramtype; |
f7633b0f | 1523 | if (!is_null($size)) { |
1524 | $this->size = $size; | |
1525 | } else { | |
1526 | $this->size = ($paramtype == PARAM_INT) ? 5 : 30; | |
1527 | } | |
73fa96d5 | 1528 | parent::__construct($name, $visiblename, $description, $defaultsetting); |
6e4dc10f | 1529 | } |
1530 | ||
db26acd4 | 1531 | /** |
1532 | * Return the setting | |
1533 | * | |
1534 | * @return mixed returns config if successfull else null | |
1535 | */ | |
73fa96d5 | 1536 | public function get_setting() { |
220a90c5 | 1537 | return $this->config_read($this->name); |
6e4dc10f | 1538 | } |
eef868d1 | 1539 | |
73fa96d5 | 1540 | public function write_setting($data) { |
8cad6cca | 1541 | if ($this->paramtype === PARAM_INT and $data === '') { |
9baf6825 | 1542 | // do not complain if '' used instead of 0 |
8cad6cca | 1543 | $data = 0; |
1544 | } | |
220a90c5 | 1545 | // $data is a string |
c5d2d0dd | 1546 | $validated = $this->validate($data); |
e33fbf87 | 1547 | if ($validated !== true) { |
1548 | return $validated; | |
c235598d | 1549 | } |
220a90c5 | 1550 | return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f | 1551 | } |
1552 | ||
e33fbf87 | 1553 | /** |
1554 | * Validate data before storage | |
1555 | * @param string data | |
1556 | * @return mixed true if ok string if error found | |
1557 | */ | |
73fa96d5 | 1558 | public function validate($data) { |
58aaa8e9 | 1559 | // allow paramtype to be a custom regex if it is the form of /pattern/ |
1560 | if (preg_match('#^/.*/$#', $this->paramtype)) { | |
e33fbf87 | 1561 | if (preg_match($this->paramtype, $data)) { |
1562 | return true; | |
1563 | } else { | |
1564 | return get_string('validateerror', 'admin'); | |
1565 | } | |
1566 | ||
9e24fbd1 | 1567 | } else if ($this->paramtype === PARAM_RAW) { |
4ea56b3f | 1568 | return true; |
9baf6825 | 1569 | |
4ea56b3f | 1570 | } else { |
1571 | $cleaned = clean_param($data, $this->paramtype); | |
1572 | if ("$data" == "$cleaned") { // implicit conversion to string is needed to do exact comparison | |
1573 | return true; | |
e33fbf87 | 1574 | } else { |
4ea56b3f | 1575 | return get_string('validateerror', 'admin'); |
e33fbf87 | 1576 | } |
4ea56b3f | 1577 | } |
c235598d | 1578 | } |
1579 | ||
db26acd4 | 1580 | /** |
1581 | * Return an XHTML string for the setting | |
1582 | * @return string Returns an XHTML string | |
1583 | */ | |
73fa96d5 | 1584 | public function output_html($data, $query='') { |
220a90c5 | 1585 | $default = $this->get_defaultsetting(); |
1586 | ||
220a90c5 | 1587 | return format_admin_setting($this, $this->visiblename, |
9baf6825 | 1588 | '<div class="form-text defaultsnext"><input type="text" size="'.$this->size.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" /></div>', |
1589 | $this->description, true, '', $default, $query); | |
6e4dc10f | 1590 | } |
6e4dc10f | 1591 | } |
1592 | ||
220a90c5 | 1593 | /** |
1594 | * General text area without html editor. | |
db26acd4 | 1595 | * |
1596 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 1597 | */ |
1598 | class admin_setting_configtextarea extends admin_setting_configtext { | |
4fe2250a | 1599 | private $rows; |
1600 | private $cols; | |
eba8cd63 | 1601 | |
db26acd4 | 1602 | /** |
1603 | * @param string $name | |
1604 | * @param string $visiblename | |
1605 | * @param string $description | |
1606 | * @param mixed $defaultsetting string or array | |
1607 | * @param mixed $paramtype | |
1608 | * @param string $cols The number of columns to make the editor | |
1609 | * @param string $rows The number of rows to make the editor | |
1610 | */ | |
73fa96d5 | 1611 | public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $cols='60', $rows='8') { |
220a90c5 | 1612 | $this->rows = $rows; |
1613 | $this->cols = $cols; | |
73fa96d5 | 1614 | parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype); |
eba8cd63 | 1615 | } |
db26acd4 | 1616 | /** |
1617 | * Returns an XHTML string for the editor | |
1618 | * | |
1619 | * @param string $data | |
1620 | * @param string $query | |
1621 | * @return string XHTML string for the editor | |
1622 | */ | |
73fa96d5 | 1623 | public function output_html($data, $query='') { |
220a90c5 | 1624 | $default = $this->get_defaultsetting(); |
1625 | ||
587c7040 | 1626 | $defaultinfo = $default; |
1627 | if (!is_null($default) and $default !== '') { | |
1628 | $defaultinfo = "\n".$default; | |
c5d2d0dd | 1629 | } |
220a90c5 | 1630 | |
1631 | return format_admin_setting($this, $this->visiblename, | |
9baf6825 | 1632 | '<div class="form-textarea" ><textarea rows="'. $this->rows .'" cols="'. $this->cols .'" id="'. $this->get_id() .'" name="'. $this->get_full_name() .'">'. s($data) .'</textarea></div>', |
1633 | $this->description, true, '', $defaultinfo, $query); | |
4fe2250a | 1634 | } |
1635 | } | |
1636 | ||
1637 | /** | |
1638 | * General text area with html editor. | |
1639 | */ | |
1640 | class admin_setting_confightmleditor extends admin_setting_configtext { | |
1641 | private $rows; | |
1642 | private $cols; | |
0c079f19 | 1643 | |
4fe2250a | 1644 | /** |
1645 | * @param string $name | |
1646 | * @param string $visiblename | |
1647 | * @param string $description | |
1648 | * @param mixed $defaultsetting string or array | |
1649 | * @param mixed $paramtype | |
1650 | */ | |
1651 | public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $cols='60', $rows='8') { | |
1652 | $this->rows = $rows; | |
1653 | $this->cols = $cols; | |
1654 | parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype); | |
ff5fe311 | 1655 | editors_head_setup(); |
4fe2250a | 1656 | } |
1657 | /** | |
1658 | * Returns an XHTML string for the editor | |
1659 | * | |
1660 | * @param string $data | |
1661 | * @param string $query | |
1662 | * @return string XHTML string for the editor | |
1663 | */ | |
1664 | public function output_html($data, $query='') { | |
1665 | $default = $this->get_defaultsetting(); | |
1666 | ||
1667 | $defaultinfo = $default; | |
1668 | if (!is_null($default) and $default !== '') { | |
1669 | $defaultinfo = "\n".$default; | |
1670 | } | |
1671 | ||
1672 | $editor = get_preferred_texteditor(FORMAT_HTML); | |
69429650 | 1673 | $editor->use_editor($this->get_id(), array('noclean'=>true)); |
4fe2250a | 1674 | |
1675 | return format_admin_setting($this, $this->visiblename, | |
9baf6825 | 1676 | '<div class="form-textarea"><textarea rows="'. $this->rows .'" cols="'. $this->cols .'" id="'. $this->get_id() .'" name="'. $this->get_full_name() .'">'. s($data) .'</textarea></div>', |
1677 | $this->description, true, '', $defaultinfo, $query); | |
220a90c5 | 1678 | } |
1679 | } | |
1680 | ||
1681 | /** | |
1682 | * Password field, allows unmasking of password | |
db26acd4 | 1683 | * |
1684 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 1685 | */ |
1686 | class admin_setting_configpasswordunmask extends admin_setting_configtext { | |
9baf6825 | 1687 | /** |
1688 | * Constructor | |
1689 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. | |
1690 | * @param string $visiblename localised | |
1691 | * @param string $description long localised info | |
1692 | * @param string $defaultsetting default password | |
1693 | */ | |
73fa96d5 | 1694 | public function __construct($name, $visiblename, $description, $defaultsetting) { |
1695 | parent::__construct($name, $visiblename, $description, $defaultsetting, PARAM_RAW, 30); | |
220a90c5 | 1696 | } |
0c079f19 | 1697 | |
db26acd4 | 1698 | /** |
1699 | * Returns XHTML for the field | |
1700 | * Writes Javascript into the HTML below right before the last div | |
1701 | * | |
1702 | * @todo Make javascript available through newer methods if possible | |
1703 | * @param string $data Value for the field | |
1704 | * @param string $query Passed as final argument for format_admin_setting | |
1705 | * @return string XHTML field | |
1706 | */ | |
73fa96d5 | 1707 | public function output_html($data, $query='') { |
220a90c5 | 1708 | $id = $this->get_id(); |
1709 | $unmask = get_string('unmaskpassword', 'form'); | |
1710 | $unmaskjs = '<script type="text/javascript"> | |
eba8cd63 | 1711 | //<![CDATA[ |
633239f6 | 1712 | var is_ie = (navigator.userAgent.toLowerCase().indexOf("msie") != -1); |
1713 | ||
1714 | document.getElementById("'.$id.'").setAttribute("autocomplete", "off"); | |
1715 | ||
1716 | var unmaskdiv = document.getElementById("'.$id.'unmaskdiv"); | |
1717 | ||
1718 | var unmaskchb = document.createElement("input"); | |
1719 | unmaskchb.setAttribute("type", "checkbox"); | |
1720 | unmaskchb.setAttribute("id", "'.$id.'unmask"); | |
1721 | unmaskchb.onchange = function() {unmaskPassword("'.$id.'");}; | |
1722 | unmaskdiv.appendChild(unmaskchb); | |
1723 | ||
1724 | var unmasklbl = document.createElement("label"); | |
1725 | unmasklbl.innerHTML = "'.addslashes_js($unmask).'"; | |
1726 | if (is_ie) { | |
1727 | unmasklbl.setAttribute("htmlFor", "'.$id.'unmask"); | |
1728 | } else { | |
1729 | unmasklbl.setAttribute("for", "'.$id.'unmask"); | |
1730 | } | |
1731 | unmaskdiv.appendChild(unmasklbl); | |
1732 | ||
1733 | if (is_ie) { | |
1734 | // ugly hack to work around the famous onchange IE bug | |
1735 | unmaskchb.onclick = function() {this.blur();}; | |
1736 | unmaskdiv.onclick = function() {this.blur();}; | |
1737 | } | |
eba8cd63 | 1738 | //]]> |
1739 | </script>'; | |
220a90c5 | 1740 | return format_admin_setting($this, $this->visiblename, |
9baf6825 | 1741 | '<div class="form-password"><input type="password" size="'.$this->size.'" id="'.$id.'" name="'.$this->get_full_name().'" value="'.s($data).'" /><div class="unmask" id="'.$id.'unmaskdiv"></div>'.$unmaskjs.'</div>', |
1742 | $this->description, true, '', NULL, $query); | |
220a90c5 | 1743 | } |
1744 | } | |
1745 | ||
1746 | /** | |
e9c0fa35 | 1747 | * Path to directory |
db26acd4 | 1748 | * |
1749 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 1750 | */ |
e9c0fa35 | 1751 | class admin_setting_configfile extends admin_setting_configtext { |
9baf6825 | 1752 | /** |
1753 | * Constructor | |
1754 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. | |
1755 | * @param string $visiblename localised | |
1756 | * @param string $description long localised info | |
1757 | * @param string $defaultdirectory default directory location | |
1758 | */ | |
73fa96d5 | 1759 | public function __construct($name, $visiblename, $description, $defaultdirectory) { |
1760 | parent::__construct($name, $visiblename, $description, $defaultdirectory, PARAM_RAW, 50); | |
220a90c5 | 1761 | } |
1762 | ||
db26acd4 | 1763 | /** |
1764 | * Returns XHTML for the field | |
0c079f19 | 1765 | * |
db26acd4 | 1766 | * Returns XHTML for the field and also checks whether the file |
1767 | * specified in $data exists using file_exists() | |
1768 | * | |
1769 | * @param string $data File name and path to use in value attr | |
1770 | * @param string $query | |
1771 | * @return string XHTML field | |
1772 | */ | |
73fa96d5 | 1773 | public function output_html($data, $query='') { |
220a90c5 | 1774 | $default = $this->get_defaultsetting(); |
1775 | ||
220a90c5 | 1776 | if ($data) { |
e9c0fa35 | 1777 | if (file_exists($data)) { |
220a90c5 | 1778 | $executable = '<span class="pathok">✔</span>'; |
1779 | } else { | |
1780 | $executable = '<span class="patherror">✘</span>'; | |
1781 | } | |
1782 | } else { | |
1783 | $executable = ''; | |
1784 | } | |
1785 | ||
1786 | return format_admin_setting($this, $this->visiblename, | |
9baf6825 | 1787 | '<div class="form-file defaultsnext"><input type="text" size="'.$this->size.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" />'.$executable.'</div>', |
1788 | $this->description, true, '', $default, $query); | |
eba8cd63 | 1789 | } |
220a90c5 | 1790 | } |
1791 | ||
1792 | /** | |
e9c0fa35 | 1793 | * Path to executable file |
db26acd4 | 1794 | * |
1795 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 1796 | */ |
e9c0fa35 | 1797 | class admin_setting_configexecutable extends admin_setting_configfile { |
1798 | ||
9baf6825 | 1799 | /** |
1800 | * Returns an XHTML field | |
1801 | * | |
1802 | * @param string $data This is the value for the field | |
1803 | * @param string $query | |
1804 | * @return string XHTML field | |
1805 | */ | |
73fa96d5 | 1806 | public function output_html($data, $query='') { |
e9c0fa35 | 1807 | $default = $this->get_defaultsetting(); |
1808 | ||
1809 | if ($data) { | |
1810 | if (file_exists($data) and is_executable($data)) { | |
1811 | $executable = '<span class="pathok">✔</span>'; | |
1812 | } else { | |
1813 | $executable = '<span class="patherror">✘</span>'; | |
1814 | } | |
1815 | } else { | |
1816 | $executable = ''; | |
1817 | } | |
1818 | ||
1819 | return format_admin_setting($this, $this->visiblename, | |
9baf6825 | 1820 | '<div class="form-file defaultsnext"><input type="text" size="'.$this->size.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" />'.$executable.'</div>', |
1821 | $this->description, true, '', $default, $query); | |
220a90c5 | 1822 | } |
e9c0fa35 | 1823 | } |
220a90c5 | 1824 | |
e9c0fa35 | 1825 | /** |
1826 | * Path to directory | |
db26acd4 | 1827 | * |
1828 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
e9c0fa35 | 1829 | */ |
1830 | class admin_setting_configdirectory extends admin_setting_configfile { | |
db26acd4 | 1831 | |
9baf6825 | 1832 | /** |
1833 | * Returns an XHTML field | |
1834 | * | |
1835 | * @param string $data This is the value for the field | |
1836 | * @param string $query | |
1837 | * @return string XHTML | |
1838 | */ | |
73fa96d5 | 1839 | public function output_html($data, $query='') { |
220a90c5 | 1840 | $default = $this->get_defaultsetting(); |
1841 | ||
220a90c5 | 1842 | if ($data) { |
1843 | if (file_exists($data) and is_dir($data)) { | |
1844 | $executable = '<span class="pathok">✔</span>'; | |
1845 | } else { | |
1846 | $executable = '<span class="patherror">✘</span>'; | |
1847 | } | |
1848 | } else { | |
1849 | $executable = ''; | |
1850 | } | |
9ba38673 | 1851 | |
220a90c5 | 1852 | return format_admin_setting($this, $this->visiblename, |
9baf6825 | 1853 | '<div class="form-file defaultsnext"><input type="text" size="'.$this->size.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" />'.$executable.'</div>', |
1854 | $this->description, true, '', $default, $query); | |
220a90c5 | 1855 | } |
eba8cd63 | 1856 | } |
1857 | ||
220a90c5 | 1858 | /** |
1859 | * Checkbox | |
db26acd4 | 1860 | * |
1861 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 1862 | */ |
6e4dc10f | 1863 | class admin_setting_configcheckbox extends admin_setting { |
9baf6825 | 1864 | /** @var string Value used when checked */ |
73fa96d5 | 1865 | public $yes; |
0c079f19 | 1866 | /** @var string Value used when not checked */ |
73fa96d5 | 1867 | public $no; |
6e4dc10f | 1868 | |
220a90c5 | 1869 | /** |
1870 | * Constructor | |
1a41e806 | 1871 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. |
220a90c5 | 1872 | * @param string $visiblename localised |
1873 | * @param string $description long localised info | |
1874 | * @param string $defaultsetting | |
1875 | * @param string $yes value used when checked | |
1876 | * @param string $no value used when not checked | |
1877 | */ | |
73fa96d5 | 1878 | public function __construct($name, $visiblename, $description, $defaultsetting, $yes='1', $no='0') { |
1879 | parent::__construct($name, $visiblename, $description, $defaultsetting); | |
220a90c5 | 1880 | $this->yes = (string)$yes; |
1881 | $this->no = (string)$no; | |
6e4dc10f | 1882 | } |
1883 | ||
db26acd4 | 1884 | /** |
1885 | * Retrieves the current setting using the objects name | |
1886 | * | |
1887 | * @return string | |
1888 | */ | |
73fa96d5 | 1889 | public function get_setting() { |
220a90c5 | 1890 | return $this->config_read($this->name); |
6e4dc10f | 1891 | } |
eef868d1 | 1892 | |
db26acd4 | 1893 | /** |
1894 | * Sets the value for the setting | |
1895 | * | |
1896 | * Sets the value for the setting to either the yes or no values | |
1897 | * of the object by comparing $data to yes | |
1898 | * | |
1899 | * @param mixed $data Gets converted to str for comparison against yes value | |
1900 | * @return string empty string or error | |
1901 | */ | |
73fa96d5 | 1902 | public function write_setting($data) { |
220a90c5 | 1903 | if ((string)$data === $this->yes) { // convert to strings before comparison |
1904 | $data = $this->yes; | |
6e4dc10f | 1905 | } else { |
220a90c5 | 1906 | $data = $this->no; |
6e4dc10f | 1907 | } |
220a90c5 | 1908 | return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f | 1909 | } |
1910 | ||
db26acd4 | 1911 | /** |
1912 | * Returns an XHTML checkbox field | |
1913 | * | |
1914 | * @param string $data If $data matches yes then checkbox is checked | |
1915 | * @param string $query | |
1916 | * @return string XHTML field | |
1917 | */ | |
73fa96d5 | 1918 | public function output_html($data, $query='') { |
220a90c5 | 1919 | $default = $this->get_defaultsetting(); |
1920 | ||
1921 | if (!is_null($default)) { | |
1922 | if ((string)$default === $this->yes) { | |
587c7040 | 1923 | $defaultinfo = get_string('checkboxyes', 'admin'); |
220a90c5 | 1924 | } else { |
587c7040 | 1925 | $defaultinfo = get_string('checkboxno', 'admin'); |
220a90c5 | 1926 | } |
c8218a42 | 1927 | } else { |
587c7040 | 1928 | $defaultinfo = NULL; |
c8218a42 | 1929 | } |
220a90c5 | 1930 | |
1931 | if ((string)$data === $this->yes) { // convert to strings before comparison | |
1932 | $checked = 'checked="checked"'; | |
1933 | } else { | |
1934 | $checked = ''; | |
1935 | } | |
1936 | ||
1937 | return format_admin_setting($this, $this->visiblename, | |
9baf6825 | 1938 | '<div class="form-checkbox defaultsnext" ><input type="hidden" name="'.$this->get_full_name().'" value="'.s($this->no).'" /> ' |
1939 | .'<input type="checkbox" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($this->yes).'" '.$checked.' /></div>', | |
1940 | $this->description, true, '', $defaultinfo, $query); | |
6e4dc10f | 1941 | } |
6e4dc10f | 1942 | } |
1943 | ||
220a90c5 | 1944 | /** |
1945 | * Multiple checkboxes, each represents different value, stored in csv format | |
db26acd4 | 1946 | * |
1947 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 1948 | */ |
1949 | class admin_setting_configmulticheckbox extends admin_setting { | |
9baf6825 | 1950 | /** @var array Array of choices value=>label */ |
73fa96d5 | 1951 | public $choices; |
eef868d1 | 1952 | |
220a90c5 | 1953 | /** |
db26acd4 | 1954 | * Constructor: uses parent::__construct |
1955 | * | |
1a41e806 | 1956 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. |
220a90c5 | 1957 | * @param string $visiblename localised |
1958 | * @param string $description long localised info | |
1959 | * @param array $defaultsetting array of selected | |
1960 | * @param array $choices array of $value=>$label for each checkbox | |
1961 | */ | |
73fa96d5 | 1962 | public function __construct($name, $visiblename, $description, $defaultsetting, $choices) { |
6e4dc10f | 1963 | $this->choices = $choices; |
73fa96d5 | 1964 | parent::__construct($name, $visiblename, $description, $defaultsetting); |
6e4dc10f | 1965 | } |
1966 | ||
0a784551 | 1967 | /** |
73fa96d5 | 1968 | * This public function may be used in ancestors for lazy loading of choices |
db26acd4 | 1969 | * |
1970 | * @todo Check if this function is still required content commented out only returns true | |
1971 | * @return bool true if loaded, false if error | |
0a784551 | 1972 | */ |
73fa96d5 | 1973 | public function load_choices() { |
0a784551 | 1974 | /* |
220a90c5 | 1975 | if (is_array($this->choices)) { |
1976 | return true; | |
0a784551 | 1977 | } |
1978 | .... load choices here | |
1979 | */ | |
220a90c5 | 1980 | return true; |
1981 | } | |
1982 | ||
1983 | /** | |
1984 | * Is setting related to query text - used when searching | |
db26acd4 | 1985 | * |
220a90c5 | 1986 | * @param string $query |
db26acd4 | 1987 | * @return bool true on related, false on not or failure |
220a90c5 | 1988 | */ |
73fa96d5 | 1989 | public function is_related($query) { |
220a90c5 | 1990 | if (!$this->load_choices() or empty($this->choices)) { |
1991 | return false; | |
1992 | } | |
1993 | if (parent::is_related($query)) { | |
1994 | return true; | |
1995 | } | |
1996 | ||
1997 | $textlib = textlib_get_instance(); | |
1998 | foreach ($this->choices as $desc) { | |
1999 | if (strpos($textlib->strtolower($desc), $query) !== false) { | |
2000 | return true; | |
2001 | } | |
2002 | } | |
2003 | return false; | |
0a784551 | 2004 | } |
2005 | ||
db26acd4 | 2006 | /** |
2007 | * Returns the current setting if it is set | |
2008 | * | |
2009 | * @return mixed null if null, else an array | |
2010 | */ | |
73fa96d5 | 2011 | public function get_setting() { |
220a90c5 | 2012 | $result = $this->config_read($this->name); |
10f19c49 | 2013 | |
220a90c5 | 2014 | if (is_null($result)) { |
2015 | return NULL; | |
2016 | } | |
2017 | if ($result === '') { | |
2018 | return array(); | |
2019 | } | |
10f19c49 | 2020 | $enabled = explode(',', $result); |
2021 | $setting = array(); | |
2022 | foreach ($enabled as $option) { | |
2023 | $setting[$option] = 1; | |
2024 | } | |
2025 | return $setting; | |
6e4dc10f | 2026 | } |
eef868d1 | 2027 | |
db26acd4 | 2028 | /** |
2029 | * Saves the setting(s) provided in $data | |
2030 | * | |
2031 | * @param array $data An array of data, if not array returns empty str | |
2032 | * @return mixed empty string on useless data or bool true=success, false=failed | |
2033 | */ | |
73fa96d5 | 2034 | public function write_setting($data) { |
220a90c5 | 2035 | if (!is_array($data)) { |
2036 | return ''; // ignore it | |
2037 | } | |
2038 | if (!$this->load_choices() or empty($this->choices)) { | |
2039 | return ''; | |
2040 | } | |
2041 | unset($data['xxxxx']); | |
2042 | $result = array(); | |
2043 | foreach ($data as $key => $value) { | |
2044 | if ($value and array_key_exists($key, $this->choices)) { | |
2045 | $result[] = $key; | |
2046 | } | |
2047 | } | |
2048 | return $this->config_write($this->name, implode(',', $result)) ? '' : get_string('errorsetting', 'admin'); | |
6e4dc10f | 2049 | } |
0c079f19 | 2050 | |
db26acd4 | 2051 | /** |
2052 | * Returns XHTML field(s) as required by choices | |
2053 | * | |
2054 | * Relies on data being an array should data ever be another valid vartype with | |
2055 | * acceptable value this may cause a warning/error | |
2056 | * if (!is_array($data)) would fix the problem | |
2057 | * | |
2058 | * @todo Add vartype handling to ensure $data is an array | |
2059 | * | |
2060 | * @param array $data An array of checked values | |
2061 | * @param string $query | |
2062 | * @return string XHTML field | |
2063 | */ | |
73fa96d5 | 2064 | public function output_html($data, $query='') { |
220a90c5 | 2065 | if (!$this->load_choices() or empty($this->choices)) { |
2066 | return ''; | |
2067 | } | |
2068 | $default = $this->get_defaultsetting(); | |
2069 | if (is_null($default)) { | |
2070 | $default = array(); | |
2071 | } | |
2072 | if (is_null($data)) { | |
775f811a | 2073 | $data = array(); |
220a90c5 | 2074 | } |
220a90c5 | 2075 | $options = array(); |
2076 | $defaults = array(); | |
10f19c49 | 2077 | foreach ($this->choices as $key=>$description) { |
2078 | if (!empty($data[$key])) { | |
220a90c5 | 2079 | $checked = 'checked="checked"'; |
2080 | } else { | |
2081 | $checked = ''; | |
2082 | } | |
10f19c49 | 2083 | if (!empty($default[$key])) { |
220a90c5 | 2084 | $defaults[] = $description; |
2085 | } | |
2086 | ||
2087 | $options[] = '<input type="checkbox" id="'.$this->get_id().'_'.$key.'" name="'.$this->get_full_name().'['.$key.']" value="1" '.$checked.' />' | |
9baf6825 | 2088 | .'<label for="'.$this->get_id().'_'.$key.'">'.highlightfast($query, $description).'</label>'; |
220a90c5 | 2089 | } |
2090 | ||
587c7040 | 2091 | if (is_null($default)) { |
2092 | $defaultinfo = NULL; | |
2093 | } else if (!empty($defaults)) { | |
9baf6825 | 2094 | $defaultinfo = implode(', ', $defaults); |
2095 | } else { | |
2096 | $defaultinfo = get_string('none'); | |
2097 | } | |
220a90c5 | 2098 | |
2099 | $return = '<div class="form-multicheckbox">'; | |
2100 | $return .= '<input type="hidden" name="'.$this->get_full_name().'[xxxxx]" value="1" />'; // something must be submitted even if nothing selected | |
2101 | if ($options) { | |
2102 | $return .= '<ul>'; | |
2103 | foreach ($options as $option) { | |
2104 | $return .= '<li>'.$option.'</li>'; | |
2105 | } | |
2106 | $return .= '</ul>'; | |
6e4dc10f | 2107 | } |
587c7040 | 2108 | $return .= '</div>'; |
6153cf58 | 2109 | |
587c7040 | 2110 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', $defaultinfo, $query); |
c5d2d0dd | 2111 | |
6e4dc10f | 2112 | } |
6e4dc10f | 2113 | } |
2114 | ||
220a90c5 | 2115 | /** |
2116 | * Multiple checkboxes 2, value stored as string 00101011 | |
db26acd4 | 2117 | * |
2118 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2119 | */ |
2120 | class admin_setting_configmulticheckbox2 extends admin_setting_configmulticheckbox { | |
db26acd4 | 2121 | |
9baf6825 | 2122 | /** |
2123 | * Returns the setting if set | |
2124 | * | |
2125 | * @return mixed null if not set, else an array of set settings | |
2126 | */ | |
73fa96d5 | 2127 | public function get_setting() { |
220a90c5 | 2128 | $result = $this->config_read($this->name); |
2129 | if (is_null($result)) { | |
2130 | return NULL; | |
2131 | } | |
2132 | if (!$this->load_choices()) { | |
2133 | return NULL; | |
2134 | } | |
2135 | $result = str_pad($result, count($this->choices), '0'); | |
2136 | $result = preg_split('//', $result, -1, PREG_SPLIT_NO_EMPTY); | |
2137 | $setting = array(); | |
2138 | foreach ($this->choices as $key=>$unused) { | |
2139 | $value = array_shift($result); | |
2140 | if ($value) { | |
10f19c49 | 2141 | $setting[$key] = 1; |
220a90c5 | 2142 | } |
2143 | } | |
2144 | return $setting; | |
2145 | } | |
6e4dc10f | 2146 | |
db26acd4 | 2147 | /** |
2148 | * Save setting(s) provided in $data param | |
2149 | * | |
2150 | * @param array $data An array of settings to save | |
2151 | * @return mixed empty string for bad data or bool true=>success, false=>error | |
2152 | */ | |
73fa96d5 | 2153 | public function write_setting($data) { |
220a90c5 | 2154 | if (!is_array($data)) { |
2155 | return ''; // ignore it | |
2156 | } | |
2157 | if (!$this->load_choices() or empty($this->choices)) { | |
2158 | return ''; | |
2159 | } | |
2160 | $result = ''; | |
2161 | foreach ($this->choices as $key=>$unused) { | |
2162 | if (!empty($data[$key])) { | |
2163 | $result .= '1'; | |
2164 | } else { | |
2165 | $result .= '0'; | |
2166 | } | |
2167 | } | |
2168 | return $this->config_write($this->name, $result) ? '' : get_string('errorsetting', 'admin'); | |
2169 | } | |
2170 | } | |
2171 | ||
2172 | /** | |
2173 | * Select one value from list | |
db26acd4 | 2174 | * |
2175 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2176 | */ |
2177 | class admin_setting_configselect extends admin_setting { | |
9baf6825 | 2178 | /** @var array Array of choices value=>label */ |
73fa96d5 | 2179 | public $choices; |
6e4dc10f | 2180 | |
220a90c5 | 2181 | /** |
2182 | * Constructor | |
1a41e806 | 2183 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. |
220a90c5 | 2184 | * @param string $visiblename localised |
2185 | * @param string $description long localised info | |
2186 | * @param string $defaultsetting | |
2187 | * @param array $choices array of $value=>$label for each selection | |
2188 | */ | |
73fa96d5 | 2189 | public function __construct($name, $visiblename, $description, $defaultsetting, $choices) { |
220a90c5 | 2190 | $this->choices = $choices; |
73fa96d5 | 2191 | parent::__construct($name, $visiblename, $description, $defaultsetting); |
220a90c5 | 2192 | } |
2193 | ||
2194 | /** | |
2195 | * This function may be used in ancestors for lazy loading of choices | |
db26acd4 | 2196 | * |
0c079f19 | 2197 | * Override this method if loading of choices is expensive, such |
2198 | * as when it requires multiple db requests. | |
2199 | * | |
db26acd4 | 2200 | * @return bool true if loaded, false if error |
220a90c5 | 2201 | */ |
73fa96d5 | 2202 | public function load_choices() { |
220a90c5 | 2203 | /* |
2204 | if (is_array($this->choices)) { | |
2205 | return true; | |
6e4dc10f | 2206 | } |
220a90c5 | 2207 | .... load choices here |
2208 | */ | |
2209 | return true; | |
6e4dc10f | 2210 | } |
2211 | ||
db26acd4 | 2212 | /** |
2213 | * Check if this is $query is related to a choice | |
2214 | * | |
2215 | * @param string $query | |
2216 | * @return bool true if related, false if not | |
2217 | */ | |
73fa96d5 | 2218 | public function is_related($query) { |
407d8134 | 2219 | if (parent::is_related($query)) { |
2220 | return true; | |
2221 | } | |
2222 | if (!$this->load_choices()) { | |
2223 | return false; | |
2224 | } | |
2225 | $textlib = textlib_get_instance(); | |
2226 | foreach ($this->choices as $key=>$value) { | |
2227 | if (strpos($textlib->strtolower($key), $query) !== false) { | |
2228 | return true; | |
2229 | } | |
2230 | if (strpos($textlib->strtolower($value), $query) !== false) { | |
2231 | return true; | |
2232 | } | |
c5d2d0dd | 2233 | } |
407d8134 | 2234 | return false; |
2235 | } | |
2236 | ||
db26acd4 | 2237 | /** |
2238 | * Return the setting | |
0c079f19 | 2239 | * |
db26acd4 | 2240 | * @return mixed returns config if successfull else null |
2241 | */ | |
73fa96d5 | 2242 | public function get_setting() { |
220a90c5 | 2243 | return $this->config_read($this->name); |
6e4dc10f | 2244 | } |
eef868d1 | 2245 | |
db26acd4 | 2246 | /** |
2247 | * Save a setting | |
2248 | * | |
2249 | * @param string $data | |
2250 | * @return string empty of error string | |
2251 | */ | |
73fa96d5 | 2252 | public function write_setting($data) { |
220a90c5 | 2253 | if (!$this->load_choices() or empty($this->choices)) { |
2254 | return ''; | |
2255 | } | |
2256 | if (!array_key_exists($data, $this->choices)) { | |
2257 | return ''; // ignore it | |
2258 | } | |
eef868d1 | 2259 | |
220a90c5 | 2260 | return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f | 2261 | } |
eef868d1 | 2262 | |
e2249afe | 2263 | /** |
db26acd4 | 2264 | * Returns XHTML select field |
2265 | * | |
2266 | * Ensure the options are loaded, and generate the XHTML for the select | |
e2249afe | 2267 | * element and any warning message. Separating this out from output_html |
2268 | * makes it easier to subclass this class. | |
2269 | * | |
2270 | * @param string $data the option to show as selected. | |
2271 | * @param string $current the currently selected option in the database, null if none. | |
2272 | * @param string $default the default selected option. | |
2273 | * @return array the HTML for the select element, and a warning message. | |
2274 | */ | |
73fa96d5 | 2275 | public function output_select_html($data, $current, $default, $extraname = '') { |
220a90c5 | 2276 | if (!$this->load_choices() or empty($this->choices)) { |
e2249afe | 2277 | return array('', ''); |
6e4dc10f | 2278 | } |
220a90c5 | 2279 | |
9c305ba1 | 2280 | $warning = ''; |
2281 | if (is_null($current)) { | |
9baf6825 | 2282 | // first run |
9c305ba1 | 2283 | } else if (empty($current) and (array_key_exists('', $this->choices) or array_key_exists(0, $this->choices))) { |
2284 | // no warning | |
9baf6825 | 2285 | } else if (!array_key_exists($current, $this->choices)) { |
2286 | $warning = get_string('warningcurrentsetting', 'admin', s($current)); | |
2287 | if (!is_null($default) and $data == $current) { | |
2288 | $data = $default; // use default instead of first value when showing the form | |
2289 | } | |
2290 | } | |
9c305ba1 | 2291 | |
e2249afe | 2292 | $selecthtml = '<select id="'.$this->get_id().'" name="'.$this->get_full_name().$extraname.'">'; |
6e4dc10f | 2293 | foreach ($this->choices as $key => $value) { |
9baf6825 | 2294 | // the string cast is needed because key may be integer - 0 is equal to most strings! |
e2249afe | 2295 | $selecthtml .= '<option value="'.$key.'"'.((string)$key==$data ? ' selected="selected"' : '').'>'.$value.'</option>'; |
eef868d1 | 2296 | } |
e2249afe | 2297 | $selecthtml .= '</select>'; |
2298 | return array($selecthtml, $warning); | |
2299 | } | |
2300 | ||
db26acd4 | 2301 | /** |
2302 | * Returns XHTML select field and wrapping div(s) | |
2303 | * | |
2304 | * @see output_select_html() | |
0c079f19 | 2305 | * |
db26acd4 | 2306 | * @param string $data the option to show as selected |
2307 | * @param string $query | |
2308 | * @return string XHTML field and wrapping div | |
2309 | */ | |
73fa96d5 | 2310 | public function output_html($data, $query='') { |
e2249afe | 2311 | $default = $this->get_defaultsetting(); |
2312 | $current = $this->get_setting(); | |
2313 | ||
2314 | list($selecthtml, $warning) = $this->output_select_html($data, $current, $default); | |
2315 | if (!$selecthtml) { | |
2316 | return ''; | |
2317 | } | |
2318 | ||
2319 | if (!is_null($default) and array_key_exists($default, $this->choices)) { | |
2320 | $defaultinfo = $this->choices[$default]; | |
2321 | } else { | |
2322 | $defaultinfo = NULL; | |
2323 | } | |
2324 | ||
2325 | $return = '<div class="form-select defaultsnext">' . $selecthtml . '</div>'; | |
220a90c5 | 2326 | |
587c7040 | 2327 | return format_admin_setting($this, $this->visiblename, $return, $this->description, true, $warning, $defaultinfo, $query); |
6e4dc10f | 2328 | } |
6e4dc10f | 2329 | } |
2330 | ||
220a90c5 | 2331 | /** |
2332 | * Select multiple items from list | |
db26acd4 | 2333 | * |
2334 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2335 | */ |
6e4dc10f | 2336 | class admin_setting_configmultiselect extends admin_setting_configselect { |
9baf6825 | 2337 | /** |
2338 | * Constructor | |
2339 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. | |
2340 | * @param string $visiblename localised | |
2341 | * @param string $description long localised info | |
2342 | * @param array $defaultsetting array of selected items | |
2343 | * @param array $choices array of $value=>$label for each list item | |
2344 | */ | |
73fa96d5 | 2345 | public function __construct($name, $visiblename, $description, $defaultsetting, $choices) { |
2346 | parent::__construct($name, $visiblename, $description, $defaultsetting, $choices); | |
6e4dc10f | 2347 | } |
2348 | ||
db26acd4 | 2349 | /** |
2350 | * Returns the select setting(s) | |
2351 | * | |
2352 | * @return mixed null or array. Null if no settings else array of setting(s) | |
2353 | */ | |
73fa96d5 | 2354 | public function get_setting() { |
220a90c5 | 2355 | $result = $this->config_read($this->name); |
2356 | if (is_null($result)) { | |
d7933a55 | 2357 | return NULL; |
2358 | } | |
220a90c5 | 2359 | if ($result === '') { |
2360 | return array(); | |
2361 | } | |
2362 | return explode(',', $result); | |
6e4dc10f | 2363 | } |
eef868d1 | 2364 | |
db26acd4 | 2365 | /** |
2366 | * Saves setting(s) provided through $data | |
2367 | * | |
2368 | * Potential bug in the works should anyone call with this function | |
2369 | * using a vartype that is not an array | |
2370 | * | |
2371 | * @todo Add vartype handling to ensure $data is an array | |
2372 | * @param array $data | |
2373 | */ | |
73fa96d5 | 2374 | public function write_setting($data) { |
220a90c5 | 2375 | if (!is_array($data)) { |
2376 | return ''; //ignore it | |
2377 | } | |
2378 | if (!$this->load_choices() or empty($this->choices)) { | |
2379 | return ''; | |
2380 | } | |
2381 | ||
a7ad48fd | 2382 | unset($data['xxxxx']); |
2383 | ||
220a90c5 | 2384 | $save = array(); |
2385 | foreach ($data as $value) { | |
2386 | if (!array_key_exists($value, $this->choices)) { | |
2387 | continue; // ignore it | |
2388 | } | |
2389 | $save[] = $value; | |
2390 | } | |
2391 | ||
2392 | return ($this->config_write($this->name, implode(',', $save)) ? '' : get_string('errorsetting', 'admin')); | |
2393 | } | |
2394 | ||
2395 | /** | |
2396 | * Is setting related to query text - used when searching | |
db26acd4 | 2397 | * |
220a90c5 | 2398 | * @param string $query |
db26acd4 | 2399 | * @return bool true if related, false if not |
220a90c5 | 2400 | */ |
73fa96d5 | 2401 | public function is_related($query) { |
220a90c5 | 2402 | if (!$this->load_choices() or empty($this->choices)) { |
2403 | return false; | |
2404 | } | |
2405 | if (parent::is_related($query)) { | |
2406 | return true; | |
2407 | } | |
2408 | ||
2409 | $textlib = textlib_get_instance(); | |
2410 | foreach ($this->choices as $desc) { | |
2411 | if (strpos($textlib->strtolower($desc), $query) !== false) { | |
2412 | return true; | |
2413 | } | |
2414 | } | |
2415 | return false; | |
2416 | } | |
2417 | ||
db26acd4 | 2418 | /** |
2419 | * Returns XHTML multi-select field | |
2420 | * | |
2421 | * @todo Add vartype handling to ensure $data is an array | |
2422 | * @param array $data Array of values to select by default | |
2423 | * @param string $query | |
2424 | * @return string XHTML multi-select field | |
2425 | */ | |
73fa96d5 | 2426 | public function output_html($data, $query='') { |
220a90c5 | 2427 | if (!$this->load_choices() or empty($this->choices)) { |
2428 | return ''; | |
2429 | } | |
2430 | $choices = $this->choices; | |
2431 | $default = $this->get_defaultsetting(); | |
2432 | if (is_null($default)) { | |
2433 | $default = array(); | |
2434 | } | |
2435 | if (is_null($data)) { | |
d7933a55 | 2436 | $data = array(); |
2437 | } | |
220a90c5 | 2438 | |
2439 | $defaults = array(); | |
4413941f | 2440 | $size = min(10, count($this->choices)); |
a7ad48fd | 2441 | $return = '<div class="form-select"><input type="hidden" name="'.$this->get_full_name().'[xxxxx]" value="1" />'; // something must be submitted even if nothing selected |
4413941f | 2442 | $return .= '<select id="'.$this->get_id().'" name="'.$this->get_full_name().'[]" size="'.$size.'" multiple="multiple">'; |
220a90c5 | 2443 | foreach ($this->choices as $key => $description) { |
2444 | if (in_array($key, $data)) { | |
2445 | $selected = 'selected="selected"'; | |
2446 | } else { | |
2447 | $selected = ''; | |
2448 | } | |
2449 | if (in_array($key, $default)) { | |
2450 | $defaults[] = $description; | |
6e4dc10f | 2451 | } |
220a90c5 | 2452 | |
2453 | $return .= '<option value="'.s($key).'" '.$selected.'>'.$description.'</option>'; | |
2454 | } | |
2455 | ||
587c7040 | 2456 | if (is_null($default)) { |
2457 | $defaultinfo = NULL; | |
2458 | } if (!empty($defaults)) { | |
2459 | $defaultinfo = implode(', ', $defaults); | |
220a90c5 | 2460 | } else { |
587c7040 | 2461 | $defaultinfo = get_string('none'); |
6e4dc10f | 2462 | } |
eef868d1 | 2463 | |
587c7040 | 2464 | $return .= '</select></div>'; |
2465 | return format_admin_setting($this, $this->visiblename, $return, $this->description, true, '', $defaultinfo, $query); | |
6e4dc10f | 2466 | } |
220a90c5 | 2467 | } |
eef868d1 | 2468 | |
220a90c5 | 2469 | /** |
2470 | * Time selector | |
db26acd4 | 2471 | * |
2472 | * This is a liiitle bit messy. we're using two selects, but we're returning | |
220a90c5 | 2473 | * them as an array named after $name (so we only use $name2 internally for the setting) |
db26acd4 | 2474 | * |
2475 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2476 | */ |
2477 | class admin_setting_configtime extends admin_setting { | |
9baf6825 | 2478 | /** @var string Used for setting second select (minutes) */ |
73fa96d5 | 2479 | public $name2; |
220a90c5 | 2480 | |
2481 | /** | |
2482 | * Constructor | |
2483 | * @param string $hoursname setting for hours | |
2484 | * @param string $minutesname setting for hours | |
2485 | * @param string $visiblename localised | |
2486 | * @param string $description long localised info | |
2487 | * @param array $defaultsetting array representing default time 'h'=>hours, 'm'=>minutes | |
2488 | */ | |
73fa96d5 | 2489 | public function __construct($hoursname, $minutesname, $visiblename, $description, $defaultsetting) { |
220a90c5 | 2490 | $this->name2 = $minutesname; |
73fa96d5 | 2491 | parent::__construct($hoursname, $visiblename, $description, $defaultsetting); |
220a90c5 | 2492 | } |
2493 | ||
db26acd4 | 2494 | /** |
2495 | * Get the selected time | |
0c079f19 | 2496 | * |
db26acd4 | 2497 | * @return mixed An array containing 'h'=>xx, 'm'=>xx, or null if not set |
2498 | */ | |
73fa96d5 | 2499 | public function get_setting() { |
220a90c5 | 2500 | $result1 = $this->config_read($this->name); |
2501 | $result2 = $this->config_read($this->name2); | |
2502 | if (is_null($result1) or is_null($result2)) { | |
2503 | return NULL; | |
2504 | } | |
2505 | ||
2506 | return array('h' => $result1, 'm' => $result2); | |
2507 | } | |
2508 | ||
db26acd4 | 2509 | /** |
2510 | * Store the time (hours and minutes) | |
0c079f19 | 2511 | * |
db26acd4 | 2512 | * @param array $data Must be form 'h'=>xx, 'm'=>xx |
2513 | * @return bool true if success, false if not | |
2514 | */ | |
73fa96d5 | 2515 | public function write_setting($data) { |
220a90c5 | 2516 | if (!is_array($data)) { |
2517 | return ''; | |
2518 | } | |
2519 | ||
2520 | $result = $this->config_write($this->name, (int)$data['h']) && $this->config_write($this->name2, (int)$data['m']); | |
2521 | return ($result ? '' : get_string('errorsetting', 'admin')); | |
2522 | } | |
2523 | ||
db26acd4 | 2524 | /** |
2525 | * Returns XHTML time select fields | |
2526 | * | |
2527 | * @param array $data Must be form 'h'=>xx, 'm'=>xx | |
2528 | * @param string $query | |
2529 | * @return string XHTML time select fields and wrapping div(s) | |
2530 | */ | |
73fa96d5 | 2531 | public function output_html($data, $query='') { |
220a90c5 | 2532 | $default = $this->get_defaultsetting(); |
2533 | ||
2534 | if (is_array($default)) { | |
587c7040 | 2535 | $defaultinfo = $default['h'].':'.$default['m']; |
cc73de71 | 2536 | } else { |
587c7040 | 2537 | $defaultinfo = NULL; |
6e4dc10f | 2538 | } |
220a90c5 | 2539 | |
587c7040 | 2540 | $return = '<div class="form-time defaultsnext">'. |
9baf6825 | 2541 | '<select id="'.$this->get_id().'h" name="'.$this->get_full_name().'[h]">'; |
220a90c5 | 2542 | for ($i = 0; $i < 24; $i++) { |
2543 | $return .= '<option value="'.$i.'"'.($i == $data['h'] ? ' selected="selected"' : '').'>'.$i.'</option>'; | |
6e4dc10f | 2544 | } |
220a90c5 | 2545 | $return .= '</select>:<select id="'.$this->get_id().'m" name="'.$this->get_full_name().'[m]">'; |
2546 | for ($i = 0; $i < 60; $i += 5) { | |
2547 | $return .= '<option value="'.$i.'"'.($i == $data['m'] ? ' selected="selected"' : '').'>'.$i.'</option>'; | |
2548 | } | |
587c7040 | 2549 | $return .= '</select></div>'; |
2550 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', $defaultinfo, $query); | |
6e4dc10f | 2551 | } |
2552 | ||
2553 | } | |
2554 | ||
db26acd4 | 2555 | /** |
2556 | * Used to validate a textarea used for ip addresses | |
2557 | * | |
2558 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
db26acd4 | 2559 | */ |
4e639121 | 2560 | class admin_setting_configiplist extends admin_setting_configtextarea { |
db26acd4 | 2561 | |
9baf6825 | 2562 | /** |
2563 | * Validate the contents of the textarea as IP addresses | |
2564 | * | |
2565 | * Used to validate a new line seperated list of IP addresses collected from | |
2566 | * a textarea control | |
2567 | * | |
2568 | * @param string $data A list of IP Addresses seperated by new lines | |
2569 | * @return mixed bool true for success or string:error on failure | |
2570 | */ | |
73fa96d5 | 2571 | public function validate($data) { |
4e639121 | 2572 | if(!empty($data)) { |
c5d2d0dd | 2573 | $ips = explode("\n", $data); |
4e639121 | 2574 | } else { |
2575 | return true; | |
2576 | } | |
2577 | $result = true; | |
2578 | foreach($ips as $ip) { | |
2579 | $ip = trim($ip); | |
2580 | if(preg_match('#^(\d{1,3})(\.\d{1,3}){0,3}$#', $ip, $match) || | |
9baf6825 | 2581 | preg_match('#^(\d{1,3})(\.\d{1,3}){0,3}(\/\d{1,2})$#', $ip, $match) || |
2582 | preg_match('#^(\d{1,3})(\.\d{1,3}){3}(-\d{1,3})$#', $ip, $match)) { | |
4e639121 | 2583 | $result = true; |
2584 | } else { | |
2585 | $result = false; | |
2586 | break; | |
2587 | } | |
2588 | } | |
9baf6825 | 2589 | if($result) { |
4e639121 | 2590 | return true; |
2591 | } else { | |
2592 | return get_string('validateerror', 'admin'); | |
2593 | } | |
2594 | } | |
2595 | } | |
2596 | ||
4413941f | 2597 | /** |
db26acd4 | 2598 | * An admin setting for selecting one or more users who have a capability |
2599 | * in the system context | |
2600 | * | |
4413941f | 2601 | * An admin setting for selecting one or more users, who have a particular capability |
2602 | * in the system context. Warning, make sure the list will never be too long. There is | |
2603 | * no paging or searching of this list. | |
2604 | * | |
2605 | * To correctly get a list of users from this config setting, you need to call the | |
2606 | * get_users_from_config($CFG->mysetting, $capability); function in moodlelib.php. | |
db26acd4 | 2607 | * |
2608 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
4413941f | 2609 | */ |
2610 | class admin_setting_users_with_capability extends admin_setting_configmultiselect { | |
adf176d7 | 2611 | /** @var string The capabilities name */ |
4413941f | 2612 | protected $capability; |
adf176d7 PS |
2613 | /** @var int include admin users too */ |
2614 | protected $includeadmins; | |
4413941f | 2615 | |
2616 | /** | |
2617 | * Constructor. | |
2618 | * | |
2619 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. | |
2620 | * @param string $visiblename localised name | |
2621 | * @param string $description localised long description | |
2622 | * @param array $defaultsetting array of usernames | |
2623 | * @param string $capability string capability name. | |
adf176d7 | 2624 | * @param bool $include admins, include aadministrators |
4413941f | 2625 | */ |
adf176d7 PS |
2626 | function __construct($name, $visiblename, $description, $defaultsetting, $capability, $includeadmins = true) { |
2627 | $this->capability = $capability; | |
2628 | $this->includeadmins = $includeadmins; | |
fb073f60 | 2629 | parent::__construct($name, $visiblename, $description, $defaultsetting, NULL); |
2630 | } | |
2631 | ||
db26acd4 | 2632 | /** |
2633 | * Load all of the uses who have the capability into choice array | |
2634 | * | |
2635 | * @return bool Always returns true | |
2636 | */ | |
fb073f60 | 2637 | function load_choices() { |
2638 | if (is_array($this->choices)) { | |
2639 | return true; | |
2640 | } | |
4413941f | 2641 | $users = get_users_by_capability(get_context_instance(CONTEXT_SYSTEM), |
9baf6825 | 2642 | $this->capability, 'u.id,u.username,u.firstname,u.lastname', 'u.lastname,u.firstname'); |
fb073f60 | 2643 | $this->choices = array( |
4413941f | 2644 | '$@NONE@$' => get_string('nobody'), |
fb073f60 | 2645 | '$@ALL@$' => get_string('everyonewhocan', 'admin', get_capability_string($this->capability)), |
4413941f | 2646 | ); |
adf176d7 PS |
2647 | if ($this->includeadmins) { |
2648 | $admins = get_admins(); | |
2649 | foreach ($admins as $user) { | |
2650 | $this->choices[$user->id] = fullname($user); | |
2651 | } | |
2652 | } | |
4413941f | 2653 | foreach ($users as $user) { |
adf176d7 | 2654 | $this->choices[$user->id] = fullname($user); |
4413941f | 2655 | } |
fb073f60 | 2656 | return true; |
4413941f | 2657 | } |
2658 | ||
db26acd4 | 2659 | /** |
2660 | * Returns the default setting for class | |
2661 | * | |
2662 | * @return mixed Array, or string. Empty string if no default | |
2663 | */ | |
73fa96d5 | 2664 | public function get_defaultsetting() { |
4413941f | 2665 | $this->load_choices(); |
cd3acbf2 | 2666 | $defaultsetting = parent::get_defaultsetting(); |
2667 | if (empty($defaultsetting)) { | |
4413941f | 2668 | return array('$@NONE@$'); |
cd3acbf2 | 2669 | } else if (array_key_exists($defaultsetting, $this->choices)) { |
9baf6825 | 2670 | return $defaultsetting; |
2671 | } else { | |
2672 | return ''; | |
2673 | } | |
4413941f | 2674 | } |
2675 | ||
db26acd4 | 2676 | /** |
2677 | * Returns the current setting | |
2678 | * | |
2679 | * @return mixed array or string | |
2680 | */ | |
73fa96d5 | 2681 | public function get_setting() { |
4413941f | 2682 | $result = parent::get_setting(); |
adf176d7 PS |
2683 | if ($result === null) { |
2684 | // this is necessary for settings upgrade | |
2685 | return null; | |
2686 | } | |
4413941f | 2687 | if (empty($result)) { |
2688 | $result = array('$@NONE@$'); | |
2689 | } | |
2690 | return $result; | |
2691 | } | |
2692 | ||
db26acd4 | 2693 | /** |
2694 | * Save the chosen setting provided as $data | |
2695 | * | |
2696 | * @param array $data | |
2697 | * @return mixed string or array | |
2698 | */ | |
73fa96d5 | 2699 | public function write_setting($data) { |
9baf6825 | 2700 | // If all is selected, remove any explicit options. |
4413941f | 2701 | if (in_array('$@ALL@$', $data)) { |
2702 | $data = array('$@ALL@$'); | |
2703 | } | |
2704 | // None never needs to be writted to the DB. | |
2705 | if (in_array('$@NONE@$', $data)) { | |
2706 | unset($data[array_search('$@NONE@$', $data)]); | |
2707 | } | |
2708 | return parent::write_setting($data); | |
2709 | } | |
2710 | } | |
2711 | ||
220a90c5 | 2712 | /** |
2713 | * Special checkbox for calendar - resets SESSION vars. | |
db26acd4 | 2714 | * |
2715 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2716 | */ |
6e4dc10f | 2717 | class admin_setting_special_adminseesall extends admin_setting_configcheckbox { |
9baf6825 | 2718 | /** |
2719 | * Calls the parent::__construct with default values | |
2720 | * | |
2721 | * name => calendar_adminseesall | |
2722 | * visiblename => get_string('adminseesall', 'admin') | |
2723 | * description => get_string('helpadminseesall', 'admin') | |
2724 | * defaultsetting => 0 | |
2725 | */ | |
73fa96d5 | 2726 | public function __construct() { |
2727 | parent::__construct('calendar_adminseesall', get_string('adminseesall', 'admin'), | |
9baf6825 | 2728 | get_string('helpadminseesall', 'admin'), '0'); |
6e4dc10f | 2729 | } |
2730 | ||
db26acd4 | 2731 | /** |
2732 | * Stores the setting passed in $data | |
2733 | * | |
db26acd4 | 2734 | * @param mixed gets converted to string for comparison |
2735 | * @return string empty string or error message | |
2736 | */ | |
73fa96d5 | 2737 | public function write_setting($data) { |
6e4dc10f | 2738 | global $SESSION; |
2739 | unset($SESSION->cal_courses_shown); | |
220a90c5 | 2740 | return parent::write_setting($data); |
6e4dc10f | 2741 | } |
2742 | } | |
2743 | ||
392e7363 | 2744 | /** |
2745 | * Special select for settings that are altered in setup.php and can not be altered on the fly | |
db26acd4 | 2746 | * |
2747 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
392e7363 | 2748 | */ |
2749 | class admin_setting_special_selectsetup extends admin_setting_configselect { | |
9baf6825 | 2750 | /** |
2751 | * Reads the setting directly from the database | |
2752 | * | |
2753 | * @return mixed | |
2754 | */ | |
73fa96d5 | 2755 | public function get_setting() { |
9baf6825 | 2756 | // read directly from db! |
392e7363 | 2757 | return get_config(NULL, $this->name); |
2758 | } | |
2759 | ||
db26acd4 | 2760 | /** |
2761 | * Save the setting passed in $data | |
2762 | * | |
db26acd4 | 2763 | * @param string $data The setting to save |
2764 | * @return string empty or error message | |
2765 | */ | |
73fa96d5 | 2766 | public function write_setting($data) { |
392e7363 | 2767 | global $CFG; |
2768 | // do not change active CFG setting! | |
2769 | $current = $CFG->{$this->name}; | |
2770 | $result = parent::write_setting($data); | |
2771 | $CFG->{$this->name} = $current; | |
2772 | return $result; | |
2773 | } | |
2774 | } | |
2775 | ||
220a90c5 | 2776 | /** |
2777 | * Special select for frontpage - stores data in course table | |
db26acd4 | 2778 | * |
2779 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2780 | */ |
6e4dc10f | 2781 | class admin_setting_sitesetselect extends admin_setting_configselect { |
9baf6825 | 2782 | /** |
2783 | * Returns the site name for the selected site | |
2784 | * | |
2785 | * @see get_site() | |
2786 | * @return string The site name of the selected site | |
2787 | */ | |
73fa96d5 | 2788 | public function get_setting() { |
b2bf016e | 2789 | $site = get_site(); |
2790 | return $site->{$this->name}; | |
6e4dc10f | 2791 | } |
db26acd4 | 2792 | /** |
2793 | * Updates the database and save the setting | |
2794 | * | |
db26acd4 | 2795 | * @param string data |
2796 | * @return string empty or error message | |
2797 | */ | |
73fa96d5 | 2798 | public function write_setting($data) { |
b2bf016e | 2799 | global $DB, $SITE; |
6e4dc10f | 2800 | if (!in_array($data, array_keys($this->choices))) { |
220a90c5 | 2801 | return get_string('errorsetting', 'admin'); |
6e4dc10f | 2802 | } |
2803 | $record = new stdClass(); | |
220a90c5 | 2804 | $record->id = SITEID; |
2805 | $temp = $this->name; | |
2806 | $record->$temp = $data; | |
6e4dc10f | 2807 | $record->timemodified = time(); |
b2bf016e | 2808 | // update $SITE |
2809 | $SITE->{$this->name} = $data; | |
f33e1ed4 | 2810 | return ($DB->update_record('course', $record) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f | 2811 | } |
6e4dc10f | 2812 | } |
2813 | ||
ca497f4f | 2814 | /** |
2815 | * Select for blog's bloglevel setting: if set to 0, will set blog_menu | |
2816 | * block to hidden. | |
2817 | * | |
2818 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2819 | */ | |
2820 | class admin_setting_bloglevel extends admin_setting_configselect { | |
2821 | /** | |
2822 | * Updates the database and save the setting | |
2823 | * | |
2824 | * @param string data | |
2825 | * @return string empty or error message | |
2826 | */ | |
2827 | public function write_setting($data) { | |
2828 | global $DB; | |
2829 | if ($data['bloglevel'] == 0) { | |
2830 | $DB->set_field('block', 'visible', 0, array('name' => 'blog_menu')); | |
2831 | } else { | |
2832 | $DB->set_field('block', 'visible', 1, array('name' => 'blog_menu')); | |
2833 | } | |
2834 | return parent::write_setting($data); | |
2835 | } | |
2836 | } | |
2837 | ||
220a90c5 | 2838 | /** |
2839 | * Special select - lists on the frontpage - hacky | |
db26acd4 | 2840 | * |
2841 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2842 | */ |
2843 | class admin_setting_courselist_frontpage extends admin_setting { | |
9baf6825 | 2844 | /** @var array Array of choices value=>label */ |
73fa96d5 | 2845 | public $choices; |
6e4dc10f | 2846 | |
db26acd4 | 2847 | /** |
2848 | * Construct override, requires one param | |
2849 | * | |
db26acd4 | 2850 | * @param bool $loggedin Is the user logged in |
2851 | */ | |
73fa96d5 | 2852 | public function __construct($loggedin) { |
6e4dc10f | 2853 | global $CFG; |
220a90c5 | 2854 | require_once($CFG->dirroot.'/course/lib.php'); |
2855 | $name = 'frontpage'.($loggedin ? 'loggedin' : ''); | |
2856 | $visiblename = get_string('frontpage'.($loggedin ? 'loggedin' : ''),'admin'); | |
2857 | $description = get_string('configfrontpage'.($loggedin ? 'loggedin' : ''),'admin'); | |
2858 | $defaults = array(FRONTPAGECOURSELIST); | |
73fa96d5 | 2859 | parent::__construct($name, $visiblename, $description, $defaults); |
6e4dc10f | 2860 | } |
eef868d1 | 2861 | |
db26acd4 | 2862 | /** |
2863 | * Loads the choices available | |
2864 | * | |
db26acd4 | 2865 | * @return bool always returns true |
2866 | */ | |
73fa96d5 | 2867 | public function load_choices() { |
c7da4357 | 2868 | global $DB; |
220a90c5 | 2869 | if (is_array($this->choices)) { |
2870 | return true; | |
2871 | } | |
2872 | $this->choices = array(FRONTPAGENEWS => get_string('frontpagenews'), | |
9baf6825 | 2873 | FRONTPAGECOURSELIST => get_string('frontpagecourselist'), |
2874 | FRONTPAGECATEGORYNAMES => get_string('frontpagecategorynames'), | |
2875 | FRONTPAGECATEGORYCOMBO => get_string('frontpagecategorycombo'), | |
2876 | 'none' => get_string('none')); | |
c7da4357 | 2877 | if ($this->name == 'frontpage' and $DB->count_records('course') > FRONTPAGECOURSELIMIT) { |
220a90c5 | 2878 | unset($this->choices[FRONTPAGECOURSELIST]); |
2879 | } | |
2880 | return true; | |
2881 | } | |
db26acd4 | 2882 | /** |
2883 | * Returns the selected settings | |
2884 | * | |
2885 | * @param mixed array or setting or null | |
2886 | */ | |
73fa96d5 | 2887 | public function get_setting() { |
220a90c5 | 2888 | $result = $this->config_read($this->name); |
2889 | if (is_null($result)) { | |
2890 | return NULL; | |
2891 | } | |
2892 | if ($result === '') { | |
2893 | return array(); | |
2894 | } | |
2895 | return explode(',', $result); | |
6e4dc10f | 2896 | } |
eef868d1 | 2897 | |
db26acd4 | 2898 | /** |
2899 | * Save the selected options | |
2900 | * | |
2901 | * @param array $data | |
2902 | * @return mixed empty string (data is not an array) or bool true=success false=failure | |
2903 | */ | |
73fa96d5 | 2904 | public function write_setting($data) { |
220a90c5 | 2905 | if (!is_array($data)) { |
2906 | return ''; | |
6e4dc10f | 2907 | } |
220a90c5 | 2908 | $this->load_choices(); |
2909 | $save = array(); | |
6e4dc10f | 2910 | foreach($data as $datum) { |
220a90c5 | 2911 | if ($datum == 'none' or !array_key_exists($datum, $this->choices)) { |
2912 | continue; | |
6e4dc10f | 2913 | } |
220a90c5 | 2914 | $save[$datum] = $datum; // no duplicates |
6e4dc10f | 2915 | } |
220a90c5 | 2916 | return ($this->config_write($this->name, implode(',', $save)) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f | 2917 | } |
eef868d1 | 2918 | |
db26acd4 | 2919 | /** |
2920 | * Return XHTML select field and wrapping div | |
2921 | * | |
2922 | * @todo Add vartype handling to make sure $data is an array | |
2923 | * @param array $data Array of elements to select by default | |
2924 | * @return string XHTML select field and wrapping div | |
2925 | */ | |
73fa96d5 | 2926 | public function output_html($data, $query='') { |
220a90c5 | 2927 | $this->load_choices(); |
2928 | $currentsetting = array(); | |
2929 | foreach ($data as $key) { | |
2930 | if ($key != 'none' and array_key_exists($key, $this->choices)) { | |
2931 | $currentsetting[] = $key; // already selected first | |
6e4dc10f | 2932 | } |
2933 | } | |
220a90c5 | 2934 | |
0a7e84c3 | 2935 | $return = '<div class="form-group">'; |
6e4dc10f | 2936 | for ($i = 0; $i < count($this->choices) - 1; $i++) { |
220a90c5 | 2937 | if (!array_key_exists($i, $currentsetting)) { |
2938 | $currentsetting[$i] = 'none'; //none | |
2939 | } | |
2940 | $return .='<select class="form-select" id="'.$this->get_id().$i.'" name="'.$this->get_full_name().'[]">'; | |
6e4dc10f | 2941 | foreach ($this->choices as $key => $value) { |
220a90c5 | 2942 | $return .= '<option value="'.$key.'"'.("$key" == $currentsetting[$i] ? ' selected="selected"' : '').'>'.$value.'</option>'; |
6e4dc10f | 2943 | } |
2944 | $return .= '</select>'; | |
2945 | if ($i !== count($this->choices) - 2) { | |
975211bb | 2946 | $return .= '<br />'; |
6e4dc10f | 2947 | } |
2948 | } | |
0a7e84c3 | 2949 | $return .= '</div>'; |
2950 | ||
587c7040 | 2951 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', NULL, $query); |
6e4dc10f | 2952 | } |
2953 | } | |
2954 | ||
220a90c5 | 2955 | /** |
2956 | * Special checkbox for frontpage - stores data in course table | |
db26acd4 | 2957 | * |
2958 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2959 | */ |
6e4dc10f | 2960 | class admin_setting_sitesetcheckbox extends admin_setting_configcheckbox { |
9baf6825 | 2961 | /** |
2962 | * Returns the current sites name | |
2963 | * | |
2964 | * @return string | |
2965 | */ | |
73fa96d5 | 2966 | public function get_setting() { |
b2bf016e | 2967 | $site = get_site(); |
2968 | return $site->{$this->name}; | |
6e4dc10f | 2969 | } |
eef868d1 | 2970 | |
db26acd4 | 2971 | /** |
2972 | * Save the selected setting | |
2973 | * | |
db26acd4 | 2974 | * @param string $data The selected site |
2975 | * @return string empty string or error message | |
2976 | */ | |
73fa96d5 | 2977 | public function write_setting($data) { |
b2bf016e | 2978 | global $DB, $SITE; |
220a90c5 | 2979 | $record = new object(); |
2980 | $record->id = SITEID; | |
2981 | $record->{$this->name} = ($data == '1' ? 1 : 0); | |
2982 | $record->timemodified = time(); | |
b2bf016e | 2983 | // update $SITE |
2984 | $SITE->{$this->name} = $data; | |
f33e1ed4 | 2985 | return ($DB->update_record('course', $record) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f | 2986 | } |
6e4dc10f | 2987 | } |
2988 | ||
220a90c5 | 2989 | /** |
2990 | * Special text for frontpage - stores data in course table. | |
2991 | * Empty string means not set here. Manual setting is required. | |
db26acd4 | 2992 | * |
2993 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2994 | */ |
6e4dc10f | 2995 | class admin_setting_sitesettext extends admin_setting_configtext { |
9baf6825 | 2996 | /** |
2997 | * Return the current setting | |
2998 | * | |
2999 | * @return mixed string or null | |
3000 | */ | |
73fa96d5 | 3001 | public function get_setting() { |
b2bf016e | 3002 | $site = get_site(); |
a7747679 | 3003 | return $site->{$this->name} != '' ? $site->{$this->name} : NULL; |
6e4dc10f | 3004 | } |
90cfbd0a | 3005 | |
db26acd4 | 3006 | /** |
3007 | * Validate the selected data | |
3008 | * | |
3009 | * @param string $data The selected value to validate | |
3010 | * @return mixed true or message string | |
3011 | */ | |
73fa96d5 | 3012 | public function validate($data) { |
294ce987 | 3013 | $cleaned = clean_param($data, PARAM_MULTILANG); |
e33fbf87 | 3014 | if ($cleaned === '') { |
3015 | return get_string('required'); | |
3016 | } | |
3017 | if ("$data" == "$cleaned") { // implicit conversion to string is needed to do exact comparison | |
3018 | return true; | |
3019 | } else { | |
3020 | return get_string('validateerror', 'admin'); | |
b89639f9 | 3021 | } |
b89639f9 | 3022 | } |
3023 | ||
db26acd4 | 3024 | /** |
3025 | * Save the selected setting | |
3026 | * | |
db26acd4 | 3027 | * @param string $data The selected value |
3028 | * @return string emtpy or error message | |
3029 | */ | |
73fa96d5 | 3030 | public function write_setting($data) { |
b2bf016e | 3031 | global $DB, $SITE; |
b89639f9 | 3032 | $data = trim($data); |
c5d2d0dd | 3033 | $validated = $this->validate($data); |
e33fbf87 | 3034 | if ($validated !== true) { |
3035 | return $validated; | |
90cfbd0a | 3036 | } |
eef868d1 | 3037 | |
220a90c5 | 3038 | $record = new object(); |
3039 | $record->id = SITEID; | |
f33e1ed4 | 3040 | $record->{$this->name} = $data; |
220a90c5 | 3041 | $record->timemodified = time(); |
b2bf016e | 3042 | // update $SITE |
3043 | $SITE->{$this->name} = $data; | |
f33e1ed4 | 3044 | return ($DB->update_record('course', $record) ? '' : get_string('dbupdatefailed', 'error')); |
6e4dc10f | 3045 | } |
6e4dc10f | 3046 | } |
3047 | ||
220a90c5 | 3048 | /** |
3049 | * Special text editor for site description. | |
db26acd4 | 3050 | * |
3051 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 3052 | */ |
6e4dc10f | 3053 | class admin_setting_special_frontpagedesc extends admin_setting { |
9baf6825 | 3054 | /** |
3055 | * Calls parent::__construct with specific arguments | |
3056 | */ | |
73fa96d5 | 3057 | public function __construct() { |
3058 | parent::__construct('summary', get_string('frontpagedescription'), get_string('frontpagedescriptionhelp'), NULL); | |
ff5fe311 | 3059 | editors_head_setup(); |
6e4dc10f | 3060 | } |
eef868d1 | 3061 | |
db26acd4 | 3062 | /** |
3063 | * Return the current setting | |
3064 | * @return string The current setting | |
3065 | */ | |
73fa96d5 | 3066 | public function get_setting() { |
b2bf016e | 3067 | $site = get_site(); |
3068 | return $site->{$this->name}; | |
c626c2f4 | 3069 | } |
eef868d1 | 3070 | |
db26acd4 | 3071 | /** |
3072 | * Save the new setting | |
3073 | * | |
db26acd4 | 3074 | * @param string $data The new value to save |
3075 | * @return string empty or error message | |
3076 | */ | |
73fa96d5 | 3077 | public function write_setting($data) { |
b2bf016e | 3078 | global $DB, $SITE; |
c626c2f4 | 3079 | $record = new object(); |
220a90c5 | 3080 | $record->id = SITEID; |
f33e1ed4 | 3081 | $record->{$this->name} = $data; |
c626c2f4 | 3082 | $record->timemodified = time(); |
b2bf016e | 3083 | $SITE->{$this->name} = $data; |
7719860b | 3084 | return ($DB->update_record('course', $record) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f | 3085 | } |
3086 | ||
db26acd4 | 3087 | /** |
3088 | * Returns XHTML for the field plus wrapping div | |
3089 | * | |
db26acd4 | 3090 | * @param string $data The current value |
3091 | * @param string $query | |
3092 | * @return string The XHTML output | |
3093 | */ | |
73fa96d5 | 3094 | public function output_html($data, $query='') { |
cb6c02c4 | 3095 | global $CFG; |
6e4dc10f | 3096 | |
220a90c5 | 3097 | $CFG->adminusehtmleditor = can_use_html_editor(); |
8c37106d | 3098 | $return = '<div class="form-htmlarea">'.print_textarea($CFG->adminusehtmleditor, 15, 60, 0, 0, $this->get_full_name(), $data, 0, true, 'summary') .'</div>'; |
220a90c5 | 3099 | |
587c7040 | 3100 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', NULL, $query); |
220a90c5 | 3101 | } |
3102 | } | |
6e4dc10f | 3103 | |
db26acd4 | 3104 | /** |
3105 | * Special font selector for use in admin section | |
3106 | * | |
3107 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
db26acd4 | 3108 | */ |
6e4dc10f | 3109 | class admin_setting_special_editorfontlist extends admin_setting { |
3110 | ||
9baf6825 | 3111 | /** |
3112 | * Construct method, calls parent::__construct with specific args | |
3113 | */ | |
73fa96d5 | 3114 | public function __construct() { |
6e4dc10f | 3115 | global $CFG; |
3116 | $name = 'editorfontlist'; | |
3117 | $visiblename = get_string('editorfontlist', 'admin'); | |
3118 | $description = get_string('configeditorfontlist', 'admin'); | |
6e4dc10f | 3119 | $defaults = array('k0' => 'Trebuchet', |
9baf6825 | 3120 | 'v0' => 'Trebuchet MS,Verdana,Arial,Helvetica,sans-serif', |
3121 | 'k1' => 'Arial', | |
3122 | 'v1' => 'arial,helvetica,sans-serif', | |
3123 | 'k2' => 'Courier New', | |
3124 | 'v2' => 'courier new,courier,monospace', | |
3125 | 'k3' => 'Georgia', | |
3126 | 'v3' => 'georgia,times new roman,times,serif', | |
3127 | 'k4' => 'Tahoma', | |
3128 | 'v4' => 'tahoma,arial,helvetica,sans-serif', | |
3129 | 'k5' => 'Times New Roman', | |
3130 | 'v5' => 'times new roman,times,serif', | |
3131 | 'k6' => 'Verdana', | |
3132 | 'v6' => 'verdana,arial,helvetica,sans-serif', | |
3133 | 'k7' => 'Impact', | |
3134 | 'v7' => 'impact', | |
3135 | 'k8' => 'Wingdings', | |
3136 | 'v8' => 'wingdings'); | |
73fa96d5 | 3137 | parent::__construct($name, $visiblename, $description, $defaults); |
6e4dc10f | 3138 | } |
eef868d1 | 3139 | |
db26acd4 | 3140 | /** |
3141 | * Return the current setting | |
0c079f19 | 3142 | * |
db26acd4 | 3143 | * @return array Array of the current setting(s) |
3144 | */ | |
73fa96d5 | 3145 | public function get_setting() { |
cc73de71 | 3146 | global $CFG; |
220a90c5 | 3147 | $result = $this->config_read($this->name); |
3148 | if (is_null($result)) { | |
cc73de71 | 3149 | return NULL; |
3150 | } | |
220a90c5 | 3151 | $i = 0; |
3152 | $currentsetting = array(); | |
3153 | $items = explode(';', $result); | |
3154 | foreach ($items as $item) { | |
9baf6825 | 3155 | $item = explode(':', $item); |
3156 | $currentsetting['k'.$i] = $item[0]; | |
3157 | $currentsetting['v'.$i] = $item[1]; | |
3158 | $i++; | |
220a90c5 | 3159 | } |
3160 | return $currentsetting; | |
6e4dc10f | 3161 | } |
eef868d1 | 3162 | |
db26acd4 | 3163 | /** |
3164 | * Save the new setting(s) | |
3165 | * | |
3166 | * @todo Add vartype handling to ensure $data is an array | |
3167 | * @param array $data Array containing the new settings | |
3168 | * @return bool | |
3169 | */ | |
73fa96d5 | 3170 | public function write_setting($data) { |
eef868d1 | 3171 | |
9baf6825 | 3172 | // there miiight be an easier way to do this :) |
3173 | // if this is changed, make sure the $defaults array above is modified so that this | |
3174 | // function processes it correctly | |
eef868d1 | 3175 | |
6e4dc10f | 3176 | $keys = array(); |
3177 | $values = array(); | |
eef868d1 | 3178 | |
6e4dc10f | 3179 | foreach ($data as $key => $value) { |
3180 | if (substr($key,0,1) == 'k') { | |
3181 | $keys[substr($key,1)] = $value; | |
3182 | } elseif (substr($key,0,1) == 'v') { | |
3183 | $values[substr($key,1)] = $value; | |
3184 | } | |
3185 | } | |
eef868d1 | 3186 | |
220a90c5 | 3187 | $result = array(); |
6e4dc10f | 3188 | for ($i = 0; $i < count($keys); $i++) { |
3189 | if (($keys[$i] !== '') && ($values[$i] !== '')) { | |
220a90c5 | 3190 | $result[] = clean_param($keys[$i],PARAM_NOTAGS).':'.clean_param($values[$i], PARAM_NOTAGS); |
6e4dc10f | 3191 | } |
3192 | } | |
eef868d1 | 3193 | |
220a90c5 | 3194 | return ($this->config_write($this->name, implode(';', $result)) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f | 3195 | } |
eef868d1 | 3196 | |
db26acd4 | 3197 | /** |
3198 | * Returns XHTML for the options | |
3199 | * | |
3200 | * @todo Add vartype handling to ensure that $data is an array | |
3201 | * @param array $data An array of values to set | |
3202 | * @param string $query | |
3203 | * @return string XHTML | |
3204 | */ | |
73fa96d5 | 3205 | public function output_html($data, $query='') { |
220a90c5 | 3206 | $fullname = $this->get_full_name(); |
1beed35f | 3207 | $return = '<div class="form-group">'; |
220a90c5 | 3208 | for ($i = 0; $i < count($data) / 2; $i++) { |
3209 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[k'.$i.']" value="'.$data['k'.$i].'" />'; | |
6e4dc10f | 3210 | $return .= ' '; |
220a90c5 | 3211 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[v'.$i.']" value="'.$data['v'.$i].'" /><br />'; |
6e4dc10f | 3212 | } |
220a90c5 | 3213 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[k'.$i.']" value="" />'; |
6e4dc10f | 3214 | $return .= ' '; |
220a90c5 | 3215 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[v'.$i.']" value="" /><br />'; |
3216 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[k'.($i + 1).']" value="" />'; | |
6e4dc10f | 3217 | $return .= ' '; |
220a90c5 | 3218 | $return .= '<input type="text" class="form-text" name="'.$fullname.'[v'.($i + 1).']" value="" />'; |
1beed35f | 3219 | $return .= '</div>'; |
6153cf58 | 3220 | |
587c7040 | 3221 | return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', NULL, $query); |
6e4dc10f | 3222 | } |
eef868d1 | 3223 | |
6e4dc10f | 3224 | } |
db26acd4 | 3225 | /** |
3226 | * Special settings for emoticons | |
3227 | * | |
3228 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
db26acd4 | 3229 | */ |
93c61c18 | 3230 | class admin_setting_emoticons extends admin_setting { |
3231 | ||
9baf6825 | 3232 | /** |
3233 | * Calls parent::__construct with specific args | |
3234 | */ | |
73fa96d5 | 3235 | public function __construct() { |
93c61c18 | 3236 | global $CFG; |
3237 | $name = 'emoticons'; | |
3238 | $visiblename = get_string('emoticons', 'admin'); | |
3239 | $description = get_string('configemoticons', 'admin'); | |
3240 | $defaults = array('k0' => ':-)', | |
9baf6825 | 3241 | 'v0' => 'smiley', |
3242 | 'k1' => ':)', | |
3243 | 'v1' => 'smiley', | |
3244 | 'k2' => ':-D', | |
3245 | 'v2' => 'biggrin', | |
3246 | 'k3' => ';-)', | |
3247 | 'v3' => 'wink', | |
3248 | 'k4' => ':-/', | |
3249 | 'v4' => 'mixed', | |
3250 | 'k5' => 'V-.', | |
3251 | 'v5' => 'thoughtful', | |
3252 | 'k6' => ':-P', | |
3253 | 'v6' => 'tongueout', | |
3254 | 'k7' => 'B-)', | |
3255 | 'v7' => 'cool', | |
3256 | 'k8' => '^-)', | |
3257 | 'v8' => 'approve', | |
3258 | 'k9' => '8-)', | |
3259 | 'v9' => 'wideeyes', | |
3260 | 'k10' => ':o)', | |
3261 | 'v10' => 'clown', | |
3262 | 'k11' => ':-(', | |
3263 | 'v11' => 'sad', | |
3264 | 'k12' => ':(', | |
3265 | 'v12' => 'sad', | |
3266 | 'k13' => '8-.', | |
3267 | 'v13' => 'shy', | |
3268 | 'k14' => ':-I', | |
3269 | 'v14' => 'blush', | |
3270 | 'k15' => ':-X', | |
3271 | 'v15' => 'kiss', | |
3272 | 'k16' => '8-o', | |
3273 | 'v16' => 'surprise', | |
3274 | 'k17' => 'P-|', | |
3275 | 'v17' => 'blackeye', | |
3276 | 'k18' => '8-[', | |
3277 | 'v18' => 'angry', | |
3278 | 'k19' => 'xx-P', | |
3279 | 'v19' => 'dead', | |
3280 | 'k20' => '|-.', | |
3281 | 'v20' => 'sleepy', | |
3282 | 'k21' => '}-]', | |
3283 | 'v21' => 'evil', | |
3284 | 'k22' => '(h)', | |
3285 | 'v22' => 'heart', | |
3286 | 'k23' => '(heart)', | |
3287 | 'v23' => 'heart', | |
3288 | 'k24' => '(y)', | |
3289 | 'v24' => 'yes', | |
3290 | 'k25' => '(n)', | |
3291 | 'v25' => 'no', | |
3292 | 'k26' => '(martin)', | |
3293 | 'v26' => 'martin', | |
3294 | 'k27' => '( )', | |
3295 | 'v27' => 'egg'); | |
73fa96d5 | 3296 | parent::__construct($name, $visiblename, $description, $defaults); |
93c61c18 | 3297 | } |
0c079f19 | 3298 | |
db26acd4 | 3299 | /** |
3300 | * Return the current setting(s) | |
3301 | * | |
db26acd4 | 3302 | * @return array Current settings array |
3303 | */ | |
73fa96d5 | 3304 | public function get_setting() { |
93c61c18 | 3305 | global $CFG; |
220a90c5 | 3306 | $result = $this->config_read($this->name); |
3307 | if (is_null($result)) { | |
93c61c18 | 3308 | return NULL; |
3309 |