3 // detects settings that were added during an upgrade, displays a screen for the admin to
4 // modify them, and then processes modifications
6 require_once('../config.php');
7 require_once($CFG->libdir
.'/adminlib.php');
9 admin_externalpage_setup('upgradesettings'); // now hidden page
11 // a caveat: we're depending on only having one admin access this page at once. why? the following line
12 // (the function call to find_new_settings) must have the EXACT SAME RETURN VALUE both times that this
13 // page is loaded (i.e. both when we're displaying the form and then when we process the form's input).
14 // if the return values don't match, we could potentially lose changes that the admin is making.
16 $newsettingshtml = output_new_settings_by_page(admin_get_root());
18 // first we deal with the case where there are no new settings to be set
19 if ($newsettingshtml == '') {
20 redirect($CFG->wwwroot
. '/' . $CFG->admin
. '/index.php');
24 // now we'll deal with the case that the admin has submitted the form with new settings
25 if ($data = data_submitted()) {
26 $unslashed = (array)stripslashes_recursive($data);
27 if (confirm_sesskey()) {
28 $newsettings = find_new_settings(admin_get_root());
31 foreach($newsettings as $newsetting) {
32 if (isset($unslashed['s_' . $newsetting->name
])) {
33 $errors .= $newsetting->write_setting($unslashed['s_' . $newsetting->name
]);
35 $errors .= $newsetting->write_setting($newsetting->defaultsetting
);
40 // there must be either redirect without message or continue button or else upgrade would be sometimes broken
41 redirect($CFG->wwwroot
. '/' . $CFG->admin
. '/index.php');
44 error(get_string('errorwithsettings', 'admin') . ' <br />' . $errors);
48 error(get_string('confirmsesskeybad', 'error'));
54 // and finally, if we get here, then there are new settings and we have to print a form
56 admin_externalpage_print_header();
58 print_simple_box(get_string('upgradesettingsintro','admin'),'','100%','',5,'generalbox','');
60 echo '<form action="upgradesettings.php" method="post" id="adminsettings">';
61 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
63 echo '<div class="clearer"><!-- --></div>';
64 echo $newsettingshtml;
66 echo '<div class="form-buttons"><input class="form-submit" type="submit" value="' . get_string('savechanges','admin') . '" /></div>';
69 admin_externalpage_print_footer();
73 * Find settings that have not been initialized (e.g. during initial install or an upgrade).
75 * Tests each setting's get_setting() method. If the result is NULL, we consider the setting
76 * to be uninitialized.
78 * @param string &$node The node at which to start searching. Should be $ADMIN for all external calls to this function.
79 * @return array An array containing admin_setting objects that haven't yet been initialized
81 function find_new_settings(&$node) {
83 if (is_a($node, 'admin_category')) {
85 $entries = array_keys($node->children
);
86 foreach ($entries as $entry) {
87 $return = array_merge($return, find_new_settings($node->children
[$entry]));
92 if (is_a($node, 'admin_settingpage')) {
94 foreach ($node->settings
as $setting) {
95 if ($setting->get_setting() === NULL) {
96 $return[] =& $setting;
98 unset($setting); // needed to prevent odd (imho) reference behaviour
99 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
108 function output_new_settings_by_page(&$node) {
110 if (is_a($node, 'admin_category')) {
111 $entries = array_keys($node->children
);
113 foreach ($entries as $entry) {
114 $return .= output_new_settings_by_page($node->children
[$entry]);
119 if (is_a($node, 'admin_settingpage')) {
120 $newsettings = array();
121 foreach ($node->settings
as $setting) {
122 if ($setting->get_setting() === NULL) {
123 $newsettings[] =& $setting;
125 unset($setting); // needed to prevent odd (imho) reference behaviour
126 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
129 if (count($newsettings) > 0) {
130 $return .= print_heading(get_string('upgradesettings','admin').' - '.$node->visiblename
, '', 2, 'main', true);
131 $return .= '<fieldset class="adminsettings">' . "\n";
132 foreach ($newsettings as $newsetting) {
133 $return .= '<div class="clearer"><!-- --></div>' . "\n";
134 $return .= $newsetting->output_html();
136 $return .= '</fieldset>';