MDL-20636 Fix upgrade of numerical questions.
[moodle.git] / admin / index.php
blob47ce50c19ab8270e19921cc5e2073f0db5110665
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Main administration script.
21 * @package core
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // Check that config.php exists, if not then call the install script
27 if (!file_exists('../config.php')) {
28 header('Location: ../install.php');
29 die;
32 // Check that PHP is of a sufficient version as soon as possible
33 if (version_compare(phpversion(), '5.2.0') < 0) {
34 $phpversion = phpversion();
35 // do NOT localise - lang strings would not work here and we CAN NOT move it to later place
36 echo "Sorry, Moodle 2.0 requires PHP 5.2.8 or later (currently using version $phpversion). ";
37 echo "Please upgrade your server software or use latest Moodle 1.9.x instead.";
38 die;
41 define('NO_OUTPUT_BUFFERING', true);
43 require('../config.php');
44 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
45 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
47 $id = optional_param('id', '', PARAM_TEXT);
48 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
49 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
50 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
51 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
52 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
54 // Check some PHP server settings
56 $PAGE->set_url('/admin/index.php');
57 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
59 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
61 if (ini_get_bool('session.auto_start')) {
62 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
65 if (ini_get_bool('magic_quotes_runtime')) {
66 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
69 if (!ini_get_bool('file_uploads')) {
70 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
73 if (is_float_problem()) {
74 print_error('phpfloatproblem', 'admin', '', $documentationlink);
77 // Set some necessary variables during set-up to avoid PHP warnings later on this page
78 if (!isset($CFG->framename)) {
79 $CFG->framename = '_top';
81 if (!isset($CFG->release)) {
82 $CFG->release = '';
84 if (!isset($CFG->version)) {
85 $CFG->version = '';
88 $version = null;
89 $release = null;
90 require("$CFG->dirroot/version.php"); // defines $version, $release and $maturity
91 $CFG->target_release = $release; // used during installation and upgrades
93 if (!$version or !$release) {
94 print_error('withoutversion', 'debug'); // without version, stop
97 // Turn off xmlstrictheaders during upgrade.
98 $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
99 $CFG->xmlstrictheaders = false;
101 if (!core_tables_exist()) {
102 $PAGE->set_pagelayout('maintenance');
103 $PAGE->set_popup_notification_allowed(false);
105 // fake some settings
106 $CFG->docroot = 'http://docs.moodle.org';
108 $strinstallation = get_string('installation', 'install');
110 // remove current session content completely
111 session_get_instance()->terminate_current();
113 if (empty($agreelicense)) {
114 $strlicense = get_string('license');
115 $PAGE->navbar->add($strlicense);
116 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
117 $PAGE->set_heading($strinstallation);
118 $PAGE->set_cacheable(false);
119 echo $OUTPUT->header();
120 echo $OUTPUT->heading('<a href="http://moodle.org">Moodle</a> - Modular Object-Oriented Dynamic Learning Environment');
121 echo $OUTPUT->heading(get_string('copyrightnotice'));
122 $copyrightnotice = text_to_html(get_string('gpl3'));
123 $copyrightnotice = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $copyrightnotice); // extremely ugly validation hack
124 echo $OUTPUT->box($copyrightnotice, 'copyrightnotice');
125 echo '<br />';
126 $continue = new single_button(new moodle_url('/admin/index.php', array('lang'=>$CFG->lang, 'agreelicense'=>1)), get_string('continue'), 'get');
127 echo $OUTPUT->confirm(get_string('doyouagree'), $continue, "http://docs.moodle.org/en/License");
128 echo $OUTPUT->footer();
129 die;
131 if (empty($confirmrelease)) {
132 $strcurrentrelease = get_string('currentrelease');
133 $PAGE->navbar->add($strcurrentrelease);
134 $PAGE->set_title($strinstallation);
135 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
136 $PAGE->set_cacheable(false);
137 echo $OUTPUT->header();
138 echo $OUTPUT->heading("Moodle $release");
140 if (isset($maturity)) {
141 // main version.php declares moodle code maturity
142 if ($maturity < MATURITY_STABLE) {
143 $maturitylevel = get_string('maturity'.$maturity, 'admin');
144 echo $OUTPUT->box(
145 $OUTPUT->container(get_string('maturitycorewarning', 'admin', $maturitylevel)) .
146 $OUTPUT->container($OUTPUT->doc_link('admin/versions', get_string('morehelp'))),
147 'generalbox maturitywarning');
151 $releasenoteslink = get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes');
152 $releasenoteslink = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $releasenoteslink); // extremely ugly validation hack
153 echo $OUTPUT->box($releasenoteslink, 'generalbox releasenoteslink');
155 require_once($CFG->libdir.'/environmentlib.php');
156 if (!check_moodle_environment(normalize_version($release), $environment_results, true, ENV_SELECT_RELEASE)) {
157 print_upgrade_reload("index.php?agreelicense=1&amp;lang=$CFG->lang");
158 } else {
159 echo $OUTPUT->notification(get_string('environmentok', 'admin'), 'notifysuccess');
160 echo $OUTPUT->continue_button(new moodle_url('/admin/index.php', array('agreelicense'=>1, 'confirmrelease'=>1, 'lang'=>$CFG->lang)));
163 echo $OUTPUT->footer();
164 die;
167 $strdatabasesetup = get_string('databasesetup');
168 upgrade_init_javascript();
169 $PAGE->navbar->add($strdatabasesetup);
170 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
171 $PAGE->set_heading($strinstallation);
172 $PAGE->set_cacheable(false);
173 echo $OUTPUT->header();
175 if (!$DB->setup_is_unicodedb()) {
176 if (!$DB->change_db_encoding()) {
177 // If could not convert successfully, throw error, and prevent installation
178 print_error('unicoderequired', 'admin');
182 install_core($version, true);
186 // Check version of Moodle code on disk compared with database
187 // and upgrade if possible.
189 $stradministration = get_string('administration');
190 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
192 if (empty($CFG->version)) {
193 print_error('missingconfigversion', 'debug');
196 if ($version > $CFG->version) { // upgrade
197 purge_all_caches();
198 $PAGE->set_pagelayout('maintenance');
199 $PAGE->set_popup_notification_allowed(false);
201 $a->oldversion = "$CFG->release ($CFG->version)";
202 $a->newversion = "$release ($version)";
203 $strdatabasechecking = get_string('databasechecking', '', $a);
205 if (empty($confirmupgrade)) {
206 $PAGE->set_title($stradministration);
207 $PAGE->set_heading($strdatabasechecking);
208 $PAGE->set_cacheable(false);
209 echo $OUTPUT->header();
210 if (isset($maturity)) {
211 // main version.php declares moodle code maturity
212 if ($maturity < MATURITY_STABLE) {
213 $maturitylevel = get_string('maturity'.$maturity, 'admin');
214 echo $OUTPUT->box(
215 $OUTPUT->container(get_string('maturitycorewarning', 'admin', $maturitylevel)) .
216 $OUTPUT->container($OUTPUT->doc_link('admin/versions', get_string('morehelp'))),
217 'generalbox maturitywarning');
220 $continueurl = new moodle_url('index.php', array('confirmupgrade' => 1));
221 $cancelurl = new moodle_url('index.php');
222 echo $OUTPUT->confirm(get_string('upgradesure', 'admin', $a->newversion), $continueurl, $cancelurl);
223 echo $OUTPUT->footer();
224 exit;
226 } else if (empty($confirmrelease)){
227 $strcurrentrelease = get_string('currentrelease');
228 $PAGE->navbar->add($strcurrentrelease);
229 $PAGE->set_title($strcurrentrelease);
230 $PAGE->set_heading($strcurrentrelease);
231 $PAGE->set_cacheable(false);
232 echo $OUTPUT->header();
233 echo $OUTPUT->heading("Moodle $release");
234 $releasenoteslink = get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes');
235 $releasenoteslink = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $releasenoteslink); // extremely ugly validation hack
236 echo $OUTPUT->box($releasenoteslink);
238 require_once($CFG->libdir.'/environmentlib.php');
239 if (!check_moodle_environment($release, $environment_results, true, ENV_SELECT_RELEASE)) {
240 print_upgrade_reload('index.php?confirmupgrade=1');
241 } else {
242 echo $OUTPUT->notification(get_string('environmentok', 'admin'), 'notifysuccess');
243 if (empty($CFG->skiplangupgrade)) {
244 echo $OUTPUT->box_start('generalbox', 'notice');
245 print_string('langpackwillbeupdated', 'admin');
246 echo $OUTPUT->box_end();
248 echo $OUTPUT->continue_button('index.php?confirmupgrade=1&confirmrelease=1');
251 echo $OUTPUT->footer();
252 die;
254 } elseif (empty($confirmplugins)) {
255 $strplugincheck = get_string('plugincheck');
256 $PAGE->navbar->add($strplugincheck);
257 $PAGE->set_title($strplugincheck);
258 $PAGE->set_heading($strplugincheck);
259 $PAGE->set_cacheable(false);
260 $output = $PAGE->get_renderer('core', 'admin');
261 $pluginman = plugin_manager::instance();
263 echo $output->header();
264 echo $output->box_start('generalbox');
265 echo $output->container(get_string('pluginchecknotice', 'core_plugin'), 'generalbox', 'notice');
266 echo $output->plugins_check($pluginman->get_plugins(), array('full' => $showallplugins));
267 echo $output->box_end();
268 print_upgrade_reload('index.php?confirmupgrade=1&amp;confirmrelease=1');
269 $button = new single_button(new moodle_url('index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)), get_string('upgradestart', 'admin'), 'get');
270 $button->class = 'continuebutton';
271 echo $output->render($button);
272 echo $output->footer();
273 die();
275 } else {
276 // Launch main upgrade
277 upgrade_core($version, true);
279 } else if ($version < $CFG->version) {
280 // better stop here, we can not continue with plugin upgrades or anything else
281 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
284 // Updated human-readable release version if necessary
285 if ($release <> $CFG->release) { // Update the release version
286 set_config('release', $release);
289 if (moodle_needs_upgrading()) {
290 if (!$PAGE->headerprinted) {
291 // means core upgrade or installation was not already done
292 if (!$confirmplugins) {
293 $PAGE->set_pagelayout('maintenance');
294 $PAGE->set_popup_notification_allowed(false);
295 $strplugincheck = get_string('plugincheck');
296 $PAGE->navbar->add($strplugincheck);
297 $PAGE->set_title($strplugincheck);
298 $PAGE->set_heading($strplugincheck);
299 $PAGE->set_cacheable(false);
300 $output = $PAGE->get_renderer('core', 'admin');
301 $pluginman = plugin_manager::instance();
303 echo $output->header();
304 echo $output->box_start('generalbox');
305 echo $output->container(get_string('pluginchecknotice', 'core_plugin'), 'generalbox', 'notice');
306 echo $output->plugins_check($pluginman->get_plugins(), array('full' => $showallplugins));
307 echo $output->box_end();
308 print_upgrade_reload('index.php');
309 $button = new single_button(new moodle_url('index.php', array('confirmplugincheck'=>1)), get_string('upgradestart', 'admin'), 'get');
310 $button->class = 'continuebutton';
311 echo $output->render($button);
312 echo $output->footer();
313 die();
316 // install/upgrade all plugins and other parts
317 upgrade_noncore(true);
320 // If this is the first install, indicate that this site is fully configured
321 // except the admin password
322 if (during_initial_install()) {
323 set_config('rolesactive', 1); // after this, during_initial_install will return false.
324 set_config('adminsetuppending', 1);
325 // we need this redirect to setup proper session
326 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
329 // make sure admin user is created - this is the last step because we need
330 // session to be working properly in order to edit admin account
331 if (!empty($CFG->adminsetuppending)) {
332 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
333 if (!$sessionstarted) {
334 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
335 } else {
336 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
337 if (!$sessionverify) {
338 $SESSION->sessionverify = 1;
339 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
340 } else {
341 if (empty($SESSION->sessionverify)) {
342 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
344 unset($SESSION->sessionverify);
348 // at this stage there can be only one admin - users may change username, so do not rely on that
349 $adminuser = get_complete_user_data('id', $CFG->siteadmins);
351 if ($adminuser->password === 'adminsetuppending') {
352 // prevent installation hijacking
353 if ($adminuser->lastip !== getremoteaddr()) {
354 print_error('installhijacked', 'admin');
356 // login user and let him set password and admin details
357 $adminuser->newadminuser = 1;
358 message_set_default_message_preferences($adminuser);
359 complete_user_login($adminuser, false);
360 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
362 } else {
363 unset_config('adminsetuppending');
366 } else {
367 // just make sure upgrade logging is properly terminated
368 upgrade_finished('upgradesettings.php');
371 // Turn xmlstrictheaders back on now.
372 $CFG->xmlstrictheaders = $origxmlstrictheaders;
373 unset($origxmlstrictheaders);
375 // Check for valid admin user - no guest autologin
376 require_login(0, false);
377 $context = get_context_instance(CONTEXT_SYSTEM);
378 require_capability('moodle/site:config', $context);
380 // check that site is properly customized
381 $site = get_site();
382 if (empty($site->shortname)) {
383 // probably new installation - lets return to frontpage after this step
384 // remove settings that we want uninitialised
385 unset_config('registerauth');
386 redirect('upgradesettings.php?return=site');
389 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
390 if (!empty($id) and $id == $CFG->siteidentifier) {
391 set_config('registered', time());
394 // setup critical warnings before printing admin tree block
395 $insecuredataroot = is_dataroot_insecure(true);
396 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
398 $adminroot = admin_get_root();
400 // Check if there are any new admin settings which have still yet to be set
401 if (any_new_admin_settings($adminroot)){
402 redirect('upgradesettings.php');
405 // Everything should now be set up, and the user is an admin
407 // Print default admin page with notifications.
408 admin_externalpage_setup('adminnotifications');
409 echo $OUTPUT->header();
411 // Unstable code warning
412 if (isset($maturity)) {
413 if ($maturity < MATURITY_STABLE) {
414 $maturitylevel = get_string('maturity'.$maturity, 'admin');
415 echo $OUTPUT->box(
416 get_string('maturitycoreinfo', 'admin', $maturitylevel) . ' ' .
417 $OUTPUT->doc_link('admin/versions', get_string('morehelp')),
418 'generalbox adminwarning maturityinfo');
422 if ($insecuredataroot == INSECURE_DATAROOT_WARNING) {
423 echo $OUTPUT->box(get_string('datarootsecuritywarning', 'admin', $CFG->dataroot), 'generalbox adminwarning');
424 } else if ($insecuredataroot == INSECURE_DATAROOT_ERROR) {
425 echo $OUTPUT->box(get_string('datarootsecurityerror', 'admin', $CFG->dataroot), 'generalbox adminerror');
429 if (defined('WARN_DISPLAY_ERRORS_ENABLED')) {
430 echo $OUTPUT->box(get_string('displayerrorswarning', 'admin'), 'generalbox adminwarning');
433 // If no recently cron run
434 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
435 if (time() - $lastcron > 3600 * 24) {
436 $helpbutton = $OUTPUT->help_icon('cron', 'admin');
437 echo $OUTPUT->box(get_string('cronwarning', 'admin').'&nbsp;'.$helpbutton, 'generalbox adminwarning');
440 // Hidden bloglevel upgrade
441 $showbloglevelupgrade = ($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete);
442 if ($showbloglevelupgrade) {
443 echo $OUTPUT->box(get_string('bloglevelupgradenotice', 'admin'), 'generalbox adminwarning');
446 // diagnose DB, especially the sloppy MyISAM tables
447 $diagnose = $DB->diagnose();
448 if ($diagnose !== NULL) {
449 echo $OUTPUT->box($diagnose, 'generalbox adminwarning');
452 // Alert if we are currently in maintenance mode
453 if (!empty($CFG->maintenance_enabled)) {
454 echo $OUTPUT->box(get_string('sitemaintenancewarning2', 'admin', "$CFG->wwwroot/$CFG->admin/settings.php?section=maintenancemode"), 'generalbox adminwarning');
457 //////////////////////////////////////////////////////////////////////////////////////////////////
458 //// IT IS ILLEGAL AND A VIOLATION OF THE GPL TO HIDE, REMOVE OR MODIFY THIS COPYRIGHT NOTICE ///
459 $copyrighttext = '<a href="http://moodle.org/">Moodle</a> '.
460 '<a href="http://docs.moodle.org/en/Release" title="'.$CFG->version.'">'.$CFG->release.'</a><br />'.
461 'Copyright &copy; 1999 onwards, Martin Dougiamas<br />'.
462 'and <a href="http://docs.moodle.org/en/Credits">many other contributors</a>.<br />'.
463 '<a href="http://docs.moodle.org/en/License">GNU Public License</a>';
464 echo $OUTPUT->box($copyrighttext, 'copyright');
465 //////////////////////////////////////////////////////////////////////////////////////////////////
467 echo $OUTPUT->footer();