From f1700fab26adb7557022291b696a3ed6c55a6607 Mon Sep 17 00:00:00 2001 From: vinkmar Date: Mon, 28 Aug 2006 06:02:00 +0000 Subject: [PATCH] Install/upgrade procedures changed to use $ADMIN instead of defaults.php and config.php. See http://moodle.org/mod/forum/discuss.php?d=52636 for details --- admin/index.php | 86 +++++++++++++++++++++-------- admin/upgradesettings.php | 111 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 21 deletions(-) create mode 100644 admin/upgradesettings.php diff --git a/admin/index.php b/admin/index.php index 71e34d5229..518f4d4aa2 100644 --- a/admin/index.php +++ b/admin/index.php @@ -144,6 +144,27 @@ if (!update_capabilities()) { error('Had trouble installing the core capabilities for the Roles System'); } + + // Write default settings unconditionally (i.e. even if a setting is already set, overwrite it) + // (this should only have any effect during initial install). + require_once($CFG->dirroot . '/admin/adminlib.php'); + apply_default_settings($ADMIN); + + /// This is used to handle any settings that must exist in $CFG but which do not exist in + /// $ADMIN as admin_setting objects (there are some exceptions). + apply_default_exception_settings(array('alternateloginurl' => '', + 'auth' => 'email', + 'auth_pop3mailbox' => 'INBOX', + 'changepassword' => '', + 'enrol' => 'internal', + 'frontpage' => 1, + 'guestloginbutton' => 1, + 'prefix' => 1, + 'style' => 'default', + 'template' => 'default', + 'textfilters' => 'mod/glossary/dynalink.php', + 'theme' => 'standardwhite')); + notify($strdatabasesuccess, "green"); } else { $db->debug = false; @@ -152,7 +173,7 @@ } else { error("Error: Your database ($CFG->dbtype) is not yet fully supported by Moodle. See the lib/db directory."); } - print_continue("index.php"); + print_continue('index.php'); die; } @@ -211,7 +232,7 @@ if (set_config("version", $version)) { remove_dir($CFG->dataroot . '/cache', true); // flush cache notify($strdatabasesuccess, "green"); - print_continue("index.php"); + print_continue("upgradesettings.php"); exit; } else { notify("Upgrade failed! (Could not update version in config table)"); @@ -262,26 +283,9 @@ exit; } -/// Insert default values for any important configuration variables - - include_once("$CFG->dirroot/lib/defaults.php"); - - foreach ($defaults as $name => $value) { - if (!isset($CFG->$name)) { - $CFG->$name = $value; - set_config($name, $value); - $configchange = true; - } - } - /// Send $CFG->unicodedb to DB to have it available for next requests set_config('unicodedb', $CFG->unicodedb); -/// If any new configurations were found then send to the config page to check - - if (!empty($configchange)) { - redirect("config.php"); - } /// Find and check all main modules and load them up or upgrade them if necessary @@ -373,7 +377,7 @@ /// Print default admin page with notifications. - require_once($CFG->dirroot . '/admin/adminlib.php'); + require_once($CFG->dirroot . '/' . $CFG->admin . '/adminlib.php'); admin_externalpage_setup('adminnotifications'); admin_externalpage_print_header(); @@ -567,4 +571,44 @@ admin_externalpage_print_footer(); -?> +// n.b. this function unconditionally applies default settings +function apply_default_settings(&$node) { + + global $CFG; + + if (is_a($node, 'admin_category')) { + $entries = array_keys($node->children); + foreach ($entries as $entry) { + apply_default_settings($node->children[$entry]); + } + return; + } + + if (is_a($node, 'admin_settingpage')) { + foreach ($node->settings as $setting) { + $CFG->{$setting->name} = $setting->defaultsetting; + $setting->write_setting($setting->defaultsetting); + unset($setting); // needed to prevent odd (imho) reference behaviour + // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399 + } + return; + } + + return; + +} + +// n.b. this function unconditionally applies default settings +function apply_default_exception_settings($defaults) { + + global $CFG; + + foreach($defaults as $key => $value) { + $CFG->$key = $value; + set_config($key, $value); + } + +} + + +?> \ No newline at end of file diff --git a/admin/upgradesettings.php b/admin/upgradesettings.php new file mode 100644 index 0000000000..cc42f17af6 --- /dev/null +++ b/admin/upgradesettings.php @@ -0,0 +1,111 @@ +dirroot . '/' . $CFG->admin . '/adminlib.php'); + +admin_externalpage_setup('adminnotifications'); // we pretend to be the adminnotifications page... don't wanna show up in the menu :) + +// a caveat: we're depending on only having one admin access this page at once. why? the following line +// (the function call to find_new_settings) must have the EXACT SAME RETURN VALUE both times that this +// page is loaded (i.e. both when we're displaying the form and then when we process the form's input). +// if the return values don't match, we could potentially lose changes that the admin is making. +$newsettings = find_new_settings($ADMIN); + +// first we deal with the case where there are no new settings to be set +if (count($newsettings) === 0) { + redirect($CFG->wwwroot . '/' . $CFG->admin . '/index.php', get_string('nonewsettings','admin'),1); + die; +} + +// now we'll deal with the case that the admin has submitted the form with new settings +if ($data = data_submitted()) { + $data = (array)$data; + if (confirm_sesskey()) { + $errors = ''; + + foreach($newsettings as $newsetting) { + if (isset($data['s_' . $newsetting->name])) { + $errors .= $newsetting->write_setting($data['s_' . $newsetting->name]); + } else { + $errors .= $newsetting->write_setting($newsetting->defaultsetting); + } + } + + if (empty($errors)) { + redirect($CFG->wwwroot . '/' . $CFG->admin . '/index.php', get_string('changessaved'),1); + die; + } else { + error(get_string('errorwithsettings', 'admin') . '
' . $errors); + die; + } + } else { + error(get_string('confirmsesskeybad', 'error')); + die; + } + +} + +// and finally, if we get here, then there are new settings and we have to print a form +// to modify them +admin_externalpage_print_header(); + + +echo '
'; +echo ''; +print_simple_box_start('','100%','',5,'generalbox',''); +echo '' . "\n"; +echo ''; +foreach ($newsettings as $newsetting) { + echo $newsetting->output_html(); +} +echo '
' . get_string('modifiedsettingsintro','admin') . '
'; +echo '
'; +print_simple_box_end(); +echo '
'; + +admin_externalpage_print_footer(); + + + +// function that we use (vital to this page working) + +/** + * Find settings that have not been initialized (e.g. during initial install or an upgrade). + * + * Tests each setting's get_setting() method. If the result is NULL, we consider the setting + * to be uninitialized. + * + * @param string &$node The node at which to start searching. Should be $ADMIN for all external calls to this function. + * @return array An array containing admin_setting objects that haven't yet been initialized + */ +function find_new_settings(&$node) { + + if (is_a($node, 'admin_category')) { + $return = array(); + $entries = array_keys($node->children); + foreach ($entries as $entry) { + $return = array_merge($return, find_new_settings($node->children[$entry])); + } + return $return; + } + + if (is_a($node, 'admin_settingpage')) { + $return = array(); + foreach ($node->settings as $setting) { + if ($setting->get_setting() === NULL) { + $return[] =& $setting; + } + unset($setting); // needed to prevent odd (imho) reference behaviour + // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399 + } + return $return; + } + + return array(); + +} + +?> \ No newline at end of file -- 2.39.5