4 * adminlib.php - Contains functions that only administrators will ever need to use
6 * @author Martin Dougiamas and many others
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
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) {
24 if (!$plugs = get_list_of_plugins($dir) ) {
25 debugging('No '.$type.' plugins installed!');
29 $updated_plugins = false;
30 $strpluginsetup = get_string('pluginsetup');
32 foreach ($plugs as $plug) {
34 $fullplug = $CFG->dirroot
.'/'.$dir.'/'. $plug;
38 if (is_readable($fullplug .'/version.php')) {
39 include_once($fullplug .'/version.php'); // defines $plugin with version etc
41 continue; // Nothing to do.
46 if (is_readable($fullplug . '/db/'. $CFG->dbtype
. '.php')) {
47 include_once($fullplug . '/db/'. $CFG->dbtype
. '.php'); // defines old upgrading function
50 if (is_readable($fullplug . '/db/upgrade.php')) {
51 include_once($fullplug . '/db/upgrade.php'); // defines new upgrading function
55 if (!isset($plugin)) {
59 if (!empty($plugin->requires
)) {
60 if ($plugin->requires
> $CFG->version
) {
62 $info->pluginname
= $plug;
63 $info->pluginversion
= $plugin->version
;
64 $info->currentmoodle
= $CFG->version
;
65 $info->requiremoodle
= $plugin->requires
;
66 if (!$updated_plugins) {
67 print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '',
68 upgrade_get_javascript(), false, ' ', ' ');
71 notify(get_string('pluginrequirementsnotmet', 'error', $info));
72 $updated_plugins = true;
77 $plugin->name
= $plug; // The name MUST match the directory
79 $pluginversion = $type.'_'.$plug.'_version';
81 if (!isset($CFG->$pluginversion)) {
82 set_config($pluginversion, 0);
85 if ($CFG->$pluginversion == $plugin->version
) {
87 } else if ($CFG->$pluginversion < $plugin->version
) {
88 if (!$updated_plugins) {
89 print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '',
90 upgrade_get_javascript(), false, ' ', ' ');
92 $updated_plugins = true;
94 print_heading($dir.'/'. $plugin->name
.' plugin needs upgrading');
96 @set_time_limit
(0); // To allow slow databases to complete the long SQL
98 if ($CFG->$pluginversion == 0) { // It's a new install of this plugin
99 /// Both old .sql files and new install.xml are supported
100 /// but we priorize install.xml (XMLDB) if present
102 if (file_exists($fullplug . '/db/install.xml')) {
103 $status = install_from_xmldb_file($fullplug . '/db/install.xml'); //New method
104 } else if (file_exists($fullplug .'/db/'. $CFG->dbtype
.'.sql')) {
105 $status = modify_database($fullplug .'/db/'. $CFG->dbtype
.'.sql'); //Old method
111 /// Continue with the instalation, roles and other stuff
113 /// OK so far, now update the plugins record
114 set_config($pluginversion, $plugin->version
);
116 /// Install capabilities
117 if (!update_capabilities($type.'/'.$plug)) {
118 error('Could not set up the capabilities for '.$plugin->name
.'!');
121 events_update_definition($type.'/'.$plug);
123 /// Run local install function if there is one
124 if (is_readable($fullplug .'/lib.php')) {
125 include_once($fullplug .'/lib.php');
126 $installfunction = $plugin->name
.'_install';
127 if (function_exists($installfunction)) {
128 if (! $installfunction() ) {
129 notify('Encountered a problem running install function for '.$module->name
.'!');
134 notify(get_string('modulesuccess', '', $plugin->name
), 'notifysuccess');
136 notify('Installing '. $plugin->name
.' FAILED!');
138 } else { // Upgrade existing install
139 /// Run de old and new upgrade functions for the module
140 $oldupgrade_function = $type.'_'.$plugin->name
.'_upgrade';
141 $newupgrade_function = 'xmldb_' . $type.'_'.$plugin->name
.'_upgrade';
143 /// First, the old function if exists
144 $oldupgrade_status = true;
145 if ($oldupgrade && function_exists($oldupgrade_function)) {
147 $oldupgrade_status = $oldupgrade_function($CFG->$pluginversion);
148 } else if ($oldupgrade) {
149 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
150 $fullplug . '/db/' . $CFG->dbtype
. '.php');
153 /// Then, the new function if exists and the old one was ok
154 $newupgrade_status = true;
155 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
157 $newupgrade_status = $newupgrade_function($CFG->$pluginversion);
158 } else if ($newupgrade) {
159 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
160 $fullplug . '/db/upgrade.php');
164 /// Now analyze upgrade results
165 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
166 // OK so far, now update the plugins record
167 set_config($pluginversion, $plugin->version
);
168 if (!update_capabilities($type.'/'.$plug)) {
169 error('Could not update '.$plugin->name
.' capabilities!');
171 events_update_definition($type.'/'.$plug);
172 notify(get_string('modulesuccess', '', $plugin->name
), 'notifysuccess');
174 notify('Upgrading '. $plugin->name
.' from '. $CFG->$pluginversion .' to '. $plugin->version
.' FAILED!');
180 error('Version mismatch: '. $plugin->name
.' can\'t downgrade '. $CFG->$pluginversion .' -> '. $plugin->version
.' !');
184 upgrade_log_finish();
186 if ($updated_plugins) {
187 print_continue($return);
188 print_footer('none');
194 * Find and check all modules and load them up or upgrade them if necessary
198 * @param string $return The url to prompt the user to continue to
199 * @todo Finish documenting this function
201 function upgrade_activity_modules($return) {
205 if (!$mods = get_list_of_plugins('mod') ) {
206 error('No modules installed!');
209 $updated_modules = false;
210 $strmodulesetup = get_string('modulesetup');
212 foreach ($mods as $mod) {
214 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
218 $fullmod = $CFG->dirroot
.'/mod/'. $mod;
222 if ( is_readable($fullmod .'/version.php')) {
223 include_once($fullmod .'/version.php'); // defines $module with version etc
225 notify('Module '. $mod .': '. $fullmod .'/version.php was not readable');
231 if ( is_readable($fullmod .'/db/' . $CFG->dbtype
. '.php')) {
232 include_once($fullmod .'/db/' . $CFG->dbtype
. '.php'); // defines old upgrading function
235 if ( is_readable($fullmod . '/db/upgrade.php')) {
236 include_once($fullmod . '/db/upgrade.php'); // defines new upgrading function
240 if (!isset($module)) {
244 if (!empty($module->requires
)) {
245 if ($module->requires
> $CFG->version
) {
246 $info = new object();
247 $info->modulename
= $mod;
248 $info->moduleversion
= $module->version
;
249 $info->currentmoodle
= $CFG->version
;
250 $info->requiremoodle
= $module->requires
;
251 if (!$updated_modules) {
252 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
253 upgrade_get_javascript(), false, ' ', ' ');
256 notify(get_string('modulerequirementsnotmet', 'error', $info));
257 $updated_modules = true;
262 $module->name
= $mod; // The name MUST match the directory
264 include_once($fullmod.'/lib.php'); // defines upgrading and/or installing functions
266 if ($currmodule = get_record('modules', 'name', $module->name
)) {
267 if ($currmodule->version
== $module->version
) {
269 } else if ($currmodule->version
< $module->version
) {
270 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
271 if (!$oldupgrade && !$newupgrade) {
272 notify('Upgrade files ' . $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype
. '.php or ' .
273 $fullmod . '/db/upgrade.php were not readable');
276 if (!$updated_modules) {
277 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
278 upgrade_get_javascript(), false, ' ', ' ');
281 print_heading($module->name
.' module needs upgrading');
283 /// Run de old and new upgrade functions for the module
284 $oldupgrade_function = $module->name
. '_upgrade';
285 $newupgrade_function = 'xmldb_' . $module->name
. '_upgrade';
287 /// First, the old function if exists
288 $oldupgrade_status = true;
289 if ($oldupgrade && function_exists($oldupgrade_function)) {
291 $oldupgrade_status = $oldupgrade_function($currmodule->version
, $module);
292 } else if ($oldupgrade) {
293 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
294 $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype
. '.php');
297 /// Then, the new function if exists and the old one was ok
298 $newupgrade_status = true;
299 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
301 $newupgrade_status = $newupgrade_function($currmodule->version
, $module);
302 } else if ($newupgrade) {
303 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
304 $mod . ': ' . $fullmod . '/db/upgrade.php');
308 /// Now analyze upgrade results
309 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
310 // OK so far, now update the modules record
311 $module->id
= $currmodule->id
;
312 if (! update_record('modules', $module)) {
313 error('Could not update '. $module->name
.' record in modules table!');
315 remove_dir($CFG->dataroot
. '/cache', true); // flush cache
316 notify(get_string('modulesuccess', '', $module->name
), 'notifysuccess');
319 notify('Upgrading '. $module->name
.' from '. $currmodule->version
.' to '. $module->version
.' FAILED!');
322 /// Update the capabilities table?
323 if (!update_capabilities('mod/'.$module->name
)) {
324 error('Could not update '.$module->name
.' capabilities!');
326 events_update_definition('mod/'.$module->name
);
328 $updated_modules = true;
332 error('Version mismatch: '. $module->name
.' can\'t downgrade '. $currmodule->version
.' -> '. $module->version
.' !');
335 } else { // module not installed yet, so install it
336 if (!$updated_modules) {
337 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
338 upgrade_get_javascript(), false, ' ', ' ');
341 print_heading($module->name
);
342 $updated_modules = true;
344 @set_time_limit
(0); // To allow slow databases to complete the long SQL
346 /// Both old .sql files and new install.xml are supported
347 /// but we priorize install.xml (XMLDB) if present
348 if (file_exists($fullmod . '/db/install.xml')) {
349 $status = install_from_xmldb_file($fullmod . '/db/install.xml'); //New method
351 $status = modify_database($fullmod .'/db/'. $CFG->dbtype
.'.sql'); //Old method
356 /// Continue with the installation, roles and other stuff
358 if ($module->id
= insert_record('modules', $module)) {
361 if (!update_capabilities('mod/'.$module->name
)) {
362 error('Could not set up the capabilities for '.$module->name
.'!');
366 events_update_definition('mod/'.$module->name
);
368 /// Run local install function if there is one
369 $installfunction = $module->name
.'_install';
370 if (function_exists($installfunction)) {
371 if (! $installfunction() ) {
372 notify('Encountered a problem running install function for '.$module->name
.'!');
376 notify(get_string('modulesuccess', '', $module->name
), 'notifysuccess');
379 error($module->name
.' module could not be added to the module list!');
382 error($module->name
.' tables could NOT be set up successfully!');
386 /// Check submodules of this module if necessary
388 $submoduleupgrade = $module->name
.'_upgrade_submodules';
389 if (function_exists($submoduleupgrade)) {
394 /// Run any defaults or final code that is necessary for this module
396 if ( is_readable($fullmod .'/defaults.php')) {
397 // Insert default values for any important configuration variables
399 include_once($fullmod .'/defaults.php');
400 if (!empty($defaults)) {
401 foreach ($defaults as $name => $value) {
402 if (!isset($CFG->$name)) {
403 set_config($name, $value);
410 upgrade_log_finish(); // finish logging if started
412 if ($updated_modules) {
413 print_continue($return);
414 print_footer('none');
420 * This function will return FALSE if the lock fails to be set (ie, if it's already locked)
422 * @param string $name ?
423 * @param bool $value ?
424 * @param int $staleafter ?
425 * @param bool $clobberstale ?
426 * @todo Finish documenting this function
428 function set_cron_lock($name,$value=true,$staleafter=7200,$clobberstale=false) {
431 mtrace("Tried to get a cron lock for a null fieldname");
440 if ($config = get_record('config','name',$name)) {
441 if (empty($config->value
)) {
442 set_config($name,time());
445 if ((time() - $staleafter) > $config->value
) {
446 mtrace("STALE LOCKFILE FOR $name - was $config->value");
447 if (!empty($clobberstale)) {
448 set_config($name,time());
452 return false; // was not stale - ie, we're ok to still be running.
457 set_config($name,time());
462 function print_progress($done, $total, $updatetime=5, $sleeptime=1, $donetext='') {
466 if ($total < 2) { // No need to show anything
470 if (empty($starttime)) {
471 $starttime = $lasttime = time();
472 $lasttime = $starttime - $updatetime;
473 echo '<table width="500" cellpadding="0" cellspacing="0" align="center"><tr><td width="500">';
474 echo '<div id="bar'.$total.'" style="border-style:solid;border-width:1px;width:500px;height:50px;">';
475 echo '<div id="slider'.$total.'" style="border-style:solid;border-width:1px;height:48px;width:10px;background-color:green;"></div>';
477 echo '<div id="text'.$total.'" align="center" style="width:500px;"></div>';
478 echo '</td></tr></table>';
484 if ($done && (($now - $lasttime) >= $updatetime)) {
485 $elapsedtime = $now - $starttime;
486 $projectedtime = (int)(((float)$total / (float)$done) * $elapsedtime) - $elapsedtime;
487 $percentage = format_float((float)$done / (float)$total, 2);
488 $width = (int)(500 * $percentage);
490 if ($projectedtime > 10) {
491 $projectedtext = ' Ending: '.format_time($projectedtime);
497 echo 'document.getElementById("text'.$total.'").innerHTML = "'.addslashes($donetext).' ('.$done.'/'.$total.') '.$projectedtext.'";'."\n";
498 echo 'document.getElementById("slider'.$total.'").style.width = \''.$width.'px\';'."\n";
506 function upgrade_get_javascript() {
509 if (!empty($_SESSION['installautopilot'])) {
510 $linktoscrolltoerrors = '<script type="text/javascript">var installautopilot = true;</script>'."\n";
512 $linktoscrolltoerrors = '<script type="text/javascript">var installautopilot = false;</script>'."\n";
514 $linktoscrolltoerrors .= '<script type="text/javascript" src="' . $CFG->wwwroot
. '/lib/scroll_to_errors.js"></script>';
516 return $linktoscrolltoerrors;
519 function create_admin_user() {
522 if (empty($CFG->rolesactive
)) { // No admin user yet.
524 $user = new object();
525 $user->auth
= 'manual';
526 $user->firstname
= get_string('admin');
527 $user->lastname
= get_string('user');
528 $user->username
= 'admin';
529 $user->password
= hash_internal_user_password('admin');
530 $user->email
= 'root@localhost';
531 $user->confirmed
= 1;
532 $user->mnethostid
= $CFG->mnet_localhost_id
;
533 $user->lang
= $CFG->lang
;
534 $user->maildisplay
= 1;
535 $user->timemodified
= time();
537 if (!$user->id
= insert_record('user', $user)) {
538 error('SERIOUS ERROR: Could not create admin user record !!!');
541 if (!$user = get_record('user', 'id', $user->id
)) { // Double check.
542 error('User ID was incorrect (can\'t find it)');
545 // Assign the default admin roles to the new user.
546 if (!$adminroles = get_roles_with_capability('moodle/legacy:admin', CAP_ALLOW
)) {
547 error('No admin role could be found');
549 $sitecontext = get_context_instance(CONTEXT_SYSTEM
);
550 foreach ($adminroles as $adminrole) {
551 role_assign($adminrole->id
, $user->id
, 0, $sitecontext->id
);
554 set_config('rolesactive', 1);
557 $USER = get_complete_user_data('username', 'admin');
558 $USER->newadminuser
= 1;
559 load_all_capabilities();
561 redirect("$CFG->wwwroot/user/editadvanced.php?id=$user->id"); // Edit thyself
563 error('Can not create admin!');
567 ////////////////////////////////////////////////
568 /// upgrade logging functions
569 ////////////////////////////////////////////////
571 $upgradeloghandle = false;
572 $upgradelogbuffer = '';
573 // I did not find out how to use static variable in callback function,
574 // the problem was that I could not flush the static buffer :-(
575 global $upgradeloghandle, $upgradelogbuffer;
578 * Check if upgrade is already running.
580 * If anything goes wrong due to missing call to upgrade_log_finish()
581 * just restart the browser.
583 * @param string warning message indicating upgrade is already running
584 * @param int page reload timeout
586 function upgrade_check_running($message, $timeout) {
587 if (!empty($_SESSION['upgraderunning'])) {
589 redirect(me(), $message, $timeout);
594 * Start logging of output into file (if not disabled) and
595 * prevent aborting and concurrent execution of upgrade script.
597 * Please note that you can not write into session variables after calling this function!
599 * This function may be called repeatedly.
601 function upgrade_log_start() {
602 global $CFG, $upgradeloghandle;
604 if (!empty($_SESSION['upgraderunning'])) {
605 return; // logging already started
608 @ignore_user_abort
(true); // ignore if user stops or otherwise aborts page loading
609 $_SESSION['upgraderunning'] = 1; // set upgrade indicator
610 if (empty($CFG->dbsessions
)) { // workaround for bug in adodb, db session can not be restarted
611 session_write_close(); // from now on user can reload page - will be displayed warning
613 make_upload_directory('upgradelogs');
614 ob_start('upgrade_log_callback', 2); // function for logging to disk; flush each line of text ASAP
615 register_shutdown_function('upgrade_log_finish'); // in case somebody forgets to stop logging
619 * Terminate logging of output, flush all data, allow script aborting
620 * and reopen session for writing. Function error() does terminate the logging too.
622 * Please make sure that each upgrade_log_start() is properly terminated by
623 * this function or error().
625 * This function may be called repeatedly.
627 function upgrade_log_finish() {
628 global $CFG, $upgradeloghandle, $upgradelogbuffer;
630 if (empty($_SESSION['upgraderunning'])) {
631 return; // logging already terminated
635 if ($upgradelogbuffer !== '') {
636 @fwrite
($upgradeloghandle, $upgradelogbuffer);
637 $upgradelogbuffer = '';
639 if ($upgradeloghandle and ($upgradeloghandle !== 'error')) {
640 @fclose
($upgradeloghandle);
641 $upgradeloghandle = false;
643 if (empty($CFG->dbsessions
)) {
644 @session_start
(); // ignore header errors, we only need to reopen session
646 $_SESSION['upgraderunning'] = 0; // clear upgrade indicator
647 if (connection_aborted()) {
650 @ignore_user_abort
(false);
654 * Callback function for logging into files. Not more than one file is created per minute,
655 * upgrade session (terminated by upgrade_log_finish()) is always stored in one file.
657 * This function must not output any characters or throw warnigns and errors!
659 function upgrade_log_callback($string) {
660 global $CFG, $upgradeloghandle, $upgradelogbuffer;
662 if (empty($CFG->disableupgradelogging
) and ($string != '') and ($upgradeloghandle !== 'error')) {
663 if ($upgradeloghandle or ($upgradeloghandle = @fopen
($CFG->dataroot
.'/upgradelogs/upg_'.date('Ymd-Hi').'.html', 'a'))) {
664 $upgradelogbuffer .= $string;
665 if (strlen($upgradelogbuffer) > 2048) { // 2kB write buffer
666 @fwrite
($upgradeloghandle, $upgradelogbuffer);
667 $upgradelogbuffer = '';
670 $upgradeloghandle = 'error';
677 * Try to verify that dataroot is not accessible from web.
678 * It is not 100% correct but might help to reduce number of vulnerable sites.
680 * Protection from httpd.conf and .htaccess is not detected properly.
682 function is_dataroot_insecure() {
685 $siteroot = str_replace('\\', '/', strrev($CFG->dirroot
.'/')); // win32 backslash workaround
687 $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot
, 1);
688 $rp = strrev(trim($rp, '/'));
689 $rp = explode('/', $rp);
691 if (strpos($siteroot, '/'.$r.'/') === 0) {
692 $siteroot = substr($siteroot, strlen($r)+
1); // moodle web in subdirectory
694 break; // probably alias root
698 $siteroot = strrev($siteroot);
699 $dataroot = str_replace('\\', '/', $CFG->dataroot
.'/');
701 if (strpos($dataroot, $siteroot) === 0) {
707 /// =============================================================================================================
708 /// administration tree classes and functions
711 // n.b. documentation is still in progress for this code
715 /// This file performs the following tasks:
716 /// -it defines the necessary objects and interfaces to build the Moodle
718 /// -it defines the admin_externalpage_setup(), admin_externalpage_print_header(),
719 /// and admin_externalpage_print_footer() functions used on admin pages
721 /// ADMIN_SETTING OBJECTS
723 /// Moodle settings are represented by objects that inherit from the admin_setting
724 /// class. These objects encapsulate how to read a setting, how to write a new value
725 /// to a setting, and how to appropriately display the HTML to modify the setting.
727 /// ADMIN_SETTINGPAGE OBJECTS
729 /// The admin_setting objects are then grouped into admin_settingpages. The latter
730 /// appear in the Moodle admin tree block. All interaction with admin_settingpage
731 /// objects is handled by the admin/settings.php file.
733 /// ADMIN_EXTERNALPAGE OBJECTS
735 /// There are some settings in Moodle that are too complex to (efficiently) handle
736 /// with admin_settingpages. (Consider, for example, user management and displaying
737 /// lists of users.) In this case, we use the admin_externalpage object. This object
738 /// places a link to an external PHP file in the admin tree block.
740 /// If you're using an admin_externalpage object for some settings, you can take
741 /// advantage of the admin_externalpage_* functions. For example, suppose you wanted
742 /// to add a foo.php file into admin. First off, you add the following line to
743 /// admin/settings/first.php (at the end of the file) or to some other file in
746 /// $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'),
747 /// $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission'));
749 /// Next, in foo.php, your file structure would resemble the following:
751 /// require_once('.../config.php');
752 /// require_once($CFG->libdir.'/adminlib.php');
753 /// admin_externalpage_setup('foo');
754 /// // functionality like processing form submissions goes here
755 /// admin_externalpage_print_header();
756 /// // your HTML goes here
757 /// admin_externalpage_print_footer();
759 /// The admin_externalpage_setup() function call ensures the user is logged in,
760 /// and makes sure that they have the proper role permission to access the page.
762 /// The admin_externalpage_print_header() function prints the header (it figures
763 /// out what category and subcategories the page is classified under) and ensures
764 /// that you're using the admin pagelib (which provides the admin tree block and
765 /// the admin bookmarks block).
767 /// The admin_externalpage_print_footer() function properly closes the tables
768 /// opened up by the admin_externalpage_print_header() function and prints the
769 /// standard Moodle footer.
771 /// ADMIN_CATEGORY OBJECTS
773 /// Above and beyond all this, we have admin_category objects. These objects
774 /// appear as folders in the admin tree block. They contain admin_settingpage's,
775 /// admin_externalpage's, and other admin_category's.
779 /// admin_settingpage's, admin_externalpage's, and admin_category's all inherit
780 /// from part_of_admin_tree (a pseudointerface). This interface insists that
781 /// a class has a check_access method for access permissions, a locate method
782 /// used to find a specific node in the admin tree, and a path method used
783 /// to determine the path to a specific node in the $ADMIN tree.
785 /// admin_category's inherit from parentable_part_of_admin_tree. This pseudo-
786 /// interface ensures that the class implements a recursive add function which
787 /// accepts a part_of_admin_tree object and searches for the proper place to
788 /// put it. parentable_part_of_admin_tree implies part_of_admin_tree.
790 /// Please note that the $this->name field of any part_of_admin_tree must be
791 /// UNIQUE throughout the ENTIRE admin tree.
793 /// The $this->name field of an admin_setting object (which is *not* part_of_
794 /// admin_tree) must be unique on the respective admin_settingpage where it is
798 /// MISCELLANEOUS STUFF (used by classes defined below) ///////////////////////
799 include_once($CFG->dirroot
. '/backup/lib.php');
801 /// CLASS DEFINITIONS /////////////////////////////////////////////////////////
804 * Pseudointerface for anything appearing in the admin tree
806 * The pseudointerface that is implemented by anything that appears in the admin tree
807 * block. It forces inheriting classes to define a method for checking user permissions
808 * and methods for finding something in the admin tree.
810 * @author Vincenzo K. Marcovecchio
813 class part_of_admin_tree
{
816 * Finds a named part_of_admin_tree.
818 * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree
819 * and not parentable_part_of_admin_tree, then this function should only check if
820 * $this->name matches $name. If it does, it should return a reference to $this,
821 * otherwise, it should return a reference to NULL.
823 * If a class inherits parentable_part_of_admin_tree, this method should be called
824 * recursively on all child objects (assuming, of course, the parent object's name
825 * doesn't match the search criterion).
827 * @param string $name The internal name of the part_of_admin_tree we're searching for.
828 * @return mixed An object reference or a NULL reference.
830 function &locate($name) {
831 trigger_error('Admin class does not implement method <strong>locate()</strong>', E_USER_WARNING
);
836 * Removes named part_of_admin_tree.
838 * @param string $name The internal name of the part_of_admin_tree we want to remove.
839 * @return bool success.
841 function prune($name) {
842 trigger_error('Admin class does not implement method <strong>prune()</strong>', E_USER_WARNING
);
847 * Verifies current user's access to this part_of_admin_tree.
849 * Used to check if the current user has access to this part of the admin tree or
850 * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree,
851 * then this method is usually just a call to has_capability() in the site context.
853 * If a class inherits parentable_part_of_admin_tree, this method should return the
854 * logical OR of the return of check_access() on all child objects.
856 * @return bool True if the user has access, false if she doesn't.
858 function check_access() {
859 trigger_error('Admin class does not implement method <strong>check_access()</strong>', E_USER_WARNING
);
864 * Mostly usefull for removing of some parts of the tree in admin tree block.
866 * @return True is hidden from normal list view
868 function is_hidden() {
869 trigger_error('Admin class does not implement method <strong>is_hidden()</strong>', E_USER_WARNING
);
874 * Determines the path to $name in the admin tree.
876 * Used to determine the path to $name in the admin tree. If a class inherits only
877 * part_of_admin_tree and not parentable_part_of_admin_tree, then this method should
878 * check if $this->name matches $name. If it does, $name is pushed onto the $path
879 * array (at the end), and $path should be returned. If it doesn't, NULL should be
882 * If a class inherits parentable_part_of_admin_tree, it should do the above, but not
883 * return NULL on failure. Instead, it pushes $this->name onto $path, and then
884 * recursively calls path() on its child objects. If any are non-NULL, it should
885 * return $path (being certain that the last element of $path is equal to $name).
886 * If they are all NULL, it returns NULL.
888 * @param string $name The internal name of the part_of_admin_tree we're searching for.
889 * @param array $path Not used on external calls. Defaults to empty array.
890 * @return mixed If found, an array containing the internal names of each part_of_admin_tree that leads to $name. If not found, NULL.
892 function path($name, $path = array()) {
893 trigger_error('Admin class does not implement method <strong>path()</strong>', E_USER_WARNING
);
899 * Pseudointerface implemented by any part_of_admin_tree that has children.
901 * The pseudointerface implemented by any part_of_admin_tree that can be a parent
902 * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart
903 * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods
904 * include an add method for adding other part_of_admin_tree objects as children.
906 * @author Vincenzo K. Marcovecchio
909 class parentable_part_of_admin_tree
extends part_of_admin_tree
{
912 * Adds a part_of_admin_tree object to the admin tree.
914 * Used to add a part_of_admin_tree object to this object or a child of this
915 * object. $something should only be added if $destinationname matches
916 * $this->name. If it doesn't, add should be called on child objects that are
917 * also parentable_part_of_admin_tree's.
919 * @param string $destinationname The internal name of the new parent for $something.
920 * @param part_of_admin_tree &$something The object to be added.
921 * @return bool True on success, false on failure.
923 function add($destinationname, &$something) {
924 trigger_error('Admin class does not implement method <strong>add()</strong>', E_USER_WARNING
);
931 * The object used to represent folders (a.k.a. categories) in the admin tree block.
933 * Each admin_category object contains a number of part_of_admin_tree objects.
935 * @author Vincenzo K. Marcovecchio
938 class admin_category
extends parentable_part_of_admin_tree
{
941 * @var mixed An array of part_of_admin_tree objects that are this object's children
946 * @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
951 * @var string The displayed name for this category. Usually obtained through get_string()
956 * @var bool Should this category be hidden in admin tree block?
960 // constructor for an empty admin category
961 // $name is the internal name of the category. it MUST be unique in the entire hierarchy
962 // $visiblename is the displayed name of the category. use a get_string for this
965 * Constructor for an empty admin category
967 * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
968 * @param string $visiblename The displayed named for this category. Usually obtained through get_string()
969 * @param bool $hidden hide category in admin tree block
970 * @return mixed Returns the new object.
972 function admin_category($name, $visiblename, $hidden = false) {
973 $this->children
= array();
975 $this->visiblename
= $visiblename;
976 $this->hidden
= $hidden;
980 * Finds the path to the part_of_admin_tree called $name.
982 * @param string $name The internal name that we're searching for.
983 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
984 * @return mixed An array of internal names that leads to $name, or NULL if not found.
986 function path($name, $path = array()) {
988 $path[count($path)] = $this->name
;
990 if ($this->name
== $name) {
994 foreach($this->children
as $child) {
995 if ($return = $child->path($name, $path)) {
1005 * Returns a reference to the part_of_admin_tree object with internal name $name.
1007 * @param string $name The internal name of the object we want.
1008 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1010 function &locate($name) {
1012 if ($this->name
== $name) {
1016 foreach($this->children
as $child) {
1017 if ($return =& $child->locate($name)) {
1026 * Removes part_of_admin_tree object with internal name $name.
1028 * @param string $name The internal name of the object we want to remove.
1029 * @return bool success
1031 function prune($name) {
1033 if ($this->name
== $name) {
1034 return false; //can not remove itself
1037 foreach($this->children
as $precedence => $child) {
1038 if ($child->name
== $name) {
1040 unset($this->children
[$precedence]);
1043 if ($this->children
[$precedence]->prune($name)) {
1051 * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object.
1053 * @param string $destinationame The internal name of the immediate parent that we want for &$something.
1054 * @param mixed &$something A part_of_admin_tree object to be added.
1055 * @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".
1056 * @return bool True if successfully added, false if &$something is not a part_of_admin_tree or if $name is not found.
1058 function add($destinationname, &$something, $precedence = '') {
1060 if (!is_a($something, 'part_of_admin_tree')) {
1064 if ($destinationname == $this->name
) {
1065 if ($precedence === '') {
1066 $this->children
[] = $something;
1068 if (isset($this->children
[$precedence])) { // this should never, ever be triggered in a release version of moodle.
1069 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 />');
1071 $this->children
[$precedence] = $something;
1078 $entries = array_keys($this->children
);
1080 foreach($entries as $entry) {
1081 $child =& $this->children
[$entry];
1082 if (is_a($child, 'parentable_part_of_admin_tree')) {
1083 if ($child->add($destinationname, $something, $precedence)) {
1094 * Checks if the user has access to anything in this category.
1096 * @return bool True if the user has access to atleast one child in this category, false otherwise.
1098 function check_access() {
1101 foreach ($this->children
as $child) {
1102 $return = $return ||
$child->check_access();
1110 * Is this category hidden in admin tree block?
1112 * @return bool True if hidden
1114 function is_hidden() {
1115 return $this->hidden
;
1120 * Links external PHP pages into the admin tree.
1122 * See detailed usage example at the top of this document (adminlib.php)
1124 * @author Vincenzo K. Marcovecchio
1127 class admin_externalpage
extends part_of_admin_tree
{
1130 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1135 * @var string The displayed name for this external page. Usually obtained through get_string().
1140 * @var string The external URL that we should link to when someone requests this external page.
1145 * @var string The role capability/permission a user must have to access this external page.
1147 var $req_capability;
1150 * @var object The context in which capability/permission should be checked, default is site context.
1155 * @var bool hidden in admin tree block.
1160 * Constructor for adding an external page into the admin tree.
1162 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1163 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1164 * @param string $url The external URL that we should link to when someone requests this external page.
1165 * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
1167 function admin_externalpage($name, $visiblename, $url, $req_capability = 'moodle/site:config', $hidden=false, $context=NULL) {
1168 $this->name
= $name;
1169 $this->visiblename
= $visiblename;
1171 if (is_array($req_capability)) {
1172 $this->req_capability
= $req_capability;
1174 $this->req_capability
= array($req_capability);
1176 $this->hidden
= $hidden;
1177 $this->context
= $context;
1181 * Finds the path to the part_of_admin_tree called $name.
1183 * @param string $name The internal name that we're searching for.
1184 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
1185 * @return mixed An array of internal names that leads to $name, or NULL if not found.
1187 function path($name, $path = array()) {
1188 if ($name == $this->name
) {
1189 array_push($path, $this->name
);
1197 * Returns a reference to the part_of_admin_tree object with internal name $name.
1199 * @param string $name The internal name of the object we want.
1200 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1202 function &locate($name) {
1203 $return = ($this->name
== $name ?
$this : NULL);
1207 function prune($name) {
1212 * Determines if the current user has access to this external page based on $this->req_capability.
1214 * @uses CONTEXT_SYSTEM
1216 * @return bool True if user has access, false otherwise.
1218 function check_access() {
1220 return true; // no access check before site is fully set up
1222 $context = empty($this->context
) ?
get_context_instance(CONTEXT_SYSTEM
) : $this->context
;
1223 foreach($this->req_capability
as $cap) {
1224 if (has_capability($cap, $context)) {
1232 * Is this external page hidden in admin tree block?
1234 * @return bool True if hidden
1236 function is_hidden() {
1237 return $this->hidden
;
1243 * Used to group a number of admin_setting objects into a page and add them to the admin tree.
1245 * @author Vincenzo K. Marcovecchio
1248 class admin_settingpage
extends part_of_admin_tree
{
1251 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1256 * @var string The displayed name for this external page. Usually obtained through get_string().
1260 * @var mixed An array of admin_setting objects that are part of this setting page.
1265 * @var string The role capability/permission a user must have to access this external page.
1267 var $req_capability;
1270 * @var object The context in which capability/permission should be checked, default is site context.
1275 * @var bool hidden in admin tree block.
1279 // see admin_category
1280 function path($name, $path = array()) {
1281 if ($name == $this->name
) {
1282 array_push($path, $this->name
);
1289 // see admin_category
1290 function &locate($name) {
1291 $return = ($this->name
== $name ?
$this : NULL);
1295 function prune($name) {
1299 // see admin_externalpage
1300 function admin_settingpage($name, $visiblename, $req_capability = 'moodle/site:config', $hidden=false, $context=NULL) {
1302 $this->settings
= new stdClass();
1303 $this->name
= $name;
1304 $this->visiblename
= $visiblename;
1305 if (is_array($req_capability)) {
1306 $this->req_capability
= $req_capability;
1308 $this->req_capability
= array($req_capability);
1310 $this->hidden
= false;
1311 $this->context
= $context;
1314 // 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
1315 // n.b. each admin_setting in an admin_settingpage must have a unique internal name
1316 // &$setting is the admin_setting object you want to add
1317 // returns true if successful, false if not (will fail if &$setting is an admin_setting or child thereof)
1318 function add(&$setting) {
1319 if (is_a($setting, 'admin_setting')) {
1320 $this->settings
->{$setting->name
} =& $setting;
1326 // see admin_externalpage
1327 function check_access() {
1329 return true; // no access check before site is fully set up
1331 $context = empty($this->context
) ?
get_context_instance(CONTEXT_SYSTEM
) : $this->context
;
1332 foreach($this->req_capability
as $cap) {
1333 if (has_capability($cap, $context)) {
1340 // outputs this page as html in a table (suitable for inclusion in an admin pagetype)
1341 // returns a string of the html
1342 function output_html() {
1343 $return = '<fieldset>' . "\n";
1344 $return .= '<div class="clearer"><!-- --></div>' . "\n";
1345 foreach($this->settings
as $setting) {
1346 $return .= $setting->output_html();
1348 $return .= '</fieldset>';
1352 // 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
1353 // -- calls write_setting() to each child setting, sending it only the data that matches each setting's internal name
1354 // $data should be the result from data_submitted()
1355 // returns an empty string if everything went well, otherwise returns a printable error string (that's language-specific)
1356 function write_settings($data) {
1358 foreach($this->settings
as $setting) {
1359 if (isset($data['s_' . $setting->name
])) {
1360 $return .= $setting->write_setting($data['s_' . $setting->name
]);
1362 $return .= $setting->write_setting('');
1369 * Is this settigns page hidden in admin tree block?
1371 * @return bool True if hidden
1373 function is_hidden() {
1374 return $this->hidden
;
1380 // read & write happens at this level; no authentication
1381 class admin_setting
{
1386 var $defaultsetting;
1388 function admin_setting($name, $visiblename, $description, $defaultsetting) {
1389 $this->name
= $name;
1390 $this->visiblename
= $visiblename;
1391 $this->description
= $description;
1392 $this->defaultsetting
= $defaultsetting;
1395 function get_setting() {
1396 return NULL; // has to be overridden
1399 function write_setting($data) {
1400 return; // has to be overridden
1403 function output_html() {
1404 return; // has to be overridden
1410 class admin_setting_configtext
extends admin_setting
{
1414 function admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW
) {
1415 $this->paramtype
= $paramtype;
1416 parent
::admin_setting($name, $visiblename, $description, $defaultsetting);
1419 // returns a string or NULL
1420 function get_setting() {
1422 return (isset($CFG->{$this->name
}) ?
$CFG->{$this->name
} : NULL);
1425 // $data is a string
1426 function write_setting($data) {
1427 if (!$this->validate($data)) {
1428 return get_string('validateerror', 'admin') . $this->visiblename
. '<br />';
1430 return (set_config($this->name
,$data) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1433 function validate($data) {
1434 if (is_string($this->paramtype
)) {
1435 return preg_match($this->paramtype
, $data);
1436 } else if ($this->paramtype
=== PARAM_RAW
) {
1439 $cleaned = clean_param($data, $this->paramtype
);
1440 return ("$data" == "$cleaned"); // implicit conversion to string is needed to do exact comparison
1444 function output_html() {
1445 if ($this->get_setting() === NULL) {
1446 $current = $this->defaultsetting
;
1448 $current = $this->get_setting();
1450 return format_admin_setting($this->name
, $this->visiblename
,
1451 '<input type="text" class="form-text" id="id_s_'.$this->name
.'" name="s_'.$this->name
.'" value="'.s($current).'" />',
1452 $this->description
);
1457 class admin_setting_configpasswordunmask
extends admin_setting_configtext
{
1459 function admin_setting_configpasswordunmask($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW
) {
1460 parent
::admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype);
1463 function output_html() {
1464 if ($this->get_setting() === NULL) {
1465 $current = $this->defaultsetting
;
1467 $current = $this->get_setting();
1469 $id = 'id_s_'.$this->name
;
1470 $unmask = get_string('unmaskpassword', 'form');
1471 $unmaskjs = '<script type="text/javascript">
1473 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>\');
1476 return format_admin_setting($this->name
, $this->visiblename
,
1477 '<input type="password" class="form-text" id="id_s_'.$this->name
.'" name="s_'.$this->name
.'" value="'.s($current).'" />'.$unmaskjs,
1478 $this->description
);
1483 class admin_setting_configcheckbox
extends admin_setting
{
1485 function admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting) {
1486 parent
::admin_setting($name, $visiblename, $description, $defaultsetting);
1489 function get_setting() {
1491 return (isset($CFG->{$this->name
}) ?
$CFG->{$this->name
} : NULL);
1494 function write_setting($data) {
1496 return (set_config($this->name
,1) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1498 return (set_config($this->name
,0) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1502 function output_html() {
1503 if ($this->get_setting() === NULL) {
1504 $current = $this->defaultsetting
;
1506 $current = $this->get_setting();
1508 return format_admin_setting($this->name
, $this->visiblename
,
1509 '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name
.'" name="s_'. $this->name
.'" value="1" ' . ($current == true ?
'checked="checked"' : '') . ' />',
1510 $this->description
);
1515 class admin_setting_configselect
extends admin_setting
{
1519 function admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices) {
1520 $this->choices
= $choices;
1521 parent
::admin_setting($name, $visiblename, $description, $defaultsetting);
1524 function get_setting() {
1526 return (isset($CFG->{$this->name
}) ?
$CFG->{$this->name
} : NULL);
1529 function write_setting($data) {
1530 // check that what we got was in the original choices
1531 // or that the data is the default setting - needed during install when choices can not be constructed yet
1532 if ($data != $this->defaultsetting
and ! in_array($data, array_keys($this->choices
))) {
1533 return 'Error setting ' . $this->visiblename
. '<br />';
1536 return (set_config($this->name
, $data) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1539 function output_html() {
1540 if ($this->get_setting() === NULL) {
1541 $current = $this->defaultsetting
;
1543 $current = $this->get_setting();
1545 $return = '<select class="form-select" id="id_s_'.$this->name
.'" name="s_' . $this->name
.'">';
1546 foreach ($this->choices
as $key => $value) {
1547 // the string cast is needed because key may be integer - 0 is equal to most strings!
1548 $return .= '<option value="'.$key.'"'.((string)$key==$current ?
' selected="selected"' : '').'>'.$value.'</option>';
1550 $return .= '</select>';
1552 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
);
1557 // 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
1558 // internally for the setting)
1559 class admin_setting_configtime
extends admin_setting
{
1565 function admin_setting_configtime($hoursname, $minutesname, $visiblename, $description, $defaultsetting) {
1566 $this->name2
= $minutesname;
1567 $this->choices
= array();
1568 for ($i = 0; $i < 24; $i++
) {
1569 $this->choices
[$i] = $i;
1571 $this->choices2
= array();
1572 for ($i = 0; $i < 60; $i +
= 5) {
1573 $this->choices2
[$i] = $i;
1575 parent
::admin_setting($hoursname, $visiblename, $description, $defaultsetting);
1578 function get_setting() {
1580 return (isset($CFG->{$this->name
}) && isset($CFG->{$this->name2
}) ?
array('h' => $CFG->{$this->name
}, 'm' => $CFG->{$this->name2
}) : NULL);
1583 function write_setting($data) {
1584 // check that what we got was in the original choices
1585 if (!(in_array($data['h'], array_keys($this->choices
)) && in_array($data['m'], array_keys($this->choices2
)))) {
1586 return get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
1589 return (set_config($this->name
, $data['h']) && set_config($this->name2
, $data['m']) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1592 function output_html() {
1593 if ($this->get_setting() === NULL) {
1594 $currentsetting = $this->defaultsetting
;
1596 $currentsetting = $this->get_setting();
1598 $return = '<div class="form-group">'.
1599 '<select class="form-select" id="id_s_'.$this->name
.'h" name="s_' . $this->name
.'[h]">';
1600 foreach ($this->choices
as $key => $value) {
1601 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['h'] ?
' selected="selected"' : '') . '>' . $value . '</option>';
1603 $return .= '</select>:<select class="form-select" id="id_s_'.$this->name
.'m" name="s_' . $this->name
. '[m]">';
1604 foreach ($this->choices2
as $key => $value) {
1605 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['m'] ?
' selected="selected"' : '') . '>' . $value . '</option>';
1607 $return .= '</select></div>';
1608 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
, false);
1613 class admin_setting_configmultiselect
extends admin_setting_configselect
{
1615 function admin_setting_configmultiselect($name, $visiblename, $description, $defaultsetting, $choices) {
1616 parent
::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
1619 function get_setting() {
1621 if (isset($CFG->{$this->name
})) {
1622 if ($CFG->{$this->name
}) {
1623 return explode(',', $CFG->{$this->name
});
1632 function write_setting($data) {
1636 foreach ($data as $datum) {
1637 if (! in_array($datum, array_keys($this->choices
))) {
1638 return get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
1642 return (set_config($this->name
, implode(',',$data)) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1645 function output_html() {
1646 if ($this->get_setting() === NULL) {
1647 $currentsetting = $this->defaultsetting
;
1648 if (!$currentsetting) {
1649 $currentsetting = array();
1652 $currentsetting = $this->get_setting();
1654 $return = '<select class="form-select" id="id_s_'.$this->name
.'" name="s_' . $this->name
.'[]" size="10" multiple="multiple">';
1655 foreach ($this->choices
as $key => $value) {
1656 $return .= '<option value="' . $key . '"' . (in_array($key,$currentsetting) ?
' selected="selected"' : '') . '>' . $value . '</option>';
1658 $return .= '</select>';
1659 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
);
1664 class admin_setting_special_adminseesall
extends admin_setting_configcheckbox
{
1666 function admin_setting_special_adminseesall() {
1667 $name = 'calendar_adminseesall';
1668 $visiblename = get_string('adminseesall', 'admin');
1669 $description = get_string('helpadminseesall', 'admin');
1670 parent
::admin_setting($name, $visiblename, $description, 0);
1673 function write_setting($data) {
1675 unset($SESSION->cal_courses_shown
);
1676 parent
::write_setting($data);
1680 class admin_setting_sitesetselect
extends admin_setting_configselect
{
1684 function admin_setting_sitesetselect($name, $visiblename, $description, $defaultsetting, $choices) {
1687 parent
::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
1691 function get_setting() {
1693 return (isset($site->{$this->name
}) ?
$site->{$this->name
} : NULL);
1696 function write_setting($data) {
1697 if (!in_array($data, array_keys($this->choices
))) {
1698 return get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
1700 $record = new stdClass();
1701 $record->id
= $this->id
;
1702 $temp = $this->name
;
1703 $record->$temp = $data;
1704 $record->timemodified
= time();
1705 return (update_record('course', $record) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1711 class admin_setting_courselist_frontpage
extends admin_setting_configselect
{
1713 function admin_setting_courselist_frontpage($loggedin) {
1715 require_once($CFG->dirroot
. '/course/lib.php');
1716 $name = 'frontpage' . ($loggedin ?
'loggedin' : '');
1717 $visiblename = get_string('frontpage' . ($loggedin ?
'loggedin' : ''),'admin');
1718 $description = get_string('configfrontpage' . ($loggedin ?
'loggedin' : ''),'admin');
1719 $choices = array(FRONTPAGENEWS
=> get_string('frontpagenews'),
1720 FRONTPAGECOURSELIST
=> get_string('frontpagecourselist'),
1721 FRONTPAGECATEGORYNAMES
=> get_string('frontpagecategorynames'),
1722 FRONTPAGECATEGORYCOMBO
=> get_string('frontpagecategorycombo'),
1723 '' => get_string('none'));
1724 if (!$loggedin and count_records("course") > FRONTPAGECOURSELIMIT
) {
1725 unset($choices[FRONTPAGECOURSELIST
]);
1727 $defaults = FRONTPAGECOURSELIST
.',,,';
1728 parent
::admin_setting_configselect($name, $visiblename, $description, $defaults, $choices);
1731 function get_setting() {
1733 return (isset($CFG->{$this->name
}) ?
explode(',', $CFG->{$this->name
}) : ',1,,');
1736 function write_setting($data) {
1739 } if (!is_array($data)) {
1740 $data = explode(',', $data);
1742 foreach($data as $datum) {
1743 if (! in_array($datum, array_keys($this->choices
))) {
1744 return get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
1747 return (set_config($this->name
, implode(',', $data)) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1750 function output_html() {
1751 if ($this->get_setting() === NULL) {
1752 $currentsetting = $this->defaultsetting
;
1754 $currentsetting = $this->get_setting();
1756 for ($i = 0; $i < count($this->choices
) - 1; $i++
) {
1757 if (!isset($currentsetting[$i])) {
1758 $currentsetting[$i] = 0;
1761 $return = '<div class="form-group">';
1762 for ($i = 0; $i < count($this->choices
) - 1; $i++
) {
1763 $return .='<select class="form-select" id="id_s_'.$this->name
.$i.'" name="s_' . $this->name
.'[]">';
1764 foreach ($this->choices
as $key => $value) {
1765 $return .= '<option value="' . $key . '"' . ($key == $currentsetting[$i] ?
' selected="selected"' : '') . '>' . $value . '</option>';
1767 $return .= '</select>';
1768 if ($i !== count($this->choices
) - 2) {
1769 $return .= '<br />';
1772 $return .= '</div>';
1774 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
, false);
1778 class admin_setting_sitesetcheckbox
extends admin_setting_configcheckbox
{
1782 function admin_setting_sitesetcheckbox($name, $visiblename, $description, $defaultsetting) {
1785 parent
::admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting);
1789 function get_setting() {
1791 return (isset($site->{$this->name
}) ?
$site->{$this->name
} : NULL);
1794 function write_setting($data) {
1795 $record = new stdClass();
1796 $record->id
= $this->id
;
1797 $temp = $this->name
;
1798 $record->$temp = ($data == '1' ?
1 : 0);
1799 $record->timemodified
= time();
1800 return (update_record('course', $record) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1805 class admin_setting_sitesettext
extends admin_setting_configtext
{
1809 function admin_setting_sitesettext($name, $visiblename, $description, $defaultsetting) {
1812 parent
::admin_setting_configtext($name, $visiblename, $description, $defaultsetting);
1816 function get_setting() {
1818 return (isset($site->{$this->name
}) ?
$site->{$this->name
} : NULL);
1821 function validate($data) {
1822 $cleaned = stripslashes(clean_param($data, PARAM_MULTILANG
));
1823 if ($cleaned == '') {
1824 return false; // can not be empty
1826 return ($data == $cleaned); // implicit conversion to string is needed to do exact comparison
1829 function write_setting($data) {
1830 $data = trim($data);
1831 if (!$this->validate($data)) {
1832 return get_string('validateerror', 'admin') . $this->visiblename
. '<br />';
1835 $record = new stdClass();
1836 $record->id
= $this->id
;
1837 $record->{$this->name
} = addslashes($data);
1838 $record->timemodified
= time();
1839 return (update_record('course', $record) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1844 class admin_setting_special_frontpagedesc
extends admin_setting
{
1848 function admin_setting_special_frontpagedesc() {
1851 $visiblename = get_string('frontpagedescription');
1852 $description = get_string('frontpagedescriptionhelp');
1853 parent
::admin_setting($name, $visiblename, $description, '');
1856 function output_html() {
1860 if ($this->get_setting() === NULL) {
1861 $currentsetting = $this->defaultsetting
;
1863 $currentsetting = $this->get_setting();
1866 $CFG->adminusehtmleditor
= can_use_html_editor();
1868 $return = print_textarea($CFG->adminusehtmleditor
, 15, 60, 0, 0, 's_' . $this->name
, $currentsetting, 0, true);
1870 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
, false);
1873 function get_setting() {
1876 return (isset($site->{$this->name
}) ?
$site->{$this->name
} : NULL);
1880 function write_setting($data) {
1882 $data = addslashes(clean_param($data, PARAM_CLEANHTML
));
1884 $record = new stdClass();
1885 $record->id
= $this->id
;
1886 $temp = $this->name
;
1887 $record->$temp = $data;
1888 $record->timemodified
= time();
1890 return(update_record('course', $record) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1897 class admin_setting_special_editorfontlist
extends admin_setting
{
1901 function admin_setting_special_editorfontlist() {
1903 $name = 'editorfontlist';
1904 $visiblename = get_string('editorfontlist', 'admin');
1905 $description = get_string('configeditorfontlist', 'admin');
1906 $defaults = array('k0' => 'Trebuchet',
1907 'v0' => 'Trebuchet MS,Verdana,Arial,Helvetica,sans-serif',
1909 'v1' => 'arial,helvetica,sans-serif',
1910 'k2' => 'Courier New',
1911 'v2' => 'courier new,courier,monospace',
1913 'v3' => 'georgia,times new roman,times,serif',
1915 'v4' => 'tahoma,arial,helvetica,sans-serif',
1916 'k5' => 'Times New Roman',
1917 'v5' => 'times new roman,times,serif',
1919 'v6' => 'verdana,arial,helvetica,sans-serif',
1922 'k8' => 'Wingdings',
1923 'v8' => 'wingdings');
1924 parent
::admin_setting($name, $visiblename, $description, $defaults);
1927 function get_setting() {
1929 if (isset($CFG->editorfontlist
)) {
1931 $currentsetting = array();
1932 $items = explode(';', $CFG->editorfontlist
);
1933 foreach ($items as $item) {
1934 $item = explode(':', $item);
1935 $currentsetting['k' . $i] = $item[0];
1936 $currentsetting['v' . $i] = $item[1];
1939 return $currentsetting;
1945 function write_setting($data) {
1947 // there miiight be an easier way to do this :)
1948 // if this is changed, make sure the $defaults array above is modified so that this
1949 // function processes it correctly
1954 foreach ($data as $key => $value) {
1955 if (substr($key,0,1) == 'k') {
1956 $keys[substr($key,1)] = $value;
1957 } elseif (substr($key,0,1) == 'v') {
1958 $values[substr($key,1)] = $value;
1963 for ($i = 0; $i < count($keys); $i++
) {
1964 if (($keys[$i] !== '') && ($values[$i] !== '')) {
1965 $result .= clean_param($keys[$i],PARAM_NOTAGS
) . ':' . clean_param($values[$i], PARAM_NOTAGS
) . ';';
1969 $result = substr($result, 0, -1); // trim the last semicolon
1971 return (set_config($this->name
, $result) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
1974 function output_html() {
1976 if ($this->get_setting() === NULL) {
1977 $currentsetting = $this->defaultsetting
;
1979 $currentsetting = $this->get_setting();
1982 $return = '<div class="form-group">';
1983 for ($i = 0; $i < count($currentsetting) / 2; $i++
) {
1984 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . $i . ']" value="' . $currentsetting['k' . $i] . '" />';
1985 $return .= ' ';
1986 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . $i . ']" value="' . $currentsetting['v' . $i] . '" /><br />';
1988 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . $i . ']" value="" />';
1989 $return .= ' ';
1990 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . $i . ']" value="" /><br />';
1991 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . ($i +
1) . ']" value="" />';
1992 $return .= ' ';
1993 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . ($i +
1) . ']" value="" />';
1994 $return .= '</div>';
1996 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
, false);
2001 class admin_setting_special_editordictionary
extends admin_setting_configselect
{
2003 function admin_setting_special_editordictionary() {
2004 $name = 'editordictionary';
2005 $visiblename = get_string('editordictionary','admin');
2006 $description = get_string('configeditordictionary', 'admin');
2007 $choices = $this->editor_get_dictionaries();
2008 if (! is_array($choices)) {
2009 $choices = array('');
2012 parent
::admin_setting_configselect($name, $visiblename, $description, '', $choices);
2015 // function borrowed from the old moodle/admin/editor.php, slightly modified
2016 function editor_get_dictionaries () {
2017 /// Get all installed dictionaries in the system
2021 // error_reporting(E_ALL); // for debug, final version shouldn't have this...
2024 // If aspellpath isn't set don't even bother ;-)
2025 if (empty($CFG->aspellpath
)) {
2026 return 'Empty aspell path!';
2029 // Do we have access to popen function?
2030 if (!function_exists('popen')) {
2031 return 'Popen function disabled!';
2034 $cmd = $CFG->aspellpath
;
2036 $dictionaries = array();
2039 if(!($handle = @popen
(escapeshellarg($cmd) .' dump dicts', 'r'))) {
2040 return 'Couldn\'t create handle!';
2043 while(!feof($handle)) {
2044 $output .= fread($handle, 1024);
2048 $dictionaries = explode(chr(10), $output);
2050 // Get rid of possible empty values
2051 if (is_array($dictionaries)) {
2053 $cnt = count($dictionaries);
2055 for ($i = 0; $i < $cnt; $i++
) {
2056 if (!empty($dictionaries[$i])) {
2057 $dicts[$dictionaries[$i]] = $dictionaries[$i];
2062 if (count($dicts) >= 1) {
2066 return 'Error! Check your aspell installation!';
2074 class admin_setting_special_editorhidebuttons
extends admin_setting
{
2081 function admin_setting_special_editorhidebuttons() {
2082 $this->name
= 'editorhidebuttons';
2083 $this->visiblename
= get_string('editorhidebuttons', 'admin');
2084 $this->description
= get_string('confeditorhidebuttons', 'admin');
2085 $this->defaultsetting
= array();
2086 // weird array... buttonname => buttonimage (assume proper path appended). if you leave buttomimage blank, text will be printed instead
2087 $this->items
= array('fontname' => '',
2089 'formatblock' => '',
2090 'bold' => 'ed_format_bold.gif',
2091 'italic' => 'ed_format_italic.gif',
2092 'underline' => 'ed_format_underline.gif',
2093 'strikethrough' => 'ed_format_strike.gif',
2094 'subscript' => 'ed_format_sub.gif',
2095 'superscript' => 'ed_format_sup.gif',
2096 'copy' => 'ed_copy.gif',
2097 'cut' => 'ed_cut.gif',
2098 'paste' => 'ed_paste.gif',
2099 'clean' => 'ed_wordclean.gif',
2100 'undo' => 'ed_undo.gif',
2101 'redo' => 'ed_redo.gif',
2102 'justifyleft' => 'ed_align_left.gif',
2103 'justifycenter' => 'ed_align_center.gif',
2104 'justifyright' => 'ed_align_right.gif',
2105 'justifyfull' => 'ed_align_justify.gif',
2106 'lefttoright' => 'ed_left_to_right.gif',
2107 'righttoleft' => 'ed_right_to_left.gif',
2108 'insertorderedlist' => 'ed_list_num.gif',
2109 'insertunorderedlist' => 'ed_list_bullet.gif',
2110 'outdent' => 'ed_indent_less.gif',
2111 'indent' => 'ed_indent_more.gif',
2112 'forecolor' => 'ed_color_fg.gif',
2113 'hilitecolor' => 'ed_color_bg.gif',
2114 'inserthorizontalrule' => 'ed_hr.gif',
2115 'createanchor' => 'ed_anchor.gif',
2116 'createlink' => 'ed_link.gif',
2117 'unlink' => 'ed_unlink.gif',
2118 'insertimage' => 'ed_image.gif',
2119 'inserttable' => 'insert_table.gif',
2120 'insertsmile' => 'em.icon.smile.gif',
2121 'insertchar' => 'icon_ins_char.gif',
2122 'spellcheck' => 'spell-check.gif',
2123 'htmlmode' => 'ed_html.gif',
2124 'popupeditor' => 'fullscreen_maximize.gif',
2125 'search_replace' => 'ed_replace.gif');
2128 function get_setting() {
2130 return (isset($CFG->{$this->name
}) ?
explode(' ', $CFG->{$this->name
}) : NULL);
2133 function write_setting($data) {
2135 if (empty($data)) { $data = array(); }
2136 foreach ($data as $key => $value) {
2137 if (!in_array($key, array_keys($this->items
))) {
2138 return get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
2140 if ($value == '1') {
2144 return (set_config($this->name
, implode(' ',$result)) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2147 function output_html() {
2151 // checkboxes with input name="$this->name[$key]" value="1"
2152 // we do 15 fields per column
2154 if ($this->get_setting() === NULL) {
2155 $currentsetting = $this->defaultsetting
;
2157 $currentsetting = $this->get_setting();
2160 $return = '<div class="form-group">';
2161 $return .= '<table><tr><td valign="top" align="right">';
2165 foreach($this->items
as $key => $value) {
2166 if ($count %
15 == 0 and $count != 0) {
2167 $return .= '</td><td valign="top" align="right">';
2170 $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') . '" />') . ' ';
2171 $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"' : '') . ' /> ';
2173 if ($count %
15 != 0) {
2174 $return .= '<br /><br />';
2178 $return .= '</td></tr>';
2179 $return .= '</table>';
2180 $return .= '</div>';
2182 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
, false);
2187 class admin_setting_langlist
extends admin_setting_configtext
{
2188 function admin_setting_langlist() {
2189 parent
::admin_setting_configtext('langlist', get_string('langlist', 'admin'), get_string('configlanglist', 'admin'), '', PARAM_NOTAGS
);
2192 function write_setting($data) {
2193 $return = parent
::write_setting($data);
2194 get_list_of_languages(true);//refresh the list
2199 class admin_setting_backupselect
extends admin_setting_configselect
{
2201 function admin_setting_backupselect($name, $visiblename, $description, $default, $choices) {
2202 parent
::admin_setting_configselect($name, $visiblename, $description, $default, $choices);
2205 function get_setting() {
2206 $backup_config = backup_get_config();
2207 return (isset($backup_config->{$this->name
}) ?
$backup_config->{$this->name
} : NULL);
2210 function write_setting($data) {
2211 // check that what we got was in the original choices
2212 if (! in_array($data, array_keys($this->choices
))) {
2213 return get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
2216 return (backup_set_config($this->name
, $data) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2221 class admin_setting_special_backupsaveto
extends admin_setting_configtext
{
2223 function admin_setting_special_backupsaveto() {
2224 $name = 'backup_sche_destination';
2225 $visiblename = get_string('saveto');
2226 $description = get_string('backupsavetohelp');
2227 parent
::admin_setting_configtext($name, $visiblename, $description, '');
2230 function get_setting() {
2231 $backup_config = backup_get_config();
2232 return (isset($backup_config->{$this->name
}) ?
$backup_config->{$this->name
} : NULL);
2235 function write_setting($data) {
2236 $data = trim($data);
2237 if (!empty($data) and !is_dir($data)) {
2238 return get_string('pathnotexists') . '<br />';
2240 return (backup_set_config($this->name
, $data) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2245 class admin_setting_backupcheckbox
extends admin_setting_configcheckbox
{
2247 function admin_setting_backupcheckbox($name, $visiblename, $description, $default) {
2248 parent
::admin_setting_configcheckbox($name, $visiblename, $description, $default);
2251 function write_setting($data) {
2253 return (backup_set_config($this->name
, 1) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2255 return (backup_set_config($this->name
, 0) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2259 function get_setting() {
2260 $backup_config = backup_get_config();
2261 return (isset($backup_config->{$this->name
}) ?
$backup_config->{$this->name
} : NULL);
2266 class admin_setting_special_backuptime
extends admin_setting_configtime
{
2268 function admin_setting_special_backuptime() {
2269 $name = 'backup_sche_hour';
2270 $name2 = 'backup_sche_minute';
2271 $visiblename = get_string('executeat');
2272 $description = get_string('backupexecuteathelp');
2273 $default = array('h' => 0, 'm' => 0);
2274 parent
::admin_setting_configtime($name, $name2, $visiblename, $description, $default);
2277 function get_setting() {
2278 $backup_config = backup_get_config();
2279 return (isset($backup_config->{$this->name
}) && isset($backup_config->{$this->name
}) ?
array('h'=>$backup_config->{$this->name
}, 'm'=>$backup_config->{$this->name2
}) : NULL);
2282 function write_setting($data) {
2283 // check that what we got was in the original choices
2284 if (!(in_array($data['h'], array_keys($this->choices
)) && in_array($data['m'], array_keys($this->choices2
)))) {
2285 return get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
2288 return (backup_set_config($this->name
, $data['h']) && backup_set_config($this->name2
, $data['m']) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2293 class admin_setting_special_backupdays
extends admin_setting
{
2295 function admin_setting_special_backupdays() {
2296 $name = 'backup_sche_weekdays';
2297 $visiblename = get_string('schedule');
2298 $description = get_string('backupschedulehelp');
2299 $default = array('u' => 0, 'm' => 0, 't' => 0, 'w' => 0, 'r' => 0, 'f' => 0, 's' => 0);
2300 parent
::admin_setting($name, $visiblename, $description, $default);
2303 function get_setting() {
2304 $backup_config = backup_get_config();
2305 if (isset($backup_config->{$this->name
})) {
2306 $currentsetting = $backup_config->{$this->name
};
2307 return array('u' => substr($currentsetting, 0, 1),
2308 'm' => substr($currentsetting, 1, 1),
2309 't' => substr($currentsetting, 2, 1),
2310 'w' => substr($currentsetting, 3, 1),
2311 'r' => substr($currentsetting, 4, 1),
2312 'f' => substr($currentsetting, 5, 1),
2313 's' => substr($currentsetting, 6, 1));
2319 function output_html() {
2321 if ($this->get_setting() === NULL) {
2322 $currentsetting = $this->defaultsetting
;
2324 $currentsetting = $this->get_setting();
2327 // rewrite for simplicity
2328 $currentsetting = $currentsetting['u'] . $currentsetting['m'] . $currentsetting['t'] . $currentsetting['w'] .
2329 $currentsetting['r'] . $currentsetting['f'] . $currentsetting['s'];
2331 $return = '<table><tr><td><div style="text-align:center"> ' . get_string('sunday', 'calendar') . ' </div></td><td><div style="text-align:center"> ' .
2332 get_string('monday', 'calendar') . ' </div></td><td><div style="text-align:center"> ' . get_string('tuesday', 'calendar') . ' </div></td><td><div style="text-align:center"> ' .
2333 get_string('wednesday', 'calendar') . ' </div></td><td><div style="text-align:center"> ' . get_string('thursday', 'calendar') . ' </div></td><td><div style="text-align:center"> ' .
2334 get_string('friday', 'calendar') . ' </div></td><td><div style="text-align:center"> ' . get_string('saturday', 'calendar') . ' </div></td></tr><tr>' .
2335 '<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>' .
2336 '<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>' .
2337 '<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>' .
2338 '<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>' .
2339 '<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>' .
2340 '<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>' .
2341 '<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>' .
2344 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
, false);
2348 // 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
2349 // admin_settingpage (note that admin_settingpage only calls write_setting with the data that matches $this->name... so if we have multiple form fields,
2350 // they MUST go into an array named $this->name, or else we won't receive them here
2351 function write_setting($data) {
2353 $result = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
2354 if (!empty($data)) {
2355 foreach($data as $key => $value) {
2356 if ($value == '1') {
2357 $result[strpos($week, $key)] = 1;
2361 return (backup_set_config($this->name
, implode('',$result)) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2365 class admin_setting_special_debug
extends admin_setting_configselect
{
2367 function admin_setting_special_debug() {
2369 $visiblename = get_string('debug', 'admin');
2370 $description = get_string('configdebug', 'admin');
2371 $choices = array( DEBUG_NONE
=> get_string('debugnone', 'admin'),
2372 DEBUG_MINIMAL
=> get_string('debugminimal', 'admin'),
2373 DEBUG_NORMAL
=> get_string('debugnormal', 'admin'),
2374 DEBUG_ALL
=> get_string('debugall', 'admin'),
2375 DEBUG_DEVELOPER
=> get_string('debugdeveloper', 'admin')
2377 parent
::admin_setting_configselect($name, $visiblename, $description, '', $choices);
2380 function get_setting() {
2382 if (isset($CFG->debug
)) {
2389 function write_setting($data) {
2390 return (set_config($this->name
,$data) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2396 class admin_setting_special_calendar_weekend
extends admin_setting
{
2398 function admin_setting_special_calendar_weekend() {
2399 $name = 'calendar_weekend';
2400 $visiblename = get_string('calendar_weekend', 'admin');
2401 $description = get_string('helpweekenddays', 'admin');
2402 $default = array ('0', '6'); // Saturdays and Sundays
2403 parent
::admin_setting($name, $visiblename, $description, $default);
2406 function get_setting() {
2408 return isset($CFG->{$this->name
}) ?
$CFG->{$this->name
} : 0;
2411 function write_setting($data) {
2413 if (!empty($data)) {
2414 foreach($data as $index) {
2415 $result |
= 1 << $index;
2418 return (set_config($this->name
, $result) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2421 function output_html() {
2422 if ($this->get_setting() === NULL) {
2423 $currentsetting = $this->defaultsetting
;
2425 $currentsetting = $this->get_setting();
2428 // The order matters very much because of the implied numeric keys
2429 $days = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
2430 $return = '<table><thead><tr>';
2431 foreach($days as $index => $day) {
2432 $return .= '<td><label for="id_s_'.$this->name
.$index.'">'.get_string($day, 'calendar').'</label></td>';
2434 $return .= '</tr></thead><tbody><tr>';
2435 foreach($days as $index => $day) {
2436 $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>';
2438 $return .= '</tr></tbody></table>';
2440 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
, false);
2447 * this is used in config->appearance->gradeconfig
2449 class admin_setting_special_gradebookroles
extends admin_setting
{
2451 function admin_setting_special_gradebookroles() {
2452 $name = 'gradebookroles';
2453 $visiblename = get_string('gradebookroles', 'admin');
2454 $description = get_string('configgradebookroles', 'admin');
2455 $default = array(5=>'1'); // The student role in a default install
2456 parent
::admin_setting($name, $visiblename, $description, $default);
2459 function get_setting() {
2461 if (!empty($CFG->{$this->name
})) {
2462 $result = explode(',', $CFG->{$this->name
});
2463 foreach ($result as $roleid) {
2464 $array[$roleid] = 1;
2472 function write_setting($data) {
2473 if (!empty($data)) {
2475 foreach ($data as $key => $value) {
2480 return set_config($this->name
, rtrim($str, ","))?
'':get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
2482 return set_config($this->name
, '')?
'':get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
2486 function output_html() {
2488 if ($this->get_setting() === NULL) {
2489 $currentsetting = $this->defaultsetting
;
2491 $currentsetting = $this->get_setting();
2493 // from to process which roles to display
2494 if ($roles = get_records('role')) {
2495 $return = '<div class="form-group">';
2497 foreach ($roles as $roleid=>$role) {
2498 if (is_array($currentsetting) && in_array($roleid, array_keys($currentsetting))) {
2499 $checked = ' checked="checked"';
2506 $return .= '<br />';
2508 $return .= '<input type="checkbox" name="s_'.$this->name
.'['.$roleid.']" value="1"'.$checked.' /> '.format_string($role->name
);
2510 $return .= '</div>';
2513 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
, false);
2520 * this is used in config->appearance->coursemanager
2521 * (which roles to show on course decription page)
2523 class admin_setting_special_coursemanager
extends admin_setting
{
2525 function admin_setting_special_coursemanager() {
2526 $name = 'coursemanager';
2527 $visiblename = get_string('coursemanager', 'admin');
2528 $description = get_string('configcoursemanager', 'admin');
2529 $default = array(3=>'1'); // The teahcer role in a default install
2530 parent
::admin_setting($name, $visiblename, $description, $default);
2533 function get_setting() {
2536 if (!empty($CFG->{$this->name
})) {
2537 $result = explode(',', $CFG->{$this->name
});
2538 foreach ($result as $roleid) {
2539 $array[$roleid] = 1;
2542 } else if (isset($CFG->{$this->name
})) {
2549 function write_setting($data) {
2551 if (!empty($data)) {
2553 foreach ($data as $key => $value) {
2558 return set_config($this->name
, rtrim($str, ","))?
'':get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
2560 return set_config($this->name
, '')?
'':get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
2564 function output_html() {
2566 if ($this->get_setting() === NULL) {
2567 $currentsetting = $this->defaultsetting
;
2569 $currentsetting = $this->get_setting();
2571 // from to process which roles to display
2572 if ($roles = get_records_select('role', '', 'sortorder')) {
2573 $return = '<div class="form-group">';
2575 foreach ($roles as $roleid=>$role) {
2576 if (is_array($currentsetting) && in_array($roleid, array_keys($currentsetting))) {
2577 $checked = 'checked="checked"';
2584 $return .= '<br />';
2586 $return .= '<input type="checkbox" name="s_'.$this->name
.'['.$roleid.']" value="1" '.$checked.' /> '.$role->name
;
2588 $return .= '</div>';
2590 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
, false);
2595 * this is used in config->courses->gradeexports
2596 * (which roles to show on course decription page)
2598 class admin_setting_special_gradeexport
extends admin_setting
{
2600 function admin_setting_special_gradeexport() {
2601 $name = 'gradeexport';
2602 $visiblename = get_string('gradeexport', 'admin');
2603 $description = get_string('configgradeexport', 'admin');
2604 $default = array(3=>'1'); // The teahcer role in a default install
2605 parent
::admin_setting($name, $visiblename, $description, $default);
2608 function get_setting() {
2611 if (!empty($CFG->{$this->name
})) {
2612 $result = explode(',', $CFG->{$this->name
});
2613 foreach ($result as $plugin) {
2614 $array[$plugin] = 1;
2617 } else if (isset($CFG->{$this->name
})) {
2624 function write_setting($data) {
2626 if (!empty($data)) {
2628 foreach ($data as $key => $value) {
2633 return set_config($this->name
, rtrim($str, ","))?
'':get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
2635 return set_config($this->name
, '')?
'':get_string('errorsetting', 'admin') . $this->visiblename
. '<br />';
2639 function output_html() {
2641 if ($this->get_setting() === NULL) {
2642 $currentsetting = $this->defaultsetting
;
2644 $currentsetting = $this->get_setting();
2646 // from to process which roles to display
2647 if ($exports = get_list_of_plugins('grade/export')) {
2648 $return = '<div class="form-group">';
2650 foreach ($exports as $export) {
2651 if (is_array($currentsetting) && in_array($export, array_keys($currentsetting))) {
2652 $checked = 'checked="checked"';
2659 $return .= '<br />';
2661 $return .= '<input type="checkbox" name="s_'.$this->name
.'['.$export.']" value="1" '.$checked.' /> '.$export;
2663 $return .= '</div>';
2665 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
, false);
2672 class admin_setting_special_perfdebug
extends admin_setting_configcheckbox
{
2674 function admin_setting_special_perfdebug() {
2675 $name = 'perfdebug';
2676 $visiblename = get_string('perfdebug', 'admin');
2677 $description = get_string('configperfdebug', 'admin');
2678 parent
::admin_setting_configcheckbox($name, $visiblename, $description, '');
2681 function write_setting($data) {
2684 return (set_config($this->name
,15) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2686 return (set_config($this->name
,7) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2690 function output_html() {
2692 if ($this->get_setting() === NULL) {
2693 $currentsetting = $this->defaultsetting
;
2695 $currentsetting = $this->get_setting();
2698 $return = '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name
.'" name="s_'. $this->name
.'" value="1" ' . ($currentsetting == 15 ?
'checked="checked"' : '') . ' />';
2699 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
);
2704 class admin_setting_special_debugdisplay
extends admin_setting_configcheckbox
{
2706 function admin_setting_special_debugdisplay() {
2707 $name = 'debugdisplay';
2708 $visiblename = get_string('debugdisplay', 'admin');
2709 $description = get_string('configdebugdisplay', 'admin');
2710 $default = ini_get('display_errors');
2711 parent
::admin_setting_configcheckbox($name, $visiblename, $description, $default);
2714 function write_setting($data) {
2717 return (set_config($this->name
,1) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2719 return (set_config($this->name
,0) ?
'' : get_string('errorsetting', 'admin') . $this->visiblename
. '<br />');
2723 function output_html() {
2725 if ($this->get_setting() === NULL) {
2726 $currentsetting = $this->defaultsetting
;
2728 $currentsetting = $this->get_setting();
2731 $return = '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name
.'" name="s_'. $this->name
.'" value="1" ' . ($currentsetting == 1 ?
'checked="checked"' : '') . ' />';
2732 return format_admin_setting($this->name
, $this->visiblename
, $return, $this->description
);
2738 // Code for a function that helps externalpages print proper headers and footers
2739 // N.B.: THIS FUNCTION HANDLES AUTHENTICATION
2740 function admin_externalpage_setup($section) {
2742 global $CFG, $PAGE, $USER;
2744 $adminroot = admin_get_root();
2746 require_once($CFG->libdir
. '/blocklib.php');
2747 require_once($CFG->dirroot
. '/'.$CFG->admin
.'/pagelib.php');
2749 page_map_class(PAGE_ADMIN
, 'page_admin');
2751 $PAGE = page_create_object(PAGE_ADMIN
, 0); // there must be any constant id number
2753 $PAGE->init_extra($section); // hack alert!
2755 $root = $adminroot->locate($PAGE->section
);
2757 if ($site = get_site()) {
2760 redirect($CFG->wwwroot
. '/'.$CFG->admin
.'/index.php');
2764 if (!is_a($root, 'admin_externalpage')) {
2765 error(get_string('sectionerror','admin'));
2769 // this eliminates our need to authenticate on the actual pages
2770 if (!($root->check_access())) {
2771 error(get_string('accessdenied', 'admin'));
2775 $adminediting = optional_param('adminedit', -1, PARAM_BOOL
);
2777 if (!isset($USER->adminediting
)) {
2778 $USER->adminediting
= false;
2781 if ($PAGE->user_allowed_editing()) {
2782 if ($adminediting == 1) {
2783 $USER->adminediting
= true;
2784 } elseif ($adminediting == 0) {
2785 $USER->adminediting
= false;
2791 function admin_externalpage_print_header() {
2793 global $CFG, $PAGE, $SITE, $THEME;
2795 if (!empty($THEME->customcorners
)) {
2796 require_once($CFG->dirroot
.'/lib/custom_corners_lib.php');
2799 define('ADMIN_EXT_HEADER_PRINTED', 'true');
2801 if (!empty($SITE->fullname
)) {
2802 $pageblocks = blocks_setup($PAGE);
2804 $preferred_width_left = bounded_number(BLOCK_L_MIN_WIDTH
,
2805 blocks_preferred_width($pageblocks[BLOCK_POS_LEFT
]),
2807 $PAGE->print_header();
2808 echo '<table id="layout-table" summary=""><tr>';
2809 echo '<td style="width: ' . $preferred_width_left . 'px;" id="left-column">';
2810 if (!empty($THEME->customcorners
)) print_custom_corners_start();
2811 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT
);
2812 if (!empty($THEME->customcorners
)) print_custom_corners_end();
2814 echo '<td id="middle-column">';
2815 if (!empty($THEME->customcorners
)) print_custom_corners_start();
2821 function admin_externalpage_print_footer() {
2823 global $CFG, $PAGE, $SITE, $THEME;
2825 if (!empty($THEME->customcorners
)) {
2826 require_once($CFG->dirroot
.'/lib/custom_corners_lib.php');
2829 define('ADMIN_EXT_FOOTER_PRINTED', 'true');
2831 if (!empty($SITE->fullname
)) {
2832 $pageblocks = blocks_setup($PAGE);
2833 $preferred_width_right = bounded_number(BLOCK_R_MIN_WIDTH
,
2834 blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT
]),
2836 if (!empty($THEME->customcorners
)) print_custom_corners_end();
2838 if (blocks_have_content($pageblocks, BLOCK_POS_RIGHT
)) {
2839 echo '<td style="width: ' . $preferred_width_right . 'px;" id="right-column">';
2840 if (!empty($THEME->customcorners
)) print_custom_corners_start();
2841 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT
);
2842 if (!empty($THEME->customcorners
)) print_custom_corners_end();
2845 echo '</tr></table>';
2850 function admin_get_root() {
2855 if (!isset($ADMIN)) {
2856 // start the admin tree!
2857 $ADMIN = new admin_category('root', get_string("administration"));
2858 // we process this file first to get categories up and running
2859 include($CFG->dirroot
. '/'.$CFG->admin
.'/settings/top.php');
2861 // now we process all other files in admin/settings to build the
2863 foreach (glob($CFG->dirroot
. '/'.$CFG->admin
.'/settings/*.php') as $file) {
2864 if ($file != $CFG->dirroot
. '/'.$CFG->admin
.'/settings/top.php') {
2865 include_once($file);
2873 /// settings utiliti functions
2875 // n.b. this function unconditionally applies default settings
2876 function apply_default_settings(&$node) {
2880 if (is_a($node, 'admin_category')) {
2881 $entries = array_keys($node->children
);
2882 foreach ($entries as $entry) {
2883 apply_default_settings($node->children
[$entry]);
2888 if (is_a($node, 'admin_settingpage')) {
2889 foreach ($node->settings
as $setting) {
2890 $CFG->{$setting->name
} = $setting->defaultsetting
;
2891 $setting->write_setting($setting->defaultsetting
);
2892 unset($setting); // needed to prevent odd (imho) reference behaviour
2893 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
2902 // n.b. this function unconditionally applies default settings
2903 function apply_default_exception_settings($defaults) {
2907 foreach($defaults as $key => $value) {
2908 $CFG->$key = $value;
2909 set_config($key, $value);
2914 function format_admin_setting($name, $title='', $form='', $description='', $label=true) {
2916 // sometimes the id is not id_s_name, but id_s_name_m or something, and this does not validate
2918 $labelfor = 'for = "id_s_'.$name.'"';
2924 '<div class="form-item" id="admin-'.$name.'">'."\n".
2925 '<label '.$labelfor.'>'.$title."\n".
2926 ' <span class="form-shortname">'.$name.'</span>'."\n".
2929 '<div class="description">'.$description.'</div>'."\n".
2937 * Try to upgrade the given language pack (or current language)
2938 * If it doesn't work, fail silently and return false
2940 function upgrade_language_pack($lang='') {
2944 $lang = current_language();
2947 if ($lang == 'en_utf8') {
2948 return true; // Nothing to do
2951 notify(get_string('langimport', 'admin').': '.$lang.' ... ', 'notifysuccess');
2953 @mkdir
($CFG->dataroot
.'/temp/'); //make it in case it's a fresh install, it might not be there
2954 @mkdir
($CFG->dataroot
.'/lang/');
2956 require_once($CFG->libdir
.'/componentlib.class.php');
2958 if ($cd = new component_installer('http://download.moodle.org', 'lang16', $lang.'.zip', 'languages.md5', 'lang')) {
2959 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
2961 if ($status == INSTALLED
) {
2962 debugging('Downloading successful: '.$lang);
2963 @unlink
($CFG->dataroot
.'/cache/languages');
2972 * Based on find_new_settings{@link ()} in upgradesettings.php
2973 * Looks to find any admin settings that have not been initialized. Returns 1 if it finds any.
2975 * @param string &$node The node at which to start searching.
2976 * @return int Returns 1 if any settings haven't been initialised, 0 if they all have
2978 function any_new_admin_settings(&$node) {
2980 if (is_a($node, 'admin_category')) {
2981 $entries = array_keys($node->children
);
2982 foreach ($entries as $entry) {
2983 if( any_new_admin_settings($node->children
[$entry]) ){
2989 if (is_a($node, 'admin_settingpage')) {
2990 foreach ($node->settings
as $setting) {
2991 if ($setting->get_setting() === NULL) {