Minor fixes for XHTML compliance
[pfb-moodle.git] / lib / db / upgrade.php
blobd8b938cae97fc1442763aa55e75e36c7ccfc78f3
1 <?PHP //$Id$
3 // This file keeps track of upgrades to Moodle.
4 //
5 // Sometimes, changes between versions involve
6 // alterations to database structures and other
7 // major things that may break installations.
8 //
9 // The upgrade function in this file will attempt
10 // to perform all the necessary actions to upgrade
11 // your older installtion to the current version.
13 // If there's something it cannot do itself, it
14 // will tell you what you need to do.
16 // The commands in here will all be database-neutral,
17 // using the functions defined in lib/ddllib.php
20 function xmldb_main_upgrade($oldversion=0) {
22 global $CFG, $THEME, $USER, $db;
24 $result = true;
26 if ($oldversion < 2006100401) {
27 /// Only for those tracking Moodle 1.7 dev, others will have these dropped in moodle_install_roles()
28 if (!empty($CFG->rolesactive)) {
29 drop_table(new XMLDBTable('user_students'));
30 drop_table(new XMLDBTable('user_teachers'));
31 drop_table(new XMLDBTable('user_coursecreators'));
32 drop_table(new XMLDBTable('user_admins'));
36 if ($oldversion < 2006100601) { /// Disable the exercise module because it's unmaintained
37 if ($module = get_record('modules', 'name', 'exercise')) {
38 if ($module->visible) {
39 // Hide/disable the module entry
40 set_field('modules', 'visible', '0', 'id', $module->id);
41 // Save existing visible state for all activities
42 set_field('course_modules', 'visibleold', '1', 'visible' ,'1', 'module', $module->id);
43 set_field('course_modules', 'visibleold', '0', 'visible' ,'0', 'module', $module->id);
44 // Hide all activities
45 set_field('course_modules', 'visible', '0', 'module', $module->id);
47 require_once($CFG->dirroot.'/course/lib.php');
48 rebuild_course_cache(); // Rebuld cache for all modules because they might have changed
53 if ($oldversion < 2006101001) { /// Disable the LAMS module by default (if it is installed)
54 if (count_records('modules', 'name', 'lams') && !count_records('lams')) {
55 set_field('modules', 'visible', 0, 'name', 'lams'); // Disable it by default
59 if ($result && $oldversion < 2006102600) {
61 /// Define fields to be added to user_info_field
62 $table = new XMLDBTable('user_info_field');
63 $field = new XMLDBField('description');
64 $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'categoryid');
65 $field1 = new XMLDBField('param1');
66 $field1->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'defaultdata');
67 $field2 = new XMLDBField('param2');
68 $field2->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param1');
69 $field3 = new XMLDBField('param3');
70 $field3->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param2');
71 $field4 = new XMLDBField('param4');
72 $field4->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param3');
73 $field5 = new XMLDBField('param5');
74 $field5->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param4');
76 /// Launch add fields
77 $result = $result && add_field($table, $field);
78 $result = $result && add_field($table, $field1);
79 $result = $result && add_field($table, $field2);
80 $result = $result && add_field($table, $field3);
81 $result = $result && add_field($table, $field4);
82 $result = $result && add_field($table, $field5);
85 if ($result && $oldversion < 2006112000) {
87 /// Define field attachment to be added to post
88 $table = new XMLDBTable('post');
89 $field = new XMLDBField('attachment');
90 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null, 'format');
92 /// Launch add field attachment
93 $result = $result && add_field($table, $field);
96 if ($result && $oldversion < 2006112200) {
98 /// Define field imagealt to be added to user
99 $table = new XMLDBTable('user');
100 $field = new XMLDBField('imagealt');
101 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'trustbitmask');
103 /// Launch add field imagealt
104 $result = $result && add_field($table, $field);
106 $table = new XMLDBTable('user');
107 $field = new XMLDBField('screenreader');
108 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, null, '0', 'imagealt');
110 /// Launch add field screenreader
111 $result = $result && add_field($table, $field);
114 if ($oldversion < 2006120300) { /// Delete guest course section settings
115 // following code can be executed repeatedly, such as when upgrading from 1.7.x - it is ok
116 if ($guest = get_record('user', 'username', 'guest')) {
117 execute_sql("DELETE FROM {$CFG->prefix}course_display where userid=$guest->id", true);
121 if ($oldversion < 2006120400) { /// Remove secureforms config setting
122 execute_sql("DELETE FROM {$CFG->prefix}config where name='secureforms'", true);
125 if ($oldversion < 2006120700) { // add moodle/user:viewdetails to all roles!
126 if ($roles = get_records('role')) {
127 $context = get_context_instance(CONTEXT_SYSTEM);
128 foreach ($roles as $roleid=>$role) {
129 assign_capability('moodle/user:viewdetails', CAP_ALLOW, $roleid, $context->id);
134 // Move the auth plugin settings into the config_plugin table
135 if ($oldversion < 2007010300) {
136 if ($CFG->auth == 'email') {
137 set_config('registerauth', 'email');
138 } else {
139 set_config('registerauth', '');
141 $authplugins = get_list_of_plugins('auth');
142 foreach ($CFG as $k => $v) {
143 if (strpos($k, 'auth_') !== 0) {
144 continue;
146 $authsetting = substr($k, 5);
147 foreach ($authplugins as $auth) {
148 if (strpos($authsetting, $auth) !== 0) {
149 continue;
151 $setting = substr($authsetting, strlen($auth));
152 if (set_config($setting, $v, "auth/$auth")) {
153 delete_records('config', 'name', $k);
154 unset($CFG->{$k});
156 break; // don't check the rest of the auth plugin names
161 if ($oldversion < 2007010301) {
163 // Core MNET tables
165 $table = new XMLDBTable('mnet_host');
166 $table->comment = 'Information about the local and remote hosts for RPC';
167 // fields
168 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
169 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
170 $f->comment = 'Unique Host ID';
171 $f = $table->addFieldInfo('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
172 XMLDB_NOTNULL, null, null, null, 0);
173 $f = $table->addFieldInfo('wwwroot', XMLDB_TYPE_CHAR, '255', null,
174 XMLDB_NOTNULL, null, null, null, null);
175 $f = $table->addFieldInfo('ip_address', XMLDB_TYPE_CHAR, '39', null,
176 XMLDB_NOTNULL, null, null, null, null);
177 $f = $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '80', null,
178 XMLDB_NOTNULL, null, null, null, null);
179 $f = $table->addFieldInfo('public_key', XMLDB_TYPE_TEXT, 'medium', null,
180 XMLDB_NOTNULL, null, null, null, null);
181 $f = $table->addFieldInfo('public_key_expires', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
182 XMLDB_NOTNULL, null, null, null, 0);
183 $f = $table->addFieldInfo('transport', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED,
184 XMLDB_NOTNULL, null, null, null, 0);
185 $f = $table->addFieldInfo('portno', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED,
186 XMLDB_NOTNULL, null, null, null, 0);
187 $f = $table->addFieldInfo('last_connect_time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
188 XMLDB_NOTNULL, null, null, null, 0);
189 $f = $table->addFieldInfo('last_log_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
190 XMLDB_NOTNULL, null, null, null, 0);
191 // PK and indexes
192 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
193 // Create the table
194 $result = $result && create_table($table);
196 $table = new XMLDBTable('mnet_host2service');
197 $table->comment = 'Information about the services for a given host';
198 // fields
199 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
200 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
201 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
202 XMLDB_NOTNULL, NULL, null, null, 0);
203 $f = $table->addFieldInfo('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
204 XMLDB_NOTNULL, NULL, null, null, 0);
205 $f = $table->addFieldInfo('publish', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
206 XMLDB_NOTNULL, NULL, null, null, 0);
207 $f = $table->addFieldInfo('subscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
208 XMLDB_NOTNULL, NULL, null, null, 0);
209 // PK and indexes
210 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
211 $table->addIndexInfo('hostid_serviceid', XMLDB_INDEX_UNIQUE, array('hostid', 'serviceid'));
212 // Create the table
213 $result = $result && create_table($table);
215 $table = new XMLDBTable('mnet_log');
216 $table->comment = 'Store session data from users migrating to other sites';
217 // fields
218 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
219 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
220 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
221 XMLDB_NOTNULL, NULL, null, null, 0);
222 $f = $table->addFieldInfo('remoteid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
223 XMLDB_NOTNULL, NULL, null, null, 0);
224 $f = $table->addFieldInfo('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
225 XMLDB_NOTNULL, NULL, null, null, 0);
226 $f = $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
227 XMLDB_NOTNULL, NULL, null, null, 0);
228 $f = $table->addFieldInfo('ip', XMLDB_TYPE_CHAR, '15', null,
229 XMLDB_NOTNULL, NULL, null, null, null);
230 $f = $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
231 XMLDB_NOTNULL, NULL, null, null, 0);
232 $f = $table->addFieldInfo('coursename', XMLDB_TYPE_CHAR, '40', null,
233 XMLDB_NOTNULL, NULL, null, null, null);
234 $f = $table->addFieldInfo('module', XMLDB_TYPE_CHAR, '20', null,
235 XMLDB_NOTNULL, NULL, null, null, null);
236 $f = $table->addFieldInfo('cmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
237 XMLDB_NOTNULL, NULL, null, null, 0);
238 $f = $table->addFieldInfo('action', XMLDB_TYPE_CHAR, '40', null,
239 XMLDB_NOTNULL, NULL, null, null, null);
240 $f = $table->addFieldInfo('url', XMLDB_TYPE_CHAR, '100', null,
241 XMLDB_NOTNULL, NULL, null, null, null);
242 $f = $table->addFieldInfo('info', XMLDB_TYPE_CHAR, '255', null,
243 XMLDB_NOTNULL, NULL, null, null, null);
244 // PK and indexes
245 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
246 $table->addIndexInfo('host_user_course', XMLDB_INDEX_NOTUNIQUE, array('hostid', 'userid', 'course'));
247 // Create the table
248 $result = $result && create_table($table);
251 $table = new XMLDBTable('mnet_rpc');
252 $table->comment = 'Functions or methods that we may publish or subscribe to';
253 // fields
254 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
255 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
256 $f = $table->addFieldInfo('function_name', XMLDB_TYPE_CHAR, '40', null,
257 XMLDB_NOTNULL, NULL, null, null, null);
258 $f = $table->addFieldInfo('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null,
259 XMLDB_NOTNULL, NULL, null, null, null);
260 $f = $table->addFieldInfo('parent_type', XMLDB_TYPE_CHAR, '6', null,
261 XMLDB_NOTNULL, NULL, null, null, null);
262 $f = $table->addFieldInfo('parent', XMLDB_TYPE_CHAR, '20', null,
263 XMLDB_NOTNULL, NULL, null, null, null);
264 $f = $table->addFieldInfo('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
265 XMLDB_NOTNULL, NULL, null, null, 0);
266 $f = $table->addFieldInfo('help', XMLDB_TYPE_TEXT, 'medium', null,
267 XMLDB_NOTNULL, NULL, null, null, null);
268 $f = $table->addFieldInfo('profile', XMLDB_TYPE_TEXT, 'medium', null,
269 XMLDB_NOTNULL, NULL, null, null, null);
270 // PK and indexes
271 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
272 $table->addIndexInfo('enabled_xpath', XMLDB_INDEX_NOTUNIQUE, array('enabled', 'xmlrpc_path'));
273 // Create the table
274 $result = $result && create_table($table);
276 $table = new XMLDBTable('mnet_service');
277 $table->comment = 'A service is a group of functions';
278 // fields
279 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
280 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
281 $f = $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '40', null,
282 XMLDB_NOTNULL, NULL, null, null, null);
283 $f = $table->addFieldInfo('description', XMLDB_TYPE_CHAR, '40', null,
284 XMLDB_NOTNULL, NULL, null, null, null);
285 $f = $table->addFieldInfo('apiversion', XMLDB_TYPE_CHAR, '10', null,
286 XMLDB_NOTNULL, NULL, null, null, null);
287 $f = $table->addFieldInfo('offer', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
288 XMLDB_NOTNULL, NULL, null, null, 0);
289 // PK and indexes
290 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
291 // Create the table
292 $result = $result && create_table($table);
294 $table = new XMLDBTable('mnet_service2rpc');
295 $table->comment = 'Group functions or methods under a service';
296 // fields
297 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
298 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
299 $f = $table->addFieldInfo('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
300 XMLDB_NOTNULL, NULL, null, null, 0);
301 $f = $table->addFieldInfo('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
302 XMLDB_NOTNULL, NULL, null, null, 0);
303 // PK and indexes
304 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
305 $table->addIndexInfo('unique', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
306 // Create the table
307 $result = $result && create_table($table);
310 // Prime MNET configuration entries -- will be needed later by auth/mnet
312 include_once $CFG->dirroot . '/mnet/lib.php';
313 $env = new mnet_environment();
314 $env->init();
315 unset($env);
317 // add mnethostid to user-
318 $table = new XMLDBTable('user');
319 $field = new XMLDBField('mnethostid');
320 $field->setType(XMLDB_TYPE_INTEGER);
321 $field->setLength(10);
322 $field->setNotNull(true);
323 $field->setSequence(null);
324 $field->setEnum(null);
325 $field->setDefault('0');
326 $field->setPrevious("deleted");
327 $field->setNext("username");
328 $result = $result && add_field($table, $field);
330 // The default mnethostid is zero... we need to update this for all
331 // users of the local IdP service.
332 set_field('user',
333 'mnethostid', $CFG->mnet_localhost_id,
334 'mnethostid', '0');
337 $index = new XMLDBIndex('username');
338 $index->setUnique(true);
339 $index->setFields(array('username'));
340 drop_index($table, $index);
341 $index->setFields(array('mnethostid', 'username'));
342 if (!add_index($table, $index)) {
343 notify(get_string('duplicate_usernames', 'mnet', 'http://docs.moodle.org/en/DuplicateUsernames'));
346 unset($table, $field, $index);
349 ** auth/mnet tables
351 $table = new XMLDBTable('mnet_session');
352 $table->comment='Store session data from users migrating to other sites';
353 // fields
354 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
355 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
356 $f = $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
357 XMLDB_NOTNULL, NULL, null, null, 0);
358 $f = $table->addFieldInfo('username', XMLDB_TYPE_CHAR, '100', null,
359 XMLDB_NOTNULL, NULL, null, null, null);
360 $f = $table->addFieldInfo('token', XMLDB_TYPE_CHAR, '40', null,
361 XMLDB_NOTNULL, NULL, null, null, null);
362 $f = $table->addFieldInfo('mnethostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
363 XMLDB_NOTNULL, NULL, null, null, 0);
364 $f = $table->addFieldInfo('useragent', XMLDB_TYPE_CHAR, '40', null,
365 XMLDB_NOTNULL, NULL, null, null, null);
366 $f = $table->addFieldInfo('confirm_timeout', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
367 XMLDB_NOTNULL, NULL, null, null, 0);
368 $f = $table->addFieldInfo('session_id', XMLDB_TYPE_CHAR, '40', null,
369 XMLDB_NOTNULL, NULL, null, null, null);
370 $f = $table->addFieldInfo('expires', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
371 XMLDB_NOTNULL, NULL, null, null, 0);
372 // PK and indexes
373 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
374 $table->addIndexInfo('token', XMLDB_INDEX_UNIQUE, array('token'));
375 // Create the table
376 $result = $result && create_table($table);
379 $table = new XMLDBTable('mnet_sso_access_control');
380 $table->comment = 'Users by host permitted (or not) to login from a remote provider';
381 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
382 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
383 $f = $table->addFieldInfo('username', XMLDB_TYPE_CHAR, '100', null,
384 XMLDB_NOTNULL, NULL, null, null, null);
385 $f = $table->addFieldInfo('mnet_host_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
386 XMLDB_NOTNULL, NULL, null, null, 0);
387 $f = $table->addFieldInfo('access', XMLDB_TYPE_CHAR, '20', null,
388 XMLDB_NOTNULL, NULL, null, null, 'allow');
389 // PK and indexes
390 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
391 $table->addIndexInfo('mnethostid_username', XMLDB_INDEX_UNIQUE, array('mnet_host_id', 'username'));
392 // Create the table
393 $result = $result && create_table($table);
395 if (empty($USER->mnet_host_id)) {
396 $USER->mnet_host_id = $CFG->mnet_localhost_id; // Something for the current user to prevent warnings
400 ** enrol/mnet tables
402 $table = new XMLDBTable('mnet_enrol_course');
403 $table->comment = 'Information about courses on remote hosts';
404 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
405 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
406 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
407 XMLDB_NOTNULL, NULL, null, null, 0);
408 $f = $table->addFieldInfo('remoteid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
409 XMLDB_NOTNULL, NULL, null, null, 0);
410 $f = $table->addFieldInfo('cat_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
411 XMLDB_NOTNULL, NULL, null, null, 0);
412 $f = $table->addFieldInfo('cat_name', XMLDB_TYPE_CHAR, '255', null,
413 XMLDB_NOTNULL, NULL, null, null, null);
414 $f = $table->addFieldInfo('cat_description', XMLDB_TYPE_TEXT, 'medium', null,
415 XMLDB_NOTNULL, NULL, null, null, null);
416 $f = $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
417 XMLDB_NOTNULL, NULL, null, null, 0);
418 $f = $table->addFieldInfo('fullname', XMLDB_TYPE_CHAR, '254', null,
419 XMLDB_NOTNULL, NULL, null, null, null);
420 $f = $table->addFieldInfo('shortname', XMLDB_TYPE_CHAR, '15', null,
421 XMLDB_NOTNULL, NULL, null, null, null);
422 $f = $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '100', null,
423 XMLDB_NOTNULL, NULL, null, null, null);
424 $f = $table->addFieldInfo('summary', XMLDB_TYPE_TEXT, 'medium', null,
425 XMLDB_NOTNULL, NULL, null, null, null);
426 $f = $table->addFieldInfo('startdate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
427 XMLDB_NOTNULL, NULL, null, null, 0);
428 $f = $table->addFieldInfo('cost', XMLDB_TYPE_CHAR, '10', null,
429 XMLDB_NOTNULL, NULL, null, null, null);
430 $f = $table->addFieldInfo('currency', XMLDB_TYPE_CHAR, '3', null,
431 XMLDB_NOTNULL, NULL, null, null, null);
432 $f = $table->addFieldInfo('defaultroleid', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
433 XMLDB_NOTNULL, NULL, null, null, 0);
434 $f = $table->addFieldInfo('defaultrolename', XMLDB_TYPE_CHAR, '255', null,
435 XMLDB_NOTNULL, NULL, null, null, null);
436 // PK and indexes
437 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
438 $table->addIndexInfo('hostid_remoteid', XMLDB_INDEX_UNIQUE, array('hostid', 'remoteid'));
439 // Create the table
440 $result = $result && create_table($table);
443 $table = new XMLDBTable('mnet_enrol_assignments');
445 $table->comment = 'Information about enrolments on courses on remote hosts';
446 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
447 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
448 $f = $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
449 XMLDB_NOTNULL, NULL, null, null, 0);
450 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
451 XMLDB_NOTNULL, NULL, null, null, 0);
452 $f = $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
453 XMLDB_NOTNULL, NULL, null, null, 0);
454 $f = $table->addFieldInfo('rolename', XMLDB_TYPE_CHAR, '255', null,
455 XMLDB_NOTNULL, NULL, null, null, null);
456 $f = $table->addFieldInfo('enroltime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
457 XMLDB_NOTNULL, NULL, null, null, 0);
458 $f = $table->addFieldInfo('enroltype', XMLDB_TYPE_CHAR, '20', null,
459 XMLDB_NOTNULL, NULL, null, null, null);
461 // PK and indexes
462 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
463 $table->addIndexInfo('hostid_courseid', XMLDB_INDEX_NOTUNIQUE, array('hostid', 'courseid'));
464 $table->addIndexInfo('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
465 // Create the table
466 $result = $result && create_table($table);
470 if ($result && $oldversion < 2007010404) {
472 /// Define field shortname to be added to user_info_field
473 $table = new XMLDBTable('user_info_field');
474 $field = new XMLDBField('shortname');
475 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, 'shortname', 'id');
477 /// Launch add field shortname
478 $result = $result && add_field($table, $field);
480 /// Changing type of field name on table user_info_field to text
481 $table = new XMLDBTable('user_info_field');
482 $field = new XMLDBField('name');
483 $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null, 'shortname');
485 /// Launch change of type for field name
486 $result = $result && change_field_type($table, $field);
488 /// For existing fields use 'name' as the 'shortname' entry
489 if ($fields = get_records_select('user_info_field', '', '', 'id, name')) {
490 foreach ($fields as $field) {
491 $field->shortname = clean_param($field->name, PARAM_ALPHANUM);
492 $result && update_record('user_info_field', $field);
497 if ($result && $oldversion < 2007011200) {
499 /// Define table context_rel to be created
500 $table = new XMLDBTable('context_rel');
502 /// Adding fields to table context_rel
503 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
504 $table->addFieldInfo('c1', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
505 $table->addFieldInfo('c2', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
507 /// Adding keys to table context_rel
508 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
509 $table->addKeyInfo('c1', XMLDB_KEY_FOREIGN, array('c1'), 'context', array('id'));
510 $table->addKeyInfo('c2', XMLDB_KEY_FOREIGN, array('c2'), 'context', array('id'));
511 $table->addKeyInfo('c1c2', XMLDB_KEY_UNIQUE, array('c1', 'c2'));
513 /// Launch create table for context_rel
514 $result = $result && create_table($table);
516 /// code here to fill the context_rel table
517 /// use get record set to iterate slower
518 build_context_rel();
521 if ($result && $oldversion < 2007011501) {
522 if (!empty($CFG->enablerecordcache) && empty($CFG->rcache) &&
523 // Note: won't force-load these settings into CFG
524 // we don't need or want cache during the upgrade itself
525 empty($CFG->cachetype) && empty($CFG->intcachemax)) {
526 set_config('cachetype', 'internal');
527 set_config('rcache', true);
528 set_config('intcachemax', $CFG->enablerecordcache);
529 unset_config('enablerecordcache');
530 unset($CFG->enablerecordcache);
534 if ($result && $oldversion < 2007012100) {
535 /// Some old PG servers have user->firstname & user->lastname with 30cc. They must be 100cc.
536 /// Fixing that conditionally. MDL-7110
537 if ($CFG->dbfamily == 'postgres') {
538 /// Get Metadata from user table
539 $cols = array_change_key_case($db->MetaColumns($CFG->prefix . 'user'), CASE_LOWER);
541 /// Process user->firstname if needed
542 if ($col = $cols['firstname']) {
543 if ($col->max_length < 100) {
544 /// Changing precision of field firstname on table user to (100)
545 $table = new XMLDBTable('user');
546 $field = new XMLDBField('firstname');
547 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null, 'idnumber');
549 /// Launch change of precision for field firstname
550 $result = $result && change_field_precision($table, $field);
554 /// Process user->lastname if needed
555 if ($col = $cols['lastname']) {
556 if ($col->max_length < 100) {
557 /// Changing precision of field lastname on table user to (100)
558 $table = new XMLDBTable('user');
559 $field = new XMLDBField('lastname');
560 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null, 'firstname');
562 /// Launch change of precision for field lastname
563 $result = $result && change_field_precision($table, $field);
569 if ($result && $oldversion < 2007012101) {
571 /// Changing precision of field lang on table course to (30)
572 $table = new XMLDBTable('course');
573 $field = new XMLDBField('lang');
574 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null, 'groupmodeforce');
576 /// Launch change of precision for field course->lang
577 $result = $result && change_field_precision($table, $field);
579 /// Changing precision of field lang on table user to (30)
580 $table = new XMLDBTable('user');
581 $field = new XMLDBField('lang');
582 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'en', 'country');
584 /// Launch change of precision for field user->lang
585 $result = $result && change_field_precision($table, $field);
588 if ($result && $oldversion < 2007012400) {
590 /// Rename field access on table mnet_sso_access_control to accessctrl
591 $table = new XMLDBTable('mnet_sso_access_control');
592 $field = new XMLDBField('access');
593 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'allow', 'mnet_host_id');
595 /// Launch rename field accessctrl
596 $result = $result && rename_field($table, $field, 'accessctrl');
599 if ($result && $oldversion < 2007012500) {
600 execute_sql("DELETE FROM {$CFG->prefix}user WHERE username='changeme'", true);
603 if ($result && $oldversion < 2007020400) {
604 /// Only for MySQL and PG, declare the user->ajax field as not null. MDL-8421.
605 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
606 /// Changing nullability of field ajax on table user to not null
607 $table = new XMLDBTable('user');
608 $field = new XMLDBField('ajax');
609 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'htmleditor');
611 /// Launch change of nullability for field ajax
612 $result = $result && change_field_notnull($table, $field);
616 if (!empty($CFG->rolesactive) && $result && $oldversion < 2007021401) {
617 /// create default logged in user role if not present - upgrade rom 1.7.x
618 if (empty($CFG->defaultuserroleid) or $CFG->defaultuserroleid == $CFG->guestroleid) {
619 if (!get_records('role', 'shortname', 'user')) {
620 $userroleid = create_role(addslashes(get_string('authenticateduser')), 'user',
621 addslashes(get_string('authenticateduserdescription')), 'moodle/legacy:user');
622 if ($userroleid) {
623 reset_role_capabilities($userroleid);
624 set_config('defaultuserroleid', $userroleid);
630 return $result;