Moodle release 3.0rc3
[moodle.git] / lib / db / install.php
CommitLineData
117bd748 1<?php
5b4a78e2 2// This file is part of Moodle - http://moodle.org/
1caea91e 3//
5b4a78e2
PS
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file is executed right after the install.xml
19 *
39b90b51
EL
20 * For more information, take a look to the documentation available:
21 * - Upgrade API: {@link http://docs.moodle.org/dev/Upgrade_API}
22 *
23 * @package core_install
24 * @category upgrade
25 * @copyright 2009 Petr Skoda (http://skodak.org)
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5b4a78e2
PS
27 */
28
29defined('MOODLE_INTERNAL') || die();
1caea91e 30
39b90b51
EL
31/**
32 * Main post-install tasks to be executed after the BD schema is available
33 *
34 * This function is automatically executed after Moodle core DB has been
35 * created at initial install. It's in charge of perform the initial tasks
36 * not covered by the {@link install.xml} file, like create initial users,
37 * roles, templates, moving stuff from other plugins...
38 *
39 * Note that the function is only invoked once, at install time, so if new tasks
40 * are needed in the future, they will need to be added both here (for new sites)
41 * and in the corresponding {@link upgrade.php} file (for existing sites).
42 *
43 * All plugins within Moodle (modules, blocks, reports...) support the existence of
44 * their own install.php file, using the "Frankenstyle" component name as
45 * defined at {@link http://docs.moodle.org/dev/Frankenstyle}, for example:
46 * - {@link xmldb_page_install()}. (modules don't require the plugintype ("mod_") to be used.
47 * - {@link xmldb_enrol_meta_install()}.
48 * - {@link xmldb_workshopform_accumulative_install()}.
49 * - ....
50 *
51 * Finally, note that it's also supported to have one uninstall.php file that is
52 * executed also once, each time one plugin is uninstalled (before the DB schema is
53 * deleted). Those uninstall files will contain one function, using the "Frankenstyle"
54 * naming conventions, like {@link xmldb_enrol_meta_uninstall()} or {@link xmldb_workshop_uninstall()}.
55 */
c20ce874 56function xmldb_main_install() {
3912cdd1 57 global $CFG, $DB, $SITE, $OUTPUT;
1caea91e 58
39b90b51 59 // Make sure system context exists
e922fe23
PS
60 $syscontext = context_system::instance(0, MUST_EXIST, false);
61 if ($syscontext->id != SYSCONTEXTID) {
19e7a192 62 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected new system context id!');
1caea91e 63 }
64
65
39b90b51 66 // Create site course
e922fe23
PS
67 if ($DB->record_exists('course', array())) {
68 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create frontpage course, courses already exist.');
69 }
365a5941 70 $newsite = new stdClass();
19e7a192
PS
71 $newsite->fullname = '';
72 $newsite->shortname = '';
73 $newsite->summary = NULL;
74 $newsite->newsitems = 3;
ba0dde97 75 $newsite->numsections = 1;
19e7a192
PS
76 $newsite->category = 0;
77 $newsite->format = 'site'; // Only for this course
c4dd3bb8
PS
78 $newsite->timecreated = time();
79 $newsite->timemodified = $newsite->timecreated;
1caea91e 80
e922fe23
PS
81 if (defined('SITEID')) {
82 $newsite->id = SITEID;
83 $DB->import_record('course', $newsite);
84 $DB->get_manager()->reset_sequence('course');
85 } else {
86 $newsite->id = $DB->insert_record('course', $newsite);
87 define('SITEID', $newsite->id);
88 }
b5cf83f0
MG
89 // set the field 'numsections'. We can not use format_site::update_format_options() because
90 // the file is not loaded
91 $DB->insert_record('course_format_options', array('courseid' => SITEID, 'format' => 'site',
92 'sectionid' => 0, 'name' => 'numsections', 'value' => $newsite->numsections));
1caea91e 93 $SITE = get_site();
e922fe23 94 if ($newsite->id != $SITE->id) {
19e7a192 95 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected new site course id!');
1caea91e 96 }
e922fe23
PS
97 // Make sure site course context exists
98 context_course::instance($SITE->id);
74df2951 99 // Update the global frontpage cache
e922fe23 100 $SITE = $DB->get_record('course', array('id'=>$newsite->id), '*', MUST_EXIST);
1caea91e 101
102
39b90b51 103 // Create default course category
e922fe23
PS
104 if ($DB->record_exists('course_categories', array())) {
105 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default course category, categories already exist.');
106 }
107 $cat = new stdClass();
108 $cat->name = get_string('miscellaneous');
109 $cat->depth = 1;
110 $cat->sortorder = MAX_COURSES_IN_CATEGORY;
111 $cat->timemodified = time();
112 $catid = $DB->insert_record('course_categories', $cat);
113 $DB->set_field('course_categories', 'path', '/'.$catid, array('id'=>$catid));
114 // Make sure category context exists
115 context_coursecat::instance($catid);
1caea91e 116
1caea91e 117
5b8fa09b 118 $defaults = array(
119 'rolesactive' => '0', // marks fully set up system
120 'auth' => 'email',
121 'auth_pop3mailbox' => 'INBOX',
df997f84 122 'enrol_plugins_enabled' => 'manual,guest,self,cohort',
5f0baa43 123 'theme' => theme_config::DEFAULT_THEME,
5b8fa09b 124 'filter_multilang_converted' => 1,
aa282b10 125 'siteidentifier' => random_string(32).get_host_from_url($CFG->wwwroot),
5b8fa09b 126 'backup_version' => 2008111700,
127 'backup_release' => '2.0 dev',
5b8fa09b 128 'mnet_dispatcher_mode' => 'off',
129 'sessiontimeout' => 7200, // must be present during roles installation
130 'stringfilters' => '', // These two are managed in a strange way by the filters
131 'filterall' => 0, // setting page, so have to be initialised here.
9f07f05a 132 'texteditors' => 'atto,tinymce,textarea',
6bd5ca3d 133 'upgrade_minmaxgradestepignored' => 1, // New installs should not run this upgrade step.
156d0486 134 'upgrade_extracreditweightsstepignored' => 1, // New installs should not run this upgrade step.
4d4dcc27 135 'upgrade_calculatedgradeitemsignored' => 1, // New installs should not run this upgrade step.
5b8fa09b 136 );
1caea91e 137 foreach($defaults as $key => $value) {
138 set_config($key, $value);
139 }
140
141
39b90b51 142 // Bootstrap mnet
365a5941 143 $mnethost = new stdClass();
1caea91e 144 $mnethost->wwwroot = $CFG->wwwroot;
145 $mnethost->name = '';
146 $mnethost->name = '';
147 $mnethost->public_key = '';
148
149 if (empty($_SERVER['SERVER_ADDR'])) {
150 // SERVER_ADDR is only returned by Apache-like webservers
151 preg_match("@^(?:http[s]?://)?([A-Z0-9\-\.]+).*@i", $CFG->wwwroot, $matches);
152 $my_hostname = $matches[1];
153 $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH!
154 if ($my_ip == $my_hostname) {
155 $mnethost->ip_address = 'UNKNOWN';
156 } else {
157 $mnethost->ip_address = $my_ip;
158 }
159 } else {
160 $mnethost->ip_address = $_SERVER['SERVER_ADDR'];
161 }
162
163 $mnetid = $DB->insert_record('mnet_host', $mnethost);
164 set_config('mnet_localhost_id', $mnetid);
165
c20ce874 166 // Initial insert of mnet applications info
365a5941 167 $mnet_app = new stdClass();
c20ce874 168 $mnet_app->name = 'moodle';
169 $mnet_app->display_name = 'Moodle';
170 $mnet_app->xmlrpc_server_url = '/mnet/xmlrpc/server.php';
171 $mnet_app->sso_land_url = '/auth/mnet/land.php';
0eadebb1 172 $mnet_app->sso_jump_url = '/auth/mnet/jump.php';
6f4f82f2 173 $moodleapplicationid = $DB->insert_record('mnet_application', $mnet_app);
c20ce874 174
365a5941 175 $mnet_app = new stdClass();
c20ce874 176 $mnet_app->name = 'mahara';
177 $mnet_app->display_name = 'Mahara';
178 $mnet_app->xmlrpc_server_url = '/api/xmlrpc/server.php';
179 $mnet_app->sso_land_url = '/auth/xmlrpc/land.php';
180 $mnet_app->sso_jump_url = '/auth/xmlrpc/jump.php';
181 $DB->insert_record('mnet_application', $mnet_app);
182
6f4f82f2 183 // Set up the probably-to-be-removed-soon 'All hosts' record
365a5941 184 $mnetallhosts = new stdClass();
6f4f82f2
PS
185 $mnetallhosts->wwwroot = '';
186 $mnetallhosts->ip_address = '';
187 $mnetallhosts->public_key = '';
188 $mnetallhosts->public_key_expires = 0;
189 $mnetallhosts->last_connect_time = 0;
190 $mnetallhosts->last_log_id = 0;
191 $mnetallhosts->deleted = 0;
192 $mnetallhosts->name = 'All Hosts';
193 $mnetallhosts->applicationid = $moodleapplicationid;
194 $mnetallhosts->id = $DB->insert_record('mnet_host', $mnetallhosts, true);
195 set_config('mnet_all_hosts_id', $mnetallhosts->id);
88582df4 196
39b90b51 197 // Create guest record - do not assign any role, guest user gets the default guest role automatically on the fly
e922fe23
PS
198 if ($DB->record_exists('user', array())) {
199 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default users, users already exist.');
200 }
365a5941 201 $guest = new stdClass();
88582df4 202 $guest->auth = 'manual';
203 $guest->username = 'guest';
204 $guest->password = hash_internal_user_password('guest');
205 $guest->firstname = get_string('guestuser');
206 $guest->lastname = ' ';
207 $guest->email = 'root@localhost';
208 $guest->description = get_string('guestuserinfo');
209 $guest->mnethostid = $CFG->mnet_localhost_id;
210 $guest->confirmed = 1;
211 $guest->lang = $CFG->lang;
212 $guest->timemodified= time();
213 $guest->id = $DB->insert_record('user', $guest);
19e7a192 214 if ($guest->id != 1) {
3912cdd1 215 echo $OUTPUT->notification('Unexpected id generated for the Guest account. Your database configuration or clustering setup may not be fully supported', 'notifyproblem');
19e7a192
PS
216 }
217 // Store guest id
218 set_config('siteguest', $guest->id);
e922fe23
PS
219 // Make sure user context exists
220 context_user::instance($guest->id);
88582df4 221
222
39b90b51 223 // Now create admin user
365a5941 224 $admin = new stdClass();
88582df4 225 $admin->auth = 'manual';
226 $admin->firstname = get_string('admin');
227 $admin->lastname = get_string('user');
228 $admin->username = 'admin';
229 $admin->password = 'adminsetuppending';
1deff123 230 $admin->email = '';
88582df4 231 $admin->confirmed = 1;
232 $admin->mnethostid = $CFG->mnet_localhost_id;
233 $admin->lang = $CFG->lang;
234 $admin->maildisplay = 1;
235 $admin->timemodified = time();
aa282b10 236 $admin->lastip = CLI_SCRIPT ? '0.0.0.0' : getremoteaddr(); // installation hijacking prevention
88582df4 237 $admin->id = $DB->insert_record('user', $admin);
3912cdd1 238
19e7a192 239 if ($admin->id != 2) {
3912cdd1
SH
240 echo $OUTPUT->notification('Unexpected id generated for the Admin account. Your database configuration or clustering setup may not be fully supported', 'notifyproblem');
241 }
242 if ($admin->id != ($guest->id + 1)) {
243 echo $OUTPUT->notification('Nonconsecutive id generated for the Admin account. Your database configuration or clustering setup may not be fully supported.', 'notifyproblem');
19e7a192 244 }
3912cdd1 245
39b90b51 246 // Store list of admins
4f0c2d00 247 set_config('siteadmins', $admin->id);
e922fe23
PS
248 // Make sure user context exists
249 context_user::instance($admin->id);
88582df4 250
1caea91e 251
39b90b51 252 // Install the roles system.
ff22f229
PS
253 $managerrole = create_role('', 'manager', '', 'manager');
254 $coursecreatorrole = create_role('', 'coursecreator', '', 'coursecreator');
255 $editteacherrole = create_role('', 'editingteacher', '', 'editingteacher');
256 $noneditteacherrole = create_role('', 'teacher', '', 'teacher');
257 $studentrole = create_role('', 'student', '', 'student');
258 $guestrole = create_role('', 'guest', '', 'guest');
259 $userrole = create_role('', 'user', '', 'user');
260 $frontpagerole = create_role('', 'frontpage', '', 'frontpage');
88582df4 261
39b90b51 262 // Now is the correct moment to install capabilities - after creation of legacy roles, but before assigning of roles
88582df4 263 update_capabilities('moodle');
264
88582df4 265
5e72efd4
PS
266 // Default allow role matrices.
267 foreach ($DB->get_records('role') as $role) {
268 foreach (array('assign', 'override', 'switch') as $type) {
269 $function = 'allow_'.$type;
270 $allows = get_default_role_archetype_allows($type, $role->archetype);
271 foreach ($allows as $allowid) {
272 $function($role->id, $allowid);
273 }
274 }
c468795c 275 }
88582df4 276
39b90b51 277 // Set up the context levels where you can assign each role.
4f0c2d00 278 set_role_contextlevels($managerrole, get_default_contextlevels('manager'));
88582df4 279 set_role_contextlevels($coursecreatorrole, get_default_contextlevels('coursecreator'));
280 set_role_contextlevels($editteacherrole, get_default_contextlevels('editingteacher'));
281 set_role_contextlevels($noneditteacherrole, get_default_contextlevels('teacher'));
282 set_role_contextlevels($studentrole, get_default_contextlevels('student'));
283 set_role_contextlevels($guestrole, get_default_contextlevels('guest'));
284 set_role_contextlevels($userrole, get_default_contextlevels('user'));
ac173d3e 285
1635da56
PS
286 // Init theme and JS revisions
287 set_config('themerev', time());
288 set_config('jsrev', time());
b64b465c 289
c7289da7 290 // No admin setting for this any more, GD is now required, remove in Moodle 2.6.
8aae16fb 291 set_config('gdversion', 2);
c7289da7 292
b64b465c
PS
293 // Install licenses
294 require_once($CFG->libdir . '/licenselib.php');
295 license_manager::install_licenses();
03d9401e 296
e922fe23
PS
297 // Init profile pages defaults
298 if ($DB->record_exists('my_pages', array())) {
299 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default profile pages, records already exist.');
300 }
365a5941 301 $mypage = new stdClass();
03d9401e
MD
302 $mypage->userid = NULL;
303 $mypage->name = '__default';
304 $mypage->private = 0;
305 $mypage->sortorder = 0;
e922fe23 306 $DB->insert_record('my_pages', $mypage);
03d9401e 307 $mypage->private = 1;
e922fe23 308 $DB->insert_record('my_pages', $mypage);
9872f0a8
TH
309
310 // Set a sensible default sort order for the most-used question types.
311 set_config('multichoice_sortorder', 1, 'question');
312 set_config('truefalse_sortorder', 2, 'question');
313 set_config('match_sortorder', 3, 'question');
314 set_config('shortanswer_sortorder', 4, 'question');
315 set_config('numerical_sortorder', 5, 'question');
316 set_config('essay_sortorder', 6, 'question');
117bd748 317}