Fixed the upgrade from 1.6.x. The role->shortname field was not being created.
[moodle.git] / lib / db / postgres7.php
CommitLineData
31f0900c 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// Versions are defined by /version.php
17//
18// This file is tailored to PostgreSQL 7
19
e7311a0a 20function main_upgrade($oldversion=0) {
5867bfb5 21
4d744a22 22 global $CFG, $THEME, $db;
5867bfb5 23
e40488f8 24 $result = true;
5867bfb5 25
26
db70b54b 27 if ($oldversion < 2003010101) {
28 delete_records("log_display", "module", "user");
29 $new->module = "user";
30 $new->action = "view";
31 $new->mtable = "user";
32 $new->field = "CONCAT(firstname,\" \",lastname)";
33 insert_record("log_display", $new);
34
35 delete_records("log_display", "module", "course");
36 $new->module = "course";
37 $new->action = "view";
38 $new->mtable = "course";
39 $new->field = "fullname";
40 insert_record("log_display", $new);
41 $new->action = "update";
42 insert_record("log_display", $new);
43 $new->action = "enrol";
44 insert_record("log_display", $new);
45 }
1924074c 46
047d30d1 47 //support user based course creating
1924074c 48 if ($oldversion < 2003032400) {
a273084d 49 execute_sql("CREATE TABLE {$CFG->prefix}user_coursecreators (
1924074c 50 id int8 SERIAL PRIMARY KEY,
51 userid int8 NOT NULL default '0'
52 )");
047d30d1 53 }
1acfbce5 54
047d30d1 55 if ($oldversion < 2003041400) {
1acfbce5 56 table_column("course_modules", "", "visible", "integer", "1", "unsigned", "1", "not null", "score");
57 }
718ad19f 58
047d30d1 59 if ($oldversion < 2003042104) { // Try to update permissions of all files
718ad19f 60 if ($files = get_directory_list($CFG->dataroot)) {
61 echo "Attempting to update permissions for all files... ignore any errors.";
62 foreach ($files as $file) {
839f2456 63 echo "$CFG->dataroot/$file<br />";
b369ff55 64 @chmod("$CFG->dataroot/$file", $CFG->directorypermissions);
718ad19f 65 }
66 }
67 }
68
13df5aee 69 if ($oldversion < 2003042400) {
70 // Rebuild all course caches, because of changes to do with visible variable
71 if ($courses = get_records_sql("SELECT * FROM {$CFG->prefix}course")) {
72 require_once("$CFG->dirroot/course/lib.php");
73 foreach ($courses as $course) {
74 $modinfo = serialize(get_array_of_activities($course->id));
75
76 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
77 notify("Could not cache module information for course '$course->fullname'!");
78 }
79 }
80 }
81 }
31f0900c 82
047d30d1 83 if ($oldversion < 2003042500) {
84 // Convert all usernames to lowercase.
85 $users = get_records_sql("SELECT id, username FROM {$CFG->prefix}user");
86 $cerrors = "";
87 $rarray = array();
88
89 foreach ($users as $user) { // Check for possible conflicts
90 $lcname = trim(moodle_strtolower($user->username));
91 if (in_array($lcname, $rarray)) {
e40488f8 92 $cerrors .= $user->id."->".$lcname.'<br/>' ;
047d30d1 93 } else {
94 array_push($rarray,$lcname);
95 }
e40488f8 96 }
047d30d1 97
e40488f8 98 if ($cerrors != '') {
047d30d1 99 notify("Error: Cannot convert usernames to lowercase.
100 Following usernames would overlap (id->username):<br/> $cerrors .
101 Please resolve overlapping errors.");
e40488f8 102 $result = false;
047d30d1 103 }
104
105 $cerrors = "";
839f2456 106 echo "Checking userdatabase:<br />";
047d30d1 107 foreach ($users as $user) {
108 $lcname = trim(moodle_strtolower($user->username));
109 if ($lcname != $user->username) {
110 $convert = set_field("user" , "username" , $lcname, "id", $user->id);
111 if (!$convert) {
e40488f8 112 if ($cerrors){
113 $cerrors .= ", ";
114 }
115 $cerrors .= $item;
047d30d1 116 } else {
117 echo ".";
e40488f8 118 }
119 }
047d30d1 120 }
121 if ($cerrors != '') {
122 notify("There were errors when converting following usernames to lowercase.
123 '$cerrors' . Sorry, but you will need to fix your database by hand.");
124 $result = false;
e40488f8 125 }
126 }
047d30d1 127
e209e0b0 128 if ($oldversion < 2003042700) {
129 /// Changing to multiple indexes
ea563f8f 130 execute_sql(" CREATE INDEX {$CFG->prefix}log_coursemoduleaction_idx ON {$CFG->prefix}log (course,module,action) ");
131 execute_sql(" CREATE INDEX {$CFG->prefix}log_courseuserid_idx ON {$CFG->prefix}log (course,userid) ");
e209e0b0 132 }
b86fc0e2 133
134 if ($oldversion < 2003042801) {
135 execute_sql("CREATE TABLE {$CFG->prefix}course_display (
136 id SERIAL PRIMARY KEY,
137 course integer NOT NULL default '0',
138 userid integer NOT NULL default '0',
139 display integer NOT NULL default '0'
140 )");
141
ea563f8f 142 execute_sql("CREATE INDEX {$CFG->prefix}course_display_courseuserid_idx ON {$CFG->prefix}course_display (course,userid)");
b86fc0e2 143 }
7d99d695 144
145 if ($oldversion < 2003050400) {
146 table_column("course_sections", "", "visible", "integer", "1", "unsigned", "1", "", "");
147 }
e209e0b0 148
465fd00e 149 if ($oldversion < 2003050401) {
150 table_column("user", "", "lang", "VARCHAR", "5", "", "$CFG->lang" ,"NOT NULL","");
151 }
7e6b0b3b 152
153 if ($oldversion < 2003050900) {
154 table_column("modules", "", "visible", "integer", "1", "unsigned", "1", "", "");
155 }
156
5867bfb5 157 if ($oldversion < 2003050902) {
158 if (get_records("modules", "name", "pgassignment")) {
159 print_simple_box("Note: the pgassignment module will soon be deleted from CVS! Go to the new 'Manage Modules' page and DELETE IT from your system", "center", "50%", "$THEME->cellheading", "20", "noticebox");
160 }
161 }
162
7adf6787 163 if ($oldversion < 2003051600) {
164 print_simple_box("Thanks for upgrading!<p>There are many changes since the last release. Please read the release notes carefully. If you are using CUSTOM themes you will need to edit them. You will also need to check your site's config.php file.", "center", "50%", "$THEME->cellheading", "20", "noticebox");
165 }
166
7f2a3e67 167 if ($oldversion < 2003052300) {
168 table_column("user", "", "autosubscribe", "integer", "1", "unsigned", "1", "", "htmleditor");
169 }
170
a6d82c3f 171 if ($oldversion < 2003072100) {
172 table_column("course", "", "visible", "integer", "1", "unsigned", "1", "", "marker");
173 }
174
dd0bd508 175 if ($oldversion < 2003072101) {
176 table_column("course_sections", "sequence", "sequence", "text", "", "", "", "", "");
177 }
178
a8fa25d1 179 if ($oldversion < 2003072800) {
180 print_simple_box("The following database index improves performance, but can be quite large - if you are upgrading and you have problems with a limited quota you may want to delete this index later from the '{$CFG->prefix}log' table in your database", "center", "50%", "$THEME->cellheading", "20", "noticebox");
1dbb6e50 181 flush();
a8fa25d1 182 execute_sql(" CREATE INDEX {$CFG->prefix}log_timecoursemoduleaction_idx ON {$CFG->prefix}log (time,course,module,action) ");
183 execute_sql(" CREATE INDEX {$CFG->prefix}user_students_courseuserid_idx ON {$CFG->prefix}user_students (course,userid) ");
184 execute_sql(" CREATE INDEX {$CFG->prefix}user_teachers_courseuserid_idx ON {$CFG->prefix}user_teachers (course,userid) ");
185 }
186
c2cb4545 187 if ($oldversion < 2003072802) {
188 table_column("course_categories", "", "description", "text", "", "", "");
189 table_column("course_categories", "", "parent", "integer", "10", "unsigned");
190 table_column("course_categories", "", "sortorder", "integer", "10", "unsigned");
191 table_column("course_categories", "", "courseorder", "text", "", "", "");
192 table_column("course_categories", "", "visible", "integer", "1", "unsigned", "1");
193 table_column("course_categories", "", "timemodified", "integer", "10", "unsigned");
194 }
195
d2b6ba70 196 if ($oldversion < 2003080400) {
c7ce6d80 197 notify("If the following command fails you may want to change the type manually, from TEXT to INTEGER. Moodle should keep working even if you don't.");
d2b6ba70 198 table_column("course_categories", "courseorder", "courseorder", "integer", "10", "unsigned");
199 table_column("course", "", "sortorder", "integer", "10", "unsigned", "0", "", "category");
200 }
201
a142b3b4 202 if ($oldversion < 2003081502) {
203 execute_sql(" CREATE TABLE {$CFG->prefix}scale (
204 id SERIAL PRIMARY KEY,
205 courseid integer NOT NULL default '0',
206 userid integer NOT NULL default '0',
207 name varchar(255) NOT NULL default '',
208 scale text,
209 description text,
210 timemodified integer NOT NULL default '0'
211 )");
212 }
213
11402bbd 214 if ($oldversion < 2003081503) {
6f4f04df 215 table_column("forum", "", "scale", "integer", "10", "unsigned", "0", "", "assessed");
11402bbd 216 get_scales_menu(0); // Just to force the default scale to be created
217 }
218
73047f2f 219 if ($oldversion < 2003081600) {
220 table_column("user_teachers", "", "editall", "integer", "1", "unsigned", "1", "", "role");
221 table_column("user_teachers", "", "timemodified", "integer", "10", "unsigned", "0", "", "editall");
222 }
50b5487c 223
9936fe81 224 if ($oldversion < 2003081900) {
225 table_column("course_categories", "courseorder", "coursecount", "integer", "10", "unsigned", "0");
226 }
227
f63823b3 228 if ($oldversion < 2003080700) {
229 notify("Cleaning up categories and course ordering...");
6bc502cc 230 fix_course_sortorder();
f63823b3 231 }
232
233
fff79722 234 if ($oldversion < 2003082001) {
235 table_column("course", "", "showgrades", "integer", "2", "unsigned", "1", "", "format");
236 }
237
3052e775 238 if ($oldversion < 2003082101) {
239 execute_sql(" CREATE INDEX {$CFG->prefix}course_category_idx ON {$CFG->prefix}course (category) ");
240 }
d0117715 241 if ($oldversion < 2003082702) {
242 execute_sql(" INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'user report', 'user', 'CONCAT(firstname,\" \",lastname)') ");
243 }
3052e775 244
e71f132d 245 if ($oldversion < 2003091000) {
246 # Old field that was never added!
247 table_column("course", "", "showrecent", "integer", "10", "unsigned", "1", "", "numsections");
248 }
249
aac94fd0 250 if ($oldversion < 2003091400) {
251 table_column("course_modules", "", "indent", "integer", "5", "unsigned", "0", "", "score");
252 }
253
4909e176 254 if ($oldversion < 2003092900) {
255 table_column("course", "", "maxbytes", "integer", "10", "unsigned", "0", "", "marker");
256 }
aac94fd0 257
4d744a22 258 if ($oldversion < 2003102700) {
259 table_column("user_students", "", "timeaccess", "integer", "10", "unsigned", "0", "", "time");
260 table_column("user_teachers", "", "timeaccess", "integer", "10", "unsigned", "0", "", "timemodified");
261
4d744a22 262 $db->debug = false;
29b25b04 263 $CFG->debug = 0;
264 notify("Calculating access times. Please wait - this may take a long time on big sites...", "green");
265 flush();
266
267 if ($courses = get_records_select("course", "category > 0")) {
268 foreach ($courses as $course) {
269 notify("Processing $course->fullname ...", "green");
270 flush();
271 if ($users = get_records_select("user_teachers", "course = '$course->id'",
272 "id", "id, userid, timeaccess")) {
273 foreach ($users as $user) {
274 $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log WHERE course = '$course->id' and userid = '$user->userid' ORDER by time DESC");
275 if (empty($loginfo->time)) {
276 $loginfo->time = 0;
277 }
278 execute_sql("UPDATE {$CFG->prefix}user_teachers SET timeaccess = '$loginfo->time'
279 WHERE userid = '$user->userid' AND course = '$course->id'", false);
280
281 }
282 }
283
284 if ($users = get_records_select("user_students", "course = '$course->id'",
285 "id", "id, userid, timeaccess")) {
286 foreach ($users as $user) {
287 $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log
288 WHERE course = '$course->id' and userid = '$user->userid'
289 ORDER by time DESC");
290 if (empty($loginfo->time)) {
291 $loginfo->time = 0;
292 }
293 execute_sql("UPDATE {$CFG->prefix}user_students
294 SET timeaccess = '$loginfo->time'
295 WHERE userid = '$user->userid' AND course = '$course->id'", false);
296
297 }
298 }
299 }
4d744a22 300 }
29b25b04 301 notify("All courses complete.", "green");
4d744a22 302 $db->debug = true;
303 }
304
3f125001 305 if ($oldversion < 2003103100) {
306 table_column("course", "", "showreports", "integer", "4", "unsigned", "0", "", "maxbytes");
307 }
308
4d744a22 309
f374fb10 310 if ($oldversion < 2003121600) {
0da33e07 311 execute_sql("CREATE TABLE {$CFG->prefix}groups (
f374fb10 312 id SERIAL PRIMARY KEY,
313 courseid integer NOT NULL default '0',
314 name varchar(255) NOT NULL default '',
315 description text,
316 lang varchar(10) NOT NULL default '',
317 picture integer NOT NULL default '0',
318 timecreated integer NOT NULL default '0',
319 timemodified integer NOT NULL default '0'
320 )");
321
0da33e07 322 execute_sql("CREATE INDEX {$CFG->prefix}groups_idx ON {$CFG->prefix}groups (courseid) ");
f374fb10 323
0da33e07 324 execute_sql("CREATE TABLE {$CFG->prefix}groups_members (
f374fb10 325 id SERIAL PRIMARY KEY,
326 groupid integer NOT NULL default '0',
327 userid integer NOT NULL default '0',
328 timeadded integer NOT NULL default '0'
329 )");
330
0da33e07 331 execute_sql("CREATE INDEX {$CFG->prefix}groups_members_idx ON {$CFG->prefix}groups_members (groupid) ");
f374fb10 332 }
333
334 if ($oldversion < 2003122600) {
335 table_column("course", "", "groupmode", "integer", "4", "unsigned", "0", "", "visible");
336 table_column("course", "", "groupmodeforce", "integer", "4", "unsigned", "0", "", "groupmode");
337 }
338
ddc7afad 339 if ($oldversion < 2004010900) {
340 table_column("course_modules", "", "groupmode", "integer", "4", "unsigned", "0", "", "visible");
341 }
342
5fba04fb 343 if ($oldversion < 2004011700) {
344 modify_database("", "CREATE TABLE prefix_event (
345 id SERIAL PRIMARY KEY,
346 name varchar(255) NOT NULL default '',
347 description text,
348 courseid integer NOT NULL default '0',
349 groupid integer NOT NULL default '0',
350 userid integer NOT NULL default '0',
351 modulename varchar(20) NOT NULL default '',
352 instance integer NOT NULL default '0',
353 eventtype varchar(20) NOT NULL default '',
354 timestart integer NOT NULL default '0',
355 timeduration integer NOT NULL default '0',
356 timemodified integer NOT NULL default '0'
357 ); ");
358
359 modify_database("", "CREATE INDEX prefix_event_courseid_idx ON prefix_event (courseid);");
360 modify_database("", "CREATE INDEX prefix_event_userid_idx ON prefix_event (userid);");
361 }
362
363
70812e39 364 if ($oldversion < 2004012800) {
365 modify_database("", "CREATE TABLE prefix_user_preferences (
366 id SERIAL PRIMARY KEY,
367 userid integer NOT NULL default '0',
368 name varchar(50) NOT NULL default '',
369 value varchar(255) NOT NULL default ''
370 ); ");
371
372 modify_database("", "CREATE INDEX prefix_user_preferences_useridname_idx ON prefix_user_preferences (userid,name);");
373 }
374
ba39fa10 375 if ($oldversion < 2004012900) {
376 table_column("config", "value", "value", "text", "", "", "");
377 }
69d79bc3 378
379 if ($oldversion < 2004013101) {
380 table_column("log", "", "cmid", "integer", "10", "unsigned", "0", "", "module");
4da1a0a1 381 set_config("upgrade", "logs");
69d79bc3 382 }
95d45757 383
b3153e4b 384 if ($oldversion < 2004020900) {
385 table_column("course", "", "lang", "varchar", "5", "", "", "", "groupmodeforce");
386 }
387
45121ffb 388 if ($oldversion < 2004020903) {
389 modify_database("", "CREATE TABLE prefix_cache_text (
d363047e 390 id SERIAL PRIMARY KEY,
391 md5key varchar(32) NOT NULL default '',
392 formattedtext text,
393 timemodified integer NOT NULL default '0'
394 );");
395 }
396
d523d2ea 397 if ($oldversion < 2004021000) {
398 $textfilters = array();
399 for ($i=1; $i<=10; $i++) {
400 $variable = "textfilter$i";
401 if (!empty($CFG->$variable)) { /// No more filters
402 if (is_readable("$CFG->dirroot/".$CFG->$variable)) {
403 $textfilters[] = $CFG->$variable;
404 }
405 }
406 }
407 $textfilters = implode(',', $textfilters);
408 if (empty($textfilters)) {
409 $textfilters = 'mod/glossary/dynalink.php';
410 }
411 set_config('textfilters', $textfilters);
412 }
413
f1d604cb 414 if ($oldversion < 2004021201) {
415 modify_database("", "CREATE TABLE prefix_cache_filters (
416 id SERIAL PRIMARY KEY,
417 filter varchar(32) NOT NULL default '',
418 version integer NOT NULL default '0',
419 md5key varchar(32) NOT NULL default '',
420 rawtext text,
421 timemodified integer NOT NULL default '0'
422 );");
423
424 modify_database("", "CREATE INDEX prefix_cache_filters_filtermd5key_idx ON prefix_cache_filters (filter,md5key);");
425 modify_database("", "CREATE INDEX prefix_cache_text_md5key_idx ON prefix_cache_text (md5key);");
426 }
d523d2ea 427
3c0561cf 428 if ($oldversion < 2004021500) {
429 table_column("groups", "", "hidepicture", "integer", "2", "unsigned", "0", "", "picture");
430 }
431
9e353ce7 432 if ($oldversion < 2004021700) {
433 if (!empty($CFG->textfilters)) {
434 $CFG->textfilters = str_replace("tex_filter.php", "filter.php", $CFG->textfilters);
435 $CFG->textfilters = str_replace("multilang.php", "filter.php", $CFG->textfilters);
436 $CFG->textfilters = str_replace("censor.php", "filter.php", $CFG->textfilters);
437 $CFG->textfilters = str_replace("mediaplugin.php", "filter.php", $CFG->textfilters);
438 $CFG->textfilters = str_replace("algebra_filter.php", "filter.php", $CFG->textfilters);
828aeff2 439 $CFG->textfilters = str_replace("dynalink.php", "filter.php", $CFG->textfilters);
9e353ce7 440 set_config("textfilters", $CFG->textfilters);
441 }
442 }
443
8199e3f0 444 if ($oldversion < 2004022000) {
445 table_column("user", "", "emailstop", "integer", "1", "unsigned", "0", "not null", "email");
446 }
447
828aeff2 448 if ($oldversion < 2004022200) { /// Final renaming I hope. :-)
449 if (!empty($CFG->textfilters)) {
450 $CFG->textfilters = str_replace("/filter.php", "", $CFG->textfilters);
a4dda51f 451 $CFG->textfilters = str_replace("mod/glossary/dynalink.php", "mod/glossary", $CFG->textfilters);
828aeff2 452 $textfilters = explode(',', $CFG->textfilters);
453 foreach ($textfilters as $key => $textfilter) {
454 $textfilters[$key] = trim($textfilter);
455 }
456 set_config("textfilters", implode(',',$textfilters));
457 }
458 }
459
4bf58667 460 if ($oldversion < 2004030702) { /// Because of the renaming of Czech language pack
461 execute_sql("UPDATE {$CFG->prefix}user SET lang = 'cs' WHERE lang = 'cz'");
462 execute_sql("UPDATE {$CFG->prefix}course SET lang = 'cs' WHERE lang = 'cz'");
7277e306 463 }
464
0f3fe4b6 465 if ($oldversion < 2004041800) { /// Integrate Block System from contrib
466 table_column("course", "", "blockinfo", "varchar", "255", "", "", "not null", "modinfo");
467 }
468
822ede92 469 if ($oldversion < 2004042600) { /// Rebuild course caches for resource icons
470 include_once("$CFG->dirroot/course/lib.php");
471 rebuild_course_cache();
472 }
473
cda21d48 474 if ($oldversion < 2004042700) { /// Increase size of lang fields
37aaf074 475 table_column("user", "lang", "lang", "varchar", "10", "", "en");
476 table_column("groups", "lang", "lang", "varchar", "10", "", "");
477 table_column("course", "lang", "lang", "varchar", "10", "", "");
478 }
479
009cc726 480 if ($oldversion < 2004042701) { /// Add hiddentopics field to control hidden topics behaviour
464fd803 481 #table_column("course", "", "hiddentopics", "integer", "1", "unsigned", "0", "not null", "visible");
482 #See 'hiddensections' further down
009cc726 483 }
484
cda21d48 485 if ($oldversion < 2004042702) { /// Add a format field for the description
486 table_column("event", "", "format", "integer", "4", "unsigned", "0", "not null", "description");
487 }
488
464fd803 489 if ($oldversion < 2004043001) { /// Add hiddentopics field to control hidden topics behaviour
490 table_column("course", "", "hiddensections", "integer", "2", "unsigned", "0", "not null", "visible");
19a40309 491 }
dcd338ff 492
493 if ($oldversion < 2004050400) { /// add a visible field for events
05ba8bd0 494 table_column("event", "", "visible", "smallint", "1", "", "1", "not null", "timeduration");
dcd338ff 495 if ($events = get_records('event')) {
496 foreach($events as $event) {
497 if ($moduleid = get_field('modules', 'id', 'name', $event->modulename)) {
498 if (get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $event->instance) == 0) {
499 set_field('event', 'visible', 0, 'id', $event->id);
500 }
501 }
502 }
503 }
504 }
19a40309 505
3cac987a 506 if ($oldversion < 2004052800) { /// First version tagged "1.4 development", version.php 1.227
e02c35b2 507 set_config('siteblocksadded', true); /// This will be used later by the block upgrade
3cac987a 508 }
509
62b80756 510 if ($oldversion < 2004053000) { /// set defaults for site course
511 $site = get_site();
a7e07837 512 set_field('course', 'numsections', 0, 'id', $site->id);
62b80756 513 set_field('course', 'groupmodeforce', 1, 'id', $site->id);
514 set_field('course', 'teacher', get_string('administrator'), 'id', $site->id);
515 set_field('course', 'teachers', get_string('administrators'), 'id', $site->id);
516 set_field('course', 'student', get_string('user'), 'id', $site->id);
517 set_field('course', 'students', get_string('users'), 'id', $site->id);
518 }
519
cc21211e 520 if ($oldversion < 2004060100) {
521 set_config('digestmailtime', 0);
522 table_column('user', "", 'maildigest', 'smallint', '1', '', '0', 'not null', 'mailformat');
cc21211e 523 }
524
faef9f7b 525 if ($oldversion < 2004062400) {
526 table_column('user_teachers', "", 'timeend', 'int', '10', 'unsigned', '0', 'not null', 'editall');
527 table_column('user_teachers', "", 'timestart', 'int', '10', 'unsigned', '0', 'not null', 'editall');
528 }
529
838ee71b 530 if ($oldversion < 2004062401) {
531 table_column('course', '', 'idnumber', 'varchar', '100', '', '', 'not null', 'shortname');
532 execute_sql('UPDATE '.$CFG->prefix.'course SET idnumber = shortname'); // By default
533 }
534
8c5c6133 535 if ($oldversion < 2004062600) {
536 table_column('course', '', 'cost', 'varchar', '10', '', '', 'not null', 'lang');
537 }
838ee71b 538
18763fd4 539 if ($oldversion < 2004072900) {
540 table_column('course', '', 'enrolperiod', 'int', '10', 'unsigned', '0', 'not null', 'startdate');
541 }
542
f28db22d 543 if ($oldversion < 2004072901) { // Fixing error in schema
544 if ($record = get_record('log_display', 'module', 'course', 'action', 'update')) {
545 delete_records('log_display', 'module', 'course', 'action', 'update');
4d69a487 546 insert_record('log_display', $record, false);
f28db22d 547 }
548 }
549
9fb216e8 550 if ($oldversion < 2004081200) { // Fixing version errors in some blocks
551 set_field('blocks', 'version', 2004081200, 'name', 'admin');
552 set_field('blocks', 'version', 2004081200, 'name', 'calendar_month');
553 set_field('blocks', 'version', 2004081200, 'name', 'course_list');
554 }
555
4e11ad4f 556 if ($oldversion < 2004081500) { // Adding new "auth" field to user table to allow more flexibility
557 table_column('user', '', 'auth', 'varchar', '20', '', 'manual', 'not null', 'id');
558
559 execute_sql("UPDATE {$CFG->prefix}user SET auth = 'manual'"); // Set everyone to 'manual' to be sure
560
561 if ($admins = get_admins()) { // Set all the NON-admins to whatever the current auth module is
562 $adminlist = array();
563 foreach ($admins as $user) {
564 $adminlist[] = $user->id;
565 }
566 $adminlist = implode(',', $adminlist);
567 execute_sql("UPDATE {$CFG->prefix}user SET auth = '$CFG->auth' WHERE id NOT IN ($adminlist)");
568 }
569 }
c14964b0 570
571 if ($oldversion < 2004082600) {
572 //update auth-fields for external users
573 include_once ($CFG->dirroot."/auth/".$CFG->auth."/lib.php");
574 if (function_exists('auth_get_userlist')) {
575 $externalusers = auth_get_userlist();
576 if (!empty($externalusers)){
577 $externalusers = '\''. implode('\',\'',$externalusers).'\'';
578 execute_sql("UPDATE {$CFG->prefix}user SET auth = '$CFG->auth' WHERE username IN ($externalusers)");
579 }
580 }
581 }
582
3cac987a 583 if ($oldversion < 2004082900) { // Make sure guest is "manual" too.
584 set_field('user', 'auth', 'manual', 'username', 'guest');
585 }
586
d35757eb 587 /* Just commenteed unused fields out
01e2ea5f 588 if ($oldversion < 2004090300) { // Add guid-field used in user syncronization
589 table_column('user', '', 'guid', 'varchar', '128', '', '', '', 'auth');
590 execute_sql("CREATE INDEX {$CFG->prefix}user_auth_guid_idx ON {$CFG->prefix}user (auth, guid)");
591 }
d35757eb 592 */
01e2ea5f 593
d35757eb 594 if ($oldversion < 2004091900) { //Modify idnumber to hold longer keys
595 set_field('user', 'auth', 'manual', 'username', 'guest');
596 table_column('user', 'idnumber', 'idnumber', 'varchar', '64', '', '', '', '');
17f3e7d0 597 execute_sql("DROP INDEX {$CFG->prefix}user_idnumber_idx ;",false);// added in case of conflicts with upgrade from 14stable
598 execute_sql("DROP INDEX {$CFG->prefix}user_auth_idx ;",false);// added in case of conflicts with upgrade from 14stable
d35757eb 599 execute_sql("CREATE INDEX {$CFG->prefix}user_idnumber_idx ON {$CFG->prefix}user (idnumber)");
600 execute_sql("CREATE INDEX {$CFG->prefix}user_auth_idx ON {$CFG->prefix}user (auth)");
601 }
602
5a2432d4 603 if ($oldversion < 2004092000) { //redoing this just to be sure that column type is text (postgres type changes didnt work when this was done first time)
604 table_column("config", "value", "value", "text", "", "", "");
605 }
42b90599 606
607 if ($oldversion < 2004093001) { // add new table for sessions storage
608 execute_sql(" CREATE TABLE {$CFG->prefix}sessions (
609 sesskey char(32) PRIMARY KEY,
610 expiry integer NOT null,
611 expireref varchar(64),
612 data text NOT null
613 );");
614
615 execute_sql(" CREATE INDEX {$CFG->prefix}sessions_expiry_idx ON {$CFG->prefix}sessions (expiry)");
616 }
bb043e38 617
618 if ($oldversion < 2004111500) { // Update any users/courses using wrongly-named lang pack
619 execute_sql("UPDATE {$CFG->prefix}user SET lang = 'mi_nt' WHERE lang = 'ma_nt'");
620 execute_sql("UPDATE {$CFG->prefix}course SET lang = 'mi_nt' WHERE lang = 'ma_nt'");
621 }
17f3e7d0 622
623 if ($oldversion < 2004111700) { // add indexes- drop them first silently to avoid conflicts when upgrading.
624 execute_sql("DROP INDEX {$CFG->prefix}course_idnumber_idx;",false);
625 execute_sql("DROP INDEX {$CFG->prefix}course_shortname_idx;",false);
626 execute_sql("DROP INDEX {$CFG->prefix}user_students_userid_idx;",false);
627 execute_sql("DROP INDEX {$CFG->prefix}user_teachers_userid_idx;",false);
628
629 modify_database("","CREATE INDEX {$CFG->prefix}course_idnumber_idx ON {$CFG->prefix}course (idnumber);" );
630 modify_database("","CREATE INDEX {$CFG->prefix}course_shortname_idx ON {$CFG->prefix}course (shortname);" );
631 modify_database("","CREATE INDEX {$CFG->prefix}user_students_userid_idx ON {$CFG->prefix}user_students (userid);");
632 modify_database("","CREATE INDEX {$CFG->prefix}user_teachers_userid_idx ON {$CFG->prefix}user_teachers (userid);");
633 }
634
635 if ($oldversion < 2004111700) { // add an index to event for timestart and timeduration- drop them first silently to avoid conflicts when upgrading.
275c630a 636 execute_sql("DROP INDEX {$CFG->prefix}event_timestart_idx;",false);
637 execute_sql("DROP INDEX {$CFG->prefix}event_timeduration_idx;",false);
17f3e7d0 638
639 modify_database('','CREATE INDEX prefix_event_timestart_idx ON prefix_event (timestart);');
640 modify_database('','CREATE INDEX prefix_event_timeduration_idx ON prefix_event (timeduration);');
641 }
642
643 if ($oldversion < 2004117000) { // add an index on the groups_members table- drop them first silently to avoid conflicts when upgrading.
275c630a 644 execute_sql("DROP INDEX {$CFG->prefix}groups_members_userid_idx;",false);
17f3e7d0 645
646 modify_database('','CREATE INDEX prefix_groups_members_userid_idx ON prefix_groups_members (userid);');
647 }
d35757eb 648
17f3e7d0 649 if ($oldversion < 2004111700) { //add indexes on modules and course_modules- drop them first silently to avoid conflicts when upgrading.
275c630a 650 execute_sql("DROP INDEX {$CFG->prefix}course_modules_visible_idx;",false);
651 execute_sql("DROP INDEX {$CFG->prefix}course_modules_course_idx;",false);
652 execute_sql("DROP INDEX {$CFG->prefix}course_modules_module_idx;",false);
653 execute_sql("DROP INDEX {$CFG->prefix}course_modules_instance_idx;",false);
654 execute_sql("DROP INDEX {$CFG->prefix}course_modules_deleted_idx;",false);
655 execute_sql("DROP INDEX {$CFG->prefix}modules_name_idx;",false);
17f3e7d0 656
657 modify_database('','CREATE INDEX prefix_course_modules_visible_idx ON prefix_course_modules (visible);');
658 modify_database('','CREATE INDEX prefix_course_modules_course_idx ON prefix_course_modules (course);');
659 modify_database('','CREATE INDEX prefix_course_modules_module_idx ON prefix_course_modules (module);');
660 modify_database('','CREATE INDEX prefix_course_modules_instance_idx ON prefix_course_modules (instance);');
661 modify_database('','CREATE INDEX prefix_course_modules_deleted_idx ON prefix_course_modules (deleted);');
662 modify_database('','CREATE INDEX prefix_modules_name_idx ON prefix_modules (name);');
663 }
664
665 if ($oldversion < 2004111700) { // add an index on user students timeaccess (used for sorting)- drop them first silently to avoid conflicts when upgrading
275c630a 666 execute_sql("DROP INDEX {$CFG->prefix}user_students_timeaccess_idx;",false);
17f3e7d0 667
668 modify_database('','CREATE INDEX prefix_user_students_timeaccess_idx ON prefix_user_students (timeaccess);');
669 }
670
671 if ($oldversion < 2004111700) { //add indexes on faux foreign keys - drop them first silently to avoid conflicts when upgrading.
275c630a 672 execute_sql("DROP INDEX {$CFG->prefix}course_sections_coursesection_idx;",false);
673 execute_sql("DROP INDEX {$CFG->prefix}scale_courseid_idx;",false);
674 execute_sql("DROP INDEX {$CFG->prefix}user_admins_userid_idx;",false);
675 execute_sql("DROP INDEX {$CFG->prefix}user_coursecreators_userid_idx;",false);
17f3e7d0 676
677 modify_database('','CREATE INDEX prefix_course_sections_coursesection_idx ON prefix_course_sections (course,section);');
678 modify_database('','CREATE INDEX prefix_scale_courseid_idx ON prefix_scale (courseid);');
679 modify_database('','CREATE INDEX prefix_user_admins_userid_idx ON prefix_user_admins (userid);');
680 modify_database('','CREATE INDEX prefix_user_coursecreators_userid_idx ON prefix_user_coursecreators (userid);');
681 }
682
ffa5f71b 683 if ($oldversion < 2004111700) { // make new indexes on user table.
684 fix_course_sortorder(0,0,1);
685
275c630a 686 execute_sql("DROP INDEX {$CFG->prefix}course_category_idx;",false);
687 execute_sql("DROP INDEX {$CFG->prefix}course_category_sortorder_uk;",false);
ffa5f71b 688 modify_database('', "CREATE UNIQUE INDEX prefix_course_category_sortorder_uk ON prefix_course(category,sortorder)");
689
275c630a 690 execute_sql("DROP INDEX {$CFG->prefix}user_deleted_idx;",false);
691 execute_sql("DROP INDEX {$CFG->prefix}user_confirmed_idx;",false);
692 execute_sql("DROP INDEX {$CFG->prefix}user_firstname_idx;",false);
693 execute_sql("DROP INDEX {$CFG->prefix}user_lastname_idx;",false);
694 execute_sql("DROP INDEX {$CFG->prefix}user_city_idx;",false);
695 execute_sql("DROP INDEX {$CFG->prefix}user_country_idx;",false);
696 execute_sql("DROP INDEX {$CFG->prefix}user_lastaccess_idx;",false);
ffa5f71b 697
698 modify_database("","CREATE INDEX prefix_user_deleted_idx ON prefix_user (deleted)");
699 modify_database("","CREATE INDEX prefix_user_confirmed_idx ON prefix_user (confirmed)");
700 modify_database("","CREATE INDEX prefix_user_firstname_idx ON prefix_user (firstname)");
701 modify_database("","CREATE INDEX prefix_user_lastname_idx ON prefix_user (lastname)");
702 modify_database("","CREATE INDEX prefix_user_city_idx ON prefix_user (city)");
703 modify_database("","CREATE INDEX prefix_user_country_idx ON prefix_user (country)");
704 modify_database("","CREATE INDEX prefix_user_lastaccess_idx ON prefix_user (lastaccess)");
705 }
706
707 if ($oldversion < 2004111700) { // one more index for email (for sorting)
275c630a 708 execute_sql("DROP INDEX {$CFG->prefix}user_email_idx;",false);
ffa5f71b 709
710 modify_database('','CREATE INDEX prefix_user_email_idx ON prefix_user (email);');
711 }
5cd79686 712
713 if ($oldversion < 2004112200) { // new 'enrol' field for enrolment tables
714 table_column('user_students', '', 'enrol', 'varchar', '20', '', '', 'not null');
715 table_column('user_teachers', '', 'enrol', 'varchar', '20', '', '', 'not null');
716 modify_database("","CREATE INDEX {$CFG->prefix}user_students_enrol_idx ON {$CFG->prefix}user_students (enrol);");
91137862 717 modify_database("","CREATE INDEX {$CFG->prefix}user_teachers_enrol_idx ON {$CFG->prefix}user_teachers (enrol);");
5cd79686 718 }
f1f2c125 719
720 if ($oldversion < 2004112300) { // update log display to use correct postgres friendly sql
721 execute_sql("UPDATE {$CFG->prefix}log_display SET field='firstname||\' \'||lastname' WHERE module='user' AND action='view' AND mtable='user'");
722 execute_sql("UPDATE {$CFG->prefix}log_display SET field='firstname||\' \'||lastname' WHERE module='course' AND action='user report' AND mtable='user'");
723 }
724
b110348b 725 if ($oldversion < 2004112400) {
726
727 /// Delete duplicate enrolments
728 /// and then tell the database course,userid is a unique combination
729 if ($users = get_records_select("user_students", "userid > 0 GROUP BY course, userid ".
730 "HAVING count(*) > 1", "", "max(id) as id, userid, course ,count(*)")) {
731 foreach ($users as $user) {
732 delete_records_select("user_students", "userid = '$user->userid' ".
733 "AND course = '$user->course' AND id <> '$user->id'");
734 }
735 }
736 flush();
737
b692d2be 738 // drop some indexes quietly -- they may or may not exist depending on what version
739 // the user upgrades from
b110348b 740 execute_sql("DROP INDEX {$CFG->prefix}user_students_courseuserid_idx ", false);
b692d2be 741 execute_sql("DROP INDEX {$CFG->prefix}user_students_courseuserid_uk ", false);
b110348b 742 modify_database('','CREATE UNIQUE INDEX prefix_user_students_courseuserid_uk ON prefix_user_students (course,userid);');
743
744 /// Delete duplicate teacher enrolments
745 /// and then tell the database course,userid is a unique combination
746 if ($users = get_records_select("user_teachers", "userid > 0 GROUP BY course, userid ".
747 "HAVING count(*) > 1", "", "max(id) as id, userid, course ,count(*)")) {
748 foreach ($users as $user) {
749 delete_records_select("user_teachers", "userid = '$user->userid' ".
750 "AND course = '$user->course' AND id <> '$user->id'");
751 }
752 }
753 flush();
754
b692d2be 755 // drop some indexes quietly -- they may or may not exist depending on what version
756 // the user upgrades from
b110348b 757 execute_sql("DROP INDEX {$CFG->prefix}user_teachers_courseuserid_idx ", false);
b692d2be 758 execute_sql("DROP INDEX {$CFG->prefix}user_teachers_courseuserid_uk ", false);
b110348b 759 modify_database('','CREATE UNIQUE INDEX prefix_user_teachers_courseuserid_uk ON prefix_user_teachers (course,userid);');
760 }
761
b692d2be 762 if ($oldversion < 2004112401) {
763 // some postgres databases may have a non-unique index mislabeled unique.
764 fix_course_sortorder(0,0,1);
765 execute_sql("DROP INDEX {$CFG->prefix}course_category_sortorder_uk ", false);
766 execute_sql("DROP INDEX {$CFG->prefix}course_category_idx ", false);
767 modify_database('', "CREATE UNIQUE INDEX prefix_course_category_sortorder_uk ON prefix_course(category,sortorder);");
768
769 // odd! username was missing its unique index!
770 // first silently drop it just in case...
71dcd6c0 771 execute_sql("ALTER TABLE {$CFG->prefix}user DROP CONSTRAINT {$CFG->prefix}user_username_uk;", false);
b692d2be 772 execute_sql("DROP INDEX {$CFG->prefix}user_username_uk", false);
773 modify_database('', "CREATE UNIQUE INDEX prefix_user_username_uk ON prefix_user (username);");
774
775 }
027a1604 776
777 if ($oldversion < 2004112900) {
778 table_column('user', '', 'policyagreed', 'integer', '1', 'unsigned', '0', 'not null', 'confirmed');
779 }
1a5bc046 780
781 if ($oldversion < 2004121400) {
782 table_column('groups', '', 'password', 'varchar', '50', '', '', 'not null', 'description');
783 }
ffce79c0 784
785 if ($oldversion < 2004121600) {
786 modify_database('',"CREATE TABLE prefix_dst_preset (
787 id SERIAL PRIMARY KEY,
788 name varchar(48) NOT NULL default '',
789 apply_offset integer NOT NULL default '0',
790 activate_index integer NOT NULL default '1',
791 activate_day integer NOT NULL default '1',
792 activate_month integer NOT NULL default '1',
793 activate_time char(5) NOT NULL default '03:00',
794 deactivate_index integer NOT NULL default '1',
795 deactivate_day integer NOT NULL default '1',
796 deactivate_month integer NOT NULL default '2',
797 deactivate_time char(5) NOT NULL default '03:00',
91b4c5ee 798 last_change integer NOT NULL default '0',
799 next_change integer NOT NULL default '0',
ffce79c0 800 current_offset integer NOT NULL default '0'
91b4c5ee 801 );");
0eacb23c 802 }
803
804 if ($oldversion < 2004122800) {
805 execute_sql("DROP TABLE {$CFG->prefix}message", false);
806 execute_sql("DROP TABLE {$CFG->prefix}message_read", false);
807 execute_sql("DROP TABLE {$CFG->prefix}message_contacts", false);
ffce79c0 808
0eacb23c 809 execute_sql("DROP INDEX {$CFG->prefix}message_useridfrom_idx", false);
810 execute_sql("DROP INDEX {$CFG->prefix}message_useridto_idx", false);
811 execute_sql("DROP INDEX {$CFG->prefix}message_read_useridfrom_idx", false);
812 execute_sql("DROP INDEX {$CFG->prefix}message_read_useridto_idx", false);
813 execute_sql("DROP INDEX {$CFG->prefix}message_contacts_useridcontactid_idx", false);
ffce79c0 814
815 modify_database('',"CREATE TABLE prefix_message (
816 id SERIAL PRIMARY KEY,
817 useridfrom integer NOT NULL default '0',
818 useridto integer NOT NULL default '0',
819 message text,
0eacb23c 820 timecreated integer NOT NULL default '0',
ffce79c0 821 messagetype varchar(50) NOT NULL default ''
822 );
823
824 CREATE INDEX prefix_message_useridfrom_idx ON prefix_message (useridfrom);
825 CREATE INDEX prefix_message_useridto_idx ON prefix_message (useridto);
826
827 CREATE TABLE prefix_message_read (
828 id SERIAL PRIMARY KEY,
829 useridfrom integer NOT NULL default '0',
830 useridto integer NOT NULL default '0',
831 message text,
0eacb23c 832 timecreated integer NOT NULL default '0',
833 timeread integer NOT NULL default '0',
ffce79c0 834 messagetype varchar(50) NOT NULL default '',
835 mailed integer NOT NULL default '0'
836 );
837
838 CREATE INDEX prefix_message_read_useridfrom_idx ON prefix_message_read (useridfrom);
839 CREATE INDEX prefix_message_read_useridto_idx ON prefix_message_read (useridto);
840 ");
0eacb23c 841
842 modify_database('',"CREATE TABLE prefix_message_contacts (
843 id SERIAL PRIMARY KEY,
844 userid integer NOT NULL default '0',
845 contactid integer NOT NULL default '0',
846 blocked integer NOT NULL default '0'
847 );
848
849 CREATE INDEX prefix_message_contacts_useridcontactid_idx ON prefix_message_contacts (userid,contactid);
850 ");
851
c343bc88 852 modify_database('',"INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'write', 'user', 'firstname||\' \'||lastname');
853 INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'read', 'user', 'firstname||\' \'||lastname');
0eacb23c 854 ");
ffce79c0 855 }
e53c030b 856
857 if ($oldversion < 2004122801) {
858 table_column('message', '', 'format', 'integer', '4', 'unsigned', '0', 'not null', 'message');
859 table_column('message_read', '', 'format', 'integer', '4', 'unsigned', '0', 'not null', 'message');
860 }
0eacb23c 861
ffce79c0 862
36f01e7b 863 if ($oldversion < 2005010100) {
c343bc88 864 modify_database('',"INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'add contact', 'user', 'firstname||\' \'||lastname');
865 INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'remove contact', 'user', 'firstname||\' \'||lastname');
866 INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'block contact', 'user', 'firstname||\' \'||lastname');
867 INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'unblock contact', 'user', 'firstname||\' \'||lastname');
36f01e7b 868 ");
869 }
870
2765411a 871 if ($oldversion < 2005011000) { // Create a .htaccess file in dataroot, just in case
872 if (!file_exists($CFG->dataroot.'/.htaccess')) {
873 if ($handle = fopen($CFG->dataroot.'/.htaccess', 'w')) { // For safety
d389b128 874 @fwrite($handle, "deny from all\r\nAllowOverride None\r\n");
2765411a 875 @fclose($handle);
876 notify("Created a default .htaccess file in $CFG->dataroot");
877 }
878 }
879 }
880
b61efafb 881 if ($oldversion < 2005012500) { // add new table for meta courses.
483b7158 882 /*
b61efafb 883 modify_database("","CREATE TABLE prefix_meta_course (
450a0a7d 884 id SERIAL primary key,
885 parent_course integer NOT NULL,
886 child_course integer NOT NULL
b61efafb 887 );");
888
889 modify_database("","CREATE INDEX prefix_meta_course_parent_idx ON prefix_meta_course (parent_course);");
890 modify_database("","CREATE INDEX prefix_meta_course_child_idx ON prefix_meta_course (child_course);");
891 table_column('course','','meta_course','integer','1','','0','not null');
483b7158 892 */ // taking this OUT for upgrade from 1.4 to 1.5 (those tracking head will have already seen it)
b61efafb 893 }
894
5f37b628 895 if ($oldversion < 2005012501) { //fix table names for consistency
896 execute_sql("DROP TABLE {$CFG->prefix}meta_course",false); // drop silently
897 execute_sql("ALTER TABLE {$CFG->prefix}course DROP COLUMN meta_course",false); // drop silently
898
899 modify_database("","CREATE TABLE prefix_course_meta (
450a0a7d 900 id SERIAL primary key,
901 parent_course integer NOT NULL,
902 child_course integer NOT NULL
5f37b628 903 );");
904
905 modify_database("","CREATE INDEX prefix_course_meta_parent_idx ON prefix_course_meta (parent_course);");
906 modify_database("","CREATE INDEX prefix_course_meta_child_idx ON prefix_course_meta (child_course);");
907 table_column('course','','metacourse','integer','1','','0','not null');
908 }
909
4c3911f9 910 if ($oldversion < 2005020100) {
911 fix_course_sortorder(0, 1, 1);
912 }
913
915602ea 914 if ($oldversion < 2005021000) { // New fields for theme choices
915 table_column('course', '', 'theme', 'varchar', '50', '', '', '', 'lang');
916 table_column('groups', '', 'theme', 'varchar', '50', '', '', '', 'lang');
917 table_column('user', '', 'theme', 'varchar', '50', '', '', '', 'lang');
35b04d5e 918
919 set_config('theme', 'standardwhite'); // Reset to a known good theme
915602ea 920 }
921
41b7678d 922 if ($oldversion < 2005021700) {
923 table_column('user', '', 'dstpreset', 'int', '10', '', '0', 'not null', 'timezone');
924 }
925
ceff9307 926 if ($oldversion < 2005021800) {
927 modify_database("","CREATE TABLE adodb_logsql (
928 created timestamp NOT NULL,
929 sql0 varchar(250) NOT NULL,
930 sql1 text NOT NULL,
931 params text NOT NULL,
932 tracer text NOT NULL,
933 timer decimal(16,6) NOT NULL
934 );");
935 }
936
13222d99 937 if ($oldversion < 2005022400) {
938 table_column('dst_preset', '', 'family', 'varchar', '100', '', '', 'not null', 'name');
939 table_column('dst_preset', '', 'year', 'int', '10', '', '0', 'not null', 'family');
940 }
941
766d2bf3 942 if ($oldversion < 2005030501) {
943 table_column('user', '', 'msn', 'varchar', '50', '', '', '', 'icq');
944 table_column('user', '', 'aim', 'varchar', '50', '', '', '', 'icq');
945 table_column('user', '', 'yahoo', 'varchar', '50', '', '', '', 'icq');
946 table_column('user', '', 'skype', 'varchar', '50', '', '', '', 'icq');
947 }
948
1789c548 949 if ($oldversion < 2005032300) {
950 table_column('user', 'dstpreset', 'timezonename', 'varchar', '100');
8a72bfa2 951 execute_sql('UPDATE '.$CFG->prefix.'user SET timezonename = \'\'');
1789c548 952 }
953
4eb25d43 954
66336c5f 955 if ($oldversion < 2005032600) {
956 execute_sql('DROP TABLE '.$CFG->prefix.'dst_preset', false);
957 modify_database('',"CREATE TABLE prefix_timezone (
958 id SERIAL PRIMARY KEY,
959 name varchar(100) NOT NULL default '',
960 year integer NOT NULL default '0',
961 rule varchar(20) NOT NULL default '',
962 gmtoff integer NOT NULL default '0',
963 dstoff integer NOT NULL default '0',
964 dst_month integer NOT NULL default '0',
965 dst_startday integer NOT NULL default '0',
966 dst_weekday integer NOT NULL default '0',
967 dst_skipweeks integer NOT NULL default '0',
968 dst_time varchar(5) NOT NULL default '00:00',
969 std_month integer NOT NULL default '0',
970 std_startday integer NOT NULL default '0',
971 std_weekday integer NOT NULL default '0',
972 std_skipweeks integer NOT NULL default '0',
973 std_time varchar(5) NOT NULL default '00:00'
974 );");
975 }
976
4eb25d43 977 if ($oldversion < 2005032800) {
8a72bfa2 978 modify_database('',"CREATE TABLE prefix_grade_category (
979 id SERIAL PRIMARY KEY,
980 name varchar(64) default NULL,
981 courseid integer NOT NULL default '0',
982 drop_x_lowest integer NOT NULL default '0',
983 bonus_points integer NOT NULL default '0',
984 hidden integer NOT NULL default '0',
985 weight decimal(4,2) default '0.00'
986 );");
987
988 modify_database('',"CREATE INDEX prefix_grade_category_courseid_idx ON prefix_grade_category (courseid);");
989
990 modify_database('',"CREATE TABLE prefix_grade_exceptions (
991 id SERIAL PRIMARY KEY,
992 courseid integer NOT NULL default '0',
993 grade_itemid integer NOT NULL default '0',
994 userid integer NOT NULL default '0'
995 );");
996
997 modify_database('',"CREATE INDEX prefix_grade_exceptions_courseid_idx ON prefix_grade_exceptions (courseid);");
998
999
1000 modify_database('',"CREATE TABLE prefix_grade_item (
1001 id SERIAL PRIMARY KEY,
1002 courseid integer default NULL,
1003 category integer default NULL,
1004 modid integer default NULL,
1005 cminstance integer default NULL,
1006 scale_grade float(11) default '1.0000000000',
1007 extra_credit integer NOT NULL default '0',
1008 sort_order integer NOT NULL default '0'
1009 );");
1010
1011 modify_database('',"CREATE INDEX prefix_grade_item_courseid_idx ON prefix_grade_item (courseid);");
1012
1013 modify_database('',"CREATE TABLE prefix_grade_letter (
1014 id SERIAL PRIMARY KEY,
1015 courseid integer NOT NULL default '0',
1016 letter varchar(8) NOT NULL default 'NA',
c87139db 1017 grade_high decimal(6,2) NOT NULL default '100.00',
1018 grade_low decimal(6,2) NOT NULL default '0.00'
8a72bfa2 1019 );");
1020
1021 modify_database('',"CREATE INDEX prefix_grade_letter_courseid_idx ON prefix_grade_letter (courseid);");
1022
1023 modify_database('',"CREATE TABLE prefix_grade_preferences (
1024 id SERIAL PRIMARY KEY,
1025 courseid integer default NULL,
1026 preference integer NOT NULL default '0',
1027 value integer NOT NULL default '0'
1028 );");
1029
1030 modify_database('',"CREATE UNIQUE INDEX prefix_grade_prefs_courseidpref_uk ON prefix_grade_preferences (courseid,preference);");
4eb25d43 1031 }
1032
594ceedd 1033 if ($oldversion < 2005033100) { // Get rid of defunct field from course modules table
1034 delete_records('course_modules', 'deleted', 1); // Delete old records we don't need any more
bb2889b4 1035 execute_sql('DROP INDEX '.$CFG->prefix.'course_modules_deleted_idx;'); // Old index
1036 execute_sql('ALTER TABLE '.$CFG->prefix.'course_modules DROP deleted;'); // Old field
594ceedd 1037 }
1038
68c0264e 1039 if ($oldversion < 2005040800) {
1040 table_column('user', 'timezone', 'timezone', 'varchar', '100', '', '99');
ab5aeca1 1041 execute_sql(" ALTER TABLE {$CFG->prefix}user DROP timezonename ");
68c0264e 1042 }
1043
6927f0e7 1044 if ($oldversion < 2005041101) {
1045 require_once($CFG->libdir.'/filelib.php');
1046 if (is_readable($CFG->dirroot.'/lib/timezones.txt')) { // Distribution file
1047 if ($timezones = get_records_csv($CFG->dirroot.'/lib/timezones.txt', 'timezone')) {
1048 $db->debug = false;
1049 update_timezone_records($timezones);
1050 notify(count($timezones).' timezones installed');
1051 $db->debug = true;
1052 }
1053 }
1054 }
1055
323d2abe 1056 if ($oldversion < 2005041900) { // Copy all Dialogue entries into Messages, and hide Dialogue module
1057
1058 if ($entries = get_records_sql('SELECT e.id, e.userid, c.recipientid, e.text, e.timecreated
1059 FROM '.$CFG->prefix.'dialogue_conversations c,
1060 '.$CFG->prefix.'dialogue_entries e
1061 WHERE e.conversationid = c.id')) {
1062 foreach ($entries as $entry) {
1063 $message = NULL;
1064 $message->useridfrom = $entry->userid;
1065 $message->useridto = $entry->recipientid;
2a0cf3ca 1066 $message->message = addslashes($entry->text);
323d2abe 1067 $message->format = FORMAT_HTML;
1068 $message->timecreated = $entry->timecreated;
1069 $message->messagetype = 'direct';
1070
1071 insert_record('message_read', $message);
1072 }
1073 }
1074
1075 set_field('modules', 'visible', 0, 'name', 'dialogue');
1076
1077 notify('The Dialogue module has been disabled, and all the old Messages from it copied into the new standard Message feature. If you really want Dialogue back, you can enable it using the "eye" icon here: Admin >> Modules >> Dialogue');
1078
1079 }
1080
8180c859 1081 if ($oldversion < 2005042100) {
1082 $result = table_column('event', '', 'repeatid', 'int', '10', 'unsigned', '0', 'not null', 'userid') && $result;
1083 }
1084
b71772e2 1085 if ($oldversion < 2005042400) { // Add user tracking prefs field.
9e33ce2c 1086 table_column('user', '', 'trackforums', 'int', '4', 'unsigned', '0', 'not null', 'autosubscribe');
b71772e2 1087 }
1088
5a8c6476 1089 if ($oldversion < 2005051500) { // Add user tracking prefs field.
1090 table_column('grade_category', 'weight', 'weight', 'numeric(5,2)', '', '', '0.00', '', '');
1091 }
1092
a13e99bd 1093 if ($oldversion < 2005053000 ) { // Add config_plugins table
1094
1095 // this table was created on the MOODLE_15_STABLE branch
1096 // so it may already exist. Therefore we hide potential errors
1097 // (Postgres doesn't support CREATE TABLE IF NOT EXISTS)
1098 execute_sql("CREATE TABLE {$CFG->prefix}config_plugins (
1099 id SERIAL PRIMARY KEY,
ee708245 1100 plugin varchar(100) NOT NULL default 'core',
1101 name varchar(100) NOT NULL default '',
a13e99bd 1102 value text NOT NULL default '',
1103 CONSTRAINT {$CFG->prefix}config_plugins_plugin_name_uk UNIQUE (plugin, name)
1104 );", false);
1105
1106 }
5a8c6476 1107
a4080313 1108 if ($oldversion < 2005060200) { // migrate some config items to config_plugins table
1109
1110 // NOTE: this block is in both postgres AND mysql upgrade
1111 // files. If you edit either, update the otherone.
1112 $user_fields = array("firstname", "lastname", "email",
1113 "phone1", "phone2", "department",
1114 "address", "city", "country",
1115 "description", "idnumber", "lang");
1116 if (!empty($CFG->auth)) { // if we have no auth, just pass
1117 foreach ($user_fields as $field) {
1118 $suffixes = array('', '_editlock', '_updateremote', '_updatelocal');
1119 foreach ($suffixes as $suffix) {
1120 $key = 'auth_user_' . $field . $suffix;
1121 if (isset($CFG->$key)) {
1122
1123 // translate keys & values
1124 // to the new convention
1125 // this should support upgrading
1126 // even 1.5dev installs
1127 $newkey = $key;
1128 $newval = $CFG->$key;
1129 if ($suffix === '') {
1130 $newkey = 'field_map_' . $field;
1131 } elseif ($suffix === '_editlock') {
1132 $newkey = 'field_lock_' . $field;
1133 $newval = ($newval==1) ? 'locked' : 'unlocked'; // translate 0/1 to locked/unlocked
1134 } elseif ($suffix === '_updateremote') {
1135 $newkey = 'field_updateremote_' . $field;
1136 } elseif ($suffix === '_updatelocal') {
1137 $newkey = 'field_updatelocal_' . $field;
1138 $newval = ($newval==1) ? 'onlogin' : 'oncreate'; // translate 0/1 to locked/unlocked
1139 }
1140
967f5f4b 1141 if (!(set_config($newkey, addslashes($newval), 'auth/'.$CFG->auth)
a4080313 1142 && delete_records('config', 'name', $key))) {
1143 notify("Error updating Auth configuration $key to {$CFG->auth} $newkey .");
1144 $result = false;
1145 }
1146 } // end if isset key
1147 } // end foreach suffix
1148 } // end foreach field
1149 }
1150 }
1151
62831050 1152 if ($oldversion < 2005060201) { // Close down the Attendance module, we are removing it from CVS.
1153 if (!file_exists($CFG->dirroot.'/mod/attendance/lib.php')) {
1154 if (count_records('attendance')) { // We have some data, so should keep it
1155
1156 set_field('modules', 'visible', 0, 'name', 'attendance');
1157 notify('The Attendance module has been discontinued. If you really want to
1158 continue using it, you should download it individually from
1159 http://download.moodle.org/modules and install it, then
1160 reactivate it from Admin >> Configuration >> Modules.
1161 None of your existing data has been deleted, so all existing
1162 Attendance activities should re-appear.');
1163
1164 } else { // No data, so do a complete delete
1165
1166 execute_sql('DROP TABLE '.$CFG->prefix.'attendance', false);
1167 delete_records('modules', 'name', 'attendance');
1168 notify("The Attendance module has been discontinued and removed from your site.
1169 You weren't using it anyway. ;-)");
1170 }
1171 }
1172 }
1173
4e980e5a 1174 if ($oldversion < 2005060223) { // Mass cleanup of bad postgres upgrade scripts
1175 execute_sql("DROP TABLE {$CFG->prefix}attendance_roll", false); // There are no attendance module anymore
1176 modify_database('','ALTER TABLE prefix_config ALTER value SET NOT NULL');
1177 modify_database('','ALTER TABLE prefix_course ALTER metacourse SET NOT NULL');
1178 modify_database('','ALTER TABLE prefix_course ALTER theme SET NOT NULL');
1179 modify_database('','ALTER TABLE prefix_event ALTER repeatid SET NOT NULL');
1180 modify_database('','ALTER TABLE prefix_groups ALTER password SET NOT NULL');
1181 modify_database('','ALTER TABLE prefix_groups ALTER theme SET NOT NULL');
1182 modify_database('','ALTER TABLE prefix_message ALTER format SET NOT NULL');
1183 modify_database('','ALTER TABLE prefix_message_read ALTER format SET NOT NULL');
1184 modify_database('','ALTER TABLE prefix_groups ALTER theme SET NOT NULL');
1185 modify_database('','ALTER TABLE prefix_user ALTER aim DROP DEFAULT');
1186 modify_database('','ALTER TABLE prefix_user ALTER idnumber DROP DEFAULT');
1187 modify_database('','ALTER TABLE prefix_user ALTER msn DROP DEFAULT');
1188 modify_database('','ALTER TABLE prefix_user ALTER policyagreed SET NOT NULL');
1189 modify_database('','ALTER TABLE prefix_user ALTER skype DROP DEFAULT');
1190 modify_database('','ALTER TABLE prefix_user ALTER theme SET NOT NULL');
1191 modify_database('','ALTER TABLE prefix_user ALTER timezone SET NOT NULL');
1192 modify_database('','ALTER TABLE prefix_user ALTER trackforums SET NOT NULL');
1193 modify_database('','ALTER TABLE prefix_user ALTER yahoo DROP DEFAULT');
1194 modify_database('','ALTER TABLE prefix_user_students ALTER enrol SET NOT NULL');
1195 modify_database('','ALTER TABLE prefix_user_teachers ALTER enrol SET NOT NULL');
1196 }
1197
27d8712a 1198 if ($oldversion < 2005071700) { // Close down the Dialogue module, we are removing it from CVS.
1199 if (!file_exists($CFG->dirroot.'/mod/dialogue/lib.php')) {
1200 if (count_records('dialogue')) { // We have some data, so should keep it
1201
1202 set_field('modules', 'visible', 0, 'name', 'dialogue');
1203 notify('The Dialogue module has been discontinued. If you really want to
1204 continue using it, you should download it individually from
1205 http://download.moodle.org/modules and install it, then
1206 reactivate it from Admin >> Configuration >> Modules.
1207 None of your existing data has been deleted, so all existing
1208 Dialogue activities should re-appear.');
1209
1210 } else { // No data, so do a complete delete
1211
1212 execute_sql('DROP TABLE '.$CFG->prefix.'dialogue', false);
1213 delete_records('modules', 'name', 'dialogue');
1214 notify("The Dialogue module has been discontinued and removed from your site.
1215 You weren't using it anyway. ;-)");
1216 }
1217 }
1218 }
1219
98f8f9c2 1220 if ($oldversion < 2005072000) { // Add a couple fields to mdl_event to work towards iCal import/export
1221 table_column('event', '', 'uuid', 'char', '36', '', '', 'not null', 'visible');
1222 table_column('event', '', 'sequence', 'integer', '10', 'unsigned', '1', 'not null', 'uuid');
1223 }
3b4ff6ed 1224
1225 if ($oldversion < 2005072100) { // run the online assignment cleanup code
1226 include($CFG->dirroot.'/'.$CFG->admin.'/oacleanup.php');
1227 if (function_exists('online_assignment_cleanup')) {
1228 online_assignment_cleanup();
1229 }
1230 }
98f8f9c2 1231
f2e34253 1232 if ($oldversion < 2005072200) { // fix the mistakenly-added currency stuff from enrol/authorize
1233 execute_sql("DROP TABLE {$CFG->prefix}currencies", false); // drop silently
1234 execute_sql("ALTER TABLE {$CFG->prefix}course DROP currency", false);
1235 $defaultcurrency = empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency;
1236 table_column('course', '', 'currency', 'char', '3', '', $defaultcurrency, 'not null', 'cost');
1237 }
1238
0705ff84 1239 if ($oldversion < 2005081600) { //set up the course requests table
1240 modify_database('',"CREATE TABLE prefix_course_request (
1241 id SERIAL PRIMARY KEY,
1242 fullname varchar(254) NOT NULL default '',
1243 shortname varchar(15) NOT NULL default '',
1244 summary text NOT NULL default '',
1245 reason text NOT NULL default '',
1246 requester INTEGER NOT NULL default 0
1247 );");
1248
1249 modify_database('','CREATE INDEX prefix_course_request_shortname_idx ON prefix_course_request (shortname);');
1250
1251 table_column('course','','requested');
1252 }
1253
1254 if ($oldversion < 2005081601) {
1255 modify_database('','CREATE TABLE prefix_course_allowed_modules (
1256 id SERIAL PRIMARY KEY,
1257 course INTEGER NOT NULL default 0,
1258 module INTEGER NOT NULL default 0
1259 );');
1260
1261 modify_database('','CREATE INDEX prefix_course_allowed_modules_course_idx ON prefix_course_allowed_modules (course);');
1262 modify_database('','CREATE INDEX prefix_course_allowed_modules_module_idx ON prefix_course_allowed_modules (module);');
1263 table_column('course','','restrictmodules','int','1','','0','not null');
f41ef63e 1264 }
0705ff84 1265
f41ef63e 1266 if ($oldversion < 2005081700) {
1267 table_column('course_categories','','depth','integer');
1268 table_column('course_categories','','path','varchar','255');
1269 }
0705ff84 1270
f3221af9 1271 if ($oldversion < 2005090100) { // stats!
1272 modify_database('','CREATE TABLE prefix_stats_daily (
1273 id SERIAL PRIMARY KEY,
1274 courseid INTEGER NOT NULL default 0,
1275 timeend INTEGER NOT NULL default 0,
1276 students INTEGER NOT NULL default 0,
1277 teachers INTEGER NOT NULL default 0,
1278 activestudents INTEGER NOT NULL default 0,
1279 activeteachers INTEGER NOT NULL default 0,
1280 studentreads INTEGER NOT NULL default 0,
1281 studentwrites INTEGER NOT NULL default 0,
1282 teacherreads INTEGER NOT NULL default 0,
1283 teacherwrites INTEGER NOT NULL default 0,
1284 logins INTEGER NOT NULL default 0,
1285 uniquelogins INTEGER NOT NULL default 0
1286 );');
1287
1288 modify_database('','CREATE INDEX prefix_stats_daily_courseid_idx ON prefix_stats_daily (courseid);');
1289 modify_database('','CREATE INDEX prefix_stats_daily_timeend_idx ON prefix_stats_daily (timeend);');
1290
1291 modify_database('','CREATE TABLE prefix_stats_weekly (
1292 id SERIAL PRIMARY KEY,
1293 courseid INTEGER NOT NULL default 0,
1294 timeend INTEGER NOT NULL default 0,
1295 students INTEGER NOT NULL default 0,
1296 teachers INTEGER NOT NULL default 0,
1297 activestudents INTEGER NOT NULL default 0,
1298 activeteachers INTEGER NOT NULL default 0,
1299 studentreads INTEGER NOT NULL default 0,
1300 studentwrites INTEGER NOT NULL default 0,
1301 teacherreads INTEGER NOT NULL default 0,
1302 teacherwrites INTEGER NOT NULL default 0,
1303 logins INTEGER NOT NULL default 0,
1304 uniquelogins INTEGER NOT NULL default 0
1305 );');
1306
1307 modify_database('','CREATE INDEX prefix_stats_weekly_courseid_idx ON prefix_stats_weekly (courseid);');
1308 modify_database('','CREATE INDEX prefix_stats_weekly_timeend_idx ON prefix_stats_weekly (timeend);');
1309
1310 modify_database('','CREATE TABLE prefix_stats_monthly (
1311 id SERIAL PRIMARY KEY,
1312 courseid INTEGER NOT NULL default 0,
1313 timeend INTEGER NOT NULL default 0,
1314 students INTEGER NOT NULL default 0,
1315 teachers INTEGER NOT NULL default 0,
1316 activestudents INTEGER NOT NULL default 0,
1317 activeteachers INTEGER NOT NULL default 0,
1318 studentreads INTEGER NOT NULL default 0,
1319 studentwrites INTEGER NOT NULL default 0,
1320 teacherreads INTEGER NOT NULL default 0,
1321 teacherwrites INTEGER NOT NULL default 0,
1322 logins INTEGER NOT NULL default 0,
1323 uniquelogins INTEGER NOT NULL default 0
1324 );');
1325
1326 modify_database('','CREATE INDEX prefix_stats_monthly_courseid_idx ON prefix_stats_monthly (courseid);');
1327 modify_database('','CREATE INDEX prefix_stats_monthly_timeend_idx ON prefix_stats_monthly (timeend);');
1328
1329 modify_database("","CREATE TABLE prefix_stats_user_daily (
1330 id SERIAL PRIMARY KEY,
1331 courseid INTEGER NOT NULL default 0,
1332 userid INTEGER NOT NULL default 0,
1333 roleid INTEGER NOT NULL default 0,
1334 timeend INTEGER NOT NULL default 0,
462458c6 1335 statsreads INTEGER NOT NULL default 0,
1336 statswrites INTEGER NOT NULL default 0,
f3221af9 1337 stattype varchar(30) NOT NULL default ''
1338 );");
1339
1340 modify_database("","CREATE INDEX prefix_stats_user_daily_courseid_idx ON prefix_stats_user_daily (courseid);");
1341 modify_database("","CREATE INDEX prefix_stats_user_daily_userid_idx ON prefix_stats_user_daily (userid);");
1342 modify_database("","CREATE INDEX prefix_stats_user_daily_roleid_idx ON prefix_stats_user_daily (roleid);");
1343 modify_database("","CREATE INDEX prefix_stats_user_daily_timeend_idx ON prefix_stats_user_daily (timeend);");
1344
1345 modify_database("","CREATE TABLE prefix_stats_user_weekly (
1346 id SERIAL PRIMARY KEY,
1347 courseid INTEGER NOT NULL default 0,
1348 userid INTEGER NOT NULL default 0,
1349 roleid INTEGER NOT NULL default 0,
1350 timeend INTEGER NOT NULL default 0,
462458c6 1351 statsreads INTEGER NOT NULL default 0,
1352 statswrites INTEGER NOT NULL default 0,
f3221af9 1353 stattype varchar(30) NOT NULL default ''
1354 );");
1355
1356 modify_database("","CREATE INDEX prefix_stats_user_weekly_courseid_idx ON prefix_stats_user_weekly (courseid);");
1357 modify_database("","CREATE INDEX prefix_stats_user_weekly_userid_idx ON prefix_stats_user_weekly (userid);");
1358 modify_database("","CREATE INDEX prefix_stats_user_weekly_roleid_idx ON prefix_stats_user_weekly (roleid);");
1359 modify_database("","CREATE INDEX prefix_stats_user_weekly_timeend_idx ON prefix_stats_user_weekly (timeend);");
1360
1361 modify_database("","CREATE TABLE prefix_stats_user_monthly (
1362 id SERIAL PRIMARY KEY,
1363 courseid INTEGER NOT NULL default 0,
1364 userid INTEGER NOT NULL default 0,
1365 roleid INTEGER NOT NULL default 0,
1366 timeend INTEGER NOT NULL default 0,
462458c6 1367 statsreads INTEGER NOT NULL default 0,
1368 statswrites INTEGER NOT NULL default 0,
f3221af9 1369 stattype varchar(30) NOT NULL default ''
1370 );");
1371
1372 modify_database("","CREATE INDEX prefix_stats_user_monthly_courseid_idx ON prefix_stats_user_monthly (courseid);");
1373 modify_database("","CREATE INDEX prefix_stats_user_monthly_userid_idx ON prefix_stats_user_monthly (userid);");
1374 modify_database("","CREATE INDEX prefix_stats_user_monthly_roleid_idx ON prefix_stats_user_monthly (roleid);");
1375 modify_database("","CREATE INDEX prefix_stats_user_monthly_timeend_idx ON prefix_stats_user_monthly (timeend);");
1376 }
1377
7246c2d1 1378 if ($oldversion < 2005100300) {
1379 table_column('course','','expirynotify','integer','1');
1380 table_column('course','','expirythreshold','integer');
1381 table_column('course','','notifystudents','integer','1');
1382 $new = new stdClass();
1383 $new->name = 'lastexpirynotify';
1384 $new->value = 0;
1385 insert_record('config', $new);
1386 }
f89033b1 1387
1388 if ($oldversion < 2005100400) {
1389 table_column('course','','enrollable','integer','1','unsigned','1');
1390 table_column('course','','enrolstartdate','integer');
1391 table_column('course','','enrolenddate','integer');
1392 }
56eb871c 1393
f9667a5a 1394
1395 if ($oldversion < 2005101200) { // add enrolment key to course_request.
56eb871c 1396 table_column('course_request','','password','text');
1397 }
fb3ea05c 1398
1399 if ($oldversion < 2006030800) { # add extra indexes to log (see bug #4112)
1400 modify_database('',"CREATE INDEX prefix_log_userid_idx ON prefix_log (userid);");
1401 modify_database('',"CREATE INDEX prefix_log_info_idx ON prefix_log (info);");
1402 }
f9667a5a 1403
1404 if ($oldversion < 2006030900) {
1405 table_column('course','','enrol','varchar','20','','');
7064007f 1406
1407 if ($CFG->enrol == 'internal' || $CFG->enrol == 'manual') {
1408 set_config('enrol_plugins_enabled', 'manual');
1409 set_config('enrol', 'manual');
1410 } else {
1411 set_config('enrol_plugins_enabled', 'manual,'.$CFG->enrol);
1412 }
1413
54e9516e 1414 require_once("$CFG->dirroot/enrol/enrol.class.php");
1415 $defaultenrol = enrolment_factory::factory($CFG->enrol);
1416 if (!method_exists($defaultenrol, 'print_entry')) { // switch enrollable to off for all courses in this case
4af1efec 1417 modify_database('', 'UPDATE prefix_course SET enrollable = 0');
54e9516e 1418 }
7064007f 1419
6a2c524c 1420 execute_sql("UPDATE {$CFG->prefix}user_students SET enrol='manual' WHERE enrol='' OR enrol='internal'");
1421 execute_sql("UPDATE {$CFG->prefix}user_teachers SET enrol='manual' WHERE enrol=''");
6a2c524c 1422
29f85332 1423 }
89d38fdd 1424
f182aaf1 1425 if ($oldversion < 2006031000) {
89d38fdd 1426
1427 modify_database("","CREATE TABLE prefix_post (
1428 id SERIAL PRIMARY KEY,
1429 userid INTEGER NOT NULL default 0,
1430 courseid INTEGER NOT NULL default 0,
1431 groupid INTEGER NOT NULL default 0,
1432 moduleid INTEGER NOT NULL default 0,
1433 coursemoduleid INTEGER NOT NULL default 0,
1434 subject varchar(128) NOT NULL default '',
d348b6f2 1435 summary text,
1436 content text,
89d38fdd 1437 uniquehash varchar(128) NOT NULL default '',
1438 rating INTEGER NOT NULL default 0,
1439 format INTEGER NOT NULL default 0,
d348b6f2 1440 publishstate varchar(10) CHECK (publishstate IN ('draft','site','public')) NOT NULL default 'draft',
89d38fdd 1441 lastmodified INTEGER NOT NULL default '0',
1442 created INTEGER NOT NULL default '0'
1443 );");
1444
1445 modify_database("","CREATE INDEX id_user_idx ON prefix_post (id, courseid);");
1446 modify_database("","CREATE INDEX post_lastmodified_idx ON prefix_post (lastmodified);");
1447 modify_database("","CREATE INDEX post_subject_idx ON prefix_post (subject);");
1448
1449 modify_database("","CREATE TABLE prefix_tags (
1450 id SERIAL PRIMARY KEY,
1451 type varchar(255) NOT NULL default 'official',
1452 userid INTEGER NOT NULL default 0,
1453 text varchar(255) NOT NULL default ''
1454 );");
1455
1456 modify_database("","CREATE TABLE prefix_blog_tag_instance (
1457 id SERIAL PRIMARY KEY,
d348b6f2 1458 entryid integer NOT NULL default 0,
1459 tagid integer NOT NULL default 0,
1460 groupid integer NOT NULL default 0,
1461 courseid integer NOT NULL default 0,
1462 userid integer NOT NULL default 0
89d38fdd 1463 );");
1464 }
29f85332 1465
cd9afe2f 1466 if ($oldversion < 2006031400) {
1467 require_once("$CFG->dirroot/enrol/enrol.class.php");
1468 $defaultenrol = enrolment_factory::factory($CFG->enrol);
1469 if (!method_exists($defaultenrol, 'print_entry')) {
1470 set_config('enrol', 'manual');
1471 }
1472 }
1473
112d0ebf 1474 if ($oldversion < 2006032000) {
1475 table_column('post','','module','varchar','20','','','not null', 'id');
53c988a4 1476 modify_database('',"CREATE INDEX post_module_idx ON prefix_post (module);");
112d0ebf 1477 modify_database('',"UPDATE prefix_post SET module = 'blog';");
1478 }
1479
6a30c073 1480 if ($oldversion < 2006032001) {
53c988a4 1481 table_column('blog_tag_instance','','timemodified','integer','10','unsigned','0','not null', 'userid');
1482 modify_database('',"CREATE INDEX bti_entryid_idx ON prefix_blog_tag_instance (entryid);");
1483 modify_database('',"CREATE INDEX bti_tagid_idx ON prefix_blog_tag_instance (tagid);");
9a8091a9 1484 modify_database('',"UPDATE prefix_blog_tag_instance SET timemodified = '".time()."';");
6a30c073 1485 }
1486
60f92a43 1487 if ($oldversion < 2006040500) { // Add an index to course_sections that was never upgraded (bug 5100)
1488 execute_sql(" CREATE INDEX {$CFG->prefix}course_sections_coursesection_idx ON {$CFG->prefix}course_sections (course,section) ", false);
1489 }
1490
12ef2c40 1491 if ($oldversion < 2006041100) {
1492 table_column('course_modules','','visibleold','integer','1','unsigned','1','not null', 'visible');
1493 }
1494
36e6706e 1495 if ($oldversion < 2006042400) {
721cb867 1496 // Look through table log_display and get rid of duplicates.
1497 $rs = get_recordset_sql('SELECT DISTINCT * FROM '.$CFG->prefix.'log_display');
1498
1499 // Drop the log_display table and create it back with an id field.
1500 execute_sql("DROP TABLE {$CFG->prefix}log_display", false);
1501
1502 modify_database('', "CREATE TABLE prefix_log_display (
1503 id SERIAL PRIMARY KEY,
1504 module varchar(30) NOT NULL default '',
1505 action varchar(40) NOT NULL default '',
1506 mtable varchar(30) NOT NULL default '',
1507 field varchar(50) NOT NULL default '')");
1508
1509 // Add index to ensure that module and action combination is unique.
1510 modify_database('', 'CREATE INDEX prefix_log_display_moduleaction ON prefix_log_display (module,action)');
1511
1512 // Insert the records back in, sans duplicates.
1513 if ($rs && $rs->RecordCount() > 0) {
1514 while (!$rs->EOF) {
1515 $sql = "INSERT INTO {$CFG->prefix}log_display ".
1516 "VALUES('', '".$rs->fields['module']."', ".
1517 "'".$rs->fields['action']."', ".
1518 "'".$rs->fields['mtable']."', ".
1519 "'".$rs->fields['field']."')";
1520
1521 execute_sql($sql, false);
1522 $rs->MoveNext();
1523 }
1524 }
1525 }
31513aa1 1526
1527 // add 2 indexes to tags table
1528 if ($oldversion < 2006042401) {
1529 modify_database('',"CREATE INDEX tags_typeuserid_idx ON prefix_tags (type, userid);");
1530 modify_database('',"CREATE INDEX tags_text_idx ON prefix_tags (text);");
1531 }
7235ae23 1532
1533 if ($oldversion < 2006050500) {
1534 table_column('log', 'action', 'action', 'varchar', '40', '', '', 'not null');
1535 }
f1720915 1536
1537 if ($oldversion < 2006050502) { // Close down the Dialogue module, we are removing it from CVS.
1538 if (!file_exists($CFG->dirroot.'/mod/dialogue/lib.php')) {
1539 if (!count_records('dialogue_conversations')) { // no data, drop the extra tables
1540 execute_sql('DROP TABLE '.$CFG->prefix.'dialogue_conversations', false);
1541 execute_sql('DROP TABLE '.$CFG->prefix.'dialogue_entries', false);
1542 notify("The Dialogue module has been discontinued and removed from your site.
1543 You weren't using it anyway. ;-)");
1544 }
1545 }
ccd218ee 1546
889e6b46 1547 table_column('course_request', 'password', 'password', 'varchar', '50', '', '');
ccd218ee 1548
889e6b46 1549 table_column('course', 'currency', 'currency', 'varchar', '3');
ccd218ee 1550
1551 modify_database('', 'ALTER TABLE prefix_course_categories
1552 ALTER COLUMN path SET DEFAULT \'\'');
1553
889e6b46 1554 table_column('log_display', 'module', 'module', 'varchar', '20');
ccd218ee 1555
05acf8e9 1556 modify_database("","DROP INDEX id_user_idx");
1557 modify_database("","DROP INDEX post_lastmodified_idx");
1558 modify_database("","DROP INDEX post_subject_idx");
1559 modify_database('',"DROP INDEX bti_entryid_idx");
1560 modify_database('',"DROP INDEX bti_tagid_idx");
1561 modify_database('',"DROP INDEX post_module_idx");
1562 modify_database('',"DROP INDEX tags_typeuserid_idx");
1563 modify_database('',"DROP INDEX tags_text_idx");
1564
1565 modify_database("","CREATE INDEX {$CFG->prefix}id_user_idx ON prefix_post (id, courseid);");
1566 modify_database("","CREATE INDEX {$CFG->prefix}post_lastmodified_idx ON prefix_post (lastmodified);");
1567 modify_database("","CREATE INDEX {$CFG->prefix}post_subject_idx ON prefix_post (subject);");
1568 modify_database('',"CREATE INDEX {$CFG->prefix}bti_entryid_idx ON prefix_blog_tag_instance (entryid);");
1569 modify_database('',"CREATE INDEX {$CFG->prefix}bti_tagid_idx ON prefix_blog_tag_instance (tagid);");
1570 modify_database('',"CREATE INDEX {$CFG->prefix}post_module_idx ON prefix_post (moduleid);");
1571 modify_database('',"CREATE INDEX {$CFG->prefix}tags_typeuserid_idx ON prefix_tags (type, userid);");
1572 modify_database('',"CREATE INDEX {$CFG->prefix}tags_text_idx ON prefix_tags (text);");
1573
f1720915 1574 }
1575
462458c6 1576 // renaming of reads and writes for stats_user_xyz
1577 if ($oldversion < 2006052400) { // change this later
1578
1579 // we are using this because we want silent updates
1580
22a92ee2 1581 execute_sql("ALTER TABLE {$CFG->prefix}stats_user_daily RENAME COLUMN reads TO statsreads", false);
1582 execute_sql("ALTER TABLE {$CFG->prefix}stats_user_daily RENAME COLUMN writes TO statswrites", false);
1583 execute_sql("ALTER TABLE {$CFG->prefix}stats_user_weekly RENAME COLUMN reads TO statsreads", false);
1584 execute_sql("ALTER TABLE {$CFG->prefix}stats_user_weekly RENAME COLUMN writes TO statswrites", false);
1585 execute_sql("ALTER TABLE {$CFG->prefix}stats_user_monthly RENAME COLUMN reads TO statsreads", false);
1586 execute_sql("ALTER TABLE {$CFG->prefix}stats_user_monthly RENAME COLUMN writes TO statswrites", false);
462458c6 1587
1588 }
ae57b07c 1589
1590 // Adding some missing log actions
1591 if ($oldversion < 2006060400) {
1592 // But only if they doesn't exist (because this was introduced after branch and we could be duplicating!)
1593 if (!record_exists('log_display', 'module', 'course', 'action', 'report log')) {
1594 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report log', 'course', 'fullname')");
1595 }
1596 if (!record_exists('log_display', 'module', 'course', 'action', 'report live')) {
1597 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report live', 'course', 'fullname')");
1598 }
1599 if (!record_exists('log_display', 'module', 'course', 'action', 'report outline')) {
1600 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report outline', 'course', 'fullname')");
1601 }
1602 if (!record_exists('log_display', 'module', 'course', 'action', 'report participation')) {
1603 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report participation', 'course', 'fullname')");
1604 }
1605 if (!record_exists('log_display', 'module', 'course', 'action', 'report stats')) {
1606 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report stats', 'course', 'fullname')");
1607 }
1608 }
1609
d836482a 1610 //Renaming lastIP to lastip (all fields lowercase)
1611 if ($oldversion < 2006060900) {
1612 //Not needed unded PG because it stores fieldnames lowecase by default
1613 //Only if it exists (because MOODLE_16_STABLE could have done this work before. Bug 5763)
1614 //$fields = $db->MetaColumnNames($CFG->prefix.'user');
1615 //if (in_array('lastIP',$fields)) {
1616 // table_column("user", "lastIP", "lastip", "varchar", "15", "", "", "", "currentlogin");
1617 //}
1618 }
1619
bbbf2d40 1620
1621 if ($oldversion < 2006080400) {
1622 modify_database('', "CREATE TABLE prefix_role (
1623 id SERIAL PRIMARY KEY,
1624 name varchar(255) NOT NULL default '',
76389cec 1625 shortname varchar(100) NOT NULL default '',
bbbf2d40 1626 description text NOT NULL default '',
1627 sortorder integer NOT NULL default '0'
1628 );");
1629
1630 modify_database('', "CREATE TABLE prefix_context (
1631 id SERIAL PRIMARY KEY,
1632 level integer NOT NULL default 0,
1633 instanceid integer NOT NULL default 0
1634 );");
1635
1636
1637 modify_database('', "CREATE TABLE prefix_role_assignments (
1638 id SERIAL PRIMARY KEY,
2ad25548 1639 roleid integer NOT NULL default 0,
1640 contextid integer NOT NULL default 0,
1641 userid integer NOT NULL default 0,
1642 hidden integer NOT NULL default 0,
1643 timestart integer NOT NULL default 0,
1644 timeend integer NOT NULL default 0,
1645 timemodified integer NOT NULL default 0,
1646 modifierid integer NOT NULL default 0,
bbbf2d40 1647 enrol varchar(20) NOT NULL default '',
2ad25548 1648 sortorder integer NOT NULL default '0'
bbbf2d40 1649 );");
1650
1651 modify_database('', "CREATE TABLE prefix_role_capabilities (
1652 id SERIAL PRIMARY KEY,
2ad25548 1653 contextid integer NOT NULL default 0,
1654 roleid integer NOT NULL default 0,
bbbf2d40 1655 capability varchar(255) NOT NULL default '',
1656 permission integer NOT NULL default 0,
2ad25548 1657 timemodified integer NOT NULL default 0,
1658 modifierid integer NOT NULL default 0
bbbf2d40 1659 );");
1660
1661 modify_database('', "CREATE TABLE prefix_role_deny_grant (
1662 id SERIAL PRIMARY KEY,
1663 roleid integer NOT NULL default '0',
1664 unviewableroleid integer NOT NULL default '0'
1665 );");
1666
1667 modify_database('', "CREATE TABLE prefix_capabilities (
2ad25548 1668 id SERIAL PRIMARY KEY,
7952cb34 1669 name varchar(255) NOT NULL default '',
2ad25548 1670 captype varchar(50) NOT NULL default '',
1671 contextlevel integer NOT NULL default 0,
1672 component varchar(100) NOT NULL default ''
bbbf2d40 1673 );");
2ad25548 1674
1675 modify_database('', "CREATE TABLE prefix_role_names (
1676 id SERIAL PRIMARY KEY,
1677 roleid integer NOT NULL default 0,
1678 contextid integer NOT NULL default 0,
1679 text text NOT NULL default ''
1680 );");
bbbf2d40 1681
1682 }
bcf214df 1683
1684 if ($oldversion < 2006081000) {
1685 modify_database('',"CREATE INDEX prefix_role_sortorder_idx ON prefix_role (sortorder);");
1686 modify_database('',"CREATE INDEX prefix_context_instanceid_idx ON prefix_context (instanceid);");
1687 modify_database('',"CREATE UNIQUE INDEX prefix_context_levelinstanceid_idx ON prefix_context (level, instanceid);");
1688 modify_database('',"CREATE INDEX prefix_role_assignments_roleid_idx ON prefix_role_assignments (roleid);");
1689 modify_database('',"CREATE INDEX prefix_role_assignments_contextidid_idx ON prefix_role_assignments (contextid);");
1690 modify_database('',"CREATE INDEX prefix_role_assignments_userid_idx ON prefix_role_assignments (userid);");
1691 modify_database('',"CREATE UNIQUE INDEX prefix_role_assignments_contextidroleiduserid_idx ON prefix_role_assignments (contextid, roleid, userid);");
1692 modify_database('',"CREATE INDEX prefix_role_assignments_sortorder_idx ON prefix_role_assignments (sortorder);");
1693 modify_database('',"CREATE INDEX prefix_role_capabilities_roleid_idx ON prefix_role_capabilities (roleid);");
1694 modify_database('',"CREATE INDEX prefix_role_capabilities_contextid_idx ON prefix_role_capabilities (contextid);");
1695 modify_database('',"CREATE INDEX prefix_role_capabilities_modifierid_idx ON prefix_role_capabilities (modifierid);");
1696 modify_database('',"CREATE UNIQUE INDEX prefix_role_capabilities_roleidcontextidcapability_idx ON prefix_role_capabilities (roleid, contextid, capability);");
1697 modify_database('',"CREATE INDEX prefix_role_deny_grant_roleid_idx ON prefix_role_deny_grant (roleid);");
1698 modify_database('',"CREATE INDEX prefix_role_deny_grant_unviewableroleid_idx ON prefix_role_deny_grant (unviewableroleid);");
1699 modify_database('',"CREATE UNIQUE INDEX prefix_role_deny_grant_roleidunviewableroleid_idx ON prefix_role_deny_grant (roleid, unviewableroleid);");
2a1ab5ec 1700 modify_database('',"CREATE UNIQUE INDEX prefix_capabilities_name_idx ON prefix_capabilities (name);");
bcf214df 1701 modify_database('',"CREATE INDEX prefix_role_names_roleid_idx ON prefix_role_names (roleid);");
1702 modify_database('',"CREATE INDEX prefix_role_names_contextid_idx ON prefix_role_names (contextid);");
f9e2f481 1703 modify_database('',"CREATE UNIQUE INDEX prefix_role_names_roleidcontextid_idx ON prefix_role_names (roleid, contextid);");
7949356d 1704 }
f9e2f481 1705
1706 if ($oldversion < 2006081700) {
1707 modify_database('',"DROP TABLE prefix_role_deny_grant");
1708
1709 modify_database('',"CREATE TABLE prefix_role_allow_assign (
1710 id SERIAL PRIMARY KEY,
1711 roleid integer NOT NULL default '0',
1712 allowassign integer NOT NULL default '0'
1713 );");
1714
1715 modify_database('',"CREATE INDEX prefix_role_allow_assign_roleid_idx ON prefix_role_allow_assign (roleid);");
1716 modify_database('',"CREATE INDEX prefix_role_allow_assign_allowassign_idx ON prefix_role_allow_assign (allowassign);");
1717 modify_database('',"CREATE UNIQUE INDEX prefix_role_allow_assign_roleidallowassign_idx ON prefix_role_allow_assign (roleid, allowassign);");
1718
1719 modify_database('',"CREATE TABLE prefix_role_allow_override (
1720 id SERIAL PRIMARY KEY,
1721 roleid integer NOT NULL default '0',
1722 allowoverride integer NOT NULL default '0'
1723 );");
1724
1725 modify_database('',"CREATE INDEX prefix_role_allow_override_roleid_idx ON prefix_role_allow_override (roleid);");
1726 modify_database('',"CREATE INDEX prefix_role_allow_override_allowoverride_idx ON prefix_role_allow_override (allowoverride);");
1727 modify_database('',"CREATE UNIQUE INDEX prefix_role_allow_override_roleidallowoverride_idx ON prefix_role_allow_override (roleid, allowoverride);");
1728
bcf214df 1729 }
d140ad3f 1730
1731 if ($oldversion < 2006082100) {
1732 execute_sql("DROP INDEX {$CFG->prefix}context_levelinstanceid_idx;",false);
1733 table_column('context', 'level', 'aggregatelevel', 'integer', '10', 'unsigned', '0', 'not null', '');
1734 modify_database('',"CREATE UNIQUE INDEX prefix_context_aggregatelevelinstanceid_idx ON prefix_context (aggregatelevel, instanceid);");
1735 }
598dc3a9 1736
1737 if ($oldversion < 2006082200) {
1738 table_column('timezone', 'rule', 'tzrule', 'varchar', '20', '', '', 'not null', '');
1739 }
1740
67a3fac5 1741 if ($oldversion < 2006082800) {
1742 table_column('user', '', 'ajax', 'integer', '1', 'unsigned', '1', '', 'htmleditor');
1743 }
1744
647a2566 1745 if ($oldversion < 2006082900) {
e9d5166a 1746 execute_sql("DROP TABLE {$CFG->prefix}sessions", true);
647a2566 1747 execute_sql("
1748 CREATE TABLE {$CFG->prefix}sessions2 (
1749 sesskey VARCHAR(255) NOT NULL default '',
1750 expiry TIMESTAMP NOT NULL,
1751 expireref VARCHAR(255),
1752 created TIMESTAMP NOT NULL,
1753 modified TIMESTAMP NOT NULL,
1754 sessdata TEXT,
1755 CONSTRAINT {$CFG->prefix}sess_ses_pk PRIMARY KEY (sesskey)
1756 );", true);
1757
1758 execute_sql("
1759 CREATE INDEX {$CFG->prefix}sess_exp_ix ON {$CFG->prefix}sessions2 (expiry);", true);
1760 execute_sql("
1761 CREATE INDEX {$CFG->prefix}sess_exp2_ix ON {$CFG->prefix}sessions2 (expireref);", true);
1762 }
be4486da 1763
1764 if ($oldversion < 2006083002) {
1765 table_column('capabilities', '', 'riskbitmask', 'INTEGER', '10', 'unsigned', '0', 'not null', '');
1766 }
647a2566 1767
99215f2c 1768 if ($oldversion < 2006083100) {
1769 execute_sql("ALTER TABLE {$CFG->prefix}course ALTER COLUMN modinfo DROP NOT NULL");
1770 execute_sql("ALTER TABLE {$CFG->prefix}course ALTER COLUMN modinfo DROP DEFAULT");
1771 }
1772
6c76032b 1773 if ($oldversion < 2006083101) {
1774 execute_sql("ALTER TABLE {$CFG->prefix}course_categories ALTER COLUMN description DROP NOT NULL");
1775 execute_sql("ALTER TABLE {$CFG->prefix}course_categories ALTER COLUMN description DROP DEFAULT");
1776 }
1777
5f941d12 1778 if ($oldversion < 2006083102) {
1779 execute_sql("ALTER TABLE {$CFG->prefix}user ALTER COLUMN description DROP NOT NULL");
1780 execute_sql("ALTER TABLE {$CFG->prefix}user ALTER COLUMN description DROP DEFAULT");
1781 }
1782
13317458 1783 if ($oldversion < 2006090200) {
1784 execute_sql("ALTER TABLE {$CFG->prefix}course_sections ALTER COLUMN summary DROP NOT NULL");
1785 execute_sql("ALTER TABLE {$CFG->prefix}course_sections ALTER COLUMN summary DROP DEFAULT");
1786 execute_sql("ALTER TABLE {$CFG->prefix}course_sections ALTER COLUMN sequence DROP NOT NULL");
1787 execute_sql("ALTER TABLE {$CFG->prefix}course_sections ALTER COLUMN sequence DROP DEFAULT");
1788 }
1789
b277dd0a 1790 // table to keep track of course page access times, used in online participants block, and participants list
1791 if ($oldversion < 2006091200) {
1792 execute_sql("CREATE TABLE {$CFG->prefix}user_lastaccess (
1793 id SERIAL PRIMARY KEY,
1794 userid integer NOT NULL default 0,
1795 courseid integer NOT NULL default 0,
1796 timeaccess integer NOT NULL default 0
1797 );", true);
1798
1799 execute_sql("CREATE INDEX {$CFG->prefix}user_lastaccess_userid_idx ON {$CFG->prefix}user_lastaccess (userid);", true);
1800 execute_sql("CREATE INDEX {$CFG->prefix}user_lastaccess_courseid_idx ON {$CFG->prefix}user_lastaccess (courseid);", true);
1801 execute_sql("CREATE UNIQUE INDEX {$CFG->prefix}user_lastaccess_useridcourseid_idx ON {$CFG->prefix}user_lastaccess (userid, courseid);", true);
1802
1803 }
8d158ed4 1804
0ad8c785 1805 if ($oldversion < 2006091212) { // Reload the guest roles completely with new defaults
8d158ed4 1806 if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
1807 delete_records('capabilities');
1808 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
1809 foreach ($guestroles as $guestrole) {
1810 delete_records('role_capabilities', 'roleid', $guestrole->id);
1811 assign_capability('moodle/legacy:guest', CAP_ALLOW, $guestrole->id, $sitecontext->id);
1812 }
1813 }
1814 }
1815
746a04c5 1816 if ($oldversion < 2006091700) {
b29ab53d 1817 table_column('course','','defaultrole','integer','10', 'unsigned', '0', 'not null');
746a04c5 1818 }
1819
dca1937c 1820 if ($oldversion < 2006091800) {
1821 delete_records('config', 'name', 'showsiteparticipantslist');
1822 delete_records('config', 'name', 'requestedteachername');
1823 delete_records('config', 'name', 'requestedteachersname');
1824 delete_records('config', 'name', 'requestedstudentname');
1825 delete_records('config', 'name', 'requestedstudentsname');
1826 }
1827
45b9781e 1828 if ($oldversion < 2006091901) {
5d0df39d 1829 if ($roles = get_records('role')) {
1830 $first = array_shift($roles);
1831 if (!empty($first->shortname)) {
1832 // shortnames already exist
1833 } else {
1834 table_column('role', '', 'shortname', 'varchar', '100', '', '', 'not null', 'name');
1835 $legacy_names = array('admin', 'coursecreator', 'editingteacher', 'teacher', 'student', 'guest');
1836 foreach ($legacy_names as $name) {
1837 if ($roles = get_roles_with_capability('moodle/legacy:'.$name, CAP_ALLOW)) {
1838 $i = '';
1839 foreach ($roles as $role) {
1840 if (empty($role->shortname)) {
1841 $updated = new object();
1842 $updated->id = $role->id;
1843 $updated->shortname = $name.$i;
1844 update_record('role', $updated);
1845 $i++;
1846 }
45b9781e 1847 }
1848 }
1849 }
1850 }
1851 }
1852 }
1853
adecf1c4 1854 /// Tables for customisable user profile fields
1855 if ($oldversion < 2006092000) {
1856 execute_sql("CREATE TABLE {$CFG->prefix}user_info_field (
1857 id BIGSERIAL,
1858 name VARCHAR(255) NOT NULL default '',
1859 datatype VARCHAR(255) NOT NULL default '',
1860 categoryid BIGINT NOT NULL default 0,
1861 sortorder BIGINT NOT NULL default 0,
1862 required SMALLINT NOT NULL default 0,
1863 locked SMALLINT NOT NULL default 0,
1864 visible SMALLINT NOT NULL default 0,
1865 defaultdata TEXT,
1866 CONSTRAINT {$CFG->prefix}userinfofiel_id_pk PRIMARY KEY (id));", true);
1867
1868 execute_sql("COMMENT ON TABLE {$CFG->prefix}user_info_field IS 'Customisable user profile fields';", true);
1869
1870 execute_sql("CREATE TABLE {$CFG->prefix}user_info_category (
1871 id BIGSERIAL,
1872 name VARCHAR(255) NOT NULL default '',
1873 sortorder BIGINT NOT NULL default 0,
1874 CONSTRAINT {$CFG->prefix}userinfocate_id_pk PRIMARY KEY (id));", true);
1875
1876 execute_sql("COMMENT ON TABLE {$CFG->prefix}user_info_category IS 'Customisable fields categories';", true);
1877
1878 execute_sql("CREATE TABLE {$CFG->prefix}user_info_data (
1879 id BIGSERIAL,
1880 userid BIGINT NOT NULL default 0,
1881 fieldid BIGINT NOT NULL default 0,
1882 data TEXT NOT NULL,
1883 CONSTRAINT {$CFG->prefix}userinfodata_id_pk PRIMARY KEY (id));", true);
1884
1885 execute_sql("COMMENT ON TABLE {$CFG->prefix}user_info_data IS 'Data for the customisable user fields';", true);
1886
1887 }
1888
aad2ba95 1889 if ($oldversion < 2006092200) {
aad2ba95 1890 table_column('context', 'aggregatelevel', 'contextlevel', 'int', '10', 'unsigned', '0', 'not null', '');
01c65218 1891/* execute_sql("ALTER TABLE `{$CFG->prefix}context` DROP INDEX `aggregatelevel-instanceid`;",false);
1892 execute_sql("ALTER TABLE `{$CFG->prefix}context` ADD UNIQUE INDEX `contextlevel-instanceid` (`contextlevel`, `instanceid`)",false); // see 2006092409 below */
aad2ba95 1893 }
1894
ece4945b 1895 if ($oldversion < 2006092302) {
1896 // fix sortorder first if needed
1897 if ($roles = get_all_roles()) {
1898 $i = 0;
1899 foreach ($roles as $rolex) {
1900 if ($rolex->sortorder != $i) {
1901 $r = new object();
1902 $r->id = $rolex->id;
1903 $r->sortorder = $i;
1904 update_record('role', $r);
1905 }
1906 $i++;
1907 }
1908 }
1cc309df 1909/* execute_sql("ALTER TABLE {$CFG->prefix}role DROP INDEX {$CFG->prefix}role_sor_ix;");
1910 execute_sql("ALTER TABLE {$CFG->prefix}role ADD UNIQUE INDEX {$CFG->prefix}role_sor_uix (sortorder)");*/
ece4945b 1911 }
adecf1c4 1912
25d8040e 1913 if ($oldversion < 2006092400) {
1914 table_column('user', '', 'trustbitmask', 'INTEGER', '10', 'unsigned', '0', 'not null', '');
1915 }
1916
1cc309df 1917 if ($oldversion < 2006092409) {
1918 // ok, once more and now correctly!
1919 execute_sql("DROP INDEX \"aggregatelevel-instanceid\";", false);
1920 execute_sql("DROP INDEX \"contextlevel-instanceid\";", false);
1921 execute_sql("CREATE UNIQUE INDEX {$CFG->prefix}cont_conins_uix ON {$CFG->prefix}context (contextlevel, instanceid);", false);
1922
1923 execute_sql("DROP INDEX {$CFG->prefix}role_sor_ix;", false);
1924 execute_sql("DROP INDEX {$CFG->prefix}role_sor_uix;", false);
1925 execute_sql("CREATE UNIQUE INDEX {$CFG->prefix}role_sor_uix ON {$CFG->prefix}role (sortorder);", false);
1926 }
1927
e920529b 1928 if ($oldversion < 2006092410) {
26b82be2 1929 /// Convert all the PG unique keys into their corresponding unique indexes
1930 /// we don't want such keys inside Moodle 1.7 and above
1931 /// Look for all the UNIQUE CONSTRAINSTS existing in DB
1932 $uniquecons = get_records_sql ("SELECT conname, relname, conkey, clas.oid AS tableoid
1933 FROM pg_constraint cons,
1934 pg_class clas
1935 WHERE cons.contype='u'
1936 AND cons.conrelid = clas.oid");
1937 /// Iterate over every unique constraint, calculating its fields
1938 if ($uniquecons) {
1939 foreach ($uniquecons as $uniquecon) {
1940 $conscols = trim(trim($uniquecon->conkey, '}'), '{');
1941 $conscols = explode(',', $conscols);
1942 /// Iterate over each column to fetch its name
1943 $indexcols = array();
1944 foreach ($conscols as $conscol) {
1945 $column = get_record_sql ("SELECT attname, attname
1946 FROM pg_attribute
1947 WHERE attrelid = $uniquecon->tableoid
1948 AND attnum = $conscol");
1949 $indexcols[] = $column->attname;
1950 }
1951 /// Drop the old UNIQUE CONSTRAINT
1952 execute_sql ("ALTER TABLE $uniquecon->relname DROP CONSTRAINT $uniquecon->conname", false);
1953 /// Create the new UNIQUE INDEX
1954 execute_sql ("CREATE UNIQUE INDEX {$uniquecon->relname}_".implode('_', $indexcols)."_uix ON $uniquecon->relname (".implode(', ', $indexcols).')', false);
1955 }
1956 }
1957 }
1958
2efd67bf 1959 if ($oldversion < 2006092601) {
1960 table_column('log_display', 'field', 'field', 'varchar', '200', '', '', 'not null', '');
1961 }
25d8040e 1962
17f3e7d0 1963 return $result;
e40488f8 1964}
f374fb10 1965
462458c6 1966?>