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