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