MDL-10724 Added the help buttons to the item edit form
[moodle-pu.git] / lib / adminlib.php
blob5cd9fbb9e902738f93ab3511c57d92209f6cbb0d
1 <?php
3 /**
4 * adminlib.php - Contains functions that only administrators will ever need to use
6 * @author Martin Dougiamas and many others
7 * @version $Id$
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package moodlecore
12 /**
13 * Upgrade plugins
15 * @uses $db
16 * @uses $CFG
17 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
18 * @param string $dir The directory where the plugins are located (e.g. 'question/questiontypes')
19 * @param string $return The url to prompt the user to continue to
21 function upgrade_plugins($type, $dir, $return) {
22 global $CFG, $db;
24 $plugs = get_list_of_plugins($dir);
25 $updated_plugins = false;
26 $strpluginsetup = get_string('pluginsetup');
28 foreach ($plugs as $plug) {
30 $fullplug = $CFG->dirroot .'/'.$dir.'/'. $plug;
32 unset($plugin);
34 if (is_readable($fullplug .'/version.php')) {
35 include_once($fullplug .'/version.php'); // defines $plugin with version etc
36 } else {
37 continue; // Nothing to do.
40 $oldupgrade = false;
41 $newupgrade = false;
42 if (is_readable($fullplug . '/db/'. $CFG->dbtype . '.php')) {
43 include_once($fullplug . '/db/'. $CFG->dbtype . '.php'); // defines old upgrading function
44 $oldupgrade = true;
46 if (is_readable($fullplug . '/db/upgrade.php')) {
47 include_once($fullplug . '/db/upgrade.php'); // defines new upgrading function
48 $newupgrade = true;
51 if (!isset($plugin)) {
52 continue;
55 if (!empty($plugin->requires)) {
56 if ($plugin->requires > $CFG->version) {
57 $info = new object();
58 $info->pluginname = $plug;
59 $info->pluginversion = $plugin->version;
60 $info->currentmoodle = $CFG->version;
61 $info->requiremoodle = $plugin->requires;
62 if (!$updated_plugins) {
63 print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '',
64 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
66 upgrade_log_start();
67 notify(get_string('pluginrequirementsnotmet', 'error', $info));
68 $updated_plugins = true;
69 continue;
73 $plugin->name = $plug; // The name MUST match the directory
75 $pluginversion = $type.'_'.$plug.'_version';
77 if (!isset($CFG->$pluginversion)) {
78 set_config($pluginversion, 0);
81 if ($CFG->$pluginversion == $plugin->version) {
82 // do nothing
83 } else if ($CFG->$pluginversion < $plugin->version) {
84 if (!$updated_plugins) {
85 print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '',
86 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
88 $updated_plugins = true;
89 upgrade_log_start();
90 print_heading($dir.'/'. $plugin->name .' plugin needs upgrading');
91 $db->debug = true;
92 @set_time_limit(0); // To allow slow databases to complete the long SQL
94 if ($CFG->$pluginversion == 0) { // It's a new install of this plugin
95 /// Both old .sql files and new install.xml are supported
96 /// but we priorize install.xml (XMLDB) if present
97 $status = false;
98 if (file_exists($fullplug . '/db/install.xml')) {
99 $status = install_from_xmldb_file($fullplug . '/db/install.xml'); //New method
100 } else if (file_exists($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
101 $status = modify_database($fullplug .'/db/'. $CFG->dbtype .'.sql'); //Old method
102 } else {
103 $status = true;
106 $db->debug = false;
107 /// Continue with the instalation, roles and other stuff
108 if ($status) {
109 /// OK so far, now update the plugins record
110 set_config($pluginversion, $plugin->version);
112 /// Install capabilities
113 if (!update_capabilities($type.'/'.$plug)) {
114 error('Could not set up the capabilities for '.$plugin->name.'!');
116 /// Install events
117 events_update_definition($type.'/'.$plug);
119 /// Run local install function if there is one
120 if (is_readable($fullplug .'/lib.php')) {
121 include_once($fullplug .'/lib.php');
122 $installfunction = $plugin->name.'_install';
123 if (function_exists($installfunction)) {
124 if (! $installfunction() ) {
125 notify('Encountered a problem running install function for '.$module->name.'!');
130 notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
131 } else {
132 notify('Installing '. $plugin->name .' FAILED!');
134 } else { // Upgrade existing install
135 /// Run de old and new upgrade functions for the module
136 $oldupgrade_function = $type.'_'.$plugin->name .'_upgrade';
137 $newupgrade_function = 'xmldb_' . $type.'_'.$plugin->name .'_upgrade';
139 /// First, the old function if exists
140 $oldupgrade_status = true;
141 if ($oldupgrade && function_exists($oldupgrade_function)) {
142 $db->debug = true;
143 $oldupgrade_status = $oldupgrade_function($CFG->$pluginversion);
144 } else if ($oldupgrade) {
145 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
146 $fullplug . '/db/' . $CFG->dbtype . '.php');
149 /// Then, the new function if exists and the old one was ok
150 $newupgrade_status = true;
151 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
152 $db->debug = true;
153 $newupgrade_status = $newupgrade_function($CFG->$pluginversion);
154 } else if ($newupgrade) {
155 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
156 $fullplug . '/db/upgrade.php');
159 $db->debug=false;
160 /// Now analyze upgrade results
161 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
162 // OK so far, now update the plugins record
163 set_config($pluginversion, $plugin->version);
164 if (!update_capabilities($type.'/'.$plug)) {
165 error('Could not update '.$plugin->name.' capabilities!');
167 events_update_definition($type.'/'.$plug);
168 notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
169 } else {
170 notify('Upgrading '. $plugin->name .' from '. $CFG->$pluginversion .' to '. $plugin->version .' FAILED!');
173 echo '<hr />';
174 } else {
175 upgrade_log_start();
176 error('Version mismatch: '. $plugin->name .' can\'t downgrade '. $CFG->$pluginversion .' -> '. $plugin->version .' !');
180 upgrade_log_finish();
182 if ($updated_plugins) {
183 print_continue($return);
184 print_footer('none');
185 die;
190 * Find and check all modules and load them up or upgrade them if necessary
192 * @uses $db
193 * @uses $CFG
194 * @param string $return The url to prompt the user to continue to
195 * @todo Finish documenting this function
197 function upgrade_activity_modules($return) {
199 global $CFG, $db;
201 if (!$mods = get_list_of_plugins('mod') ) {
202 error('No modules installed!');
205 $updated_modules = false;
206 $strmodulesetup = get_string('modulesetup');
208 foreach ($mods as $mod) {
210 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
211 continue;
214 $fullmod = $CFG->dirroot .'/mod/'. $mod;
216 unset($module);
218 if ( is_readable($fullmod .'/version.php')) {
219 include_once($fullmod .'/version.php'); // defines $module with version etc
220 } else {
221 notify('Module '. $mod .': '. $fullmod .'/version.php was not readable');
222 continue;
225 $oldupgrade = false;
226 $newupgrade = false;
227 if ( is_readable($fullmod .'/db/' . $CFG->dbtype . '.php')) {
228 include_once($fullmod .'/db/' . $CFG->dbtype . '.php'); // defines old upgrading function
229 $oldupgrade = true;
231 if ( is_readable($fullmod . '/db/upgrade.php')) {
232 include_once($fullmod . '/db/upgrade.php'); // defines new upgrading function
233 $newupgrade = true;
236 if (!isset($module)) {
237 continue;
240 if (!empty($module->requires)) {
241 if ($module->requires > $CFG->version) {
242 $info = new object();
243 $info->modulename = $mod;
244 $info->moduleversion = $module->version;
245 $info->currentmoodle = $CFG->version;
246 $info->requiremoodle = $module->requires;
247 if (!$updated_modules) {
248 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
249 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
251 upgrade_log_start();
252 notify(get_string('modulerequirementsnotmet', 'error', $info));
253 $updated_modules = true;
254 continue;
258 $module->name = $mod; // The name MUST match the directory
260 include_once($fullmod.'/lib.php'); // defines upgrading and/or installing functions
262 if ($currmodule = get_record('modules', 'name', $module->name)) {
263 if ($currmodule->version == $module->version) {
264 // do nothing
265 } else if ($currmodule->version < $module->version) {
266 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
267 if (!$oldupgrade && !$newupgrade) {
268 notify('Upgrade files ' . $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype . '.php or ' .
269 $fullmod . '/db/upgrade.php were not readable');
270 continue;
272 if (!$updated_modules) {
273 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
274 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
276 upgrade_log_start();
277 print_heading($module->name .' module needs upgrading');
279 /// Run de old and new upgrade functions for the module
280 $oldupgrade_function = $module->name . '_upgrade';
281 $newupgrade_function = 'xmldb_' . $module->name . '_upgrade';
283 /// First, the old function if exists
284 $oldupgrade_status = true;
285 if ($oldupgrade && function_exists($oldupgrade_function)) {
286 $db->debug = true;
287 $oldupgrade_status = $oldupgrade_function($currmodule->version, $module);
288 } else if ($oldupgrade) {
289 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
290 $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype . '.php');
293 /// Then, the new function if exists and the old one was ok
294 $newupgrade_status = true;
295 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
296 $db->debug = true;
297 $newupgrade_status = $newupgrade_function($currmodule->version, $module);
298 } else if ($newupgrade) {
299 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
300 $mod . ': ' . $fullmod . '/db/upgrade.php');
303 $db->debug=false;
304 /// Now analyze upgrade results
305 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
306 // OK so far, now update the modules record
307 $module->id = $currmodule->id;
308 if (! update_record('modules', $module)) {
309 error('Could not update '. $module->name .' record in modules table!');
311 remove_dir($CFG->dataroot . '/cache', true); // flush cache
312 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
313 echo '<hr />';
314 } else {
315 notify('Upgrading '. $module->name .' from '. $currmodule->version .' to '. $module->version .' FAILED!');
318 /// Update the capabilities table?
319 if (!update_capabilities('mod/'.$module->name)) {
320 error('Could not update '.$module->name.' capabilities!');
322 events_update_definition('mod/'.$module->name);
324 $updated_modules = true;
326 } else {
327 upgrade_log_start();
328 error('Version mismatch: '. $module->name .' can\'t downgrade '. $currmodule->version .' -> '. $module->version .' !');
331 } else { // module not installed yet, so install it
332 if (!$updated_modules) {
333 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
334 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
336 upgrade_log_start();
337 print_heading($module->name);
338 $updated_modules = true;
339 $db->debug = true;
340 @set_time_limit(0); // To allow slow databases to complete the long SQL
342 /// Both old .sql files and new install.xml are supported
343 /// but we priorize install.xml (XMLDB) if present
344 if (file_exists($fullmod . '/db/install.xml')) {
345 $status = install_from_xmldb_file($fullmod . '/db/install.xml'); //New method
346 } else {
347 $status = modify_database($fullmod .'/db/'. $CFG->dbtype .'.sql'); //Old method
350 $db->debug = false;
352 /// Continue with the installation, roles and other stuff
353 if ($status) {
354 if ($module->id = insert_record('modules', $module)) {
356 /// Capabilities
357 if (!update_capabilities('mod/'.$module->name)) {
358 error('Could not set up the capabilities for '.$module->name.'!');
361 /// Events
362 events_update_definition('mod/'.$module->name);
364 /// Run local install function if there is one
365 $installfunction = $module->name.'_install';
366 if (function_exists($installfunction)) {
367 if (! $installfunction() ) {
368 notify('Encountered a problem running install function for '.$module->name.'!');
372 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
373 echo '<hr />';
374 } else {
375 error($module->name .' module could not be added to the module list!');
377 } else {
378 error($module->name .' tables could NOT be set up successfully!');
382 /// Check submodules of this module if necessary
384 $submoduleupgrade = $module->name.'_upgrade_submodules';
385 if (function_exists($submoduleupgrade)) {
386 $submoduleupgrade();
390 /// Run any defaults or final code that is necessary for this module
392 if ( is_readable($fullmod .'/defaults.php')) {
393 // Insert default values for any important configuration variables
394 unset($defaults);
395 include_once($fullmod .'/defaults.php');
396 if (!empty($defaults)) {
397 foreach ($defaults as $name => $value) {
398 if (!isset($CFG->$name)) {
399 set_config($name, $value);
406 upgrade_log_finish(); // finish logging if started
408 if ($updated_modules) {
409 print_continue($return);
410 print_footer('none');
411 die;
416 * This function will return FALSE if the lock fails to be set (ie, if it's already locked)
418 * @param string $name ?
419 * @param bool $value ?
420 * @param int $staleafter ?
421 * @param bool $clobberstale ?
422 * @todo Finish documenting this function
424 function set_cron_lock($name,$value=true,$staleafter=7200,$clobberstale=false) {
426 if (empty($name)) {
427 mtrace("Tried to get a cron lock for a null fieldname");
428 return false;
431 if (empty($value)) {
432 set_config($name,0);
433 return true;
436 if ($config = get_record('config','name',$name)) {
437 if (empty($config->value)) {
438 set_config($name,time());
439 } else {
440 // check for stale.
441 if ((time() - $staleafter) > $config->value) {
442 mtrace("STALE LOCKFILE FOR $name - was $config->value");
443 if (!empty($clobberstale)) {
444 set_config($name,time());
445 return true;
447 } else {
448 return false; // was not stale - ie, we're ok to still be running.
452 else {
453 set_config($name,time());
455 return true;
458 function print_progress($done, $total, $updatetime=5, $sleeptime=1, $donetext='') {
459 static $starttime;
460 static $lasttime;
462 if ($total < 2) { // No need to show anything
463 return;
466 if (empty($starttime)) {
467 $starttime = $lasttime = time();
468 $lasttime = $starttime - $updatetime;
469 echo '<table width="500" cellpadding="0" cellspacing="0" align="center"><tr><td width="500">';
470 echo '<div id="bar'.$total.'" style="border-style:solid;border-width:1px;width:500px;height:50px;">';
471 echo '<div id="slider'.$total.'" style="border-style:solid;border-width:1px;height:48px;width:10px;background-color:green;"></div>';
472 echo '</div>';
473 echo '<div id="text'.$total.'" align="center" style="width:500px;"></div>';
474 echo '</td></tr></table>';
475 echo '</div>';
478 $now = time();
480 if ($done && (($now - $lasttime) >= $updatetime)) {
481 $elapsedtime = $now - $starttime;
482 $projectedtime = (int)(((float)$total / (float)$done) * $elapsedtime) - $elapsedtime;
483 $percentage = format_float((float)$done / (float)$total, 2);
484 $width = (int)(500 * $percentage);
486 if ($projectedtime > 10) {
487 $projectedtext = ' Ending: '.format_time($projectedtime);
488 } else {
489 $projectedtext = '';
492 echo '<script>';
493 echo 'document.getElementById("text'.$total.'").innerHTML = "'.addslashes($donetext).' ('.$done.'/'.$total.') '.$projectedtext.'";'."\n";
494 echo 'document.getElementById("slider'.$total.'").style.width = \''.$width.'px\';'."\n";
495 echo '</script>';
497 $lasttime = $now;
498 sleep($sleeptime);
502 function upgrade_get_javascript() {
503 global $CFG;
505 if (!empty($_SESSION['installautopilot'])) {
506 $linktoscrolltoerrors = '<script type="text/javascript">var installautopilot = true;</script>'."\n";
507 } else {
508 $linktoscrolltoerrors = '<script type="text/javascript">var installautopilot = false;</script>'."\n";
510 $linktoscrolltoerrors .= '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>';
512 return $linktoscrolltoerrors;
515 function create_admin_user() {
516 global $CFG, $USER;
518 if (empty($CFG->rolesactive)) { // No admin user yet.
520 $user = new object();
521 $user->auth = 'manual';
522 $user->firstname = get_string('admin');
523 $user->lastname = get_string('user');
524 $user->username = 'admin';
525 $user->password = hash_internal_user_password('admin');
526 $user->email = 'root@localhost';
527 $user->confirmed = 1;
528 $user->mnethostid = $CFG->mnet_localhost_id;
529 $user->lang = $CFG->lang;
530 $user->maildisplay = 1;
531 $user->timemodified = time();
533 if (!$user->id = insert_record('user', $user)) {
534 error('SERIOUS ERROR: Could not create admin user record !!!');
537 if (!$user = get_record('user', 'id', $user->id)) { // Double check.
538 error('User ID was incorrect (can\'t find it)');
541 // Assign the default admin roles to the new user.
542 if (!$adminroles = get_roles_with_capability('moodle/legacy:admin', CAP_ALLOW)) {
543 error('No admin role could be found');
545 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
546 foreach ($adminroles as $adminrole) {
547 role_assign($adminrole->id, $user->id, 0, $sitecontext->id);
550 set_config('rolesactive', 1);
552 // Log the user in.
553 $USER = get_complete_user_data('username', 'admin');
554 $USER->newadminuser = 1;
555 load_all_capabilities();
557 redirect("$CFG->wwwroot/user/editadvanced.php?id=$user->id"); // Edit thyself
558 } else {
559 error('Can not create admin!');
563 ////////////////////////////////////////////////
564 /// upgrade logging functions
565 ////////////////////////////////////////////////
567 $upgradeloghandle = false;
568 $upgradelogbuffer = '';
569 // I did not find out how to use static variable in callback function,
570 // the problem was that I could not flush the static buffer :-(
571 global $upgradeloghandle, $upgradelogbuffer;
574 * Check if upgrade is already running.
576 * If anything goes wrong due to missing call to upgrade_log_finish()
577 * just restart the browser.
579 * @param string warning message indicating upgrade is already running
580 * @param int page reload timeout
582 function upgrade_check_running($message, $timeout) {
583 if (!empty($_SESSION['upgraderunning'])) {
584 print_header();
585 redirect(me(), $message, $timeout);
590 * Start logging of output into file (if not disabled) and
591 * prevent aborting and concurrent execution of upgrade script.
593 * Please note that you can not write into session variables after calling this function!
595 * This function may be called repeatedly.
597 function upgrade_log_start() {
598 global $CFG, $upgradeloghandle;
600 if (!empty($_SESSION['upgraderunning'])) {
601 return; // logging already started
604 @ignore_user_abort(true); // ignore if user stops or otherwise aborts page loading
605 $_SESSION['upgraderunning'] = 1; // set upgrade indicator
606 if (empty($CFG->dbsessions)) { // workaround for bug in adodb, db session can not be restarted
607 session_write_close(); // from now on user can reload page - will be displayed warning
609 make_upload_directory('upgradelogs');
610 ob_start('upgrade_log_callback', 2); // function for logging to disk; flush each line of text ASAP
611 register_shutdown_function('upgrade_log_finish'); // in case somebody forgets to stop logging
615 * Terminate logging of output, flush all data, allow script aborting
616 * and reopen session for writing. Function error() does terminate the logging too.
618 * Please make sure that each upgrade_log_start() is properly terminated by
619 * this function or error().
621 * This function may be called repeatedly.
623 function upgrade_log_finish() {
624 global $CFG, $upgradeloghandle, $upgradelogbuffer;
626 if (empty($_SESSION['upgraderunning'])) {
627 return; // logging already terminated
630 @ob_end_flush();
631 if ($upgradelogbuffer !== '') {
632 @fwrite($upgradeloghandle, $upgradelogbuffer);
633 $upgradelogbuffer = '';
635 if ($upgradeloghandle and ($upgradeloghandle !== 'error')) {
636 @fclose($upgradeloghandle);
637 $upgradeloghandle = false;
639 if (empty($CFG->dbsessions)) {
640 @session_start(); // ignore header errors, we only need to reopen session
642 $_SESSION['upgraderunning'] = 0; // clear upgrade indicator
643 if (connection_aborted()) {
644 die;
646 @ignore_user_abort(false);
650 * Callback function for logging into files. Not more than one file is created per minute,
651 * upgrade session (terminated by upgrade_log_finish()) is always stored in one file.
653 * This function must not output any characters or throw warnigns and errors!
655 function upgrade_log_callback($string) {
656 global $CFG, $upgradeloghandle, $upgradelogbuffer;
658 if (empty($CFG->disableupgradelogging) and ($string != '') and ($upgradeloghandle !== 'error')) {
659 if ($upgradeloghandle or ($upgradeloghandle = @fopen($CFG->dataroot.'/upgradelogs/upg_'.date('Ymd-Hi').'.html', 'a'))) {
660 $upgradelogbuffer .= $string;
661 if (strlen($upgradelogbuffer) > 2048) { // 2kB write buffer
662 @fwrite($upgradeloghandle, $upgradelogbuffer);
663 $upgradelogbuffer = '';
665 } else {
666 $upgradeloghandle = 'error';
669 return $string;
673 * Try to verify that dataroot is not accessible from web.
674 * It is not 100% correct but might help to reduce number of vulnerable sites.
676 * Protection from httpd.conf and .htaccess is not detected properly.
678 function is_dataroot_insecure() {
679 global $CFG;
681 $siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround
683 $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1);
684 $rp = strrev(trim($rp, '/'));
685 $rp = explode('/', $rp);
686 foreach($rp as $r) {
687 if (strpos($siteroot, '/'.$r.'/') === 0) {
688 $siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory
689 } else {
690 break; // probably alias root
694 $siteroot = strrev($siteroot);
695 $dataroot = str_replace('\\', '/', $CFG->dataroot.'/');
697 if (strpos($dataroot, $siteroot) === 0) {
698 return true;
700 return false;
703 /// =============================================================================================================
704 /// administration tree classes and functions
707 // n.b. documentation is still in progress for this code
709 /// INTRODUCTION
711 /// This file performs the following tasks:
712 /// -it defines the necessary objects and interfaces to build the Moodle
713 /// admin hierarchy
714 /// -it defines the admin_externalpage_setup(), admin_externalpage_print_header(),
715 /// and admin_externalpage_print_footer() functions used on admin pages
717 /// ADMIN_SETTING OBJECTS
719 /// Moodle settings are represented by objects that inherit from the admin_setting
720 /// class. These objects encapsulate how to read a setting, how to write a new value
721 /// to a setting, and how to appropriately display the HTML to modify the setting.
723 /// ADMIN_SETTINGPAGE OBJECTS
725 /// The admin_setting objects are then grouped into admin_settingpages. The latter
726 /// appear in the Moodle admin tree block. All interaction with admin_settingpage
727 /// objects is handled by the admin/settings.php file.
729 /// ADMIN_EXTERNALPAGE OBJECTS
731 /// There are some settings in Moodle that are too complex to (efficiently) handle
732 /// with admin_settingpages. (Consider, for example, user management and displaying
733 /// lists of users.) In this case, we use the admin_externalpage object. This object
734 /// places a link to an external PHP file in the admin tree block.
736 /// If you're using an admin_externalpage object for some settings, you can take
737 /// advantage of the admin_externalpage_* functions. For example, suppose you wanted
738 /// to add a foo.php file into admin. First off, you add the following line to
739 /// admin/settings/first.php (at the end of the file) or to some other file in
740 /// admin/settings:
742 /// $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'),
743 /// $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission'));
745 /// Next, in foo.php, your file structure would resemble the following:
747 /// require_once('.../config.php');
748 /// require_once($CFG->libdir.'/adminlib.php');
749 /// admin_externalpage_setup('foo');
750 /// // functionality like processing form submissions goes here
751 /// admin_externalpage_print_header();
752 /// // your HTML goes here
753 /// admin_externalpage_print_footer();
755 /// The admin_externalpage_setup() function call ensures the user is logged in,
756 /// and makes sure that they have the proper role permission to access the page.
758 /// The admin_externalpage_print_header() function prints the header (it figures
759 /// out what category and subcategories the page is classified under) and ensures
760 /// that you're using the admin pagelib (which provides the admin tree block and
761 /// the admin bookmarks block).
763 /// The admin_externalpage_print_footer() function properly closes the tables
764 /// opened up by the admin_externalpage_print_header() function and prints the
765 /// standard Moodle footer.
767 /// ADMIN_CATEGORY OBJECTS
769 /// Above and beyond all this, we have admin_category objects. These objects
770 /// appear as folders in the admin tree block. They contain admin_settingpage's,
771 /// admin_externalpage's, and other admin_category's.
773 /// OTHER NOTES
775 /// admin_settingpage's, admin_externalpage's, and admin_category's all inherit
776 /// from part_of_admin_tree (a pseudointerface). This interface insists that
777 /// a class has a check_access method for access permissions, a locate method
778 /// used to find a specific node in the admin tree, and a path method used
779 /// to determine the path to a specific node in the $ADMIN tree.
781 /// admin_category's inherit from parentable_part_of_admin_tree. This pseudo-
782 /// interface ensures that the class implements a recursive add function which
783 /// accepts a part_of_admin_tree object and searches for the proper place to
784 /// put it. parentable_part_of_admin_tree implies part_of_admin_tree.
786 /// Please note that the $this->name field of any part_of_admin_tree must be
787 /// UNIQUE throughout the ENTIRE admin tree.
789 /// The $this->name field of an admin_setting object (which is *not* part_of_
790 /// admin_tree) must be unique on the respective admin_settingpage where it is
791 /// used.
794 /// MISCELLANEOUS STUFF (used by classes defined below) ///////////////////////
795 include_once($CFG->dirroot . '/backup/lib.php');
797 /// CLASS DEFINITIONS /////////////////////////////////////////////////////////
800 * Pseudointerface for anything appearing in the admin tree
802 * The pseudointerface that is implemented by anything that appears in the admin tree
803 * block. It forces inheriting classes to define a method for checking user permissions
804 * and methods for finding something in the admin tree.
806 * @author Vincenzo K. Marcovecchio
807 * @package admin
809 class part_of_admin_tree {
812 * Finds a named part_of_admin_tree.
814 * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree
815 * and not parentable_part_of_admin_tree, then this function should only check if
816 * $this->name matches $name. If it does, it should return a reference to $this,
817 * otherwise, it should return a reference to NULL.
819 * If a class inherits parentable_part_of_admin_tree, this method should be called
820 * recursively on all child objects (assuming, of course, the parent object's name
821 * doesn't match the search criterion).
823 * @param string $name The internal name of the part_of_admin_tree we're searching for.
824 * @return mixed An object reference or a NULL reference.
826 function &locate($name) {
827 trigger_error('Admin class does not implement method <strong>locate()</strong>', E_USER_WARNING);
828 return;
832 * Removes named part_of_admin_tree.
834 * @param string $name The internal name of the part_of_admin_tree we want to remove.
835 * @return bool success.
837 function prune($name) {
838 trigger_error('Admin class does not implement method <strong>prune()</strong>', E_USER_WARNING);
839 return;
843 * Verifies current user's access to this part_of_admin_tree.
845 * Used to check if the current user has access to this part of the admin tree or
846 * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree,
847 * then this method is usually just a call to has_capability() in the site context.
849 * If a class inherits parentable_part_of_admin_tree, this method should return the
850 * logical OR of the return of check_access() on all child objects.
852 * @return bool True if the user has access, false if she doesn't.
854 function check_access() {
855 trigger_error('Admin class does not implement method <strong>check_access()</strong>', E_USER_WARNING);
856 return;
860 * Mostly usefull for removing of some parts of the tree in admin tree block.
862 * @return True is hidden from normal list view
864 function is_hidden() {
865 trigger_error('Admin class does not implement method <strong>is_hidden()</strong>', E_USER_WARNING);
866 return;
870 * Determines the path to $name in the admin tree.
872 * Used to determine the path to $name in the admin tree. If a class inherits only
873 * part_of_admin_tree and not parentable_part_of_admin_tree, then this method should
874 * check if $this->name matches $name. If it does, $name is pushed onto the $path
875 * array (at the end), and $path should be returned. If it doesn't, NULL should be
876 * returned.
878 * If a class inherits parentable_part_of_admin_tree, it should do the above, but not
879 * return NULL on failure. Instead, it pushes $this->name onto $path, and then
880 * recursively calls path() on its child objects. If any are non-NULL, it should
881 * return $path (being certain that the last element of $path is equal to $name).
882 * If they are all NULL, it returns NULL.
884 * @param string $name The internal name of the part_of_admin_tree we're searching for.
885 * @param array $path Not used on external calls. Defaults to empty array.
886 * @return mixed If found, an array containing the internal names of each part_of_admin_tree that leads to $name. If not found, NULL.
888 function path($name, $path = array()) {
889 trigger_error('Admin class does not implement method <strong>path()</strong>', E_USER_WARNING);
890 return;
895 * Pseudointerface implemented by any part_of_admin_tree that has children.
897 * The pseudointerface implemented by any part_of_admin_tree that can be a parent
898 * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart
899 * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods
900 * include an add method for adding other part_of_admin_tree objects as children.
902 * @author Vincenzo K. Marcovecchio
903 * @package admin
905 class parentable_part_of_admin_tree extends part_of_admin_tree {
908 * Adds a part_of_admin_tree object to the admin tree.
910 * Used to add a part_of_admin_tree object to this object or a child of this
911 * object. $something should only be added if $destinationname matches
912 * $this->name. If it doesn't, add should be called on child objects that are
913 * also parentable_part_of_admin_tree's.
915 * @param string $destinationname The internal name of the new parent for $something.
916 * @param part_of_admin_tree &$something The object to be added.
917 * @return bool True on success, false on failure.
919 function add($destinationname, &$something) {
920 trigger_error('Admin class does not implement method <strong>add()</strong>', E_USER_WARNING);
921 return;
927 * The object used to represent folders (a.k.a. categories) in the admin tree block.
929 * Each admin_category object contains a number of part_of_admin_tree objects.
931 * @author Vincenzo K. Marcovecchio
932 * @package admin
934 class admin_category extends parentable_part_of_admin_tree {
937 * @var mixed An array of part_of_admin_tree objects that are this object's children
939 var $children;
942 * @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
944 var $name;
947 * @var string The displayed name for this category. Usually obtained through get_string()
949 var $visiblename;
952 * @var bool Should this category be hidden in admin tree block?
954 var $hidden;
956 // constructor for an empty admin category
957 // $name is the internal name of the category. it MUST be unique in the entire hierarchy
958 // $visiblename is the displayed name of the category. use a get_string for this
961 * Constructor for an empty admin category
963 * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
964 * @param string $visiblename The displayed named for this category. Usually obtained through get_string()
965 * @param bool $hidden hide category in admin tree block
966 * @return mixed Returns the new object.
968 function admin_category($name, $visiblename, $hidden = false) {
969 $this->children = array();
970 $this->name = $name;
971 $this->visiblename = $visiblename;
972 $this->hidden = $hidden;
976 * Finds the path to the part_of_admin_tree called $name.
978 * @param string $name The internal name that we're searching for.
979 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
980 * @return mixed An array of internal names that leads to $name, or NULL if not found.
982 function path($name, $path = array()) {
984 $path[count($path)] = $this->name;
986 if ($this->name == $name) {
987 return $path;
990 foreach($this->children as $child) {
991 if ($return = $child->path($name, $path)) {
992 return $return;
996 return NULL;
1001 * Returns a reference to the part_of_admin_tree object with internal name $name.
1003 * @param string $name The internal name of the object we want.
1004 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1006 function &locate($name) {
1008 if ($this->name == $name) {
1009 return $this;
1012 foreach($this->children as $child) {
1013 if ($return =& $child->locate($name)) {
1014 return $return;
1017 $return = NULL;
1018 return $return;
1022 * Removes part_of_admin_tree object with internal name $name.
1024 * @param string $name The internal name of the object we want to remove.
1025 * @return bool success
1027 function prune($name) {
1029 if ($this->name == $name) {
1030 return false; //can not remove itself
1033 foreach($this->children as $precedence => $child) {
1034 if ($child->name == $name) {
1035 // found it!
1036 unset($this->children[$precedence]);
1037 return true;
1039 if ($this->children[$precedence]->prune($name)) {
1040 return true;
1043 return false;
1047 * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object.
1049 * @param string $destinationame The internal name of the immediate parent that we want for &$something.
1050 * @param mixed &$something A part_of_admin_tree object to be added.
1051 * @param int $precedence The precedence of &$something when displayed. Smaller numbers mean it'll be displayed higher up in the admin menu. Defaults to '', meaning "next available position".
1052 * @return bool True if successfully added, false if &$something is not a part_of_admin_tree or if $name is not found.
1054 function add($destinationname, &$something, $precedence = '') {
1056 if (!is_a($something, 'part_of_admin_tree')) {
1057 return false;
1060 if ($destinationname == $this->name) {
1061 if ($precedence === '') {
1062 $this->children[] = $something;
1063 } else {
1064 if (isset($this->children[$precedence])) { // this should never, ever be triggered in a release version of moodle.
1065 echo ('<font style="color: red;">There is a precedence conflict in the category ' . $this->name . '. The object named ' . $something->name . ' is overwriting the object named ' . $this->children[$precedence]->name . '.</font><br />');
1067 $this->children[$precedence] = $something;
1069 return true;
1072 unset($entries);
1074 $entries = array_keys($this->children);
1076 foreach($entries as $entry) {
1077 $child =& $this->children[$entry];
1078 if (is_a($child, 'parentable_part_of_admin_tree')) {
1079 if ($child->add($destinationname, $something, $precedence)) {
1080 return true;
1085 return false;
1090 * Checks if the user has access to anything in this category.
1092 * @return bool True if the user has access to atleast one child in this category, false otherwise.
1094 function check_access() {
1096 $return = false;
1097 foreach ($this->children as $child) {
1098 $return = $return || $child->check_access();
1101 return $return;
1106 * Is this category hidden in admin tree block?
1108 * @return bool True if hidden
1110 function is_hidden() {
1111 return $this->hidden;
1116 * Links external PHP pages into the admin tree.
1118 * See detailed usage example at the top of this document (adminlib.php)
1120 * @author Vincenzo K. Marcovecchio
1121 * @package admin
1123 class admin_externalpage extends part_of_admin_tree {
1126 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1128 var $name;
1131 * @var string The displayed name for this external page. Usually obtained through get_string().
1133 var $visiblename;
1136 * @var string The external URL that we should link to when someone requests this external page.
1138 var $url;
1141 * @var string The role capability/permission a user must have to access this external page.
1143 var $req_capability;
1146 * @var object The context in which capability/permission should be checked, default is site context.
1148 var $context;
1151 * @var bool hidden in admin tree block.
1153 var $hidden;
1156 * Constructor for adding an external page into the admin tree.
1158 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1159 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1160 * @param string $url The external URL that we should link to when someone requests this external page.
1161 * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
1163 function admin_externalpage($name, $visiblename, $url, $req_capability = 'moodle/site:config', $hidden=false, $context=NULL) {
1164 $this->name = $name;
1165 $this->visiblename = $visiblename;
1166 $this->url = $url;
1167 if (is_array($req_capability)) {
1168 $this->req_capability = $req_capability;
1169 } else {
1170 $this->req_capability = array($req_capability);
1172 $this->hidden = $hidden;
1173 $this->context = $context;
1177 * Finds the path to the part_of_admin_tree called $name.
1179 * @param string $name The internal name that we're searching for.
1180 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
1181 * @return mixed An array of internal names that leads to $name, or NULL if not found.
1183 function path($name, $path = array()) {
1184 if ($name == $this->name) {
1185 array_push($path, $this->name);
1186 return $path;
1187 } else {
1188 return NULL;
1193 * Returns a reference to the part_of_admin_tree object with internal name $name.
1195 * @param string $name The internal name of the object we want.
1196 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1198 function &locate($name) {
1199 $return = ($this->name == $name ? $this : NULL);
1200 return $return;
1203 function prune($name) {
1204 return false;
1208 * Determines if the current user has access to this external page based on $this->req_capability.
1210 * @uses CONTEXT_SYSTEM
1211 * @uses SITEID
1212 * @return bool True if user has access, false otherwise.
1214 function check_access() {
1215 if (!get_site()) {
1216 return true; // no access check before site is fully set up
1218 $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context;
1219 foreach($this->req_capability as $cap) {
1220 if (has_capability($cap, $context)) {
1221 return true;
1224 return false;
1228 * Is this external page hidden in admin tree block?
1230 * @return bool True if hidden
1232 function is_hidden() {
1233 return $this->hidden;
1239 * Used to group a number of admin_setting objects into a page and add them to the admin tree.
1241 * @author Vincenzo K. Marcovecchio
1242 * @package admin
1244 class admin_settingpage extends part_of_admin_tree {
1247 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1249 var $name;
1252 * @var string The displayed name for this external page. Usually obtained through get_string().
1254 var $visiblename;
1256 * @var mixed An array of admin_setting objects that are part of this setting page.
1258 var $settings;
1261 * @var string The role capability/permission a user must have to access this external page.
1263 var $req_capability;
1266 * @var object The context in which capability/permission should be checked, default is site context.
1268 var $context;
1271 * @var bool hidden in admin tree block.
1273 var $hidden;
1275 // see admin_category
1276 function path($name, $path = array()) {
1277 if ($name == $this->name) {
1278 array_push($path, $this->name);
1279 return $path;
1280 } else {
1281 return NULL;
1285 // see admin_category
1286 function &locate($name) {
1287 $return = ($this->name == $name ? $this : NULL);
1288 return $return;
1291 function prune($name) {
1292 return false;
1295 // see admin_externalpage
1296 function admin_settingpage($name, $visiblename, $req_capability = 'moodle/site:config', $hidden=false, $context=NULL) {
1297 global $CFG;
1298 $this->settings = new stdClass();
1299 $this->name = $name;
1300 $this->visiblename = $visiblename;
1301 if (is_array($req_capability)) {
1302 $this->req_capability = $req_capability;
1303 } else {
1304 $this->req_capability = array($req_capability);
1306 $this->hidden = false;
1307 $this->context = $context;
1310 // 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
1311 // n.b. each admin_setting in an admin_settingpage must have a unique internal name
1312 // &$setting is the admin_setting object you want to add
1313 // returns true if successful, false if not (will fail if &$setting is an admin_setting or child thereof)
1314 function add(&$setting) {
1315 if (is_a($setting, 'admin_setting')) {
1316 $this->settings->{$setting->name} =& $setting;
1317 return true;
1319 return false;
1322 // see admin_externalpage
1323 function check_access() {
1324 if (!get_site()) {
1325 return true; // no access check before site is fully set up
1327 $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context;
1328 foreach($this->req_capability as $cap) {
1329 if (has_capability($cap, $context)) {
1330 return true;
1333 return false;
1336 // outputs this page as html in a table (suitable for inclusion in an admin pagetype)
1337 // returns a string of the html
1338 function output_html() {
1339 $return = '<fieldset>' . "\n";
1340 $return .= '<div class="clearer"><!-- --></div>' . "\n";
1341 foreach($this->settings as $setting) {
1342 $return .= $setting->output_html();
1344 $return .= '</fieldset>';
1345 return $return;
1348 // writes settings (the ones that have been added to this admin_settingpage) to the database, or wherever else they're supposed to be written to
1349 // -- calls write_setting() to each child setting, sending it only the data that matches each setting's internal name
1350 // $data should be the result from data_submitted()
1351 // returns an empty string if everything went well, otherwise returns a printable error string (that's language-specific)
1352 function write_settings($data) {
1353 $return = '';
1354 foreach($this->settings as $setting) {
1355 if (isset($data['s_' . $setting->name])) {
1356 $return .= $setting->write_setting($data['s_' . $setting->name]);
1357 } else {
1358 $return .= $setting->write_setting('');
1361 return $return;
1365 * Is this settigns page hidden in admin tree block?
1367 * @return bool True if hidden
1369 function is_hidden() {
1370 return $this->hidden;
1376 // read & write happens at this level; no authentication
1377 class admin_setting {
1379 var $name;
1380 var $visiblename;
1381 var $description;
1382 var $defaultsetting;
1384 function admin_setting($name, $visiblename, $description, $defaultsetting) {
1385 $this->name = $name;
1386 $this->visiblename = $visiblename;
1387 $this->description = $description;
1388 $this->defaultsetting = $defaultsetting;
1391 function get_setting() {
1392 return NULL; // has to be overridden
1395 function write_setting($data) {
1396 return; // has to be overridden
1399 function output_html() {
1400 return; // has to be overridden
1406 class admin_setting_configtext extends admin_setting {
1408 var $paramtype;
1410 function admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW) {
1411 $this->paramtype = $paramtype;
1412 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1415 // returns a string or NULL
1416 function get_setting() {
1417 global $CFG;
1418 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1421 // $data is a string
1422 function write_setting($data) {
1423 if (!$this->validate($data)) {
1424 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
1426 return (set_config($this->name,$data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1429 function validate($data) {
1430 if (is_string($this->paramtype)) {
1431 return preg_match($this->paramtype, $data);
1432 } else if ($this->paramtype === PARAM_RAW) {
1433 return true;
1434 } else {
1435 $cleaned = clean_param($data, $this->paramtype);
1436 return ("$data" == "$cleaned"); // implicit conversion to string is needed to do exact comparison
1440 function output_html() {
1441 if ($this->get_setting() === NULL) {
1442 $current = $this->defaultsetting;
1443 } else {
1444 $current = $this->get_setting();
1446 return format_admin_setting($this->name, $this->visiblename,
1447 '<input type="text" class="form-text" id="id_s_'.$this->name.'" name="s_'.$this->name.'" value="'.s($current).'" />',
1448 $this->description);
1453 class admin_setting_configpasswordunmask extends admin_setting_configtext {
1455 function admin_setting_configpasswordunmask($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW) {
1456 parent::admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype);
1459 function output_html() {
1460 if ($this->get_setting() === NULL) {
1461 $current = $this->defaultsetting;
1462 } else {
1463 $current = $this->get_setting();
1465 $id = 'id_s_'.$this->name;
1466 $unmask = get_string('unmaskpassword', 'form');
1467 $unmaskjs = '<script type="text/javascript">
1468 //<![CDATA[
1469 document.write(\'<div class="unmask"><input id="'.$id.'unmask" value="1" type="checkbox" onclick="unmaskPassword(\\\''.$id.'\\\')"/><label for="'.$id.'unmask">'.addslashes_js($unmask).'<\/label><\/div>\');
1470 //]]>
1471 </script>';
1472 return format_admin_setting($this->name, $this->visiblename,
1473 '<input type="password" class="form-text" id="id_s_'.$this->name.'" name="s_'.$this->name.'" value="'.s($current).'" />'.$unmaskjs,
1474 $this->description);
1479 class admin_setting_configcheckbox extends admin_setting {
1481 function admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting) {
1482 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1485 function get_setting() {
1486 global $CFG;
1487 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1490 function write_setting($data) {
1491 if ($data == '1') {
1492 return (set_config($this->name,1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1493 } else {
1494 return (set_config($this->name,0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1498 function output_html() {
1499 if ($this->get_setting() === NULL) {
1500 $current = $this->defaultsetting;
1501 } else {
1502 $current = $this->get_setting();
1504 return format_admin_setting($this->name, $this->visiblename,
1505 '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'" name="s_'. $this->name .'" value="1" ' . ($current == true ? 'checked="checked"' : '') . ' />',
1506 $this->description);
1511 class admin_setting_configselect extends admin_setting {
1513 var $choices;
1515 function admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices) {
1516 $this->choices = $choices;
1517 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1520 function get_setting() {
1521 global $CFG;
1522 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1525 function write_setting($data) {
1526 // check that what we got was in the original choices
1527 // or that the data is the default setting - needed during install when choices can not be constructed yet
1528 if ($data != $this->defaultsetting and ! in_array($data, array_keys($this->choices))) {
1529 return 'Error setting ' . $this->visiblename . '<br />';
1532 return (set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1535 function output_html() {
1536 if ($this->get_setting() === NULL) {
1537 $current = $this->defaultsetting;
1538 } else {
1539 $current = $this->get_setting();
1541 $return = '<select class="form-select" id="id_s_'.$this->name.'" name="s_' . $this->name .'">';
1542 foreach ($this->choices as $key => $value) {
1543 // the string cast is needed because key may be integer - 0 is equal to most strings!
1544 $return .= '<option value="'.$key.'"'.((string)$key==$current ? ' selected="selected"' : '').'>'.$value.'</option>';
1546 $return .= '</select>';
1548 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
1553 // this is a liiitle bit messy. we're using two selects, but we're returning them as an array named after $name (so we only use $name2
1554 // internally for the setting)
1555 class admin_setting_configtime extends admin_setting {
1557 var $name2;
1558 var $choices;
1559 var $choices2;
1561 function admin_setting_configtime($hoursname, $minutesname, $visiblename, $description, $defaultsetting) {
1562 $this->name2 = $minutesname;
1563 $this->choices = array();
1564 for ($i = 0; $i < 24; $i++) {
1565 $this->choices[$i] = $i;
1567 $this->choices2 = array();
1568 for ($i = 0; $i < 60; $i += 5) {
1569 $this->choices2[$i] = $i;
1571 parent::admin_setting($hoursname, $visiblename, $description, $defaultsetting);
1574 function get_setting() {
1575 global $CFG;
1576 return (isset($CFG->{$this->name}) && isset($CFG->{$this->name2}) ? array('h' => $CFG->{$this->name}, 'm' => $CFG->{$this->name2}) : NULL);
1579 function write_setting($data) {
1580 // check that what we got was in the original choices
1581 if (!(in_array($data['h'], array_keys($this->choices)) && in_array($data['m'], array_keys($this->choices2)))) {
1582 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1585 return (set_config($this->name, $data['h']) && set_config($this->name2, $data['m']) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1588 function output_html() {
1589 if ($this->get_setting() === NULL) {
1590 $currentsetting = $this->defaultsetting;
1591 } else {
1592 $currentsetting = $this->get_setting();
1594 $return = '<div class="form-group">'.
1595 '<select class="form-select" id="id_s_'.$this->name.'h" name="s_' . $this->name .'[h]">';
1596 foreach ($this->choices as $key => $value) {
1597 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['h'] ? ' selected="selected"' : '') . '>' . $value . '</option>';
1599 $return .= '</select>:<select class="form-select" id="id_s_'.$this->name.'m" name="s_' . $this->name . '[m]">';
1600 foreach ($this->choices2 as $key => $value) {
1601 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['m'] ? ' selected="selected"' : '') . '>' . $value . '</option>';
1603 $return .= '</select></div>';
1604 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
1609 class admin_setting_configmultiselect extends admin_setting_configselect {
1611 function admin_setting_configmultiselect($name, $visiblename, $description, $defaultsetting, $choices) {
1612 parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
1615 function get_setting() {
1616 global $CFG;
1617 if (isset($CFG->{$this->name})) {
1618 if ($CFG->{$this->name}) {
1619 return explode(',', $CFG->{$this->name});
1620 } else {
1621 return array();
1623 } else {
1624 return NULL;
1628 function write_setting($data) {
1629 if (empty($data)) {
1630 $data = array();
1632 foreach ($data as $datum) {
1633 if (! in_array($datum, array_keys($this->choices))) {
1634 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1638 return (set_config($this->name, implode(',',$data)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1641 function output_html() {
1642 if ($this->get_setting() === NULL) {
1643 $currentsetting = $this->defaultsetting;
1644 if (!$currentsetting) {
1645 $currentsetting = array();
1647 } else {
1648 $currentsetting = $this->get_setting();
1650 $return = '<select class="form-select" id="id_s_'.$this->name.'" name="s_' . $this->name .'[]" size="10" multiple="multiple">';
1651 foreach ($this->choices as $key => $value) {
1652 $return .= '<option value="' . $key . '"' . (in_array($key,$currentsetting) ? ' selected="selected"' : '') . '>' . $value . '</option>';
1654 $return .= '</select>';
1655 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
1660 class admin_setting_special_adminseesall extends admin_setting_configcheckbox {
1662 function admin_setting_special_adminseesall() {
1663 $name = 'calendar_adminseesall';
1664 $visiblename = get_string('adminseesall', 'admin');
1665 $description = get_string('helpadminseesall', 'admin');
1666 parent::admin_setting($name, $visiblename, $description, 0);
1669 function write_setting($data) {
1670 global $SESSION;
1671 unset($SESSION->cal_courses_shown);
1672 parent::write_setting($data);
1676 class admin_setting_sitesetselect extends admin_setting_configselect {
1678 var $id;
1680 function admin_setting_sitesetselect($name, $visiblename, $description, $defaultsetting, $choices) {
1682 $this->id = SITEID;
1683 parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
1687 function get_setting() {
1688 $site = get_site();
1689 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1692 function write_setting($data) {
1693 if (!in_array($data, array_keys($this->choices))) {
1694 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1696 $record = new stdClass();
1697 $record->id = $this->id;
1698 $temp = $this->name;
1699 $record->$temp = $data;
1700 $record->timemodified = time();
1701 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1707 class admin_setting_courselist_frontpage extends admin_setting_configselect {
1709 function admin_setting_courselist_frontpage($loggedin) {
1710 global $CFG;
1711 require_once($CFG->dirroot . '/course/lib.php');
1712 $name = 'frontpage' . ($loggedin ? 'loggedin' : '');
1713 $visiblename = get_string('frontpage' . ($loggedin ? 'loggedin' : ''),'admin');
1714 $description = get_string('configfrontpage' . ($loggedin ? 'loggedin' : ''),'admin');
1715 $choices = array(FRONTPAGENEWS => get_string('frontpagenews'),
1716 FRONTPAGECOURSELIST => get_string('frontpagecourselist'),
1717 FRONTPAGECATEGORYNAMES => get_string('frontpagecategorynames'),
1718 FRONTPAGECATEGORYCOMBO => get_string('frontpagecategorycombo'),
1719 '' => get_string('none'));
1720 if (!$loggedin and count_records("course") > FRONTPAGECOURSELIMIT) {
1721 unset($choices[FRONTPAGECOURSELIST]);
1723 $defaults = FRONTPAGECOURSELIST.',,,';
1724 parent::admin_setting_configselect($name, $visiblename, $description, $defaults, $choices);
1727 function get_setting() {
1728 global $CFG;
1729 return (isset($CFG->{$this->name}) ? explode(',', $CFG->{$this->name}) : ',1,,');
1732 function write_setting($data) {
1733 if (empty($data)) {
1734 $data = array();
1735 } if (!is_array($data)) {
1736 $data = explode(',', $data);
1738 foreach($data as $datum) {
1739 if (! in_array($datum, array_keys($this->choices))) {
1740 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1743 return (set_config($this->name, implode(',', $data)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1746 function output_html() {
1747 if ($this->get_setting() === NULL) {
1748 $currentsetting = $this->defaultsetting;
1749 } else {
1750 $currentsetting = $this->get_setting();
1752 for ($i = 0; $i < count($this->choices) - 1; $i++) {
1753 if (!isset($currentsetting[$i])) {
1754 $currentsetting[$i] = 0;
1757 $return = '<div class="form-group">';
1758 for ($i = 0; $i < count($this->choices) - 1; $i++) {
1759 $return .='<select class="form-select" id="id_s_'.$this->name.$i.'" name="s_' . $this->name .'[]">';
1760 foreach ($this->choices as $key => $value) {
1761 $return .= '<option value="' . $key . '"' . ($key == $currentsetting[$i] ? ' selected="selected"' : '') . '>' . $value . '</option>';
1763 $return .= '</select>';
1764 if ($i !== count($this->choices) - 2) {
1765 $return .= '<br />';
1768 $return .= '</div>';
1770 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
1774 class admin_setting_sitesetcheckbox extends admin_setting_configcheckbox {
1776 var $id;
1778 function admin_setting_sitesetcheckbox($name, $visiblename, $description, $defaultsetting) {
1780 $this->id = SITEID;
1781 parent::admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting);
1785 function get_setting() {
1786 $site = get_site();
1787 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1790 function write_setting($data) {
1791 $record = new stdClass();
1792 $record->id = $this->id;
1793 $temp = $this->name;
1794 $record->$temp = ($data == '1' ? 1 : 0);
1795 $record->timemodified = time();
1796 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1801 class admin_setting_sitesettext extends admin_setting_configtext {
1803 var $id;
1805 function admin_setting_sitesettext($name, $visiblename, $description, $defaultsetting) {
1807 $this->id = SITEID;
1808 parent::admin_setting_configtext($name, $visiblename, $description, $defaultsetting);
1812 function get_setting() {
1813 $site = get_site();
1814 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1817 function validate($data) {
1818 $cleaned = stripslashes(clean_param($data, PARAM_MULTILANG));
1819 if ($cleaned == '') {
1820 return false; // can not be empty
1822 return ($data == $cleaned); // implicit conversion to string is needed to do exact comparison
1825 function write_setting($data) {
1826 $data = trim($data);
1827 if (!$this->validate($data)) {
1828 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
1831 $record = new stdClass();
1832 $record->id = $this->id;
1833 $record->{$this->name} = addslashes($data);
1834 $record->timemodified = time();
1835 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1840 class admin_setting_special_frontpagedesc extends admin_setting {
1842 var $id;
1844 function admin_setting_special_frontpagedesc() {
1845 $this->id = SITEID;
1846 $name = 'summary';
1847 $visiblename = get_string('frontpagedescription');
1848 $description = get_string('frontpagedescriptionhelp');
1849 parent::admin_setting($name, $visiblename, $description, '');
1852 function output_html() {
1854 global $CFG;
1856 if ($this->get_setting() === NULL) {
1857 $currentsetting = $this->defaultsetting;
1858 } else {
1859 $currentsetting = $this->get_setting();
1862 $CFG->adminusehtmleditor = can_use_html_editor();
1864 $return = print_textarea($CFG->adminusehtmleditor, 15, 60, 0, 0, 's_' . $this->name, $currentsetting, 0, true);
1866 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
1869 function get_setting() {
1871 $site = get_site();
1872 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1876 function write_setting($data) {
1878 $data = addslashes(clean_param($data, PARAM_CLEANHTML));
1880 $record = new stdClass();
1881 $record->id = $this->id;
1882 $temp = $this->name;
1883 $record->$temp = $data;
1884 $record->timemodified = time();
1886 return(update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1893 class admin_setting_special_editorfontlist extends admin_setting {
1895 var $items;
1897 function admin_setting_special_editorfontlist() {
1898 global $CFG;
1899 $name = 'editorfontlist';
1900 $visiblename = get_string('editorfontlist', 'admin');
1901 $description = get_string('configeditorfontlist', 'admin');
1902 $defaults = array('k0' => 'Trebuchet',
1903 'v0' => 'Trebuchet MS,Verdana,Arial,Helvetica,sans-serif',
1904 'k1' => 'Arial',
1905 'v1' => 'arial,helvetica,sans-serif',
1906 'k2' => 'Courier New',
1907 'v2' => 'courier new,courier,monospace',
1908 'k3' => 'Georgia',
1909 'v3' => 'georgia,times new roman,times,serif',
1910 'k4' => 'Tahoma',
1911 'v4' => 'tahoma,arial,helvetica,sans-serif',
1912 'k5' => 'Times New Roman',
1913 'v5' => 'times new roman,times,serif',
1914 'k6' => 'Verdana',
1915 'v6' => 'verdana,arial,helvetica,sans-serif',
1916 'k7' => 'Impact',
1917 'v7' => 'impact',
1918 'k8' => 'Wingdings',
1919 'v8' => 'wingdings');
1920 parent::admin_setting($name, $visiblename, $description, $defaults);
1923 function get_setting() {
1924 global $CFG;
1925 if (isset($CFG->editorfontlist)) {
1926 $i = 0;
1927 $currentsetting = array();
1928 $items = explode(';', $CFG->editorfontlist);
1929 foreach ($items as $item) {
1930 $item = explode(':', $item);
1931 $currentsetting['k' . $i] = $item[0];
1932 $currentsetting['v' . $i] = $item[1];
1933 $i++;
1935 return $currentsetting;
1936 } else {
1937 return NULL;
1941 function write_setting($data) {
1943 // there miiight be an easier way to do this :)
1944 // if this is changed, make sure the $defaults array above is modified so that this
1945 // function processes it correctly
1947 $keys = array();
1948 $values = array();
1950 foreach ($data as $key => $value) {
1951 if (substr($key,0,1) == 'k') {
1952 $keys[substr($key,1)] = $value;
1953 } elseif (substr($key,0,1) == 'v') {
1954 $values[substr($key,1)] = $value;
1958 $result = '';
1959 for ($i = 0; $i < count($keys); $i++) {
1960 if (($keys[$i] !== '') && ($values[$i] !== '')) {
1961 $result .= clean_param($keys[$i],PARAM_NOTAGS) . ':' . clean_param($values[$i], PARAM_NOTAGS) . ';';
1965 $result = substr($result, 0, -1); // trim the last semicolon
1967 return (set_config($this->name, $result) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1970 function output_html() {
1972 if ($this->get_setting() === NULL) {
1973 $currentsetting = $this->defaultsetting;
1974 } else {
1975 $currentsetting = $this->get_setting();
1978 $return = '<div class="form-group">';
1979 for ($i = 0; $i < count($currentsetting) / 2; $i++) {
1980 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . $i . ']" value="' . $currentsetting['k' . $i] . '" />';
1981 $return .= '&nbsp;&nbsp;';
1982 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . $i . ']" value="' . $currentsetting['v' . $i] . '" /><br />';
1984 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . $i . ']" value="" />';
1985 $return .= '&nbsp;&nbsp;';
1986 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . $i . ']" value="" /><br />';
1987 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . ($i + 1) . ']" value="" />';
1988 $return .= '&nbsp;&nbsp;';
1989 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . ($i + 1) . ']" value="" />';
1990 $return .= '</div>';
1992 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
1997 class admin_setting_special_editordictionary extends admin_setting_configselect {
1999 function admin_setting_special_editordictionary() {
2000 $name = 'editordictionary';
2001 $visiblename = get_string('editordictionary','admin');
2002 $description = get_string('configeditordictionary', 'admin');
2003 $choices = $this->editor_get_dictionaries();
2004 if (! is_array($choices)) {
2005 $choices = array('');
2008 parent::admin_setting_configselect($name, $visiblename, $description, '', $choices);
2011 // function borrowed from the old moodle/admin/editor.php, slightly modified
2012 function editor_get_dictionaries () {
2013 /// Get all installed dictionaries in the system
2015 global $CFG;
2017 // error_reporting(E_ALL); // for debug, final version shouldn't have this...
2018 clearstatcache();
2020 // If aspellpath isn't set don't even bother ;-)
2021 if (empty($CFG->aspellpath)) {
2022 return 'Empty aspell path!';
2025 // Do we have access to popen function?
2026 if (!function_exists('popen')) {
2027 return 'Popen function disabled!';
2030 $cmd = $CFG->aspellpath;
2031 $output = '';
2032 $dictionaries = array();
2033 $dicts = array();
2035 if(!($handle = @popen(escapeshellarg($cmd) .' dump dicts', 'r'))) {
2036 return 'Couldn\'t create handle!';
2039 while(!feof($handle)) {
2040 $output .= fread($handle, 1024);
2042 @pclose($handle);
2044 $dictionaries = explode(chr(10), $output);
2046 // Get rid of possible empty values
2047 if (is_array($dictionaries)) {
2049 $cnt = count($dictionaries);
2051 for ($i = 0; $i < $cnt; $i++) {
2052 if (!empty($dictionaries[$i])) {
2053 $dicts[$dictionaries[$i]] = $dictionaries[$i];
2058 if (count($dicts) >= 1) {
2059 return $dicts;
2062 return 'Error! Check your aspell installation!';
2070 class admin_setting_special_editorhidebuttons extends admin_setting {
2072 var $name;
2073 var $visiblename;
2074 var $description;
2075 var $items;
2077 function admin_setting_special_editorhidebuttons() {
2078 $this->name = 'editorhidebuttons';
2079 $this->visiblename = get_string('editorhidebuttons', 'admin');
2080 $this->description = get_string('confeditorhidebuttons', 'admin');
2081 $this->defaultsetting = array();
2082 // weird array... buttonname => buttonimage (assume proper path appended). if you leave buttomimage blank, text will be printed instead
2083 $this->items = array('fontname' => '',
2084 'fontsize' => '',
2085 'formatblock' => '',
2086 'bold' => 'ed_format_bold.gif',
2087 'italic' => 'ed_format_italic.gif',
2088 'underline' => 'ed_format_underline.gif',
2089 'strikethrough' => 'ed_format_strike.gif',
2090 'subscript' => 'ed_format_sub.gif',
2091 'superscript' => 'ed_format_sup.gif',
2092 'copy' => 'ed_copy.gif',
2093 'cut' => 'ed_cut.gif',
2094 'paste' => 'ed_paste.gif',
2095 'clean' => 'ed_wordclean.gif',
2096 'undo' => 'ed_undo.gif',
2097 'redo' => 'ed_redo.gif',
2098 'justifyleft' => 'ed_align_left.gif',
2099 'justifycenter' => 'ed_align_center.gif',
2100 'justifyright' => 'ed_align_right.gif',
2101 'justifyfull' => 'ed_align_justify.gif',
2102 'lefttoright' => 'ed_left_to_right.gif',
2103 'righttoleft' => 'ed_right_to_left.gif',
2104 'insertorderedlist' => 'ed_list_num.gif',
2105 'insertunorderedlist' => 'ed_list_bullet.gif',
2106 'outdent' => 'ed_indent_less.gif',
2107 'indent' => 'ed_indent_more.gif',
2108 'forecolor' => 'ed_color_fg.gif',
2109 'hilitecolor' => 'ed_color_bg.gif',
2110 'inserthorizontalrule' => 'ed_hr.gif',
2111 'createanchor' => 'ed_anchor.gif',
2112 'createlink' => 'ed_link.gif',
2113 'unlink' => 'ed_unlink.gif',
2114 'insertimage' => 'ed_image.gif',
2115 'inserttable' => 'insert_table.gif',
2116 'insertsmile' => 'em.icon.smile.gif',
2117 'insertchar' => 'icon_ins_char.gif',
2118 'spellcheck' => 'spell-check.gif',
2119 'htmlmode' => 'ed_html.gif',
2120 'popupeditor' => 'fullscreen_maximize.gif',
2121 'search_replace' => 'ed_replace.gif');
2124 function get_setting() {
2125 global $CFG;
2126 return (isset($CFG->{$this->name}) ? explode(' ', $CFG->{$this->name}) : NULL);
2129 function write_setting($data) {
2130 $result = array();
2131 if (empty($data)) { $data = array(); }
2132 foreach ($data as $key => $value) {
2133 if (!in_array($key, array_keys($this->items))) {
2134 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2136 if ($value == '1') {
2137 $result[] = $key;
2140 return (set_config($this->name, implode(' ',$result)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2143 function output_html() {
2145 global $CFG;
2147 // checkboxes with input name="$this->name[$key]" value="1"
2148 // we do 15 fields per column
2150 if ($this->get_setting() === NULL) {
2151 $currentsetting = $this->defaultsetting;
2152 } else {
2153 $currentsetting = $this->get_setting();
2156 $return = '<div class="form-group">';
2157 $return .= '<table><tr><td valign="top" align="right">';
2159 $count = 0;
2161 foreach($this->items as $key => $value) {
2162 if ($count % 15 == 0 and $count != 0) {
2163 $return .= '</td><td valign="top" align="right">';
2166 $return .= ($value == '' ? get_string($key,'editor') : '<img width="18" height="18" src="' . $CFG->wwwroot . '/lib/editor/htmlarea/images/' . $value . '" alt="' . get_string($key,'editor') . '" title="' . get_string($key,'editor') . '" />') . '&nbsp;';
2167 $return .= '<input type="checkbox" class="form-checkbox" value="1" id="id_s_'.$this->name.$key.'" name="s_' . $this->name . '[' . $key . ']"' . (in_array($key,$currentsetting) ? ' checked="checked"' : '') . ' />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
2168 $count++;
2169 if ($count % 15 != 0) {
2170 $return .= '<br /><br />';
2174 $return .= '</td></tr>';
2175 $return .= '</table>';
2176 $return .= '</div>';
2178 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2183 class admin_setting_langlist extends admin_setting_configtext {
2184 function admin_setting_langlist() {
2185 parent::admin_setting_configtext('langlist', get_string('langlist', 'admin'), get_string('configlanglist', 'admin'), '', PARAM_NOTAGS);
2188 function write_setting($data) {
2189 $return = parent::write_setting($data);
2190 get_list_of_languages(true);//refresh the list
2191 return $return;
2195 class admin_setting_backupselect extends admin_setting_configselect {
2197 function admin_setting_backupselect($name, $visiblename, $description, $default, $choices) {
2198 parent::admin_setting_configselect($name, $visiblename, $description, $default, $choices);
2201 function get_setting() {
2202 $backup_config = backup_get_config();
2203 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2206 function write_setting($data) {
2207 // check that what we got was in the original choices
2208 if (! in_array($data, array_keys($this->choices))) {
2209 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2212 return (backup_set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2217 class admin_setting_special_backupsaveto extends admin_setting_configtext {
2219 function admin_setting_special_backupsaveto() {
2220 $name = 'backup_sche_destination';
2221 $visiblename = get_string('saveto');
2222 $description = get_string('backupsavetohelp');
2223 parent::admin_setting_configtext($name, $visiblename, $description, '');
2226 function get_setting() {
2227 $backup_config = backup_get_config();
2228 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2231 function write_setting($data) {
2232 $data = trim($data);
2233 if (!empty($data) and !is_dir($data)) {
2234 return get_string('pathnotexists') . '<br />';
2236 return (backup_set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2241 class admin_setting_backupcheckbox extends admin_setting_configcheckbox {
2243 function admin_setting_backupcheckbox($name, $visiblename, $description, $default) {
2244 parent::admin_setting_configcheckbox($name, $visiblename, $description, $default);
2247 function write_setting($data) {
2248 if ($data == '1') {
2249 return (backup_set_config($this->name, 1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2250 } else {
2251 return (backup_set_config($this->name, 0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2255 function get_setting() {
2256 $backup_config = backup_get_config();
2257 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2262 class admin_setting_special_backuptime extends admin_setting_configtime {
2264 function admin_setting_special_backuptime() {
2265 $name = 'backup_sche_hour';
2266 $name2 = 'backup_sche_minute';
2267 $visiblename = get_string('executeat');
2268 $description = get_string('backupexecuteathelp');
2269 $default = array('h' => 0, 'm' => 0);
2270 parent::admin_setting_configtime($name, $name2, $visiblename, $description, $default);
2273 function get_setting() {
2274 $backup_config = backup_get_config();
2275 return (isset($backup_config->{$this->name}) && isset($backup_config->{$this->name}) ? array('h'=>$backup_config->{$this->name}, 'm'=>$backup_config->{$this->name2}) : NULL);
2278 function write_setting($data) {
2279 // check that what we got was in the original choices
2280 if (!(in_array($data['h'], array_keys($this->choices)) && in_array($data['m'], array_keys($this->choices2)))) {
2281 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2284 return (backup_set_config($this->name, $data['h']) && backup_set_config($this->name2, $data['m']) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2289 class admin_setting_special_backupdays extends admin_setting {
2291 function admin_setting_special_backupdays() {
2292 $name = 'backup_sche_weekdays';
2293 $visiblename = get_string('schedule');
2294 $description = get_string('backupschedulehelp');
2295 $default = array('u' => 0, 'm' => 0, 't' => 0, 'w' => 0, 'r' => 0, 'f' => 0, 's' => 0);
2296 parent::admin_setting($name, $visiblename, $description, $default);
2299 function get_setting() {
2300 $backup_config = backup_get_config();
2301 if (isset($backup_config->{$this->name})) {
2302 $currentsetting = $backup_config->{$this->name};
2303 return array('u' => substr($currentsetting, 0, 1),
2304 'm' => substr($currentsetting, 1, 1),
2305 't' => substr($currentsetting, 2, 1),
2306 'w' => substr($currentsetting, 3, 1),
2307 'r' => substr($currentsetting, 4, 1),
2308 'f' => substr($currentsetting, 5, 1),
2309 's' => substr($currentsetting, 6, 1));
2310 } else {
2311 return NULL;
2315 function output_html() {
2317 if ($this->get_setting() === NULL) {
2318 $currentsetting = $this->defaultsetting;
2319 } else {
2320 $currentsetting = $this->get_setting();
2323 // rewrite for simplicity
2324 $currentsetting = $currentsetting['u'] . $currentsetting['m'] . $currentsetting['t'] . $currentsetting['w'] .
2325 $currentsetting['r'] . $currentsetting['f'] . $currentsetting['s'];
2327 $return = '<table><tr><td><div style="text-align:center">&nbsp;&nbsp;' . get_string('sunday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' .
2328 get_string('monday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' . get_string('tuesday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' .
2329 get_string('wednesday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' . get_string('thursday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' .
2330 get_string('friday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' . get_string('saturday', 'calendar') . '&nbsp;&nbsp;</div></td></tr><tr>' .
2331 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'u" name="s_'. $this->name .'[u]" value="1" ' . (substr($currentsetting,0,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2332 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'m" name="s_'. $this->name .'[m]" value="1" ' . (substr($currentsetting,1,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2333 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'t" name="s_'. $this->name .'[t]" value="1" ' . (substr($currentsetting,2,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2334 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'w" name="s_'. $this->name .'[w]" value="1" ' . (substr($currentsetting,3,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2335 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'r" name="s_'. $this->name .'[r]" value="1" ' . (substr($currentsetting,4,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2336 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'f" name="s_'. $this->name .'[f]" value="1" ' . (substr($currentsetting,5,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2337 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'s" name="s_'. $this->name .'[s]" value="1" ' . (substr($currentsetting,6,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2338 '</tr></table>';
2340 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2344 // we're using the array trick (see http://ca.php.net/manual/en/faq.html.php#faq.html.arrays) to get the data passed to use without having to modify
2345 // admin_settingpage (note that admin_settingpage only calls write_setting with the data that matches $this->name... so if we have multiple form fields,
2346 // they MUST go into an array named $this->name, or else we won't receive them here
2347 function write_setting($data) {
2348 $week = 'umtwrfs';
2349 $result = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
2350 if (!empty($data)) {
2351 foreach($data as $key => $value) {
2352 if ($value == '1') {
2353 $result[strpos($week, $key)] = 1;
2357 return (backup_set_config($this->name, implode('',$result)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2361 class admin_setting_special_debug extends admin_setting_configselect {
2363 function admin_setting_special_debug() {
2364 $name = 'debug';
2365 $visiblename = get_string('debug', 'admin');
2366 $description = get_string('configdebug', 'admin');
2367 $choices = array( DEBUG_NONE => get_string('debugnone', 'admin'),
2368 DEBUG_MINIMAL => get_string('debugminimal', 'admin'),
2369 DEBUG_NORMAL => get_string('debugnormal', 'admin'),
2370 DEBUG_ALL => get_string('debugall', 'admin'),
2371 DEBUG_DEVELOPER => get_string('debugdeveloper', 'admin')
2373 parent::admin_setting_configselect($name, $visiblename, $description, '', $choices);
2376 function get_setting() {
2377 global $CFG;
2378 if (isset($CFG->debug)) {
2379 return $CFG->debug;
2380 } else {
2381 return NULL;
2385 function write_setting($data) {
2386 return (set_config($this->name,$data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2392 class admin_setting_special_calendar_weekend extends admin_setting {
2394 function admin_setting_special_calendar_weekend() {
2395 $name = 'calendar_weekend';
2396 $visiblename = get_string('calendar_weekend', 'admin');
2397 $description = get_string('helpweekenddays', 'admin');
2398 $default = array ('0', '6'); // Saturdays and Sundays
2399 parent::admin_setting($name, $visiblename, $description, $default);
2402 function get_setting() {
2403 global $CFG;
2404 return isset($CFG->{$this->name}) ? $CFG->{$this->name} : 0;
2407 function write_setting($data) {
2408 $result = 0;
2409 if (!empty($data)) {
2410 foreach($data as $index) {
2411 $result |= 1 << $index;
2414 return (set_config($this->name, $result) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2417 function output_html() {
2418 if ($this->get_setting() === NULL) {
2419 $currentsetting = $this->defaultsetting;
2420 } else {
2421 $currentsetting = $this->get_setting();
2424 // The order matters very much because of the implied numeric keys
2425 $days = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
2426 $return = '<table><thead><tr>';
2427 foreach($days as $index => $day) {
2428 $return .= '<td><label for="id_s_'.$this->name.$index.'">'.get_string($day, 'calendar').'</label></td>';
2430 $return .= '</tr></thead><tbody><tr>';
2431 foreach($days as $index => $day) {
2432 $return .= '<td><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.$index.'" name="s_'.$this->name.'[]" value="'.$index.'" '.($currentsetting & (1 << $index) ? 'checked="checked"' : '') . ' /></td>';
2434 $return .= '</tr></tbody></table>';
2436 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2443 * this is used in config->appearance->gradeconfig
2445 class admin_setting_special_gradebookroles extends admin_setting {
2447 function admin_setting_special_gradebookroles() {
2448 $name = 'gradebookroles';
2449 $visiblename = get_string('gradebookroles', 'admin');
2450 $description = get_string('configgradebookroles', 'admin');
2451 $default = array(5=>'1'); // The student role in a default install
2452 parent::admin_setting($name, $visiblename, $description, $default);
2455 function get_setting() {
2456 global $CFG;
2457 if (!empty($CFG->{$this->name})) {
2458 $result = explode(',', $CFG->{$this->name});
2459 foreach ($result as $roleid) {
2460 $array[$roleid] = 1;
2462 return $array;
2463 } else {
2464 return null;
2468 function write_setting($data) {
2469 if (!empty($data)) {
2470 $str = '';
2471 foreach ($data as $key => $value) {
2472 if ($value) {
2473 $str .= $key.',';
2476 return set_config($this->name, rtrim($str, ","))?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2477 } else {
2478 return set_config($this->name, '')?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2482 function output_html() {
2484 if ($this->get_setting() === NULL) {
2485 $currentsetting = $this->defaultsetting;
2486 } else {
2487 $currentsetting = $this->get_setting();
2489 // from to process which roles to display
2490 if ($roles = get_records('role')) {
2491 $return = '<div class="form-group">';
2492 $first = true;
2493 foreach ($roles as $roleid=>$role) {
2494 if (is_array($currentsetting) && in_array($roleid, array_keys($currentsetting))) {
2495 $checked = ' checked="checked"';
2496 } else {
2497 $checked = '';
2499 if ($first) {
2500 $first = false;
2501 } else {
2502 $return .= '<br />';
2504 $return .= '<input type="checkbox" name="s_'.$this->name.'['.$roleid.']" value="1"'.$checked.' />&nbsp;'.format_string($role->name);
2506 $return .= '</div>';
2509 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2516 * this is used in config->appearance->coursemanager
2517 * (which roles to show on course decription page)
2519 class admin_setting_special_coursemanager extends admin_setting {
2521 function admin_setting_special_coursemanager() {
2522 $name = 'coursemanager';
2523 $visiblename = get_string('coursemanager', 'admin');
2524 $description = get_string('configcoursemanager', 'admin');
2525 $default = array(3=>'1'); // The teahcer role in a default install
2526 parent::admin_setting($name, $visiblename, $description, $default);
2529 function get_setting() {
2531 global $CFG;
2532 if (!empty($CFG->{$this->name})) {
2533 $result = explode(',', $CFG->{$this->name});
2534 foreach ($result as $roleid) {
2535 $array[$roleid] = 1;
2537 return $array;
2538 } else if (isset($CFG->{$this->name})) {
2539 return array();
2540 } else {
2541 return null;
2545 function write_setting($data) {
2547 if (!empty($data)) {
2548 $str = '';
2549 foreach ($data as $key => $value) {
2550 if ($value) {
2551 $str .= $key.',';
2554 return set_config($this->name, rtrim($str, ","))?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2555 } else {
2556 return set_config($this->name, '')?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2560 function output_html() {
2562 if ($this->get_setting() === NULL) {
2563 $currentsetting = $this->defaultsetting;
2564 } else {
2565 $currentsetting = $this->get_setting();
2567 // from to process which roles to display
2568 if ($roles = get_records_select('role', '', 'sortorder')) {
2569 $return = '<div class="form-group">';
2570 $first = true;
2571 foreach ($roles as $roleid=>$role) {
2572 if (is_array($currentsetting) && in_array($roleid, array_keys($currentsetting))) {
2573 $checked = 'checked="checked"';
2574 } else {
2575 $checked = '';
2577 if ($first) {
2578 $first = false;
2579 } else {
2580 $return .= '<br />';
2582 $return .= '<input type="checkbox" name="s_'.$this->name.'['.$roleid.']" value="1" '.$checked.' />&nbsp;'.$role->name;
2584 $return .= '</div>';
2586 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2591 * this is used in config->courses->gradeexports
2592 * (which roles to show on course decription page)
2594 class admin_setting_special_gradeexport extends admin_setting {
2596 function admin_setting_special_gradeexport() {
2597 $name = 'gradeexport';
2598 $visiblename = get_string('gradeexport', 'admin');
2599 $description = get_string('configgradeexport', 'admin');
2600 $default = array(3=>'1'); // The teahcer role in a default install
2601 parent::admin_setting($name, $visiblename, $description, $default);
2604 function get_setting() {
2606 global $CFG;
2607 if (!empty($CFG->{$this->name})) {
2608 $result = explode(',', $CFG->{$this->name});
2609 foreach ($result as $plugin) {
2610 $array[$plugin] = 1;
2612 return $array;
2613 } else if (isset($CFG->{$this->name})) {
2614 return array();
2615 } else {
2616 return null;
2620 function write_setting($data) {
2622 if (!empty($data)) {
2623 $str = '';
2624 foreach ($data as $key => $value) {
2625 if ($value) {
2626 $str .= $key.',';
2629 return set_config($this->name, rtrim($str, ","))?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2630 } else {
2631 return set_config($this->name, '')?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2635 function output_html() {
2637 if ($this->get_setting() === NULL) {
2638 $currentsetting = $this->defaultsetting;
2639 } else {
2640 $currentsetting = $this->get_setting();
2642 // from to process which roles to display
2643 if ($exports = get_list_of_plugins('grade/export')) {
2644 $return = '<div class="form-group">';
2645 $first = true;
2646 foreach ($exports as $export) {
2647 if (is_array($currentsetting) && in_array($export, array_keys($currentsetting))) {
2648 $checked = 'checked="checked"';
2649 } else {
2650 $checked = '';
2652 if ($first) {
2653 $first = false;
2654 } else {
2655 $return .= '<br />';
2657 $return .= '<input type="checkbox" name="s_'.$this->name.'['.$export.']" value="1" '.$checked.' />&nbsp;'.$export;
2659 $return .= '</div>';
2661 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2668 class admin_setting_special_perfdebug extends admin_setting_configcheckbox {
2670 function admin_setting_special_perfdebug() {
2671 $name = 'perfdebug';
2672 $visiblename = get_string('perfdebug', 'admin');
2673 $description = get_string('configperfdebug', 'admin');
2674 parent::admin_setting_configcheckbox($name, $visiblename, $description, '');
2677 function write_setting($data) {
2679 if ($data == '1') {
2680 return (set_config($this->name,15) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2681 } else {
2682 return (set_config($this->name,7) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2686 function output_html() {
2688 if ($this->get_setting() === NULL) {
2689 $currentsetting = $this->defaultsetting;
2690 } else {
2691 $currentsetting = $this->get_setting();
2694 $return = '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'" name="s_'. $this->name .'" value="1" ' . ($currentsetting == 15 ? 'checked="checked"' : '') . ' />';
2695 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
2700 class admin_setting_special_debugdisplay extends admin_setting_configcheckbox {
2702 function admin_setting_special_debugdisplay() {
2703 $name = 'debugdisplay';
2704 $visiblename = get_string('debugdisplay', 'admin');
2705 $description = get_string('configdebugdisplay', 'admin');
2706 $default = ini_get('display_errors');
2707 parent::admin_setting_configcheckbox($name, $visiblename, $description, $default);
2710 function write_setting($data) {
2712 if ($data == '1') {
2713 return (set_config($this->name,1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2714 } else {
2715 return (set_config($this->name,0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2719 function output_html() {
2721 if ($this->get_setting() === NULL) {
2722 $currentsetting = $this->defaultsetting;
2723 } else {
2724 $currentsetting = $this->get_setting();
2727 $return = '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'" name="s_'. $this->name .'" value="1" ' . ($currentsetting == 1 ? 'checked="checked"' : '') . ' />';
2728 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
2734 // Code for a function that helps externalpages print proper headers and footers
2735 // N.B.: THIS FUNCTION HANDLES AUTHENTICATION
2736 function admin_externalpage_setup($section) {
2738 global $CFG, $PAGE, $USER;
2740 $adminroot = admin_get_root();
2742 require_once($CFG->libdir . '/blocklib.php');
2743 require_once($CFG->dirroot . '/'.$CFG->admin.'/pagelib.php');
2745 page_map_class(PAGE_ADMIN, 'page_admin');
2747 $PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id number
2749 $PAGE->init_extra($section); // hack alert!
2751 $root = $adminroot->locate($PAGE->section);
2753 if ($site = get_site()) {
2754 require_login();
2755 } else {
2756 redirect($CFG->wwwroot . '/'.$CFG->admin.'/index.php');
2757 die;
2760 if (!is_a($root, 'admin_externalpage')) {
2761 error(get_string('sectionerror','admin'));
2762 die;
2765 // this eliminates our need to authenticate on the actual pages
2766 if (!($root->check_access())) {
2767 error(get_string('accessdenied', 'admin'));
2768 die;
2771 $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
2773 if (!isset($USER->adminediting)) {
2774 $USER->adminediting = false;
2777 if ($PAGE->user_allowed_editing()) {
2778 if ($adminediting == 1) {
2779 $USER->adminediting = true;
2780 } elseif ($adminediting == 0) {
2781 $USER->adminediting = false;
2787 function admin_externalpage_print_header() {
2789 global $CFG, $PAGE, $SITE, $THEME;
2791 if (!empty($THEME->customcorners)) {
2792 require_once($CFG->dirroot.'/lib/custom_corners_lib.php');
2795 define('ADMIN_EXT_HEADER_PRINTED', 'true');
2797 if (!empty($SITE->fullname)) {
2798 $pageblocks = blocks_setup($PAGE);
2800 $preferred_width_left = bounded_number(BLOCK_L_MIN_WIDTH,
2801 blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]),
2802 BLOCK_L_MAX_WIDTH);
2803 $PAGE->print_header();
2804 echo '<table id="layout-table" summary=""><tr>';
2805 echo '<td style="width: ' . $preferred_width_left . 'px;" id="left-column">';
2806 if (!empty($THEME->customcorners)) print_custom_corners_start();
2807 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
2808 if (!empty($THEME->customcorners)) print_custom_corners_end();
2809 echo '</td>';
2810 echo '<td id="middle-column">';
2811 if (!empty($THEME->customcorners)) print_custom_corners_start();
2812 } else {
2813 print_header();
2817 function admin_externalpage_print_footer() {
2819 global $CFG, $PAGE, $SITE, $THEME;
2821 if (!empty($THEME->customcorners)) {
2822 require_once($CFG->dirroot.'/lib/custom_corners_lib.php');
2825 define('ADMIN_EXT_FOOTER_PRINTED', 'true');
2827 if (!empty($SITE->fullname)) {
2828 $pageblocks = blocks_setup($PAGE);
2829 $preferred_width_right = bounded_number(BLOCK_R_MIN_WIDTH,
2830 blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]),
2831 BLOCK_R_MAX_WIDTH);
2832 if (!empty($THEME->customcorners)) print_custom_corners_end();
2833 echo '</td>';
2834 if (blocks_have_content($pageblocks, BLOCK_POS_RIGHT)) {
2835 echo '<td style="width: ' . $preferred_width_right . 'px;" id="right-column">';
2836 if (!empty($THEME->customcorners)) print_custom_corners_start();
2837 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
2838 if (!empty($THEME->customcorners)) print_custom_corners_end();
2839 echo '</td>';
2841 echo '</tr></table>';
2843 print_footer();
2846 function admin_get_root() {
2847 global $CFG;
2849 static $ADMIN;
2851 if (!isset($ADMIN)) {
2852 // start the admin tree!
2853 $ADMIN = new admin_category('root', get_string("administration"));
2854 // we process this file first to get categories up and running
2855 include($CFG->dirroot . '/'.$CFG->admin.'/settings/top.php');
2857 // now we process all other files in admin/settings to build the
2858 // admin tree
2859 foreach (glob($CFG->dirroot . '/'.$CFG->admin.'/settings/*.php') as $file) {
2860 if ($file != $CFG->dirroot . '/'.$CFG->admin.'/settings/top.php') {
2861 include_once($file);
2866 return $ADMIN;
2869 /// settings utiliti functions
2871 // n.b. this function unconditionally applies default settings
2872 function apply_default_settings(&$node) {
2874 global $CFG;
2876 if (is_a($node, 'admin_category')) {
2877 $entries = array_keys($node->children);
2878 foreach ($entries as $entry) {
2879 apply_default_settings($node->children[$entry]);
2881 return;
2884 if (is_a($node, 'admin_settingpage')) {
2885 foreach ($node->settings as $setting) {
2886 $CFG->{$setting->name} = $setting->defaultsetting;
2887 $setting->write_setting($setting->defaultsetting);
2888 unset($setting); // needed to prevent odd (imho) reference behaviour
2889 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
2891 return;
2894 return;
2898 // n.b. this function unconditionally applies default settings
2899 function apply_default_exception_settings($defaults) {
2901 global $CFG;
2903 foreach($defaults as $key => $value) {
2904 $CFG->$key = $value;
2905 set_config($key, $value);
2910 function format_admin_setting($name, $title='', $form='', $description='', $label=true) {
2912 // sometimes the id is not id_s_name, but id_s_name_m or something, and this does not validate
2913 if ($label) {
2914 $labelfor = 'for = "id_s_'.$name.'"';
2915 } else {
2916 $labelfor = '';
2919 $str = "\n".
2920 '<div class="form-item" id="admin-'.$name.'">'."\n".
2921 '<label '.$labelfor.'>'.$title."\n".
2922 ' <span class="form-shortname">'.$name.'</span>'."\n".
2923 '</label>'."\n".
2924 $form."\n".
2925 '<div class="description">'.$description.'</div>'."\n".
2926 '</div>'.
2927 "\n\n";
2929 return $str;
2933 * Try to upgrade the given language pack (or current language)
2934 * If it doesn't work, fail silently and return false
2936 function upgrade_language_pack($lang='') {
2937 global $CFG;
2939 if (empty($lang)) {
2940 $lang = current_language();
2943 if ($lang == 'en_utf8') {
2944 return true; // Nothing to do
2947 notify(get_string('langimport', 'admin').': '.$lang.' ... ', 'notifysuccess');
2949 @mkdir ($CFG->dataroot.'/temp/'); //make it in case it's a fresh install, it might not be there
2950 @mkdir ($CFG->dataroot.'/lang/');
2952 require_once($CFG->libdir.'/componentlib.class.php');
2954 if ($cd = new component_installer('http://download.moodle.org', 'lang16', $lang.'.zip', 'languages.md5', 'lang')) {
2955 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
2957 if ($status == INSTALLED) {
2958 debugging('Downloading successful: '.$lang);
2959 @unlink($CFG->dataroot.'/cache/languages');
2960 return true;
2964 return false;
2968 * Based on find_new_settings{@link ()} in upgradesettings.php
2969 * Looks to find any admin settings that have not been initialized. Returns 1 if it finds any.
2971 * @param string &$node The node at which to start searching.
2972 * @return int Returns 1 if any settings haven't been initialised, 0 if they all have
2974 function any_new_admin_settings(&$node) {
2976 if (is_a($node, 'admin_category')) {
2977 $entries = array_keys($node->children);
2978 foreach ($entries as $entry) {
2979 if( any_new_admin_settings($node->children[$entry]) ){
2980 return 1;
2985 if (is_a($node, 'admin_settingpage')) {
2986 foreach ($node->settings as $setting) {
2987 if ($setting->get_setting() === NULL) {
2988 return 1;
2994 return 0;