MDL-11432 eliminated majority of RecordCount uses, added several missing rs_close...
[moodle.git] / lib / db / upgrade.php
CommitLineData
4e423cbf 1<?PHP //$Id$
2
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.
12//
13// If there's something it cannot do itself, it
14// will tell you what you need to do.
15//
16// The commands in here will all be database-neutral,
17// using the functions defined in lib/ddllib.php
18
19
20function xmldb_main_upgrade($oldversion=0) {
21
8a45fcdf 22 global $CFG, $THEME, $USER, $db;
4e423cbf 23
24 $result = true;
25
26 if ($oldversion < 2006100401) {
27 /// Only for those tracking Moodle 1.7 dev, others will have these dropped in moodle_install_roles()
4c2275d5 28 if (!empty($CFG->rolesactive)) {
acc26e71 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'));
4e423cbf 33 }
34 }
35
e27a9437 36 if ($oldversion < 2006100601) { /// Disable the exercise module because it's unmaintained
37 if ($module = get_record('modules', 'name', 'exercise')) {
38 if ($module->visible) {
39 // Hide/disable the module entry
271e6dec 40 set_field('modules', 'visible', '0', 'id', $module->id);
e27a9437 41 // Save existing visible state for all activities
42 set_field('course_modules', 'visibleold', '1', 'visible' ,'1', 'module', $module->id);
43 set_field('course_modules', 'visibleold', '0', 'visible' ,'0', 'module', $module->id);
44 // Hide all activities
45 set_field('course_modules', 'visible', '0', 'module', $module->id);
271e6dec 46
e27a9437 47 require_once($CFG->dirroot.'/course/lib.php');
48 rebuild_course_cache(); // Rebuld cache for all modules because they might have changed
49 }
50 }
51 }
52
171d8232 53 if ($oldversion < 2006101001) { /// Disable the LAMS module by default (if it is installed)
54 if (count_records('modules', 'name', 'lams') && !count_records('lams')) {
49e67510 55 set_field('modules', 'visible', 0, 'name', 'lams'); // Disable it by default
56 }
57 }
271e6dec 58
74807b43 59 if ($result && $oldversion < 2006102600) {
60
61 /// Define fields to be added to user_info_field
62 $table = new XMLDBTable('user_info_field');
63 $field = new XMLDBField('description');
64 $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'categoryid');
65 $field1 = new XMLDBField('param1');
66 $field1->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'defaultdata');
67 $field2 = new XMLDBField('param2');
68 $field2->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param1');
69 $field3 = new XMLDBField('param3');
70 $field3->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param2');
71 $field4 = new XMLDBField('param4');
72 $field4->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param3');
73 $field5 = new XMLDBField('param5');
74 $field5->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'param4');
75
76 /// Launch add fields
77 $result = $result && add_field($table, $field);
78 $result = $result && add_field($table, $field1);
79 $result = $result && add_field($table, $field2);
80 $result = $result && add_field($table, $field3);
81 $result = $result && add_field($table, $field4);
82 $result = $result && add_field($table, $field5);
83 }
271e6dec 84
7d0e5a95 85 if ($result && $oldversion < 2006112000) {
86
87 /// Define field attachment to be added to post
88 $table = new XMLDBTable('post');
89 $field = new XMLDBField('attachment');
90 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null, 'format');
91
92 /// Launch add field attachment
93 $result = $result && add_field($table, $field);
94 }
271e6dec 95
54dc87b8 96 if ($result && $oldversion < 2006112200) {
97
98 /// Define field imagealt to be added to user
99 $table = new XMLDBTable('user');
100 $field = new XMLDBField('imagealt');
101 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'trustbitmask');
102
103 /// Launch add field imagealt
104 $result = $result && add_field($table, $field);
271e6dec 105
54dc87b8 106 $table = new XMLDBTable('user');
107 $field = new XMLDBField('screenreader');
108 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, null, '0', 'imagealt');
109
110 /// Launch add field screenreader
111 $result = $result && add_field($table, $field);
112 }
49e67510 113
7b678e0a 114 if ($oldversion < 2006120300) { /// Delete guest course section settings
115 // following code can be executed repeatedly, such as when upgrading from 1.7.x - it is ok
116 if ($guest = get_record('user', 'username', 'guest')) {
77b09448 117 execute_sql("DELETE FROM {$CFG->prefix}course_display where userid=$guest->id", true);
7b678e0a 118 }
119 }
120
9c0f063b 121 if ($oldversion < 2006120400) { /// Remove secureforms config setting
77b09448 122 execute_sql("DELETE FROM {$CFG->prefix}config where name='secureforms'", true);
9c0f063b 123 }
271e6dec 124
8c6c185a 125 if (!empty($CFG->rolesactive) && $oldversion < 2006120700) { // add moodle/user:viewdetails to all roles!
126 // note: use of assign_capability() is discouraged in upgrade script!
22ae509e 127 if ($roles = get_records('role')) {
128 $context = get_context_instance(CONTEXT_SYSTEM);
129 foreach ($roles as $roleid=>$role) {
130 assign_capability('moodle/user:viewdetails', CAP_ALLOW, $roleid, $context->id);
131 }
132 }
133 }
9c0f063b 134
5cd129c7 135 // Move the auth plugin settings into the config_plugin table
136 if ($oldversion < 2007010300) {
177df3c6 137 if ($CFG->auth == 'email') {
138 set_config('registerauth', 'email');
139 } else {
140 set_config('registerauth', '');
141 }
5cd129c7 142 $authplugins = get_list_of_plugins('auth');
143 foreach ($CFG as $k => $v) {
922c5b36 144 if (strpos($k, 'ldap_') === 0) {
145 //upgrade nonstandard ldap settings
146 $setting = substr($k, 5);
147 if (set_config($setting, $v, "auth/ldap")) {
148 delete_records('config', 'name', $k);
149 unset($CFG->{$k});
150 }
151 continue;
152 }
5cd129c7 153 if (strpos($k, 'auth_') !== 0) {
154 continue;
155 }
156 $authsetting = substr($k, 5);
157 foreach ($authplugins as $auth) {
158 if (strpos($authsetting, $auth) !== 0) {
159 continue;
160 }
161 $setting = substr($authsetting, strlen($auth));
162 if (set_config($setting, $v, "auth/$auth")) {
163 delete_records('config', 'name', $k);
164 unset($CFG->{$k});
165 }
166 break; // don't check the rest of the auth plugin names
167 }
168 }
169 }
170
171 if ($oldversion < 2007010301) {
172 //
173 // Core MNET tables
174 //
175 $table = new XMLDBTable('mnet_host');
176 $table->comment = 'Information about the local and remote hosts for RPC';
177 // fields
178 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
179 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
180 $f->comment = 'Unique Host ID';
181 $f = $table->addFieldInfo('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
182 XMLDB_NOTNULL, null, null, null, 0);
183 $f = $table->addFieldInfo('wwwroot', XMLDB_TYPE_CHAR, '255', null,
a54d16ef 184 XMLDB_NOTNULL, null, null, null, null);
5cd129c7 185 $f = $table->addFieldInfo('ip_address', XMLDB_TYPE_CHAR, '39', null,
a54d16ef 186 XMLDB_NOTNULL, null, null, null, null);
5cd129c7 187 $f = $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '80', null,
a54d16ef 188 XMLDB_NOTNULL, null, null, null, null);
357f08fa 189 $f = $table->addFieldInfo('public_key', XMLDB_TYPE_TEXT, 'medium', null,
5cd129c7 190 XMLDB_NOTNULL, null, null, null, null);
191 $f = $table->addFieldInfo('public_key_expires', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
192 XMLDB_NOTNULL, null, null, null, 0);
193 $f = $table->addFieldInfo('transport', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED,
194 XMLDB_NOTNULL, null, null, null, 0);
195 $f = $table->addFieldInfo('portno', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED,
196 XMLDB_NOTNULL, null, null, null, 0);
197 $f = $table->addFieldInfo('last_connect_time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
198 XMLDB_NOTNULL, null, null, null, 0);
199 $f = $table->addFieldInfo('last_log_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
200 XMLDB_NOTNULL, null, null, null, 0);
271e6dec 201 // PK and indexes
5cd129c7 202 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
203 // Create the table
204 $result = $result && create_table($table);
205
206 $table = new XMLDBTable('mnet_host2service');
207 $table->comment = 'Information about the services for a given host';
208 // fields
209 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
210 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
211 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
212 XMLDB_NOTNULL, NULL, null, null, 0);
213 $f = $table->addFieldInfo('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
214 XMLDB_NOTNULL, NULL, null, null, 0);
215 $f = $table->addFieldInfo('publish', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
216 XMLDB_NOTNULL, NULL, null, null, 0);
217 $f = $table->addFieldInfo('subscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
218 XMLDB_NOTNULL, NULL, null, null, 0);
219 // PK and indexes
220 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
221 $table->addIndexInfo('hostid_serviceid', XMLDB_INDEX_UNIQUE, array('hostid', 'serviceid'));
222 // Create the table
223 $result = $result && create_table($table);
224
225 $table = new XMLDBTable('mnet_log');
226 $table->comment = 'Store session data from users migrating to other sites';
227 // fields
228 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
229 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
230 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
231 XMLDB_NOTNULL, NULL, null, null, 0);
232 $f = $table->addFieldInfo('remoteid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
233 XMLDB_NOTNULL, NULL, null, null, 0);
234 $f = $table->addFieldInfo('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
235 XMLDB_NOTNULL, NULL, null, null, 0);
236 $f = $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
237 XMLDB_NOTNULL, NULL, null, null, 0);
238 $f = $table->addFieldInfo('ip', XMLDB_TYPE_CHAR, '15', null,
239 XMLDB_NOTNULL, NULL, null, null, null);
240 $f = $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
241 XMLDB_NOTNULL, NULL, null, null, 0);
242 $f = $table->addFieldInfo('coursename', XMLDB_TYPE_CHAR, '40', null,
243 XMLDB_NOTNULL, NULL, null, null, null);
244 $f = $table->addFieldInfo('module', XMLDB_TYPE_CHAR, '20', null,
245 XMLDB_NOTNULL, NULL, null, null, null);
246 $f = $table->addFieldInfo('cmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
247 XMLDB_NOTNULL, NULL, null, null, 0);
248 $f = $table->addFieldInfo('action', XMLDB_TYPE_CHAR, '40', null,
249 XMLDB_NOTNULL, NULL, null, null, null);
250 $f = $table->addFieldInfo('url', XMLDB_TYPE_CHAR, '100', null,
251 XMLDB_NOTNULL, NULL, null, null, null);
252 $f = $table->addFieldInfo('info', XMLDB_TYPE_CHAR, '255', null,
253 XMLDB_NOTNULL, NULL, null, null, null);
254 // PK and indexes
255 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
256 $table->addIndexInfo('host_user_course', XMLDB_INDEX_NOTUNIQUE, array('hostid', 'userid', 'course'));
257 // Create the table
258 $result = $result && create_table($table);
259
260
261 $table = new XMLDBTable('mnet_rpc');
262 $table->comment = 'Functions or methods that we may publish or subscribe to';
263 // fields
264 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
265 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
266 $f = $table->addFieldInfo('function_name', XMLDB_TYPE_CHAR, '40', null,
267 XMLDB_NOTNULL, NULL, null, null, null);
268 $f = $table->addFieldInfo('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null,
269 XMLDB_NOTNULL, NULL, null, null, null);
270 $f = $table->addFieldInfo('parent_type', XMLDB_TYPE_CHAR, '6', null,
271 XMLDB_NOTNULL, NULL, null, null, null);
272 $f = $table->addFieldInfo('parent', XMLDB_TYPE_CHAR, '20', null,
273 XMLDB_NOTNULL, NULL, null, null, null);
274 $f = $table->addFieldInfo('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
275 XMLDB_NOTNULL, NULL, null, null, 0);
276 $f = $table->addFieldInfo('help', XMLDB_TYPE_TEXT, 'medium', null,
277 XMLDB_NOTNULL, NULL, null, null, null);
278 $f = $table->addFieldInfo('profile', XMLDB_TYPE_TEXT, 'medium', null,
279 XMLDB_NOTNULL, NULL, null, null, null);
280 // PK and indexes
281 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
282 $table->addIndexInfo('enabled_xpath', XMLDB_INDEX_NOTUNIQUE, array('enabled', 'xmlrpc_path'));
283 // Create the table
284 $result = $result && create_table($table);
285
286 $table = new XMLDBTable('mnet_service');
287 $table->comment = 'A service is a group of functions';
288 // fields
289 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
290 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
291 $f = $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '40', null,
292 XMLDB_NOTNULL, NULL, null, null, null);
293 $f = $table->addFieldInfo('description', XMLDB_TYPE_CHAR, '40', null,
294 XMLDB_NOTNULL, NULL, null, null, null);
295 $f = $table->addFieldInfo('apiversion', XMLDB_TYPE_CHAR, '10', null,
296 XMLDB_NOTNULL, NULL, null, null, null);
297 $f = $table->addFieldInfo('offer', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
298 XMLDB_NOTNULL, NULL, null, null, 0);
299 // PK and indexes
300 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
301 // Create the table
302 $result = $result && create_table($table);
303
304 $table = new XMLDBTable('mnet_service2rpc');
357f08fa 305 $table->comment = 'Group functions or methods under a service';
5cd129c7 306 // fields
307 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
308 XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
309 $f = $table->addFieldInfo('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
310 XMLDB_NOTNULL, NULL, null, null, 0);
311 $f = $table->addFieldInfo('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
312 XMLDB_NOTNULL, NULL, null, null, 0);
313 // PK and indexes
314 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
315 $table->addIndexInfo('unique', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
316 // Create the table
317 $result = $result && create_table($table);
318
319 //
271e6dec 320 // Prime MNET configuration entries -- will be needed later by auth/mnet
5cd129c7 321 //
322 include_once $CFG->dirroot . '/mnet/lib.php';
323 $env = new mnet_environment();
324 $env->init();
325 unset($env);
326
271e6dec 327 // add mnethostid to user-
5cd129c7 328 $table = new XMLDBTable('user');
329 $field = new XMLDBField('mnethostid');
330 $field->setType(XMLDB_TYPE_INTEGER);
331 $field->setLength(10);
332 $field->setNotNull(true);
333 $field->setSequence(null);
334 $field->setEnum(null);
335 $field->setDefault('0');
336 $field->setPrevious("deleted");
337 $field->setNext("username");
338 $result = $result && add_field($table, $field);
339
340 // The default mnethostid is zero... we need to update this for all
341 // users of the local IdP service.
271e6dec 342 set_field('user',
343 'mnethostid', $CFG->mnet_localhost_id,
5cd129c7 344 'mnethostid', '0');
345
346
347 $index = new XMLDBIndex('username');
348 $index->setUnique(true);
349 $index->setFields(array('username'));
350 drop_index($table, $index);
351 $index->setFields(array('mnethostid', 'username'));
b7791fab 352 if (!add_index($table, $index)) {
68879a58 353 notify(get_string('duplicate_usernames', 'mnet', 'http://docs.moodle.org/en/DuplicateUsernames'));
354 }
5cd129c7 355
271e6dec 356 unset($table, $field, $index);
5cd129c7 357
358 /**
359 ** auth/mnet tables
360 **/
361 $table = new XMLDBTable('mnet_session');
362 $table->comment='Store session data from users migrating to other sites';
363 // fields
364 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
365 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
366 $f = $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
367 XMLDB_NOTNULL, NULL, null, null, 0);
368 $f = $table->addFieldInfo('username', XMLDB_TYPE_CHAR, '100', null,
369 XMLDB_NOTNULL, NULL, null, null, null);
370 $f = $table->addFieldInfo('token', XMLDB_TYPE_CHAR, '40', null,
371 XMLDB_NOTNULL, NULL, null, null, null);
372 $f = $table->addFieldInfo('mnethostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
373 XMLDB_NOTNULL, NULL, null, null, 0);
374 $f = $table->addFieldInfo('useragent', XMLDB_TYPE_CHAR, '40', null,
375 XMLDB_NOTNULL, NULL, null, null, null);
376 $f = $table->addFieldInfo('confirm_timeout', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
377 XMLDB_NOTNULL, NULL, null, null, 0);
378 $f = $table->addFieldInfo('session_id', XMLDB_TYPE_CHAR, '40', null,
379 XMLDB_NOTNULL, NULL, null, null, null);
380 $f = $table->addFieldInfo('expires', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
381 XMLDB_NOTNULL, NULL, null, null, 0);
382 // PK and indexes
383 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
357f08fa 384 $table->addIndexInfo('token', XMLDB_INDEX_UNIQUE, array('token'));
5cd129c7 385 // Create the table
386 $result = $result && create_table($table);
387
388
389 $table = new XMLDBTable('mnet_sso_access_control');
390 $table->comment = 'Users by host permitted (or not) to login from a remote provider';
391 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
392 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
393 $f = $table->addFieldInfo('username', XMLDB_TYPE_CHAR, '100', null,
394 XMLDB_NOTNULL, NULL, null, null, null);
395 $f = $table->addFieldInfo('mnet_host_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
396 XMLDB_NOTNULL, NULL, null, null, 0);
397 $f = $table->addFieldInfo('access', XMLDB_TYPE_CHAR, '20', null,
398 XMLDB_NOTNULL, NULL, null, null, 'allow');
399 // PK and indexes
400 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
401 $table->addIndexInfo('mnethostid_username', XMLDB_INDEX_UNIQUE, array('mnet_host_id', 'username'));
402 // Create the table
403 $result = $result && create_table($table);
404
8a45fcdf 405 if (empty($USER->mnet_host_id)) {
cac88d40 406 $USER->mnet_host_id = $CFG->mnet_localhost_id; // Something for the current user to prevent warnings
8a45fcdf 407 }
408
5cd129c7 409 /**
410 ** enrol/mnet tables
411 **/
412 $table = new XMLDBTable('mnet_enrol_course');
413 $table->comment = 'Information about courses on remote hosts';
414 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
415 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
416 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
417 XMLDB_NOTNULL, NULL, null, null, 0);
418 $f = $table->addFieldInfo('remoteid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
419 XMLDB_NOTNULL, NULL, null, null, 0);
420 $f = $table->addFieldInfo('cat_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
421 XMLDB_NOTNULL, NULL, null, null, 0);
422 $f = $table->addFieldInfo('cat_name', XMLDB_TYPE_CHAR, '255', null,
423 XMLDB_NOTNULL, NULL, null, null, null);
424 $f = $table->addFieldInfo('cat_description', XMLDB_TYPE_TEXT, 'medium', null,
425 XMLDB_NOTNULL, NULL, null, null, null);
426 $f = $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
427 XMLDB_NOTNULL, NULL, null, null, 0);
428 $f = $table->addFieldInfo('fullname', XMLDB_TYPE_CHAR, '254', null,
a54d16ef 429 XMLDB_NOTNULL, NULL, null, null, null);
5cd129c7 430 $f = $table->addFieldInfo('shortname', XMLDB_TYPE_CHAR, '15', null,
431 XMLDB_NOTNULL, NULL, null, null, null);
432 $f = $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '100', null,
433 XMLDB_NOTNULL, NULL, null, null, null);
434 $f = $table->addFieldInfo('summary', XMLDB_TYPE_TEXT, 'medium', null,
435 XMLDB_NOTNULL, NULL, null, null, null);
436 $f = $table->addFieldInfo('startdate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
437 XMLDB_NOTNULL, NULL, null, null, 0);
438 $f = $table->addFieldInfo('cost', XMLDB_TYPE_CHAR, '10', null,
439 XMLDB_NOTNULL, NULL, null, null, null);
440 $f = $table->addFieldInfo('currency', XMLDB_TYPE_CHAR, '3', null,
441 XMLDB_NOTNULL, NULL, null, null, null);
442 $f = $table->addFieldInfo('defaultroleid', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
443 XMLDB_NOTNULL, NULL, null, null, 0);
444 $f = $table->addFieldInfo('defaultrolename', XMLDB_TYPE_CHAR, '255', null,
445 XMLDB_NOTNULL, NULL, null, null, null);
446 // PK and indexes
447 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
448 $table->addIndexInfo('hostid_remoteid', XMLDB_INDEX_UNIQUE, array('hostid', 'remoteid'));
449 // Create the table
450 $result = $result && create_table($table);
451
452
453 $table = new XMLDBTable('mnet_enrol_assignments');
454
455 $table->comment = 'Information about enrolments on courses on remote hosts';
456 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
457 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
458 $f = $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
459 XMLDB_NOTNULL, NULL, null, null, 0);
460 $f = $table->addFieldInfo('hostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
271e6dec 461 XMLDB_NOTNULL, NULL, null, null, 0);
5cd129c7 462 $f = $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
463 XMLDB_NOTNULL, NULL, null, null, 0);
464 $f = $table->addFieldInfo('rolename', XMLDB_TYPE_CHAR, '255', null,
465 XMLDB_NOTNULL, NULL, null, null, null);
466 $f = $table->addFieldInfo('enroltime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
467 XMLDB_NOTNULL, NULL, null, null, 0);
468 $f = $table->addFieldInfo('enroltype', XMLDB_TYPE_CHAR, '20', null,
469 XMLDB_NOTNULL, NULL, null, null, null);
470
471 // PK and indexes
472 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
473 $table->addIndexInfo('hostid_courseid', XMLDB_INDEX_NOTUNIQUE, array('hostid', 'courseid'));
357f08fa 474 $table->addIndexInfo('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
5cd129c7 475 // Create the table
476 $result = $result && create_table($table);
477
478 }
479
dedda83d 480 if ($result && $oldversion < 2007010404) {
481
482 /// Define field shortname to be added to user_info_field
483 $table = new XMLDBTable('user_info_field');
484 $field = new XMLDBField('shortname');
485 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, 'shortname', 'id');
486
487 /// Launch add field shortname
488 $result = $result && add_field($table, $field);
489
490 /// Changing type of field name on table user_info_field to text
491 $table = new XMLDBTable('user_info_field');
492 $field = new XMLDBField('name');
493 $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null, 'shortname');
494
495 /// Launch change of type for field name
496 $result = $result && change_field_type($table, $field);
497
498 /// For existing fields use 'name' as the 'shortname' entry
4218c9b2 499 if ($fields = get_records_select('user_info_field', '', '', 'id, name')) {
dedda83d 500 foreach ($fields as $field) {
501 $field->shortname = clean_param($field->name, PARAM_ALPHANUM);
502 $result && update_record('user_info_field', $field);
503 }
504 }
505 }
506
bb931a61 507 if ($result && $oldversion < 2007011501) {
271e6dec 508 if (!empty($CFG->enablerecordcache) && empty($CFG->rcache) &&
bb931a61 509 // Note: won't force-load these settings into CFG
510 // we don't need or want cache during the upgrade itself
511 empty($CFG->cachetype) && empty($CFG->intcachemax)) {
512 set_config('cachetype', 'internal');
513 set_config('rcache', true);
514 set_config('intcachemax', $CFG->enablerecordcache);
515 unset_config('enablerecordcache');
516 unset($CFG->enablerecordcache);
517 }
518 }
519
9fc57f4c 520 if ($result && $oldversion < 2007012100) {
521 /// Some old PG servers have user->firstname & user->lastname with 30cc. They must be 100cc.
522 /// Fixing that conditionally. MDL-7110
523 if ($CFG->dbfamily == 'postgres') {
524 /// Get Metadata from user table
525 $cols = array_change_key_case($db->MetaColumns($CFG->prefix . 'user'), CASE_LOWER);
526
527 /// Process user->firstname if needed
528 if ($col = $cols['firstname']) {
529 if ($col->max_length < 100) {
530 /// Changing precision of field firstname on table user to (100)
531 $table = new XMLDBTable('user');
532 $field = new XMLDBField('firstname');
533 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null, 'idnumber');
534
535 /// Launch change of precision for field firstname
536 $result = $result && change_field_precision($table, $field);
537 }
538 }
539
540 /// Process user->lastname if needed
541 if ($col = $cols['lastname']) {
542 if ($col->max_length < 100) {
543 /// Changing precision of field lastname on table user to (100)
544 $table = new XMLDBTable('user');
545 $field = new XMLDBField('lastname');
546 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null, 'firstname');
547
548 /// Launch change of precision for field lastname
549 $result = $result && change_field_precision($table, $field);
550 }
551 }
552 }
553 }
554
deb12ef4 555 if ($result && $oldversion < 2007012101) {
556
557 /// Changing precision of field lang on table course to (30)
558 $table = new XMLDBTable('course');
559 $field = new XMLDBField('lang');
560 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null, 'groupmodeforce');
561
562 /// Launch change of precision for field course->lang
563 $result = $result && change_field_precision($table, $field);
564
565 /// Changing precision of field lang on table user to (30)
566 $table = new XMLDBTable('user');
567 $field = new XMLDBField('lang');
568 $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'en', 'country');
569
570 /// Launch change of precision for field user->lang
571 $result = $result && change_field_precision($table, $field);
572 }
573
cdf22329 574 if ($result && $oldversion < 2007012400) {
575
576 /// Rename field access on table mnet_sso_access_control to accessctrl
577 $table = new XMLDBTable('mnet_sso_access_control');
578 $field = new XMLDBField('access');
fdaacd8b 579 $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'allow', 'mnet_host_id');
cdf22329 580
581 /// Launch rename field accessctrl
582 $result = $result && rename_field($table, $field, 'accessctrl');
583 }
584
e8e0bb2d 585 if ($result && $oldversion < 2007012500) {
77b09448 586 execute_sql("DELETE FROM {$CFG->prefix}user WHERE username='changeme'", true);
e8e0bb2d 587 }
588
8dadea88 589 if ($result && $oldversion < 2007020400) {
590 /// Only for MySQL and PG, declare the user->ajax field as not null. MDL-8421.
591 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
592 /// Changing nullability of field ajax on table user to not null
593 $table = new XMLDBTable('user');
594 $field = new XMLDBField('ajax');
595 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'htmleditor');
596
597 /// Launch change of nullability for field ajax
598 $result = $result && change_field_notnull($table, $field);
599 }
600 }
601
ee266f33 602 if (!empty($CFG->rolesactive) && $result && $oldversion < 2007021401) {
efe12f6c 603 /// create default logged in user role if not present - upgrade rom 1.7.x
c7888a90 604 if (empty($CFG->defaultuserroleid) or empty($CFG->guestroleid) or $CFG->defaultuserroleid == $CFG->guestroleid) {
efe12f6c 605 if (!get_records('role', 'shortname', 'user')) {
606 $userroleid = create_role(addslashes(get_string('authenticateduser')), 'user',
607 addslashes(get_string('authenticateduserdescription')), 'moodle/legacy:user');
608 if ($userroleid) {
609 reset_role_capabilities($userroleid);
610 set_config('defaultuserroleid', $userroleid);
611 }
612 }
613 }
614 }
615
77d08333 616 if ($result && $oldversion < 2007021501) {
617 /// delete removed setting from config
618 unset_config('tabselectedtofront');
619 }
620
b318ecfd 621
622 if ($result && $oldversion < 2007032200) {
623
624 /// Define table role_sortorder to be created
625 $table = new XMLDBTable('role_sortorder');
626
627 /// Adding fields to table role_sortorder
abd30252 628 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
b318ecfd 629 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
630 $table->addFieldInfo('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
631 $table->addFieldInfo('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
632 $table->addFieldInfo('sortoder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null);
633
634 /// Adding keys to table role_sortorder
635 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
636 $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
637 $table->addKeyInfo('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
638 $table->addKeyInfo('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
639
640 /// Adding indexes to table role_sortorder
641 $table->addIndexInfo('userid-roleid-contextid', XMLDB_INDEX_UNIQUE, array('userid', 'roleid', 'contextid'));
642
643 /// Launch create table for role_sortorder
644 $result = $result && create_table($table);
645 }
646
07076b70 647
648 /// code to change lenghen tag field to 255, MDL-9095
649 if ($result && $oldversion < 2007040400) {
650
651 /// Define index text (not unique) to be dropped form tags
652 $table = new XMLDBTable('tags');
653 $index = new XMLDBIndex('text');
654 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('text'));
655
656 /// Launch drop index text
657 $result = $result && drop_index($table, $index);
271e6dec 658
07076b70 659 $field = new XMLDBField('text');
660 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'userid');
661
662 /// Launch change of type for field text
663 $result = $result && change_field_type($table, $field);
271e6dec 664
07076b70 665 $index = new XMLDBIndex('text');
666 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('text'));
667
668 /// Launch add index text
271e6dec 669 $result = $result && add_index($table, $index);
07076b70 670 }
271e6dec 671
177d4abf 672 if ($result && $oldversion < 2007041100) {
673
674 /// Define field idnumber to be added to course_modules
675 $table = new XMLDBTable('course_modules');
676 $field = new XMLDBField('idnumber');
677 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null, 'section');
271e6dec 678
177d4abf 679 /// Launch add field idnumber
680 $result = $result && add_field($table, $field);
271e6dec 681
177d4abf 682 /// Define index idnumber (unique) to be added to course_modules
683 $table = new XMLDBTable('course_modules');
684 $index = new XMLDBIndex('idnumber');
685 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('idnumber'));
686
687 /// Launch add index idnumber
688 $result = $result && add_index($table, $index);
689
690 }
691
334415e9 692 /* Changes to the custom profile menu type - store values rather than indices.
693 We could do all this with one tricky SQL statement but it's a one-off so no
694 harm in using PHP loops */
695 if ($result && $oldversion < 2007041600) {
271e6dec 696
334415e9 697 /// Get the menu fields
698 if ($fields = get_records('user_info_field', 'datatype', 'menu')) {
699 foreach ($fields as $field) {
700
701 /// Get user data for the menu field
702 if ($data = get_records('user_info_data', 'fieldid', $field->id)) {
703
704 /// Get the menu options
540286f1 705 $options = explode("\n", $field->param1);
334415e9 706 foreach ($data as $d) {
707 $key = array_search($d->data, $options);
271e6dec 708
334415e9 709 /// If the data is an integer and is not one of the options,
710 /// set the respective option value
711 if (is_int($d->data) and (($key === NULL) or ($key === false)) and isset($options[$d->data])) {
712 $d->data = $options[$d->data];
713 $result = $result && update_record('user_info_data', $d);
714 }
715 }
716 }
717 }
718 }
271e6dec 719
334415e9 720 }
271e6dec 721
5cb564aa 722 /// adding new gradebook tables
ec36253d 723 if ($result && $oldversion < 2007041800) {
724
725 /// Define table events_handlers to be created
726 $table = new XMLDBTable('events_handlers');
727
728 /// Adding fields to table events_handlers
197ba19c 729 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
5cb564aa 730 $table->addFieldInfo('eventname', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null, null, null);
731 $table->addFieldInfo('handlermodule', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null, null, null);
ec36253d 732 $table->addFieldInfo('handlerfile', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
733 $table->addFieldInfo('handlerfunction', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
734
735 /// Adding keys to table events_handlers
736 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
737
738 /// Adding indexes to table events_handlers
5cb564aa 739 $table->addIndexInfo('eventname-handlermodule', XMLDB_INDEX_UNIQUE, array('eventname', 'handlermodule'));
ec36253d 740
741 /// Launch create table for events_handlers
742 $result = $result && create_table($table);
271e6dec 743
ec36253d 744 /// Define table events_queue to be created
745 $table = new XMLDBTable('events_queue');
746
747 /// Adding fields to table events_queue
197ba19c 748 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
ec36253d 749 $table->addFieldInfo('eventdata', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null);
750 $table->addFieldInfo('schedule', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
751 $table->addFieldInfo('stackdump', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
752 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
753 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
754
755 /// Adding keys to table events_queue
756 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
757 $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
334415e9 758
ec36253d 759 /// Launch create table for events_queue
760 $result = $result && create_table($table);
271e6dec 761
ec36253d 762 /// Define table events_queue_handlers to be created
763 $table = new XMLDBTable('events_queue_handlers');
764
765 /// Adding fields to table events_queue_handlers
197ba19c 766 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
ec36253d 767 $table->addFieldInfo('queuedeventid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
768 $table->addFieldInfo('handlerid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
769 $table->addFieldInfo('status', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
770 $table->addFieldInfo('errormessage', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
771 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
772
773 /// Adding keys to table events_queue_handlers
774 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
775 $table->addKeyInfo('queuedeventid', XMLDB_KEY_FOREIGN, array('queuedeventid'), 'events_queue', array('id'));
776 $table->addKeyInfo('handlerid', XMLDB_KEY_FOREIGN, array('handlerid'), 'events_handlers', array('id'));
777
778 /// Launch create table for events_queue_handlers
779 $result = $result && create_table($table);
780
781 }
9d2cddf1 782
896f1e03 783 if ($result && $oldversion < 2007043001) {
784
896f1e03 785 /// Define field schedule to be added to events_handlers
786 $table = new XMLDBTable('events_handlers');
787 $field = new XMLDBField('schedule');
788 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'handlerfunction');
789
790 /// Launch add field schedule
791 $result = $result && add_field($table, $field);
271e6dec 792
896f1e03 793 /// Define field status to be added to events_handlers
794 $table = new XMLDBTable('events_handlers');
795 $field = new XMLDBField('status');
796 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'schedule');
797
798 /// Launch add field status
799 $result = $result && add_field($table, $field);
800 }
801
ec75a182 802 if ($result && $oldversion < 2007050201) {
803
804 /// Define field theme to be added to course_categories
805 $table = new XMLDBTable('course_categories');
806 $field = new XMLDBField('theme');
807 $field->setAttributes(XMLDB_TYPE_CHAR, '50', null, null, null, null, null, null, 'path');
808
809 /// Launch add field theme
810 $result = $result && add_field($table, $field);
811 }
9aaa214e 812
813 if ($result && $oldversion < 2007051100) {
814
815 /// Define field forceunique to be added to user_info_field
816 $table = new XMLDBTable('user_info_field');
817 $field = new XMLDBField('forceunique');
818 $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'visible');
819
820 /// Launch add field forceunique
821 $result = $result && add_field($table, $field);
822
823 /// Define field signup to be added to user_info_field
824 $table = new XMLDBTable('user_info_field');
825 $field = new XMLDBField('signup');
826 $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'forceunique');
827
828 /// Launch add field signup
829 $result = $result && add_field($table, $field);
830 }
93759def 831
8c6c185a 832 if (!empty($CFG->rolesactive) && $result && $oldversion < 2007051801) {
833 // Get the role id of the "Auth. User" role and check if the default role id is different
834 // note: use of assign_capability() is discouraged in upgrade script!
282c1695 835 $userrole = get_record( 'role', 'shortname', 'user' );
836 $defaultroleid = $CFG->defaultuserroleid;
837
838 if( $defaultroleid != $userrole->id ) {
839 // Add in the new moodle/my:manageblocks capibility to the default user role
840 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
841 assign_capability('moodle/my:manageblocks',CAP_ALLOW,$defaultroleid,$context->id);
842 }
843 }
844
d46306de 845 if ($result && $oldversion < 2007052200) {
846
847 /// Define field schedule to be dropped from events_queue
848 $table = new XMLDBTable('events_queue');
849 $field = new XMLDBField('schedule');
850
851 /// Launch drop field stackdump
852 $result = $result && drop_field($table, $field);
853 }
854
ec4560cc 855 if ($result && $oldversion < 2007052300) {
856 require_once($CFG->dirroot . '/question/upgrade.php');
857 $result = $result && question_remove_rqp_qtype();
858 }
859
1b63e573 860 if ($result && $oldversion < 2007060500) {
861
862 /// Define field usermodified to be added to post
863 $table = new XMLDBTable('post');
864 $field = new XMLDBField('usermodified');
865 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'created');
866
867 /// Launch add field usermodified
868 $result = $result && add_field($table, $field);
271e6dec 869
1b63e573 870 /// Define key usermodified (foreign) to be added to post
871 $table = new XMLDBTable('post');
872 $key = new XMLDBKey('usermodified');
873 $key->setAttributes(XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
874
875 /// Launch add key usermodified
876 $result = $result && add_key($table, $key);
877 }
ac9b0805 878
42ff9ce6 879 if ($result && $oldversion < 2007070603) {
880 // Small update of guest user to be 100% sure it has the correct mnethostid (MDL-10375)
881 set_field('user', 'mnethostid', $CFG->mnet_localhost_id, 'username', 'guest');
882 }
883
884 if ($result && $oldversion < 2007071400) {
885 /**
886 ** mnet application table
887 **/
888 $table = new XMLDBTable('mnet_application');
889 $table->comment = 'Information about applications on remote hosts';
890 $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false,
891 XMLDB_NOTNULL,XMLDB_SEQUENCE, null, null, null);
892 $f = $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '50', null,
893 XMLDB_NOTNULL, NULL, null, null, null);
894 $f = $table->addFieldInfo('display_name', XMLDB_TYPE_CHAR, '50', null,
895 XMLDB_NOTNULL, NULL, null, null, null);
896 $f = $table->addFieldInfo('xmlrpc_server_url', XMLDB_TYPE_CHAR, '255', null,
897 XMLDB_NOTNULL, NULL, null, null, null);
898 $f = $table->addFieldInfo('sso_land_url', XMLDB_TYPE_CHAR, '255', null,
899 XMLDB_NOTNULL, NULL, null, null, null);
900
901 // PK and indexes
902 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
903 // Create the table
904 $result = $result && create_table($table);
905
906 // Insert initial applications (moodle and mahara)
907 $application = new stdClass();
908 $application->name = 'moodle';
909 $application->display_name = 'Moodle';
910 $application->xmlrpc_server_url = '/mnet/xmlrpc/server.php';
911 $application->sso_land_url = '/auth/mnet/land.php';
912 if ($result) {
913 $newid = insert_record('mnet_application', $application, false);
914 }
915
916 $application = new stdClass();
917 $application->name = 'mahara';
918 $application->display_name = 'Mahara';
919 $application->xmlrpc_server_url = '/api/xmlrpc/server.php';
920 $application->sso_land_url = '/auth/xmlrpc/land.php';
921 $result = $result && insert_record('mnet_application', $application, false);
271e6dec 922
42ff9ce6 923 // New mnet_host->applicationid field
924 $table = new XMLDBTable('mnet_host');
925 $field = new XMLDBField('applicationid');
926 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, $newid , 'last_log_id');
927
928 $result = $result && add_field($table, $field);
929
930 /// Define key applicationid (foreign) to be added to mnet_host
931 $table = new XMLDBTable('mnet_host');
932 $key = new XMLDBKey('applicationid');
933 $key->setAttributes(XMLDB_KEY_FOREIGN, array('applicationid'), 'mnet_application', array('id'));
934
935 /// Launch add key applicationid
936 $result = $result && add_key($table, $key);
578075c6 937
42ff9ce6 938 }
939
940 if ($result && $oldversion < 2007071607) {
941 require_once($CFG->dirroot . '/question/upgrade.php');
942 $result = $result && question_remove_rqp_qtype_config_string();
943 }
944
42ff9ce6 945 if ($result && $oldversion < 2007072200) {
42ff9ce6 946
947/// Remove all grade tables used in development phases - we need new empty tables for final gradebook upgrade
948 $tables = array('grade_categories',
949 'grade_items',
950 'grade_calculations',
951 'grade_grades',
952 'grade_grades_raw',
953 'grade_grades_final',
954 'grade_grades_text',
955 'grade_outcomes',
956 'grade_outcomes_courses',
957 'grade_history',
958 'grade_import_newitem',
959 'grade_import_values');
578075c6 960
b3ac6c3e 961 foreach ($tables as $table) {
962 $table = new XMLDBTable($table);
963 if (table_exists($table)) {
964 drop_table($table);
965 }
578075c6 966 }
578075c6 967
42ff9ce6 968 $tables = array('grade_categories_history',
969 'grade_items_history',
970 'grade_grades_history',
971 'grade_grades_text_history',
972 'grade_scale_history',
973 'grade_outcomes_history');
974
975 foreach ($tables as $table) {
976 $table = new XMLDBTable($table);
977 if (table_exists($table)) {
978 drop_table($table);
979 }
980 }
981
982
983 /// Define table grade_outcomes to be created
984 $table = new XMLDBTable('grade_outcomes');
985
986 /// Adding fields to table grade_outcomes
987 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
988 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
989 $table->addFieldInfo('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
990 $table->addFieldInfo('fullname', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
991 $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
678a79ca 992 $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
42ff9ce6 993 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
994 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
995 $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
996
997 /// Adding keys to table grade_outcomes
998 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
999 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1000 $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id'));
1001 $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
1002
1003 /// Launch create table for grade_outcomes
1004 $result = $result && create_table($table);
1005
1006
42ff9ce6 1007 /// Define table grade_categories to be created
1008 $table = new XMLDBTable('grade_categories');
1009
1010 /// Adding fields to table grade_categories
1011 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1012 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1013 $table->addFieldInfo('parent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1014 $table->addFieldInfo('depth', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1015 $table->addFieldInfo('path', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1016 $table->addFieldInfo('fullname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1017 $table->addFieldInfo('aggregation', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1018 $table->addFieldInfo('keephigh', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1019 $table->addFieldInfo('droplow', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
c2efb501 1020 $table->addFieldInfo('aggregateonlygraded', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
29d509f5 1021 $table->addFieldInfo('aggregateoutcomes', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
c2efb501 1022 $table->addFieldInfo('aggregatesubcats', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
42ff9ce6 1023 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1024 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1025
1026 /// Adding keys to table grade_categories
1027 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1028 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1029 $table->addKeyInfo('parent', XMLDB_KEY_FOREIGN, array('parent'), 'grade_categories', array('id'));
1030
1031 /// Launch create table for grade_categories
1032 $result = $result && create_table($table);
1033
61c33818 1034
b3ac6c3e 1035 /// Define table grade_items to be created
1036 $table = new XMLDBTable('grade_items');
61c33818 1037
b3ac6c3e 1038 /// Adding fields to table grade_items
197ba19c 1039 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
b3ac6c3e 1040 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1041 $table->addFieldInfo('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1042 $table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1043 $table->addFieldInfo('itemtype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null);
1044 $table->addFieldInfo('itemmodule', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null);
1045 $table->addFieldInfo('iteminstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1046 $table->addFieldInfo('itemnumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1047 $table->addFieldInfo('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1048 $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1049 $table->addFieldInfo('calculation', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1050 $table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1');
1051 $table->addFieldInfo('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
1052 $table->addFieldInfo('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1053 $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
abd30252 1054 $table->addFieldInfo('outcomeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
b3ac6c3e 1055 $table->addFieldInfo('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1056 $table->addFieldInfo('multfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '1.0');
1057 $table->addFieldInfo('plusfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
cea2542b 1058 $table->addFieldInfo('aggregationcoef', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
b3ac6c3e 1059 $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
d9266304 1060 $table->addFieldInfo('display', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1061 $table->addFieldInfo('decimals', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null);
b3ac6c3e 1062 $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1063 $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1064 $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
b3ac6c3e 1065 $table->addFieldInfo('needsupdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1066 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1067 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
61c33818 1068
b3ac6c3e 1069 /// Adding keys to table grade_items
1070 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1071 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1072 $table->addKeyInfo('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'grade_categories', array('id'));
1073 $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id'));
1074 $table->addKeyInfo('outcomeid', XMLDB_KEY_FOREIGN, array('outcomeid'), 'grade_outcomes', array('id'));
61c33818 1075
173403cf 1076 /// Adding indexes to table grade_grades
6c9f6725 1077 $table->addIndexInfo('locked-locktime', XMLDB_INDEX_NOTUNIQUE, array('locked', 'locktime'));
90274c73 1078 $table->addIndexInfo('itemtype-needsupdate', XMLDB_INDEX_NOTUNIQUE, array('itemtype', 'needsupdate'));
1079 $table->addIndexInfo('gradetype', XMLDB_INDEX_NOTUNIQUE, array('gradetype'));
173403cf 1080
b3ac6c3e 1081 /// Launch create table for grade_items
1082 $result = $result && create_table($table);
f92dcad8 1083
f92dcad8 1084
b3ac6c3e 1085 /// Define table grade_grades to be created
1086 $table = new XMLDBTable('grade_grades');
f92dcad8 1087
b3ac6c3e 1088 /// Adding fields to table grade_grades
197ba19c 1089 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
b3ac6c3e 1090 $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1091 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1092 $table->addFieldInfo('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
1093 $table->addFieldInfo('rawgrademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
1094 $table->addFieldInfo('rawgrademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1095 $table->addFieldInfo('rawscaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1096 $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1097 $table->addFieldInfo('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
1098 $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1099 $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1100 $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1101 $table->addFieldInfo('exported', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
98353ff1 1102 $table->addFieldInfo('overridden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
23207a1a 1103 $table->addFieldInfo('excluded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
3f2b0c8a 1104 $table->addFieldInfo('feedback', XMLDB_TYPE_TEXT, 'medium', XMLDB_UNSIGNED, null, null, null, null, null);
1105 $table->addFieldInfo('feedbackformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1106 $table->addFieldInfo('information', XMLDB_TYPE_TEXT, 'medium', XMLDB_UNSIGNED, null, null, null, null, null);
1107 $table->addFieldInfo('informationformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
b3ac6c3e 1108 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1109 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
f92dcad8 1110
b3ac6c3e 1111 /// Adding keys to table grade_grades
1112 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1113 $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
1114 $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1115 $table->addKeyInfo('rawscaleid', XMLDB_KEY_FOREIGN, array('rawscaleid'), 'scale', array('id'));
1116 $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
f92dcad8 1117
173403cf 1118 /// Adding indexes to table grade_grades
6c9f6725 1119 $table->addIndexInfo('locked-locktime', XMLDB_INDEX_NOTUNIQUE, array('locked', 'locktime'));
173403cf 1120
b3ac6c3e 1121 /// Launch create table for grade_grades
1122 $result = $result && create_table($table);
f92dcad8 1123
61c33818 1124
42ff9ce6 1125 /// Define table grade_outcomes_history to be created
1126 $table = new XMLDBTable('grade_outcomes_history');
b3ac6c3e 1127
42ff9ce6 1128 /// Adding fields to table grade_outcomes_history
197ba19c 1129 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
abd30252 1130 $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1131 $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
42ff9ce6 1132 $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1133 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1134 $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
b3ac6c3e 1135 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1136 $table->addFieldInfo('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1137 $table->addFieldInfo('fullname', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
1138 $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
678a79ca 1139 $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
b3ac6c3e 1140
42ff9ce6 1141 /// Adding keys to table grade_outcomes_history
b3ac6c3e 1142 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
42ff9ce6 1143 $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_outcomes', array('id'));
b3ac6c3e 1144 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1145 $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id'));
42ff9ce6 1146 $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id'));
b3ac6c3e 1147
42ff9ce6 1148 /// Adding indexes to table grade_outcomes_history
1149 $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
b3ac6c3e 1150
42ff9ce6 1151 /// Launch create table for grade_outcomes_history
1152 $result = $result && create_table($table);
61c33818 1153
f13002d5 1154
42ff9ce6 1155 /// Define table grade_categories_history to be created
1156 $table = new XMLDBTable('grade_categories_history');
f13002d5 1157
42ff9ce6 1158 /// Adding fields to table grade_categories_history
1159 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
abd30252 1160 $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1161 $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
42ff9ce6 1162 $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1163 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1164 $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1165 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1166 $table->addFieldInfo('parent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1167 $table->addFieldInfo('depth', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1168 $table->addFieldInfo('path', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1169 $table->addFieldInfo('fullname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1170 $table->addFieldInfo('aggregation', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1171 $table->addFieldInfo('keephigh', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1172 $table->addFieldInfo('droplow', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
c2efb501 1173 $table->addFieldInfo('aggregateonlygraded', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
29d509f5 1174 $table->addFieldInfo('aggregateoutcomes', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
c2efb501 1175 $table->addFieldInfo('aggregatesubcats', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
4a129c47 1176
42ff9ce6 1177 /// Adding keys to table grade_categories_history
1178 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1179 $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_categories', array('id'));
1180 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1181 $table->addKeyInfo('parent', XMLDB_KEY_FOREIGN, array('parent'), 'grade_categories', array('id'));
1182 $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id'));
aaff71da 1183
42ff9ce6 1184 /// Adding indexes to table grade_categories_history
1185 $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
aaff71da 1186
42ff9ce6 1187 /// Launch create table for grade_categories_history
1188 $result = $result && create_table($table);
aaff71da 1189
1190
1191 /// Define table grade_items_history to be created
1192 $table = new XMLDBTable('grade_items_history');
1193
1194 /// Adding fields to table grade_items_history
197ba19c 1195 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
abd30252 1196 $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1197 $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
aaff71da 1198 $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1199 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
42ff9ce6 1200 $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
aaff71da 1201 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1202 $table->addFieldInfo('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1203 $table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1204 $table->addFieldInfo('itemtype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null);
1205 $table->addFieldInfo('itemmodule', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null);
1206 $table->addFieldInfo('iteminstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1207 $table->addFieldInfo('itemnumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1208 $table->addFieldInfo('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1209 $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1210 $table->addFieldInfo('calculation', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
1211 $table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1');
1212 $table->addFieldInfo('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
1213 $table->addFieldInfo('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1214 $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
abd30252 1215 $table->addFieldInfo('outcomeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
aaff71da 1216 $table->addFieldInfo('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1217 $table->addFieldInfo('multfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '1.0');
1218 $table->addFieldInfo('plusfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
cea2542b 1219 $table->addFieldInfo('aggregationcoef', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
aaff71da 1220 $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
d9266304 1221 $table->addFieldInfo('display', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1222 $table->addFieldInfo('decimals', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null);
aaff71da 1223 $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1224 $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1225 $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1226 $table->addFieldInfo('needsupdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
1227
1228 /// Adding keys to table grade_items_history
1229 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1230 $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_items', array('id'));
1231 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1232 $table->addKeyInfo('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'grade_categories', array('id'));
1233 $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id'));
1234 $table->addKeyInfo('outcomeid', XMLDB_KEY_FOREIGN, array('outcomeid'), 'grade_outcomes', array('id'));
42ff9ce6 1235 $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id'));
aaff71da 1236
1237 /// Adding indexes to table grade_items_history
1238 $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
1239
1240 /// Launch create table for grade_items_history
1241 $result = $result && create_table($table);
1242
1243
aaff71da 1244 /// Define table grade_grades_history to be created
1245 $table = new XMLDBTable('grade_grades_history');
1246
1247 /// Adding fields to table grade_grades_history
197ba19c 1248 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
abd30252 1249 $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1250 $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
aaff71da 1251 $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1252 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
42ff9ce6 1253 $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
aaff71da 1254 $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1255 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1256 $table->addFieldInfo('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
1257 $table->addFieldInfo('rawgrademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
1258 $table->addFieldInfo('rawgrademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
1259 $table->addFieldInfo('rawscaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1260 $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1261 $table->addFieldInfo('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
1262 $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1263 $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1264 $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1265 $table->addFieldInfo('exported', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
98353ff1 1266 $table->addFieldInfo('overridden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
23207a1a 1267 $table->addFieldInfo('excluded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
3f2b0c8a 1268 $table->addFieldInfo('feedback', XMLDB_TYPE_TEXT, 'medium', XMLDB_UNSIGNED, null, null, null, null, null);
1269 $table->addFieldInfo('feedbackformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1270 $table->addFieldInfo('information', XMLDB_TYPE_TEXT, 'medium', XMLDB_UNSIGNED, null, null, null, null, null);
1271 $table->addFieldInfo('informationformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
aaff71da 1272
1273 /// Adding keys to table grade_grades_history
1274 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1275 $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_grades', array('id'));
1276 $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
1277 $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1278 $table->addKeyInfo('rawscaleid', XMLDB_KEY_FOREIGN, array('rawscaleid'), 'scale', array('id'));
1279 $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
42ff9ce6 1280 $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id'));
aaff71da 1281
1282 /// Adding indexes to table grade_grades_history
1283 $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
1284
1285 /// Launch create table for grade_grades_history
1286 $result = $result && create_table($table);
1287
42ff9ce6 1288 /// upgrade the old 1.8 gradebook - migrade data into new grade tables
1289 if ($result) {
1290 require_once($CFG->libdir.'/db/upgradelib.php');
1291 if ($rs = get_recordset('course')) {
03cedd62 1292 while ($course = rs_fetch_next_record($rs)) {
1293 // this function uses SQL only, it must not be changed after 1.9 goes stable!!
1294 if (!upgrade_18_gradebook($course->id)) {
1295 $result = false;
1296 break;
42ff9ce6 1297 }
1298 }
1299 rs_close($rs);
1300 }
cea2542b 1301 }
cea2542b 1302 }
1303
b264019b 1304 if ($result && $oldversion < 2007072400) {
1305 /// Dropping one DEFAULT in a TEXT column. It's was only one remaining
1306 /// since Moodle 1.7, so new servers won't have those anymore.
1307
1308 /// Changing the default of field sessdata on table sessions2 to drop it
1309 $table = new XMLDBTable('sessions2');
1310 $field = new XMLDBField('sessdata');
1311 $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'modified');
1312
1313 /// Launch change of default for field sessdata
1314 $result = $result && change_field_default($table, $field);
1315 }
1316
521d54ec 1317
1318 if ($result && $oldversion < 2007073100) {
271e6dec 1319 /// Define table grade_outcomes_courses to be created
1320 $table = new XMLDBTable('grade_outcomes_courses');
1321
1322 /// Adding fields to table grade_outcomes_courses
abd30252 1323 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
271e6dec 1324 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1325 $table->addFieldInfo('outcomeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1326
1327 /// Adding keys to table grade_outcomes_courses
1328 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1329 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1330 $table->addKeyInfo('outcomeid', XMLDB_KEY_FOREIGN, array('outcomeid'), 'grade_outcomes', array('id'));
f37cc4bb 1331 $table->addKeyInfo('courseid-outcomeid', XMLDB_KEY_UNIQUE, array('courseid', 'outcomeid'));
271e6dec 1332 /// Launch create table for grade_outcomes_courses
e480c18c 1333 $result = $result && create_table($table);
1334
e99c48d8 1335 }
173a9d21 1336
79642064 1337
1338 if ($result && $oldversion < 2007073101) { // Add new tag tables
1339
1340 /// Define table tag to be created
1341 $table = new XMLDBTable('tag');
1342
1343 /// Adding fields to table tag
1344 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1345 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null, null, null);
1346 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1347 $table->addFieldInfo('tagtype', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1348 $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
1349 $table->addFieldInfo('descriptionformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null, null, null);
1350 $table->addFieldInfo('flag', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0');
1351 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1352
1353 /// Adding keys to table tag
1354 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1355
1356 /// Adding indexes to table tag
1357 $table->addIndexInfo('name', XMLDB_INDEX_UNIQUE, array('name'));
1358
1359 /// Launch create table for tag
1360 $result = $result && create_table($table);
1361
1362
1363
1364 /// Define table tag_correlation to be created
1365 $table = new XMLDBTable('tag_correlation');
1366
1367 /// Adding fields to table tag_correlation
1368 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1369 $table->addFieldInfo('tagid', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null, null, null);
1370 $table->addFieldInfo('correlatedtags', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
1371
1372 /// Adding keys to table tag_correlation
1373 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1374
1375 /// Adding indexes to table tag_correlation
1376 $table->addIndexInfo('tagid', XMLDB_INDEX_UNIQUE, array('tagid'));
1377
1378 /// Launch create table for tag_correlation
1379 $result = $result && create_table($table);
1380
1381
1382
1383 /// Define table tag_instance to be created
1384 $table = new XMLDBTable('tag_instance');
1385
1386 /// Adding fields to table tag_instance
1387 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1388 $table->addFieldInfo('tagid', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null, null, null);
1389 $table->addFieldInfo('itemtype', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1390 $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null, null, null);
1391
1392 /// Adding keys to table tag_instance
1393 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1394
1395 /// Adding indexes to table tag_instance
1396 $table->addIndexInfo('tagiditem', XMLDB_INDEX_NOTUNIQUE, array('tagid', 'itemtype', 'itemid'));
1397
1398 /// Launch create table for tag_instance
1399 $result = $result && create_table($table);
1400
1401 }
1402
1403
705789bd 1404 if ($result && $oldversion < 2007073103) {
1405
1406 /// Define field rawname to be added to tag
1407 $table = new XMLDBTable('tag');
1408 $field = new XMLDBField('rawname');
1409 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'name');
1410
1411 /// Launch add field rawname
1412 $result = $result && add_field($table, $field);
1413 }
1414
678a79ca 1415 if ($result && $oldversion < 2007073105) {
1416
1417 /// Define field description to be added to grade_outcomes
1418 $table = new XMLDBTable('grade_outcomes');
1419 $field = new XMLDBField('description');
1420 if (!field_exists($table, $field)) {
1421 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, 'scaleid');
1422 /// Launch add field description
1423 $result = $result && add_field($table, $field);
1424 }
1425
1426 $table = new XMLDBTable('grade_outcomes_history');
1427 $field = new XMLDBField('description');
1428 if (!field_exists($table, $field)) {
1429 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, 'scaleid');
1430 /// Launch add field description
1431 $result = $result && add_field($table, $field);
1432 }
678a79ca 1433 }
271e6dec 1434
1f496ded 1435 // adding unique contraint on (courseid,shortname) of an outcome
1436 if ($result && $oldversion < 2007080100) {
1437
1438 /// Define key courseid-shortname (unique) to be added to grade_outcomes
1439 $table = new XMLDBTable('grade_outcomes');
1440 $key = new XMLDBKey('courseid-shortname');
1441 $key->setAttributes(XMLDB_KEY_UNIQUE, array('courseid', 'shortname'));
705789bd 1442
1f496ded 1443 /// Launch add key courseid-shortname
1444 $result = $result && add_key($table, $key);
1445 }
705789bd 1446
7d0c81b3 1447 /// originally there was supportname and supportemail upgrade code - this is handled in upgradesettings.php instead
271e6dec 1448
1a85d89a 1449 if ($result && $oldversion < 2007080202) {
32ef9149 1450
1451 /// Define index tagiditem (not unique) to be dropped form tag_instance
1452 $table = new XMLDBTable('tag_instance');
1453 $index = new XMLDBIndex('tagiditem');
1a85d89a 1454 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('tagid', 'itemtype', 'itemid'));
32ef9149 1455
1456 /// Launch drop index tagiditem
1457 drop_index($table, $index);
1458
1459 /// Define index tagiditem (unique) to be added to tag_instance
1a85d89a 1460 $table = new XMLDBTable('tag_instance');
1461 $index = new XMLDBIndex('tagiditem');
32ef9149 1462 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('tagid', 'itemtype', 'itemid'));
1463
1464 /// Launch add index tagiditem
1465 $result = $result && add_index($table, $index);
1466
1467 }
1468
29d509f5 1469 if ($result && $oldversion < 2007080300) {
1470
1471 /// Define field aggregateoutcomes to be added to grade_categories
1472 $table = new XMLDBTable('grade_categories');
1473 $field = new XMLDBField('aggregateoutcomes');
1474 if (!field_exists($table, $field)) {
1475 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'droplow');
1476
1477 /// Launch add field aggregateoutcomes
1478 $result = $result && add_field($table, $field);
1479 }
1480
1481 /// Define field aggregateoutcomes to be added to grade_categories
1482 $table = new XMLDBTable('grade_categories_history');
1483 $field = new XMLDBField('aggregateoutcomes');
1484 if (!field_exists($table, $field)) {
1485 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'droplow');
1486
1487 /// Launch add field aggregateoutcomes
1488 $result = $result && add_field($table, $field);
1489 }
1490 }
32ef9149 1491
341426f0 1492 if ($result && $oldversion < 2007080800) { /// Normalize course->shortname MDL-10026
1493
1494 /// Changing precision of field shortname on table course to (100)
1495 $table = new XMLDBTable('course');
1496 $field = new XMLDBField('shortname');
1497 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null, 'fullname');
1498
1499 /// Launch change of precision for field shortname
1500 $result = $result && change_field_precision($table, $field);
1501 }
1502
c7c57cfb 1503 if ($result && $oldversion < 2007080900) {
1504 /// Add context.path & index
1505 $table = new XMLDBTable('context');
1506 $field = new XMLDBField('path');
7d0c81b3 1507 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'instanceid');
c7c57cfb 1508 $result = $result && add_field($table, $field);
1509 $table = new XMLDBTable('context');
1510 $index = new XMLDBIndex('path');
1511 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('path'));
1512 $result = $result && add_index($table, $index);
1513
1514 /// Add context.depth
1515 $table = new XMLDBTable('context');
1516 $field = new XMLDBField('depth');
1517 $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'path');
1518 $result = $result && add_field($table, $field);
7d0c81b3 1519
1520 /// make sure the system context has proper data
1521 get_system_context(false);
c7c57cfb 1522 }
1523
6c9f6725 1524 if ($result && $oldversion < 2007080903) {
173403cf 1525 /// Define index
1526 $table = new XMLDBTable('grade_grades');
6c9f6725 1527 $index = new XMLDBIndex('locked-locktime');
1528 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('locked', 'locktime'));
173403cf 1529
1530 if (!index_exists($table, $index)) {
1531 /// Launch add index
1532 $result = $result && add_index($table, $index);
1533 }
1534
1535 /// Define index
1536 $table = new XMLDBTable('grade_items');
6c9f6725 1537 $index = new XMLDBIndex('locked-locktime');
1538 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('locked', 'locktime'));
173403cf 1539
1540 if (!index_exists($table, $index)) {
1541 /// Launch add index
1542 $result = $result && add_index($table, $index);
1543 }
1544
90274c73 1545 /// Define index itemtype-needsupdate (not unique) to be added to grade_items
1546 $table = new XMLDBTable('grade_items');
1547 $index = new XMLDBIndex('itemtype-needsupdate');
1548 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('itemtype', 'needsupdate'));
1549 if (!index_exists($table, $index)) {
1550 /// Launch add index itemtype-needsupdate
1551 $result = $result && add_index($table, $index);
1552 }
1553
173403cf 1554 /// Define index
1555 $table = new XMLDBTable('grade_items');
90274c73 1556 $index = new XMLDBIndex('gradetype');
1557 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('gradetype'));
173403cf 1558
1559 if (!index_exists($table, $index)) {
1560 /// Launch add index
1561 $result = $result && add_index($table, $index);
1562 }
1563
173403cf 1564 }
1565
2524b0f2 1566 if ($result && $oldversion < 2007081000) {
1567 require_once($CFG->dirroot . '/question/upgrade.php');
1568 $result = $result && question_upgrade_context_etc();
1569 }
1570
1571 if ($result && $oldversion < 2007081302) {
1572 require_once($CFG->libdir.'/db/upgradelib.php');
1573
1574 if (table_exists(new XMLDBTable('groups_groupings'))) {
1575 /// IF 'groups_groupings' table exists, this is for 1.8.* only.
1576 $result = $result && upgrade_18_groups();
1577
1578 } else {
1579 /// ELSE, 1.7.*/1.6.*/1.5.* - create 'groupings' and 'groupings_groups' + rename password to enrolmentkey
1580 $result = $result && upgrade_17_groups();
1581 }
1582
1583 /// For both 1.8.* and 1.7.*/1.6.*..
1584
1585 // delete not used fields
1586 $table = new XMLDBTable('groups');
1587 $field = new XMLDBField('theme');
1588 if (field_exists($table, $field)) {
1589 drop_field($table, $field);
1590 }
1591 $table = new XMLDBTable('groups');
1592 $field = new XMLDBField('lang');
1593 if (field_exists($table, $field)) {
1594 drop_field($table, $field);
1595 }
1596
1597 /// Add groupingid field/f.key to 'course' table.
1598 $table = new XMLDBTable('course');
1599 $field = new XMLDBField('defaultgroupingid');
1600 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='groupmodeforce');
1601 $result = $result && add_field($table, $field);
1602
1603
1604 /// Add grouping ID, grouponly field/f.key to 'course_modules' table.
1605 $table = new XMLDBTable('course_modules');
1606 $field = new XMLDBField('groupingid');
1607 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='groupmode');
1608 $result = $result && add_field($table, $field);
1609
1610 $table = new XMLDBTable('course_modules');
1611 $field = new XMLDBField('groupmembersonly');
1612 $field->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $prev='groupingid');
1613 $result = $result && add_field($table, $field);
1614
1615 $table = new XMLDBTable('course_modules');
1616 $key = new XMLDBKey('groupingid');
1617 $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupingid'), 'groupings', array('id'));
1618 $result = $result && add_key($table, $key);
1619
1620 }
1621
1acd661d 1622 if ($result && $oldversion < 2007082300) {
1623
1624 /// Define field ordering to be added to tag_instance table
1625 $table = new XMLDBTable('tag_instance');
1626 $field = new XMLDBField('ordering');
e480c18c 1627
1acd661d 1628 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'itemid');
1629
1630 /// Launch add field rawname
1631 $result = $result && add_field($table, $field);
e480c18c 1632 }
2524b0f2 1633
a4232e95 1634 if ($result && $oldversion < 2007082700) {
1635
1636 /// Define field timemodified to be added to tag_instance
1637 $table = new XMLDBTable('tag_instance');
1638 $field = new XMLDBField('timemodified');
1639 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'ordering');
1640
1641 /// Launch add field timemodified
1642 $result = $result && add_field($table, $field);
1643 }
e480c18c 1644
535c0080 1645 /// migrate all tags table to tag - this code MUST use SQL only,
1646 /// because if the db structure changes the library functions will fail in future
bd1f4559 1647 if ($result && $oldversion < 2007082701) {
bd1f4559 1648 $tagrefs = array(); // $tagrefs[$oldtagid] = $newtagid
535c0080 1649 if ($rs = get_recordset('tags')) {
1650 $db->debug = false;
03cedd62 1651 while ($oldtag = rs_fetch_next_record($rs)) {
1652 $raw_normalized = clean_param($oldtag->text, PARAM_TAG);
1653 $normalized = moodle_strtolower($raw_normalized);
1654 // if this tag does not exist in tag table yet
1655 if (!$newtag = get_record('tag', 'name', $normalized, '', '', '', '', 'id')) {
1656 $itag = new object();
1657 $itag->name = $normalized;
1658 $itag->rawname = $raw_normalized;
1659 $itag->userid = $oldtag->userid;
1660 $itag->timemodified = time();
1661 $itag->descriptionformat = 0; // default format
1662 if ($oldtag->type == 'official') {
1663 $itag->tagtype = 'official';
535c0080 1664 } else {
03cedd62 1665 $itag->tagtype = 'default';
bd1f4559 1666 }
03cedd62 1667
1668 if ($idx = insert_record('tag', $itag)) {
1669 $tagrefs[$oldtag->id] = $idx;
1670 }
1671 // if this tag is already used by tag table
1672 } else {
1673 $tagrefs[$oldtag->id] = $newtag->id;
bd1f4559 1674 }
1675 }
535c0080 1676 $db->debug = true;
1677 rs_close($rs);
bd1f4559 1678 }
e480c18c 1679
bd1f4559 1680 // fetch all the tag instances and migrate them as well
535c0080 1681 if ($rs = get_recordset('blog_tag_instance')) {
1682 $db->debug = false;
03cedd62 1683 while ($blogtag = rs_fetch_next_record($rs)) {
1684 if (array_key_exists($blogtag->tagid, $tagrefs)) {
1685 $tag_instance = new object();
1686 $tag_instance->tagid = $tagrefs[$blogtag->tagid];
1687 $tag_instance->itemtype = 'blog';
1688 $tag_instance->itemid = $blogtag->entryid;
1689 $tag_instance->ordering = 1; // does not matter much, because originally there was no ordering in blogs
1690 $tag_instance->timemodified = time();
1691 insert_record('tag_instance', $tag_instance);
bd1f4559 1692 }
1693 }
535c0080 1694 $db->debug = true;
1695 rs_close($rs);
bd1f4559 1696 }
1697
535c0080 1698 unset($tagrefs); // release memory
1699
bd1f4559 1700 $table = new XMLDBTable('tags');
1701 drop_table($table);
1702 $table = new XMLDBTable('blog_tag_instance');
1703 drop_table($table);
1704 }
e480c18c 1705
c4956945 1706 /// MDL-11015, MDL-11016
1707 if ($result && $oldversion < 2007082800) {
1708
1709 /// Changing type of field userid on table tag to int
1710 $table = new XMLDBTable('tag');
1711 $field = new XMLDBField('userid');
1712 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'id');
1713
1714 /// Launch change of type for field userid
1715 $result = $result && change_field_type($table, $field);
e480c18c 1716
c4956945 1717 /// Changing type of field descriptionformat on table tag to int
1718 $table = new XMLDBTable('tag');
1719 $field = new XMLDBField('descriptionformat');
1720 $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'description');
1721
1722 /// Launch change of type for field descriptionformat
1723 $result = $result && change_field_type($table, $field);
e480c18c 1724
c4956945 1725 /// Define key userid (foreign) to be added to tag
1726 $table = new XMLDBTable('tag');
1727 $key = new XMLDBKey('userid');
1728 $key->setAttributes(XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1729
1730 /// Launch add key userid
1731 $result = $result && add_key($table, $key);
e480c18c 1732
c4956945 1733 /// Define index tagiditem (unique) to be dropped form tag_instance
1734 $table = new XMLDBTable('tag_instance');
1735 $index = new XMLDBIndex('tagiditem');
1736 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('tagid', 'itemtype', 'itemid'));
1737
1738 /// Launch drop index tagiditem
1739 $result = $result && drop_index($table, $index);
e480c18c 1740
c4956945 1741 /// Changing type of field tagid on table tag_instance to int
1742 $table = new XMLDBTable('tag_instance');
1743 $field = new XMLDBField('tagid');
1744 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'id');
1745
1746 /// Launch change of type for field tagid
1747 $result = $result && change_field_type($table, $field);
e480c18c 1748
c4956945 1749 /// Define key tagid (foreign) to be added to tag_instance
1750 $table = new XMLDBTable('tag_instance');
1751 $key = new XMLDBKey('tagid');
1752 $key->setAttributes(XMLDB_KEY_FOREIGN, array('tagid'), 'tag', array('id'));
1753
1754 /// Launch add key tagid
e480c18c 1755 $result = $result && add_key($table, $key);
c4956945 1756
1757 /// Changing sign of field itemid on table tag_instance to unsigned
1758 $table = new XMLDBTable('tag_instance');
1759 $field = new XMLDBField('itemid');
1760 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'itemtype');
1761
1762 /// Launch change of sign for field itemid
1763 $result = $result && change_field_unsigned($table, $field);
e480c18c 1764
c4956945 1765 /// Changing sign of field ordering on table tag_instance to unsigned
1766 $table = new XMLDBTable('tag_instance');
1767 $field = new XMLDBField('ordering');
1768 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'itemid');
1769
1770 /// Launch change of sign for field ordering
e480c18c 1771 $result = $result && change_field_unsigned($table, $field);
1772
c4956945 1773 /// Define index itemtype-itemid-tagid (unique) to be added to tag_instance
1774 $table = new XMLDBTable('tag_instance');
1775 $index = new XMLDBIndex('itemtype-itemid-tagid');
1776 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
1777
1778 /// Launch add index itemtype-itemid-tagid
1779 $result = $result && add_index($table, $index);
e480c18c 1780
c4956945 1781 /// Define index tagid (unique) to be dropped form tag_correlation
1782 $table = new XMLDBTable('tag_correlation');
1783 $index = new XMLDBIndex('tagid');
1784 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('tagid'));
1785
1786 /// Launch drop index tagid
e480c18c 1787 $result = $result && drop_index($table, $index);
1788
c4956945 1789 /// Changing type of field tagid on table tag_correlation to int
1790 $table = new XMLDBTable('tag_correlation');
1791 $field = new XMLDBField('tagid');
1792 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'id');
1793
1794 /// Launch change of type for field tagid
1795 $result = $result && change_field_type($table, $field);
e480c18c 1796
1797
c4956945 1798 /// Define key tagid (foreign) to be added to tag_correlation
1799 $table = new XMLDBTable('tag_correlation');
1800 $key = new XMLDBKey('tagid');
1801 $key->setAttributes(XMLDB_KEY_FOREIGN, array('tagid'), 'tag', array('id'));
1802
1803 /// Launch add key tagid
1804 $result = $result && add_key($table, $key);
e480c18c 1805
1806 }
61c6071f 1807
1808
1809 if ($result && $oldversion < 2007082801) {
1810
1811 /// Define table user_private_key to be created
1812 $table = new XMLDBTable('user_private_key');
1813
1814 /// Adding fields to table user_private_key
1815 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1816 $table->addFieldInfo('script', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null, null, null);
1817 $table->addFieldInfo('value', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null, null, null);
1818 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1819 $table->addFieldInfo('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1820 $table->addFieldInfo('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
1821 $table->addFieldInfo('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1822 $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1823
1824 /// Adding keys to table user_private_key
1825 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1826 $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1827
1828 /// Adding indexes to table user_private_key
1829 $table->addIndexInfo('script-value', XMLDB_INDEX_NOTUNIQUE, array('script', 'value'));
1830
1831 /// Launch create table for user_private_key
1832 $result = $result && create_table($table);
1833 }
1834
4149e289 1835/// Going to modify the applicationid from int(1) to int(10). Dropping and
1836/// re-creating the associated keys/indexes is mandatory to be cross-db. MDL-11042
1837 if ($result && $oldversion < 2007082803) {
1838
1839 /// Define key applicationid (foreign) to be dropped form mnet_host
1840 $table = new XMLDBTable('mnet_host');
1841 $key = new XMLDBKey('applicationid');
1842 $key->setAttributes(XMLDB_KEY_FOREIGN, array('applicationid'), 'mnet_application', array('id'));
1843
1844 /// Launch drop key applicationid
1845 $result = $result && drop_key($table, $key);
1846
1847 /// Changing type of field applicationid on table mnet_host to int
1848 $field = new XMLDBField('applicationid');
1849 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'last_log_id');
1850
1851 /// Launch change of type for field applicationid
1852 $result = $result && change_field_type($table, $field);
1853
1854 /// Define key applicationid (foreign) to be added to mnet_host
1855 $key = new XMLDBKey('applicationid');
1856 $key->setAttributes(XMLDB_KEY_FOREIGN, array('applicationid'), 'mnet_application', array('id'));
1857
1858 /// Launch add key applicationid
1859 $result = $result && add_key($table, $key);
1860
1861 }
1862
c2efb501 1863 if ($result && $oldversion < 2007090503) {
c2efb501 1864 /// Define field aggregatesubcats to be added to grade_categories
1865 $table = new XMLDBTable('grade_categories');
1866 $field = new XMLDBField('aggregatesubcats');
1867 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'aggregateoutcomes');
1868
1869 if (!field_exists($table, $field)) {
1870 /// Launch add field aggregateonlygraded
1871 $result = $result && add_field($table, $field);
1872 }
1873
1874 /// Define field aggregateonlygraded to be added to grade_categories
1875 $table = new XMLDBTable('grade_categories');
1876 $field = new XMLDBField('aggregateonlygraded');
1877 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'droplow');
1878
1879 if (!field_exists($table, $field)) {
1880 /// Launch add field aggregateonlygraded
1881 $result = $result && add_field($table, $field);
1882 }
1883
1884 /// Define field aggregatesubcats to be added to grade_categories_history
1885 $table = new XMLDBTable('grade_categories_history');
1886 $field = new XMLDBField('aggregatesubcats');
1887 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'aggregateoutcomes');
1888
1889 if (!field_exists($table, $field)) {
1890 /// Launch add field aggregateonlygraded
1891 $result = $result && add_field($table, $field);
1892 }
1893
1894 /// Define field aggregateonlygraded to be added to grade_categories_history
1895 $table = new XMLDBTable('grade_categories_history');
1896 $field = new XMLDBField('aggregateonlygraded');
1897 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'droplow');
1898
1899 if (!field_exists($table, $field)) {
1900 /// Launch add field aggregateonlygraded
1901 $result = $result && add_field($table, $field);
1902 }
1903
1904 /// upgrade path in grade_categrories table - now using slash on both ends
1905 $concat = sql_concat('path', "'/'");
1906 $sql = "UPDATE {$CFG->prefix}grade_categories SET path = $concat WHERE path NOT LIKE '/%/'";
1907 execute_sql($sql, true);
1908
1909 /// convert old aggregation constants if needed
1910 for ($i=0; $i<=12; $i=$i+2) {
1911 $j = $i+1;
1912 $sql = "UPDATE {$CFG->prefix}grade_categories SET aggregation = $i, aggregateonlygraded = 1 WHERE aggregation = $j";
1913 execute_sql($sql, true);
1914 }
1915 }
1916
4a039315 1917/// To have UNIQUE indexes over NULLable columns isn't cross-db at all
1918/// so we create a non unique index and programatically enforce uniqueness
1919 if ($result && $oldversion < 2007090600) {
1920
1921 /// Define index idnumber (unique) to be dropped form course_modules
1922 $table = new XMLDBTable('course_modules');
1923 $index = new XMLDBIndex('idnumber');
1924 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('idnumber'));
1925
1926 /// Launch drop index idnumber
1927 $result = $result && drop_index($table, $index);
1928
1929 /// Define index idnumber-course (not unique) to be added to course_modules
1930 $table = new XMLDBTable('course_modules');
1931 $index = new XMLDBIndex('idnumber-course');
1932 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('idnumber', 'course'));
1933
1934 /// Launch add index idnumber-course
1935 $result = $result && add_index($table, $index);
1936
1937 /// Define index idnumber-courseid (not unique) to be added to grade_items
1938 $table = new XMLDBTable('grade_items');
1939 $index = new XMLDBIndex('idnumber-courseid');
1940 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('idnumber', 'courseid'));
1941
1942 /// Launch add index idnumber-courseid
1943 $result = $result && add_index($table, $index);
1944
1945 }
c2efb501 1946
d5d2f8a6 1947/// Create the permanent context_temp table to be used by build_context_path()
1948 if ($result && $oldversion < 2007092001) {
1949
1950 /// Define table context_temp to be created
1951 $table = new XMLDBTable('context_temp');
1952
1953 /// Adding fields to table context_temp
1954 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1955 $table->addFieldInfo('path', XMLDB_TYPE_CHAR, '255', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1956 $table->addFieldInfo('depth', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1957
1958 /// Adding keys to table context_temp
1959 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
1960
1961 /// Launch create table for context_temp
1962 $result = $result && create_table($table);
1963
1964 /// Recalculate depths, paths and so on
7d0c81b3 1965 if (!empty($CFG->rolesactive)) {
1966 cleanup_contexts();
8a4d32d8 1967 // make sure all course, category and user contexts exist - we need it for grade letter upgrade, etc.
1968 create_contexts(CONTEXT_COURSE, false, true);
1969 create_contexts(CONTEXT_USER, false, true);
1970 // we need all contexts path/depths filled properly
1971 build_context_path(true, true);
7d0c81b3 1972 load_all_capabilities();
8a4d32d8 1973
1974 } else {
1975 // upgrade from 1.6 - build all contexts
1976 create_contexts(null, true, true);
7d0c81b3 1977 }
c24f4599 1978 }
1979
3f2b0c8a 1980 /**
1981 * Merging of grade_grades_text back into grade_grades
1982 */
1983 if ($result && $oldversion < 2007092002) {
1984
3f2b0c8a 1985 /// Define field feedback to be added to grade_grades
1986 $table = new XMLDBTable('grade_grades');
1987 $field = new XMLDBField('feedback');
1988 $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', XMLDB_UNSIGNED, null, null, null, null, null, 'excluded');
1989
1990 if (!field_exists($table, $field)) {
1991 /// Launch add field feedback
1992 $result = $result && add_field($table, $field);
1993 }
1994
1995 /// Define field feedbackformat to be added to grade_grades
1996 $table = new XMLDBTable('grade_grades');
1997 $field = new XMLDBField('feedbackformat');
1998 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'feedback');
1999
2000 if (!field_exists($table, $field)) {
2001 /// Launch add field feedbackformat
2002 $result = $result && add_field($table, $field);
2003 }
2004
2005 /// Define field information to be added to grade_grades
2006 $table = new XMLDBTable('grade_grades');
2007 $field = new XMLDBField('information');
2008 $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', XMLDB_UNSIGNED, null, null, null, null, null, 'feedbackformat');
2009
2010 if (!field_exists($table, $field)) {
2011 /// Launch add field information
2012 $result = $result && add_field($table, $field);
2013 }
2014
2015 /// Define field informationformat to be added to grade_grades
2016 $table = new XMLDBTable('grade_grades');
2017 $field = new XMLDBField('informationformat');
2018 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'information');
2019
2020 if (!field_exists($table, $field)) {
2021 /// Launch add field informationformat
2022 $result = $result && add_field($table, $field);
2023 }
2024
2025 /// Define field feedback to be added to grade_grades_history
2026 $table = new XMLDBTable('grade_grades_history');
2027 $field = new XMLDBField('feedback');
2028 $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', XMLDB_UNSIGNED, null, null, null, null, null, 'excluded');
2029
2030 if (!field_exists($table, $field)) {
2031 /// Launch add field feedback
2032 $result = $result && add_field($table, $field);
2033 }
2034
2035 /// Define field feedbackformat to be added to grade_grades_history
2036 $table = new XMLDBTable('grade_grades_history');
2037 $field = new XMLDBField('feedbackformat');
2038 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'feedback');
2039
2040 if (!field_exists($table, $field)) {
2041 /// Launch add field feedbackformat
2042 $result = $result && add_field($table, $field);
2043 }
2044
2045 /// Define field information to be added to grade_grades_history
2046 $table = new XMLDBTable('grade_grades_history');
2047 $field = new XMLDBField('information');
2048 $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', XMLDB_UNSIGNED, null, null, null, null, null, 'feedbackformat');
2049
2050 if (!field_exists($table, $field)) {
2051 /// Launch add field information
2052 $result = $result && add_field($table, $field);
2053 }
2054
2055 /// Define field informationformat to be added to grade_grades_history
2056 $table = new XMLDBTable('grade_grades_history');
2057 $field = new XMLDBField('informationformat');
2058 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'information');
2059
2060 if (!field_exists($table, $field)) {
2061 /// Launch add field informationformat
2062 $result = $result && add_field($table, $field);
2063 }
2064
2065 $table = new XMLDBTable('grade_grades_text');
2066 if ($result and table_exists($table)) {
2067 //migrade existing data into grade_grades table - this is slow but works for all dbs,
2068 //it will be executed on development sites only
2069 $fields = array('feedback', 'information');
2070 foreach ($fields as $field) {
2071 $sql = "UPDATE {$CFG->prefix}grade_grades
2072 SET $field = (
2073 SELECT $field
2074 FROM {$CFG->prefix}grade_grades_text ggt
2075 WHERE ggt.gradeid = {$CFG->prefix}grade_grades.id)";
2076 $result = execute_sql($sql) && $result;
2077 }
2078 $fields = array('feedbackformat', 'informationformat');
2079 foreach ($fields as $field) {
2080 $sql = "UPDATE {$CFG->prefix}grade_grades
2081 SET $field = COALESCE((
2082 SELECT $field
2083 FROM {$CFG->prefix}grade_grades_text ggt
2084 WHERE ggt.gradeid = {$CFG->prefix}grade_grades.id), 0)";
2085 $result = execute_sql($sql) && $result;
2086 }
2087
2088 if ($result) {
2089 $tables = array('grade_grades_text', 'grade_grades_text_history');
52cb2e47 2090
3f2b0c8a 2091 foreach ($tables as $table) {
2092 $table = new XMLDBTable($table);
2093 if (table_exists($table)) {
2094 drop_table($table);
2095 }
2096 }
2097 }
2098 }
2099 }
c2efb501 2100
d9266304 2101 if ($result && $oldversion < 2007092803) {
b39b58fe 2102
d9266304 2103/// Remove obsoleted unit tests tables - they will be recreated automatically
2104 $tables = array('grade_categories',
2105 'scale',
2106 'grade_items',
2107 'grade_calculations',
2108 'grade_grades',
2109 'grade_grades_raw',
2110 'grade_grades_final',
2111 'grade_grades_text',
2112 'grade_outcomes',
2113 'grade_outcomes_courses');
2114
2115 foreach ($tables as $tablename) {
2116 $table = new XMLDBTable('unittest_'.$tablename);
2117 if (table_exists($table)) {
2118 drop_table($table);
2119 }
2120 $table = new XMLDBTable('unittest_'.$tablename.'_history');
2121 if (table_exists($table)) {
2122 drop_table($table);
2123 }
2124 }
2125
2126 /// Define field display to be added to grade_items
2127 $table = new XMLDBTable('grade_items');
2128 $field = new XMLDBField('display');
2129 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0', 'sortorder');
2130
2131 /// Launch add field display
2132 if (!field_exists($table, $field)) {
2133 $result = $result && add_field($table, $field);
2134 } else {
2135 $result = $result && change_field_default($table, $field);
2136 }
2137
2138 /// Define field display to be added to grade_items_history
2139 $table = new XMLDBTable('grade_items_history');
2140 $field = new XMLDBField('display');
2141 $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0', 'sortorder');
2142
2143 /// Launch add field display
2144 if (!field_exists($table, $field)) {
2145 $result = $result && add_field($table, $field);
2146 }
2147
2148
2149 /// Define field decimals to be added to grade_items
b39b58fe 2150 $table = new XMLDBTable('grade_items');
2151 $field = new XMLDBField('decimals');
2152 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null, 'display');
2153
d9266304 2154 /// Launch add field decimals
2155 if (!field_exists($table, $field)) {
2156 $result = $result && add_field($table, $field);
2157 } else {
2158 $result = $result && change_field_default($table, $field);
2159 $result = $result && change_field_notnull($table, $field);
2160 }
b39b58fe 2161
d9266304 2162 /// Define field decimals to be added to grade_items_history
2163 $table = new XMLDBTable('grade_items_history');
b39b58fe 2164 $field = new XMLDBField('decimals');
2165 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null, 'display');
2166
d9266304 2167 /// Launch add field decimals
2168 if (!field_exists($table, $field)) {
2169 $result = $result && add_field($table, $field);
2170 }
b39b58fe 2171
284abb09 2172
d9266304 2173 /// fix incorrect -1 default for grade_item->display
2174 execute_sql("UPDATE {$CFG->prefix}grade_items SET display=0 WHERE display=-1");
5cf5e181 2175 }
2176
8a4d32d8 2177/// migrade grade letters - we can not do this in normal grades upgrade becuase we need all course contexts
5cf5e181 2178 if ($result && $oldversion < 2007092806) {
2179 require_once($CFG->libdir.'/db/upgradelib.php');
2180
8a4d32d8 2181 $result = upgrade_18_letters();
284abb09 2182
5cf5e181 2183 /// Define index contextidlowerboundary (not unique) to be added to grade_letters
2184 $table = new XMLDBTable('grade_letters');
2185 $index = new XMLDBIndex('contextid-lowerboundary');
2186 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
2187
2188 /// Launch add index contextidlowerboundary
2189 if (!index_exists($table, $index)) {
2190 $result = $result && add_index($table, $index);
2191 }
284abb09 2192 }
2193
e4a5c864 2194 if ($result && $oldversion < 2007100100) {
2195
e4a5c864 2196 /// Define table cache_flags to be created
2197 $table = new XMLDBTable('cache_flags');
2198
2199 /// Adding fields to table cache_flags
2200 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
2201 $table->addFieldInfo('flagtype', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
2202 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
2203 $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
2204 $table->addFieldInfo('value', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null);
2205 $table->addFieldInfo('expiry', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2206
2207 /// Adding keys to table cache_flags
2208 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
2209
266f6b59 2210 /*
2211 * Note: mysql can not create indexes on text fields larger than 333 chars!
2212 */
2213
e4a5c864 2214 /// Adding indexes to table cache_flags
266f6b59 2215 $table->addIndexInfo('flagtype', XMLDB_INDEX_NOTUNIQUE, array('flagtype'));
2216 $table->addIndexInfo('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
e4a5c864 2217
2218 /// Launch create table for cache_flags
266f6b59 2219 if (!table_exists($table)) {
2220 $result = $result && create_table($table);
c2efb501 2221 }
2222 }
c2efb501 2223
c57be126 2224
38d507af 2225 if ($oldversion < 2007100300) {
c57be126 2226 /// MNET stuff for roaming theme
2227 /// Define field force_theme to be added to mnet_host
ac4e96fc 2228 $table = new XMLDBTable('mnet_host');
ac4e96fc 2229 $field = new XMLDBField('force_theme');
c57be126 2230 $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'last_log_id');
ac4e96fc 2231
c57be126 2232 /// Launch add field force_theme
ac4e96fc 2233 $result = $result && add_field($table, $field);
2234
c57be126 2235 /// Define field theme to be added to mnet_host
2236 $table = new XMLDBTable('mnet_host');
ac4e96fc 2237 $field = new XMLDBField('theme');
2238 $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null, 'force_theme');
2239
c57be126 2240 /// Launch add field theme
ac4e96fc 2241 $result = $result && add_field($table, $field);
2242 }
2243
266f6b59 2244 if ($result && $oldversion < 2007100301) {
2245
2246 /// Define table cache_flags to be created
2247 $table = new XMLDBTable('cache_flags');
2248 $index = new XMLDBIndex('typename');
2249 if (index_exists($table, $index)) {
2250 $result = $result && drop_index($table, $index);
2251 }
2252
2253 $table = new XMLDBTable('cache_flags');
2254 $index = new XMLDBIndex('flagtype');
2255 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('flagtype'));
2256 if (!index_exists($table, $index)) {
2257 $result = $result && add_index($table, $index);
2258 }
2259
2260 $table = new XMLDBTable('cache_flags');
2261 $index = new XMLDBIndex('name');
2262 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('name'));
2263 if (!index_exists($table, $index)) {
2264 $result = $result && add_index($table, $index);
2265 }
2266 }
2267
c626c2f4 2268 if ($result && $oldversion < 2007100303) {
2269
2270 /// Changing nullability of field summary on table course to null
2271 $table = new XMLDBTable('course');
2272 $field = new XMLDBField('summary');
2273 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, 'idnumber');
2274
2275 /// Launch change of nullability for field summary
2276 $result = $result && change_field_notnull($table, $field);
2277 }
266f6b59 2278
7d0c81b3 2279 if ($result && $oldversion < 2007100500) {
2280 /// for dev sites - it is ok to do this repeatedly
2281
2282 /// Changing nullability of field path on table context to null
2283 $table = new XMLDBTable('context');
2284 $field = new XMLDBField('path');
2285 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'instanceid');
2286
2287 /// Launch change of nullability for field path
2288 $result = $result && change_field_notnull($table, $field);
2289 }
2290
8108909a 2291 if ($result && $oldversion < 2007100700) {
2292
2293 /// first drop existing tables - we do not need any data from there
2294 $table = new XMLDBTable('grade_import_values');
2295 if (table_exists($table)) {
2296 drop_table($table);
2297 }
2298
2299 $table = new XMLDBTable('grade_import_newitem');
2300 if (table_exists($table)) {
2301 drop_table($table);
2302 }
2303
2304 /// Define table grade_import_newitem to be created
2305 $table = new XMLDBTable('grade_import_newitem');
2306
2307 /// Adding fields to table grade_import_newitem
2308 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
2309 $table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2310 $table->addFieldInfo('importcode', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2311 $table->addFieldInfo('importer', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2312
2313 /// Adding keys to table grade_import_newitem
2314 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
2315 $table->addKeyInfo('importer', XMLDB_KEY_FOREIGN, array('importer'), 'user', array('id'));
2316
2317 /// Launch create table for grade_import_newitem
2318 $result = $result && create_table($table);
2319
2320
2321 /// Define table grade_import_values to be created
2322 $table = new XMLDBTable('grade_import_values');
2323
2324 /// Adding fields to table grade_import_values
2325 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
2326 $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
2327 $table->addFieldInfo('newgradeitem', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
2328 $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2329 $table->addFieldInfo('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
2330 $table->addFieldInfo('feedback', XMLDB_TYPE_TEXT, 'medium', XMLDB_UNSIGNED, null, null, null, null, null);
2331 $table->addFieldInfo('importcode', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2332 $table->addFieldInfo('importer', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
2333
2334 /// Adding keys to table grade_import_values
2335 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
2336 $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
2337 $table->addKeyInfo('newgradeitem', XMLDB_KEY_FOREIGN, array('newgradeitem'), 'grade_import_newitem', array('id'));
2338 $table->addKeyInfo('importer', XMLDB_KEY_FOREIGN, array('importer'), 'user', array('id'));
2339
2340 /// Launch create table for grade_import_values
2341 $result = $result && create_table($table);
2342
2343 }
2344
e0724506 2345 if ($result && $oldversion < 2007100803) {
2346
2347 /// Define table grade_settings to be created
2348 $table = new XMLDBTable('grade_settings');
2349
2350 /// Adding fields to table grade_settings
2351 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
2352 $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2353 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
2354 $table->addFieldInfo('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
2355
2356 /// Adding keys to table grade_settings
2357 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
2358 $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
2359
2360 /// Adding indexes to table grade_settings
2361 $table->addIndexInfo('courseid-name', XMLDB_INDEX_UNIQUE, array('courseid', 'name'));
2362
2363 /// Launch create table for grade_settings
2364 $result = $result && create_table($table);
2365 }
2366
eeccaea1 2367/// dropping context_rel table - not used anymore
a506e40c 2368 if ($result && $oldversion < 2007100800) {
266f6b59 2369
a506e40c 2370 /// Define table context_rel to be dropped
2371 $table = new XMLDBTable('context_rel');
2372
2373 /// Launch drop table for context_rel
eeccaea1 2374 if (table_exists($table)) {
2375 drop_table($table);
2376 }
a506e40c 2377 }
266f6b59 2378
cef1ea8e 2379/// Truncate the text_cahe table and add new index
2380 if ($result && $oldversion < 2007100802) {
2381
2382 /// Truncate the cache_text table
2383 execute_sql("TRUNCATE TABLE {$CFG->prefix}cache_text", true);
2384
2385 /// Define index timemodified (not unique) to be added to cache_text
2386 $table = new XMLDBTable('cache_text');
2387 $index = new XMLDBIndex('timemodified');
2388 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
2389
2390 /// Launch add index timemodified
2391 $result = $result && add_index($table, $index);
2392 }
2393
8600cada 2394/// cleanup in user_lastaccess
2395 if ($result && $oldversion < 2007100902) {
2396 $sql = "DELETE
2397 FROM {$CFG->prefix}user_lastaccess
2398 WHERE NOT EXISTS (SELECT 'x'
2399 FROM {$CFG->prefix}course c
2400 WHERE c.id = {$CFG->prefix}user_lastaccess.courseid)";
2401 execute_sql($sql);
2402 }
2403
eeccaea1 2404/// drop old gradebook tables
2405 if ($result && $oldversion < 2007100903) {
8600cada 2406 $tables = array('grade_category',
2407 'grade_item',
2408 'grade_letter',
2409 'grade_preferences',
2410 'grade_exceptions');
2411
2412 foreach ($tables as $table) {
2413 $table = new XMLDBTable($table);
2414 if (table_exists($table)) {
2415 drop_table($table);
2416 }
2417 }
2418 }
eeccaea1 2419
2420
8600cada 2421
ac9b0805 2422 return $result;
4e423cbf 2423}
271e6dec 2424
2425
4e423cbf 2426?>