weekly release 2.3dev
[moodle.git] / lib / db / install.php
CommitLineData
117bd748 1<?php
1caea91e 2
5b4a78e2 3// This file is part of Moodle - http://moodle.org/
1caea91e 4//
5b4a78e2
PS
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.
14//
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/>.
17
18/**
19 * This file is executed right after the install.xml
20 *
21 * @package core
22 * @subpackage admin
23 * @copyright 2009 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
1caea91e 28
c20ce874 29function xmldb_main_install() {
3912cdd1 30 global $CFG, $DB, $SITE, $OUTPUT;
1caea91e 31
e922fe23
PS
32 /// Make sure system context exists
33 $syscontext = context_system::instance(0, MUST_EXIST, false);
34 if ($syscontext->id != SYSCONTEXTID) {
19e7a192 35 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected new system context id!');
1caea91e 36 }
37
38
e922fe23
PS
39 /// Create site course
40 if ($DB->record_exists('course', array())) {
41 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create frontpage course, courses already exist.');
42 }
365a5941 43 $newsite = new stdClass();
19e7a192
PS
44 $newsite->fullname = '';
45 $newsite->shortname = '';
46 $newsite->summary = NULL;
47 $newsite->newsitems = 3;
48 $newsite->numsections = 0;
49 $newsite->category = 0;
50 $newsite->format = 'site'; // Only for this course
c4dd3bb8
PS
51 $newsite->timecreated = time();
52 $newsite->timemodified = $newsite->timecreated;
1caea91e 53
e922fe23
PS
54 if (defined('SITEID')) {
55 $newsite->id = SITEID;
56 $DB->import_record('course', $newsite);
57 $DB->get_manager()->reset_sequence('course');
58 } else {
59 $newsite->id = $DB->insert_record('course', $newsite);
60 define('SITEID', $newsite->id);
61 }
1caea91e 62 $SITE = get_site();
e922fe23 63 if ($newsite->id != $SITE->id) {
19e7a192 64 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected new site course id!');
1caea91e 65 }
e922fe23
PS
66 // Make sure site course context exists
67 context_course::instance($SITE->id);
68 // Update the global frontpage cache
69 $SITE = $DB->get_record('course', array('id'=>$newsite->id), '*', MUST_EXIST);
1caea91e 70
71
e922fe23
PS
72 /// Create default course category
73 if ($DB->record_exists('course_categories', array())) {
74 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default course category, categories already exist.');
75 }
76 $cat = new stdClass();
77 $cat->name = get_string('miscellaneous');
78 $cat->depth = 1;
79 $cat->sortorder = MAX_COURSES_IN_CATEGORY;
80 $cat->timemodified = time();
81 $catid = $DB->insert_record('course_categories', $cat);
82 $DB->set_field('course_categories', 'path', '/'.$catid, array('id'=>$catid));
83 // Make sure category context exists
84 context_coursecat::instance($catid);
1caea91e 85
1caea91e 86
5b8fa09b 87 $defaults = array(
88 'rolesactive' => '0', // marks fully set up system
89 'auth' => 'email',
90 'auth_pop3mailbox' => 'INBOX',
df997f84 91 'enrol_plugins_enabled' => 'manual,guest,self,cohort',
5f0baa43 92 'theme' => theme_config::DEFAULT_THEME,
5b8fa09b 93 'filter_multilang_converted' => 1,
aa282b10 94 'siteidentifier' => random_string(32).get_host_from_url($CFG->wwwroot),
5b8fa09b 95 'backup_version' => 2008111700,
96 'backup_release' => '2.0 dev',
5b8fa09b 97 'mnet_dispatcher_mode' => 'off',
98 'sessiontimeout' => 7200, // must be present during roles installation
99 'stringfilters' => '', // These two are managed in a strange way by the filters
100 'filterall' => 0, // setting page, so have to be initialised here.
a5747cf8 101 'texteditors' => 'tinymce,textarea',
5b8fa09b 102 );
1caea91e 103 foreach($defaults as $key => $value) {
104 set_config($key, $value);
105 }
106
107
e922fe23 108 /// Bootstrap mnet
365a5941 109 $mnethost = new stdClass();
1caea91e 110 $mnethost->wwwroot = $CFG->wwwroot;
111 $mnethost->name = '';
112 $mnethost->name = '';
113 $mnethost->public_key = '';
114
115 if (empty($_SERVER['SERVER_ADDR'])) {
116 // SERVER_ADDR is only returned by Apache-like webservers
117 preg_match("@^(?:http[s]?://)?([A-Z0-9\-\.]+).*@i", $CFG->wwwroot, $matches);
118 $my_hostname = $matches[1];
119 $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH!
120 if ($my_ip == $my_hostname) {
121 $mnethost->ip_address = 'UNKNOWN';
122 } else {
123 $mnethost->ip_address = $my_ip;
124 }
125 } else {
126 $mnethost->ip_address = $_SERVER['SERVER_ADDR'];
127 }
128
129 $mnetid = $DB->insert_record('mnet_host', $mnethost);
130 set_config('mnet_localhost_id', $mnetid);
131
c20ce874 132 // Initial insert of mnet applications info
365a5941 133 $mnet_app = new stdClass();
c20ce874 134 $mnet_app->name = 'moodle';
135 $mnet_app->display_name = 'Moodle';
136 $mnet_app->xmlrpc_server_url = '/mnet/xmlrpc/server.php';
137 $mnet_app->sso_land_url = '/auth/mnet/land.php';
0eadebb1 138 $mnet_app->sso_jump_url = '/auth/mnet/jump.php';
6f4f82f2 139 $moodleapplicationid = $DB->insert_record('mnet_application', $mnet_app);
c20ce874 140
365a5941 141 $mnet_app = new stdClass();
c20ce874 142 $mnet_app->name = 'mahara';
143 $mnet_app->display_name = 'Mahara';
144 $mnet_app->xmlrpc_server_url = '/api/xmlrpc/server.php';
145 $mnet_app->sso_land_url = '/auth/xmlrpc/land.php';
146 $mnet_app->sso_jump_url = '/auth/xmlrpc/jump.php';
147 $DB->insert_record('mnet_application', $mnet_app);
148
6f4f82f2 149 // Set up the probably-to-be-removed-soon 'All hosts' record
365a5941 150 $mnetallhosts = new stdClass();
6f4f82f2
PS
151 $mnetallhosts->wwwroot = '';
152 $mnetallhosts->ip_address = '';
153 $mnetallhosts->public_key = '';
154 $mnetallhosts->public_key_expires = 0;
155 $mnetallhosts->last_connect_time = 0;
156 $mnetallhosts->last_log_id = 0;
157 $mnetallhosts->deleted = 0;
158 $mnetallhosts->name = 'All Hosts';
159 $mnetallhosts->applicationid = $moodleapplicationid;
160 $mnetallhosts->id = $DB->insert_record('mnet_host', $mnetallhosts, true);
161 set_config('mnet_all_hosts_id', $mnetallhosts->id);
88582df4 162
e922fe23
PS
163 /// Create guest record - do not assign any role, guest user gets the default guest role automatically on the fly
164 if ($DB->record_exists('user', array())) {
165 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default users, users already exist.');
166 }
365a5941 167 $guest = new stdClass();
88582df4 168 $guest->auth = 'manual';
169 $guest->username = 'guest';
170 $guest->password = hash_internal_user_password('guest');
171 $guest->firstname = get_string('guestuser');
172 $guest->lastname = ' ';
173 $guest->email = 'root@localhost';
174 $guest->description = get_string('guestuserinfo');
175 $guest->mnethostid = $CFG->mnet_localhost_id;
176 $guest->confirmed = 1;
177 $guest->lang = $CFG->lang;
178 $guest->timemodified= time();
179 $guest->id = $DB->insert_record('user', $guest);
19e7a192 180 if ($guest->id != 1) {
3912cdd1 181 echo $OUTPUT->notification('Unexpected id generated for the Guest account. Your database configuration or clustering setup may not be fully supported', 'notifyproblem');
19e7a192
PS
182 }
183 // Store guest id
184 set_config('siteguest', $guest->id);
e922fe23
PS
185 // Make sure user context exists
186 context_user::instance($guest->id);
88582df4 187
188
4f0c2d00 189 /// Now create admin user
365a5941 190 $admin = new stdClass();
88582df4 191 $admin->auth = 'manual';
192 $admin->firstname = get_string('admin');
193 $admin->lastname = get_string('user');
194 $admin->username = 'admin';
195 $admin->password = 'adminsetuppending';
1deff123 196 $admin->email = '';
88582df4 197 $admin->confirmed = 1;
198 $admin->mnethostid = $CFG->mnet_localhost_id;
199 $admin->lang = $CFG->lang;
200 $admin->maildisplay = 1;
201 $admin->timemodified = time();
aa282b10 202 $admin->lastip = CLI_SCRIPT ? '0.0.0.0' : getremoteaddr(); // installation hijacking prevention
88582df4 203 $admin->id = $DB->insert_record('user', $admin);
3912cdd1 204
19e7a192 205 if ($admin->id != 2) {
3912cdd1
SH
206 echo $OUTPUT->notification('Unexpected id generated for the Admin account. Your database configuration or clustering setup may not be fully supported', 'notifyproblem');
207 }
208 if ($admin->id != ($guest->id + 1)) {
209 echo $OUTPUT->notification('Nonconsecutive id generated for the Admin account. Your database configuration or clustering setup may not be fully supported.', 'notifyproblem');
19e7a192 210 }
3912cdd1
SH
211
212 /// Store list of admins
4f0c2d00 213 set_config('siteadmins', $admin->id);
e922fe23
PS
214 // Make sure user context exists
215 context_user::instance($admin->id);
88582df4 216
1caea91e 217
4f0c2d00
PS
218 /// Install the roles system.
219 $managerrole = create_role(get_string('manager', 'role'), 'manager', get_string('managerdescription', 'role'), 'manager');
220 $coursecreatorrole = create_role(get_string('coursecreators'), 'coursecreator', get_string('coursecreatorsdescription'), 'coursecreator');
221 $editteacherrole = create_role(get_string('defaultcourseteacher'), 'editingteacher', get_string('defaultcourseteacherdescription'), 'editingteacher');
222 $noneditteacherrole = create_role(get_string('noneditingteacher'), 'teacher', get_string('noneditingteacherdescription'), 'teacher');
223 $studentrole = create_role(get_string('defaultcoursestudent'), 'student', get_string('defaultcoursestudentdescription'), 'student');
224 $guestrole = create_role(get_string('guest'), 'guest', get_string('guestdescription'), 'guest');
225 $userrole = create_role(get_string('authenticateduser'), 'user', get_string('authenticateduserdescription'), 'user');
226 $frontpagerole = create_role(get_string('frontpageuser', 'role'), 'frontpage', get_string('frontpageuserdescription', 'role'), 'frontpage');
88582df4 227
228 /// Now is the correct moment to install capabilities - after creation of legacy roles, but before assigning of roles
88582df4 229 update_capabilities('moodle');
230
4f0c2d00
PS
231 /// Default allow assign
232 $defaultallowassigns = array(
233 array($managerrole, $managerrole),
234 array($managerrole, $coursecreatorrole),
235 array($managerrole, $editteacherrole),
236 array($managerrole, $noneditteacherrole),
237 array($managerrole, $studentrole),
88582df4 238
4f0c2d00
PS
239 array($editteacherrole, $noneditteacherrole),
240 array($editteacherrole, $studentrole),
c468795c 241 );
4f0c2d00
PS
242 foreach ($defaultallowassigns as $allow) {
243 list($fromroleid, $toroleid) = $allow;
c468795c 244 allow_assign($fromroleid, $toroleid);
4f0c2d00
PS
245 }
246
247 /// Default allow override
248 $defaultallowoverrides = array(
249 array($managerrole, $managerrole),
250 array($managerrole, $coursecreatorrole),
251 array($managerrole, $editteacherrole),
252 array($managerrole, $noneditteacherrole),
253 array($managerrole, $studentrole),
254 array($managerrole, $guestrole),
255 array($managerrole, $userrole),
256 array($managerrole, $frontpagerole),
257
258 array($editteacherrole, $noneditteacherrole),
259 array($editteacherrole, $studentrole),
260 array($editteacherrole, $guestrole),
261 );
262 foreach ($defaultallowoverrides as $allow) {
263 list($fromroleid, $toroleid) = $allow;
c468795c 264 allow_override($fromroleid, $toroleid); // There is a rant about this in MDL-15841.
4f0c2d00
PS
265 }
266
267 /// Default allow switch.
268 $defaultallowswitch = array(
269 array($managerrole, $editteacherrole),
270 array($managerrole, $noneditteacherrole),
271 array($managerrole, $studentrole),
272 array($managerrole, $guestrole),
273
274 array($editteacherrole, $noneditteacherrole),
275 array($editteacherrole, $studentrole),
276 array($editteacherrole, $guestrole),
277
278 array($noneditteacherrole, $studentrole),
279 array($noneditteacherrole, $guestrole),
280 );
281 foreach ($defaultallowswitch as $allow) {
282 list($fromroleid, $toroleid) = $allow;
c468795c 283 allow_switch($fromroleid, $toroleid);
284 }
88582df4 285
286 /// Set up the context levels where you can assign each role.
4f0c2d00 287 set_role_contextlevels($managerrole, get_default_contextlevels('manager'));
88582df4 288 set_role_contextlevels($coursecreatorrole, get_default_contextlevels('coursecreator'));
289 set_role_contextlevels($editteacherrole, get_default_contextlevels('editingteacher'));
290 set_role_contextlevels($noneditteacherrole, get_default_contextlevels('teacher'));
291 set_role_contextlevels($studentrole, get_default_contextlevels('student'));
292 set_role_contextlevels($guestrole, get_default_contextlevels('guest'));
293 set_role_contextlevels($userrole, get_default_contextlevels('user'));
ac173d3e 294
4f0c2d00 295 // Init themes
78946b9b 296 set_config('themerev', 1);
b64b465c
PS
297
298 // Install licenses
299 require_once($CFG->libdir . '/licenselib.php');
300 license_manager::install_licenses();
03d9401e 301
e922fe23
PS
302 // Init profile pages defaults
303 if ($DB->record_exists('my_pages', array())) {
304 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default profile pages, records already exist.');
305 }
365a5941 306 $mypage = new stdClass();
03d9401e
MD
307 $mypage->userid = NULL;
308 $mypage->name = '__default';
309 $mypage->private = 0;
310 $mypage->sortorder = 0;
e922fe23 311 $DB->insert_record('my_pages', $mypage);
03d9401e 312 $mypage->private = 1;
e922fe23 313 $DB->insert_record('my_pages', $mypage);
117bd748 314}