3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Functions and classes used during installation, upgrades and for admin settings.
21 * ADMIN SETTINGS TREE INTRODUCTION
23 * This file performs the following tasks:
24 * -it defines the necessary objects and interfaces to build the Moodle
26 * -it defines the admin_externalpage_setup()
28 * ADMIN_SETTING OBJECTS
30 * Moodle settings are represented by objects that inherit from the admin_setting
31 * class. These objects encapsulate how to read a setting, how to write a new value
32 * to a setting, and how to appropriately display the HTML to modify the setting.
34 * ADMIN_SETTINGPAGE OBJECTS
36 * The admin_setting objects are then grouped into admin_settingpages. The latter
37 * appear in the Moodle admin tree block. All interaction with admin_settingpage
38 * objects is handled by the admin/settings.php file.
40 * ADMIN_EXTERNALPAGE OBJECTS
42 * There are some settings in Moodle that are too complex to (efficiently) handle
43 * with admin_settingpages. (Consider, for example, user management and displaying
44 * lists of users.) In this case, we use the admin_externalpage object. This object
45 * places a link to an external PHP file in the admin tree block.
47 * If you're using an admin_externalpage object for some settings, you can take
48 * advantage of the admin_externalpage_* functions. For example, suppose you wanted
49 * to add a foo.php file into admin. First off, you add the following line to
50 * admin/settings/first.php (at the end of the file) or to some other file in
53 * $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'),
54 * $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission'));
57 * Next, in foo.php, your file structure would resemble the following:
59 * require(dirname(dirname(dirname(__FILE__))).'/config.php');
60 * require_once($CFG->libdir.'/adminlib.php');
61 * admin_externalpage_setup('foo');
62 * // functionality like processing form submissions goes here
63 * echo $OUTPUT->header();
64 * // your HTML goes here
65 * echo $OUTPUT->footer();
68 * The admin_externalpage_setup() function call ensures the user is logged in,
69 * and makes sure that they have the proper role permission to access the page.
70 * It also configures all $PAGE properties needed for navigation.
72 * ADMIN_CATEGORY OBJECTS
74 * Above and beyond all this, we have admin_category objects. These objects
75 * appear as folders in the admin tree block. They contain admin_settingpage's,
76 * admin_externalpage's, and other admin_category's.
80 * admin_settingpage's, admin_externalpage's, and admin_category's all inherit
81 * from part_of_admin_tree (a pseudointerface). This interface insists that
82 * a class has a check_access method for access permissions, a locate method
83 * used to find a specific node in the admin tree and find parent path.
85 * admin_category's inherit from parentable_part_of_admin_tree. This pseudo-
86 * interface ensures that the class implements a recursive add function which
87 * accepts a part_of_admin_tree object and searches for the proper place to
88 * put it. parentable_part_of_admin_tree implies part_of_admin_tree.
90 * Please note that the $this->name field of any part_of_admin_tree must be
91 * UNIQUE throughout the ENTIRE admin tree.
93 * The $this->name field of an admin_setting object (which is *not* part_of_
94 * admin_tree) must be unique on the respective admin_settingpage where it is
97 * Original author: Vincenzo K. Marcovecchio
98 * Maintainer: Petr Skoda
102 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
103 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
106 defined('MOODLE_INTERNAL') || die();
109 require_once($CFG->libdir.'/ddllib.php');
110 require_once($CFG->libdir.'/xmlize.php');
112 define('INSECURE_DATAROOT_WARNING', 1);
113 define('INSECURE_DATAROOT_ERROR', 2);
116 * Automatically clean-up all plugin data and remove the plugin DB tables
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
123 function uninstall_plugin($type, $name) {
124 global $CFG, $DB, $OUTPUT;
126 // recursively uninstall all module subplugins first
127 if ($type === 'mod') {
128 if (file_exists("$CFG->dirroot/mod/$name/db/subplugins.php")) {
129 $subplugins = array();
130 include("$CFG->dirroot/mod/$name/db/subplugins.php");
131 foreach ($subplugins as $subplugintype=>$dir) {
132 $instances = get_plugin_list($subplugintype);
133 foreach ($instances as $subpluginname => $notusedpluginpath) {
134 uninstall_plugin($subplugintype, $subpluginname);
141 $component = $type . '_' . $name; // eg. 'qtype_multichoice' or 'workshopgrading_accumulative' or 'mod_forum'
143 if ($type === 'mod') {
144 $pluginname = $name; // eg. 'forum'
145 if (get_string_manager()->string_exists('modulename', $component)) {
146 $strpluginname = get_string('modulename', $component);
148 $strpluginname = $component;
152 $pluginname = $component;
153 if (get_string_manager()->string_exists('pluginname', $component)) {
154 $strpluginname = get_string('pluginname', $component);
156 $strpluginname = $component;
160 echo $OUTPUT->heading($pluginname);
162 $plugindirectory = get_plugin_directory($type, $name);
163 $uninstalllib = $plugindirectory . '/db/uninstall.php';
164 if (file_exists($uninstalllib)) {
165 require_once($uninstalllib);
166 $uninstallfunction = 'xmldb_' . $pluginname . '_uninstall'; // eg. 'xmldb_workshop_uninstall()'
167 if (function_exists($uninstallfunction)) {
168 if (!$uninstallfunction()) {
169 echo $OUTPUT->notification('Encountered a problem running uninstall function for '. $pluginname);
174 if ($type === 'mod') {
175 // perform cleanup tasks specific for activity modules
177 if (!$module = $DB->get_record('modules', array('name' => $name))) {
178 print_error('moduledoesnotexist', 'error');
181 // delete all the relevant instances from all course sections
182 if ($coursemods = $DB->get_records('course_modules', array('module' => $module->id))) {
183 foreach ($coursemods as $coursemod) {
184 if (!delete_mod_from_section($coursemod->id, $coursemod->section)) {
185 echo $OUTPUT->notification("Could not delete the $strpluginname with id = $coursemod->id from section $coursemod->section");
190 // clear course.modinfo for courses that used this module
191 $sql = "UPDATE {course}
193 WHERE id IN (SELECT DISTINCT course
194 FROM {course_modules}
196 $DB->execute($sql, array($module->id));
198 // delete all the course module records
199 $DB->delete_records('course_modules', array('module' => $module->id));
201 // delete module contexts
203 foreach ($coursemods as $coursemod) {
204 if (!delete_context(CONTEXT_MODULE, $coursemod->id)) {
205 echo $OUTPUT->notification("Could not delete the context for $strpluginname with id = $coursemod->id");
210 // delete the module entry itself
211 $DB->delete_records('modules', array('name' => $module->name));
213 // cleanup the gradebook
214 require_once($CFG->libdir.'/gradelib.php');
215 grade_uninstalled_module($module->name);
217 // Perform any custom uninstall tasks
218 if (file_exists($CFG->dirroot . '/mod/' . $module->name . '/lib.php')) {
219 require_once($CFG->dirroot . '/mod/' . $module->name . '/lib.php');
220 $uninstallfunction = $module->name . '_uninstall';
221 if (function_exists($uninstallfunction)) {
222 debugging("{$uninstallfunction}() has been deprecated. Use the plugin's db/uninstall.php instead", DEBUG_DEVELOPER);
223 if (!$uninstallfunction()) {
224 echo $OUTPUT->notification('Encountered a problem running uninstall function for '. $module->name.'!');
229 } else if ($type === 'enrol') {
230 // NOTE: this is a bit brute force way - it will not trigger events and hooks properly
231 // nuke all role assignments
232 role_unassign_all(array('component'=>$component));
233 // purge participants
234 $DB->delete_records_select('user_enrolments', "enrolid IN (SELECT id FROM {enrol} WHERE enrol = ?)", array($name));
235 // purge enrol instances
236 $DB->delete_records('enrol', array('enrol'=>$name));
237 // tweak enrol settings
238 if (!empty($CFG->enrol_plugins_enabled)) {
239 $enabledenrols = explode(',', $CFG->enrol_plugins_enabled);
240 $enabledenrols = array_unique($enabledenrols);
241 $enabledenrols = array_flip($enabledenrols);
242 unset($enabledenrols[$name]);
243 $enabledenrols = array_flip($enabledenrols);
244 if (is_array($enabledenrols)) {
245 set_config('enrol_plugins_enabled', implode(',', $enabledenrols));
250 // perform clean-up task common for all the plugin/subplugin types
252 // delete calendar events
253 $DB->delete_records('event', array('modulename' => $pluginname));
255 // delete all the logs
256 $DB->delete_records('log', array('module' => $pluginname));
258 // delete log_display information
259 $DB->delete_records('log_display', array('component' => $component));
261 // delete the module configuration records
262 unset_all_config_for_plugin($pluginname);
264 // delete the plugin tables
265 $xmldbfilepath = $plugindirectory . '/db/install.xml';
266 drop_plugin_tables($pluginname, $xmldbfilepath, false);
268 // delete the capabilities that were defined by this module
269 capabilities_cleanup($component);
271 // remove event handlers and dequeue pending events
272 events_uninstall($component);
274 echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
278 * Returns the version of installed component
280 * @param string $component component name
281 * @param string $source either 'disk' or 'installed' - where to get the version information from
282 * @return string|bool version number or false if the component is not found
284 function get_component_version($component, $source='installed') {
287 list($type, $name) = normalize_component($component);
289 // moodle core or a core subsystem
290 if ($type === 'core') {
291 if ($source === 'installed') {
292 if (empty($CFG->version)) {
295 return $CFG->version;
298 if (!is_readable($CFG->dirroot.'/version.php')) {
301 $version = null; //initialize variable for IDEs
302 include($CFG->dirroot.'/version.php');
309 if ($type === 'mod') {
310 if ($source === 'installed') {
311 return $DB->get_field('modules', 'version', array('name'=>$name));
313 $mods = get_plugin_list('mod');
314 if (empty($mods[$name]) or !is_readable($mods[$name].'/version.php')) {
317 $module = new stdclass();
318 include($mods[$name].'/version.php');
319 return $module->version;
325 if ($type === 'block') {
326 if ($source === 'installed') {
327 return $DB->get_field('block', 'version', array('name'=>$name));
329 $blocks = get_plugin_list('block');
330 if (empty($blocks[$name]) or !is_readable($blocks[$name].'/version.php')) {
333 $plugin = new stdclass();
334 include($blocks[$name].'/version.php');
335 return $plugin->version;
340 // all other plugin types
341 if ($source === 'installed') {
342 return get_config($type.'_'.$name, 'version');
344 $plugins = get_plugin_list($type);
345 if (empty($plugins[$name])) {
348 $plugin = new stdclass();
349 include($plugins[$name].'/version.php');
350 return $plugin->version;
356 * Delete all plugin tables
358 * @param string $name Name of plugin, used as table prefix
359 * @param string $file Path to install.xml file
360 * @param bool $feedback defaults to true
361 * @return bool Always returns true
363 function drop_plugin_tables($name, $file, $feedback=true) {
366 // first try normal delete
367 if (file_exists($file) and $DB->get_manager()->delete_tables_from_xmldb_file($file)) {
371 // then try to find all tables that start with name and are not in any xml file
372 $used_tables = get_used_table_names();
374 $tables = $DB->get_tables();
376 /// Iterate over, fixing id fields as necessary
377 foreach ($tables as $table) {
378 if (in_array($table, $used_tables)) {
382 if (strpos($table, $name) !== 0) {
386 // found orphan table --> delete it
387 if ($DB->get_manager()->table_exists($table)) {
388 $xmldb_table = new xmldb_table($table);
389 $DB->get_manager()->drop_table($xmldb_table);
397 * Returns names of all known tables == tables that moodle knows about.
399 * @return array Array of lowercase table names
401 function get_used_table_names() {
402 $table_names = array();
403 $dbdirs = get_db_directories();
405 foreach ($dbdirs as $dbdir) {
406 $file = $dbdir.'/install.xml';
408 $xmldb_file = new xmldb_file($file);
410 if (!$xmldb_file->fileExists()) {
414 $loaded = $xmldb_file->loadXMLStructure();
415 $structure = $xmldb_file->getStructure();
417 if ($loaded and $tables = $structure->getTables()) {
418 foreach($tables as $table) {
419 $table_names[] = strtolower($table->name);
428 * Returns list of all directories where we expect install.xml files
429 * @return array Array of paths
431 function get_db_directories() {
436 /// First, the main one (lib/db)
437 $dbdirs[] = $CFG->libdir.'/db';
439 /// Then, all the ones defined by get_plugin_types()
440 $plugintypes = get_plugin_types();
441 foreach ($plugintypes as $plugintype => $pluginbasedir) {
442 if ($plugins = get_plugin_list($plugintype)) {
443 foreach ($plugins as $plugin => $plugindir) {
444 $dbdirs[] = $plugindir.'/db';
453 * Try to obtain or release the cron lock.
454 * @param string $name name of lock
455 * @param int $until timestamp when this lock considered stale, null means remove lock unconditionally
456 * @param bool $ignorecurrent ignore current lock state, usually extend previous lock, defaults to false
457 * @return bool true if lock obtained
459 function set_cron_lock($name, $until, $ignorecurrent=false) {
462 debugging("Tried to get a cron lock for a null fieldname");
466 // remove lock by force == remove from config table
467 if (is_null($until)) {
468 set_config($name, null);
472 if (!$ignorecurrent) {
473 // read value from db - other processes might have changed it
474 $value = $DB->get_field('config', 'value', array('name'=>$name));
476 if ($value and $value > time()) {
482 set_config($name, $until);
487 * Test if and critical warnings are present
490 function admin_critical_warnings_present() {
493 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
497 if (!isset($SESSION->admin_critical_warning)) {
498 $SESSION->admin_critical_warning = 0;
499 if (is_dataroot_insecure(true) === INSECURE_DATAROOT_ERROR) {
500 $SESSION->admin_critical_warning = 1;
504 return $SESSION->admin_critical_warning;
508 * Detects if float supports at least 10 decimal digits
510 * Detects if float supports at least 10 decimal digits
511 * and also if float-->string conversion works as expected.
513 * @return bool true if problem found
515 function is_float_problem() {
516 $num1 = 2009010200.01;
517 $num2 = 2009010200.02;
519 return ((string)$num1 === (string)$num2 or $num1 === $num2 or $num2 <= (string)$num1);
523 * Try to verify that dataroot is not accessible from web.
525 * Try to verify that dataroot is not accessible from web.
526 * It is not 100% correct but might help to reduce number of vulnerable sites.
527 * Protection from httpd.conf and .htaccess is not detected properly.
529 * @uses INSECURE_DATAROOT_WARNING
530 * @uses INSECURE_DATAROOT_ERROR
531 * @param bool $fetchtest try to test public access by fetching file, default false
532 * @return mixed empty means secure, INSECURE_DATAROOT_ERROR found a critical problem, INSECURE_DATAROOT_WARNING might be problematic
534 function is_dataroot_insecure($fetchtest=false) {
537 $siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround
539 $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1);
540 $rp = strrev(trim($rp, '/'));
541 $rp = explode('/', $rp);
543 if (strpos($siteroot, '/'.$r.'/') === 0) {
544 $siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory
546 break; // probably alias root
550 $siteroot = strrev($siteroot);
551 $dataroot = str_replace('\\', '/', $CFG->dataroot.'/');
553 if (strpos($dataroot, $siteroot) !== 0) {
558 return INSECURE_DATAROOT_WARNING;
561 // now try all methods to fetch a test file using http protocol
563 $httpdocroot = str_replace('\\', '/', strrev($CFG->dirroot.'/'));
564 preg_match('|(https?://[^/]+)|i', $CFG->wwwroot, $matches);
565 $httpdocroot = $matches[1];
566 $datarooturl = $httpdocroot.'/'. substr($dataroot, strlen($siteroot));
567 make_upload_directory('diag');
568 $testfile = $CFG->dataroot.'/diag/public.txt';
569 if (!file_exists($testfile)) {
570 file_put_contents($testfile, 'test file, do not delete');
572 $teststr = trim(file_get_contents($testfile));
573 if (empty($teststr)) {
575 return INSECURE_DATAROOT_WARNING;
578 $testurl = $datarooturl.'/diag/public.txt';
579 if (extension_loaded('curl') and
580 !(stripos(ini_get('disable_functions'), 'curl_init') !== FALSE) and
581 !(stripos(ini_get('disable_functions'), 'curl_setop') !== FALSE) and
582 ($ch = @curl_init($testurl)) !== false) {
583 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
584 curl_setopt($ch, CURLOPT_HEADER, false);
585 $data = curl_exec($ch);
586 if (!curl_errno($ch)) {
588 if ($data === $teststr) {
590 return INSECURE_DATAROOT_ERROR;
596 if ($data = @file_get_contents($testurl)) {
598 if ($data === $teststr) {
599 return INSECURE_DATAROOT_ERROR;
603 preg_match('|https?://([^/]+)|i', $testurl, $matches);
604 $sitename = $matches[1];
606 if ($fp = @fsockopen($sitename, 80, $error)) {
607 preg_match('|https?://[^/]+(.*)|i', $testurl, $matches);
608 $localurl = $matches[1];
609 $out = "GET $localurl HTTP/1.1\r\n";
610 $out .= "Host: $sitename\r\n";
611 $out .= "Connection: Close\r\n\r\n";
617 $data .= fgets($fp, 1024);
618 } else if (@fgets($fp, 1024) === "\r\n") {
624 if ($data === $teststr) {
625 return INSECURE_DATAROOT_ERROR;
629 return INSECURE_DATAROOT_WARNING;
632 /// CLASS DEFINITIONS /////////////////////////////////////////////////////////
635 * Interface for anything appearing in the admin tree
637 * The interface that is implemented by anything that appears in the admin tree
638 * block. It forces inheriting classes to define a method for checking user permissions
639 * and methods for finding something in the admin tree.
641 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
643 interface part_of_admin_tree {
646 * Finds a named part_of_admin_tree.
648 * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree
649 * and not parentable_part_of_admin_tree, then this function should only check if
650 * $this->name matches $name. If it does, it should return a reference to $this,
651 * otherwise, it should return a reference to NULL.
653 * If a class inherits parentable_part_of_admin_tree, this method should be called
654 * recursively on all child objects (assuming, of course, the parent object's name
655 * doesn't match the search criterion).
657 * @param string $name The internal name of the part_of_admin_tree we're searching for.
658 * @return mixed An object reference or a NULL reference.
660 public function locate($name);
663 * Removes named part_of_admin_tree.
665 * @param string $name The internal name of the part_of_admin_tree we want to remove.
666 * @return bool success.
668 public function prune($name);
672 * @param string $query
673 * @return mixed array-object structure of found settings and pages
675 public function search($query);
678 * Verifies current user's access to this part_of_admin_tree.
680 * Used to check if the current user has access to this part of the admin tree or
681 * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree,
682 * then this method is usually just a call to has_capability() in the site context.
684 * If a class inherits parentable_part_of_admin_tree, this method should return the
685 * logical OR of the return of check_access() on all child objects.
687 * @return bool True if the user has access, false if she doesn't.
689 public function check_access();
692 * Mostly useful for removing of some parts of the tree in admin tree block.
694 * @return True is hidden from normal list view
696 public function is_hidden();
699 * Show we display Save button at the page bottom?
702 public function show_save();
706 * Interface implemented by any part_of_admin_tree that has children.
708 * The interface implemented by any part_of_admin_tree that can be a parent
709 * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart
710 * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods
711 * include an add method for adding other part_of_admin_tree objects as children.
713 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
715 interface parentable_part_of_admin_tree extends part_of_admin_tree {
718 * Adds a part_of_admin_tree object to the admin tree.
720 * Used to add a part_of_admin_tree object to this object or a child of this
721 * object. $something should only be added if $destinationname matches
722 * $this->name. If it doesn't, add should be called on child objects that are
723 * also parentable_part_of_admin_tree's.
725 * @param string $destinationname The internal name of the new parent for $something.
726 * @param part_of_admin_tree $something The object to be added.
727 * @return bool True on success, false on failure.
729 public function add($destinationname, $something);
734 * The object used to represent folders (a.k.a. categories) in the admin tree block.
736 * Each admin_category object contains a number of part_of_admin_tree objects.
738 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
740 class admin_category implements parentable_part_of_admin_tree {
742 /** @var mixed An array of part_of_admin_tree objects that are this object's children */
744 /** @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects */
746 /** @var string The displayed name for this category. Usually obtained through get_string() */
748 /** @var bool Should this category be hidden in admin tree block? */
750 /** @var mixed Either a string or an array or strings */
752 /** @var mixed Either a string or an array or strings */
755 /** @var array fast lookup category cache, all categories of one tree point to one cache */
756 protected $category_cache;
759 * Constructor for an empty admin category
761 * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
762 * @param string $visiblename The displayed named for this category. Usually obtained through get_string()
763 * @param bool $hidden hide category in admin tree block, defaults to false
765 public function __construct($name, $visiblename, $hidden=false) {
766 $this->children = array();
768 $this->visiblename = $visiblename;
769 $this->hidden = $hidden;
773 * Returns a reference to the part_of_admin_tree object with internal name $name.
775 * @param string $name The internal name of the object we want.
776 * @param bool $findpath initialize path and visiblepath arrays
777 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
780 public function locate($name, $findpath=false) {
781 if (is_array($this->category_cache) and !isset($this->category_cache[$this->name])) {
782 // somebody much have purged the cache
783 $this->category_cache[$this->name] = $this;
786 if ($this->name == $name) {
788 $this->visiblepath[] = $this->visiblename;
789 $this->path[] = $this->name;
794 // quick category lookup
795 if (!$findpath and is_array($this->category_cache) and isset($this->category_cache[$name])) {
796 return $this->category_cache[$name];
800 foreach($this->children as $childid=>$unused) {
801 if ($return = $this->children[$childid]->locate($name, $findpath)) {
806 if (!is_null($return) and $findpath) {
807 $return->visiblepath[] = $this->visiblename;
808 $return->path[] = $this->name;
817 * @param string query
818 * @return mixed array-object structure of found settings and pages
820 public function search($query) {
822 foreach ($this->children as $child) {
823 $subsearch = $child->search($query);
824 if (!is_array($subsearch)) {
825 debugging('Incorrect search result from '.$child->name);
828 $result = array_merge($result, $subsearch);
834 * Removes part_of_admin_tree object with internal name $name.
836 * @param string $name The internal name of the object we want to remove.
837 * @return bool success
839 public function prune($name) {
841 if ($this->name == $name) {
842 return false; //can not remove itself
845 foreach($this->children as $precedence => $child) {
846 if ($child->name == $name) {
847 // clear cache and delete self
848 if (is_array($this->category_cache)) {
849 while($this->category_cache) {
850 // delete the cache, but keep the original array address
851 array_pop($this->category_cache);
854 unset($this->children[$precedence]);
856 } else if ($this->children[$precedence]->prune($name)) {
864 * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object.
866 * @param string $destinationame The internal name of the immediate parent that we want for $something.
867 * @param mixed $something A part_of_admin_tree or setting instance to be added.
868 * @return bool True if successfully added, false if $something can not be added.
870 public function add($parentname, $something) {
871 $parent = $this->locate($parentname);
872 if (is_null($parent)) {
873 debugging('parent does not exist!');
877 if ($something instanceof part_of_admin_tree) {
878 if (!($parent instanceof parentable_part_of_admin_tree)) {
879 debugging('error - parts of tree can be inserted only into parentable parts');
882 $parent->children[] = $something;
883 if (is_array($this->category_cache) and ($something instanceof admin_category)) {
884 if (isset($this->category_cache[$something->name])) {
885 debugging('Duplicate admin catefory name: '.$something->name);
887 $this->category_cache[$something->name] = $something;
888 $something->category_cache =& $this->category_cache;
889 foreach ($something->children as $child) {
890 // just in case somebody already added subcategories
891 if ($child instanceof admin_category) {
892 if (isset($this->category_cache[$child->name])) {
893 debugging('Duplicate admin catefory name: '.$child->name);
895 $this->category_cache[$child->name] = $child;
896 $child->category_cache =& $this->category_cache;
905 debugging('error - can not add this element');
912 * Checks if the user has access to anything in this category.
914 * @return bool True if the user has access to at least one child in this category, false otherwise.
916 public function check_access() {
917 foreach ($this->children as $child) {
918 if ($child->check_access()) {
926 * Is this category hidden in admin tree block?
928 * @return bool True if hidden
930 public function is_hidden() {
931 return $this->hidden;
935 * Show we display Save button at the page bottom?
938 public function show_save() {
939 foreach ($this->children as $child) {
940 if ($child->show_save()) {
949 * Root of admin settings tree, does not have any parent.
951 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
953 class admin_root extends admin_category {
954 /** @var array List of errors */
956 /** @var string search query */
958 /** @var bool full tree flag - true means all settings required, false only pages required */
960 /** @var bool flag indicating loaded tree */
962 /** @var mixed site custom defaults overriding defaults in settings files*/
963 public $custom_defaults;
966 * @param bool $fulltree true means all settings required,
967 * false only pages required
969 public function __construct($fulltree) {
972 parent::__construct('root', get_string('administration'), false);
973 $this->errors = array();
975 $this->fulltree = $fulltree;
976 $this->loaded = false;
978 $this->category_cache = array();
980 // load custom defaults if found
981 $this->custom_defaults = null;
982 $defaultsfile = "$CFG->dirroot/local/defaults.php";
983 if (is_readable($defaultsfile)) {
985 include($defaultsfile);
986 if (is_array($defaults) and count($defaults)) {
987 $this->custom_defaults = $defaults;
993 * Empties children array, and sets loaded to false
995 * @param bool $requirefulltree
997 public function purge_children($requirefulltree) {
998 $this->children = array();
999 $this->fulltree = ($requirefulltree || $this->fulltree);
1000 $this->loaded = false;
1001 //break circular dependencies - this helps PHP 5.2
1002 while($this->category_cache) {
1003 array_pop($this->category_cache);
1005 $this->category_cache = array();
1010 * Links external PHP pages into the admin tree.
1012 * See detailed usage example at the top of this document (adminlib.php)
1014 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1016 class admin_externalpage implements part_of_admin_tree {
1018 /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */
1021 /** @var string The displayed name for this external page. Usually obtained through get_string(). */
1022 public $visiblename;
1024 /** @var string The external URL that we should link to when someone requests this external page. */
1027 /** @var string The role capability/permission a user must have to access this external page. */
1028 public $req_capability;
1030 /** @var object The context in which capability/permission should be checked, default is site context. */
1033 /** @var bool hidden in admin tree block. */
1036 /** @var mixed either string or array of string */
1038 public $visiblepath;
1041 * Constructor for adding an external page into the admin tree.
1043 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1044 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1045 * @param string $url The external URL that we should link to when someone requests this external page.
1046 * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
1047 * @param boolean $hidden Is this external page hidden in admin tree block? Default false.
1048 * @param stdClass $context The context the page relates to. Not sure what happens
1049 * if you specify something other than system or front page. Defaults to system.
1051 public function __construct($name, $visiblename, $url, $req_capability='moodle/site:config', $hidden=false, $context=NULL) {
1052 $this->name = $name;
1053 $this->visiblename = $visiblename;
1055 if (is_array($req_capability)) {
1056 $this->req_capability = $req_capability;
1058 $this->req_capability = array($req_capability);
1060 $this->hidden = $hidden;
1061 $this->context = $context;
1065 * Returns a reference to the part_of_admin_tree object with internal name $name.
1067 * @param string $name The internal name of the object we want.
1068 * @param bool $findpath defaults to false
1069 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1071 public function locate($name, $findpath=false) {
1072 if ($this->name == $name) {
1074 $this->visiblepath = array($this->visiblename);
1075 $this->path = array($this->name);
1085 * This function always returns false, required function by interface
1087 * @param string $name
1090 public function prune($name) {
1095 * Search using query
1097 * @param string $query
1098 * @return mixed array-object structure of found settings and pages
1100 public function search($query) {
1101 $textlib = textlib_get_instance();
1104 if (strpos(strtolower($this->name), $query) !== false) {
1106 } else if (strpos($textlib->strtolower($this->visiblename), $query) !== false) {
1110 $result = new stdClass();
1111 $result->page = $this;
1112 $result->settings = array();
1113 return array($this->name => $result);
1120 * Determines if the current user has access to this external page based on $this->req_capability.
1122 * @return bool True if user has access, false otherwise.
1124 public function check_access() {
1126 $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context;
1127 foreach($this->req_capability as $cap) {
1128 if (has_capability($cap, $context)) {
1136 * Is this external page hidden in admin tree block?
1138 * @return bool True if hidden
1140 public function is_hidden() {
1141 return $this->hidden;
1145 * Show we display Save button at the page bottom?
1148 public function show_save() {
1154 * Used to group a number of admin_setting objects into a page and add them to the admin tree.
1156 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1158 class admin_settingpage implements part_of_admin_tree {
1160 /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */
1163 /** @var string The displayed name for this external page. Usually obtained through get_string(). */
1164 public $visiblename;
1166 /** @var mixed An array of admin_setting objects that are part of this setting page. */
1169 /** @var string The role capability/permission a user must have to access this external page. */
1170 public $req_capability;
1172 /** @var object The context in which capability/permission should be checked, default is site context. */
1175 /** @var bool hidden in admin tree block. */
1178 /** @var mixed string of paths or array of strings of paths */
1180 public $visiblepath;
1183 * see admin_settingpage for details of this function
1185 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1186 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1187 * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
1188 * @param boolean $hidden Is this external page hidden in admin tree block? Default false.
1189 * @param stdClass $context The context the page relates to. Not sure what happens
1190 * if you specify something other than system or front page. Defaults to system.
1192 public function __construct($name, $visiblename, $req_capability='moodle/site:config', $hidden=false, $context=NULL) {
1193 $this->settings = new stdClass();
1194 $this->name = $name;
1195 $this->visiblename = $visiblename;
1196 if (is_array($req_capability)) {
1197 $this->req_capability = $req_capability;
1199 $this->req_capability = array($req_capability);
1201 $this->hidden = $hidden;
1202 $this->context = $context;
1206 * see admin_category
1208 * @param string $name
1209 * @param bool $findpath
1210 * @return mixed Object (this) if name == this->name, else returns null
1212 public function locate($name, $findpath=false) {
1213 if ($this->name == $name) {
1215 $this->visiblepath = array($this->visiblename);
1216 $this->path = array($this->name);
1226 * Search string in settings page.
1228 * @param string $query
1231 public function search($query) {
1234 foreach ($this->settings as $setting) {
1235 if ($setting->is_related($query)) {
1236 $found[] = $setting;
1241 $result = new stdClass();
1242 $result->page = $this;
1243 $result->settings = $found;
1244 return array($this->name => $result);
1247 $textlib = textlib_get_instance();
1250 if (strpos(strtolower($this->name), $query) !== false) {
1252 } else if (strpos($textlib->strtolower($this->visiblename), $query) !== false) {
1256 $result = new stdClass();
1257 $result->page = $this;
1258 $result->settings = array();
1259 return array($this->name => $result);
1266 * This function always returns false, required by interface
1268 * @param string $name
1269 * @return bool Always false
1271 public function prune($name) {
1276 * adds an admin_setting to this admin_settingpage
1278 * 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
1279 * n.b. each admin_setting in an admin_settingpage must have a unique internal name
1281 * @param object $setting is the admin_setting object you want to add
1282 * @return bool true if successful, false if not
1284 public function add($setting) {
1285 if (!($setting instanceof admin_setting)) {
1286 debugging('error - not a setting instance');
1290 $this->settings->{$setting->name} = $setting;
1295 * see admin_externalpage
1297 * @return bool Returns true for yes false for no
1299 public function check_access() {
1301 $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context;
1302 foreach($this->req_capability as $cap) {
1303 if (has_capability($cap, $context)) {
1311 * outputs this page as html in a table (suitable for inclusion in an admin pagetype)
1312 * @return string Returns an XHTML string
1314 public function output_html() {
1315 $adminroot = admin_get_root();
1316 $return = '<fieldset>'."\n".'<div class="clearer"><!-- --></div>'."\n";
1317 foreach($this->settings as $setting) {
1318 $fullname = $setting->get_full_name();
1319 if (array_key_exists($fullname, $adminroot->errors)) {
1320 $data = $adminroot->errors[$fullname]->data;
1322 $data = $setting->get_setting();
1323 // do not use defaults if settings not available - upgrade settings handles the defaults!
1325 $return .= $setting->output_html($data);
1327 $return .= '</fieldset>';
1332 * Is this settings page hidden in admin tree block?
1334 * @return bool True if hidden
1336 public function is_hidden() {
1337 return $this->hidden;
1341 * Show we display Save button at the page bottom?
1344 public function show_save() {
1345 foreach($this->settings as $setting) {
1346 if (empty($setting->nosave)) {
1356 * Admin settings class. Only exists on setting pages.
1357 * Read & write happens at this level; no authentication.
1359 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1361 abstract class admin_setting {
1362 /** @var string unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. */
1364 /** @var string localised name */
1365 public $visiblename;
1366 /** @var string localised long description in Markdown format */
1367 public $description;
1368 /** @var mixed Can be string or array of string */
1369 public $defaultsetting;
1371 public $updatedcallback;
1372 /** @var mixed can be String or Null. Null means main config table */
1373 public $plugin; // null means main config table
1374 /** @var bool true indicates this setting does not actually save anything, just information */
1375 public $nosave = false;
1379 * @param string $name unique ascii name, either 'mysetting' for settings that in config,
1380 * or 'myplugin/mysetting' for ones in config_plugins.
1381 * @param string $visiblename localised name
1382 * @param string $description localised long description
1383 * @param mixed $defaultsetting string or array depending on implementation
1385 public function __construct($name, $visiblename, $description, $defaultsetting) {
1386 $this->parse_setting_name($name);
1387 $this->visiblename = $visiblename;
1388 $this->description = $description;
1389 $this->defaultsetting = $defaultsetting;
1393 * Set up $this->name and potentially $this->plugin
1395 * Set up $this->name and possibly $this->plugin based on whether $name looks
1396 * like 'settingname' or 'plugin/settingname'. Also, do some sanity checking
1397 * on the names, that is, output a developer debug warning if the name
1398 * contains anything other than [a-zA-Z0-9_]+.
1400 * @param string $name the setting name passed in to the constructor.
1402 private function parse_setting_name($name) {
1403 $bits = explode('/', $name);
1404 if (count($bits) > 2) {
1405 throw new moodle_exception('invalidadminsettingname', '', '', $name);
1407 $this->name = array_pop($bits);
1408 if (!preg_match('/^[a-zA-Z0-9_]+$/', $this->name)) {
1409 throw new moodle_exception('invalidadminsettingname', '', '', $name);
1411 if (!empty($bits)) {
1412 $this->plugin = array_pop($bits);
1413 if ($this->plugin === 'moodle') {
1414 $this->plugin = null;
1415 } else if (!preg_match('/^[a-zA-Z0-9_]+$/', $this->plugin)) {
1416 throw new moodle_exception('invalidadminsettingname', '', '', $name);
1422 * Returns the fullname prefixed by the plugin
1425 public function get_full_name() {
1426 return 's_'.$this->plugin.'_'.$this->name;
1430 * Returns the ID string based on plugin and name
1433 public function get_id() {
1434 return 'id_s_'.$this->plugin.'_'.$this->name;
1438 * Returns the config if possible
1440 * @return mixed returns config if successfull else null
1442 public function config_read($name) {
1444 if (!empty($this->plugin)) {
1445 $value = get_config($this->plugin, $name);
1446 return $value === false ? NULL : $value;
1449 if (isset($CFG->$name)) {
1458 * Used to set a config pair and log change
1460 * @param string $name
1461 * @param mixed $value Gets converted to string if not null
1462 * @return bool Write setting to config table
1464 public function config_write($name, $value) {
1465 global $DB, $USER, $CFG;
1467 if ($this->nosave) {
1471 // make sure it is a real change
1472 $oldvalue = get_config($this->plugin, $name);
1473 $oldvalue = ($oldvalue === false) ? null : $oldvalue; // normalise
1474 $value = is_null($value) ? null : (string)$value;
1476 if ($oldvalue === $value) {
1481 set_config($name, $value, $this->plugin);
1484 $log = new stdClass();
1485 $log->userid = during_initial_install() ? 0 :$USER->id; // 0 as user id during install
1486 $log->timemodified = time();
1487 $log->plugin = $this->plugin;
1489 $log->value = $value;
1490 $log->oldvalue = $oldvalue;
1491 $DB->insert_record('config_log', $log);
1493 return true; // BC only
1497 * Returns current value of this setting
1498 * @return mixed array or string depending on instance, NULL means not set yet
1500 public abstract function get_setting();
1503 * Returns default setting if exists
1504 * @return mixed array or string depending on instance; NULL means no default, user must supply
1506 public function get_defaultsetting() {
1507 $adminroot = admin_get_root(false, false);
1508 if (!empty($adminroot->custom_defaults)) {
1509 $plugin = is_null($this->plugin) ? 'moodle' : $this->plugin;
1510 if (isset($adminroot->custom_defaults[$plugin])) {
1511 if (array_key_exists($this->name, $adminroot->custom_defaults[$plugin])) { // null is valid value here ;-)
1512 return $adminroot->custom_defaults[$plugin][$this->name];
1516 return $this->defaultsetting;
1522 * @param mixed $data string or array, must not be NULL
1523 * @return string empty string if ok, string error message otherwise
1525 public abstract function write_setting($data);
1528 * Return part of form with setting
1529 * This function should always be overwritten
1531 * @param mixed $data array or string depending on setting
1532 * @param string $query
1535 public function output_html($data, $query='') {
1536 // should be overridden
1541 * Function called if setting updated - cleanup, cache reset, etc.
1542 * @param string $functionname Sets the function name
1544 public function set_updatedcallback($functionname) {
1545 $this->updatedcallback = $functionname;
1549 * Is setting related to query text - used when searching
1550 * @param string $query
1553 public function is_related($query) {
1554 if (strpos(strtolower($this->name), $query) !== false) {
1557 $textlib = textlib_get_instance();
1558 if (strpos($textlib->strtolower($this->visiblename), $query) !== false) {
1561 if (strpos($textlib->strtolower($this->description), $query) !== false) {
1564 $current = $this->get_setting();
1565 if (!is_null($current)) {
1566 if (is_string($current)) {
1567 if (strpos($textlib->strtolower($current), $query) !== false) {
1572 $default = $this->get_defaultsetting();
1573 if (!is_null($default)) {
1574 if (is_string($default)) {
1575 if (strpos($textlib->strtolower($default), $query) !== false) {
1585 * No setting - just heading and text.
1587 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1589 class admin_setting_heading extends admin_setting {
1591 * not a setting, just text
1592 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
1593 * @param string $heading heading
1594 * @param string $information text in box
1596 public function __construct($name, $heading, $information) {
1597 $this->nosave = true;
1598 parent::__construct($name, $heading, $information, '');
1602 * Always returns true
1603 * @return bool Always returns true
1605 public function get_setting() {
1610 * Always returns true
1611 * @return bool Always returns true
1613 public function get_defaultsetting() {
1618 * Never write settings
1619 * @return string Always returns an empty string
1621 public function write_setting($data) {
1622 // do not write any setting
1627 * Returns an HTML string
1628 * @return string Returns an HTML string
1630 public function output_html($data, $query='') {
1633 if ($this->visiblename != '') {
1634 $return .= $OUTPUT->heading($this->visiblename, 3, 'main');
1636 if ($this->description != '') {
1637 $return .= $OUTPUT->box(highlight($query, markdown_to_html($this->description)), 'generalbox formsettingheading');
1644 * The most flexibly setting, user is typing text
1646 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1648 class admin_setting_configtext extends admin_setting {
1650 /** @var mixed int means PARAM_XXX type, string is a allowed format in regex */
1652 /** @var int default field size */
1656 * Config text constructor
1658 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
1659 * @param string $visiblename localised
1660 * @param string $description long localised info
1661 * @param string $defaultsetting
1662 * @param mixed $paramtype int means PARAM_XXX type, string is a allowed format in regex
1663 * @param int $size default field size
1665 public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $size=null) {
1666 $this->paramtype = $paramtype;
1667 if (!is_null($size)) {
1668 $this->size = $size;
1670 $this->size = ($paramtype === PARAM_INT) ? 5 : 30;
1672 parent::__construct($name, $visiblename, $description, $defaultsetting);
1676 * Return the setting
1678 * @return mixed returns config if successful else null
1680 public function get_setting() {
1681 return $this->config_read($this->name);
1684 public function write_setting($data) {
1685 if ($this->paramtype === PARAM_INT and $data === '') {
1686 // do not complain if '' used instead of 0
1689 // $data is a string
1690 $validated = $this->validate($data);
1691 if ($validated !== true) {
1694 return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
1698 * Validate data before storage
1699 * @param string data
1700 * @return mixed true if ok string if error found
1702 public function validate($data) {
1703 // allow paramtype to be a custom regex if it is the form of /pattern/
1704 if (preg_match('#^/.*/$#', $this->paramtype)) {
1705 if (preg_match($this->paramtype, $data)) {
1708 return get_string('validateerror', 'admin');
1711 } else if ($this->paramtype === PARAM_RAW) {
1715 $cleaned = clean_param($data, $this->paramtype);
1716 if ("$data" === "$cleaned") { // implicit conversion to string is needed to do exact comparison
1719 return get_string('validateerror', 'admin');
1725 * Return an XHTML string for the setting
1726 * @return string Returns an XHTML string
1728 public function output_html($data, $query='') {
1729 $default = $this->get_defaultsetting();
1731 return format_admin_setting($this, $this->visiblename,
1732 '<div class="form-text defaultsnext"><input type="text" size="'.$this->size.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" /></div>',
1733 $this->description, true, '', $default, $query);
1738 * General text area without html editor.
1740 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1742 class admin_setting_configtextarea extends admin_setting_configtext {
1747 * @param string $name
1748 * @param string $visiblename
1749 * @param string $description
1750 * @param mixed $defaultsetting string or array
1751 * @param mixed $paramtype
1752 * @param string $cols The number of columns to make the editor
1753 * @param string $rows The number of rows to make the editor
1755 public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $cols='60', $rows='8') {
1756 $this->rows = $rows;
1757 $this->cols = $cols;
1758 parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype);
1761 * Returns an XHTML string for the editor
1763 * @param string $data
1764 * @param string $query
1765 * @return string XHTML string for the editor
1767 public function output_html($data, $query='') {
1768 $default = $this->get_defaultsetting();
1770 $defaultinfo = $default;
1771 if (!is_null($default) and $default !== '') {
1772 $defaultinfo = "\n".$default;
1775 return format_admin_setting($this, $this->visiblename,
1776 '<div class="form-textarea" ><textarea rows="'. $this->rows .'" cols="'. $this->cols .'" id="'. $this->get_id() .'" name="'. $this->get_full_name() .'">'. s($data) .'</textarea></div>',
1777 $this->description, true, '', $defaultinfo, $query);
1782 * General text area with html editor.
1784 class admin_setting_confightmleditor extends admin_setting_configtext {
1789 * @param string $name
1790 * @param string $visiblename
1791 * @param string $description
1792 * @param mixed $defaultsetting string or array
1793 * @param mixed $paramtype
1795 public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $cols='60', $rows='8') {
1796 $this->rows = $rows;
1797 $this->cols = $cols;
1798 parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype);
1799 editors_head_setup();
1802 * Returns an XHTML string for the editor
1804 * @param string $data
1805 * @param string $query
1806 * @return string XHTML string for the editor
1808 public function output_html($data, $query='') {
1809 $default = $this->get_defaultsetting();
1811 $defaultinfo = $default;
1812 if (!is_null($default) and $default !== '') {
1813 $defaultinfo = "\n".$default;
1816 $editor = editors_get_preferred_editor(FORMAT_HTML);
1817 $editor->use_editor($this->get_id(), array('noclean'=>true));
1819 return format_admin_setting($this, $this->visiblename,
1820 '<div class="form-textarea"><textarea rows="'. $this->rows .'" cols="'. $this->cols .'" id="'. $this->get_id() .'" name="'. $this->get_full_name() .'">'. s($data) .'</textarea></div>',
1821 $this->description, true, '', $defaultinfo, $query);
1826 * Password field, allows unmasking of password
1828 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1830 class admin_setting_configpasswordunmask extends admin_setting_configtext {
1833 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
1834 * @param string $visiblename localised
1835 * @param string $description long localised info
1836 * @param string $defaultsetting default password
1838 public function __construct($name, $visiblename, $description, $defaultsetting) {
1839 parent::__construct($name, $visiblename, $description, $defaultsetting, PARAM_RAW, 30);
1843 * Returns XHTML for the field
1844 * Writes Javascript into the HTML below right before the last div
1846 * @todo Make javascript available through newer methods if possible
1847 * @param string $data Value for the field
1848 * @param string $query Passed as final argument for format_admin_setting
1849 * @return string XHTML field
1851 public function output_html($data, $query='') {
1852 $id = $this->get_id();
1853 $unmask = get_string('unmaskpassword', 'form');
1854 $unmaskjs = '<script type="text/javascript">
1856 var is_ie = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
1858 document.getElementById("'.$id.'").setAttribute("autocomplete", "off");
1860 var unmaskdiv = document.getElementById("'.$id.'unmaskdiv");
1862 var unmaskchb = document.createElement("input");
1863 unmaskchb.setAttribute("type", "checkbox");
1864 unmaskchb.setAttribute("id", "'.$id.'unmask");
1865 unmaskchb.onchange = function() {unmaskPassword("'.$id.'");};
1866 unmaskdiv.appendChild(unmaskchb);
1868 var unmasklbl = document.createElement("label");
1869 unmasklbl.innerHTML = "'.addslashes_js($unmask).'";
1871 unmasklbl.setAttribute("htmlFor", "'.$id.'unmask");
1873 unmasklbl.setAttribute("for", "'.$id.'unmask");
1875 unmaskdiv.appendChild(unmasklbl);
1878 // ugly hack to work around the famous onchange IE bug
1879 unmaskchb.onclick = function() {this.blur();};
1880 unmaskdiv.onclick = function() {this.blur();};
1884 return format_admin_setting($this, $this->visiblename,
1885 '<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>',
1886 $this->description, true, '', NULL, $query);
1893 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1895 class admin_setting_configfile extends admin_setting_configtext {
1898 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
1899 * @param string $visiblename localised
1900 * @param string $description long localised info
1901 * @param string $defaultdirectory default directory location
1903 public function __construct($name, $visiblename, $description, $defaultdirectory) {
1904 parent::__construct($name, $visiblename, $description, $defaultdirectory, PARAM_RAW, 50);
1908 * Returns XHTML for the field
1910 * Returns XHTML for the field and also checks whether the file
1911 * specified in $data exists using file_exists()
1913 * @param string $data File name and path to use in value attr
1914 * @param string $query
1915 * @return string XHTML field
1917 public function output_html($data, $query='') {
1918 $default = $this->get_defaultsetting();
1921 if (file_exists($data)) {
1922 $executable = '<span class="pathok">✔</span>';
1924 $executable = '<span class="patherror">✘</span>';
1930 return format_admin_setting($this, $this->visiblename,
1931 '<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>',
1932 $this->description, true, '', $default, $query);
1937 * Path to executable file
1939 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1941 class admin_setting_configexecutable extends admin_setting_configfile {
1944 * Returns an XHTML field
1946 * @param string $data This is the value for the field
1947 * @param string $query
1948 * @return string XHTML field
1950 public function output_html($data, $query='') {
1951 $default = $this->get_defaultsetting();
1954 if (file_exists($data) and is_executable($data)) {
1955 $executable = '<span class="pathok">✔</span>';
1957 $executable = '<span class="patherror">✘</span>';
1963 return format_admin_setting($this, $this->visiblename,
1964 '<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>',
1965 $this->description, true, '', $default, $query);
1972 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1974 class admin_setting_configdirectory extends admin_setting_configfile {
1977 * Returns an XHTML field
1979 * @param string $data This is the value for the field
1980 * @param string $query
1981 * @return string XHTML
1983 public function output_html($data, $query='') {
1984 $default = $this->get_defaultsetting();
1987 if (file_exists($data) and is_dir($data)) {
1988 $executable = '<span class="pathok">✔</span>';
1990 $executable = '<span class="patherror">✘</span>';
1996 return format_admin_setting($this, $this->visiblename,
1997 '<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>',
1998 $this->description, true, '', $default, $query);
2005 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2007 class admin_setting_configcheckbox extends admin_setting {
2008 /** @var string Value used when checked */
2010 /** @var string Value used when not checked */
2015 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2016 * @param string $visiblename localised
2017 * @param string $description long localised info
2018 * @param string $defaultsetting
2019 * @param string $yes value used when checked
2020 * @param string $no value used when not checked
2022 public function __construct($name, $visiblename, $description, $defaultsetting, $yes='1', $no='0') {
2023 parent::__construct($name, $visiblename, $description, $defaultsetting);
2024 $this->yes = (string)$yes;
2025 $this->no = (string)$no;
2029 * Retrieves the current setting using the objects name
2033 public function get_setting() {
2034 return $this->config_read($this->name);
2038 * Sets the value for the setting
2040 * Sets the value for the setting to either the yes or no values
2041 * of the object by comparing $data to yes
2043 * @param mixed $data Gets converted to str for comparison against yes value
2044 * @return string empty string or error
2046 public function write_setting($data) {
2047 if ((string)$data === $this->yes) { // convert to strings before comparison
2052 return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
2056 * Returns an XHTML checkbox field
2058 * @param string $data If $data matches yes then checkbox is checked
2059 * @param string $query
2060 * @return string XHTML field
2062 public function output_html($data, $query='') {
2063 $default = $this->get_defaultsetting();
2065 if (!is_null($default)) {
2066 if ((string)$default === $this->yes) {
2067 $defaultinfo = get_string('checkboxyes', 'admin');
2069 $defaultinfo = get_string('checkboxno', 'admin');
2072 $defaultinfo = NULL;
2075 if ((string)$data === $this->yes) { // convert to strings before comparison
2076 $checked = 'checked="checked"';
2081 return format_admin_setting($this, $this->visiblename,
2082 '<div class="form-checkbox defaultsnext" ><input type="hidden" name="'.$this->get_full_name().'" value="'.s($this->no).'" /> '
2083 .'<input type="checkbox" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($this->yes).'" '.$checked.' /></div>',
2084 $this->description, true, '', $defaultinfo, $query);
2089 * Multiple checkboxes, each represents different value, stored in csv format
2091 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2093 class admin_setting_configmulticheckbox extends admin_setting {
2094 /** @var array Array of choices value=>label */
2098 * Constructor: uses parent::__construct
2100 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2101 * @param string $visiblename localised
2102 * @param string $description long localised info
2103 * @param array $defaultsetting array of selected
2104 * @param array $choices array of $value=>$label for each checkbox
2106 public function __construct($name, $visiblename, $description, $defaultsetting, $choices) {
2107 $this->choices = $choices;
2108 parent::__construct($name, $visiblename, $description, $defaultsetting);
2112 * This public function may be used in ancestors for lazy loading of choices
2114 * @todo Check if this function is still required content commented out only returns true
2115 * @return bool true if loaded, false if error
2117 public function load_choices() {
2119 if (is_array($this->choices)) {
2122 .... load choices here
2128 * Is setting related to query text - used when searching
2130 * @param string $query
2131 * @return bool true on related, false on not or failure
2133 public function is_related($query) {
2134 if (!$this->load_choices() or empty($this->choices)) {
2137 if (parent::is_related($query)) {
2141 $textlib = textlib_get_instance();
2142 foreach ($this->choices as $desc) {
2143 if (strpos($textlib->strtolower($desc), $query) !== false) {
2151 * Returns the current setting if it is set
2153 * @return mixed null if null, else an array
2155 public function get_setting() {
2156 $result = $this->config_read($this->name);
2158 if (is_null($result)) {
2161 if ($result === '') {
2164 $enabled = explode(',', $result);
2166 foreach ($enabled as $option) {
2167 $setting[$option] = 1;
2173 * Saves the setting(s) provided in $data
2175 * @param array $data An array of data, if not array returns empty str
2176 * @return mixed empty string on useless data or bool true=success, false=failed
2178 public function write_setting($data) {
2179 if (!is_array($data)) {
2180 return ''; // ignore it
2182 if (!$this->load_choices() or empty($this->choices)) {
2185 unset($data['xxxxx']);
2187 foreach ($data as $key => $value) {
2188 if ($value and array_key_exists($key, $this->choices)) {
2192 return $this->config_write($this->name, implode(',', $result)) ? '' : get_string('errorsetting', 'admin');
2196 * Returns XHTML field(s) as required by choices
2198 * Relies on data being an array should data ever be another valid vartype with
2199 * acceptable value this may cause a warning/error
2200 * if (!is_array($data)) would fix the problem
2202 * @todo Add vartype handling to ensure $data is an array
2204 * @param array $data An array of checked values
2205 * @param string $query
2206 * @return string XHTML field
2208 public function output_html($data, $query='') {
2209 if (!$this->load_choices() or empty($this->choices)) {
2212 $default = $this->get_defaultsetting();
2213 if (is_null($default)) {
2216 if (is_null($data)) {
2220 $defaults = array();
2221 foreach ($this->choices as $key=>$description) {
2222 if (!empty($data[$key])) {
2223 $checked = 'checked="checked"';
2227 if (!empty($default[$key])) {
2228 $defaults[] = $description;
2231 $options[] = '<input type="checkbox" id="'.$this->get_id().'_'.$key.'" name="'.$this->get_full_name().'['.$key.']" value="1" '.$checked.' />'
2232 .'<label for="'.$this->get_id().'_'.$key.'">'.highlightfast($query, $description).'</label>';
2235 if (is_null($default)) {
2236 $defaultinfo = NULL;
2237 } else if (!empty($defaults)) {
2238 $defaultinfo = implode(', ', $defaults);
2240 $defaultinfo = get_string('none');
2243 $return = '<div class="form-multicheckbox">';
2244 $return .= '<input type="hidden" name="'.$this->get_full_name().'[xxxxx]" value="1" />'; // something must be submitted even if nothing selected
2247 foreach ($options as $option) {
2248 $return .= '<li>'.$option.'</li>';
2252 $return .= '</div>';
2254 return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', $defaultinfo, $query);
2260 * Multiple checkboxes 2, value stored as string 00101011
2262 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2264 class admin_setting_configmulticheckbox2 extends admin_setting_configmulticheckbox {
2267 * Returns the setting if set
2269 * @return mixed null if not set, else an array of set settings
2271 public function get_setting() {
2272 $result = $this->config_read($this->name);
2273 if (is_null($result)) {
2276 if (!$this->load_choices()) {
2279 $result = str_pad($result, count($this->choices), '0');
2280 $result = preg_split('//', $result, -1, PREG_SPLIT_NO_EMPTY);
2282 foreach ($this->choices as $key=>$unused) {
2283 $value = array_shift($result);
2292 * Save setting(s) provided in $data param
2294 * @param array $data An array of settings to save
2295 * @return mixed empty string for bad data or bool true=>success, false=>error
2297 public function write_setting($data) {
2298 if (!is_array($data)) {
2299 return ''; // ignore it
2301 if (!$this->load_choices() or empty($this->choices)) {
2305 foreach ($this->choices as $key=>$unused) {
2306 if (!empty($data[$key])) {
2312 return $this->config_write($this->name, $result) ? '' : get_string('errorsetting', 'admin');
2317 * Select one value from list
2319 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2321 class admin_setting_configselect extends admin_setting {
2322 /** @var array Array of choices value=>label */
2327 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2328 * @param string $visiblename localised
2329 * @param string $description long localised info
2330 * @param string|int $defaultsetting
2331 * @param array $choices array of $value=>$label for each selection
2333 public function __construct($name, $visiblename, $description, $defaultsetting, $choices) {
2334 $this->choices = $choices;
2335 parent::__construct($name, $visiblename, $description, $defaultsetting);
2339 * This function may be used in ancestors for lazy loading of choices
2341 * Override this method if loading of choices is expensive, such
2342 * as when it requires multiple db requests.
2344 * @return bool true if loaded, false if error
2346 public function load_choices() {
2348 if (is_array($this->choices)) {
2351 .... load choices here
2357 * Check if this is $query is related to a choice
2359 * @param string $query
2360 * @return bool true if related, false if not
2362 public function is_related($query) {
2363 if (parent::is_related($query)) {
2366 if (!$this->load_choices()) {
2369 $textlib = textlib_get_instance();
2370 foreach ($this->choices as $key=>$value) {
2371 if (strpos($textlib->strtolower($key), $query) !== false) {
2374 if (strpos($textlib->strtolower($value), $query) !== false) {
2382 * Return the setting
2384 * @return mixed returns config if successful else null
2386 public function get_setting() {
2387 return $this->config_read($this->name);
2393 * @param string $data
2394 * @return string empty of error string
2396 public function write_setting($data) {
2397 if (!$this->load_choices() or empty($this->choices)) {
2400 if (!array_key_exists($data, $this->choices)) {
2401 return ''; // ignore it
2404 return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
2408 * Returns XHTML select field
2410 * Ensure the options are loaded, and generate the XHTML for the select
2411 * element and any warning message. Separating this out from output_html
2412 * makes it easier to subclass this class.
2414 * @param string $data the option to show as selected.
2415 * @param string $current the currently selected option in the database, null if none.
2416 * @param string $default the default selected option.
2417 * @return array the HTML for the select element, and a warning message.
2419 public function output_select_html($data, $current, $default, $extraname = '') {
2420 if (!$this->load_choices() or empty($this->choices)) {
2421 return array('', '');
2425 if (is_null($current)) {
2427 } else if (empty($current) and (array_key_exists('', $this->choices) or array_key_exists(0, $this->choices))) {
2429 } else if (!array_key_exists($current, $this->choices)) {
2430 $warning = get_string('warningcurrentsetting', 'admin', s($current));
2431 if (!is_null($default) and $data == $current) {
2432 $data = $default; // use default instead of first value when showing the form
2436 $selecthtml = '<select id="'.$this->get_id().'" name="'.$this->get_full_name().$extraname.'">';
2437 foreach ($this->choices as $key => $value) {
2438 // the string cast is needed because key may be integer - 0 is equal to most strings!
2439 $selecthtml .= '<option value="'.$key.'"'.((string)$key==$data ? ' selected="selected"' : '').'>'.$value.'</option>';
2441 $selecthtml .= '</select>';
2442 return array($selecthtml, $warning);
2446 * Returns XHTML select field and wrapping div(s)
2448 * @see output_select_html()
2450 * @param string $data the option to show as selected
2451 * @param string $query
2452 * @return string XHTML field and wrapping div
2454 public function output_html($data, $query='') {
2455 $default = $this->get_defaultsetting();
2456 $current = $this->get_setting();
2458 list($selecthtml, $warning) = $this->output_select_html($data, $current, $default);
2463 if (!is_null($default) and array_key_exists($default, $this->choices)) {
2464 $defaultinfo = $this->choices[$default];
2466 $defaultinfo = NULL;
2469 $return = '<div class="form-select defaultsnext">' . $selecthtml . '</div>';
2471 return format_admin_setting($this, $this->visiblename, $return, $this->description, true, $warning, $defaultinfo, $query);
2476 * Select multiple items from list
2478 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2480 class admin_setting_configmultiselect extends admin_setting_configselect {
2483 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2484 * @param string $visiblename localised
2485 * @param string $description long localised info
2486 * @param array $defaultsetting array of selected items
2487 * @param array $choices array of $value=>$label for each list item
2489 public function __construct($name, $visiblename, $description, $defaultsetting, $choices) {
2490 parent::__construct($name, $visiblename, $description, $defaultsetting, $choices);
2494 * Returns the select setting(s)
2496 * @return mixed null or array. Null if no settings else array of setting(s)
2498 public function get_setting() {
2499 $result = $this->config_read($this->name);
2500 if (is_null($result)) {
2503 if ($result === '') {
2506 return explode(',', $result);
2510 * Saves setting(s) provided through $data
2512 * Potential bug in the works should anyone call with this function
2513 * using a vartype that is not an array
2515 * @todo Add vartype handling to ensure $data is an array
2516 * @param array $data
2518 public function write_setting($data) {
2519 if (!is_array($data)) {
2520 return ''; //ignore it
2522 if (!$this->load_choices() or empty($this->choices)) {
2526 unset($data['xxxxx']);
2529 foreach ($data as $value) {
2530 if (!array_key_exists($value, $this->choices)) {
2531 continue; // ignore it
2536 return ($this->config_write($this->name, implode(',', $save)) ? '' : get_string('errorsetting', 'admin'));
2540 * Is setting related to query text - used when searching
2542 * @param string $query
2543 * @return bool true if related, false if not
2545 public function is_related($query) {
2546 if (!$this->load_choices() or empty($this->choices)) {
2549 if (parent::is_related($query)) {
2553 $textlib = textlib_get_instance();
2554 foreach ($this->choices as $desc) {
2555 if (strpos($textlib->strtolower($desc), $query) !== false) {
2563 * Returns XHTML multi-select field
2565 * @todo Add vartype handling to ensure $data is an array
2566 * @param array $data Array of values to select by default
2567 * @param string $query
2568 * @return string XHTML multi-select field
2570 public function output_html($data, $query='') {
2571 if (!$this->load_choices() or empty($this->choices)) {
2574 $choices = $this->choices;
2575 $default = $this->get_defaultsetting();
2576 if (is_null($default)) {
2579 if (is_null($data)) {
2583 $defaults = array();
2584 $size = min(10, count($this->choices));
2585 $return = '<div class="form-select"><input type="hidden" name="'.$this->get_full_name().'[xxxxx]" value="1" />'; // something must be submitted even if nothing selected
2586 $return .= '<select id="'.$this->get_id().'" name="'.$this->get_full_name().'[]" size="'.$size.'" multiple="multiple">';
2587 foreach ($this->choices as $key => $description) {
2588 if (in_array($key, $data)) {
2589 $selected = 'selected="selected"';
2593 if (in_array($key, $default)) {
2594 $defaults[] = $description;
2597 $return .= '<option value="'.s($key).'" '.$selected.'>'.$description.'</option>';
2600 if (is_null($default)) {
2601 $defaultinfo = NULL;
2602 } if (!empty($defaults)) {
2603 $defaultinfo = implode(', ', $defaults);
2605 $defaultinfo = get_string('none');
2608 $return .= '</select></div>';
2609 return format_admin_setting($this, $this->visiblename, $return, $this->description, true, '', $defaultinfo, $query);
2616 * This is a liiitle bit messy. we're using two selects, but we're returning
2617 * them as an array named after $name (so we only use $name2 internally for the setting)
2619 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2621 class admin_setting_configtime extends admin_setting {
2622 /** @var string Used for setting second select (minutes) */
2627 * @param string $hoursname setting for hours
2628 * @param string $minutesname setting for hours
2629 * @param string $visiblename localised
2630 * @param string $description long localised info
2631 * @param array $defaultsetting array representing default time 'h'=>hours, 'm'=>minutes
2633 public function __construct($hoursname, $minutesname, $visiblename, $description, $defaultsetting) {
2634 $this->name2 = $minutesname;
2635 parent::__construct($hoursname, $visiblename, $description, $defaultsetting);
2639 * Get the selected time
2641 * @return mixed An array containing 'h'=>xx, 'm'=>xx, or null if not set
2643 public function get_setting() {
2644 $result1 = $this->config_read($this->name);
2645 $result2 = $this->config_read($this->name2);
2646 if (is_null($result1) or is_null($result2)) {
2650 return array('h' => $result1, 'm' => $result2);
2654 * Store the time (hours and minutes)
2656 * @param array $data Must be form 'h'=>xx, 'm'=>xx
2657 * @return bool true if success, false if not
2659 public function write_setting($data) {
2660 if (!is_array($data)) {
2664 $result = $this->config_write($this->name, (int)$data['h']) && $this->config_write($this->name2, (int)$data['m']);
2665 return ($result ? '' : get_string('errorsetting', 'admin'));
2669 * Returns XHTML time select fields
2671 * @param array $data Must be form 'h'=>xx, 'm'=>xx
2672 * @param string $query
2673 * @return string XHTML time select fields and wrapping div(s)
2675 public function output_html($data, $query='') {
2676 $default = $this->get_defaultsetting();
2678 if (is_array($default)) {
2679 $defaultinfo = $default['h'].':'.$default['m'];
2681 $defaultinfo = NULL;
2684 $return = '<div class="form-time defaultsnext">'.
2685 '<select id="'.$this->get_id().'h" name="'.$this->get_full_name().'[h]">';
2686 for ($i = 0; $i < 24; $i++) {
2687 $return .= '<option value="'.$i.'"'.($i == $data['h'] ? ' selected="selected"' : '').'>'.$i.'</option>';
2689 $return .= '</select>:<select id="'.$this->get_id().'m" name="'.$this->get_full_name().'[m]">';
2690 for ($i = 0; $i < 60; $i += 5) {
2691 $return .= '<option value="'.$i.'"'.($i == $data['m'] ? ' selected="selected"' : '').'>'.$i.'</option>';
2693 $return .= '</select></div>';
2694 return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', $defaultinfo, $query);
2700 * Used to validate a textarea used for ip addresses
2702 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2704 class admin_setting_configiplist extends admin_setting_configtextarea {
2707 * Validate the contents of the textarea as IP addresses
2709 * Used to validate a new line separated list of IP addresses collected from
2710 * a textarea control
2712 * @param string $data A list of IP Addresses separated by new lines
2713 * @return mixed bool true for success or string:error on failure
2715 public function validate($data) {
2717 $ips = explode("\n", $data);
2722 foreach($ips as $ip) {
2724 if(preg_match('#^(\d{1,3})(\.\d{1,3}){0,3}$#', $ip, $match) ||
2725 preg_match('#^(\d{1,3})(\.\d{1,3}){0,3}(\/\d{1,2})$#', $ip, $match) ||
2726 preg_match('#^(\d{1,3})(\.\d{1,3}){3}(-\d{1,3})$#', $ip, $match)) {
2736 return get_string('validateerror', 'admin');
2742 * An admin setting for selecting one or more users who have a capability
2743 * in the system context
2745 * An admin setting for selecting one or more users, who have a particular capability
2746 * in the system context. Warning, make sure the list will never be too long. There is
2747 * no paging or searching of this list.
2749 * To correctly get a list of users from this config setting, you need to call the
2750 * get_users_from_config($CFG->mysetting, $capability); function in moodlelib.php.
2752 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2754 class admin_setting_users_with_capability extends admin_setting_configmultiselect {
2755 /** @var string The capabilities name */
2756 protected $capability;
2757 /** @var int include admin users too */
2758 protected $includeadmins;
2763 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2764 * @param string $visiblename localised name
2765 * @param string $description localised long description
2766 * @param array $defaultsetting array of usernames
2767 * @param string $capability string capability name.
2768 * @param bool $includeadmins include administrators
2770 function __construct($name, $visiblename, $description, $defaultsetting, $capability, $includeadmins = true) {
2771 $this->capability = $capability;
2772 $this->includeadmins = $includeadmins;
2773 parent::__construct($name, $visiblename, $description, $defaultsetting, NULL);
2777 * Load all of the uses who have the capability into choice array
2779 * @return bool Always returns true
2781 function load_choices() {
2782 if (is_array($this->choices)) {
2785 $users = get_users_by_capability(get_context_instance(CONTEXT_SYSTEM),
2786 $this->capability, 'u.id,u.username,u.firstname,u.lastname', 'u.lastname,u.firstname');
2787 $this->choices = array(
2788 '$@NONE@$' => get_string('nobody'),
2789 '$@ALL@$' => get_string('everyonewhocan', 'admin', get_capability_string($this->capability)),
2791 if ($this->includeadmins) {
2792 $admins = get_admins();
2793 foreach ($admins as $user) {
2794 $this->choices[$user->id] = fullname($user);
2797 if (is_array($users)) {
2798 foreach ($users as $user) {
2799 $this->choices[$user->id] = fullname($user);
2806 * Returns the default setting for class
2808 * @return mixed Array, or string. Empty string if no default
2810 public function get_defaultsetting() {
2811 $this->load_choices();
2812 $defaultsetting = parent::get_defaultsetting();
2813 if (empty($defaultsetting)) {
2814 return array('$@NONE@$');
2815 } else if (array_key_exists($defaultsetting, $this->choices)) {
2816 return $defaultsetting;
2823 * Returns the current setting
2825 * @return mixed array or string
2827 public function get_setting() {
2828 $result = parent::get_setting();
2829 if ($result === null) {
2830 // this is necessary for settings upgrade
2833 if (empty($result)) {
2834 $result = array('$@NONE@$');
2840 * Save the chosen setting provided as $data
2842 * @param array $data
2843 * @return mixed string or array
2845 public function write_setting($data) {
2846 // If all is selected, remove any explicit options.
2847 if (in_array('$@ALL@$', $data)) {
2848 $data = array('$@ALL@$');
2850 // None never needs to be written to the DB.
2851 if (in_array('$@NONE@$', $data)) {
2852 unset($data[array_search('$@NONE@$', $data)]);
2854 return parent::write_setting($data);
2859 * Special checkbox for calendar - resets SESSION vars.
2861 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2863 class admin_setting_special_adminseesall extends admin_setting_configcheckbox {
2865 * Calls the parent::__construct with default values
2867 * name => calendar_adminseesall
2868 * visiblename => get_string('adminseesall', 'admin')
2869 * description => get_string('helpadminseesall', 'admin')
2870 * defaultsetting => 0
2872 public function __construct() {
2873 parent::__construct('calendar_adminseesall', get_string('adminseesall', 'admin'),
2874 get_string('helpadminseesall', 'admin'), '0');
2878 * Stores the setting passed in $data
2880 * @param mixed gets converted to string for comparison
2881 * @return string empty string or error message
2883 public function write_setting($data) {
2885 unset($SESSION->cal_courses_shown);
2886 return parent::write_setting($data);
2891 * Special select for settings that are altered in setup.php and can not be altered on the fly
2893 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2895 class admin_setting_special_selectsetup extends admin_setting_configselect {
2897 * Reads the setting directly from the database
2901 public function get_setting() {
2902 // read directly from db!
2903 return get_config(NULL, $this->name);
2907 * Save the setting passed in $data
2909 * @param string $data The setting to save
2910 * @return string empty or error message
2912 public function write_setting($data) {
2914 // do not change active CFG setting!
2915 $current = $CFG->{$this->name};
2916 $result = parent::write_setting($data);
2917 $CFG->{$this->name} = $current;
2923 * Special select for frontpage - stores data in course table
2925 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2927 class admin_setting_sitesetselect extends admin_setting_configselect {
2929 * Returns the site name for the selected site
2932 * @return string The site name of the selected site
2934 public function get_setting() {
2936 return $site->{$this->name};
2939 * Updates the database and save the setting
2941 * @param string data
2942 * @return string empty or error message
2944 public function write_setting($data) {
2946 if (!in_array($data, array_keys($this->choices))) {
2947 return get_string('errorsetting', 'admin');
2949 $record = new stdClass();
2950 $record->id = SITEID;
2951 $temp = $this->name;
2952 $record->$temp = $data;
2953 $record->timemodified = time();
2955 $SITE->{$this->name} = $data;
2956 return ($DB->update_record('course', $record) ? '' : get_string('errorsetting', 'admin'));
2961 * Select for blog's bloglevel setting: if set to 0, will set blog_menu
2964 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2966 class admin_setting_bloglevel extends admin_setting_configselect {
2968 * Updates the database and save the setting
2970 * @param string data
2971 * @return string empty or error message
2973 public function write_setting($data) {
2975 if ($data['bloglevel'] == 0) {
2976 $DB->set_field('block', 'visible', 0, array('name' => 'blog_menu'));
2978 $DB->set_field('block', 'visible', 1, array('name' => 'blog_menu'));
2980 return parent::write_setting($data);
2985 * Special select - lists on the frontpage - hacky
2987 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2989 class admin_setting_courselist_frontpage extends admin_setting {
2990 /** @var array Array of choices value=>label */
2994 * Construct override, requires one param
2996 * @param bool $loggedin Is the user logged in
2998 public function __construct($loggedin) {
3000 require_once($CFG->dirroot.'/course/lib.php');
3001 $name = 'frontpage'.($loggedin ? 'loggedin' : '');
3002 $visiblename = get_string('frontpage'.($loggedin ? 'loggedin' : ''),'admin');
3003 $description = get_string('configfrontpage'.($loggedin ? 'loggedin' : ''),'admin');
3004 $defaults = array(FRONTPAGECOURSELIST);
3005 parent::__construct($name, $visiblename, $description, $defaults);
3009 * Loads the choices available
3011 * @return bool always returns true
3013 public function load_choices() {
3015 if (is_array($this->choices)) {
3018 $this->choices = array(FRONTPAGENEWS => get_string('frontpagenews'),
3019 FRONTPAGECOURSELIST => get_string('frontpagecourselist'),
3020 FRONTPAGECATEGORYNAMES => get_string('frontpagecategorynames'),
3021 FRONTPAGECATEGORYCOMBO => get_string('frontpagecategorycombo'),
3022 'none' => get_string('none'));
3023 if ($this->name == 'frontpage' and $DB->count_records('course') > FRONTPAGECOURSELIMIT) {
3024 unset($this->choices[FRONTPAGECOURSELIST]);
3029 * Returns the selected settings
3031 * @param mixed array or setting or null
3033 public function get_setting() {
3034 $result = $this->config_read($this->name);
3035 if (is_null($result)) {
3038 if ($result === '') {
3041 return explode(',', $result);
3045 * Save the selected options
3047 * @param array $data
3048 * @return mixed empty string (data is not an array) or bool true=success false=failure
3050 public function write_setting($data) {
3051 if (!is_array($data)) {
3054 $this->load_choices();
3056 foreach($data as $datum) {
3057 if ($datum == 'none' or !array_key_exists($datum, $this->choices)) {
3060 $save[$datum] = $datum; // no duplicates
3062 return ($this->config_write($this->name, implode(',', $save)) ? '' : get_string('errorsetting', 'admin'));
3066 * Return XHTML select field and wrapping div
3068 * @todo Add vartype handling to make sure $data is an array
3069 * @param array $data Array of elements to select by default
3070 * @return string XHTML select field and wrapping div
3072 public function output_html($data, $query='') {
3073 $this->load_choices();
3074 $currentsetting = array();
3075 foreach ($data as $key) {
3076 if ($key != 'none' and array_key_exists($key, $this->choices)) {
3077 $currentsetting[] = $key; // already selected first
3081 $return = '<div class="form-group">';
3082 for ($i = 0; $i < count($this->choices) - 1; $i++) {
3083 if (!array_key_exists($i, $currentsetting)) {
3084 $currentsetting[$i] = 'none'; //none
3086 $return .='<select class="form-select" id="'.$this->get_id().$i.'" name="'.$this->get_full_name().'[]">';
3087 foreach ($this->choices as $key => $value) {
3088 $return .= '<option value="'.$key.'"'.("$key" == $currentsetting[$i] ? ' selected="selected"' : '').'>'.$value.'</option>';
3090 $return .= '</select>';
3091 if ($i !== count($this->choices) - 2) {
3092 $return .= '<br />';
3095 $return .= '</div>';
3097 return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', NULL, $query);
3102 * Special checkbox for frontpage - stores data in course table
3104 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3106 class admin_setting_sitesetcheckbox extends admin_setting_configcheckbox {
3108 * Returns the current sites name
3112 public function get_setting() {
3114 return $site->{$this->name};
3118 * Save the selected setting
3120 * @param string $data The selected site
3121 * @return string empty string or error message
3123 public function write_setting($data) {
3125 $record = new stdClass();
3126 $record->id = SITEID;
3127 $record->{$this->name} = ($data == '1' ? 1 : 0);
3128 $record->timemodified = time();
3130 $SITE->{$this->name} = $data;
3131 return ($DB->update_record('course', $record) ? '' : get_string('errorsetting', 'admin'));
3136 * Special text for frontpage - stores data in course table.
3137 * Empty string means not set here. Manual setting is required.
3139 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3141 class admin_setting_sitesettext extends admin_setting_configtext {
3143 * Return the current setting
3145 * @return mixed string or null
3147 public function get_setting() {
3149 return $site->{$this->name} != '' ? $site->{$this->name} : NULL;
3153 * Validate the selected data
3155 * @param string $data The selected value to validate
3156 * @return mixed true or message string
3158 public function validate($data) {
3159 $cleaned = clean_param($data, PARAM_MULTILANG);
3160 if ($cleaned === '') {
3161 return get_string('required');
3163 if ("$data" == "$cleaned") { // implicit conversion to string is needed to do exact comparison
3166 return get_string('validateerror', 'admin');
3171 * Save the selected setting
3173 * @param string $data The selected value
3174 * @return string empty or error message
3176 public function write_setting($data) {
3178 $data = trim($data);
3179 $validated = $this->validate($data);
3180 if ($validated !== true) {
3184 $record = new stdClass();
3185 $record->id = SITEID;
3186 $record->{$this->name} = $data;
3187 $record->timemodified = time();
3189 $SITE->{$this->name} = $data;
3190 return ($DB->update_record('course', $record) ? '' : get_string('dbupdatefailed', 'error'));
3195 * Special text editor for site description.
3197 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3199 class admin_setting_special_frontpagedesc extends admin_setting {
3201 * Calls parent::__construct with specific arguments
3203 public function __construct() {
3204 parent::__construct('summary', get_string('frontpagedescription'), get_string('frontpagedescriptionhelp'), NULL);
3205 editors_head_setup();
3209 * Return the current setting
3210 * @return string The current setting
3212 public function get_setting() {
3214 return $site->{$this->name};
3218 * Save the new setting
3220 * @param string $data The new value to save
3221 * @return string empty or error message
3223 public function write_setting($data) {
3225 $record = new stdClass();
3226 $record->id = SITEID;
3227 $record->{$this->name} = $data;
3228 $record->timemodified = time();
3229 $SITE->{$this->name} = $data;
3230 return ($DB->update_record('course', $record) ? '' : get_string('errorsetting', 'admin'));
3234 * Returns XHTML for the field plus wrapping div
3236 * @param string $data The current value
3237 * @param string $query
3238 * @return string The XHTML output
3240 public function output_html($data, $query='') {
3243 $CFG->adminusehtmleditor = can_use_html_editor();
3244 $return = '<div class="form-htmlarea">'.print_textarea($CFG->adminusehtmleditor, 15, 60, 0, 0, $this->get_full_name(), $data, 0, true, 'summary') .'</div>';
3246 return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', NULL, $query);
3251 * Administration interface for emoticon_manager settings.
3253 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3255 class admin_setting_emoticons extends admin_setting {
3258 * Calls parent::__construct with specific args
3260 public function __construct() {
3263 $manager = get_emoticon_manager();
3264 $defaults = $this->prepare_form_data($manager->default_emoticons());
3265 parent::__construct('emoticons', get_string('emoticons', 'admin'), get_string('emoticons_desc', 'admin'), $defaults);
3269 * Return the current setting(s)
3271 * @return array Current settings array
3273 public function get_setting() {
3276 $manager = get_emoticon_manager();
3278 $config = $this->config_read($this->name);
3279 if (is_null($config)) {
3283 $config = $manager->decode_stored_config($config);
3284 if (is_null($config)) {
3288 return $this->prepare_form_data($config);
3292 * Save selected settings
3294 * @param array $data Array of settings to save
3297 public function write_setting($data) {
3299 $manager = get_emoticon_manager();
3300 $emoticons = $this->process_form_data($data);
3302 if ($emoticons === false) {
3306 if ($this->config_write($this->name, $manager->encode_stored_config($emoticons))) {
3307 return ''; // success
3309 return get_string('errorsetting', 'admin') . $this->visiblename . html_writer::empty_tag('br');
3314 * Return XHTML field(s) for options
3316 * @param array $data Array of options to set in HTML
3317 * @return string XHTML string for the fields and wrapping div(s)
3319 public function output_html($data, $query='') {
3322 $out = html_writer::start_tag('table', array('border' => 1, 'class' => 'generaltable'));
3323 $out .= html_writer::start_tag('thead');
3324 $out .= html_writer::start_tag('tr');
3325 $out .= html_writer::tag('th', get_string('emoticontext', 'admin'));
3326 $out .= html_writer::tag('th', get_string('emoticonimagename', 'admin'));
3327 $out .= html_writer::tag('th', get_string('emoticoncomponent', 'admin'));
3328 $out .= html_writer::tag('th', get_string('emoticonalt', 'admin'), array('colspan' => 2));
3329 $out .= html_writer::tag('th', '');
3330 $out .= html_writer::end_tag('tr');
3331 $out .= html_writer::end_tag('thead');
3332 $out .= html_writer::start_tag('tbody');
3334 foreach($data as $field => $value) {
3337 $out .= html_writer::start_tag('tr');
3338 $current_text = $value;
3339 $current_filename = '';
3340 $current_imagecomponent = '';
3341 $current_altidentifier = '';
3342 $current_altcomponent = '';
3344 $current_filename = $value;
3346 $current_imagecomponent = $value;
3348 $current_altidentifier = $value;
3350 $current_altcomponent = $value;
3353 $out .= html_writer::tag('td',
3354 html_writer::empty_tag('input',
3357 'class' => 'form-text',
3358 'name' => $this->get_full_name().'['.$field.']',
3361 ), array('class' => 'c'.$i)
3365 if (get_string_manager()->string_exists($current_altidentifier, $current_altcomponent)) {
3366 $alt = get_string($current_altidentifier, $current_altcomponent);
3368 $alt = $current_text;
3370 if ($current_filename) {
3371 $out .= html_writer::tag('td', $OUTPUT->render(new pix_emoticon($current_filename, $alt, $current_imagecomponent)));
3373 $out .= html_writer::tag('td', '');
3375 $out .= html_writer::end_tag('tr');
3382 $out .= html_writer::end_tag('tbody');
3383 $out .= html_writer::end_tag('table');
3384 $out = html_writer::tag('div', $out, array('class' => 'form-group'));
3385 $out .= html_writer::tag('div', html_writer::link(new moodle_url('/admin/resetemoticons.php'), get_string('emoticonsreset', 'admin')));
3387 return format_admin_setting($this, $this->visiblename, $out, $this->description, false, '', NULL, $query);
3391 * Converts the array of emoticon objects provided by {@see emoticon_manager} into admin settings form data
3393 * @see self::process_form_data()
3394 * @param array $emoticons array of emoticon objects as returned by {@see emoticon_manager}
3395 * @return array of form fields and their values
3397 protected function prepare_form_data(array $emoticons) {
3401 foreach ($emoticons as $emoticon) {
3402 $form['text'.$i] = $emoticon->text;
3403 $form['imagename'.$i] = $emoticon->imagename;
3404 $form['imagecomponent'.$i] = $emoticon->imagecomponent;
3405 $form['altidentifier'.$i] = $emoticon->altidentifier;
3406 $form['altcomponent'.$i] = $emoticon->altcomponent;
3409 // add one more blank field set for new object
3410 $form['text'.$i] = '';
3411 $form['imagename'.$i] = '';
3412 $form['imagecomponent'.$i] = '';
3413 $form['altidentifier'.$i] = '';
3414 $form['altcomponent'.$i] = '';
3420 * Converts the data from admin settings form into an array of emoticon objects
3422 * @see self::prepare_form_data()
3423 * @param array $data array of admin form fields and values
3424 * @return false|array of emoticon objects
3426 protected function process_form_data(array $form) {
3428 $count = count($form); // number of form field values
3431 // we must get five fields per emoticon object
3435 $emoticons = array();
3436 for ($i = 0; $i < $count / 5; $i++) {
3437 $emoticon = new stdClass();
3438 $emoticon->text = clean_param(trim($form['text'.$i]), PARAM_NOTAGS);
3439 $emoticon->imagename = clean_param(trim($form['imagename'.$i]), PARAM_PATH);
3440 $emoticon->imagecomponent = clean_param(trim($form['imagecomponent'.$i]), PARAM_SAFEDIR);
3441 $emoticon->altidentifier = clean_param(trim($form['altidentifier'.$i]), PARAM_STRINGID);
3442 $emoticon->altcomponent = clean_param(trim($form['altcomponent'.$i]), PARAM_SAFEDIR);
3444 if (strpos($emoticon->text, ':/') !== false or strpos($emoticon->text, '//') !== false) {
3445 // prevent from breaking http://url.addresses by accident
3446 $emoticon->text = '';
3449 if (strlen($emoticon->text) < 2) {
3450 // do not allow single character emoticons
3451 $emoticon->text = '';
3454 if (preg_match('/^[a-zA-Z]+[a-zA-Z0-9]*$/', $emoticon->text)) {
3455 // emoticon text must contain some non-alphanumeric character to prevent
3456 // breaking HTML tags
3457 $emoticon->text = '';
3460 if ($emoticon->text !== '' and $emoticon->imagename !== '' and $emoticon->imagecomponent !== '') {
3461 $emoticons[] = $emoticon;
3469 * Special setting for limiting of the list of available languages.
3471 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3473 class admin_setting_langlist extends admin_setting_configtext {
3475 * Calls parent::__construct with specific arguments
3477 public function __construct() {
3478 parent::__construct('langlist', get_string('langlist', 'admin'), get_string('configlanglist', 'admin'), '', PARAM_NOTAGS);
3482 * Save the new setting
3484 * @param string $data The new setting
3487 public function write_setting($data) {
3488 $return = parent::write_setting($data);
3489 get_string_manager()->reset_caches();
3495 * Selection of one of the recognised countries using the list
3496 * returned by {@link get_list_of_countries()}.
3498 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3500 class admin_settings_country_select extends admin_setting_configselect {
3501 protected $includeall;
3502 public function __construct($name, $visiblename, $description, $defaultsetting, $includeall=false) {
3503 $this->includeall = $includeall;
3504 parent::__construct($name, $visiblename, $description, $defaultsetting, NULL);
3508 * Lazy-load the available choices for the select box
3510 public function load_choices() {
3512 if (is_array($this->choices)) {
3515 $this->choices = array_merge(
3516 array('0' => get_string('choosedots')),
3517 get_string_manager()->get_list_of_countries($this->includeall));
3523 * Course category selection
3525 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3527 class admin_settings_coursecat_select extends admin_setting_configselect {
3529 * Calls parent::__construct with specific arguments
3531 public function __construct($name, $visiblename, $description, $defaultsetting) {
3532 parent::__construct($name, $visiblename, $description, $defaultsetting, NULL);
3536 * Load the available choices for the select box
3540 public function load_choices() {
3542 require_once($CFG->dirroot.'/course/lib.php');
3543 if (is_array($this->choices)) {
3546 $this->choices = make_categories_options();
3552 * Special control for selecting days to backup
3554 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3556 class admin_setting_special_backupdays extends admin_setting_configmulticheckbox2 {
3558 * Calls parent::__construct with specific arguments
3560 public function __construct() {
3561 parent::__construct('backup_auto_weekdays', get_string('automatedbackupschedule','backup'), get_string('automatedbackupschedulehelp','backup'), array(), NULL);
3562 $this->plugin = 'backup';
3565 * Load the available choices for the select box
3567 * @return bool Always returns true
3569 public function load_choices() {
3570 if (is_array($this->choices)) {
3573 $this->choices = array();
3574 $days = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
3575 foreach ($days as $day) {
3576 $this->choices[$day] = get_string($day, 'calendar');
3583 * Special debug setting
3585 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3587 class admin_setting_special_debug extends admin_setting_configselect {
3589 * Calls parent::__construct with specific arguments
3591 public function __construct() {
3592 parent::__construct('debug', get_string('debug', 'admin'), get_string('configdebug', 'admin'), DEBUG_NONE, NULL);
3596 * Load the available choices for the select box
3600 public function load_choices() {
3601 if (is_array($this->choices)) {
3604 $this->choices = array(DEBUG_NONE => get_string('debugnone', 'admin'),
3605 DEBUG_MINIMAL => get_string('debugminimal', 'admin'),
3606 DEBUG_NORMAL => get_string('debugnormal', 'admin'),
3607 DEBUG_ALL => get_string('debugall', 'admin'),
3608 DEBUG_DEVELOPER => get_string('debugdeveloper', 'admin'));
3614 * Special admin control
3616 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3618 class admin_setting_special_calendar_weekend extends admin_setting {
3620 * Calls parent::__construct with specific arguments
3622 public function __construct() {
3623 $name = 'calendar_weekend';
3624 $visiblename = get_string('calendar_weekend', 'admin');
3625 $description = get_string('helpweekenddays', 'admin');
3626 $default = array ('0', '6'); // Saturdays and Sundays
3627 parent::__construct($name, $visiblename, $description, $default);
3630 * Gets the current settings as an array
3632 * @return mixed Null if none, else array of settings
3634 public function get_setting() {
3635 $result = $this->config_read($this->name);
3636 if (is_null($result)) {
3639 if ($result === '') {
3642 $settings = array();
3643 for ($i=0; $i<7; $i++) {
3644 if ($result & (1 << $i)) {
3652 * Save the new settings
3654 * @param array $data Array of new settings
3657 public function write_setting($data) {
3658 if (!is_array($data)) {
3661 unset($data['xxxxx']);
3663 foreach($data as $index) {
3664 $result |= 1 << $index;
3666 return ($this->config_write($this->name, $result) ? '' : get_string('errorsetting', 'admin'));
3670 * Return XHTML to display the control
3672 * @param array $data array of selected days
3673 * @param string $query
3674 * @return string XHTML for display (field + wrapping div(s)
3676 public function output_html($data, $query='') {
3677 // The order matters very much because of the implied numeric keys
3678 $days = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
3679 $return = '<table><thead><tr>';