Fixes bug MDL-9175 "Groups simpletest, fatal errors", also DONOTCOMMIT.
[moodle-pu.git] / admin / upgradesettings.php
blob3b6482c4202c651760941fc221de7108bc8a379a
1 <?php // $Id$
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 $adminroot = admin_get_root();
10 admin_externalpage_setup('upgradesettings', $adminroot); // now hidden page
12 // a caveat: we're depending on only having one admin access this page at once. why? the following line
13 // (the function call to find_new_settings) must have the EXACT SAME RETURN VALUE both times that this
14 // page is loaded (i.e. both when we're displaying the form and then when we process the form's input).
15 // if the return values don't match, we could potentially lose changes that the admin is making.
17 $newsettingshtml = output_new_settings_by_page(admin_get_root());
19 // first we deal with the case where there are no new settings to be set
20 if ($newsettingshtml == '') {
21 redirect($CFG->wwwroot . '/' . $CFG->admin . '/index.php');
22 die;
25 // now we'll deal with the case that the admin has submitted the form with new settings
26 if ($data = data_submitted()) {
27 $unslashed = (array)stripslashes_recursive($data);
28 if (confirm_sesskey()) {
29 $newsettings = find_new_settings(admin_get_root());
30 $errors = '';
32 foreach($newsettings as $newsetting) {
33 if (isset($unslashed['s_' . $newsetting->name])) {
34 $errors .= $newsetting->write_setting($unslashed['s_' . $newsetting->name]);
35 } else {
36 $errors .= $newsetting->write_setting($newsetting->defaultsetting);
40 if (empty($errors)) {
41 // there must be either redirect without message or continue button or else upgrade would be sometimes broken
42 redirect($CFG->wwwroot . '/' . $CFG->admin . '/index.php');
43 die;
44 } else {
45 error(get_string('errorwithsettings', 'admin') . ' <br />' . $errors);
46 die;
48 } else {
49 error(get_string('confirmsesskeybad', 'error'));
50 die;
55 // and finally, if we get here, then there are new settings and we have to print a form
56 // to modify them
57 admin_externalpage_print_header($adminroot);
59 print_simple_box(get_string('upgradesettingsintro','admin'),'','100%','',5,'generalbox','');
61 echo '<form action="upgradesettings.php" method="post" id="adminsettings">';
62 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
63 echo '<fieldset>';
64 echo '<div class="clearer"><!-- --></div>';
65 echo $newsettingshtml;
66 echo '</fieldset>';
67 echo '<div class="form-buttons"><input class="form-submit" type="submit" value="' . get_string('savechanges','admin') . '" /></div>';
68 echo '</form>';
70 admin_externalpage_print_footer($adminroot);
73 /**
74 * Find settings that have not been initialized (e.g. during initial install or an upgrade).
76 * Tests each setting's get_setting() method. If the result is NULL, we consider the setting
77 * to be uninitialized.
79 * @param string &$node The node at which to start searching. Should be $ADMIN for all external calls to this function.
80 * @return array An array containing admin_setting objects that haven't yet been initialized
82 function find_new_settings(&$node) {
84 if (is_a($node, 'admin_category')) {
85 $return = array();
86 $entries = array_keys($node->children);
87 foreach ($entries as $entry) {
88 $return = array_merge($return, find_new_settings($node->children[$entry]));
90 return $return;
93 if (is_a($node, 'admin_settingpage')) {
94 $return = array();
95 foreach ($node->settings as $setting) {
96 if ($setting->get_setting() === NULL) {
97 $return[] =& $setting;
99 unset($setting); // needed to prevent odd (imho) reference behaviour
100 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
102 return $return;
105 return array();
109 function output_new_settings_by_page(&$node) {
111 if (is_a($node, 'admin_category')) {
112 $entries = array_keys($node->children);
113 $return = '';
114 foreach ($entries as $entry) {
115 $return .= output_new_settings_by_page($node->children[$entry]);
117 return $return;
120 if (is_a($node, 'admin_settingpage')) {
121 $newsettings = array();
122 foreach ($node->settings as $setting) {
123 if ($setting->get_setting() === NULL) {
124 $newsettings[] =& $setting;
126 unset($setting); // needed to prevent odd (imho) reference behaviour
127 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
129 $return = '';
130 if (count($newsettings) > 0) {
131 $return .= print_heading(get_string('upgradesettings','admin').' - '.$node->visiblename, '', 2, 'main', true);
132 $return .= '<fieldset class="adminsettings">' . "\n";
133 foreach ($newsettings as $newsetting) {
134 $return .= '<div class="clearer"><!-- --></div>' . "\n";
135 $return .= $newsetting->output_html();
137 $return .= '</fieldset>';
139 return $return;
142 return '';