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