Merge branch 'wip-mdl-41843-m25' of git://github.com/rajeshtaneja/moodle into MOODLE_...
[moodle.git] / lib / db / install.php
blob551228b7b87eecb322156824b214a7b1a9dec8dd
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
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/>.
17 /**
18 * This file is executed right after the install.xml
20 * For more information, take a look to the documentation available:
21 * - Upgrade API: {@link http://docs.moodle.org/dev/Upgrade_API}
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
29 defined('MOODLE_INTERNAL') || die();
31 /**
32 * Main post-install tasks to be executed after the BD schema is available
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...
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).
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 * - ....
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()}.
56 function xmldb_main_install() {
57 global $CFG, $DB, $SITE, $OUTPUT;
59 // Make sure system context exists
60 $syscontext = context_system::instance(0, MUST_EXIST, false);
61 if ($syscontext->id != SYSCONTEXTID) {
62 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected new system context id!');
66 // Create site course
67 if ($DB->record_exists('course', array())) {
68 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create frontpage course, courses already exist.');
70 $newsite = new stdClass();
71 $newsite->fullname = '';
72 $newsite->shortname = '';
73 $newsite->summary = NULL;
74 $newsite->newsitems = 3;
75 $newsite->numsections = 1;
76 $newsite->category = 0;
77 $newsite->format = 'site'; // Only for this course
78 $newsite->timecreated = time();
79 $newsite->timemodified = $newsite->timecreated;
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);
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));
93 $SITE = get_site();
94 if ($newsite->id != $SITE->id) {
95 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected new site course id!');
97 // Make sure site course context exists
98 context_course::instance($SITE->id);
99 // Update the global frontpage cache
100 $SITE = $DB->get_record('course', array('id'=>$newsite->id), '*', MUST_EXIST);
103 // Create default course category
104 if ($DB->record_exists('course_categories', array())) {
105 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default course category, categories already exist.');
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);
118 $defaults = array(
119 'rolesactive' => '0', // marks fully set up system
120 'auth' => 'email',
121 'auth_pop3mailbox' => 'INBOX',
122 'enrol_plugins_enabled' => 'manual,guest,self,cohort',
123 'theme' => theme_config::DEFAULT_THEME,
124 'filter_multilang_converted' => 1,
125 'siteidentifier' => random_string(32).get_host_from_url($CFG->wwwroot),
126 'backup_version' => 2008111700,
127 'backup_release' => '2.0 dev',
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.
132 'texteditors' => 'tinymce,textarea',
134 foreach($defaults as $key => $value) {
135 set_config($key, $value);
139 // Bootstrap mnet
140 $mnethost = new stdClass();
141 $mnethost->wwwroot = $CFG->wwwroot;
142 $mnethost->name = '';
143 $mnethost->name = '';
144 $mnethost->public_key = '';
146 if (empty($_SERVER['SERVER_ADDR'])) {
147 // SERVER_ADDR is only returned by Apache-like webservers
148 preg_match("@^(?:http[s]?://)?([A-Z0-9\-\.]+).*@i", $CFG->wwwroot, $matches);
149 $my_hostname = $matches[1];
150 $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH!
151 if ($my_ip == $my_hostname) {
152 $mnethost->ip_address = 'UNKNOWN';
153 } else {
154 $mnethost->ip_address = $my_ip;
156 } else {
157 $mnethost->ip_address = $_SERVER['SERVER_ADDR'];
160 $mnetid = $DB->insert_record('mnet_host', $mnethost);
161 set_config('mnet_localhost_id', $mnetid);
163 // Initial insert of mnet applications info
164 $mnet_app = new stdClass();
165 $mnet_app->name = 'moodle';
166 $mnet_app->display_name = 'Moodle';
167 $mnet_app->xmlrpc_server_url = '/mnet/xmlrpc/server.php';
168 $mnet_app->sso_land_url = '/auth/mnet/land.php';
169 $mnet_app->sso_jump_url = '/auth/mnet/jump.php';
170 $moodleapplicationid = $DB->insert_record('mnet_application', $mnet_app);
172 $mnet_app = new stdClass();
173 $mnet_app->name = 'mahara';
174 $mnet_app->display_name = 'Mahara';
175 $mnet_app->xmlrpc_server_url = '/api/xmlrpc/server.php';
176 $mnet_app->sso_land_url = '/auth/xmlrpc/land.php';
177 $mnet_app->sso_jump_url = '/auth/xmlrpc/jump.php';
178 $DB->insert_record('mnet_application', $mnet_app);
180 // Set up the probably-to-be-removed-soon 'All hosts' record
181 $mnetallhosts = new stdClass();
182 $mnetallhosts->wwwroot = '';
183 $mnetallhosts->ip_address = '';
184 $mnetallhosts->public_key = '';
185 $mnetallhosts->public_key_expires = 0;
186 $mnetallhosts->last_connect_time = 0;
187 $mnetallhosts->last_log_id = 0;
188 $mnetallhosts->deleted = 0;
189 $mnetallhosts->name = 'All Hosts';
190 $mnetallhosts->applicationid = $moodleapplicationid;
191 $mnetallhosts->id = $DB->insert_record('mnet_host', $mnetallhosts, true);
192 set_config('mnet_all_hosts_id', $mnetallhosts->id);
194 // Create guest record - do not assign any role, guest user gets the default guest role automatically on the fly
195 if ($DB->record_exists('user', array())) {
196 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default users, users already exist.');
198 $guest = new stdClass();
199 $guest->auth = 'manual';
200 $guest->username = 'guest';
201 $guest->password = hash_internal_user_password('guest');
202 $guest->firstname = get_string('guestuser');
203 $guest->lastname = ' ';
204 $guest->email = 'root@localhost';
205 $guest->description = get_string('guestuserinfo');
206 $guest->mnethostid = $CFG->mnet_localhost_id;
207 $guest->confirmed = 1;
208 $guest->lang = $CFG->lang;
209 $guest->timemodified= time();
210 $guest->id = $DB->insert_record('user', $guest);
211 if ($guest->id != 1) {
212 echo $OUTPUT->notification('Unexpected id generated for the Guest account. Your database configuration or clustering setup may not be fully supported', 'notifyproblem');
214 // Store guest id
215 set_config('siteguest', $guest->id);
216 // Make sure user context exists
217 context_user::instance($guest->id);
220 // Now create admin user
221 $admin = new stdClass();
222 $admin->auth = 'manual';
223 $admin->firstname = get_string('admin');
224 $admin->lastname = get_string('user');
225 $admin->username = 'admin';
226 $admin->password = 'adminsetuppending';
227 $admin->email = '';
228 $admin->confirmed = 1;
229 $admin->mnethostid = $CFG->mnet_localhost_id;
230 $admin->lang = $CFG->lang;
231 $admin->maildisplay = 1;
232 $admin->timemodified = time();
233 $admin->lastip = CLI_SCRIPT ? '0.0.0.0' : getremoteaddr(); // installation hijacking prevention
234 $admin->id = $DB->insert_record('user', $admin);
236 if ($admin->id != 2) {
237 echo $OUTPUT->notification('Unexpected id generated for the Admin account. Your database configuration or clustering setup may not be fully supported', 'notifyproblem');
239 if ($admin->id != ($guest->id + 1)) {
240 echo $OUTPUT->notification('Nonconsecutive id generated for the Admin account. Your database configuration or clustering setup may not be fully supported.', 'notifyproblem');
243 // Store list of admins
244 set_config('siteadmins', $admin->id);
245 // Make sure user context exists
246 context_user::instance($admin->id);
249 // Install the roles system.
250 $managerrole = create_role('', 'manager', '', 'manager');
251 $coursecreatorrole = create_role('', 'coursecreator', '', 'coursecreator');
252 $editteacherrole = create_role('', 'editingteacher', '', 'editingteacher');
253 $noneditteacherrole = create_role('', 'teacher', '', 'teacher');
254 $studentrole = create_role('', 'student', '', 'student');
255 $guestrole = create_role('', 'guest', '', 'guest');
256 $userrole = create_role('', 'user', '', 'user');
257 $frontpagerole = create_role('', 'frontpage', '', 'frontpage');
259 // Now is the correct moment to install capabilities - after creation of legacy roles, but before assigning of roles
260 update_capabilities('moodle');
262 // Default allow assign
263 $defaultallowassigns = array(
264 array($managerrole, $managerrole),
265 array($managerrole, $coursecreatorrole),
266 array($managerrole, $editteacherrole),
267 array($managerrole, $noneditteacherrole),
268 array($managerrole, $studentrole),
270 array($editteacherrole, $noneditteacherrole),
271 array($editteacherrole, $studentrole),
273 foreach ($defaultallowassigns as $allow) {
274 list($fromroleid, $toroleid) = $allow;
275 allow_assign($fromroleid, $toroleid);
278 // Default allow override
279 $defaultallowoverrides = array(
280 array($managerrole, $managerrole),
281 array($managerrole, $coursecreatorrole),
282 array($managerrole, $editteacherrole),
283 array($managerrole, $noneditteacherrole),
284 array($managerrole, $studentrole),
285 array($managerrole, $guestrole),
286 array($managerrole, $userrole),
287 array($managerrole, $frontpagerole),
289 array($editteacherrole, $noneditteacherrole),
290 array($editteacherrole, $studentrole),
291 array($editteacherrole, $guestrole),
293 foreach ($defaultallowoverrides as $allow) {
294 list($fromroleid, $toroleid) = $allow;
295 allow_override($fromroleid, $toroleid); // There is a rant about this in MDL-15841.
298 // Default allow switch.
299 $defaultallowswitch = array(
300 array($managerrole, $editteacherrole),
301 array($managerrole, $noneditteacherrole),
302 array($managerrole, $studentrole),
303 array($managerrole, $guestrole),
305 array($editteacherrole, $noneditteacherrole),
306 array($editteacherrole, $studentrole),
307 array($editteacherrole, $guestrole),
309 array($noneditteacherrole, $studentrole),
310 array($noneditteacherrole, $guestrole),
312 foreach ($defaultallowswitch as $allow) {
313 list($fromroleid, $toroleid) = $allow;
314 allow_switch($fromroleid, $toroleid);
317 // Set up the context levels where you can assign each role.
318 set_role_contextlevels($managerrole, get_default_contextlevels('manager'));
319 set_role_contextlevels($coursecreatorrole, get_default_contextlevels('coursecreator'));
320 set_role_contextlevels($editteacherrole, get_default_contextlevels('editingteacher'));
321 set_role_contextlevels($noneditteacherrole, get_default_contextlevels('teacher'));
322 set_role_contextlevels($studentrole, get_default_contextlevels('student'));
323 set_role_contextlevels($guestrole, get_default_contextlevels('guest'));
324 set_role_contextlevels($userrole, get_default_contextlevels('user'));
326 // Init theme and JS revisions
327 set_config('themerev', time());
328 set_config('jsrev', time());
330 // No admin setting for this any more, GD is now required, remove in Moodle 2.6.
331 set_config('gdversion', 2);
333 // Install licenses
334 require_once($CFG->libdir . '/licenselib.php');
335 license_manager::install_licenses();
337 // Init profile pages defaults
338 if ($DB->record_exists('my_pages', array())) {
339 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default profile pages, records already exist.');
341 $mypage = new stdClass();
342 $mypage->userid = NULL;
343 $mypage->name = '__default';
344 $mypage->private = 0;
345 $mypage->sortorder = 0;
346 $DB->insert_record('my_pages', $mypage);
347 $mypage->private = 1;
348 $DB->insert_record('my_pages', $mypage);