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 // Technically, we shouldn't need it, but during the
10 // v1.9 accesslib upgrade _sometimes_ USER->access
12 if (!isset($USER->access
)) {
13 load_all_capabilities();
15 admin_externalpage_setup('upgradesettings'); // now hidden page
17 // a caveat: we're depending on only having one admin access this page at once. why? the following line
18 // (the function call to find_new_settings) must have the EXACT SAME RETURN VALUE both times that this
19 // page is loaded (i.e. both when we're displaying the form and then when we process the form's input).
20 // if the return values don't match, we could potentially lose changes that the admin is making.
22 $newsettingshtml = output_new_settings_by_page(admin_get_root());
24 // first we deal with the case where there are no new settings to be set
25 if ($newsettingshtml == '') {
26 redirect($CFG->wwwroot
. '/' . $CFG->admin
. '/index.php');
30 // now we'll deal with the case that the admin has submitted the form with new settings
31 if ($data = data_submitted()) {
32 $unslashed = (array)stripslashes_recursive($data);
33 if (confirm_sesskey()) {
34 $newsettings = find_new_settings(admin_get_root());
37 foreach($newsettings as $newsetting) {
38 if (isset($unslashed['s_' . $newsetting->name
])) {
39 $errors .= $newsetting->write_setting($unslashed['s_' . $newsetting->name
]);
41 $errors .= $newsetting->write_setting($newsetting->defaultsetting
);
46 // there must be either redirect without message or continue button or else upgrade would be sometimes broken
47 redirect($CFG->wwwroot
. '/' . $CFG->admin
. '/index.php');
50 error(get_string('errorwithsettings', 'admin') . ' <br />' . $errors);
54 error(get_string('confirmsesskeybad', 'error'));
60 // and finally, if we get here, then there are new settings and we have to print a form
62 admin_externalpage_print_header();
64 print_simple_box(get_string('upgradesettingsintro','admin'),'','100%','',5,'generalbox','');
66 echo '<form action="upgradesettings.php" method="post" id="adminsettings">';
67 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
69 echo '<div class="clearer"><!-- --></div>';
70 echo $newsettingshtml;
72 echo '<div class="form-buttons"><input class="form-submit" type="submit" value="' . get_string('savechanges','admin') . '" /></div>';
75 admin_externalpage_print_footer();
79 * Find settings that have not been initialized (e.g. during initial install or an upgrade).
81 * Tests each setting's get_setting() method. If the result is NULL, we consider the setting
82 * to be uninitialized.
84 * @param string &$node The node at which to start searching. Should be $ADMIN for all external calls to this function.
85 * @return array An array containing admin_setting objects that haven't yet been initialized
87 function find_new_settings(&$node) {
89 if (is_a($node, 'admin_category')) {
91 $entries = array_keys($node->children
);
92 foreach ($entries as $entry) {
93 $return = array_merge($return, find_new_settings($node->children
[$entry]));
98 if (is_a($node, 'admin_settingpage')) {
100 foreach ($node->settings
as $setting) {
101 if ($setting->get_setting() === NULL) {
102 $return[] =& $setting;
104 unset($setting); // needed to prevent odd (imho) reference behaviour
105 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
114 function output_new_settings_by_page(&$node) {
116 if (is_a($node, 'admin_category')) {
117 $entries = array_keys($node->children
);
119 foreach ($entries as $entry) {
120 $return .= output_new_settings_by_page($node->children
[$entry]);
125 if (is_a($node, 'admin_settingpage')) {
126 $newsettings = array();
127 foreach ($node->settings
as $setting) {
128 if ($setting->get_setting() === NULL) {
129 $newsettings[] =& $setting;
131 unset($setting); // needed to prevent odd (imho) reference behaviour
132 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
135 if (count($newsettings) > 0) {
136 $return .= print_heading(get_string('upgradesettings','admin').' - '.$node->visiblename
, '', 2, 'main', true);
137 $return .= '<fieldset class="adminsettings">' . "\n";
138 foreach ($newsettings as $newsetting) {
139 $return .= '<div class="clearer"><!-- --></div>' . "\n";
140 $return .= $newsetting->output_html();
142 $return .= '</fieldset>';