MDL-20274 upgrade and install support for db/services.php
[moodle.git] / lib / db / install.php
CommitLineData
1caea91e 1<?php //$Id$
2
3// This file is executed right after the install.xml
4//
5
c20ce874 6function xmldb_main_install() {
1caea91e 7 global $CFG, $DB, $SITE;
8
1caea91e 9/// make sure system context exists
b4154c2d 10 $syscontext = get_system_context(false);
1caea91e 11 if ($syscontext->id != 1) {
19a4a32e 12 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected system context id created!');
1caea91e 13 }
14
15
16// create site course
17 $newsite = new object();
18 $newsite->fullname = "";
19 $newsite->shortname = "";
20 $newsite->summary = NULL;
21 $newsite->newsitems = 3;
22 $newsite->numsections = 0;
23 $newsite->category = 0;
24 $newsite->format = 'site'; // Only for this course
25 $newsite->teacher = get_string("defaultcourseteacher");
26 $newsite->teachers = get_string("defaultcourseteachers");
27 $newsite->student = get_string("defaultcoursestudent");
28 $newsite->students = get_string("defaultcoursestudents");
29 $newsite->timemodified = time();
30
31 $DB->insert_record('course', $newsite);
32 $SITE = get_site();
33 if ($SITE->id != 1) {
19a4a32e 34 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected site course id created!');
1caea91e 35 }
36
37
38/// make sure site course context exists
39 get_context_instance(CONTEXT_COURSE, $SITE->id);
40
41/// create default course category
42 $cat = get_course_category();
43
5b8fa09b 44 $defaults = array(
45 'rolesactive' => '0', // marks fully set up system
46 'auth' => 'email',
47 'auth_pop3mailbox' => 'INBOX',
48 'enrol' => 'manual',
49 'enrol_plugins_enabled' => 'manual',
50 'style' => 'default',
51 'template' => 'default',
52 'theme' => 'standardwhite',
53 'filter_multilang_converted' => 1,
aa282b10 54 'siteidentifier' => random_string(32).get_host_from_url($CFG->wwwroot),
5b8fa09b 55 'backup_version' => 2008111700,
56 'backup_release' => '2.0 dev',
57 'blocks_version' => 2007081300, // might be removed soon
58 'mnet_dispatcher_mode' => 'off',
59 'sessiontimeout' => 7200, // must be present during roles installation
60 'stringfilters' => '', // These two are managed in a strange way by the filters
61 'filterall' => 0, // setting page, so have to be initialised here.
a5747cf8 62 'texteditors' => 'tinymce,textarea',
5b8fa09b 63 );
1caea91e 64 foreach($defaults as $key => $value) {
65 set_config($key, $value);
66 }
67
68
69/// bootstrap mnet
70 $mnethost = new object();
71 $mnethost->wwwroot = $CFG->wwwroot;
72 $mnethost->name = '';
73 $mnethost->name = '';
74 $mnethost->public_key = '';
75
76 if (empty($_SERVER['SERVER_ADDR'])) {
77 // SERVER_ADDR is only returned by Apache-like webservers
78 preg_match("@^(?:http[s]?://)?([A-Z0-9\-\.]+).*@i", $CFG->wwwroot, $matches);
79 $my_hostname = $matches[1];
80 $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH!
81 if ($my_ip == $my_hostname) {
82 $mnethost->ip_address = 'UNKNOWN';
83 } else {
84 $mnethost->ip_address = $my_ip;
85 }
86 } else {
87 $mnethost->ip_address = $_SERVER['SERVER_ADDR'];
88 }
89
90 $mnetid = $DB->insert_record('mnet_host', $mnethost);
91 set_config('mnet_localhost_id', $mnetid);
92
c20ce874 93 // Initial insert of mnet applications info
94 $mnet_app = new object();
95 $mnet_app->name = 'moodle';
96 $mnet_app->display_name = 'Moodle';
97 $mnet_app->xmlrpc_server_url = '/mnet/xmlrpc/server.php';
98 $mnet_app->sso_land_url = '/auth/mnet/land.php';
99 $mnet_app->sso_jump_url = '/auth/mnet/land.php';
100 $DB->insert_record('mnet_application', $mnet_app);
101
102 $mnet_app = new object();
103 $mnet_app->name = 'mahara';
104 $mnet_app->display_name = 'Mahara';
105 $mnet_app->xmlrpc_server_url = '/api/xmlrpc/server.php';
106 $mnet_app->sso_land_url = '/auth/xmlrpc/land.php';
107 $mnet_app->sso_jump_url = '/auth/xmlrpc/jump.php';
108 $DB->insert_record('mnet_application', $mnet_app);
109
88582df4 110
c20ce874 111/// insert log entries - replaces statements section in install.xml
0cb93a7e 112 update_log_display_entry('user', 'view', 'user', 'CONCAT(firstname,\' \',lastname)');
113 update_log_display_entry('course', 'user report', 'user', 'CONCAT(firstname,\' \',lastname)');
114 update_log_display_entry('course', 'view', 'course', 'fullname');
115 update_log_display_entry('course', 'update', 'course', 'fullname');
116 update_log_display_entry('course', 'enrol', 'course', 'fullname');
117 update_log_display_entry('course', 'unenrol', 'course', 'fullname');
118 update_log_display_entry('course', 'report log', 'course', 'fullname');
119 update_log_display_entry('course', 'report live', 'course', 'fullname');
120 update_log_display_entry('course', 'report outline', 'course', 'fullname');
121 update_log_display_entry('course', 'report participation', 'course', 'fullname');
122 update_log_display_entry('course', 'report stats', 'course', 'fullname');
123 update_log_display_entry('message', 'write', 'user', 'CONCAT(firstname,\' \',lastname)');
124 update_log_display_entry('message', 'read', 'user', 'CONCAT(firstname,\' \',lastname)');
125 update_log_display_entry('message', 'add contact', 'user', 'CONCAT(firstname,\' \',lastname)');
126 update_log_display_entry('message', 'remove contact', 'user', 'CONCAT(firstname,\' \',lastname)');
127 update_log_display_entry('message', 'block contact', 'user', 'CONCAT(firstname,\' \',lastname)');
128 update_log_display_entry('message', 'unblock contact', 'user', 'CONCAT(firstname,\' \',lastname)');
129 update_log_display_entry('group', 'view', 'groups', 'name');
b89e4ad8 130 update_log_display_entry('tag', 'update', 'tag', 'name');
c20ce874 131
1caea91e 132
133/// Create guest record
88582df4 134 $guest = new object();
135 $guest->auth = 'manual';
136 $guest->username = 'guest';
137 $guest->password = hash_internal_user_password('guest');
138 $guest->firstname = get_string('guestuser');
139 $guest->lastname = ' ';
140 $guest->email = 'root@localhost';
141 $guest->description = get_string('guestuserinfo');
142 $guest->mnethostid = $CFG->mnet_localhost_id;
143 $guest->confirmed = 1;
144 $guest->lang = $CFG->lang;
145 $guest->timemodified= time();
146 $guest->id = $DB->insert_record('user', $guest);
147
148
149/// Now create admin user
150 $admin = new object();
151 $admin->auth = 'manual';
152 $admin->firstname = get_string('admin');
153 $admin->lastname = get_string('user');
154 $admin->username = 'admin';
155 $admin->password = 'adminsetuppending';
1deff123 156 $admin->email = '';
88582df4 157 $admin->confirmed = 1;
158 $admin->mnethostid = $CFG->mnet_localhost_id;
159 $admin->lang = $CFG->lang;
160 $admin->maildisplay = 1;
161 $admin->timemodified = time();
aa282b10 162 $admin->lastip = CLI_SCRIPT ? '0.0.0.0' : getremoteaddr(); // installation hijacking prevention
88582df4 163 $admin->id = $DB->insert_record('user', $admin);
164
1caea91e 165
ac173d3e 166/// Install the roles system.
88582df4 167 $adminrole = create_role(get_string('administrator'), 'admin',
168 get_string('administratordescription'), 'moodle/legacy:admin');
169 $coursecreatorrole = create_role(get_string('coursecreators'), 'coursecreator',
170 get_string('coursecreatorsdescription'), 'moodle/legacy:coursecreator');
171 $editteacherrole = create_role(get_string('defaultcourseteacher'), 'editingteacher',
172 get_string('defaultcourseteacherdescription'), 'moodle/legacy:editingteacher');
173 $noneditteacherrole = create_role(get_string('noneditingteacher'), 'teacher',
174 get_string('noneditingteacherdescription'), 'moodle/legacy:teacher');
175 $studentrole = create_role(get_string('defaultcoursestudent'), 'student',
176 get_string('defaultcoursestudentdescription'), 'moodle/legacy:student');
177 $guestrole = create_role(get_string('guest'), 'guest',
178 get_string('guestdescription'), 'moodle/legacy:guest');
179 $userrole = create_role(get_string('authenticateduser'), 'user',
180 get_string('authenticateduserdescription'), 'moodle/legacy:user');
181
182 /// Now is the correct moment to install capabilities - after creation of legacy roles, but before assigning of roles
183 assign_capability('moodle/site:doanything', CAP_ALLOW, $adminrole, $syscontext->id);
184 update_capabilities('moodle');
c976e271 185 external_update_descriptions('moodle');
88582df4 186
187 /// assign default roles
188 role_assign($guestrole, $guest->id, 0, $syscontext->id);
189 role_assign($adminrole, $admin->id, 0, $syscontext->id);
190
c468795c 191 /// Default allow assign/override/switch.
192 $defaultallows = array(
193 $coursecreatorrole => $noneditteacherrole,
194 $coursecreatorrole => $editteacherrole,
195 $coursecreatorrole => $studentrole,
196 $coursecreatorrole => $guestrole,
197
198 $editteacherrole => $noneditteacherrole,
199 $editteacherrole => $studentrole,
200 $editteacherrole => $guestrole,
201 );
202
203 foreach ($defaultallows as $fromroleid => $toroleid) {
204 allow_assign($fromroleid, $toroleid);
205 allow_override($fromroleid, $toroleid); // There is a rant about this in MDL-15841.
206 allow_switch($fromroleid, $toroleid);
207 }
208 allow_switch($noneditteacherrole, $studentrole);
88582df4 209
210 /// Set up the context levels where you can assign each role.
211 set_role_contextlevels($adminrole, get_default_contextlevels('admin'));
212 set_role_contextlevels($coursecreatorrole, get_default_contextlevels('coursecreator'));
213 set_role_contextlevels($editteacherrole, get_default_contextlevels('editingteacher'));
214 set_role_contextlevels($noneditteacherrole, get_default_contextlevels('teacher'));
215 set_role_contextlevels($studentrole, get_default_contextlevels('student'));
216 set_role_contextlevels($guestrole, get_default_contextlevels('guest'));
217 set_role_contextlevels($userrole, get_default_contextlevels('user'));
ac173d3e 218
1caea91e 219}