MDL-36017 Added default fields to format_legacy::course_format_options()
[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 }
1caea91e 89 $SITE = get_site();
e922fe23 90 if ($newsite->id != $SITE->id) {
19e7a192 91 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected new site course id!');
1caea91e 92 }
e922fe23
PS
93 // Make sure site course context exists
94 context_course::instance($SITE->id);
95 // Update the global frontpage cache
96 $SITE = $DB->get_record('course', array('id'=>$newsite->id), '*', MUST_EXIST);
1caea91e 97
98
39b90b51 99 // Create default course category
e922fe23
PS
100 if ($DB->record_exists('course_categories', array())) {
101 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default course category, categories already exist.');
102 }
103 $cat = new stdClass();
104 $cat->name = get_string('miscellaneous');
105 $cat->depth = 1;
106 $cat->sortorder = MAX_COURSES_IN_CATEGORY;
107 $cat->timemodified = time();
108 $catid = $DB->insert_record('course_categories', $cat);
109 $DB->set_field('course_categories', 'path', '/'.$catid, array('id'=>$catid));
110 // Make sure category context exists
111 context_coursecat::instance($catid);
1caea91e 112
1caea91e 113
5b8fa09b 114 $defaults = array(
115 'rolesactive' => '0', // marks fully set up system
116 'auth' => 'email',
117 'auth_pop3mailbox' => 'INBOX',
df997f84 118 'enrol_plugins_enabled' => 'manual,guest,self,cohort',
5f0baa43 119 'theme' => theme_config::DEFAULT_THEME,
5b8fa09b 120 'filter_multilang_converted' => 1,
aa282b10 121 'siteidentifier' => random_string(32).get_host_from_url($CFG->wwwroot),
5b8fa09b 122 'backup_version' => 2008111700,
123 'backup_release' => '2.0 dev',
5b8fa09b 124 'mnet_dispatcher_mode' => 'off',
125 'sessiontimeout' => 7200, // must be present during roles installation
126 'stringfilters' => '', // These two are managed in a strange way by the filters
127 'filterall' => 0, // setting page, so have to be initialised here.
a5747cf8 128 'texteditors' => 'tinymce,textarea',
5b8fa09b 129 );
1caea91e 130 foreach($defaults as $key => $value) {
131 set_config($key, $value);
132 }
133
134
39b90b51 135 // Bootstrap mnet
365a5941 136 $mnethost = new stdClass();
1caea91e 137 $mnethost->wwwroot = $CFG->wwwroot;
138 $mnethost->name = '';
139 $mnethost->name = '';
140 $mnethost->public_key = '';
141
142 if (empty($_SERVER['SERVER_ADDR'])) {
143 // SERVER_ADDR is only returned by Apache-like webservers
144 preg_match("@^(?:http[s]?://)?([A-Z0-9\-\.]+).*@i", $CFG->wwwroot, $matches);
145 $my_hostname = $matches[1];
146 $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH!
147 if ($my_ip == $my_hostname) {
148 $mnethost->ip_address = 'UNKNOWN';
149 } else {
150 $mnethost->ip_address = $my_ip;
151 }
152 } else {
153 $mnethost->ip_address = $_SERVER['SERVER_ADDR'];
154 }
155
156 $mnetid = $DB->insert_record('mnet_host', $mnethost);
157 set_config('mnet_localhost_id', $mnetid);
158
c20ce874 159 // Initial insert of mnet applications info
365a5941 160 $mnet_app = new stdClass();
c20ce874 161 $mnet_app->name = 'moodle';
162 $mnet_app->display_name = 'Moodle';
163 $mnet_app->xmlrpc_server_url = '/mnet/xmlrpc/server.php';
164 $mnet_app->sso_land_url = '/auth/mnet/land.php';
0eadebb1 165 $mnet_app->sso_jump_url = '/auth/mnet/jump.php';
6f4f82f2 166 $moodleapplicationid = $DB->insert_record('mnet_application', $mnet_app);
c20ce874 167
365a5941 168 $mnet_app = new stdClass();
c20ce874 169 $mnet_app->name = 'mahara';
170 $mnet_app->display_name = 'Mahara';
171 $mnet_app->xmlrpc_server_url = '/api/xmlrpc/server.php';
172 $mnet_app->sso_land_url = '/auth/xmlrpc/land.php';
173 $mnet_app->sso_jump_url = '/auth/xmlrpc/jump.php';
174 $DB->insert_record('mnet_application', $mnet_app);
175
6f4f82f2 176 // Set up the probably-to-be-removed-soon 'All hosts' record
365a5941 177 $mnetallhosts = new stdClass();
6f4f82f2
PS
178 $mnetallhosts->wwwroot = '';
179 $mnetallhosts->ip_address = '';
180 $mnetallhosts->public_key = '';
181 $mnetallhosts->public_key_expires = 0;
182 $mnetallhosts->last_connect_time = 0;
183 $mnetallhosts->last_log_id = 0;
184 $mnetallhosts->deleted = 0;
185 $mnetallhosts->name = 'All Hosts';
186 $mnetallhosts->applicationid = $moodleapplicationid;
187 $mnetallhosts->id = $DB->insert_record('mnet_host', $mnetallhosts, true);
188 set_config('mnet_all_hosts_id', $mnetallhosts->id);
88582df4 189
39b90b51 190 // Create guest record - do not assign any role, guest user gets the default guest role automatically on the fly
e922fe23
PS
191 if ($DB->record_exists('user', array())) {
192 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default users, users already exist.');
193 }
365a5941 194 $guest = new stdClass();
88582df4 195 $guest->auth = 'manual';
196 $guest->username = 'guest';
197 $guest->password = hash_internal_user_password('guest');
198 $guest->firstname = get_string('guestuser');
199 $guest->lastname = ' ';
200 $guest->email = 'root@localhost';
201 $guest->description = get_string('guestuserinfo');
202 $guest->mnethostid = $CFG->mnet_localhost_id;
203 $guest->confirmed = 1;
204 $guest->lang = $CFG->lang;
205 $guest->timemodified= time();
206 $guest->id = $DB->insert_record('user', $guest);
19e7a192 207 if ($guest->id != 1) {
3912cdd1 208 echo $OUTPUT->notification('Unexpected id generated for the Guest account. Your database configuration or clustering setup may not be fully supported', 'notifyproblem');
19e7a192
PS
209 }
210 // Store guest id
211 set_config('siteguest', $guest->id);
e922fe23
PS
212 // Make sure user context exists
213 context_user::instance($guest->id);
88582df4 214
215
39b90b51 216 // Now create admin user
365a5941 217 $admin = new stdClass();
88582df4 218 $admin->auth = 'manual';
219 $admin->firstname = get_string('admin');
220 $admin->lastname = get_string('user');
221 $admin->username = 'admin';
222 $admin->password = 'adminsetuppending';
1deff123 223 $admin->email = '';
88582df4 224 $admin->confirmed = 1;
225 $admin->mnethostid = $CFG->mnet_localhost_id;
226 $admin->lang = $CFG->lang;
227 $admin->maildisplay = 1;
228 $admin->timemodified = time();
aa282b10 229 $admin->lastip = CLI_SCRIPT ? '0.0.0.0' : getremoteaddr(); // installation hijacking prevention
88582df4 230 $admin->id = $DB->insert_record('user', $admin);
3912cdd1 231
19e7a192 232 if ($admin->id != 2) {
3912cdd1
SH
233 echo $OUTPUT->notification('Unexpected id generated for the Admin account. Your database configuration or clustering setup may not be fully supported', 'notifyproblem');
234 }
235 if ($admin->id != ($guest->id + 1)) {
236 echo $OUTPUT->notification('Nonconsecutive id generated for the Admin account. Your database configuration or clustering setup may not be fully supported.', 'notifyproblem');
19e7a192 237 }
3912cdd1 238
39b90b51 239 // Store list of admins
4f0c2d00 240 set_config('siteadmins', $admin->id);
e922fe23
PS
241 // Make sure user context exists
242 context_user::instance($admin->id);
88582df4 243
1caea91e 244
39b90b51 245 // Install the roles system.
ff22f229
PS
246 $managerrole = create_role('', 'manager', '', 'manager');
247 $coursecreatorrole = create_role('', 'coursecreator', '', 'coursecreator');
248 $editteacherrole = create_role('', 'editingteacher', '', 'editingteacher');
249 $noneditteacherrole = create_role('', 'teacher', '', 'teacher');
250 $studentrole = create_role('', 'student', '', 'student');
251 $guestrole = create_role('', 'guest', '', 'guest');
252 $userrole = create_role('', 'user', '', 'user');
253 $frontpagerole = create_role('', 'frontpage', '', 'frontpage');
88582df4 254
39b90b51 255 // Now is the correct moment to install capabilities - after creation of legacy roles, but before assigning of roles
88582df4 256 update_capabilities('moodle');
257
39b90b51 258 // Default allow assign
4f0c2d00
PS
259 $defaultallowassigns = array(
260 array($managerrole, $managerrole),
261 array($managerrole, $coursecreatorrole),
262 array($managerrole, $editteacherrole),
263 array($managerrole, $noneditteacherrole),
264 array($managerrole, $studentrole),
88582df4 265
4f0c2d00
PS
266 array($editteacherrole, $noneditteacherrole),
267 array($editteacherrole, $studentrole),
c468795c 268 );
4f0c2d00
PS
269 foreach ($defaultallowassigns as $allow) {
270 list($fromroleid, $toroleid) = $allow;
c468795c 271 allow_assign($fromroleid, $toroleid);
4f0c2d00
PS
272 }
273
39b90b51 274 // Default allow override
4f0c2d00
PS
275 $defaultallowoverrides = array(
276 array($managerrole, $managerrole),
277 array($managerrole, $coursecreatorrole),
278 array($managerrole, $editteacherrole),
279 array($managerrole, $noneditteacherrole),
280 array($managerrole, $studentrole),
281 array($managerrole, $guestrole),
282 array($managerrole, $userrole),
283 array($managerrole, $frontpagerole),
284
285 array($editteacherrole, $noneditteacherrole),
286 array($editteacherrole, $studentrole),
287 array($editteacherrole, $guestrole),
288 );
289 foreach ($defaultallowoverrides as $allow) {
290 list($fromroleid, $toroleid) = $allow;
c468795c 291 allow_override($fromroleid, $toroleid); // There is a rant about this in MDL-15841.
4f0c2d00
PS
292 }
293
39b90b51 294 // Default allow switch.
4f0c2d00
PS
295 $defaultallowswitch = array(
296 array($managerrole, $editteacherrole),
297 array($managerrole, $noneditteacherrole),
298 array($managerrole, $studentrole),
299 array($managerrole, $guestrole),
300
301 array($editteacherrole, $noneditteacherrole),
302 array($editteacherrole, $studentrole),
303 array($editteacherrole, $guestrole),
304
305 array($noneditteacherrole, $studentrole),
306 array($noneditteacherrole, $guestrole),
307 );
308 foreach ($defaultallowswitch as $allow) {
309 list($fromroleid, $toroleid) = $allow;
c468795c 310 allow_switch($fromroleid, $toroleid);
311 }
88582df4 312
39b90b51 313 // Set up the context levels where you can assign each role.
4f0c2d00 314 set_role_contextlevels($managerrole, get_default_contextlevels('manager'));
88582df4 315 set_role_contextlevels($coursecreatorrole, get_default_contextlevels('coursecreator'));
316 set_role_contextlevels($editteacherrole, get_default_contextlevels('editingteacher'));
317 set_role_contextlevels($noneditteacherrole, get_default_contextlevels('teacher'));
318 set_role_contextlevels($studentrole, get_default_contextlevels('student'));
319 set_role_contextlevels($guestrole, get_default_contextlevels('guest'));
320 set_role_contextlevels($userrole, get_default_contextlevels('user'));
ac173d3e 321
1635da56
PS
322 // Init theme and JS revisions
323 set_config('themerev', time());
324 set_config('jsrev', time());
b64b465c
PS
325
326 // Install licenses
327 require_once($CFG->libdir . '/licenselib.php');
328 license_manager::install_licenses();
03d9401e 329
e922fe23
PS
330 // Init profile pages defaults
331 if ($DB->record_exists('my_pages', array())) {
332 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default profile pages, records already exist.');
333 }
365a5941 334 $mypage = new stdClass();
03d9401e
MD
335 $mypage->userid = NULL;
336 $mypage->name = '__default';
337 $mypage->private = 0;
338 $mypage->sortorder = 0;
e922fe23 339 $DB->insert_record('my_pages', $mypage);
03d9401e 340 $mypage->private = 1;
e922fe23 341 $DB->insert_record('my_pages', $mypage);
117bd748 342}