prevent NULL contents before field change (old servers can have this wrongly defined...
[moodle-linuxchix.git] / lib / db / upgrade.php
blobfbe1268941a9d805484a2cabfd0d3edb5dc76278
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'));
35 upgrade_main_savepoint($result, 2006100401);
38 if ($oldversion < 2006100601) { /// Disable the exercise module because it's unmaintained
39 if ($module = get_record('modules', 'name', 'exercise')) {
40 if ($module->visible) {
41 // Hide/disable the module entry
42 set_field('modules', 'visible', '0', 'id', $module->id);
43 // Save existing visible state for all activities
44 set_field('course_modules', 'visibleold', '1', 'visible' ,'1', 'module', $module->id);
45 set_field('course_modules', 'visibleold', '0', 'visible' ,'0', 'module', $module->id);
46 // Hide all activities
47 set_field('course_modules', 'visible', '0', 'module', $module->id);
49 //require_once($CFG->dirroot.'/course/lib.php');
50 //rebuild_course_cache(); // Rebuld cache for all modules because they might have changed
54 upgrade_main_savepoint($result, 2006100601);
57 if ($oldversion < 2006101001) { /// Disable the LAMS module by default (if it is installed)
58 if (count_records('modules', 'name', 'lams') && !count_records('lams')) {
59 set_field('modules', 'visible', 0, 'name', 'lams'); // Disable it by default
62 upgrade_main_savepoint($result, 2006101001);
65 if ($result && $oldversion < 2006102600) {
67 /// Define fields to be added to user_info_field
68 $table = new XMLDBTable('user_info_field');
69 $field = new XMLDBField('description');
70 $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'categoryid');
71 $field1 = new XMLDBField('param1');
72 $field1->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'defaultdata');
73 $field2 = new XMLDBField('param2');
74 $field2->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param1');
75 $field3 = new XMLDBField('param3');
76 $field3->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param2');
77 $field4 = new XMLDBField('param4');
78 $field4->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param3');
79 $field5 = new XMLDBField('param5');
80 $field5->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param4');
82 /// Launch add fields
83 $result = $result && add_field($table, $field);
84 $result = $result && add_field($table, $field1);
85 $result = $result && add_field($table, $field2);
86 $result = $result && add_field($table, $field3);
87 $result = $result && add_field($table, $field4);
88 $result = $result && add_field($table, $field5);
90 upgrade_main_savepoint($result, 2006102600);
93 if ($result && $oldversion < 2006112000) {
95 /// Define field attachment to be added to post
96 $table = new XMLDBTable('post');
97 $field = new XMLDBField('attachment');
98 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null, 'format');
100 /// Launch add field attachment
101 $result = $result && add_field($table, $field);
103 upgrade_main_savepoint($result, 2006112000);
106 if ($result && $oldversion < 2006112200) {
108 /// Define field imagealt to be added to user
109 $table = new XMLDBTable('user');
110 $field = new XMLDBField('imagealt');
111 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'trustbitmask');
113 /// Launch add field imagealt
114 $result = $result && add_field($table, $field);
116 $table = new XMLDBTable('user');
117 $field = new XMLDBField('screenreader');
118 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, null, '0', 'imagealt');
120 /// Launch add field screenreader
121 $result = $result && add_field($table, $field);
123 upgrade_main_savepoint($result, 2006112200);
126 if ($oldversion < 2006120300) { /// Delete guest course section settings
127 // following code can be executed repeatedly, such as when upgrading from 1.7.x - it is ok
128 if ($guest = get_record('user', 'username', 'guest')) {
129 execute_sql("DELETE FROM {$CFG->prefix}course_display where userid=$guest->id", true);
132 upgrade_main_savepoint($result, 2006120300);
135 if ($oldversion < 2006120400) { /// Remove secureforms config setting
136 execute_sql("DELETE FROM {$CFG->prefix}config where name='secureforms'", true);
138 upgrade_main_savepoint($result, 2006120400);
141 if (!empty($CFG->rolesactive) && $oldversion < 2006120700) { // add moodle/user:viewdetails to all roles!
142 // note: use of assign_capability() is discouraged in upgrade script!
143 if ($roles = get_records('role')) {
144 $context = get_context_instance(CONTEXT_SYSTEM);
145 foreach ($roles as $roleid=>$role) {
146 assign_capability('moodle/user:viewdetails', CAP_ALLOW, $roleid, $context->id);
150 upgrade_main_savepoint($result, 2006120700);
153 // Move the auth plugin settings into the config_plugin table
154 if ($oldversion < 2007010300) {
155 if ($CFG->auth == 'email') {
156 set_config('registerauth', 'email');
157 } else {
158 set_config('registerauth', '');
160 $authplugins = get_list_of_plugins('auth');
161 foreach ($CFG as $k => $v) {
162 if (strpos($k, 'ldap_') === 0) {
163 //upgrade nonstandard ldap settings
164 $setting = substr($k, 5);
165 if (set_config($setting, $v, "auth/ldap")) {
166 delete_records('config', 'name', $k);
167 unset($CFG->{$k});
169 continue;
171 if (strpos($k, 'auth_') !== 0) {
172 continue;
174 $authsetting = substr($k, 5);
175 foreach ($authplugins as $auth) {
176 if (strpos($authsetting, $auth) !== 0) {
177 continue;
179 $setting = substr($authsetting, strlen($auth));
180 if (set_config($setting, $v, "auth/$auth")) {
181 delete_records('config', 'name', $k);
182 unset($CFG->{$k});
184 break; // don't check the rest of the auth plugin names
188 upgrade_main_savepoint($result, 2007010300);
191 if ($oldversion < 2007010301) {
193 // Core MNET tables
195 $table = new XMLDBTable('mnet_host');
196 $table->comment = 'Information about the local and remote hosts for RPC';
197 // fields
198 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
199 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
200 $f->comment = 'Unique Host ID';
201 $f = $table->addFieldInfo('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
202 XMLDB_NOTNULL, null, null, null, 0);
203 $f = $table->addFieldInfo('wwwroot', XMLDB_TYPE_CHAR, '255', null,
204 XMLDB_NOTNULL, null, null, null, null);
205 $f = $table->addFieldInfo('ip_address', XMLDB_TYPE_CHAR, '39', null,
206 XMLDB_NOTNULL, null, null, null, null);
207 $f = $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '80', null,
208 XMLDB_NOTNULL, null, null, null, null);
209 $f = $table->addFieldInfo('public_key', XMLDB_TYPE_TEXT, 'medium', null,
210 XMLDB_NOTNULL, null, null, null, null);
211 $f = $table->addFieldInfo('public_key_expires', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
212 XMLDB_NOTNULL, null, null, null, 0);
213 $f = $table->addFieldInfo('transport', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED,
214 XMLDB_NOTNULL, null, null, null, 0);
215 $f = $table->addFieldInfo('portno', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED,
216 XMLDB_NOTNULL, null, null, null, 0);
217 $f = $table->addFieldInfo('last_connect_time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
218 XMLDB_NOTNULL, null, null, null, 0);
219 $f = $table->addFieldInfo('last_log_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
220 XMLDB_NOTNULL, null, null, null, 0);
221 // PK and indexes
222 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
223 // Create the table
224 $result = $result && create_table($table);
226 $table = new XMLDBTable('mnet_host2service');
227 $table->comment = 'Information about the services for a given host';
228 // fields
229 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
230 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
231 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
232 XMLDB_NOTNULL, NULL, null, null, 0);
233 $f = $table->addFieldInfo('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
234 XMLDB_NOTNULL, NULL, null, null, 0);
235 $f = $table->addFieldInfo('publish', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
236 XMLDB_NOTNULL, NULL, null, null, 0);
237 $f = $table->addFieldInfo('subscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
238 XMLDB_NOTNULL, NULL, null, null, 0);
239 // PK and indexes
240 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
241 $table->addIndexInfo('hostid_serviceid', XMLDB_INDEX_UNIQUE, array('hostid', 'serviceid'));
242 // Create the table
243 $result = $result && create_table($table);
245 $table = new XMLDBTable('mnet_log');
246 $table->comment = 'Store session data from users migrating to other sites';
247 // fields
248 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
249 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
250 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
251 XMLDB_NOTNULL, NULL, null, null, 0);
252 $f = $table->addFieldInfo('remoteid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
253 XMLDB_NOTNULL, NULL, null, null, 0);
254 $f = $table->addFieldInfo('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
255 XMLDB_NOTNULL, NULL, null, null, 0);
256 $f = $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
257 XMLDB_NOTNULL, NULL, null, null, 0);
258 $f = $table->addFieldInfo('ip', XMLDB_TYPE_CHAR, '15', null,
259 XMLDB_NOTNULL, NULL, null, null, null);
260 $f = $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
261 XMLDB_NOTNULL, NULL, null, null, 0);
262 $f = $table->addFieldInfo('coursename', XMLDB_TYPE_CHAR, '40', null,
263 XMLDB_NOTNULL, NULL, null, null, null);
264 $f = $table->addFieldInfo('module', XMLDB_TYPE_CHAR, '20', null,
265 XMLDB_NOTNULL, NULL, null, null, null);
266 $f = $table->addFieldInfo('cmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
267 XMLDB_NOTNULL, NULL, null, null, 0);
268 $f = $table->addFieldInfo('action', XMLDB_TYPE_CHAR, '40', null,
269 XMLDB_NOTNULL, NULL, null, null, null);
270 $f = $table->addFieldInfo('url', XMLDB_TYPE_CHAR, '100', null,
271 XMLDB_NOTNULL, NULL, null, null, null);
272 $f = $table->addFieldInfo('info', XMLDB_TYPE_CHAR, '255', null,
273 XMLDB_NOTNULL, NULL, null, null, null);
274 // PK and indexes
275 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
276 $table->addIndexInfo('host_user_course', XMLDB_INDEX_NOTUNIQUE, array('hostid', 'userid', 'course'));
277 // Create the table
278 $result = $result && create_table($table);
281 $table = new XMLDBTable('mnet_rpc');
282 $table->comment = 'Functions or methods that we may publish or subscribe to';
283 // fields
284 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
285 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
286 $f = $table->addFieldInfo('function_name', XMLDB_TYPE_CHAR, '40', null,
287 XMLDB_NOTNULL, NULL, null, null, null);
288 $f = $table->addFieldInfo('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null,
289 XMLDB_NOTNULL, NULL, null, null, null);
290 $f = $table->addFieldInfo('parent_type', XMLDB_TYPE_CHAR, '6', null,
291 XMLDB_NOTNULL, NULL, null, null, null);
292 $f = $table->addFieldInfo('parent', XMLDB_TYPE_CHAR, '20', null,
293 XMLDB_NOTNULL, NULL, null, null, null);
294 $f = $table->addFieldInfo('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
295 XMLDB_NOTNULL, NULL, null, null, 0);
296 $f = $table->addFieldInfo('help', XMLDB_TYPE_TEXT, 'medium', null,
297 XMLDB_NOTNULL, NULL, null, null, null);
298 $f = $table->addFieldInfo('profile', XMLDB_TYPE_TEXT, 'medium', null,
299 XMLDB_NOTNULL, NULL, null, null, null);
300 // PK and indexes
301 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
302 $table->addIndexInfo('enabled_xpath', XMLDB_INDEX_NOTUNIQUE, array('enabled', 'xmlrpc_path'));
303 // Create the table
304 $result = $result && create_table($table);
306 $table = new XMLDBTable('mnet_service');
307 $table->comment = 'A service is a group of functions';
308 // fields
309 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
310 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
311 $f = $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '40', null,
312 XMLDB_NOTNULL, NULL, null, null, null);
313 $f = $table->addFieldInfo('description', XMLDB_TYPE_CHAR, '40', null,
314 XMLDB_NOTNULL, NULL, null, null, null);
315 $f = $table->addFieldInfo('apiversion', XMLDB_TYPE_CHAR, '10', null,
316 XMLDB_NOTNULL, NULL, null, null, null);
317 $f = $table->addFieldInfo('offer', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
318 XMLDB_NOTNULL, NULL, null, null, 0);
319 // PK and indexes
320 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
321 // Create the table
322 $result = $result && create_table($table);
324 $table = new XMLDBTable('mnet_service2rpc');
325 $table->comment = 'Group functions or methods under a service';
326 // fields
327 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
328 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
329 $f = $table->addFieldInfo('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
330 XMLDB_NOTNULL, NULL, null, null, 0);
331 $f = $table->addFieldInfo('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
332 XMLDB_NOTNULL, NULL, null, null, 0);
333 // PK and indexes
334 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
335 $table->addIndexInfo('unique', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
336 // Create the table
337 $result = $result && create_table($table);
340 // Prime MNET configuration entries -- will be needed later by auth/mnet
342 include_once $CFG->dirroot . '/mnet/lib.php';
343 $env = new mnet_environment();
344 $env->init();
345 unset($env);
347 // add mnethostid to user-
348 $table = new XMLDBTable('user');
349 $field = new XMLDBField('mnethostid');
350 $field->setType(XMLDB_TYPE_INTEGER);
351 $field->setLength(10);
352 $field->setNotNull(true);
353 $field->setSequence(null);
354 $field->setEnum(null);
355 $field->setDefault('0');
356 $field->setPrevious("deleted");
357 $field->setNext("username");
358 $result = $result && add_field($table, $field);
360 // The default mnethostid is zero... we need to update this for all
361 // users of the local IdP service.
362 set_field('user',
363 'mnethostid', $CFG->mnet_localhost_id,
364 'mnethostid', '0');
367 $index = new XMLDBIndex('username');
368 $index->setUnique(true);
369 $index->setFields(array('username'));
370 drop_index($table, $index);
371 $index->setFields(array('mnethostid', 'username'));
372 if (!add_index($table, $index)) {
373 notify(get_string('duplicate_usernames', 'mnet', 'http://docs.moodle.org/en/DuplicateUsernames'));
376 unset($table, $field, $index);
379 ** auth/mnet tables
381 $table = new XMLDBTable('mnet_session');
382 $table->comment='Store session data from users migrating to other sites';
383 // fields
384 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
385 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
386 $f = $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
387 XMLDB_NOTNULL, NULL, null, null, 0);
388 $f = $table->addFieldInfo('username', XMLDB_TYPE_CHAR, '100', null,
389 XMLDB_NOTNULL, NULL, null, null, null);
390 $f = $table->addFieldInfo('token', XMLDB_TYPE_CHAR, '40', null,
391 XMLDB_NOTNULL, NULL, null, null, null);
392 $f = $table->addFieldInfo('mnethostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
393 XMLDB_NOTNULL, NULL, null, null, 0);
394 $f = $table->addFieldInfo('useragent', XMLDB_TYPE_CHAR, '40', null,
395 XMLDB_NOTNULL, NULL, null, null, null);
396 $f = $table->addFieldInfo('confirm_timeout', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
397 XMLDB_NOTNULL, NULL, null, null, 0);
398 $f = $table->addFieldInfo('session_id', XMLDB_TYPE_CHAR, '40', null,
399 XMLDB_NOTNULL, NULL, null, null, null);
400 $f = $table->addFieldInfo('expires', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
401 XMLDB_NOTNULL, NULL, null, null, 0);
402 // PK and indexes
403 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
404 $table->addIndexInfo('token', XMLDB_INDEX_UNIQUE, array('token'));
405 // Create the table
406 $result = $result && create_table($table);
409 $table = new XMLDBTable('mnet_sso_access_control');
410 $table->comment = 'Users by host permitted (or not) to login from a remote provider';
411 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
412 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
413 $f = $table->addFieldInfo('username', XMLDB_TYPE_CHAR, '100', null,
414 XMLDB_NOTNULL, NULL, null, null, null);
415 $f = $table->addFieldInfo('mnet_host_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
416 XMLDB_NOTNULL, NULL, null, null, 0);
417 $f = $table->addFieldInfo('access', XMLDB_TYPE_CHAR, '20', null,
418 XMLDB_NOTNULL, NULL, null, null, 'allow');
419 // PK and indexes
420 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
421 $table->addIndexInfo('mnethostid_username', XMLDB_INDEX_UNIQUE, array('mnet_host_id', 'username'));
422 // Create the table
423 $result = $result && create_table($table);
425 if (empty($USER->mnet_host_id)) {
426 $USER->mnet_host_id = $CFG->mnet_localhost_id; // Something for the current user to prevent warnings
430 ** enrol/mnet tables
432 $table = new XMLDBTable('mnet_enrol_course');
433 $table->comment = 'Information about courses on remote hosts';
434 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
435 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
436 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
437 XMLDB_NOTNULL, NULL, null, null, 0);
438 $f = $table->addFieldInfo('remoteid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
439 XMLDB_NOTNULL, NULL, null, null, 0);
440 $f = $table->addFieldInfo('cat_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
441 XMLDB_NOTNULL, NULL, null, null, 0);
442 $f = $table->addFieldInfo('cat_name', XMLDB_TYPE_CHAR, '255', null,
443 XMLDB_NOTNULL, NULL, null, null, null);
444 $f = $table->addFieldInfo('cat_description', XMLDB_TYPE_TEXT, 'medium', null,
445 XMLDB_NOTNULL, NULL, null, null, null);
446 $f = $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
447 XMLDB_NOTNULL, NULL, null, null, 0);
448 $f = $table->addFieldInfo('fullname', XMLDB_TYPE_CHAR, '254', null,
449 XMLDB_NOTNULL, NULL, null, null, null);
450 $f = $table->addFieldInfo('shortname', XMLDB_TYPE_CHAR, '15', null,
451 XMLDB_NOTNULL, NULL, null, null, null);
452 $f = $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '100', null,
453 XMLDB_NOTNULL, NULL, null, null, null);
454 $f = $table->addFieldInfo('summary', XMLDB_TYPE_TEXT, 'medium', null,
455 XMLDB_NOTNULL, NULL, null, null, null);
456 $f = $table->addFieldInfo('startdate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
457 XMLDB_NOTNULL, NULL, null, null, 0);
458 $f = $table->addFieldInfo('cost', XMLDB_TYPE_CHAR, '10', null,
459 XMLDB_NOTNULL, NULL, null, null, null);
460 $f = $table->addFieldInfo('currency', XMLDB_TYPE_CHAR, '3', null,
461 XMLDB_NOTNULL, NULL, null, null, null);
462 $f = $table->addFieldInfo('defaultroleid', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
463 XMLDB_NOTNULL, NULL, null, null, 0);
464 $f = $table->addFieldInfo('defaultrolename', XMLDB_TYPE_CHAR, '255', null,
465 XMLDB_NOTNULL, NULL, null, null, null);
466 // PK and indexes
467 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
468 $table->addIndexInfo('hostid_remoteid', XMLDB_INDEX_UNIQUE, array('hostid', 'remoteid'));
469 // Create the table
470 $result = $result && create_table($table);
473 $table = new XMLDBTable('mnet_enrol_assignments');
475 $table->comment = 'Information about enrolments on courses on remote hosts';
476 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
477 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
478 $f = $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
479 XMLDB_NOTNULL, NULL, null, null, 0);
480 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
481 XMLDB_NOTNULL, NULL, null, null, 0);
482 $f = $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
483 XMLDB_NOTNULL, NULL, null, null, 0);
484 $f = $table->addFieldInfo('rolename', XMLDB_TYPE_CHAR, '255', null,
485 XMLDB_NOTNULL, NULL, null, null, null);
486 $f = $table->addFieldInfo('enroltime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
487 XMLDB_NOTNULL, NULL, null, null, 0);
488 $f = $table->addFieldInfo('enroltype', XMLDB_TYPE_CHAR, '20', null,
489 XMLDB_NOTNULL, NULL, null, null, null);
491 // PK and indexes
492 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
493 $table->addIndexInfo('hostid_courseid', XMLDB_INDEX_NOTUNIQUE, array('hostid', 'courseid'));
494 $table->addIndexInfo('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
495 // Create the table
496 $result = $result && create_table($table);
498 upgrade_main_savepoint($result, 2007010301);
501 if ($result && $oldversion < 2007010404) {
503 /// Define field shortname to be added to user_info_field
504 $table = new XMLDBTable('user_info_field');
505 $field = new XMLDBField('shortname');
506 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, 'shortname', 'id');
508 /// Launch add field shortname
509 $result = $result && add_field($table, $field);
511 /// Changing type of field name on table user_info_field to text
512 $table = new XMLDBTable('user_info_field');
513 $field = new XMLDBField('name');
514 $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null, 'shortname');
516 /// Launch change of type for field name
517 $result = $result && change_field_type($table, $field);
519 /// For existing fields use 'name' as the 'shortname' entry
520 if ($fields = get_records_select('user_info_field', '', '', 'id, name')) {
521 foreach ($fields as $field) {
522 $field->shortname = clean_param($field->name, PARAM_ALPHANUM);
523 $result && update_record('user_info_field', $field);
527 upgrade_main_savepoint($result, 2007010404);
530 if ($result && $oldversion < 2007011501) {
531 if (!empty($CFG->enablerecordcache) && empty($CFG->rcache) &&
532 // Note: won't force-load these settings into CFG
533 // we don't need or want cache during the upgrade itself
534 empty($CFG->cachetype) && empty($CFG->intcachemax)) {
535 set_config('cachetype', 'internal');
536 set_config('rcache', true);
537 set_config('intcachemax', $CFG->enablerecordcache);
538 unset_config('enablerecordcache');
539 unset($CFG->enablerecordcache);
542 upgrade_main_savepoint($result, 2007011501);
545 if ($result && $oldversion < 2007012100) {
546 /// Some old PG servers have user->firstname & user->lastname with 30cc. They must be 100cc.
547 /// Fixing that conditionally. MDL-7110
548 if ($CFG->dbfamily == 'postgres') {
549 /// Get Metadata from user table
550 $cols = array_change_key_case($db->MetaColumns($CFG->prefix . 'user'), CASE_LOWER);
552 /// Process user->firstname if needed
553 if ($col = $cols['firstname']) {
554 if ($col->max_length < 100) {
555 /// Changing precision of field firstname on table user to (100)
556 $table = new XMLDBTable('user');
557 $field = new XMLDBField('firstname');
558 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null, 'idnumber');
560 /// Launch change of precision for field firstname
561 $result = $result && change_field_precision($table, $field);
565 /// Process user->lastname if needed
566 if ($col = $cols['lastname']) {
567 if ($col->max_length < 100) {
568 /// Changing precision of field lastname on table user to (100)
569 $table = new XMLDBTable('user');
570 $field = new XMLDBField('lastname');
571 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null, 'firstname');
573 /// Launch change of precision for field lastname
574 $result = $result && change_field_precision($table, $field);
579 upgrade_main_savepoint($result, 2007012100);
582 if ($result && $oldversion < 2007012101) {
584 /// Changing precision of field lang on table course to (30)
585 $table = new XMLDBTable('course');
586 $field = new XMLDBField('lang');
587 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null, 'groupmodeforce');
589 /// Launch change of precision for field course->lang
590 $result = $result && change_field_precision($table, $field);
592 /// Changing precision of field lang on table user to (30)
593 $table = new XMLDBTable('user');
594 $field = new XMLDBField('lang');
595 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'en', 'country');
597 /// Launch change of precision for field user->lang
598 $result = $result && change_field_precision($table, $field);
600 upgrade_main_savepoint($result, 2007012101);
603 if ($result && $oldversion < 2007012400) {
605 /// Rename field access on table mnet_sso_access_control to accessctrl
606 $table = new XMLDBTable('mnet_sso_access_control');
607 $field = new XMLDBField('access');
608 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'allow', 'mnet_host_id');
610 /// Launch rename field accessctrl
611 $result = $result && rename_field($table, $field, 'accessctrl');
613 upgrade_main_savepoint($result, 2007012400);
616 if ($result && $oldversion < 2007012500) {
617 execute_sql("DELETE FROM {$CFG->prefix}user WHERE username='changeme'", true);
619 upgrade_main_savepoint($result, 2007012500);
622 if ($result && $oldversion < 2007020400) {
623 /// Only for MySQL and PG, declare the user->ajax field as not null. MDL-8421.
624 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
625 /// Changing nullability of field ajax on table user to not null
626 $table = new XMLDBTable('user');
627 $field = new XMLDBField('ajax');
628 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'htmleditor');
630 /// Launch change of nullability for field ajax
631 $result = $result && change_field_notnull($table, $field);
634 upgrade_main_savepoint($result, 2007020400);
637 if (!empty($CFG->rolesactive) && $result && $oldversion < 2007021401) {
638 /// create default logged in user role if not present - upgrade rom 1.7.x
639 if (empty($CFG->defaultuserroleid) or empty($CFG->guestroleid) or $CFG->defaultuserroleid == $CFG->guestroleid) {
640 if (!get_records('role', 'shortname', 'user')) {
641 $userroleid = create_role(addslashes(get_string('authenticateduser')), 'user',
642 addslashes(get_string('authenticateduserdescription')), 'moodle/legacy:user');
643 if ($userroleid) {
644 reset_role_capabilities($userroleid);
645 set_config('defaultuserroleid', $userroleid);
650 upgrade_main_savepoint($result, 2007021401);
653 if ($result && $oldversion < 2007021501) {
654 /// delete removed setting from config
655 unset_config('tabselectedtofront');
657 upgrade_main_savepoint($result, 2007021501);
661 if ($result && $oldversion < 2007032200) {
663 /// Define table role_sortorder to be created
664 $table = new XMLDBTable('role_sortorder');
666 /// Adding fields to table role_sortorder
667 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
668 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
669 $table->addFieldInfo('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
670 $table->addFieldInfo('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
671 $table->addFieldInfo('sortoder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null);
673 /// Adding keys to table role_sortorder
674 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
675 $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
676 $table->addKeyInfo('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
677 $table->addKeyInfo('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
679 /// Adding indexes to table role_sortorder
680 $table->addIndexInfo('userid-roleid-contextid', XMLDB_INDEX_UNIQUE, array('userid', 'roleid', 'contextid'));
682 /// Launch create table for role_sortorder
683 $result = $result && create_table($table);
685 upgrade_main_savepoint($result, 2007032200);
689 /// code to change lenghen tag field to 255, MDL-9095
690 if ($result && $oldversion < 2007040400) {
692 /// Define index text (not unique) to be dropped form tags
693 $table = new XMLDBTable('tags');
694 $index = new XMLDBIndex('text');
695 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('text'));
697 /// Launch drop index text
698 $result = $result && drop_index($table, $index);
700 $field = new XMLDBField('text');
701 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'userid');
703 /// Launch change of type for field text
704 $result = $result && change_field_type($table, $field);
706 $index = new XMLDBIndex('text');
707 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('text'));
709 /// Launch add index text
710 $result = $result && add_index($table, $index);
712 upgrade_main_savepoint($result, 2007040400);
715 if ($result && $oldversion < 2007041100) {
717 /// Define field idnumber to be added to course_modules
718 $table = new XMLDBTable('course_modules');
719 $field = new XMLDBField('idnumber');
720 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null, 'section');
722 /// Launch add field idnumber
723 $result = $result && add_field($table, $field);
725 upgrade_main_savepoint($result, 2007041100);
728 /* Changes to the custom profile menu type - store values rather than indices.
729 We could do all this with one tricky SQL statement but it's a one-off so no
730 harm in using PHP loops */
731 if ($result && $oldversion < 2007041600) {
733 /// Get the menu fields
734 if ($fields = get_records('user_info_field', 'datatype', 'menu')) {
735 foreach ($fields as $field) {
737 /// Get user data for the menu field
738 if ($data = get_records('user_info_data', 'fieldid', $field->id)) {
740 /// Get the menu options
741 $options = explode("\n", $field->param1);
742 foreach ($data as $d) {
743 $key = array_search($d->data, $options);
745 /// If the data is an integer and is not one of the options,
746 /// set the respective option value
747 if (is_int($d->data) and (($key === NULL) or ($key === false)) and isset($options[$d->data])) {
748 $d->data = $options[$d->data];
749 $result = $result && update_record('user_info_data', $d);
756 upgrade_main_savepoint($result, 2007041600);
759 /// adding new gradebook tables
760 if ($result && $oldversion < 2007041800) {
762 /// Define table events_handlers to be created
763 $table = new XMLDBTable('events_handlers');
765 /// Adding fields to table events_handlers
766 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
767 $table->addFieldInfo('eventname', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null, null, null);
768 $table->addFieldInfo('handlermodule', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null, null, null);
769 $table->addFieldInfo('handlerfile', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
770 $table->addFieldInfo('handlerfunction', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
772 /// Adding keys to table events_handlers
773 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
775 /// Adding indexes to table events_handlers
776 $table->addIndexInfo('eventname-handlermodule', XMLDB_INDEX_UNIQUE, array('eventname', 'handlermodule'));
778 /// Launch create table for events_handlers
779 $result = $result && create_table($table);
781 /// Define table events_queue to be created
782 $table = new XMLDBTable('events_queue');
784 /// Adding fields to table events_queue
785 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
786 $table->addFieldInfo('eventdata', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null);
787 $table->addFieldInfo('schedule', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
788 $table->addFieldInfo('stackdump', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
789 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
790 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
792 /// Adding keys to table events_queue
793 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
794 $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
796 /// Launch create table for events_queue
797 $result = $result && create_table($table);
799 /// Define table events_queue_handlers to be created
800 $table = new XMLDBTable('events_queue_handlers');
802 /// Adding fields to table events_queue_handlers
803 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
804 $table->addFieldInfo('queuedeventid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
805 $table->addFieldInfo('handlerid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
806 $table->addFieldInfo('status', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
807 $table->addFieldInfo('errormessage', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
808 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
810 /// Adding keys to table events_queue_handlers
811 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
812 $table->addKeyInfo('queuedeventid', XMLDB_KEY_FOREIGN, array('queuedeventid'), 'events_queue', array('id'));
813 $table->addKeyInfo('handlerid', XMLDB_KEY_FOREIGN, array('handlerid'), 'events_handlers', array('id'));
815 /// Launch create table for events_queue_handlers
816 $result = $result && create_table($table);
818 upgrade_main_savepoint($result, 2007041800);
821 if ($result && $oldversion < 2007043001) {
823 /// Define field schedule to be added to events_handlers
824 $table = new XMLDBTable('events_handlers');
825 $field = new XMLDBField('schedule');
826 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'handlerfunction');
828 /// Launch add field schedule
829 $result = $result && add_field($table, $field);
831 /// Define field status to be added to events_handlers
832 $table = new XMLDBTable('events_handlers');
833 $field = new XMLDBField('status');
834 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'schedule');
836 /// Launch add field status
837 $result = $result && add_field($table, $field);
839 upgrade_main_savepoint($result, 2007043001);
842 if ($result && $oldversion < 2007050201) {
844 /// Define field theme to be added to course_categories
845 $table = new XMLDBTable('course_categories');
846 $field = new XMLDBField('theme');
847 $field->setAttributes(XMLDB_TYPE_CHAR, '50', null, null, null, null, null, null, 'path');
849 /// Launch add field theme
850 $result = $result && add_field($table, $field);
852 upgrade_main_savepoint($result, 2007050201);
855 if ($result && $oldversion < 2007051100) {
857 /// Define field forceunique to be added to user_info_field
858 $table = new XMLDBTable('user_info_field');
859 $field = new XMLDBField('forceunique');
860 $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'visible');
862 /// Launch add field forceunique
863 $result = $result && add_field($table, $field);
865 /// Define field signup to be added to user_info_field
866 $table = new XMLDBTable('user_info_field');
867 $field = new XMLDBField('signup');
868 $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'forceunique');
870 /// Launch add field signup
871 $result = $result && add_field($table, $field);
873 upgrade_main_savepoint($result, 2007051100);
876 if (!empty($CFG->rolesactive) && $result && $oldversion < 2007051801) {
877 // Get the role id of the "Auth. User" role and check if the default role id is different
878 // note: use of assign_capability() is discouraged in upgrade script!
879 $userrole = get_record( 'role', 'shortname', 'user' );
880 $defaultroleid = $CFG->defaultuserroleid;
882 if( $defaultroleid != $userrole->id ) {
883 // Add in the new moodle/my:manageblocks capibility to the default user role
884 $context = get_context_instance(CONTEXT_SYSTEM);
885 assign_capability('moodle/my:manageblocks',CAP_ALLOW,$defaultroleid,$context->id);
888 upgrade_main_savepoint($result, 2007051801);
891 if ($result && $oldversion < 2007052200) {
893 /// Define field schedule to be dropped from events_queue
894 $table = new XMLDBTable('events_queue');
895 $field = new XMLDBField('schedule');
897 /// Launch drop field stackdump
898 $result = $result && drop_field($table, $field);
900 upgrade_main_savepoint($result, 2007052200);
903 if ($result && $oldversion < 2007052300) {
904 require_once($CFG->dirroot . '/question/upgrade.php');
905 $result = $result && question_remove_rqp_qtype();
907 upgrade_main_savepoint($result, 2007052300);
910 if ($result && $oldversion < 2007060500) {
912 /// Define field usermodified to be added to post
913 $table = new XMLDBTable('post');
914 $field = new XMLDBField('usermodified');
915 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'created');
917 /// Launch add field usermodified
918 $result = $result && add_field($table, $field);
920 /// Define key usermodified (foreign) to be added to post
921 $table = new XMLDBTable('post');
922 $key = new XMLDBKey('usermodified');
923 $key->setAttributes(XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
925 /// Launch add key usermodified
926 $result = $result && add_key($table, $key);
928 upgrade_main_savepoint($result, 2007060500);
931 if ($result && $oldversion < 2007070603) {
932 // Small update of guest user to be 100% sure it has the correct mnethostid (MDL-10375)
933 set_field('user', 'mnethostid', $CFG->mnet_localhost_id, 'username', 'guest');
935 upgrade_main_savepoint($result, 2007070603);
938 if ($result && $oldversion < 2007071400) {
940 ** mnet application table
942 $table = new XMLDBTable('mnet_application');
943 $table->comment = 'Information about applications on remote hosts';
944 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
945 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
946 $f = $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '50', null,
947 XMLDB_NOTNULL, NULL, null, null, null);
948 $f = $table->addFieldInfo('display_name', XMLDB_TYPE_CHAR, '50', null,
949 XMLDB_NOTNULL, NULL, null, null, null);
950 $f = $table->addFieldInfo('xmlrpc_server_url', XMLDB_TYPE_CHAR, '255', null,
951 XMLDB_NOTNULL, NULL, null, null, null);
952 $f = $table->addFieldInfo('sso_land_url', XMLDB_TYPE_CHAR, '255', null,
953 XMLDB_NOTNULL, NULL, null, null, null);
955 // PK and indexes
956 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
957 // Create the table
958 $result = $result && create_table($table);
960 // Insert initial applications (moodle and mahara)
961 $application = new stdClass();
962 $application->name = 'moodle';
963 $application->display_name = 'Moodle';
964 $application->xmlrpc_server_url = '/mnet/xmlrpc/server.php';
965 $application->sso_land_url = '/auth/mnet/land.php';
966 if ($result) {
967 $newid = insert_record('mnet_application', $application, false);
970 $application = new stdClass();
971 $application->name = 'mahara';
972 $application->display_name = 'Mahara';
973 $application->xmlrpc_server_url = '/api/xmlrpc/server.php';
974 $application->sso_land_url = '/auth/xmlrpc/land.php';
975 $result = $result && insert_record('mnet_application', $application, false);
977 // New mnet_host->applicationid field
978 $table = new XMLDBTable('mnet_host');
979 $field = new XMLDBField('applicationid');
980 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, $newid , 'last_log_id');
982 $result = $result && add_field($table, $field);
984 /// Define key applicationid (foreign) to be added to mnet_host
985 $table = new XMLDBTable('mnet_host');
986 $key = new XMLDBKey('applicationid');
987 $key->setAttributes(XMLDB_KEY_FOREIGN, array('applicationid'), 'mnet_application', array('id'));
989 /// Launch add key applicationid
990 $result = $result && add_key($table, $key);
992 upgrade_main_savepoint($result, 2007071400);
995 if ($result && $oldversion < 2007071607) {
996 require_once($CFG->dirroot . '/question/upgrade.php');
997 $result = $result && question_remove_rqp_qtype_config_string();
999 upgrade_main_savepoint($result, 2007071607);
1002 if ($result && $oldversion < 2007072200) {
1004 /// Remove all grade tables used in development phases - we need new empty tables for final gradebook upgrade
1005 $tables = array('grade_categories',
1006 'grade_items',
1007 'grade_calculations',
1008 'grade_grades',
1009 'grade_grades_raw',
1010 'grade_grades_final',
1011 'grade_grades_text',
1012 'grade_outcomes',
1013 'grade_outcomes_courses',
1014 'grade_history',
1015 'grade_import_newitem',
1016 'grade_import_values');
1018 foreach ($tables as $table) {
1019 $table = new XMLDBTable($table);
1020 if (table_exists($table)) {
1021 drop_table($table);
1025 $tables = array('grade_categories_history',
1026 'grade_items_history',
1027 'grade_grades_history',
1028 'grade_grades_text_history',
1029 'grade_scale_history',
1030 'grade_outcomes_history');
1032 foreach ($tables as $table) {
1033 $table = new XMLDBTable($table);
1034 if (table_exists($table)) {
1035 drop_table($table);
1040 /// Define table grade_outcomes to be created
1041 $table = new XMLDBTable('grade_outcomes');
1043 /// Adding fields to table grade_outcomes
1044 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1045 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1046 $table->addFieldInfo('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1047 $table->addFieldInfo('fullname', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
1048 $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1049 $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
1050 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1051 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1052 $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1054 /// Adding keys to table grade_outcomes
1055 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1056 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1057 $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id'));
1058 $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
1060 /// Launch create table for grade_outcomes
1061 $result = $result && create_table($table);
1064 /// Define table grade_categories to be created
1065 $table = new XMLDBTable('grade_categories');
1067 /// Adding fields to table grade_categories
1068 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1069 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1070 $table->addFieldInfo('parent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1071 $table->addFieldInfo('depth', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1072 $table->addFieldInfo('path', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1073 $table->addFieldInfo('fullname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1074 $table->addFieldInfo('aggregation', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1075 $table->addFieldInfo('keephigh', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1076 $table->addFieldInfo('droplow', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1077 $table->addFieldInfo('aggregateonlygraded', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1078 $table->addFieldInfo('aggregateoutcomes', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1079 $table->addFieldInfo('aggregatesubcats', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1080 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1081 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1083 /// Adding keys to table grade_categories
1084 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1085 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1086 $table->addKeyInfo('parent', XMLDB_KEY_FOREIGN, array('parent'), 'grade_categories', array('id'));
1088 /// Launch create table for grade_categories
1089 $result = $result && create_table($table);
1092 /// Define table grade_items to be created
1093 $table = new XMLDBTable('grade_items');
1095 /// Adding fields to table grade_items
1096 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1097 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1098 $table->addFieldInfo('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1099 $table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1100 $table->addFieldInfo('itemtype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null);
1101 $table->addFieldInfo('itemmodule', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null);
1102 $table->addFieldInfo('iteminstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1103 $table->addFieldInfo('itemnumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1104 $table->addFieldInfo('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1105 $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1106 $table->addFieldInfo('calculation', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1107 $table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1');
1108 $table->addFieldInfo('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
1109 $table->addFieldInfo('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1110 $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1111 $table->addFieldInfo('outcomeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1112 $table->addFieldInfo('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1113 $table->addFieldInfo('multfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '1.0');
1114 $table->addFieldInfo('plusfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1115 $table->addFieldInfo('aggregationcoef', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1116 $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1117 $table->addFieldInfo('display', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1118 $table->addFieldInfo('decimals', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null);
1119 $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1120 $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1121 $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1122 $table->addFieldInfo('needsupdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1123 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1124 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1126 /// Adding keys to table grade_items
1127 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1128 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1129 $table->addKeyInfo('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'grade_categories', array('id'));
1130 $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id'));
1131 $table->addKeyInfo('outcomeid', XMLDB_KEY_FOREIGN, array('outcomeid'), 'grade_outcomes', array('id'));
1133 /// Adding indexes to table grade_grades
1134 $table->addIndexInfo('locked-locktime', XMLDB_INDEX_NOTUNIQUE, array('locked', 'locktime'));
1135 $table->addIndexInfo('itemtype-needsupdate', XMLDB_INDEX_NOTUNIQUE, array('itemtype', 'needsupdate'));
1136 $table->addIndexInfo('gradetype', XMLDB_INDEX_NOTUNIQUE, array('gradetype'));
1138 /// Launch create table for grade_items
1139 $result = $result && create_table($table);
1142 /// Define table grade_grades to be created
1143 $table = new XMLDBTable('grade_grades');
1145 /// Adding fields to table grade_grades
1146 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1147 $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1148 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1149 $table->addFieldInfo('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
1150 $table->addFieldInfo('rawgrademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
1151 $table->addFieldInfo('rawgrademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1152 $table->addFieldInfo('rawscaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1153 $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1154 $table->addFieldInfo('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
1155 $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1156 $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1157 $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1158 $table->addFieldInfo('exported', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1159 $table->addFieldInfo('overridden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1160 $table->addFieldInfo('excluded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1161 $table->addFieldInfo('feedback', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1162 $table->addFieldInfo('feedbackformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1163 $table->addFieldInfo('information', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1164 $table->addFieldInfo('informationformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1165 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1166 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1168 /// Adding keys to table grade_grades
1169 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1170 $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
1171 $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1172 $table->addKeyInfo('rawscaleid', XMLDB_KEY_FOREIGN, array('rawscaleid'), 'scale', array('id'));
1173 $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
1175 /// Adding indexes to table grade_grades
1176 $table->addIndexInfo('locked-locktime', XMLDB_INDEX_NOTUNIQUE, array('locked', 'locktime'));
1178 /// Launch create table for grade_grades
1179 $result = $result && create_table($table);
1182 /// Define table grade_outcomes_history to be created
1183 $table = new XMLDBTable('grade_outcomes_history');
1185 /// Adding fields to table grade_outcomes_history
1186 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1187 $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1188 $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1189 $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1190 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1191 $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1192 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1193 $table->addFieldInfo('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1194 $table->addFieldInfo('fullname', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
1195 $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1196 $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
1198 /// Adding keys to table grade_outcomes_history
1199 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1200 $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_outcomes', array('id'));
1201 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1202 $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id'));
1203 $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id'));
1205 /// Adding indexes to table grade_outcomes_history
1206 $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
1208 /// Launch create table for grade_outcomes_history
1209 $result = $result && create_table($table);
1212 /// Define table grade_categories_history to be created
1213 $table = new XMLDBTable('grade_categories_history');
1215 /// Adding fields to table grade_categories_history
1216 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1217 $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1218 $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1219 $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1220 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1221 $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1222 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1223 $table->addFieldInfo('parent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1224 $table->addFieldInfo('depth', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1225 $table->addFieldInfo('path', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1226 $table->addFieldInfo('fullname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1227 $table->addFieldInfo('aggregation', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1228 $table->addFieldInfo('keephigh', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1229 $table->addFieldInfo('droplow', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1230 $table->addFieldInfo('aggregateonlygraded', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1231 $table->addFieldInfo('aggregateoutcomes', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1232 $table->addFieldInfo('aggregatesubcats', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1234 /// Adding keys to table grade_categories_history
1235 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1236 $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_categories', array('id'));
1237 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1238 $table->addKeyInfo('parent', XMLDB_KEY_FOREIGN, array('parent'), 'grade_categories', array('id'));
1239 $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id'));
1241 /// Adding indexes to table grade_categories_history
1242 $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
1244 /// Launch create table for grade_categories_history
1245 $result = $result && create_table($table);
1248 /// Define table grade_items_history to be created
1249 $table = new XMLDBTable('grade_items_history');
1251 /// Adding fields to table grade_items_history
1252 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1253 $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1254 $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1255 $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1256 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1257 $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1258 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1259 $table->addFieldInfo('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1260 $table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1261 $table->addFieldInfo('itemtype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null);
1262 $table->addFieldInfo('itemmodule', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null);
1263 $table->addFieldInfo('iteminstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1264 $table->addFieldInfo('itemnumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1265 $table->addFieldInfo('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1266 $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1267 $table->addFieldInfo('calculation', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1268 $table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1');
1269 $table->addFieldInfo('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
1270 $table->addFieldInfo('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1271 $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1272 $table->addFieldInfo('outcomeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1273 $table->addFieldInfo('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1274 $table->addFieldInfo('multfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '1.0');
1275 $table->addFieldInfo('plusfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1276 $table->addFieldInfo('aggregationcoef', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1277 $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1278 $table->addFieldInfo('display', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1279 $table->addFieldInfo('decimals', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null);
1280 $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1281 $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1282 $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1283 $table->addFieldInfo('needsupdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1285 /// Adding keys to table grade_items_history
1286 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1287 $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_items', array('id'));
1288 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1289 $table->addKeyInfo('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'grade_categories', array('id'));
1290 $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id'));
1291 $table->addKeyInfo('outcomeid', XMLDB_KEY_FOREIGN, array('outcomeid'), 'grade_outcomes', array('id'));
1292 $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id'));
1294 /// Adding indexes to table grade_items_history
1295 $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
1297 /// Launch create table for grade_items_history
1298 $result = $result && create_table($table);
1301 /// Define table grade_grades_history to be created
1302 $table = new XMLDBTable('grade_grades_history');
1304 /// Adding fields to table grade_grades_history
1305 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1306 $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1307 $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1308 $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1309 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1310 $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1311 $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1312 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1313 $table->addFieldInfo('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
1314 $table->addFieldInfo('rawgrademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
1315 $table->addFieldInfo('rawgrademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1316 $table->addFieldInfo('rawscaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1317 $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1318 $table->addFieldInfo('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
1319 $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1320 $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1321 $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1322 $table->addFieldInfo('exported', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1323 $table->addFieldInfo('overridden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1324 $table->addFieldInfo('excluded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1325 $table->addFieldInfo('feedback', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1326 $table->addFieldInfo('feedbackformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1327 $table->addFieldInfo('information', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1328 $table->addFieldInfo('informationformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1330 /// Adding keys to table grade_grades_history
1331 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1332 $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_grades', array('id'));
1333 $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
1334 $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1335 $table->addKeyInfo('rawscaleid', XMLDB_KEY_FOREIGN, array('rawscaleid'), 'scale', array('id'));
1336 $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
1337 $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id'));
1339 /// Adding indexes to table grade_grades_history
1340 $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
1342 /// Launch create table for grade_grades_history
1343 $result = $result && create_table($table);
1346 /// Define table scale_history to be created
1347 $table = new XMLDBTable('scale_history');
1349 /// Adding fields to table scale_history
1350 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1351 $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1352 $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1353 $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1354 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1355 $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1356 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1357 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1358 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1359 $table->addFieldInfo('scale', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
1360 $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
1362 /// Adding keys to table scale_history
1363 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1364 $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'scale', array('id'));
1365 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1366 $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id'));
1368 /// Adding indexes to table scale_history
1369 $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
1371 /// Launch create table for scale_history
1372 $result = $result && create_table($table);
1374 /// upgrade the old 1.8 gradebook - migrade data into new grade tables
1375 if ($result) {
1376 if ($rs = get_recordset('course')) {
1377 while ($course = rs_fetch_next_record($rs)) {
1378 // this function uses SQL only, it must not be changed after 1.9 goes stable!!
1379 if (!upgrade_18_gradebook($course->id)) {
1380 $result = false;
1381 break;
1384 rs_close($rs);
1388 upgrade_main_savepoint($result, 2007072200);
1391 if ($result && $oldversion < 2007072400) {
1392 /// Dropping one DEFAULT in a TEXT column. It's was only one remaining
1393 /// since Moodle 1.7, so new servers won't have those anymore.
1395 /// Changing the default of field sessdata on table sessions2 to drop it
1396 $table = new XMLDBTable('sessions2');
1397 $field = new XMLDBField('sessdata');
1398 $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'modified');
1400 /// Launch change of default for field sessdata
1401 $result = $result && change_field_default($table, $field);
1403 upgrade_main_savepoint($result, 2007072400);
1407 if ($result && $oldversion < 2007073100) {
1408 /// Define table grade_outcomes_courses to be created
1409 $table = new XMLDBTable('grade_outcomes_courses');
1411 /// Adding fields to table grade_outcomes_courses
1412 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1413 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1414 $table->addFieldInfo('outcomeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1416 /// Adding keys to table grade_outcomes_courses
1417 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1418 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1419 $table->addKeyInfo('outcomeid', XMLDB_KEY_FOREIGN, array('outcomeid'), 'grade_outcomes', array('id'));
1420 $table->addKeyInfo('courseid-outcomeid', XMLDB_KEY_UNIQUE, array('courseid', 'outcomeid'));
1421 /// Launch create table for grade_outcomes_courses
1422 $result = $result && create_table($table);
1424 upgrade_main_savepoint($result, 2007073100);
1428 if ($result && $oldversion < 2007073101) { // Add new tag tables
1430 /// Define table tag to be created
1431 $table = new XMLDBTable('tag');
1433 /// Adding fields to table tag
1434 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1435 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null);
1436 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1437 $table->addFieldInfo('tagtype', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1438 $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
1439 $table->addFieldInfo('descriptionformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null, null, null);
1440 $table->addFieldInfo('flag', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0');
1441 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1443 /// Adding keys to table tag
1444 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1446 /// Adding indexes to table tag
1447 $table->addIndexInfo('name', XMLDB_INDEX_UNIQUE, array('name'));
1449 /// Launch create table for tag
1450 $result = $result && create_table($table);
1454 /// Define table tag_correlation to be created
1455 $table = new XMLDBTable('tag_correlation');
1457 /// Adding fields to table tag_correlation
1458 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1459 $table->addFieldInfo('tagid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null);
1460 $table->addFieldInfo('correlatedtags', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
1462 /// Adding keys to table tag_correlation
1463 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1465 /// Adding indexes to table tag_correlation
1466 $table->addIndexInfo('tagid', XMLDB_INDEX_UNIQUE, array('tagid'));
1468 /// Launch create table for tag_correlation
1469 $result = $result && create_table($table);
1473 /// Define table tag_instance to be created
1474 $table = new XMLDBTable('tag_instance');
1476 /// Adding fields to table tag_instance
1477 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1478 $table->addFieldInfo('tagid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null);
1479 $table->addFieldInfo('itemtype', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1480 $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null);
1482 /// Adding keys to table tag_instance
1483 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1485 /// Adding indexes to table tag_instance
1486 $table->addIndexInfo('tagiditem', XMLDB_INDEX_NOTUNIQUE, array('tagid', 'itemtype', 'itemid'));
1488 /// Launch create table for tag_instance
1489 $result = $result && create_table($table);
1491 upgrade_main_savepoint($result, 2007073101);
1495 if ($result && $oldversion < 2007073103) {
1497 /// Define field rawname to be added to tag
1498 $table = new XMLDBTable('tag');
1499 $field = new XMLDBField('rawname');
1500 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'name');
1502 /// Launch add field rawname
1503 $result = $result && add_field($table, $field);
1505 upgrade_main_savepoint($result, 2007073103);
1508 if ($result && $oldversion < 2007073105) {
1510 /// Define field description to be added to grade_outcomes
1511 $table = new XMLDBTable('grade_outcomes');
1512 $field = new XMLDBField('description');
1513 if (!field_exists($table, $field)) {
1514 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, 'scaleid');
1515 /// Launch add field description
1516 $result = $result && add_field($table, $field);
1519 $table = new XMLDBTable('grade_outcomes_history');
1520 $field = new XMLDBField('description');
1521 if (!field_exists($table, $field)) {
1522 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, 'scaleid');
1523 /// Launch add field description
1524 $result = $result && add_field($table, $field);
1527 upgrade_main_savepoint($result, 2007073105);
1530 // adding unique contraint on (courseid,shortname) of an outcome
1531 if ($result && $oldversion < 2007080100) {
1533 /// Define key courseid-shortname (unique) to be added to grade_outcomes
1534 $table = new XMLDBTable('grade_outcomes');
1535 $key = new XMLDBKey('courseid-shortname');
1536 $key->setAttributes(XMLDB_KEY_UNIQUE, array('courseid', 'shortname'));
1538 /// Launch add key courseid-shortname
1539 $result = $result && add_key($table, $key);
1541 upgrade_main_savepoint($result, 2007080100);
1544 /// originally there was supportname and supportemail upgrade code - this is handled in upgradesettings.php instead
1546 if ($result && $oldversion < 2007080202) {
1548 /// Define index tagiditem (not unique) to be dropped form tag_instance
1549 $table = new XMLDBTable('tag_instance');
1550 $index = new XMLDBIndex('tagiditem');
1551 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('tagid', 'itemtype', 'itemid'));
1553 /// Launch drop index tagiditem
1554 drop_index($table, $index);
1556 /// Define index tagiditem (unique) to be added to tag_instance
1557 $table = new XMLDBTable('tag_instance');
1558 $index = new XMLDBIndex('tagiditem');
1559 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('tagid', 'itemtype', 'itemid'));
1561 /// Launch add index tagiditem
1562 $result = $result && add_index($table, $index);
1564 upgrade_main_savepoint($result, 2007080202);
1567 if ($result && $oldversion < 2007080300) {
1569 /// Define field aggregateoutcomes to be added to grade_categories
1570 $table = new XMLDBTable('grade_categories');
1571 $field = new XMLDBField('aggregateoutcomes');
1572 if (!field_exists($table, $field)) {
1573 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'droplow');
1575 /// Launch add field aggregateoutcomes
1576 $result = $result && add_field($table, $field);
1579 /// Define field aggregateoutcomes to be added to grade_categories
1580 $table = new XMLDBTable('grade_categories_history');
1581 $field = new XMLDBField('aggregateoutcomes');
1582 if (!field_exists($table, $field)) {
1583 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'droplow');
1585 /// Launch add field aggregateoutcomes
1586 $result = $result && add_field($table, $field);
1589 upgrade_main_savepoint($result, 2007080300);
1592 if ($result && $oldversion < 2007080800) { /// Normalize course->shortname MDL-10026
1594 /// Changing precision of field shortname on table course to (100)
1595 $table = new XMLDBTable('course');
1596 $field = new XMLDBField('shortname');
1597 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null, 'fullname');
1599 /// Launch change of precision for field shortname
1600 $result = $result && change_field_precision($table, $field);
1602 upgrade_main_savepoint($result, 2007080800);
1605 if ($result && $oldversion < 2007080900) {
1606 /// Add context.path & index
1607 $table = new XMLDBTable('context');
1608 $field = new XMLDBField('path');
1609 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'instanceid');
1610 $result = $result && add_field($table, $field);
1611 $table = new XMLDBTable('context');
1612 $index = new XMLDBIndex('path');
1613 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('path'));
1614 $result = $result && add_index($table, $index);
1616 /// Add context.depth
1617 $table = new XMLDBTable('context');
1618 $field = new XMLDBField('depth');
1619 $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'path');
1620 $result = $result && add_field($table, $field);
1622 /// make sure the system context has proper data
1623 get_system_context(false);
1625 upgrade_main_savepoint($result, 2007080900);
1628 if ($result && $oldversion < 2007080903) {
1629 /// Define index
1630 $table = new XMLDBTable('grade_grades');
1631 $index = new XMLDBIndex('locked-locktime');
1632 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('locked', 'locktime'));
1634 if (!index_exists($table, $index)) {
1635 /// Launch add index
1636 $result = $result && add_index($table, $index);
1639 /// Define index
1640 $table = new XMLDBTable('grade_items');
1641 $index = new XMLDBIndex('locked-locktime');
1642 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('locked', 'locktime'));
1644 if (!index_exists($table, $index)) {
1645 /// Launch add index
1646 $result = $result && add_index($table, $index);
1649 /// Define index itemtype-needsupdate (not unique) to be added to grade_items
1650 $table = new XMLDBTable('grade_items');
1651 $index = new XMLDBIndex('itemtype-needsupdate');
1652 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('itemtype', 'needsupdate'));
1653 if (!index_exists($table, $index)) {
1654 /// Launch add index itemtype-needsupdate
1655 $result = $result && add_index($table, $index);
1658 /// Define index
1659 $table = new XMLDBTable('grade_items');
1660 $index = new XMLDBIndex('gradetype');
1661 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('gradetype'));
1663 if (!index_exists($table, $index)) {
1664 /// Launch add index
1665 $result = $result && add_index($table, $index);
1668 upgrade_main_savepoint($result, 2007080903);
1671 if ($result && $oldversion < 2007081000) {
1672 require_once($CFG->dirroot . '/question/upgrade.php');
1673 $result = $result && question_upgrade_context_etc();
1675 upgrade_main_savepoint($result, 2007081000);
1678 if ($result && $oldversion < 2007081302) {
1680 if (table_exists(new XMLDBTable('groups_groupings'))) {
1681 /// IF 'groups_groupings' table exists, this is for 1.8.* only.
1682 $result = $result && upgrade_18_groups();
1684 } else {
1685 /// ELSE, 1.7.*/1.6.*/1.5.* - create 'groupings' and 'groupings_groups' + rename password to enrolmentkey
1686 $result = $result && upgrade_17_groups();
1689 /// For both 1.8.* and 1.7.*/1.6.*..
1691 // delete not used fields
1692 $table = new XMLDBTable('groups');
1693 $field = new XMLDBField('theme');
1694 if (field_exists($table, $field)) {
1695 drop_field($table, $field);
1697 $table = new XMLDBTable('groups');
1698 $field = new XMLDBField('lang');
1699 if (field_exists($table, $field)) {
1700 drop_field($table, $field);
1703 /// Add groupingid field/f.key to 'course' table.
1704 $table = new XMLDBTable('course');
1705 $field = new XMLDBField('defaultgroupingid');
1706 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='groupmodeforce');
1707 $result = $result && add_field($table, $field);
1710 /// Add grouping ID, grouponly field/f.key to 'course_modules' table.
1711 $table = new XMLDBTable('course_modules');
1712 $field = new XMLDBField('groupingid');
1713 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='groupmode');
1714 $result = $result && add_field($table, $field);
1716 $table = new XMLDBTable('course_modules');
1717 $field = new XMLDBField('groupmembersonly');
1718 $field->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='groupingid');
1719 $result = $result && add_field($table, $field);
1721 $table = new XMLDBTable('course_modules');
1722 $key = new XMLDBKey('groupingid');
1723 $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupingid'), 'groupings', array('id'));
1724 $result = $result && add_key($table, $key);
1726 upgrade_main_savepoint($result, 2007081302);
1729 if ($result && $oldversion < 2007082300) {
1731 /// Define field ordering to be added to tag_instance table
1732 $table = new XMLDBTable('tag_instance');
1733 $field = new XMLDBField('ordering');
1735 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'itemid');
1737 /// Launch add field rawname
1738 $result = $result && add_field($table, $field);
1740 upgrade_main_savepoint($result, 2007082300);
1743 if ($result && $oldversion < 2007082700) {
1745 /// Define field timemodified to be added to tag_instance
1746 $table = new XMLDBTable('tag_instance');
1747 $field = new XMLDBField('timemodified');
1748 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'ordering');
1750 /// Launch add field timemodified
1751 $result = $result && add_field($table, $field);
1753 upgrade_main_savepoint($result, 2007082700);
1756 /// migrate all tags table to tag - this code MUST use SQL only,
1757 /// because if the db structure changes the library functions will fail in future
1758 if ($result && $oldversion < 2007082701) {
1759 $tagrefs = array(); // $tagrefs[$oldtagid] = $newtagid
1760 if ($rs = get_recordset('tags')) {
1761 $db->debug = false;
1762 while ($oldtag = rs_fetch_next_record($rs)) {
1763 $raw_normalized = clean_param($oldtag->text, PARAM_TAG);
1764 $normalized = moodle_strtolower($raw_normalized);
1765 // if this tag does not exist in tag table yet
1766 if (!$newtag = get_record('tag', 'name', $normalized, '', '', '', '', 'id')) {
1767 $itag = new object();
1768 $itag->name = $normalized;
1769 $itag->rawname = $raw_normalized;
1770 $itag->userid = $oldtag->userid;
1771 $itag->timemodified = time();
1772 $itag->descriptionformat = 0; // default format
1773 if ($oldtag->type == 'official') {
1774 $itag->tagtype = 'official';
1775 } else {
1776 $itag->tagtype = 'default';
1779 if ($idx = insert_record('tag', $itag)) {
1780 $tagrefs[$oldtag->id] = $idx;
1782 // if this tag is already used by tag table
1783 } else {
1784 $tagrefs[$oldtag->id] = $newtag->id;
1787 $db->debug = true;
1788 rs_close($rs);
1791 // fetch all the tag instances and migrate them as well
1792 if ($rs = get_recordset('blog_tag_instance')) {
1793 $db->debug = false;
1794 while ($blogtag = rs_fetch_next_record($rs)) {
1795 if (array_key_exists($blogtag->tagid, $tagrefs)) {
1796 $tag_instance = new object();
1797 $tag_instance->tagid = $tagrefs[$blogtag->tagid];
1798 $tag_instance->itemtype = 'blog';
1799 $tag_instance->itemid = $blogtag->entryid;
1800 $tag_instance->ordering = 1; // does not matter much, because originally there was no ordering in blogs
1801 $tag_instance->timemodified = time();
1802 insert_record('tag_instance', $tag_instance);
1805 $db->debug = true;
1806 rs_close($rs);
1809 unset($tagrefs); // release memory
1811 $table = new XMLDBTable('tags');
1812 drop_table($table);
1813 $table = new XMLDBTable('blog_tag_instance');
1814 drop_table($table);
1816 upgrade_main_savepoint($result, 2007082701);
1819 /// MDL-11015, MDL-11016
1820 if ($result && $oldversion < 2007082800) {
1822 /// Changing type of field userid on table tag to int
1823 $table = new XMLDBTable('tag');
1824 $field = new XMLDBField('userid');
1825 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'id');
1827 /// Launch change of type for field userid
1828 $result = $result && change_field_type($table, $field);
1830 /// Changing type of field descriptionformat on table tag to int
1831 $table = new XMLDBTable('tag');
1832 $field = new XMLDBField('descriptionformat');
1833 $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'description');
1835 /// Launch change of type for field descriptionformat
1836 $result = $result && change_field_type($table, $field);
1838 /// Define key userid (foreign) to be added to tag
1839 $table = new XMLDBTable('tag');
1840 $key = new XMLDBKey('userid');
1841 $key->setAttributes(XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1843 /// Launch add key userid
1844 $result = $result && add_key($table, $key);
1846 /// Define index tagiditem (unique) to be dropped form tag_instance
1847 $table = new XMLDBTable('tag_instance');
1848 $index = new XMLDBIndex('tagiditem');
1849 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('tagid', 'itemtype', 'itemid'));
1851 /// Launch drop index tagiditem
1852 $result = $result && drop_index($table, $index);
1854 /// Changing type of field tagid on table tag_instance to int
1855 $table = new XMLDBTable('tag_instance');
1856 $field = new XMLDBField('tagid');
1857 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'id');
1859 /// Launch change of type for field tagid
1860 $result = $result && change_field_type($table, $field);
1862 /// Define key tagid (foreign) to be added to tag_instance
1863 $table = new XMLDBTable('tag_instance');
1864 $key = new XMLDBKey('tagid');
1865 $key->setAttributes(XMLDB_KEY_FOREIGN, array('tagid'), 'tag', array('id'));
1867 /// Launch add key tagid
1868 $result = $result && add_key($table, $key);
1870 /// Changing sign of field itemid on table tag_instance to unsigned
1871 $table = new XMLDBTable('tag_instance');
1872 $field = new XMLDBField('itemid');
1873 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'itemtype');
1875 /// Launch change of sign for field itemid
1876 $result = $result && change_field_unsigned($table, $field);
1878 /// Changing sign of field ordering on table tag_instance to unsigned
1879 $table = new XMLDBTable('tag_instance');
1880 $field = new XMLDBField('ordering');
1881 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'itemid');
1883 /// Launch change of sign for field ordering
1884 $result = $result && change_field_unsigned($table, $field);
1886 /// Define index itemtype-itemid-tagid (unique) to be added to tag_instance
1887 $table = new XMLDBTable('tag_instance');
1888 $index = new XMLDBIndex('itemtype-itemid-tagid');
1889 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
1891 /// Launch add index itemtype-itemid-tagid
1892 $result = $result && add_index($table, $index);
1894 /// Define index tagid (unique) to be dropped form tag_correlation
1895 $table = new XMLDBTable('tag_correlation');
1896 $index = new XMLDBIndex('tagid');
1897 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('tagid'));
1899 /// Launch drop index tagid
1900 $result = $result && drop_index($table, $index);
1902 /// Changing type of field tagid on table tag_correlation to int
1903 $table = new XMLDBTable('tag_correlation');
1904 $field = new XMLDBField('tagid');
1905 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'id');
1907 /// Launch change of type for field tagid
1908 $result = $result && change_field_type($table, $field);
1911 /// Define key tagid (foreign) to be added to tag_correlation
1912 $table = new XMLDBTable('tag_correlation');
1913 $key = new XMLDBKey('tagid');
1914 $key->setAttributes(XMLDB_KEY_FOREIGN, array('tagid'), 'tag', array('id'));
1916 /// Launch add key tagid
1917 $result = $result && add_key($table, $key);
1919 upgrade_main_savepoint($result, 2007082800);
1923 if ($result && $oldversion < 2007082801) {
1925 /// Define table user_private_key to be created
1926 $table = new XMLDBTable('user_private_key');
1928 /// Adding fields to table user_private_key
1929 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1930 $table->addFieldInfo('script', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null, null, null);
1931 $table->addFieldInfo('value', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null, null, null);
1932 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1933 $table->addFieldInfo('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1934 $table->addFieldInfo('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1935 $table->addFieldInfo('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1936 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1938 /// Adding keys to table user_private_key
1939 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1940 $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1942 /// Adding indexes to table user_private_key
1943 $table->addIndexInfo('script-value', XMLDB_INDEX_NOTUNIQUE, array('script', 'value'));
1945 /// Launch create table for user_private_key
1946 $result = $result && create_table($table);
1948 upgrade_main_savepoint($result, 2007082801);
1951 /// Going to modify the applicationid from int(1) to int(10). Dropping and
1952 /// re-creating the associated keys/indexes is mandatory to be cross-db. MDL-11042
1953 if ($result && $oldversion < 2007082803) {
1955 /// Define key applicationid (foreign) to be dropped form mnet_host
1956 $table = new XMLDBTable('mnet_host');
1957 $key = new XMLDBKey('applicationid');
1958 $key->setAttributes(XMLDB_KEY_FOREIGN, array('applicationid'), 'mnet_application', array('id'));
1960 /// Launch drop key applicationid
1961 $result = $result && drop_key($table, $key);
1963 /// Changing type of field applicationid on table mnet_host to int
1964 $field = new XMLDBField('applicationid');
1965 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'last_log_id');
1967 /// Launch change of type for field applicationid
1968 $result = $result && change_field_type($table, $field);
1970 /// Define key applicationid (foreign) to be added to mnet_host
1971 $key = new XMLDBKey('applicationid');
1972 $key->setAttributes(XMLDB_KEY_FOREIGN, array('applicationid'), 'mnet_application', array('id'));
1974 /// Launch add key applicationid
1975 $result = $result && add_key($table, $key);
1977 upgrade_main_savepoint($result, 2007082803);
1980 if ($result && $oldversion < 2007090503) {
1981 /// Define field aggregatesubcats to be added to grade_categories
1982 $table = new XMLDBTable('grade_categories');
1983 $field = new XMLDBField('aggregatesubcats');
1984 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'aggregateoutcomes');
1986 if (!field_exists($table, $field)) {
1987 /// Launch add field aggregateonlygraded
1988 $result = $result && add_field($table, $field);
1991 /// Define field aggregateonlygraded to be added to grade_categories
1992 $table = new XMLDBTable('grade_categories');
1993 $field = new XMLDBField('aggregateonlygraded');
1994 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'droplow');
1996 if (!field_exists($table, $field)) {
1997 /// Launch add field aggregateonlygraded
1998 $result = $result && add_field($table, $field);
2001 /// Define field aggregatesubcats to be added to grade_categories_history
2002 $table = new XMLDBTable('grade_categories_history');
2003 $field = new XMLDBField('aggregatesubcats');
2004 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'aggregateoutcomes');
2006 if (!field_exists($table, $field)) {
2007 /// Launch add field aggregateonlygraded
2008 $result = $result && add_field($table, $field);
2011 /// Define field aggregateonlygraded to be added to grade_categories_history
2012 $table = new XMLDBTable('grade_categories_history');
2013 $field = new XMLDBField('aggregateonlygraded');
2014 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'droplow');
2016 if (!field_exists($table, $field)) {
2017 /// Launch add field aggregateonlygraded
2018 $result = $result && add_field($table, $field);
2021 /// upgrade path in grade_categrories table - now using slash on both ends
2022 $concat = sql_concat('path', "'/'");
2023 $sql = "UPDATE {$CFG->prefix}grade_categories SET path = $concat WHERE path NOT LIKE '/%/'";
2024 execute_sql($sql, true);
2026 /// convert old aggregation constants if needed
2027 for ($i=0; $i<=12; $i=$i+2) {
2028 $j = $i+1;
2029 $sql = "UPDATE {$CFG->prefix}grade_categories SET aggregation = $i, aggregateonlygraded = 1 WHERE aggregation = $j";
2030 execute_sql($sql, true);
2033 upgrade_main_savepoint($result, 2007090503);
2036 /// To have UNIQUE indexes over NULLable columns isn't cross-db at all
2037 /// so we create a non unique index and programatically enforce uniqueness
2038 if ($result && $oldversion < 2007090600) {
2040 /// Define index idnumber (unique) to be dropped form course_modules
2041 $table = new XMLDBTable('course_modules');
2042 $index = new XMLDBIndex('idnumber');
2043 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('idnumber'));
2045 /// Launch drop index idnumber
2046 $result = $result && drop_index($table, $index);
2048 /// Define index idnumber-course (not unique) to be added to course_modules
2049 $table = new XMLDBTable('course_modules');
2050 $index = new XMLDBIndex('idnumber-course');
2051 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('idnumber', 'course'));
2053 /// Launch add index idnumber-course
2054 $result = $result && add_index($table, $index);
2056 /// Define index idnumber-courseid (not unique) to be added to grade_items
2057 $table = new XMLDBTable('grade_items');
2058 $index = new XMLDBIndex('idnumber-courseid');
2059 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('idnumber', 'courseid'));
2061 /// Launch add index idnumber-courseid
2062 $result = $result && add_index($table, $index);
2064 upgrade_main_savepoint($result, 2007090600);
2067 /// Create the permanent context_temp table to be used by build_context_path()
2068 if ($result && $oldversion < 2007092001) {
2070 /// Define table context_temp to be created
2071 $table = new XMLDBTable('context_temp');
2073 /// Adding fields to table context_temp
2074 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2075 $table->addFieldInfo('path', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
2076 $table->addFieldInfo('depth', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2078 /// Adding keys to table context_temp
2079 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
2081 /// Launch create table for context_temp
2082 $result = $result && create_table($table);
2084 /// make sure category depths, parents and paths are ok, categories from 1.5 may not be properly initialized (MDL-12585)
2085 upgrade_fix_category_depths();
2087 /// Recalculate depths, paths and so on
2088 if (!empty($CFG->rolesactive)) {
2089 cleanup_contexts();
2090 // make sure all course, category and user contexts exist - we need it for grade letter upgrade, etc.
2091 create_contexts(CONTEXT_COURSE, false, true);
2092 create_contexts(CONTEXT_USER, false, true);
2093 // we need all contexts path/depths filled properly
2094 build_context_path(true, true);
2095 load_all_capabilities();
2097 } else {
2098 // upgrade from 1.6 - build all contexts
2099 create_contexts(null, true, true);
2102 upgrade_main_savepoint($result, 2007092001);
2106 * Merging of grade_grades_text back into grade_grades
2108 if ($result && $oldversion < 2007092002) {
2110 /// Define field feedback to be added to grade_grades
2111 $table = new XMLDBTable('grade_grades');
2112 $field = new XMLDBField('feedback');
2113 $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null, 'excluded');
2115 if (!field_exists($table, $field)) {
2116 /// Launch add field feedback
2117 $result = $result && add_field($table, $field);
2120 /// Define field feedbackformat to be added to grade_grades
2121 $table = new XMLDBTable('grade_grades');
2122 $field = new XMLDBField('feedbackformat');
2123 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'feedback');
2125 if (!field_exists($table, $field)) {
2126 /// Launch add field feedbackformat
2127 $result = $result && add_field($table, $field);
2130 /// Define field information to be added to grade_grades
2131 $table = new XMLDBTable('grade_grades');
2132 $field = new XMLDBField('information');
2133 $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null, 'feedbackformat');
2135 if (!field_exists($table, $field)) {
2136 /// Launch add field information
2137 $result = $result && add_field($table, $field);
2140 /// Define field informationformat to be added to grade_grades
2141 $table = new XMLDBTable('grade_grades');
2142 $field = new XMLDBField('informationformat');
2143 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'information');
2145 if (!field_exists($table, $field)) {
2146 /// Launch add field informationformat
2147 $result = $result && add_field($table, $field);
2150 /// Define field feedback to be added to grade_grades_history
2151 $table = new XMLDBTable('grade_grades_history');
2152 $field = new XMLDBField('feedback');
2153 $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null, 'excluded');
2155 if (!field_exists($table, $field)) {
2156 /// Launch add field feedback
2157 $result = $result && add_field($table, $field);
2160 /// Define field feedbackformat to be added to grade_grades_history
2161 $table = new XMLDBTable('grade_grades_history');
2162 $field = new XMLDBField('feedbackformat');
2163 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'feedback');
2165 if (!field_exists($table, $field)) {
2166 /// Launch add field feedbackformat
2167 $result = $result && add_field($table, $field);
2170 /// Define field information to be added to grade_grades_history
2171 $table = new XMLDBTable('grade_grades_history');
2172 $field = new XMLDBField('information');
2173 $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null, 'feedbackformat');
2175 if (!field_exists($table, $field)) {
2176 /// Launch add field information
2177 $result = $result && add_field($table, $field);
2180 /// Define field informationformat to be added to grade_grades_history
2181 $table = new XMLDBTable('grade_grades_history');
2182 $field = new XMLDBField('informationformat');
2183 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'information');
2185 if (!field_exists($table, $field)) {
2186 /// Launch add field informationformat
2187 $result = $result && add_field($table, $field);
2190 $table = new XMLDBTable('grade_grades_text');
2191 if ($result and table_exists($table)) {
2192 //migrade existing data into grade_grades table - this is slow but works for all dbs,
2193 //it will be executed on development sites only
2194 $fields = array('feedback', 'information');
2195 foreach ($fields as $field) {
2196 $sql = "UPDATE {$CFG->prefix}grade_grades
2197 SET $field = (
2198 SELECT $field
2199 FROM {$CFG->prefix}grade_grades_text ggt
2200 WHERE ggt.gradeid = {$CFG->prefix}grade_grades.id)";
2201 $result = execute_sql($sql) && $result;
2203 $fields = array('feedbackformat', 'informationformat');
2204 foreach ($fields as $field) {
2205 $sql = "UPDATE {$CFG->prefix}grade_grades
2206 SET $field = COALESCE((
2207 SELECT $field
2208 FROM {$CFG->prefix}grade_grades_text ggt
2209 WHERE ggt.gradeid = {$CFG->prefix}grade_grades.id), 0)";
2210 $result = execute_sql($sql) && $result;
2213 if ($result) {
2214 $tables = array('grade_grades_text', 'grade_grades_text_history');
2216 foreach ($tables as $table) {
2217 $table = new XMLDBTable($table);
2218 if (table_exists($table)) {
2219 drop_table($table);
2225 upgrade_main_savepoint($result, 2007092002);
2228 if ($result && $oldversion < 2007092803) {
2230 /// Remove obsoleted unit tests tables - they will be recreated automatically
2231 $tables = array('grade_categories',
2232 'scale',
2233 'grade_items',
2234 'grade_calculations',
2235 'grade_grades',
2236 'grade_grades_raw',
2237 'grade_grades_final',
2238 'grade_grades_text',
2239 'grade_outcomes',
2240 'grade_outcomes_courses');
2242 foreach ($tables as $tablename) {
2243 $table = new XMLDBTable('unittest_'.$tablename);
2244 if (table_exists($table)) {
2245 drop_table($table);
2247 $table = new XMLDBTable('unittest_'.$tablename.'_history');
2248 if (table_exists($table)) {
2249 drop_table($table);
2253 /// Define field display to be added to grade_items
2254 $table = new XMLDBTable('grade_items');
2255 $field = new XMLDBField('display');
2256 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0', 'sortorder');
2258 /// Launch add field display
2259 if (!field_exists($table, $field)) {
2260 $result = $result && add_field($table, $field);
2261 } else {
2262 $result = $result && change_field_default($table, $field);
2265 /// Define field display to be added to grade_items_history
2266 $table = new XMLDBTable('grade_items_history');
2267 $field = new XMLDBField('display');
2268 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0', 'sortorder');
2270 /// Launch add field display
2271 if (!field_exists($table, $field)) {
2272 $result = $result && add_field($table, $field);
2276 /// Define field decimals to be added to grade_items
2277 $table = new XMLDBTable('grade_items');
2278 $field = new XMLDBField('decimals');
2279 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null, 'display');
2281 /// Launch add field decimals
2282 if (!field_exists($table, $field)) {
2283 $result = $result && add_field($table, $field);
2284 } else {
2285 $result = $result && change_field_default($table, $field);
2286 $result = $result && change_field_notnull($table, $field);
2289 /// Define field decimals to be added to grade_items_history
2290 $table = new XMLDBTable('grade_items_history');
2291 $field = new XMLDBField('decimals');
2292 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null, 'display');
2294 /// Launch add field decimals
2295 if (!field_exists($table, $field)) {
2296 $result = $result && add_field($table, $field);
2300 /// fix incorrect -1 default for grade_item->display
2301 execute_sql("UPDATE {$CFG->prefix}grade_items SET display=0 WHERE display=-1");
2303 upgrade_main_savepoint($result, 2007092803);
2306 /// migrade grade letters - we can not do this in normal grades upgrade becuase we need all course contexts
2307 if ($result && $oldversion < 2007092806) {
2309 $result = upgrade_18_letters();
2311 /// Define index contextidlowerboundary (not unique) to be added to grade_letters
2312 $table = new XMLDBTable('grade_letters');
2313 $index = new XMLDBIndex('contextid-lowerboundary');
2314 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
2316 /// Launch add index contextidlowerboundary
2317 if (!index_exists($table, $index)) {
2318 $result = $result && add_index($table, $index);
2321 upgrade_main_savepoint($result, 2007092806);
2324 if ($result && $oldversion < 2007100100) {
2326 /// Define table cache_flags to be created
2327 $table = new XMLDBTable('cache_flags');
2329 /// Adding fields to table cache_flags
2330 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
2331 $table->addFieldInfo('flagtype', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
2332 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
2333 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
2334 $table->addFieldInfo('value', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null);
2335 $table->addFieldInfo('expiry', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2337 /// Adding keys to table cache_flags
2338 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
2341 * Note: mysql can not create indexes on text fields larger than 333 chars!
2344 /// Adding indexes to table cache_flags
2345 $table->addIndexInfo('flagtype', XMLDB_INDEX_NOTUNIQUE, array('flagtype'));
2346 $table->addIndexInfo('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
2348 /// Launch create table for cache_flags
2349 if (!table_exists($table)) {
2350 $result = $result && create_table($table);
2353 upgrade_main_savepoint($result, 2007100100);
2357 if ($oldversion < 2007100300) {
2358 /// MNET stuff for roaming theme
2359 /// Define field force_theme to be added to mnet_host
2360 $table = new XMLDBTable('mnet_host');
2361 $field = new XMLDBField('force_theme');
2362 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'last_log_id');
2364 /// Launch add field force_theme
2365 $result = $result && add_field($table, $field);
2367 /// Define field theme to be added to mnet_host
2368 $table = new XMLDBTable('mnet_host');
2369 $field = new XMLDBField('theme');
2370 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null, 'force_theme');
2372 /// Launch add field theme
2373 $result = $result && add_field($table, $field);
2375 upgrade_main_savepoint($result, 2007100300);
2378 if ($result && $oldversion < 2007100301) {
2380 /// Define table cache_flags to be created
2381 $table = new XMLDBTable('cache_flags');
2382 $index = new XMLDBIndex('typename');
2383 if (index_exists($table, $index)) {
2384 $result = $result && drop_index($table, $index);
2387 $table = new XMLDBTable('cache_flags');
2388 $index = new XMLDBIndex('flagtype');
2389 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('flagtype'));
2390 if (!index_exists($table, $index)) {
2391 $result = $result && add_index($table, $index);
2394 $table = new XMLDBTable('cache_flags');
2395 $index = new XMLDBIndex('name');
2396 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('name'));
2397 if (!index_exists($table, $index)) {
2398 $result = $result && add_index($table, $index);
2401 upgrade_main_savepoint($result, 2007100301);
2404 if ($result && $oldversion < 2007100303) {
2406 /// Changing nullability of field summary on table course to null
2407 $table = new XMLDBTable('course');
2408 $field = new XMLDBField('summary');
2409 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, 'idnumber');
2411 /// Launch change of nullability for field summary
2412 $result = $result && change_field_notnull($table, $field);
2414 upgrade_main_savepoint($result, 2007100303);
2417 if ($result && $oldversion < 2007100500) {
2418 /// for dev sites - it is ok to do this repeatedly
2420 /// Changing nullability of field path on table context to null
2421 $table = new XMLDBTable('context');
2422 $field = new XMLDBField('path');
2423 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'instanceid');
2425 /// Launch change of nullability for field path
2426 $result = $result && change_field_notnull($table, $field);
2428 upgrade_main_savepoint($result, 2007100500);
2431 if ($result && $oldversion < 2007100700) {
2433 /// first drop existing tables - we do not need any data from there
2434 $table = new XMLDBTable('grade_import_values');
2435 if (table_exists($table)) {
2436 drop_table($table);
2439 $table = new XMLDBTable('grade_import_newitem');
2440 if (table_exists($table)) {
2441 drop_table($table);
2444 /// Define table grade_import_newitem to be created
2445 $table = new XMLDBTable('grade_import_newitem');
2447 /// Adding fields to table grade_import_newitem
2448 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
2449 $table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
2450 $table->addFieldInfo('importcode', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2451 $table->addFieldInfo('importer', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2453 /// Adding keys to table grade_import_newitem
2454 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
2455 $table->addKeyInfo('importer', XMLDB_KEY_FOREIGN, array('importer'), 'user', array('id'));
2457 /// Launch create table for grade_import_newitem
2458 $result = $result && create_table($table);
2461 /// Define table grade_import_values to be created
2462 $table = new XMLDBTable('grade_import_values');
2464 /// Adding fields to table grade_import_values
2465 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
2466 $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
2467 $table->addFieldInfo('newgradeitem', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
2468 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2469 $table->addFieldInfo('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
2470 $table->addFieldInfo('feedback', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
2471 $table->addFieldInfo('importcode', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2472 $table->addFieldInfo('importer', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
2474 /// Adding keys to table grade_import_values
2475 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
2476 $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
2477 $table->addKeyInfo('newgradeitem', XMLDB_KEY_FOREIGN, array('newgradeitem'), 'grade_import_newitem', array('id'));
2478 $table->addKeyInfo('importer', XMLDB_KEY_FOREIGN, array('importer'), 'user', array('id'));
2480 /// Launch create table for grade_import_values
2481 $result = $result && create_table($table);
2483 upgrade_main_savepoint($result, 2007100700);
2486 /// dropping context_rel table - not used anymore
2487 if ($result && $oldversion < 2007100800) {
2489 /// Define table context_rel to be dropped
2490 $table = new XMLDBTable('context_rel');
2492 /// Launch drop table for context_rel
2493 if (table_exists($table)) {
2494 drop_table($table);
2497 upgrade_main_savepoint($result, 2007100800);
2500 /// Truncate the text_cahe table and add new index
2501 if ($result && $oldversion < 2007100802) {
2503 /// Truncate the cache_text table
2504 execute_sql("TRUNCATE TABLE {$CFG->prefix}cache_text", true);
2506 /// Define index timemodified (not unique) to be added to cache_text
2507 $table = new XMLDBTable('cache_text');
2508 $index = new XMLDBIndex('timemodified');
2509 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
2511 /// Launch add index timemodified
2512 $result = $result && add_index($table, $index);
2514 upgrade_main_savepoint($result, 2007100802);
2517 /// newtable for gradebook settings per course
2518 if ($result && $oldversion < 2007100803) {
2520 /// Define table grade_settings to be created
2521 $table = new XMLDBTable('grade_settings');
2523 /// Adding fields to table grade_settings
2524 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
2525 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2526 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
2527 $table->addFieldInfo('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
2529 /// Adding keys to table grade_settings
2530 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
2531 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
2533 /// Adding indexes to table grade_settings
2534 $table->addIndexInfo('courseid-name', XMLDB_INDEX_UNIQUE, array('courseid', 'name'));
2536 /// Launch create table for grade_settings
2537 $result = $result && create_table($table);
2539 upgrade_main_savepoint($result, 2007100803);
2542 /// cleanup in user_lastaccess
2543 if ($result && $oldversion < 2007100902) {
2544 $sql = "DELETE
2545 FROM {$CFG->prefix}user_lastaccess
2546 WHERE NOT EXISTS (SELECT 'x'
2547 FROM {$CFG->prefix}course c
2548 WHERE c.id = {$CFG->prefix}user_lastaccess.courseid)";
2549 execute_sql($sql);
2551 upgrade_main_savepoint($result, 2007100902);
2554 /// drop old gradebook tables
2555 if ($result && $oldversion < 2007100903) {
2556 $tables = array('grade_category',
2557 'grade_item',
2558 'grade_letter',
2559 'grade_preferences',
2560 'grade_exceptions');
2562 foreach ($tables as $table) {
2563 $table = new XMLDBTable($table);
2564 if (table_exists($table)) {
2565 drop_table($table);
2569 upgrade_main_savepoint($result, 2007100903);
2572 if ($result && $oldversion < 2007101500 && !file_exists($CFG->dataroot . '/user')) {
2573 // Get list of users by browsing moodledata/user
2574 $oldusersdir = $CFG->dataroot . '/users';
2575 $folders = get_directory_list($oldusersdir, '', false, true, false);
2577 foreach ($folders as $userid) {
2578 $olddir = $oldusersdir . '/' . $userid;
2579 $files = get_directory_list($olddir);
2581 if (empty($files)) {
2582 continue;
2585 // Create new user directory
2586 if (!$newdir = make_user_directory($userid)) {
2587 // some weird directory - do not stop the upgrade, just ignore it
2588 continue;
2591 // Move contents of old directory to new one
2592 if (file_exists($olddir) && file_exists($newdir)) {
2593 foreach ($files as $file) {
2594 copy($olddir . '/' . $file, $newdir . '/' . $file);
2596 } else {
2597 notify("Could not move the contents of $olddir into $newdir!");
2598 $result = false;
2599 break;
2603 // Leave a README in old users directory
2604 $readmefilename = $oldusersdir . '/README.txt';
2605 if ($handle = fopen($readmefilename, 'w+b')) {
2606 if (!fwrite($handle, get_string('olduserdirectory'))) {
2607 // Could not write to the readme file. No cause for huge concern
2608 notify("Could not write to the README.txt file in $readmefilename.");
2610 fclose($handle);
2611 } else {
2612 // Could not create the readme file. No cause for huge concern
2613 notify("Could not create the README.txt file in $readmefilename.");
2617 if ($result && $oldversion < 2007101502) {
2619 /// try to remove duplicate entries
2621 $SQL = "SELECT userid, itemid, COUNT(*)
2622 FROM {$CFG->prefix}grade_grades
2623 GROUP BY userid, itemid
2624 HAVING COUNT( * ) >1";
2625 // duplicates found
2627 if ($rs = get_recordset_sql($SQL)) {
2628 if ($rs && $rs->RecordCount() > 0) {
2629 while ($dup = rs_fetch_next_record($rs)) {
2630 if ($thisdups = get_records_sql("SELECT id FROM {$CFG->prefix}grade_grades
2631 WHERE itemid = $dup->itemid AND userid = $dup->userid
2632 ORDER BY timemodified DESC")) {
2634 $processed = 0; // keep the first one
2635 foreach ($thisdups as $thisdup) {
2636 if ($processed) {
2637 // remove the duplicates
2638 delete_records('grade_grades', 'id', $thisdup->id);
2640 $processed++;
2644 rs_close($rs);
2648 /// Define key userid-itemid (unique) to be added to grade_grades
2649 $table = new XMLDBTable('grade_grades');
2650 $key = new XMLDBKey('userid-itemid');
2651 $key->setAttributes(XMLDB_KEY_UNIQUE, array('userid', 'itemid'));
2653 /// Launch add key userid-itemid
2654 $result = $result && add_key($table, $key);
2656 /// Main savepoint reached
2657 upgrade_main_savepoint($result, 2007101502);
2660 if ($result && $oldversion < 2007101505) {
2662 /// Changing precision of field dst_time on table timezone to (6)
2663 $table = new XMLDBTable('timezone');
2664 $field = new XMLDBField('dst_time');
2665 $field->setAttributes(XMLDB_TYPE_CHAR, '6', null, XMLDB_NOTNULL, null, null, null, '00:00', 'dst_skipweeks');
2667 /// Launch change of precision for field dst_time
2668 $result = $result && change_field_precision($table, $field);
2670 /// Changing precision of field std_time on table timezone to (6)
2671 $table = new XMLDBTable('timezone');
2672 $field = new XMLDBField('std_time');
2673 $field->setAttributes(XMLDB_TYPE_CHAR, '6', null, XMLDB_NOTNULL, null, null, null, '00:00', 'std_skipweeks');
2675 /// Launch change of precision for field std_time
2676 $result = $result && change_field_precision($table, $field);
2678 /// Main savepoint reached
2679 upgrade_main_savepoint($result, 2007101505);
2682 if ($result && $oldversion < 2007101506) {
2684 /// CONTEXT_PERSONAL was never implemented - removing
2685 $sql = "DELETE
2686 FROM {$CFG->prefix}context
2687 WHERE contextlevel=20";
2689 execute_sql($sql);
2691 /// Main savepoint reached
2692 upgrade_main_savepoint($result, 2007101506);
2695 if ($result && $oldversion < 2007101507) {
2696 $db->debug = false;
2697 require_once($CFG->dirroot.'/course/lib.php');
2698 notify('Started rebuilding of course cache...', 'notifysuccess');
2699 rebuild_course_cache(); // Rebuild course cache - new group related fields there
2700 notify('...finished rebuilding of course cache.', 'notifysuccess');
2701 $db->debug = true;
2702 /// Main savepoint reached
2703 upgrade_main_savepoint($result, 2007101507);
2706 if ($result && $oldversion < 2007101508) {
2707 $db->debug = false;
2708 notify('Updating country list according to recent official ISO listing...', 'notifysuccess');
2709 // re-assign users to valid countries
2710 set_field('user', 'country', 'CD', 'country', 'ZR'); // Zaire is now Congo Democratique
2711 set_field('user', 'country', 'TL', 'country', 'TP'); // Timor has changed
2712 set_field('user', 'country', 'FR', 'country', 'FX'); // France metropolitaine doesn't exist
2713 set_field('user', 'country', 'RS', 'country', 'KO'); // Kosovo is part of Serbia, "under the auspices of the United Nations, pursuant to UN Security Council Resolution 1244 of 10 June 1999."
2714 set_field('user', 'country', 'GB', 'country', 'WA'); // Wales is part of UK (ie Great Britain)
2715 set_field('user', 'country', 'RS', 'country', 'CS'); // Re-assign Serbia-Montenegro to Serbia. This is arbitrary, but there is no way to make an automatic decision on this.
2716 notify('...update complete. Remember to update the language pack to get the most recent country names defitions and codes. This is specialy important for sites with users from Congo (now CD), Timor (now TL), Kosovo (now RS), Wales (now GB), Serbia (RS) and Montenegro (ME). Users based in Montenegro (ME) will need to manually update their profile.', 'notifysuccess');
2717 $db->debug = true;
2718 upgrade_main_savepoint($result, 2007101508);
2721 if ($result && $oldversion < 2007101508.01) {
2722 // add forgotten table
2723 /// Define table scale_history to be created
2724 $table = new XMLDBTable('scale_history');
2726 /// Adding fields to table scale_history
2727 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
2728 $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
2729 $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2730 $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
2731 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
2732 $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
2733 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
2734 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
2735 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
2736 $table->addFieldInfo('scale', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
2737 $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
2739 /// Adding keys to table scale_history
2740 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
2741 $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'scale', array('id'));
2742 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
2743 $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id'));
2745 /// Adding indexes to table scale_history
2746 $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
2748 if ($result and !table_exists($table)) {
2749 /// Launch create table for scale_history
2750 $result = $result && create_table($table);
2753 /// Main savepoint reached
2754 upgrade_main_savepoint($result, 2007101508.01);
2758 if ($result && $oldversion < 2007101508.02) {
2759 // upgade totals, no big deal if it fails
2760 require_once($CFG->libdir.'/statslib.php');
2761 stats_upgrade_totals();
2763 if (isset($CFG->loglifetime) and $CFG->loglifetime == 30) {
2764 set_config('loglifetime', 35); // we need more than 31 days for monthly stats!
2767 notify('Upgrading log table indexes, this may take a long time, please be patient.', 'notifysuccess');
2769 /// Define index time-course-module-action (not unique) to be dropped form log
2770 $table = new XMLDBTable('log');
2771 $index = new XMLDBIndex('time-course-module-action');
2772 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('time', 'course', 'module', 'action'));
2774 /// Launch drop index time-course-module-action
2775 if (index_exists($table, $index)) {
2776 $result = drop_index($table, $index) && $result;
2779 /// Define index userid (not unique) to be dropped form log
2780 $table = new XMLDBTable('log');
2781 $index = new XMLDBIndex('userid');
2782 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('userid'));
2784 /// Launch drop index userid
2785 if (index_exists($table, $index)) {
2786 $result = drop_index($table, $index) && $result;
2789 /// Define index info (not unique) to be dropped form log
2790 $table = new XMLDBTable('log');
2791 $index = new XMLDBIndex('info');
2792 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('info'));
2794 /// Launch drop index info
2795 if (index_exists($table, $index)) {
2796 $result = drop_index($table, $index) && $result;
2799 /// Define index time (not unique) to be added to log
2800 $table = new XMLDBTable('log');
2801 $index = new XMLDBIndex('time');
2802 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('time'));
2804 /// Launch add index time
2805 if (!index_exists($table, $index)) {
2806 $result = add_index($table, $index) && $result;
2809 /// Define index action (not unique) to be added to log
2810 $table = new XMLDBTable('log');
2811 $index = new XMLDBIndex('action');
2812 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('action'));
2814 /// Launch add index action
2815 if (!index_exists($table, $index)) {
2816 $result = add_index($table, $index) && $result;
2819 /// Main savepoint reached
2820 upgrade_main_savepoint($result, 2007101508.02);
2823 if ($result && $oldversion < 2007101508.03) {
2825 /// Define index course-userid (not unique) to be dropped form log
2826 $table = new XMLDBTable('log');
2827 $index = new XMLDBIndex('course-userid');
2828 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'userid'));
2830 /// Launch drop index course-userid
2831 if (index_exists($table, $index)) {
2832 $result = $result && drop_index($table, $index);
2835 /// Define index userid-course (not unique) to be added to log
2836 $table = new XMLDBTable('log');
2837 $index = new XMLDBIndex('userid-course');
2838 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('userid', 'course'));
2840 /// Launch add index userid-course
2841 if (!index_exists($table, $index)) {
2842 $result = $result && add_index($table, $index);
2845 /// Main savepoint reached
2846 upgrade_main_savepoint($result, 2007101508.03);
2849 if ($result && $oldversion < 2007101508.04) {
2850 set_field('tag_instance', 'itemtype', 'post', 'itemtype', 'blog');
2851 upgrade_main_savepoint($result, 2007101508.04);
2854 if ($result && $oldversion < 2007101508.05) {
2856 /// Define index cmid (not unique) to be added to log
2857 $table = new XMLDBTable('log');
2858 $index = new XMLDBIndex('cmid');
2859 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('cmid'));
2861 /// Launch add index cmid
2862 if (!index_exists($table, $index)) {
2863 $result = $result && add_index($table, $index);
2866 /// Main savepoint reached
2867 upgrade_main_savepoint($result, 2007101508.05);
2870 if ($result && $oldversion < 2007101508.06) {
2872 /// Define index groupid-courseid-visible-userid (not unique) to be added to event
2873 $table = new XMLDBTable('event');
2874 $index = new XMLDBIndex('groupid-courseid-visible-userid');
2875 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('groupid', 'courseid', 'visible', 'userid'));
2877 /// Launch add index groupid-courseid-visible-userid
2878 if (!index_exists($table, $index)) {
2879 $result = $result && add_index($table, $index);
2882 /// Main savepoint reached
2883 upgrade_main_savepoint($result, 2007101508.06);
2886 if ($result && $oldversion < 2007101508.07) {
2888 /// Define table webdav_locks to be created
2889 $table = new XMLDBTable('webdav_locks');
2891 /// Adding fields to table webdav_locks
2892 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
2893 $table->addFieldInfo('token', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
2894 $table->addFieldInfo('path', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
2895 $table->addFieldInfo('expiry', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
2896 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
2897 $table->addFieldInfo('recursive', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
2898 $table->addFieldInfo('exclusivelock', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
2899 $table->addFieldInfo('created', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
2900 $table->addFieldInfo('modified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
2901 $table->addFieldInfo('owner', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
2903 /// Adding keys to table webdav_locks
2904 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
2905 $table->addKeyInfo('token', XMLDB_KEY_UNIQUE, array('token'));
2907 /// Adding indexes to table webdav_locks
2908 $table->addIndexInfo('path', XMLDB_INDEX_NOTUNIQUE, array('path'));
2909 $table->addIndexInfo('expiry', XMLDB_INDEX_NOTUNIQUE, array('expiry'));
2911 /// Launch create table for webdav_locks
2912 $result = $result && create_table($table);
2914 /// Main savepoint reached
2915 upgrade_main_savepoint($result, 2007101508.07);
2918 if ($result && $oldversion < 2007101508.08) { // MDL-13676
2920 /// Define field name to be added to role_names
2921 $table = new XMLDBTable('role_names');
2922 $field = new XMLDBField('name');
2923 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'text');
2925 /// Launch add field name
2926 $result = $result && add_field($table, $field);
2928 /// Copy data from old field to new field
2929 $result = $result && execute_sql('UPDATE '.$CFG->prefix.'role_names SET name = text');
2931 /// Define field text to be dropped from role_names
2932 $table = new XMLDBTable('role_names');
2933 $field = new XMLDBField('text');
2935 /// Launch drop field text
2936 $result = $result && drop_field($table, $field);
2938 /// Main savepoint reached
2939 upgrade_main_savepoint($result, 2007101508.08);
2942 if ($result && $oldversion < 2007101509) {
2943 // force full regrading
2944 set_field('grade_items', 'needsupdate', 1, 'needsupdate', 0);
2947 if ($result && $oldversion < 2007101510) {
2948 /// Fix minor problem caused by MDL-5482.
2949 require_once($CFG->dirroot . '/question/upgrade.php');
2950 $result = $result && question_fix_random_question_parents();
2951 upgrade_main_savepoint($result, 2007101510);
2954 if ($result && $oldversion < 2007101511) {
2955 // if guest role used as default user role unset it and force admin to choose new setting
2956 if (!empty($CFG->defaultuserroleid)) {
2957 if ($role = get_record('role', 'id', $CFG->defaultuserroleid)) {
2958 if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
2959 if (isset($guestroles[$role->id])) {
2960 set_config('defaultuserroleid', null);
2961 notify('Guest role removed from "Default role for all users" setting, please select another role.', 'notifysuccess');
2964 } else {
2965 set_config('defaultuserroleid', null);
2970 if ($result && $oldversion < 2007101512) {
2971 notify('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
2973 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
2974 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
2975 execute_sql("UPDATE {$CFG->prefix}user SET idnumber = '' WHERE idnumber IS NULL", true);
2978 /// Define index idnumber (not unique) to be dropped form user
2979 $table = new XMLDBTable('user');
2980 $index = new XMLDBIndex('idnumber');
2981 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
2983 /// Launch drop index idnumber
2984 if (index_exists($table, $index)) {
2985 $result = $result && drop_index($table, $index);
2988 /// Changing precision of field idnumber on table user to (255)
2989 $table = new XMLDBTable('user');
2990 $field = new XMLDBField('idnumber');
2991 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'password');
2993 /// Launch change of precision for field idnumber
2994 $result = $result && change_field_precision($table, $field);
2996 /// Launch add index idnumber again
2997 $index = new XMLDBIndex('idnumber');
2998 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
2999 $result = $result && add_index($table, $index);
3001 /// Main savepoint reached
3002 upgrade_main_savepoint($result, 2007101512);
3005 return $result;