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