Fixed the upgrade from 1.6.x. The role->shortname field was not being created.
[moodle.git] / lib / db / mysql.php
CommitLineData
31f0900c 1<?PHP //$Id$
2//
3// This file keeps track of upgrades to Moodle.
10ee08cc 4//
5// Sometimes, changes between versions involve
6// alterations to database structures and other
7// major things that may break installations.
31f0900c 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//
10ee08cc 13// If there's something it cannot do itself, it
31f0900c 14// will tell you what you need to do.
15//
16// Versions are defined by /version.php
17//
18// This file is tailored to MySQL
19
e7311a0a 20function main_upgrade($oldversion=0) {
31f0900c 21
4d744a22 22 global $CFG, $THEME, $db;
7beb45d8 23
047d30d1 24 $result = true;
25
31f0900c 26 if ($oldversion == 0) {
27 execute_sql("
28 CREATE TABLE `config` (
29 `id` int(10) unsigned NOT NULL auto_increment,
30 `name` varchar(255) NOT NULL default '',
31 `value` varchar(255) NOT NULL default '',
32 PRIMARY KEY (`id`),
33 UNIQUE KEY `name` (`name`)
34 ) COMMENT='Moodle configuration variables';");
35 notify("Created a new table 'config' to hold configuration data");
36 }
37 if ($oldversion < 2002073100) {
38 execute_sql(" DELETE FROM `modules` WHERE `name` = 'chat' ");
39 }
40 if ($oldversion < 2002080200) {
41 execute_sql(" ALTER TABLE `modules` DROP `fullname` ");
42 execute_sql(" ALTER TABLE `modules` DROP `search` ");
43 }
44 if ($oldversion < 2002080300) {
45 execute_sql(" ALTER TABLE `log_display` CHANGE `table` `mtable` VARCHAR( 20 ) NOT NULL ");
46 execute_sql(" ALTER TABLE `user_teachers` CHANGE `authority` `authority` TINYINT( 3 ) DEFAULT '3' NOT NULL ");
47 }
48 if ($oldversion < 2002082100) {
49 execute_sql(" ALTER TABLE `course` CHANGE `guest` `guest` TINYINT(2) UNSIGNED DEFAULT '0' NOT NULL ");
50 }
51 if ($oldversion < 2002082101) {
52 execute_sql(" ALTER TABLE `user` ADD `maildisplay` TINYINT(2) UNSIGNED DEFAULT '2' NOT NULL AFTER `mailformat` ");
53 }
54 if ($oldversion < 2002090100) {
55 execute_sql(" ALTER TABLE `course_sections` CHANGE `summary` `summary` TEXT NOT NULL ");
56 }
57 if ($oldversion < 2002090701) {
58 execute_sql(" ALTER TABLE `user_teachers` CHANGE `authority` `authority` TINYINT( 10 ) DEFAULT '3' NOT NULL ");
59 execute_sql(" ALTER TABLE `user_teachers` ADD `role` VARCHAR(40) NOT NULL AFTER `authority` ");
60 }
61 if ($oldversion < 2002090800) {
62 execute_sql(" ALTER TABLE `course` ADD `teachers` VARCHAR( 100 ) DEFAULT 'Teachers' NOT NULL AFTER `teacher` ");
63 execute_sql(" ALTER TABLE `course` ADD `students` VARCHAR( 100 ) DEFAULT 'Students' NOT NULL AFTER `student` ");
64 }
65 if ($oldversion < 2002091000) {
cb24115c 66 execute_sql(" ALTER TABLE `user` CHANGE `personality` `secret` VARCHAR( 15 ) NOT NULL DEFAULT '' ");
31f0900c 67 }
68 if ($oldversion < 2002091400) {
69 execute_sql(" ALTER TABLE `user` ADD `lang` VARCHAR( 3 ) DEFAULT 'en' NOT NULL AFTER `country` ");
70 }
71 if ($oldversion < 2002091900) {
72 notify("Most Moodle configuration variables have been moved to the database and can now be edited via the admin page.");
73 notify("Although it is not vital that you do so, you might want to edit <U>config.php</U> and remove all the unused settings (except the database, URL and directory definitions). See <U>config-dist.php</U> for an example of how your new slim config.php should look.");
74 }
75 if ($oldversion < 2002092000) {
76 execute_sql(" ALTER TABLE `user` CHANGE `lang` `lang` VARCHAR(5) DEFAULT 'en' NOT NULL ");
77 }
78 if ($oldversion < 2002092100) {
79 execute_sql(" ALTER TABLE `user` ADD `deleted` TINYINT(1) UNSIGNED DEFAULT '0' NOT NULL AFTER `confirmed` ");
80 }
0095d5cd 81 if ($oldversion < 2002101001) {
82 execute_sql(" ALTER TABLE `user` ADD `htmleditor` TINYINT(1) UNSIGNED DEFAULT '1' NOT NULL AFTER `maildisplay` ");
83 }
2a439ba7 84 if ($oldversion < 2002101701) {
85 execute_sql(" ALTER TABLE `reading` RENAME `resource` "); // Small line with big consequences!
10ee08cc 86 execute_sql(" DELETE FROM `log_display` WHERE module = 'reading'");
c343bc88 87 execute_sql(" INSERT INTO log_display (module, action, mtable, field) VALUES ('resource', 'view', 'resource', 'name') ");
2a439ba7 88 execute_sql(" UPDATE log SET module = 'resource' WHERE module = 'reading' ");
89 execute_sql(" UPDATE modules SET name = 'resource' WHERE name = 'reading' ");
90 }
31f0900c 91
7beb45d8 92 if ($oldversion < 2002102503) {
f82c2d42 93 execute_sql(" ALTER TABLE `course` ADD `modinfo` TEXT NOT NULL AFTER `format` ");
7beb45d8 94 require_once("$CFG->dirroot/mod/forum/lib.php");
95 require_once("$CFG->dirroot/course/lib.php");
96
97 if (! $module = get_record("modules", "name", "forum")) {
98 notify("Could not find forum module!!");
99 return false;
100 }
101
102 // First upgrade the site forums
103 if ($site = get_site()) {
104 print_heading("Making News forums editable for main site (moving to section 1)...");
105 if ($news = forum_get_course_forum($site->id, "news")) {
106 $mod->course = $site->id;
107 $mod->module = $module->id;
108 $mod->instance = $news->id;
109 $mod->section = 1;
110 if (! $mod->coursemodule = add_course_module($mod) ) {
111 notify("Could not add a new course module to the site");
112 return false;
113 }
114 if (! $sectionid = add_mod_to_section($mod) ) {
115 notify("Could not add the new course module to that section");
116 return false;
117 }
118 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
119 notify("Could not update the course module with the correct section");
120 return false;
121 }
122 }
123 }
124
125
126 // Now upgrade the courses.
127 if ($courses = get_records_sql("SELECT * FROM course WHERE category > 0")) {
128 print_heading("Making News and Social forums editable for each course (moving to section 0)...");
129 foreach ($courses as $course) {
130 if ($course->format == "social") { // we won't touch them
131 continue;
132 }
133 if ($news = forum_get_course_forum($course->id, "news")) {
134 $mod->course = $course->id;
135 $mod->module = $module->id;
136 $mod->instance = $news->id;
137 $mod->section = 0;
138 if (! $mod->coursemodule = add_course_module($mod) ) {
139 notify("Could not add a new course module to the course '$course->fullname'");
140 return false;
141 }
142 if (! $sectionid = add_mod_to_section($mod) ) {
143 notify("Could not add the new course module to that section");
144 return false;
145 }
146 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
147 notify("Could not update the course module with the correct section");
148 return false;
149 }
150 }
151 if ($social = forum_get_course_forum($course->id, "social")) {
152 $mod->course = $course->id;
153 $mod->module = $module->id;
154 $mod->instance = $social->id;
155 $mod->section = 0;
156 if (! $mod->coursemodule = add_course_module($mod) ) {
157 notify("Could not add a new course module to the course '$course->fullname'");
158 return false;
159 }
160 if (! $sectionid = add_mod_to_section($mod) ) {
161 notify("Could not add the new course module to that section");
162 return false;
163 }
164 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
165 notify("Could not update the course module with the correct section");
166 return false;
167 }
168 }
169 }
170 }
171 }
172
d897cae4 173 if ($oldversion < 2002111003) {
174 execute_sql(" ALTER TABLE `course` ADD `modinfo` TEXT NOT NULL AFTER `format` ");
175 if ($courses = get_records_sql("SELECT * FROM course")) {
176 require_once("$CFG->dirroot/course/lib.php");
177 foreach ($courses as $course) {
10ee08cc 178
d897cae4 179 $modinfo = serialize(get_array_of_activities($course->id));
180
181 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
182 notify("Could not cache module information for course '$course->fullname'!");
183 }
184 }
185 }
186 }
187
3635d82f 188 if ($oldversion < 2002111100) {
189 print_simple_box_start("CENTER", "", "#FFCCCC");
190 echo "<FONT SIZE=+1>";
191 echo "<P>Changes have been made to all built-in themes, to add the new popup navigation menu.";
192 echo "<P>If you have customised themes, you will need to edit theme/xxxx/header.html as follows:";
193 echo "<UL><LI>Change anywhere it says <B>$"."button</B> to say <B>$"."menu</B>";
194 echo "<LI>Add <B>$"."button</B> elsewhere (eg at the end of the navigation bar)</UL>";
195 echo "<P>See the standard themes for examples, eg: theme/standard/header.html";
196 print_simple_box_end();
197 }
198
13beeacb 199 if ($oldversion < 2002111200) {
200 execute_sql(" ALTER TABLE `course` ADD `showrecent` TINYINT(5) UNSIGNED DEFAULT '1' NOT NULL AFTER `numsections` ");
201 }
202
f8ef7d66 203 if ($oldversion < 2002111400) {
204 // Rebuild all course caches, because some may not be done in new installs (eg site page)
205 if ($courses = get_records_sql("SELECT * FROM course")) {
206 require_once("$CFG->dirroot/course/lib.php");
207 foreach ($courses as $course) {
10ee08cc 208
f8ef7d66 209 $modinfo = serialize(get_array_of_activities($course->id));
210
211 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
212 notify("Could not cache module information for course '$course->fullname'!");
213 }
214 }
215 }
216 }
217
277d2d9f 218 if ($oldversion < 2002112000) {
219 set_config("guestloginbutton", 1);
220 }
221
ebc3bd2b 222 if ($oldversion < 2002122300) {
223 execute_sql("ALTER TABLE `log` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
224 execute_sql("ALTER TABLE `user_admins` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
225 execute_sql("ALTER TABLE `user_students` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
226 execute_sql("ALTER TABLE `user_teachers` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
227 execute_sql("ALTER TABLE `user_students` CHANGE `start` `timestart` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
228 execute_sql("ALTER TABLE `user_students` CHANGE `end` `timeend` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
229 }
230
9d378732 231 if ($oldversion < 2002122700) {
05a6c03e 232 if (! record_exists("log_display", "module", "user", "action", "view")) {
c343bc88 233 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('user', 'view', 'user', 'CONCAT(firstname,' ',lastname)') ");
a3eea09c 234 }
235 }
db70b54b 236 if ($oldversion < 2003010101) {
14e4a496 237 delete_records("log_display", "module", "user");
238 $new->module = "user";
239 $new->action = "view";
240 $new->mtable = "user";
db70b54b 241 $new->field = "CONCAT(firstname,\" \",lastname)";
14e4a496 242 insert_record("log_display", $new);
243
244 delete_records("log_display", "module", "course");
245 $new->module = "course";
246 $new->action = "view";
247 $new->mtable = "course";
248 $new->field = "fullname";
249 insert_record("log_display", $new);
250 $new->action = "update";
251 insert_record("log_display", $new);
db70b54b 252 $new->action = "enrol";
253 insert_record("log_display", $new);
14e4a496 254 }
d897cae4 255
811c88b3 256 if ($oldversion < 2003012200) {
718ad19f 257 // execute_sql(" ALTER TABLE `log_display` CHANGE `module` `module` VARCHAR( 20 ) NOT NULL ");
258 // Commented out - see below where it's done properly
811c88b3 259 }
260
dcd338ff 261 if ($oldversion < 2003032500) {
262 modify_database("", "CREATE TABLE `prefix_user_coursecreators` (
2fdb54b8 263 `id` int(10) unsigned NOT NULL auto_increment,
264 `userid` int(10) unsigned NOT NULL default '0',
265 PRIMARY KEY (`id`),
266 UNIQUE KEY `id` (`id`)
267 ) TYPE=MyISAM COMMENT='One record per course creator';");
dcd338ff 268 }
269 if ($oldversion < 2003032602) {
264d4e0b 270 // Redoing it because of no prefix last time
271 execute_sql(" ALTER TABLE `{$CFG->prefix}log_display` CHANGE `module` `module` VARCHAR( 20 ) NOT NULL ");
272 // Add some indexes for speed
273 execute_sql(" ALTER TABLE `{$CFG->prefix}log` ADD INDEX(course) ");
274 execute_sql(" ALTER TABLE `{$CFG->prefix}log` ADD INDEX(userid) ");
275 }
10ee08cc 276
dcd338ff 277 if ($oldversion < 2003041400) {
1acfbce5 278 table_column("course_modules", "", "visible", "integer", "1", "unsigned", "1", "not null", "score");
279 }
1924074c 280
dcd338ff 281 if ($oldversion < 2003042104) { // Try to update permissions of all files
718ad19f 282 if ($files = get_directory_list($CFG->dataroot)) {
283 echo "Attempting to update permissions for all files... ignore any errors.";
284 foreach ($files as $file) {
839f2456 285 echo "$CFG->dataroot/$file<br />";
b369ff55 286 @chmod("$CFG->dataroot/$file", $CFG->directorypermissions);
718ad19f 287 }
288 }
289 }
290
13df5aee 291 if ($oldversion < 2003042400) {
292 // Rebuild all course caches, because of changes to do with visible variable
293 if ($courses = get_records_sql("SELECT * FROM {$CFG->prefix}course")) {
294 require_once("$CFG->dirroot/course/lib.php");
295 foreach ($courses as $course) {
296 $modinfo = serialize(get_array_of_activities($course->id));
297
298 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
299 notify("Could not cache module information for course '$course->fullname'!");
300 }
301 }
302 }
303 }
304
10ee08cc 305 if ($oldversion < 2003042500) {
306 // Convert all usernames to lowercase.
307 $users = get_records_sql("SELECT id, username FROM {$CFG->prefix}user");
047d30d1 308 $cerrors = "";
309 $rarray = array();
310
311 foreach ($users as $user) { // Check for possible conflicts
312 $lcname = trim(moodle_strtolower($user->username));
313 if (in_array($lcname, $rarray)) {
10ee08cc 314 $cerrors .= $user->id."->".$lcname.'<br/>' ;
047d30d1 315 } else {
316 array_push($rarray,$lcname);
317 }
318 }
319
320 if ($cerrors != '') {
10ee08cc 321 notify("Error: Cannot convert usernames to lowercase.
322 Following usernames would overlap (id->username):<br/> $cerrors .
323 Please resolve overlapping errors.");
047d30d1 324 $result = false;
325 }
326
327 $cerrors = "";
839f2456 328 echo "Checking userdatabase:<br />";
047d30d1 329 foreach ($users as $user) {
330 $lcname = trim(moodle_strtolower($user->username));
331 if ($lcname != $user->username) {
332 $convert = set_field("user" , "username" , $lcname, "id", $user->id);
333 if (!$convert) {
334 if ($cerrors){
335 $cerrors .= ", ";
10ee08cc 336 }
047d30d1 337 $cerrors .= $item;
338 } else {
339 echo ".";
10ee08cc 340 }
047d30d1 341 }
342 }
343 if ($cerrors != '') {
10ee08cc 344 notify("There were errors when converting following usernames to lowercase.
047d30d1 345 '$cerrors' . Sorry, but you will need to fix your database by hand.");
346 $result = false;
347 }
348 }
349
1ba39602 350 if ($oldversion < 2003042600) {
f9ce68ee 351 /// Some more indexes - we need all the help we can get on the logs
352 //execute_sql(" ALTER TABLE `{$CFG->prefix}log` ADD INDEX(module) ");
353 //execute_sql(" ALTER TABLE `{$CFG->prefix}log` ADD INDEX(action) ");
354 }
355
356 if ($oldversion < 2003042700) {
357 /// Changing to multiple indexes
358 execute_sql(" ALTER TABLE `{$CFG->prefix}log` DROP INDEX module ", false);
359 execute_sql(" ALTER TABLE `{$CFG->prefix}log` DROP INDEX action ", false);
360 execute_sql(" ALTER TABLE `{$CFG->prefix}log` DROP INDEX course ", false);
361 execute_sql(" ALTER TABLE `{$CFG->prefix}log` DROP INDEX userid ", false);
362 execute_sql(" ALTER TABLE `{$CFG->prefix}log` ADD INDEX coursemoduleaction (course,module,action) ");
363 execute_sql(" ALTER TABLE `{$CFG->prefix}log` ADD INDEX courseuserid (course,userid) ");
6459f225 364 }
365
b86fc0e2 366 if ($oldversion < 2003042801) {
367 execute_sql("CREATE TABLE `{$CFG->prefix}course_display` (
368 `id` int(10) unsigned NOT NULL auto_increment,
369 `course` int(10) unsigned NOT NULL default '0',
370 `userid` int(10) unsigned NOT NULL default '0',
371 `display` int(10) NOT NULL default '0',
372 PRIMARY KEY (`id`),
373 UNIQUE KEY `id` (`id`),
374 KEY `courseuserid` (course,userid)
375 ) TYPE=MyISAM COMMENT='Stores info about how to display the course'");
376 }
377
7d99d695 378 if ($oldversion < 2003050400) {
379 table_column("course_sections", "", "visible", "integer", "1", "unsigned", "1", "", "");
380 }
381
7e6b0b3b 382 if ($oldversion < 2003050900) {
383 table_column("modules", "", "visible", "integer", "1", "unsigned", "1", "", "");
384 }
385
5867bfb5 386 if ($oldversion < 2003050902) {
387 if (get_records("modules", "name", "pgassignment")) {
7adf6787 388 print_simple_box("Note: the pgassignment module has been removed (it will be replaced later by the workshop module). Go to the new 'Manage Modules' page and DELETE IT from your system", "center", "50%", "$THEME->cellheading", "20", "noticebox");
5867bfb5 389 }
390 }
391
7adf6787 392 if ($oldversion < 2003051600) {
393 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");
394 }
395
7f2a3e67 396 if ($oldversion < 2003052300) {
397 table_column("user", "", "autosubscribe", "integer", "1", "unsigned", "1", "", "htmleditor");
398 }
399
a6d82c3f 400 if ($oldversion < 2003072100) {
401 table_column("course", "", "visible", "integer", "1", "unsigned", "1", "", "marker");
402 }
403
dd0bd508 404 if ($oldversion < 2003072101) {
405 table_column("course_sections", "sequence", "sequence", "text", "", "", "", "", "");
406 }
407
a8fa25d1 408 if ($oldversion < 2003072800) {
409 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 410 flush();
a8fa25d1 411 execute_sql(" ALTER TABLE `{$CFG->prefix}log` ADD INDEX timecoursemoduleaction (time,course,module,action) ");
412 execute_sql(" ALTER TABLE `{$CFG->prefix}user_students` ADD INDEX courseuserid (course,userid) ");
413 execute_sql(" ALTER TABLE `{$CFG->prefix}user_teachers` ADD INDEX courseuserid (course,userid) ");
414 }
415
c2cb4545 416 if ($oldversion < 2003072803) {
417 table_column("course_categories", "", "description", "text", "", "", "");
418 table_column("course_categories", "", "parent", "integer", "10", "unsigned");
419 table_column("course_categories", "", "sortorder", "integer", "10", "unsigned");
420 table_column("course_categories", "", "courseorder", "text", "", "", "");
421 table_column("course_categories", "", "visible", "integer", "1", "unsigned", "1");
422 table_column("course_categories", "", "timemodified", "integer", "10", "unsigned");
423 }
424
d2b6ba70 425 if ($oldversion < 2003080400) {
426 table_column("course_categories", "courseorder", "courseorder", "integer", "10", "unsigned");
427 table_column("course", "", "sortorder", "integer", "10", "unsigned", "0", "", "category");
428 }
429
430 if ($oldversion < 2003080700) {
431 notify("Cleaning up categories and course ordering...");
6bc502cc 432 fix_course_sortorder();
d2b6ba70 433 }
434
50b5487c 435 if ($oldversion < 2003081001) {
436 table_column("course", "format", "format", "varchar", "10", "", "topics");
437 }
438
02ebf404 439 if ($oldversion < 2003081500) {
5fcd1b52 440// print_simple_box("Some important changes have been made to how course creators work. Formerly, they could create new courses and assign teachers, and teachers could edit courses. Now, ordinary teachers can no longer edit courses - they <b>need to be a teacher of a course AND a course creator</b>. A new site-wide configuration variable allows you to choose whether to allow course creators to create new courses as well (by default this is off). <p>The following update will automatically convert all your existing teachers into course creators, to maintain backward compatibility. Make sure you look at your upgraded site carefully and understand these new changes.", "center", "50%", "$THEME->cellheading", "20", "noticebox");
10ee08cc 441
5fcd1b52 442// $count = 0;
443// $errorcount = 0;
444// if ($teachers = get_records("user_teachers")) {
445// foreach ($teachers as $teacher) {
446// if (! record_exists("user_coursecreators", "userid", $teacher->userid)) {
447// $creator = NULL;
448// $creator->userid = $teacher->userid;
449// if (!insert_record("user_coursecreators", $creator)) {
450// $errorcount++;
451// } else {
452// $count++;
453// }
454// }
455// }
456// }
457// print_simple_box("$count teachers were upgraded to course creators (with $errorcount errors)", "center", "50%", "$THEME->cellheading", "20", "noticebox");
02ebf404 458
459 }
460
37eef3ed 461 if ($oldversion < 2003081501) {
462 execute_sql(" CREATE TABLE `{$CFG->prefix}scale` (
463 `id` int(10) unsigned NOT NULL auto_increment,
464 `courseid` int(10) unsigned NOT NULL default '0',
465 `userid` int(10) unsigned NOT NULL default '0',
466 `name` varchar(255) NOT NULL default '',
467 `scale` text NOT NULL,
468 `description` text NOT NULL,
469 `timemodified` int(10) unsigned NOT NULL default '0',
470 PRIMARY KEY (id)
471 ) TYPE=MyISAM COMMENT='Defines grading scales'");
37eef3ed 472
6f4f04df 473 }
11402bbd 474
475 if ($oldversion < 2003081503) {
6f4f04df 476 table_column("forum", "", "scale", "integer", "10", "unsigned", "0", "", "assessed");
11402bbd 477 get_scales_menu(0); // Just to force the default scale to be created
478 }
479
73047f2f 480 if ($oldversion < 2003081600) {
481 table_column("user_teachers", "", "editall", "integer", "1", "unsigned", "1", "", "role");
482 table_column("user_teachers", "", "timemodified", "integer", "10", "unsigned", "0", "", "editall");
483 }
11402bbd 484
9936fe81 485 if ($oldversion < 2003081900) {
486 table_column("course_categories", "courseorder", "coursecount", "integer", "10", "unsigned", "0");
487 }
488
fff79722 489 if ($oldversion < 2003082001) {
490 table_column("course", "", "showgrades", "integer", "2", "unsigned", "1", "", "format");
491 }
492
3052e775 493 if ($oldversion < 2003082101) {
494 execute_sql(" ALTER TABLE `{$CFG->prefix}course` ADD INDEX category (category) ");
495 }
d0117715 496 if ($oldversion < 2003082702) {
497 execute_sql(" INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'user report', 'user', 'CONCAT(firstname,\" \",lastname)') ");
498 }
3052e775 499
aac94fd0 500 if ($oldversion < 2003091400) {
501 table_column("course_modules", "", "indent", "integer", "5", "unsigned", "0", "", "score");
502 }
503
4909e176 504 if ($oldversion < 2003092900) {
505 table_column("course", "", "maxbytes", "integer", "10", "unsigned", "0", "", "marker");
506 }
507
4d744a22 508 if ($oldversion < 2003102700) {
509 table_column("user_students", "", "timeaccess", "integer", "10", "unsigned", "0", "", "time");
510 table_column("user_teachers", "", "timeaccess", "integer", "10", "unsigned", "0", "", "timemodified");
511
4d744a22 512 $db->debug = false;
29b25b04 513 $CFG->debug = 0;
514 notify("Calculating access times. Please wait - this may take a long time on big sites...", "green");
515 flush();
516
517 if ($courses = get_records_select("course", "category > 0")) {
518 foreach ($courses as $course) {
519 notify("Processing $course->fullname ...", "green");
520 flush();
10ee08cc 521 if ($users = get_records_select("user_teachers", "course = '$course->id'",
29b25b04 522 "id", "id, userid, timeaccess")) {
523 foreach ($users as $user) {
524 $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log WHERE course = '$course->id' and userid = '$user->userid' ORDER by time DESC");
525 if (empty($loginfo->time)) {
526 $loginfo->time = 0;
527 }
10ee08cc 528 execute_sql("UPDATE {$CFG->prefix}user_teachers SET timeaccess = '$loginfo->time'
29b25b04 529 WHERE userid = '$user->userid' AND course = '$course->id'", false);
10ee08cc 530
29b25b04 531 }
532 }
533
10ee08cc 534 if ($users = get_records_select("user_students", "course = '$course->id'",
29b25b04 535 "id", "id, userid, timeaccess")) {
536 foreach ($users as $user) {
10ee08cc 537 $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log
538 WHERE course = '$course->id' and userid = '$user->userid'
29b25b04 539 ORDER by time DESC");
540 if (empty($loginfo->time)) {
541 $loginfo->time = 0;
542 }
10ee08cc 543 execute_sql("UPDATE {$CFG->prefix}user_students
544 SET timeaccess = '$loginfo->time'
29b25b04 545 WHERE userid = '$user->userid' AND course = '$course->id'", false);
10ee08cc 546
29b25b04 547 }
548 }
549 }
4d744a22 550 }
29b25b04 551 notify("All courses complete.", "green");
4d744a22 552 $db->debug = true;
553 }
554
3f125001 555 if ($oldversion < 2003103100) {
556 table_column("course", "", "showreports", "integer", "4", "unsigned", "0", "", "maxbytes");
557 }
558
f374fb10 559 if ($oldversion < 2003121600) {
0da33e07 560 modify_database("", "CREATE TABLE `prefix_groups` (
f374fb10 561 `id` int(10) unsigned NOT NULL auto_increment,
562 `courseid` int(10) unsigned NOT NULL default '0',
563 `name` varchar(254) NOT NULL default '',
564 `description` text NOT NULL,
565 `lang` varchar(10) NOT NULL default 'en',
566 `picture` int(10) unsigned NOT NULL default '0',
567 `timecreated` int(10) unsigned NOT NULL default '0',
568 `timemodified` int(10) unsigned NOT NULL default '0',
569 PRIMARY KEY (`id`),
570 KEY `courseid` (`courseid`)
571 ) TYPE=MyISAM COMMENT='Each record is a group in a course.'; ");
572
0da33e07 573 modify_database("", "CREATE TABLE `prefix_groups_members` (
f374fb10 574 `id` int(10) unsigned NOT NULL auto_increment,
575 `groupid` int(10) unsigned NOT NULL default '0',
576 `userid` int(10) unsigned NOT NULL default '0',
577 `timeadded` int(10) unsigned NOT NULL default '0',
578 PRIMARY KEY (`id`),
579 KEY `groupid` (`groupid`)
580 ) TYPE=MyISAM COMMENT='Lists memberships of users in groups'; ");
581 }
582
583 if ($oldversion < 2003121800) {
584 table_column("course", "modinfo", "modinfo", "longtext", "", "", "");
585 }
586
587 if ($oldversion < 2003122600) {
588 table_column("course", "", "groupmode", "integer", "4", "unsigned", "0", "", "showreports");
589 table_column("course", "", "groupmodeforce", "integer", "4", "unsigned", "0", "", "groupmode");
590 }
591
ddc7afad 592 if ($oldversion < 2004010900) {
593 table_column("course_modules", "", "groupmode", "integer", "4", "unsigned", "0", "", "visible");
594 }
595
5fba04fb 596 if ($oldversion < 2004011700) {
597 modify_database("", "CREATE TABLE `prefix_event` (
598 `id` int(10) unsigned NOT NULL auto_increment,
599 `name` varchar(255) NOT NULL default '',
600 `description` text NOT NULL,
601 `courseid` int(10) unsigned NOT NULL default '0',
602 `groupid` int(10) unsigned NOT NULL default '0',
603 `userid` int(10) unsigned NOT NULL default '0',
604 `modulename` varchar(20) NOT NULL default '',
605 `instance` int(10) unsigned NOT NULL default '0',
606 `eventtype` varchar(20) NOT NULL default '',
607 `timestart` int(10) unsigned NOT NULL default '0',
608 `timeduration` int(10) unsigned NOT NULL default '0',
609 `timemodified` int(10) unsigned NOT NULL default '0',
610 PRIMARY KEY (`id`),
611 UNIQUE KEY `id` (`id`),
612 KEY `courseid` (`courseid`),
613 KEY `userid` (`userid`)
614 ) TYPE=MyISAM COMMENT='For everything with a time associated to it'; ");
615 }
616
70812e39 617 if ($oldversion < 2004012800) {
618 modify_database("", "CREATE TABLE `prefix_user_preferences` (
619 `id` int(10) unsigned NOT NULL auto_increment,
620 `userid` int(10) unsigned NOT NULL default '0',
621 `name` varchar(50) NOT NULL default '',
622 `value` varchar(255) NOT NULL default '',
623 PRIMARY KEY (`id`),
624 UNIQUE KEY `id` (`id`),
625 KEY `useridname` (userid,name)
626 ) TYPE=MyISAM COMMENT='Allows modules to store arbitrary user preferences'; ");
627 }
628
ba39fa10 629 if ($oldversion < 2004012900) {
630 table_column("config", "value", "value", "text", "", "", "");
631 }
70812e39 632
69d79bc3 633 if ($oldversion < 2004013101) {
634 table_column("log", "", "cmid", "integer", "10", "unsigned", "0", "", "module");
4da1a0a1 635 set_config("upgrade", "logs");
69d79bc3 636 }
637
b3153e4b 638 if ($oldversion < 2004020900) {
639 table_column("course", "", "lang", "varchar", "5", "", "", "", "groupmodeforce");
640 }
641
45121ffb 642 if ($oldversion < 2004020903) {
643 modify_database("", "CREATE TABLE `prefix_cache_text` (
d363047e 644 `id` int(10) unsigned NOT NULL auto_increment,
645 `md5key` varchar(32) NOT NULL default '',
646 `formattedtext` longtext NOT NULL,
647 `timemodified` int(10) unsigned NOT NULL default '0',
648 PRIMARY KEY (`id`),
649 KEY `md5key` (`md5key`)
650 ) TYPE=MyISAM COMMENT='For storing temporary copies of processed texts';");
651 }
652
d523d2ea 653 if ($oldversion < 2004021000) {
654 $textfilters = array();
655 for ($i=1; $i<=10; $i++) {
656 $variable = "textfilter$i";
657 if (!empty($CFG->$variable)) { /// No more filters
658 if (is_readable("$CFG->dirroot/".$CFG->$variable)) {
659 $textfilters[] = $CFG->$variable;
660 }
661 }
662 }
663 $textfilters = implode(',', $textfilters);
664 if (empty($textfilters)) {
665 $textfilters = 'mod/glossary/dynalink.php';
666 }
667 set_config('textfilters', $textfilters);
668 }
669
f1d604cb 670 if ($oldversion < 2004021201) {
671 modify_database("", "CREATE TABLE `prefix_cache_filters` (
672 `id` int(10) unsigned NOT NULL auto_increment,
673 `filter` varchar(32) NOT NULL default '',
674 `version` int(10) unsigned NOT NULL default '0',
675 `md5key` varchar(32) NOT NULL default '',
676 `rawtext` text NOT NULL,
677 `timemodified` int(10) unsigned NOT NULL default '0',
678 PRIMARY KEY (`id`),
679 KEY `filtermd5key` (filter,md5key)
680 ) TYPE=MyISAM COMMENT='For keeping information about cached data';");
681 }
682
3c0561cf 683 if ($oldversion < 2004021500) {
684 table_column("groups", "", "hidepicture", "integer", "2", "unsigned", "0", "", "picture");
685 }
10ee08cc 686
9e353ce7 687 if ($oldversion < 2004021700) {
688 if (!empty($CFG->textfilters)) {
689 $CFG->textfilters = str_replace("tex_filter.php", "filter.php", $CFG->textfilters);
690 $CFG->textfilters = str_replace("multilang.php", "filter.php", $CFG->textfilters);
691 $CFG->textfilters = str_replace("censor.php", "filter.php", $CFG->textfilters);
692 $CFG->textfilters = str_replace("mediaplugin.php", "filter.php", $CFG->textfilters);
693 $CFG->textfilters = str_replace("algebra_filter.php", "filter.php", $CFG->textfilters);
828aeff2 694 $CFG->textfilters = str_replace("dynalink.php", "filter.php", $CFG->textfilters);
9e353ce7 695 set_config("textfilters", $CFG->textfilters);
696 }
697 }
d523d2ea 698
8199e3f0 699 if ($oldversion < 2004022000) {
700 table_column("user", "", "emailstop", "integer", "1", "unsigned", "0", "not null", "email");
701 }
702
828aeff2 703 if ($oldversion < 2004022200) { /// Final renaming I hope. :-)
704 if (!empty($CFG->textfilters)) {
705 $CFG->textfilters = str_replace("/filter.php", "", $CFG->textfilters);
a4dda51f 706 $CFG->textfilters = str_replace("mod/glossary/dynalink.php", "mod/glossary", $CFG->textfilters);
828aeff2 707 $textfilters = explode(',', $CFG->textfilters);
708 foreach ($textfilters as $key => $textfilter) {
709 $textfilters[$key] = trim($textfilter);
710 }
711 set_config("textfilters", implode(',',$textfilters));
712 }
713 }
714
b4443d8f 715 if ($oldversion < 2004030702) { /// Because of the renaming of Czech language pack
4bf58667 716 execute_sql("UPDATE {$CFG->prefix}user SET lang = 'cs' WHERE lang = 'cz'");
717 execute_sql("UPDATE {$CFG->prefix}course SET lang = 'cs' WHERE lang = 'cz'");
7277e306 718 }
719
0f3fe4b6 720 if ($oldversion < 2004041800) { /// Integrate Block System from contrib
721 table_column("course", "", "blockinfo", "varchar", "255", "", "", "not null", "modinfo");
722 }
723
822ede92 724 if ($oldversion < 2004042600) { /// Rebuild course caches for resource icons
725 include_once("$CFG->dirroot/course/lib.php");
726 rebuild_course_cache();
727 }
728
cda21d48 729 if ($oldversion < 2004042700) { /// Increase size of lang fields
37aaf074 730 table_column("user", "lang", "lang", "varchar", "10", "", "en");
731 table_column("groups", "lang", "lang", "varchar", "10", "", "");
732 table_column("course", "lang", "lang", "varchar", "10", "", "");
733 }
734
009cc726 735 if ($oldversion < 2004042701) { /// Add hiddentopics field to control hidden topics behaviour
736 table_column("course", "", "hiddentopics", "integer", "1", "unsigned", "0", "not null", "visible");
737 }
738
cda21d48 739 if ($oldversion < 2004042702) { /// add a format field for the description
740 table_column("event", "", "format", "integer", "4", "unsigned", "0", "not null", "description");
741 }
742
10ee08cc 743 if ($oldversion < 2004042900) {
744 execute_sql(" ALTER TABLE `{$CFG->prefix}course` DROP `showrecent` ");
745 }
746
464fd803 747 if ($oldversion < 2004043001) { /// Change hiddentopics to hiddensections
748 table_column("course", "hiddentopics", "hiddensections", "integer", "2", "unsigned", "0", "not null");
749 }
750
dcd338ff 751 if ($oldversion < 2004050400) { /// add a visible field for events
752 table_column("event", "", "visible", "tinyint", "1", "", "1", "not null", "timeduration");
753 if ($events = get_records('event')) {
754 foreach($events as $event) {
755 if ($moduleid = get_field('modules', 'id', 'name', $event->modulename)) {
756 if (get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $event->instance) == 0) {
757 set_field('event', 'visible', 0, 'id', $event->id);
758 }
759 }
760 }
761 }
762 }
763
3cac987a 764 if ($oldversion < 2004052800) { /// First version tagged "1.4 development", version.php 1.227
e02c35b2 765 set_config('siteblocksadded', true); /// This will be used later by the block upgrade
3cac987a 766 }
767
62b80756 768 if ($oldversion < 2004053000) { /// set defaults for site course
769 $site = get_site();
a7e07837 770 set_field('course', 'numsections', 0, 'id', $site->id);
62b80756 771 set_field('course', 'groupmodeforce', 1, 'id', $site->id);
772 set_field('course', 'teacher', get_string('administrator'), 'id', $site->id);
773 set_field('course', 'teachers', get_string('administrators'), 'id', $site->id);
774 set_field('course', 'student', get_string('user'), 'id', $site->id);
775 set_field('course', 'students', get_string('users'), 'id', $site->id);
776 }
777
cc21211e 778 if ($oldversion < 2004060100) {
779 set_config('digestmailtime', 0);
780 table_column('user', "", 'maildigest', 'tinyint', '1', '', '0', 'not null', 'mailformat');
cc21211e 781 }
782
faef9f7b 783 if ($oldversion < 2004062400) {
784 table_column('user_teachers', "", 'timeend', 'int', '10', 'unsigned', '0', 'not null', 'editall');
785 table_column('user_teachers', "", 'timestart', 'int', '10', 'unsigned', '0', 'not null', 'editall');
786 }
787
838ee71b 788 if ($oldversion < 2004062401) {
789 table_column('course', '', 'idnumber', 'varchar', '100', '', '', 'not null', 'shortname');
790 execute_sql('UPDATE '.$CFG->prefix.'course SET idnumber = shortname'); // By default
791 }
792
8c5c6133 793 if ($oldversion < 2004062600) {
794 table_column('course', '', 'cost', 'varchar', '10', '', '', 'not null', 'lang');
795 }
796
18763fd4 797 if ($oldversion < 2004072900) {
798 table_column('course', '', 'enrolperiod', 'int', '10', 'unsigned', '0', 'not null', 'startdate');
799 }
800
f28db22d 801 if ($oldversion < 2004072901) { // Fixing error in schema
802 if ($record = get_record('log_display', 'module', 'course', 'action', 'update')) {
803 delete_records('log_display', 'module', 'course', 'action', 'update');
65342c76 804 insert_record('log_display', $record, false);
f28db22d 805 }
806 }
838ee71b 807
9fb216e8 808 if ($oldversion < 2004081200) { // Fixing version errors in some blocks
809 set_field('blocks', 'version', 2004081200, 'name', 'admin');
810 set_field('blocks', 'version', 2004081200, 'name', 'calendar_month');
811 set_field('blocks', 'version', 2004081200, 'name', 'course_list');
812 }
813
4e11ad4f 814 if ($oldversion < 2004081500) { // Adding new "auth" field to user table to allow more flexibility
815 table_column('user', '', 'auth', 'varchar', '20', '', 'manual', 'not null', 'id');
816
817 execute_sql("UPDATE {$CFG->prefix}user SET auth = 'manual'"); // Set everyone to 'manual' to be sure
818
819 if ($admins = get_admins()) { // Set all the NON-admins to whatever the current auth module is
820 $adminlist = array();
821 foreach ($admins as $user) {
822 $adminlist[] = $user->id;
823 }
824 $adminlist = implode(',', $adminlist);
825 execute_sql("UPDATE {$CFG->prefix}user SET auth = '$CFG->auth' WHERE id NOT IN ($adminlist)");
826 }
827 }
f950af3c 828
829 if ($oldversion < 2004082200) { // Making admins teachers on site course
830 $site = get_site();
831 $admins = get_admins();
832 foreach ($admins as $admin) {
833 add_teacher($admin->id, $site->id);
834 }
835 }
9fb216e8 836
c14964b0 837 if ($oldversion < 2004082600) {
838 //update auth-fields for external users
839 include_once ($CFG->dirroot."/auth/".$CFG->auth."/lib.php");
840 if (function_exists('auth_get_userlist')) {
841 $externalusers = auth_get_userlist();
842 if (!empty($externalusers)){
843 $externalusers = '\''. implode('\',\'',$externalusers).'\'';
844 execute_sql("UPDATE {$CFG->prefix}user SET auth = '$CFG->auth' WHERE username IN ($externalusers)");
845 }
846 }
847 }
848
3cac987a 849 if ($oldversion < 2004082900) { // Make sure guest is "manual" too.
850 set_field('user', 'auth', 'manual', 'username', 'guest');
851 }
01e2ea5f 852
d35757eb 853 /* Commented out unused guid-field code
01e2ea5f 854 if ($oldversion < 2004090300) { // Add guid-field used in user syncronization
855 table_column('user', '', 'guid', 'varchar', '128', '', '', '', 'auth');
856 execute_sql("ALTER TABLE {$CFG->prefix}user ADD INDEX authguid (auth, guid)");
857 }
d35757eb 858 */
859
860 if ($oldversion < 2004091900) { // modify idnumber to hold longer values
052fe105 861 table_column('user', 'idnumber', 'idnumber', 'varchar', '64', '', '', '', '');
17f3e7d0 862 execute_sql("ALTER TABLE {$CFG->prefix}user DROP INDEX user_idnumber",false); // added in case of conflicts with upgrade from 14stable
863 execute_sql("ALTER TABLE {$CFG->prefix}user DROP INDEX user_auth",false); // added in case of conflicts with upgrade from 14stable
864
d35757eb 865 execute_sql("ALTER TABLE {$CFG->prefix}user ADD INDEX idnumber (idnumber)");
866 execute_sql("ALTER TABLE {$CFG->prefix}user ADD INDEX auth (auth)");
867 }
3cac987a 868
42b90599 869 if ($oldversion < 2004093001) { // add new table for sessions storage
870 execute_sql(" CREATE TABLE `{$CFG->prefix}sessions` (
871 `sesskey` char(32) NOT null,
872 `expiry` int(11) unsigned NOT null,
873 `expireref` varchar(64),
874 `data` text NOT null,
875 PRIMARY KEY (`sesskey`),
876 KEY (`expiry`)
877 ) TYPE=MyISAM COMMENT='Optional database session storage, not used by default';");
878 }
879
bb043e38 880 if ($oldversion < 2004111500) { // Update any users/courses using wrongly-named lang pack
881 execute_sql("UPDATE {$CFG->prefix}user SET lang = 'mi_nt' WHERE lang = 'ma_nt'");
882 execute_sql("UPDATE {$CFG->prefix}course SET lang = 'mi_nt' WHERE lang = 'ma_nt'");
883 }
884
17f3e7d0 885 if ($oldversion < 2004111700) { // add indexes. - drop them first silently to avoid conflicts when upgrading.
886 execute_sql(" ALTER TABLE `{$CFG->prefix}course` DROP INDEX idnumber;",false);
887 execute_sql(" ALTER TABLE `{$CFG->prefix}course` DROP INDEX shortname;",false);
888 execute_sql(" ALTER TABLE `{$CFG->prefix}user_students` DROP INDEX userid;",false);
889 execute_sql(" ALTER TABLE `{$CFG->prefix}user_teachers` DROP INDEX userid;",false);
890
891 execute_sql(" ALTER TABLE `{$CFG->prefix}course` ADD INDEX idnumber (idnumber);");
892 execute_sql(" ALTER TABLE `{$CFG->prefix}course` ADD INDEX shortname (shortname);");
893 execute_sql(" ALTER TABLE `{$CFG->prefix}user_students` ADD INDEX userid (userid);");
894 execute_sql(" ALTER TABLE `{$CFG->prefix}user_teachers` ADD INDEX userid (userid);");
895 }
896
897 if ($oldversion < 2004111700) {// add an index to event for timestart and timeduration. - drop them first silently to avoid conflicts when upgrading.
275c630a 898 execute_sql("ALTER TABLE {$CFG->prefix}event DROP INDEX timestart;",false);
899 execute_sql("ALTER TABLE {$CFG->prefix}event DROP INDEX timeduration;",false);
17f3e7d0 900
901 modify_database('','ALTER TABLE prefix_event ADD INDEX timestart (timestart);');
902 modify_database('','ALTER TABLE prefix_event ADD INDEX timeduration (timeduration);');
903 }
904
905 if ($oldversion < 2004111700) { //add indexes on modules and course_modules. - drop them first silently to avoid conflicts when upgrading.
275c630a 906 execute_sql("ALTER TABLE {$CFG->prefix}course_modules drop key visible;",false);
907 execute_sql("ALTER TABLE {$CFG->prefix}course_modules drop key course;",false);
908 execute_sql("ALTER TABLE {$CFG->prefix}course_modules drop key module;",false);
909 execute_sql("ALTER TABLE {$CFG->prefix}course_modules drop key instance;",false);
910 execute_sql("ALTER TABLE {$CFG->prefix}course_modules drop key deleted;",false);
911 execute_sql("ALTER TABLE {$CFG->prefix}modules drop key name;",false);
17f3e7d0 912
913 modify_database('','ALTER TABLE prefix_course_modules add key visible(visible);');
914 modify_database('','ALTER TABLE prefix_course_modules add key course(course);');
915 modify_database('','ALTER TABLE prefix_course_modules add key module(module);');
916 modify_database('','ALTER TABLE prefix_course_modules add key instance (instance);');
917 modify_database('','ALTER TABLE prefix_course_modules add key deleted (deleted);');
918 modify_database('','ALTER TABLE prefix_modules add key name(name);');
919 }
920
921 if ($oldversion < 2004111700) { // add an index on the groups_members table. - drop them first silently to avoid conflicts when upgrading.
275c630a 922 execute_sql("ALTER TABLE {$CFG->prefix}groups_members DROP INDEX userid;",false);
17f3e7d0 923
924 modify_database('','ALTER TABLE prefix_groups_members ADD INDEX userid (userid);');
925 }
926
927 if ($oldversion < 2004111700) { // add an index on user students timeaccess (used for sorting)- drop them first silently to avoid conflicts when upgrading
275c630a 928 execute_sql("ALTER TABLE {$CFG->prefix}user_students DROP INDEX timeaccess;",false);
17f3e7d0 929
930 modify_database('','ALTER TABLE prefix_user_students ADD INDEX timeaccess (timeaccess);');
931 }
932
933 if ($oldversion < 2004111700) { // add indexes on faux-foreign keys. - drop them first silently to avoid conflicts when upgrading.
275c630a 934 execute_sql("ALTER TABLE {$CFG->prefix}scale DROP INDEX courseid;",false);
935 execute_sql("ALTER TABLE {$CFG->prefix}user_admins DROP INDEX userid;",false);
936 execute_sql("ALTER TABLE {$CFG->prefix}user_coursecreators DROP INDEX userid;",false);
17f3e7d0 937
938 modify_database('','ALTER TABLE prefix_scale ADD INDEX courseid (courseid);');
939 modify_database('','ALTER TABLE prefix_user_admins ADD INDEX userid (userid);');
940 modify_database('','ALTER TABLE prefix_user_coursecreators ADD INDEX userid (userid);');
941 }
ffa5f71b 942
943 if ($oldversion < 2004111700) { // replace index on course
944 fix_course_sortorder(0,0,1);
275c630a 945 execute_sql("ALTER TABLE `{$CFG->prefix}course` DROP KEY category",false);
ffa5f71b 946
275c630a 947 execute_sql("ALTER TABLE `{$CFG->prefix}course` DROP KEY category_sortorder;",false);
ffa5f71b 948 modify_database('', "ALTER TABLE `prefix_course` ADD UNIQUE KEY category_sortorder(category,sortorder)");
949
275c630a 950 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_deleted_idx;",false);
951 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_confirmed_idx;",false);
952 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_firstname_idx;",false);
953 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_lastname_idx;",false);
954 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_city_idx;",false);
955 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_country_idx;",false);
956 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_lastaccess_idx;",false);
ffa5f71b 957
958 modify_database("", "ALTER TABLE `prefix_user` ADD INDEX prefix_user_deleted_idx (deleted)");
959 modify_database("", "ALTER TABLE `prefix_user` ADD INDEX prefix_user_confirmed_idx (confirmed)");
960 modify_database("", "ALTER TABLE `prefix_user` ADD INDEX prefix_user_firstname_idx (firstname)");
961 modify_database("", "ALTER TABLE `prefix_user` ADD INDEX prefix_user_lastname_idx (lastname)");
962 modify_database("", "ALTER TABLE `prefix_user` ADD INDEX prefix_user_city_idx (city)");
963 modify_database("", "ALTER TABLE `prefix_user` ADD INDEX prefix_user_country_idx (country)");
964 modify_database("", "ALTER TABLE `prefix_user` ADD INDEX prefix_user_lastaccess_idx (lastaccess)");
965 }
966
967 if ($oldversion < 2004111700) { // one more index for email (for sorting)
275c630a 968 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_email_idx;",false);
ffa5f71b 969 modify_database('','ALTER TABLE `prefix_user` ADD INDEX prefix_user_email_idx (email);');
970 }
971
5cd79686 972 if ($oldversion < 2004112200) { // new 'enrol' field for enrolment tables
973 table_column('user_students', '', 'enrol', 'varchar', '20', '', '', 'not null');
974 table_column('user_teachers', '', 'enrol', 'varchar', '20', '', '', 'not null');
975 execute_sql("ALTER TABLE `{$CFG->prefix}user_students` ADD INDEX enrol (enrol);");
976 execute_sql("ALTER TABLE `{$CFG->prefix}user_teachers` ADD INDEX enrol (enrol);");
977 }
17f3e7d0 978
b110348b 979 if ($oldversion < 2004112400) {
980 /// Delete duplicate enrolments
981 /// and then tell the database course,userid is a unique combination
982 if ($users = get_records_select("user_students", "userid > 0 GROUP BY course, userid ".
983 "HAVING count(*) > 1", "", "max(id) as id, userid, course ,count(*)")) {
984 foreach ($users as $user) {
985 delete_records_select("user_students", "userid = '$user->userid' ".
986 "AND course = '$user->course' AND id <> '$user->id'");
987 }
988 }
989 flush();
990
991 modify_database('','ALTER TABLE prefix_user_students DROP INDEX courseuserid;');
992 modify_database('','ALTER TABLE prefix_user_students ADD UNIQUE INDEX courseuserid(course,userid);');
993
994 /// Delete duplicate teacher enrolments
995 /// and then tell the database course,userid is a unique combination
996 if ($users = get_records_select("user_teachers", "userid > 0 GROUP BY course, userid ".
997 "HAVING count(*) > 1", "", "max(id) as id, userid, course ,count(*)")) {
998 foreach ($users as $user) {
999 delete_records_select("user_teachers", "userid = '$user->userid' ".
1000 "AND course = '$user->course' AND id <> '$user->id'");
1001 }
1002 }
1003 flush();
1004 modify_database('','ALTER TABLE prefix_user_teachers DROP INDEX courseuserid;');
1005 modify_database('','ALTER TABLE prefix_user_teachers ADD UNIQUE INDEX courseuserid(course,userid);');
1006 }
027a1604 1007
1008 if ($oldversion < 2004112900) {
1009 table_column('user', '', 'policyagreed', 'integer', '1', 'unsigned', '0', 'not null', 'confirmed');
1010 }
1011
1a5bc046 1012 if ($oldversion < 2004121400) {
1013 table_column('groups', '', 'password', 'varchar', '50', '', '', 'not null', 'description');
1014 }
86f092d2 1015
1016 if ($oldversion < 2004121500) {
1017 modify_database('',"CREATE TABLE prefix_dst_preset (
1018 id int(10) NOT NULL auto_increment,
1019 name char(48) default '' NOT NULL,
1020
1021 apply_offset tinyint(3) default '0' NOT NULL,
1022
1023 activate_index tinyint(1) default '1' NOT NULL,
1024 activate_day tinyint(1) default '1' NOT NULL,
1025 activate_month tinyint(2) default '1' NOT NULL,
1026 activate_time char(5) default '03:00' NOT NULL,
1027
1028 deactivate_index tinyint(1) default '1' NOT NULL,
1029 deactivate_day tinyint(1) default '1' NOT NULL,
1030 deactivate_month tinyint(2) default '2' NOT NULL,
1031 deactivate_time char(5) default '03:00' NOT NULL,
1032
1033 last_change int(10) default '0' NOT NULL,
1034 next_change int(10) default '0' NOT NULL,
1035 current_offset tinyint(3) default '0' NOT NULL,
1036
1037 PRIMARY KEY (id))");
1038 }
ffce79c0 1039
0eacb23c 1040 if ($oldversion < 2004122800) {
1041 execute_sql("DROP TABLE {$CFG->prefix}message", false);
1042 execute_sql("DROP TABLE {$CFG->prefix}message_read", false);
1043 execute_sql("DROP TABLE {$CFG->prefix}message_contacts", false);
1044
ffce79c0 1045 modify_database('',"CREATE TABLE `prefix_message` (
1046 `id` int(10) unsigned NOT NULL auto_increment,
1047 `useridfrom` int(10) NOT NULL default '0',
1048 `useridto` int(10) NOT NULL default '0',
1049 `message` text NOT NULL,
0eacb23c 1050 `timecreated` int(10) NOT NULL default '0',
ffce79c0 1051 `messagetype` varchar(50) NOT NULL default '',
1052 PRIMARY KEY (`id`),
1053 KEY `useridfrom` (`useridfrom`),
1054 KEY `useridto` (`useridto`)
1055 ) TYPE=MyISAM COMMENT='Stores all unread messages';");
1056
1057 modify_database('',"CREATE TABLE `prefix_message_read` (
1058 `id` int(10) unsigned NOT NULL auto_increment,
1059 `useridfrom` int(10) NOT NULL default '0',
1060 `useridto` int(10) NOT NULL default '0',
1061 `message` text NOT NULL,
0eacb23c 1062 `timecreated` int(10) NOT NULL default '0',
1063 `timeread` int(10) NOT NULL default '0',
ffce79c0 1064 `messagetype` varchar(50) NOT NULL default '',
1065 `mailed` tinyint(1) NOT NULL default '0',
1066 PRIMARY KEY (`id`),
1067 KEY `useridfrom` (`useridfrom`),
1068 KEY `useridto` (`useridto`)
1069 ) TYPE=MyISAM COMMENT='Stores all messages that have been read';");
0eacb23c 1070
1071 modify_database('',"CREATE TABLE `prefix_message_contacts` (
1072 `id` int(10) unsigned NOT NULL auto_increment,
1073 `userid` int(10) unsigned NOT NULL default '0',
1074 `contactid` int(10) unsigned NOT NULL default '0',
1075 `blocked` tinyint(1) unsigned NOT NULL default '0',
1076 PRIMARY KEY (`id`),
1077 UNIQUE KEY `usercontact` (`userid`,`contactid`)
1078 ) TYPE=MyISAM COMMENT='Maintains lists of relationships between users';");
1079
c343bc88 1080 modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'write', 'user', 'CONCAT(firstname,\" \",lastname)'); ");
1081 modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'read', 'user', 'CONCAT(firstname,\" \",lastname)'); ");
ffce79c0 1082 }
0eacb23c 1083
e53c030b 1084 if ($oldversion < 2004122801) {
1085 table_column('message', '', 'format', 'integer', '4', 'unsigned', '0', 'not null', 'message');
1086 table_column('message_read', '', 'format', 'integer', '4', 'unsigned', '0', 'not null', 'message');
1087 }
1088
36f01e7b 1089 if ($oldversion < 2005010100) {
c343bc88 1090 modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'add contact', 'user', 'CONCAT(firstname,\" \",lastname)'); ");
1091 modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'remove contact', 'user', 'CONCAT(firstname,\" \",lastname)'); ");
1092 modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'block contact', 'user', 'CONCAT(firstname,\" \",lastname)'); ");
1093 modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('message', 'unblock contact', 'user', 'CONCAT(firstname,\" \",lastname)'); ");
36f01e7b 1094 }
2765411a 1095
1096 if ($oldversion < 2005011000) { // Create a .htaccess file in dataroot, just in case
1097 if (!file_exists($CFG->dataroot.'/.htaccess')) {
1098 if ($handle = fopen($CFG->dataroot.'/.htaccess', 'w')) { // For safety
d389b128 1099 @fwrite($handle, "deny from all\r\nAllowOverride None\r\n");
2765411a 1100 @fclose($handle);
1101 notify("Created a default .htaccess file in $CFG->dataroot");
1102 }
1103 }
1104 }
b61efafb 1105
2765411a 1106
b61efafb 1107 if ($oldversion < 2005012500) {
483b7158 1108 /*
b61efafb 1109 // add new table for meta courses.
1110 modify_database("","CREATE TABLE `prefix_meta_course` (
1111 `id` int(1) unsigned NOT NULL auto_increment,
1112 `parent_course` int(10) NOT NULL default 0,
1113 `child_course` int(10) NOT NULL default 0,
1114 PRIMARY KEY (`id`),
1115 KEY `parent_course` (parent_course),
1116 KEY `child_course` (child_course)
1117 );");
1118 // add flag to course field
1119 table_column('course','','meta_course','integer','1','','0','not null');
483b7158 1120 */ // taking this OUT for upgrade from 1.4 to 1.5 (those tracking head will have already seen it)
b61efafb 1121 }
047d30d1 1122
5f37b628 1123 if ($oldversion < 2005012501) {
1124 execute_sql("DROP TABLE {$CFG->prefix}meta_course",false); // drop silently
1125 execute_sql("ALTER TABLE {$CFG->prefix}course DROP COLUMN meta_course",false); // drop silently
1126
1127 // add new table for meta courses.
1128 modify_database("","CREATE TABLE `prefix_course_meta` (
a22fd258 1129 `id` int(10) unsigned NOT NULL auto_increment,
5f37b628 1130 `parent_course` int(10) NOT NULL default 0,
1131 `child_course` int(10) NOT NULL default 0,
1132 PRIMARY KEY (`id`),
1133 KEY `parent_course` (parent_course),
1134 KEY `child_course` (child_course)
1135 );");
1136 // add flag to course field
1137 table_column('course','','metacourse','integer','1','','0','not null');
1138 }
1139
a22fd258 1140 if ($oldversion < 2005012800) {
1141 // fix a typo (int 1 becomes int 10)
1142 table_column('course_meta','id','id','integer','10','','0','not null');
1143 }
1144
4c3911f9 1145 if ($oldversion < 2005020100) {
1146 fix_course_sortorder(0, 1, 1);
1147 }
1148
8a4e77ae 1149
1150 if ($oldversion < 2005020101) {
1151 // hopefully this is the LAST TIME we need to do this ;)
1152 if ($rows = count_records("course_meta")) {
1153 // we need to upgrade
1154 modify_database("","CREATE TABLE `prefix_course_meta_tmp` (
1155 `parent_course` int(10) NOT NULL default 0,
1156 `child_course` int(10) NOT NULL default 0);");
1157
1158 execute_sql("INSERT INTO {$CFG->prefix}course_meta_tmp (parent_course,child_course)
1159 SELECT {$CFG->prefix}course_meta.parent_course, {$CFG->prefix}course_meta.child_course
1160 FROM {$CFG->prefix}course_meta");
1161 $insertafter = true;
1162 }
1163
1164 execute_sql("DROP TABLE {$CFG->prefix}course_meta");
1165
1166 modify_database("","CREATE TABLE `prefix_course_meta` (
1167 `id` int(10) unsigned NOT NULL auto_increment,
1168 `parent_course` int(10) unsigned NOT NULL default 0,
1169 `child_course` int(10) unsigned NOT NULL default 0,
1170 PRIMARY KEY (`id`),
1171 KEY `parent_course` (parent_course),
1172 KEY `child_course` (child_course));");
1173
1174 if (!empty($insertafter)) {
1175 execute_sql("INSERT INTO {$CFG->prefix}course_meta (parent_course,child_course)
1176 SELECT {$CFG->prefix}course_meta_tmp.parent_course, {$CFG->prefix}course_meta_tmp.child_course
1177 FROM {$CFG->prefix}course_meta_tmp");
1178
1179 execute_sql("DROP TABLE {$CFG->prefix}course_meta_tmp");
1180 }
1181 }
1182
915602ea 1183 if ($oldversion < 2005020800) { // Expand module column to max 20 chars
78345312 1184 table_column('log','module','module','varchar','20','','','not null');
1185 }
1186
915602ea 1187 if ($oldversion < 2005021000) { // New fields for theme choices
1188 table_column('course', '', 'theme', 'varchar', '50', '', '', '', 'lang');
1189 table_column('groups', '', 'theme', 'varchar', '50', '', '', '', 'lang');
1190 table_column('user', '', 'theme', 'varchar', '50', '', '', '', 'lang');
35b04d5e 1191
1192 set_config('theme', 'standardwhite'); // Reset to a known good theme
915602ea 1193 }
e89ff6c6 1194
1195 if ($oldversion < 2005021600) { // course.idnumber should be varchar(100)
1196 table_column('course', 'idnumber', 'idnumber', 'varchar', '100', '', '', '', '');
1197 }
915602ea 1198
41b7678d 1199 if ($oldversion < 2005021700) {
1200 table_column('user', '', 'dstpreset', 'int', '10', '', '0', 'not null', 'timezone');
1201 }
1202
ceff9307 1203 if ($oldversion < 2005021800) { // For database debugging, not for normal use
1204 modify_database(""," CREATE TABLE `adodb_logsql` (
1205 `created` datetime NOT NULL,
1206 `sql0` varchar(250) NOT NULL,
1207 `sql1` text NOT NULL,
1208 `params` text NOT NULL,
1209 `tracer` text NOT NULL,
1210 `timer` decimal(16,6) NOT NULL
1211 );");
1212 }
1213
13222d99 1214 if ($oldversion < 2005022400) {
1215 // Add more visible digits to the fields
1216 table_column('dst_preset', 'activate_index', 'activate_index', 'tinyint', '2', '', '0', 'not null');
1217 table_column('dst_preset', 'activate_day', 'activate_day', 'tinyint', '2', '', '0', 'not null');
1218 // Add family and year fields
1219 table_column('dst_preset', '', 'family', 'varchar', '100', '', '', 'not null', 'name');
1220 table_column('dst_preset', '', 'year', 'int', '10', '', '0', 'not null', 'family');
1221 }
1222
766d2bf3 1223 if ($oldversion < 2005030501) {
1224 table_column('user', '', 'msn', 'varchar', '50', '', '', '', 'icq');
1225 table_column('user', '', 'aim', 'varchar', '50', '', '', '', 'icq');
1226 table_column('user', '', 'yahoo', 'varchar', '50', '', '', '', 'icq');
1227 table_column('user', '', 'skype', 'varchar', '50', '', '', '', 'icq');
1228 }
1229
1789c548 1230 if ($oldversion < 2005032300) {
1231 table_column('user', 'dstpreset', 'timezonename', 'varchar', '100');
1232 execute_sql('UPDATE `'.$CFG->prefix.'user` SET timezonename = \'\'');
1233 }
1234
66336c5f 1235 if ($oldversion < 2005032600) {
1236 execute_sql('DROP TABLE '.$CFG->prefix.'dst_preset', false);
1237 modify_database('',"CREATE TABLE `prefix_timezone` (
1238 `id` int(10) NOT NULL auto_increment,
1239 `name` varchar(100) NOT NULL default '',
1240 `year` int(11) NOT NULL default '0',
1241 `rule` varchar(20) NOT NULL default '',
1242 `gmtoff` int(11) NOT NULL default '0',
1243 `dstoff` int(11) NOT NULL default '0',
1244 `dst_month` tinyint(2) NOT NULL default '0',
1245 `dst_startday` tinyint(3) NOT NULL default '0',
1246 `dst_weekday` tinyint(3) NOT NULL default '0',
1247 `dst_skipweeks` tinyint(3) NOT NULL default '0',
1248 `dst_time` varchar(5) NOT NULL default '00:00',
1249 `std_month` tinyint(2) NOT NULL default '0',
1250 `std_startday` tinyint(3) NOT NULL default '0',
1251 `std_weekday` tinyint(3) NOT NULL default '0',
1252 `std_skipweeks` tinyint(3) NOT NULL default '0',
1253 `std_time` varchar(5) NOT NULL default '00:00',
1254 PRIMARY KEY (`id`)
1255 ) TYPE=MyISAM COMMENT='Rules for calculating local wall clock time for users';");
1256 }
1257
4eb25d43 1258 if ($oldversion < 2005032800) {
1259 execute_sql("CREATE TABLE `{$CFG->prefix}grade_category` (
1260 `id` int(10) unsigned NOT NULL auto_increment,
cb24115c 1261 `name` varchar(64) NOT NULL default '',
4eb25d43 1262 `courseid` int(10) unsigned NOT NULL default '0',
1263 `drop_x_lowest` int(10) unsigned NOT NULL default '0',
1264 `bonus_points` int(10) unsigned NOT NULL default '0',
1265 `hidden` int(10) unsigned NOT NULL default '0',
cb24115c 1266 `weight` decimal(4,2) NOT NULL default '0.00',
4eb25d43 1267 PRIMARY KEY (`id`),
1268 KEY `courseid` (`courseid`)
1269 ) TYPE=MyISAM ;");
1270
1271 execute_sql("CREATE TABLE `{$CFG->prefix}grade_exceptions` (
1272 `id` int(10) unsigned NOT NULL auto_increment,
1273 `courseid` int(10) unsigned NOT NULL default '0',
1274 `grade_itemid` int(10) unsigned NOT NULL default '0',
1275 `userid` int(10) unsigned NOT NULL default '0',
1276 PRIMARY KEY (`id`),
1277 KEY `courseid` (`courseid`)
1278 ) TYPE=MyISAM ;");
1279
1280
1281 execute_sql("CREATE TABLE `{$CFG->prefix}grade_item` (
1282 `id` int(10) unsigned NOT NULL auto_increment,
cb24115c 1283 `courseid` int(10) unsigned NOT NULL default '0',
1284 `category` int(10) unsigned NOT NULL default '0',
1285 `modid` int(10) unsigned NOT NULL default '0',
1286 `cminstance` int(10) unsigned NOT NULL default '0',
4eb25d43 1287 `scale_grade` float(11,10) default '1.0000000000',
1288 `extra_credit` int(10) unsigned NOT NULL default '0',
1289 `sort_order` int(10) unsigned NOT NULL default '0',
1290 PRIMARY KEY (`id`),
1291 KEY `courseid` (`courseid`)
1292 ) TYPE=MyISAM ;");
1293
1294
1295 execute_sql("CREATE TABLE `{$CFG->prefix}grade_letter` (
1296 `id` int(10) unsigned NOT NULL auto_increment,
1297 `courseid` int(10) unsigned NOT NULL default '0',
1298 `letter` varchar(8) NOT NULL default 'NA',
1299 `grade_high` decimal(4,2) NOT NULL default '100.00',
1300 `grade_low` decimal(4,2) NOT NULL default '0.00',
1301 PRIMARY KEY (`id`),
1302 KEY `courseid` (`courseid`)
1303 ) TYPE=MyISAM ;");
1304
1305
1306 execute_sql("CREATE TABLE `{$CFG->prefix}grade_preferences` (
1307 `id` int(10) unsigned NOT NULL auto_increment,
cb24115c 1308 `courseid` int(10) unsigned NOT NULL default '0',
4eb25d43 1309 `preference` int(10) NOT NULL default '0',
1310 `value` int(10) NOT NULL default '0',
1311 PRIMARY KEY (`id`),
1312 UNIQUE KEY `courseidpreference` (`courseid`,`preference`)
1313 ) TYPE=MyISAM ;");
1314
1315 }
1316
594ceedd 1317 if ($oldversion < 2005033100) { // Get rid of defunct field from course modules table
1318 delete_records('course_modules', 'deleted', 1); // Delete old records we don't need any more
1319 execute_sql('ALTER TABLE `'.$CFG->prefix.'course_modules` DROP INDEX `deleted`'); // Old index
1320 execute_sql('ALTER TABLE `'.$CFG->prefix.'course_modules` DROP `deleted`'); // Old field
1321 }
1322
68c0264e 1323 if ($oldversion < 2005040800) {
1324 table_column('user', 'timezone', 'timezone', 'varchar', '100', '', '99');
1325 execute_sql(" ALTER TABLE `{$CFG->prefix}user` DROP `timezonename` ");
1326 }
6927f0e7 1327
1328 if ($oldversion < 2005041101) {
1329 require_once($CFG->libdir.'/filelib.php');
1330 if (is_readable($CFG->dirroot.'/lib/timezones.txt')) { // Distribution file
1331 if ($timezones = get_records_csv($CFG->dirroot.'/lib/timezones.txt', 'timezone')) {
1332 $db->debug = false;
1333 update_timezone_records($timezones);
1334 notify(count($timezones).' timezones installed');
1335 $db->debug = true;
1336 }
1337 }
1338 }
68c0264e 1339
323d2abe 1340 if ($oldversion < 2005041900) { // Copy all Dialogue entries into Messages, and hide Dialogue module
1341
1342 if ($entries = get_records_sql('SELECT e.id, e.userid, c.recipientid, e.text, e.timecreated
1343 FROM '.$CFG->prefix.'dialogue_conversations c,
1344 '.$CFG->prefix.'dialogue_entries e
1345 WHERE e.conversationid = c.id')) {
1346 foreach ($entries as $entry) {
382648f4 1347 $message = new object;
323d2abe 1348 $message->useridfrom = $entry->userid;
1349 $message->useridto = $entry->recipientid;
2a0cf3ca 1350 $message->message = addslashes($entry->text);
323d2abe 1351 $message->format = FORMAT_HTML;
1352 $message->timecreated = $entry->timecreated;
1353 $message->messagetype = 'direct';
1354
1355 insert_record('message_read', $message);
1356 }
1357 }
1358
1359 set_field('modules', 'visible', 0, 'name', 'dialogue');
1360
1361 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');
1362
1363 }
1364
8180c859 1365 if ($oldversion < 2005042100) {
1366 $result = table_column('event', '', 'repeatid', 'int', '10', 'unsigned', '0', 'not null', 'userid') && $result;
1367 }
1368
b71772e2 1369 if ($oldversion < 2005042400) { // Add user tracking prefs field.
9e33ce2c 1370 table_column('user', '', 'trackforums', 'int', '4', 'unsigned', '0', 'not null', 'autosubscribe');
b71772e2 1371 }
1372
a13e99bd 1373 if ($oldversion < 2005053000 ) { // Add config_plugins table
1374
1375 // this table was created on the MOODLE_15_STABLE branch
1376 // so it may already exist.
1377 $result = execute_sql("CREATE TABLE IF NOT EXISTS `{$CFG->prefix}config_plugins` (
1378 `id` int(10) unsigned NOT NULL auto_increment,
ee708245 1379 `plugin` varchar(100) NOT NULL default 'core',
1380 `name` varchar(100) NOT NULL default '',
a13e99bd 1381 `value` text NOT NULL default '',
1382 PRIMARY KEY (`id`),
1383 UNIQUE KEY `plugin_name` (`plugin`, `name`)
1384 ) TYPE=MyISAM
1385 COMMENT='Moodle modules and plugins configuration variables';");
1386 }
382648f4 1387
a4080313 1388 if ($oldversion < 2005060200) { // migrate some config items to config_plugins table
1389
1390 // NOTE: this block is in both postgres AND mysql upgrade
1391 // files. If you edit either, update the otherone.
1392 $user_fields = array("firstname", "lastname", "email",
1393 "phone1", "phone2", "department",
1394 "address", "city", "country",
1395 "description", "idnumber", "lang");
1396 if (!empty($CFG->auth)) { // if we have no auth, just pass
1397 foreach ($user_fields as $field) {
1398 $suffixes = array('', '_editlock', '_updateremote', '_updatelocal');
1399 foreach ($suffixes as $suffix) {
1400 $key = 'auth_user_' . $field . $suffix;
1401 if (isset($CFG->$key)) {
1402
1403 // translate keys & values
1404 // to the new convention
1405 // this should support upgrading
1406 // even 1.5dev installs
1407 $newkey = $key;
1408 $newval = $CFG->$key;
1409 if ($suffix === '') {
1410 $newkey = 'field_map_' . $field;
1411 } elseif ($suffix === '_editlock') {
1412 $newkey = 'field_lock_' . $field;
1413 $newval = ($newval==1) ? 'locked' : 'unlocked'; // translate 0/1 to locked/unlocked
1414 } elseif ($suffix === '_updateremote') {
1415 $newkey = 'field_updateremote_' . $field;
1416 } elseif ($suffix === '_updatelocal') {
1417 $newkey = 'field_updatelocal_' . $field;
1418 $newval = ($newval==1) ? 'onlogin' : 'oncreate'; // translate 0/1 to locked/unlocked
1419 }
1420
967f5f4b 1421 if (!(set_config($newkey, addslashes($newval), 'auth/'.$CFG->auth)
a4080313 1422 && delete_records('config', 'name', $key))) {
1423 notify("Error updating Auth configuration $key to {$CFG->auth} $newkey .");
1424 $result = false;
1425 }
1426 } // end if isset key
1427 } // end foreach suffix
1428 } // end foreach field
1429 }
1430 }
a4080313 1431
62831050 1432 if ($oldversion < 2005060201) { // Close down the Attendance module, we are removing it from CVS.
1433 if (!file_exists($CFG->dirroot.'/mod/attendance/lib.php')) {
1434 if (count_records('attendance')) { // We have some data, so should keep it
1435
1436 set_field('modules', 'visible', 0, 'name', 'attendance');
1437 notify('The Attendance module has been discontinued. If you really want to
1438 continue using it, you should download it individually from
1439 http://download.moodle.org/modules and install it, then
1440 reactivate it from Admin >> Configuration >> Modules.
1441 None of your existing data has been deleted, so all existing
1442 Attendance activities should re-appear.');
1443
1444 } else { // No data, so do a complete delete
1445
1446 execute_sql('DROP TABLE '.$CFG->prefix.'attendance', false);
1447 delete_records('modules', 'name', 'attendance');
1448 notify("The Attendance module has been discontinued and removed from your site.
1449 You weren't using it anyway. ;-)");
1450 }
1451 }
1452 }
a4080313 1453
27d8712a 1454 if ($oldversion < 2005071700) { // Close down the Dialogue module, we are removing it from CVS.
1455 if (!file_exists($CFG->dirroot.'/mod/dialogue/lib.php')) {
1456 if (count_records('dialogue')) { // We have some data, so should keep it
1457
1458 set_field('modules', 'visible', 0, 'name', 'dialogue');
1459 notify('The Dialogue module has been discontinued. If you really want to
1460 continue using it, you should download it individually from
1461 http://download.moodle.org/modules and install it, then
1462 reactivate it from Admin >> Configuration >> Modules.
1463 None of your existing data has been deleted, so all existing
1464 Dialogue activities should re-appear.');
1465
1466 } else { // No data, so do a complete delete
1467
1468 execute_sql('DROP TABLE '.$CFG->prefix.'dialogue', false);
1469 delete_records('modules', 'name', 'dialogue');
1470 notify("The Dialogue module has been discontinued and removed from your site.
1471 You weren't using it anyway. ;-)");
1472 }
1473 }
1474 }
1475
98f8f9c2 1476 if ($oldversion < 2005072000) { // Add a couple fields to mdl_event to work towards iCal import/export
1477 table_column('event', '', 'uuid', 'char', '36', '', '', 'not null', 'visible');
1478 table_column('event', '', 'sequence', 'integer', '10', 'unsigned', '1', 'not null', 'uuid');
1479 }
1480
3b4ff6ed 1481 if ($oldversion < 2005072100) { // run the online assignment cleanup code
1482 include($CFG->dirroot.'/'.$CFG->admin.'/oacleanup.php');
1483 if (function_exists('online_assignment_cleanup')) {
1484 online_assignment_cleanup();
1485 }
1486 }
1487
f2e34253 1488 if ($oldversion < 2005072200) { // fix the mistakenly-added currency stuff from enrol/authorize
1489 execute_sql("DROP TABLE {$CFG->prefix}currencies", false); // drop silently
1490 execute_sql("ALTER TABLE {$CFG->prefix}course DROP currency", false);
1491 $defaultcurrency = empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency;
1492 table_column('course', '', 'currency', 'char', '3', '', $defaultcurrency, 'not null', 'cost');
1493 }
1494
0705ff84 1495 if ($oldversion < 2005081600) { //set up the course requests table
1496 modify_database('',"CREATE TABLE `prefix_course_request` (
1497 `id` int(10) unsigned NOT NULL auto_increment,
1498 `fullname` varchar(254) NOT NULL default '',
1499 `shortname` varchar(15) NOT NULL default '',
1500 `summary` text NOT NULL,
1501 `reason` text NOT NULL,
1502 `requester` int(10) NOT NULL default 0,
1503 PRIMARY KEY (`id`),
1504 KEY `shortname` (`shortname`)
1505 ) TYPE=MyISAM;");
1506
1507 table_column('course','','requested');
1508 }
1509
1510 if ($oldversion < 2005081601) {
1511 modify_database('',"CREATE TABLE `prefix_course_allowed_modules` (
1512 `id` int(10) unsigned NOT NULL auto_increment,
1513 `course` int(10) unsigned NOT NULL default 0,
1514 `module` int(10) unsigned NOT NULL default 0,
1515 PRIMARY KEY (`id`),
1516 KEY `course` (`course`),
1517 KEY `module` (`module`)
1518 ) TYPE=MyISAM;");
1519
1520 table_column('course','','restrictmodules','int','1','','0','not null');
1521 }
1522
f41ef63e 1523 if ($oldversion < 2005081700) {
1524 table_column('course_categories','','depth','integer');
1525 table_column('course_categories','','path','varchar','255');
1526 }
1527
f3221af9 1528 if ($oldversion < 2005090100) {
1529 modify_database("","CREATE TABLE `prefix_stats_daily` (
1530 `id` int(10) unsigned NOT NULL auto_increment,
1531 `courseid` int(10) unsigned NOT NULL default 0,
1532 `timeend` int(10) unsigned NOT NULL default 0,
1533 `students` int(10) unsigned NOT NULL default 0,
1534 `teachers` int(10) unsigned NOT NULL default 0,
1535 `activestudents` int(10) unsigned NOT NULL default 0,
1536 `activeteachers` int(10) unsigned NOT NULL default 0,
1537 `studentreads` int(10) unsigned NOT NULL default 0,
1538 `studentwrites` int(10) unsigned NOT NULL default 0,
1539 `teacherreads` int(10) unsigned NOT NULL default 0,
1540 `teacherwrites` int(10) unsigned NOT NULL default 0,
1541 `logins` int(10) unsigned NOT NULL default 0,
1542 `uniquelogins` int(10) unsigned NOT NULL default 0,
1543 PRIMARY KEY (`id`),
1544 KEY `courseid` (`courseid`),
450a0a7d 1545 KEY `timeend` (`timeend`)
f3221af9 1546 );");
1547
1548 modify_database("","CREATE TABLE prefix_stats_weekly (
1549 `id` int(10) unsigned NOT NULL auto_increment,
1550 `courseid` int(10) unsigned NOT NULL default 0,
1551 `timeend` int(10) unsigned NOT NULL default 0,
1552 `students` int(10) unsigned NOT NULL default 0,
1553 `teachers` int(10) unsigned NOT NULL default 0,
1554 `activestudents` int(10) unsigned NOT NULL default 0,
1555 `activeteachers` int(10) unsigned NOT NULL default 0,
1556 `studentreads` int(10) unsigned NOT NULL default 0,
1557 `studentwrites` int(10) unsigned NOT NULL default 0,
1558 `teacherreads` int(10) unsigned NOT NULL default 0,
1559 `teacherwrites` int(10) unsigned NOT NULL default 0,
1560 `logins` int(10) unsigned NOT NULL default 0,
1561 `uniquelogins` int(10) unsigned NOT NULL default 0,
1562 PRIMARY KEY (`id`),
1563 KEY `courseid` (`courseid`),
450a0a7d 1564 KEY `timeend` (`timeend`)
f3221af9 1565 );");
1566
1567 modify_database("","CREATE TABLE prefix_stats_monthly (
1568 `id` int(10) unsigned NOT NULL auto_increment,
1569 `courseid` int(10) unsigned NOT NULL default 0,
1570 `timeend` int(10) unsigned NOT NULL default 0,
1571 `students` int(10) unsigned NOT NULL default 0,
1572 `teachers` int(10) unsigned NOT NULL default 0,
1573 `activestudents` int(10) unsigned NOT NULL default 0,
1574 `activeteachers` int(10) unsigned NOT NULL default 0,
1575 `studentreads` int(10) unsigned NOT NULL default 0,
1576 `studentwrites` int(10) unsigned NOT NULL default 0,
1577 `teacherreads` int(10) unsigned NOT NULL default 0,
1578 `teacherwrites` int(10) unsigned NOT NULL default 0,
1579 `logins` int(10) unsigned NOT NULL default 0,
1580 `uniquelogins` int(10) unsigned NOT NULL default 0,
1581 PRIMARY KEY (`id`),
1582 KEY `courseid` (`courseid`),
450a0a7d 1583 KEY `timeend` (`timeend`)
f3221af9 1584 );");
1585
1586 modify_database("","CREATE TABLE prefix_stats_user_daily (
1587 `id` int(10) unsigned NOT NULL auto_increment,
1588 `courseid` int(10) unsigned NOT NULL default 0,
1589 `userid` int(10) unsigned NOT NULL default 0,
1590 `roleid` int(10) unsigned NOT NULL default 0,
1591 `timeend` int(10) unsigned NOT NULL default 0,
462458c6 1592 `statsreads` int(10) unsigned NOT NULL default 0,
1593 `statswrites` int(10) unsigned NOT NULL default 0,
f3221af9 1594 `stattype` varchar(30) NOT NULL default '',
1595 PRIMARY KEY (`id`),
1596 KEY `courseid` (`courseid`),
1597 KEY `userid` (`userid`),
1598 KEY `roleid` (`roleid`),
450a0a7d 1599 KEY `timeend` (`timeend`)
f3221af9 1600 );");
1601
1602 modify_database("","CREATE TABLE prefix_stats_user_weekly (
1603 `id` int(10) unsigned NOT NULL auto_increment,
1604 `courseid` int(10) unsigned NOT NULL default 0,
1605 `userid` int(10) unsigned NOT NULL default 0,
1606 `roleid` int(10) unsigned NOT NULL default 0,
1607 `timeend` int(10) unsigned NOT NULL default 0,
462458c6 1608 `statsreads` int(10) unsigned NOT NULL default 0,
1609 `statswrites` int(10) unsigned NOT NULL default 0,
f3221af9 1610 `stattype` varchar(30) NOT NULL default '',
1611 PRIMARY KEY (`id`),
1612 KEY `courseid` (`courseid`),
1613 KEY `userid` (`userid`),
1614 KEY `roleid` (`roleid`),
450a0a7d 1615 KEY `timeend` (`timeend`)
f3221af9 1616 );");
1617
1618 modify_database("","CREATE TABLE prefix_stats_user_monthly (
1619 `id` int(10) unsigned NOT NULL auto_increment,
1620 `courseid` int(10) unsigned NOT NULL default 0,
1621 `userid` int(10) unsigned NOT NULL default 0,
1622 `roleid` int(10) unsigned NOT NULL default 0,
1623 `timeend` int(10) unsigned NOT NULL default 0,
462458c6 1624 `statsreads` int(10) unsigned NOT NULL default 0,
1625 `statswrites` int(10) unsigned NOT NULL default 0,
f3221af9 1626 `stattype` varchar(30) NOT NULL default '',
1627 PRIMARY KEY (`id`),
1628 KEY `courseid` (`courseid`),
1629 KEY `userid` (`userid`),
1630 KEY `roleid` (`roleid`),
450a0a7d 1631 KEY `timeend` (`timeend`)
f3221af9 1632 );");
1633
1634 }
1635
7246c2d1 1636 if ($oldversion < 2005100300) {
1637 table_column('course','','expirynotify','tinyint','1');
1638 table_column('course','','expirythreshold','int','10');
1639 table_column('course','','notifystudents','tinyint','1');
1640 $new = new stdClass();
1641 $new->name = 'lastexpirynotify';
1642 $new->value = 0;
1643 insert_record('config', $new);
1644 }
1645
f89033b1 1646 if ($oldversion < 2005100400) {
1647 table_column('course','','enrollable','tinyint','1','unsigned','1');
1648 table_column('course','','enrolstartdate','int');
1649 table_column('course','','enrolenddate','int');
1650 }
1651
f9667a5a 1652 if ($oldversion < 2005101200) { // add enrolment key to course_request.
e19db110 1653 table_column('course_request','','password','varchar',50);
56eb871c 1654 }
1655
fb3ea05c 1656 if ($oldversion < 2006030800) { # add extra indexes to log (see bug #4112)
1657 modify_database('',"ALTER TABLE prefix_log ADD INDEX userid (userid);");
1658 modify_database('',"ALTER TABLE prefix_log ADD INDEX info (info);");
1659 }
1660
f9667a5a 1661 if ($oldversion < 2006030900) {
1662 table_column('course','','enrol','varchar','20','','');
7064007f 1663
1664 if ($CFG->enrol == 'internal' || $CFG->enrol == 'manual') {
1665 set_config('enrol_plugins_enabled', 'manual');
1666 set_config('enrol', 'manual');
1667 } else {
1668 set_config('enrol_plugins_enabled', 'manual,'.$CFG->enrol);
1669 }
1670
54e9516e 1671 require_once("$CFG->dirroot/enrol/enrol.class.php");
1672 $defaultenrol = enrolment_factory::factory($CFG->enrol);
1673 if (!method_exists($defaultenrol, 'print_entry')) { // switch enrollable to off for all courses in this case
4af1efec 1674 modify_database('', 'UPDATE prefix_course SET enrollable = 0');
54e9516e 1675 }
f9667a5a 1676
6a2c524c 1677 execute_sql("UPDATE {$CFG->prefix}user_students SET enrol='manual' WHERE enrol='' OR enrol='internal'");
1678 execute_sql("UPDATE {$CFG->prefix}user_teachers SET enrol='manual' WHERE enrol=''");
6a2c524c 1679
29f85332 1680 }
89d38fdd 1681
1682 if ($oldversion < 2006031000) {
1683
1684 modify_database("","CREATE TABLE prefix_post (
025bec31 1685 `id` int(10) unsigned NOT NULL auto_increment,
1686 `userid` int(10) unsigned NOT NULL default '0',
1687 `courseid` int(10) unsigned NOT NULL default'0',
1688 `groupid` int(10) unsigned NOT NULL default'0',
1689 `moduleid` int(10) unsigned NOT NULL default'0',
1690 `coursemoduleid` int(10) unsigned NOT NULL default'0',
89d38fdd 1691 `subject` varchar(128) NOT NULL default '',
1692 `summary` longtext,
1693 `content` longtext,
1694 `uniquehash` varchar(128) NOT NULL default '',
025bec31 1695 `rating` int(10) unsigned NOT NULL default'0',
1696 `format` int(10) unsigned NOT NULL default'0',
89d38fdd 1697 `publishstate` enum('draft','site','public') NOT NULL default 'draft',
025bec31 1698 `lastmodified` int(10) unsigned NOT NULL default '0',
1699 `created` int(10) unsigned NOT NULL default '0',
89d38fdd 1700 PRIMARY KEY (`id`),
1701 UNIQUE KEY `id_user_idx` (`id`, `userid`),
1702 KEY `post_lastmodified_idx` (`lastmodified`),
1703 KEY `post_subject_idx` (`subject`)
1704 ) TYPE=MyISAM COMMENT='New moodle post table. Holds data posts such as forum entries or blog entries.';");
1705
1706 modify_database("","CREATE TABLE prefix_tags (
025bec31 1707 `id` int(10) unsigned NOT NULL auto_increment,
89d38fdd 1708 `type` varchar(255) NOT NULL default 'official',
025bec31 1709 `userid` int(10) unsigned NOT NULL default'0',
89d38fdd 1710 `text` varchar(255) NOT NULL default '',
1711 PRIMARY KEY (`id`)
1712 ) TYPE=MyISAM COMMENT ='tags structure for moodle.';");
1713
1714 modify_database("","CREATE TABLE prefix_blog_tag_instance (
025bec31 1715 `id` int(10) unsigned NOT NULL auto_increment,
1716 `entryid` int(10) unsigned NOT NULL default'0',
1717 `tagid` int(10) unsigned NOT NULL default'0',
1718 `groupid` int(10) unsigned NOT NULL default'0',
1719 `courseid` int(10) unsigned NOT NULL default'0',
1720 `userid` int(10) unsigned NOT NULL default'0',
89d38fdd 1721 PRIMARY KEY (`id`)
1722 ) TYPE=MyISAM COMMENT ='tag instance for blogs.';");
1723 }
29f85332 1724
cd9afe2f 1725 if ($oldversion < 2006031400) {
1726 require_once("$CFG->dirroot/enrol/enrol.class.php");
1727 $defaultenrol = enrolment_factory::factory($CFG->enrol);
1728 if (!method_exists($defaultenrol, 'print_entry')) {
1729 set_config('enrol', 'manual');
1730 }
1731 }
53f6a21e 1732
1733 if ($oldversion < 2006031600) {
1734 execute_sql(" ALTER TABLE `{$CFG->prefix}grade_category` CHANGE `weight` `weight` decimal(5,2) default '0.00';");
1735 }
cd9afe2f 1736
112d0ebf 1737 if ($oldversion < 2006032000) {
1738 table_column('post','','module','varchar','20','','','not null', 'id');
1739 modify_database('',"ALTER TABLE prefix_post ADD INDEX post_module_idx (module);");
1740 modify_database('',"UPDATE prefix_post SET module = 'blog';");
1741 }
1742
6a30c073 1743 if ($oldversion < 2006032001) {
1744 table_column('blog_tag_instance','','timemodified','integer','10','unsigned','0','not null', 'userid');
1745 modify_database('',"ALTER TABLE prefix_blog_tag_instance ADD INDEX bti_entryid_idx (entryid);");
1746 modify_database('',"ALTER TABLE prefix_blog_tag_instance ADD INDEX bti_tagid_idx (tagid);");
9a8091a9 1747 modify_database('',"UPDATE prefix_blog_tag_instance SET timemodified = '".time()."';");
6a30c073 1748 }
1749
60f92a43 1750 if ($oldversion < 2006040500) { // Add an index to course_sections that was never upgraded (bug 5100)
1751 execute_sql(" CREATE INDEX coursesection ON {$CFG->prefix}course_sections (course,section) ", false);
1752 }
1753
dffaf682 1754 /// change all the int(11) to int(10) for blogs and tags
1755
1756 if ($oldversion < 2006041000) {
1757 table_column('post','id','id','integer','10','unsigned','0','not null');
1758 table_column('post','userid','userid','integer','10','unsigned','0','not null');
1759 table_column('post','courseid','courseid','integer','10','unsigned','0','not null');
1760 table_column('post','groupid','groupid','integer','10','unsigned','0','not null');
1761 table_column('post','moduleid','moduleid','integer','10','unsigned','0','not null');
1762 table_column('post','coursemoduleid','coursemoduleid','integer','10','unsigned','0','not null');
1763 table_column('post','rating','rating','integer','10','unsigned','0','not null');
1764 table_column('post','format','format','integer','10','unsigned','0','not null');
1765 table_column('tags','id','id','integer','10','unsigned','0','not null');
1766 table_column('tags','userid','userid','integer','10','unsigned','0','not null');
1767 table_column('blog_tag_instance','id','id','integer','10','unsigned','0','not null');
1768 table_column('blog_tag_instance','entryid','entryid','integer','10','unsigned','0','not null');
1769 table_column('blog_tag_instance','tagid','tagid','integer','10','unsigned','0','not null');
1770 table_column('blog_tag_instance','groupid','groupid','integer','10','unsigned','0','not null');
1771 table_column('blog_tag_instance','courseid','courseid','integer','10','unsigned','0','not null');
1772 table_column('blog_tag_instance','userid','userid','integer','10','unsigned','0','not null');
1773 }
71470b3b 1774
1775 if ($oldversion < 2006041001) {
1776 table_column('cache_text','formattedtext','formattedtext','longblob','','','','not null');
1777 }
dffaf682 1778
12ef2c40 1779 if ($oldversion < 2006041100) {
1780 table_column('course_modules','','visibleold','integer','1','unsigned','1','not null', 'visible');
1781 }
1782
9977aa0f 1783 if ($oldversion < 2006041801) { // forgot auto_increments for ids
1784 modify_database('',"ALTER TABLE prefix_post CHANGE id id INT UNSIGNED NOT NULL AUTO_INCREMENT");
1785 modify_database('',"ALTER TABLE prefix_tags CHANGE id id INT UNSIGNED NOT NULL AUTO_INCREMENT");
1786 modify_database('',"ALTER TABLE prefix_blog_tag_instance CHANGE id id INT UNSIGNED NOT NULL AUTO_INCREMENT");
025bec31 1787 }
1788
8bcd295c 1789 // changed user->firstname, user->lastname, course->shortname to varchar(100)
1790
1791 if ($oldversion < 2006041900) {
1792 table_column('course','shortname','shortname','varchar','100','','','not null');
1793 table_column('user','firstname','firstname','varchar','100','','','not null');
1794 table_column('user','lastname','lastname','varchar','100','','','not null');
1795 }
1796
36e6706e 1797 if ($oldversion < 2006042400) {
721cb867 1798 // Look through table log_display and get rid of duplicates.
1799 $rs = get_recordset_sql('SELECT DISTINCT * FROM '.$CFG->prefix.'log_display');
1800
1801 // Drop the log_display table and create it back with an id field.
1802 execute_sql("DROP TABLE {$CFG->prefix}log_display", false);
1803
1804 modify_database('', "CREATE TABLE prefix_log_display (
1805 `id` int(10) unsigned NOT NULL auto_increment,
1806 `module` varchar(30),
1807 `action` varchar(40),
1808 `mtable` varchar(30),
1809 `field` varchar(50),
1810 PRIMARY KEY (`id`)
1811 ) TYPE=MyISAM");
1812
1813 // Add index to ensure that module and action combination is unique.
1814 modify_database('', "ALTER TABLE prefix_log_display ADD UNIQUE `moduleaction`(`module` , `action`)");
1815
1816 // Insert the records back in, sans duplicates.
1817 if ($rs && $rs->RecordCount() > 0) {
1818 while (!$rs->EOF) {
1819 $sql = "INSERT INTO {$CFG->prefix}log_display ".
1820 "VALUES('', '".$rs->fields['module']."', ".
1821 "'".$rs->fields['action']."', ".
1822 "'".$rs->fields['mtable']."', ".
1823 "'".$rs->fields['field']."')";
1824
1825 execute_sql($sql, false);
1826 $rs->MoveNext();
1827 }
1828 }
1829 }
1830
31513aa1 1831 // change tags->type to varchar(20), adding 2 indexes for tags table.
1832 if ($oldversion < 2006042401) {
1833 table_column('tags','type','type','varchar','20','','','not null');
1834 modify_database('',"ALTER TABLE prefix_tags ADD INDEX tags_typeuserid_idx (type(20), userid)");
808f9930 1835 modify_database('',"ALTER TABLE prefix_tags ADD INDEX tags_text_idx(text(255))");
31513aa1 1836 }
cb24115c 1837
1838 /***************************************************
1839 * The following is an effort to change all the *
1840 * default NULLs to NOT NULL defaut '' in all *
1841 * mysql tables, to prevent 5303 and be consistent *
1842 ***************************************************/
1843
0327c40a 1844 if ($oldversion < 2006042800) {
cb24115c 1845
0327c40a 1846 execute_sql("UPDATE {$CFG->prefix}grade_category SET name='' WHERE name IS NULL");
cb24115c 1847 table_column('grade_category','name','name','varchar','64','','','not null');
0327c40a 1848
1849 execute_sql("UPDATE {$CFG->prefix}grade_category SET weight='0' WHERE weight IS NULL");
cb24115c 1850 execute_sql("ALTER TABLE {$CFG->prefix}grade_category change weight weight decimal(5,2) NOT NULL default 0.00");
0327c40a 1851 execute_sql("UPDATE {$CFG->prefix}grade_item SET courseid='0' WHERE courseid IS NULL");
cb24115c 1852 table_column('grade_item','courseid','courseid','int','10','unsigned','0','not null');
0327c40a 1853
1854 execute_sql("UPDATE {$CFG->prefix}grade_item SET category='0' WHERE category IS NULL");
cb24115c 1855 table_column('grade_item','category','category','int','10','unsigned','0','not null');
0327c40a 1856
1857 execute_sql("UPDATE {$CFG->prefix}grade_item SET modid='0' WHERE modid IS NULL");
cb24115c 1858 table_column('grade_item','modid','modid','int','10','unsigned','0','not null');
0327c40a 1859
1860 execute_sql("UPDATE {$CFG->prefix}grade_item SET cminstance='0' WHERE cminstance IS NULL");
cb24115c 1861 table_column('grade_item','cminstance','cminstance','int','10','unsigned','0','not null');
0327c40a 1862
1863 execute_sql("UPDATE {$CFG->prefix}grade_item SET scale_grade='0' WHERE scale_grade IS NULL");
cb24115c 1864 execute_sql("ALTER TABLE {$CFG->prefix}grade_item change scale_grade scale_grade float(11,10) NOT NULL default 1.0000000000");
0327c40a 1865
1866 execute_sql("UPDATE {$CFG->prefix}grade_preferences SET courseid='0' WHERE courseid IS NULL");
cb24115c 1867 table_column('grade_preferences','courseid','courseid','int','10','unsigned','0','not null');
0327c40a 1868
1869 execute_sql("UPDATE {$CFG->prefix}user SET idnumber='' WHERE idnumber IS NULL");
cb24115c 1870 table_column('user','idnumber','idnumber','varchar','64','','','not null');
0327c40a 1871
1872 execute_sql("UPDATE {$CFG->prefix}user SET icq='' WHERE icq IS NULL");
cb24115c 1873 table_column('user','icq','icq','varchar','15','','','not null');
0327c40a 1874
1875 execute_sql("UPDATE {$CFG->prefix}user SET skype='' WHERE skype IS NULL");
cb24115c 1876 table_column('user','skype','skype','varchar','50','','','not null');
0327c40a 1877
1878 execute_sql("UPDATE {$CFG->prefix}user SET yahoo='' WHERE yahoo IS NULL");
cb24115c 1879 table_column('user','yahoo','yahoo','varchar','50','','','not null');
0327c40a 1880
1881 execute_sql("UPDATE {$CFG->prefix}user SET aim='' WHERE aim IS NULL");
cb24115c 1882 table_column('user','aim','aim','varchar','50','','','not null');
0327c40a 1883
1884 execute_sql("UPDATE {$CFG->prefix}user SET msn='' WHERE msn IS NULL");
cb24115c 1885 table_column('user','msn','msn','varchar','50','','','not null');
0327c40a 1886
1887 execute_sql("UPDATE {$CFG->prefix}user SET phone1='' WHERE phone1 IS NULL");
cb24115c 1888 table_column('user','phone1','phone1','varchar','20','','','not null');
0327c40a 1889
1890 execute_sql("UPDATE {$CFG->prefix}user SET phone2='' WHERE phone2 IS NULL");
cb24115c 1891 table_column('user','phone2','phone2','varchar','20','','','not null');
0327c40a 1892
1893 execute_sql("UPDATE {$CFG->prefix}user SET institution='' WHERE institution IS NULL");
cb24115c 1894 table_column('user','institution','institution','varchar','40','','','not null');
0327c40a 1895
1896 execute_sql("UPDATE {$CFG->prefix}user SET department='' WHERE department IS NULL");
cb24115c 1897 table_column('user','department','department','varchar','30','','','not null');
0327c40a 1898
1899 execute_sql("UPDATE {$CFG->prefix}user SET address='' WHERE address IS NULL");
cb24115c 1900 table_column('user','address','address','varchar','70','','','not null');
0327c40a 1901
1902 execute_sql("UPDATE {$CFG->prefix}user SET city='' WHERE city IS NULL");
cb24115c 1903 table_column('user','city','city','varchar','20','','','not null');
0327c40a 1904
1905 execute_sql("UPDATE {$CFG->prefix}user SET country='' WHERE country IS NULL");
cb24115c 1906 table_column('user','country','country','char','2','','','not null');
0327c40a 1907
1908 execute_sql("UPDATE {$CFG->prefix}user SET lang='' WHERE lang IS NULL");
cb24115c 1909 table_column('user','lang','lang','varchar','10','','en','not null');
0327c40a 1910
1911 execute_sql("UPDATE {$CFG->prefix}user SET lastIP='' WHERE lastIP IS NULL");
cb24115c 1912 table_column('user','lastIP','lastIP','varchar','15','','','not null');
0327c40a 1913
1914 execute_sql("UPDATE {$CFG->prefix}user SET secret='' WHERE secret IS NULL");
cb24115c 1915 table_column('user','secret','secret','varchar','15','','','not null');
0327c40a 1916
1917 execute_sql("UPDATE {$CFG->prefix}user SET picture='0' WHERE picture IS NULL");
cb24115c 1918 table_column('user','picture','picture','tinyint','1','','0','not null');
0327c40a 1919
1920 execute_sql("UPDATE {$CFG->prefix}user SET url='' WHERE url IS NULL");
cb24115c 1921 table_column('user','url','url','varchar','255','','','not null');
1922 }
1923
e503f30d 1924 if ($oldversion < 2006050400) {
1925
1926 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_deleted_idx;",false);
1927 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_confirmed_idx;",false);
1928 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_firstname_idx;",false);
1929 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_lastname_idx;",false);
1930 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_city_idx;",false);
1931 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_country_idx;",false);
1932 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_lastaccess_idx;",false);
1933 execute_sql("ALTER TABLE `{$CFG->prefix}user` DROP INDEX {$CFG->prefix}user_email_idx;",false);
1934
1935 execute_sql("ALTER TABLE `{$CFG->prefix}user` ADD INDEX user_deleted (deleted)",false);
1936 execute_sql("ALTER TABLE `{$CFG->prefix}user` ADD INDEX user_confirmed (confirmed)",false);
1937 execute_sql("ALTER TABLE `{$CFG->prefix}user` ADD INDEX user_firstname (firstname)",false);
1938 execute_sql("ALTER TABLE `{$CFG->prefix}user` ADD INDEX user_lastname (lastname)",false);
1939 execute_sql("ALTER TABLE `{$CFG->prefix}user` ADD INDEX user_city (city)",false);
1940 execute_sql("ALTER TABLE `{$CFG->prefix}user` ADD INDEX user_country (country)",false);
1941 execute_sql("ALTER TABLE `{$CFG->prefix}user` ADD INDEX user_lastaccess (lastaccess)",false);
1942 execute_sql("ALTER TABLE `{$CFG->prefix}user` ADD INDEX user_email (email)",false);
1943 }
1944
7235ae23 1945 if ($oldversion < 2006050500) {
1946 table_column('log', 'action', 'action', 'varchar', '40', '', '', 'not null');
1947 }
ef668fcf 1948
1949 if ($oldversion < 2006050501) {
1950 table_column('sessions', 'data', 'data', 'mediumtext', '', '', '', 'not null');
1951 }
7235ae23 1952
462458c6 1953 // renaming of reads and writes for stats_user_xyz
1954 if ($oldversion < 2006052400) { // change this later
1955
1956 // we are using this because we want silent updates
1957
1958 execute_sql("ALTER TABLE `{$CFG->prefix}stats_user_daily` CHANGE `reads` statsreads int(10) unsigned NOT NULL default 0", false);
1959 execute_sql("ALTER TABLE `{$CFG->prefix}stats_user_daily` CHANGE `writes` statswrites int(10) unsigned NOT NULL default 0", false);
1960 execute_sql("ALTER TABLE `{$CFG->prefix}stats_user_weekly` CHANGE `reads` statsreads int(10) unsigned NOT NULL default 0", false);
1961 execute_sql("ALTER TABLE `{$CFG->prefix}stats_user_weekly` CHANGE `writes` statswrites int(10) unsigned NOT NULL default 0", false);
1962 execute_sql("ALTER TABLE `{$CFG->prefix}stats_user_monthly` CHANGE `reads` statsreads int(10) unsigned NOT NULL default 0", false);
1963 execute_sql("ALTER TABLE `{$CFG->prefix}stats_user_monthly` CHANGE `writes` statswrites int(10) unsigned NOT NULL default 0", false);
1964
1965 }
ae57b07c 1966
1967 // Adding some missing log actions
1968 if ($oldversion < 2006060400) {
1969 // But only if they doesn't exist (because this was introduced after branch and we could be duplicating!)
1970 if (!record_exists('log_display', 'module', 'course', 'action', 'report log')) {
1971 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report log', 'course', 'fullname')");
1972 }
1973 if (!record_exists('log_display', 'module', 'course', 'action', 'report live')) {
1974 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report live', 'course', 'fullname')");
1975 }
1976 if (!record_exists('log_display', 'module', 'course', 'action', 'report outline')) {
1977 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report outline', 'course', 'fullname')");
1978 }
1979 if (!record_exists('log_display', 'module', 'course', 'action', 'report participation')) {
1980 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report participation', 'course', 'fullname')");
1981 }
1982 if (!record_exists('log_display', 'module', 'course', 'action', 'report stats')) {
1983 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'report stats', 'course', 'fullname')");
1984 }
1985 }
d836482a 1986
1987 //Renaming lastIP to lastip (all fields lowercase)
1988 if ($oldversion < 2006060900) {
1989 //Only if it exists
1990 $fields = $db->MetaColumnNames($CFG->prefix.'user');
1991 if (in_array('lastIP',$fields)) {
1992 table_column("user", "lastIP", "lastip", "varchar", "15", "", "", "not null", "currentlogin");
1993 }
1994 }
d272c2fe 1995
1996 // Change in MySQL 5.0.3 concerning how decimals are stored. Mimic from 16_STABLE
1997 // this isn't dangerous because it's a simple type change, but be careful with
1998 // versions and duplicate work in order to provide smooth upgrade paths.
1999 if ($oldversion < 2006071800) {
2000 table_column('grade_letter', 'grade_high', 'grade_high', 'decimal(5,2)', '', '', '100.00', 'not null', '');
2001 table_column('grade_letter', 'grade_low', 'grade_low', 'decimal(5,2)', '', '', '0.00', 'not null', '');
2002 }
462458c6 2003
bbbf2d40 2004 if ($oldversion < 2006080400) {
bcf214df 2005 execute_sql("CREATE TABLE {$CFG->prefix}role (
90f7a7d5 2006 `id` int(10) unsigned NOT NULL auto_increment,
bbbf2d40 2007 `name` varchar(255) NOT NULL default '',
76389cec 2008 `shortname` varchar(100) NOT NULL default '',
bbbf2d40 2009 `description` text NOT NULL default '',
90f7a7d5 2010 `sortorder` int(10) unsigned NOT NULL default '0',
bbbf2d40 2011 PRIMARY KEY (`id`)
2012 )", true);
2013
bcf214df 2014 execute_sql("CREATE TABLE {$CFG->prefix}context (
90f7a7d5 2015 `id` int(10) unsigned NOT NULL auto_increment,
2016 `level` int(10) unsigned NOT NULL default '0',
2017 `instanceid` int(10) unsigned NOT NULL default '0',
bbbf2d40 2018 PRIMARY KEY (`id`)
2019 )", true);
2020
2021 execute_sql("CREATE TABLE {$CFG->prefix}role_assignments (
90f7a7d5 2022 `id` int(10) unsigned NOT NULL auto_increment,
2023 `roleid` int(10) unsigned NOT NULL default '0',
2024 `contextid` int(10) unsigned NOT NULL default '0',
2025 `userid` int(10) unsigned NOT NULL default '0',
2026 `hidden` int(1) unsigned NOT NULL default '0',
2027 `timestart` int(10) unsigned NOT NULL default '0',
2028 `timeend` int(10) unsigned NOT NULL default '0',
2029 `timemodified` int(10) unsigned NOT NULL default '0',
2030 `modifierid` int(10) unsigned NOT NULL default '0',
bbbf2d40 2031 `enrol` varchar(20) NOT NULL default '',
90f7a7d5 2032 `sortorder` int(10) unsigned NOT NULL default '0',
bbbf2d40 2033 PRIMARY KEY (`id`)
2034 )", true);
2035
2036 execute_sql("CREATE TABLE {$CFG->prefix}role_capabilities (
90f7a7d5 2037 `id` int(10) unsigned NOT NULL auto_increment,
2038 `contextid` int(10) unsigned NOT NULL default '0',
2039 `roleid` int(10) unsigned NOT NULL default '0',
bbbf2d40 2040 `capability` varchar(255) NOT NULL default '',
90f7a7d5 2041 `permission` int(10) unsigned NOT NULL default '0',
2042 `timemodified` int(10) unsigned NOT NULL default '0',
2043 `modifierid` int(10) unsigned NOT NULL default '0',
bbbf2d40 2044 PRIMARY KEY (`id`)
2045 )", true);
2046
2047 execute_sql("CREATE TABLE {$CFG->prefix}role_deny_grant (
90f7a7d5 2048 `id` int(10) unsigned NOT NULL auto_increment,
2049 `roleid` int(10) unsigned NOT NULL default '0',
2050 `unviewableroleid` int(10) unsigned NOT NULL default '0',
bbbf2d40 2051 PRIMARY KEY (`id`)
2052 )", true);
2053
2054 execute_sql("CREATE TABLE {$CFG->prefix}capabilities (
f10306b9 2055 `id` int(10) unsigned NOT NULL auto_increment,
7952cb34 2056 `name` varchar(255) NOT NULL default '',
f10306b9 2057 `captype` varchar(50) NOT NULL default '',
2058 `contextlevel` int(10) unsigned NOT NULL default '0',
2059 `component` varchar(100) NOT NULL default '',
2060 PRIMARY KEY (`id`)
2061 )", true);
2062
2063 execute_sql("CREATE TABLE {$CFG->prefix}role_names (
2064 `id` int(10) unsigned NOT NULL auto_increment,
2065 `roleid` int(10) unsigned NOT NULL default '0',
2066 `contextid` int(10) unsigned NOT NULL default '0',
2067 `text` text NOT NULL default '',
2068 PRIMARY KEY (`id`)
2069 )", true);
2070
bbbf2d40 2071 }
bbbf2d40 2072
bcf214df 2073 if ($oldversion < 2006081000) {
2074
2075 execute_sql("ALTER TABLE `{$CFG->prefix}role` ADD INDEX `sortorder` (`sortorder`)",true);
2076
2077 execute_sql("ALTER TABLE `{$CFG->prefix}context` ADD INDEX `instanceid` (`instanceid`)",true);
2078 execute_sql("ALTER TABLE `{$CFG->prefix}context` ADD UNIQUE INDEX `level-instanceid` (`level`, `instanceid`)",true);
2079
2080 execute_sql("ALTER TABLE `{$CFG->prefix}role_assignments` ADD INDEX `roleid` (`roleid`)",true);
2081 execute_sql("ALTER TABLE `{$CFG->prefix}role_assignments` ADD INDEX `contextid` (`contextid`)",true);
2082 execute_sql("ALTER TABLE `{$CFG->prefix}role_assignments` ADD INDEX `userid` (`userid`)",true);
2083 execute_sql("ALTER TABLE `{$CFG->prefix}role_assignments` ADD UNIQUE INDEX `contextid-roleid-userid` (`contextid`, `roleid`, `userid`)",true);
2084 execute_sql("ALTER TABLE `{$CFG->prefix}role_assignments` ADD INDEX `sortorder` (`sortorder`)",true);
2085
2086 execute_sql("ALTER TABLE `{$CFG->prefix}role_capabilities` ADD INDEX `roleid` (`roleid`)",true);
2087 execute_sql("ALTER TABLE `{$CFG->prefix}role_capabilities` ADD INDEX `contextid` (`contextid`)",true);
2088 execute_sql("ALTER TABLE `{$CFG->prefix}role_capabilities` ADD INDEX `modifierid` (`modifierid`)",true);
2089 execute_sql("ALTER TABLE `{$CFG->prefix}role_capabilities` ADD UNIQUE INDEX `roleid-contextid-capability` (`roleid`, `contextid`, `capability`)",true);
2090
2091 execute_sql("ALTER TABLE `{$CFG->prefix}role_deny_grant` ADD INDEX `roleid` (`roleid`)",true);
2092 execute_sql("ALTER TABLE `{$CFG->prefix}role_deny_grant` ADD INDEX `unviewableroleid` (`unviewableroleid`)",true);
2093 execute_sql("ALTER TABLE `{$CFG->prefix}role_deny_grant` ADD UNIQUE INDEX `roleid-unviewableroleid` (`roleid`, `unviewableroleid`)",true);
2094
2a1ab5ec 2095 execute_sql("ALTER TABLE `{$CFG->prefix}capabilities` ADD UNIQUE INDEX `name` (`name`)",true);
bcf214df 2096
2097 execute_sql("ALTER TABLE `{$CFG->prefix}role_names` ADD INDEX `roleid` (`roleid`)",true);
2098 execute_sql("ALTER TABLE `{$CFG->prefix}role_names` ADD INDEX `contextid` (`contextid`)",true);
2099 execute_sql("ALTER TABLE `{$CFG->prefix}role_names` ADD UNIQUE INDEX `roleid-contextid` (`roleid`, `contextid`)",true);
2100 }
77d4953e 2101
f9e2f481 2102 if ($oldversion < 2006081600) {
77d4953e 2103 execute_sql("ALTER TABLE `{$CFG->prefix}role_capabilities` CHANGE permission permission int(10) NOT NULL default '0'",true);
2104 }
f9e2f481 2105
2106 // drop role_deny_grant table, and create 2 new ones
2107 if ($oldversion < 2006081700) {
2108 execute_sql("DROP TABLE `{$CFG->prefix}role_deny_grant`", true);
2109
2110 execute_sql("CREATE TABLE {$CFG->prefix}role_allow_assign (
2111 `id` int(10) unsigned NOT NULL auto_increment,
2112 `roleid` int(10) unsigned NOT NULL default '0',
2113 `allowassign` int(10) unsigned NOT NULL default '0',
2114 KEY `roleid` (`roleid`),
2115 KEY `allowassign` (`allowassign`),
2116 UNIQUE KEY `roleid-allowassign` (`roleid`, `allowassign`),
2117 PRIMARY KEY (`id`)
2118 )", true);
2119
2120 execute_sql("CREATE TABLE {$CFG->prefix}role_allow_override (
2121 `id` int(10) unsigned NOT NULL auto_increment,
2122 `roleid` int(10) unsigned NOT NULL default '0',
2123 `allowoverride` int(10) unsigned NOT NULL default '0',
2124 KEY `roleid` (`roleid`),
2125 KEY `allowoverride` (`allowoverride`),
2126 UNIQUE KEY `roleid-allowoverride` (`roleid`, `allowoverride`),
2127 PRIMARY KEY (`id`)
2128 )", true);
2129
2130 }
d140ad3f 2131
2132 if ($oldversion < 2006082100) {
2133 execute_sql("ALTER TABLE `{$CFG->prefix}context` DROP INDEX `level-instanceid`;",false);
2134 table_column('context', 'level', 'aggregatelevel', 'int', '10', 'unsigned', '0', 'not null', '');
2135 execute_sql("ALTER TABLE `{$CFG->prefix}context` ADD UNIQUE INDEX `aggregatelevel-instanceid` (`aggregatelevel`, `instanceid`)",false);
2136 }
2137
598dc3a9 2138 if ($oldversion < 2006082200) {
2139 table_column('timezone', 'rule', 'tzrule', 'varchar', '20', '', '', 'not null', '');
2140 }
67a3fac5 2141
2142 if ($oldversion < 2006082800) {
2143 table_column('user', '', 'ajax', 'integer', '1', 'unsigned', '1', '', 'htmleditor');
2144 }
647a2566 2145
2146 if ($oldversion < 2006082900) {
e9d5166a 2147 execute_sql("DROP TABLE {$CFG->prefix}sessions", true);
647a2566 2148 execute_sql("
2149 CREATE TABLE {$CFG->prefix}sessions2 (
2150 sesskey VARCHAR(64) NOT NULL default '',
2151 expiry DATETIME NOT NULL,
2152 expireref VARCHAR(250),
2153 created DATETIME NOT NULL,
2154 modified DATETIME NOT NULL,
2155 sessdata TEXT,
2156 CONSTRAINT PRIMARY KEY (sesskey)
2157 ) COMMENT='Optional database session storage in new format, not used by default';", true);
2158
2159 execute_sql("
2160 CREATE INDEX {$CFG->prefix}sess_exp_ix ON {$CFG->prefix}sessions2 (expiry);", true);
2161 execute_sql("
2162 CREATE INDEX {$CFG->prefix}sess_exp2_ix ON {$CFG->prefix}sessions2 (expireref);", true);
2163 }
e9d5166a 2164
2165 if ($oldversion < 2006083001) {
2166 table_column('sessions2', 'sessdata', 'sessdata', 'LONGTEXT', '', '', '', '', '');
2167 }
598dc3a9 2168
be4486da 2169 if ($oldversion < 2006083002) {
2170 table_column('capabilities', '', 'riskbitmask', 'INTEGER', '10', 'unsigned', '0', 'not null', '');
2171 }
2172
99215f2c 2173 if ($oldversion < 2006083100) {
2174 execute_sql("ALTER TABLE {$CFG->prefix}course CHANGE modinfo modinfo longtext NULL AFTER showgrades");
2175 }
2176
6c76032b 2177 if ($oldversion < 2006083101) {
2178 execute_sql("ALTER TABLE {$CFG->prefix}course_categories CHANGE description description text NULL AFTER name");
2179 }
2180
5f941d12 2181 if ($oldversion < 2006083102) {
2182 execute_sql("ALTER TABLE {$CFG->prefix}user CHANGE description description text NULL AFTER url");
2183 }
2184
13317458 2185 if ($oldversion < 2006090200) {
2186 execute_sql("ALTER TABLE {$CFG->prefix}course_sections CHANGE summary summary text NULL AFTER section");
2187 execute_sql("ALTER TABLE {$CFG->prefix}course_sections CHANGE sequence sequence text NULL AFTER section");
2188 }
2189
8d158ed4 2190
b277dd0a 2191 // table to keep track of course page access times, used in online participants block, and participants list
2192 if ($oldversion < 2006091200) {
2193 execute_sql("CREATE TABLE {$CFG->prefix}user_lastaccess (
2194 `id` int(10) unsigned NOT NULL auto_increment,
2195 `userid` int(10) unsigned NOT NULL default '0',
2196 `courseid` int(10) unsigned NOT NULL default '0',
2197 `timeaccess` int(10) unsigned NOT NULL default '0',
2198 KEY `userid` (`userid`),
2199 KEY `courseid` (`courseid`),
2200 UNIQUE KEY `userid-courseid` (`userid`, `courseid`),
2201 PRIMARY KEY (`id`)
2202 )TYPE=MYISAM COMMENT ='time user last accessed any page in a course';", true);
2203 }
8d158ed4 2204
0ad8c785 2205 if ($oldversion < 2006091212) { // Reload the guest roles completely with new defaults
8d158ed4 2206 if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
2207 delete_records('capabilities');
2208 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
2209 foreach ($guestroles as $guestrole) {
2210 delete_records('role_capabilities', 'roleid', $guestrole->id);
2211 assign_capability('moodle/legacy:guest', CAP_ALLOW, $guestrole->id, $sitecontext->id);
2212 }
2213 }
2214 }
2215
746a04c5 2216 if ($oldversion < 2006091700) {
b29ab53d 2217 table_column('course','','defaultrole','integer','10', 'unsigned', '0', 'not null');
746a04c5 2218 }
2219
dca1937c 2220 if ($oldversion < 2006091800) {
2221 delete_records('config', 'name', 'showsiteparticipantslist');
2222 delete_records('config', 'name', 'requestedteachername');
2223 delete_records('config', 'name', 'requestedteachersname');
2224 delete_records('config', 'name', 'requestedstudentname');
2225 delete_records('config', 'name', 'requestedstudentsname');
2226 }
2227
45b9781e 2228 if ($oldversion < 2006091901) {
5d0df39d 2229 if ($roles = get_records('role')) {
2230 $first = array_shift($roles);
2231 if (!empty($first->shortname)) {
2232 // shortnames already exist
2233 } else {
2234 table_column('role', '', 'shortname', 'varchar', '100', '', '', 'not null', 'name');
2235 $legacy_names = array('admin', 'coursecreator', 'editingteacher', 'teacher', 'student', 'guest');
2236 foreach ($legacy_names as $name) {
2237 if ($roles = get_roles_with_capability('moodle/legacy:'.$name, CAP_ALLOW)) {
2238 $i = '';
2239 foreach ($roles as $role) {
2240 if (empty($role->shortname)) {
2241 $updated = new object();
2242 $updated->id = $role->id;
2243 $updated->shortname = $name.$i;
2244 update_record('role', $updated);
2245 $i++;
2246 }
31f26796 2247 }
2248 }
2249 }
2250 }
2251 }
2252 }
45b9781e 2253
adecf1c4 2254 /// Tables for customisable user profile fields
2255 if ($oldversion < 2006092000) {
2256 execute_sql("CREATE TABLE {$CFG->prefix}user_info_field (
2257 id BIGINT(10) NOT NULL auto_increment,
2258 name VARCHAR(255) NOT NULL default '',
2259 datatype VARCHAR(255) NOT NULL default '',
2260 categoryid BIGINT(10) unsigned NOT NULL default 0,
2261 sortorder BIGINT(10) unsigned NOT NULL default 0,
2262 required TINYINT(2) unsigned NOT NULL default 0,
2263 locked TINYINT(2) unsigned NOT NULL default 0,
2264 visible SMALLINT(4) unsigned NOT NULL default 0,
2265 defaultdata LONGTEXT,
2266 CONSTRAINT PRIMARY KEY (id));", true);
2267
2268 execute_sql("ALTER TABLE {$CFG->prefix}user_info_field COMMENT='Customisable user profile fields';", true);
2269
2270 execute_sql("CREATE TABLE {$CFG->prefix}user_info_category (
2271 id BIGINT(10) NOT NULL auto_increment,
2272 name VARCHAR(255) NOT NULL default '',
2273 sortorder BIGINT(10) unsigned NOT NULL default 0,
2274 CONSTRAINT PRIMARY KEY (id));", true);
2275
2276 execute_sql("ALTER TABLE {$CFG->prefix}user_info_category COMMENT='Customisable fields categories';", true);
2277
2278 execute_sql("CREATE TABLE {$CFG->prefix}user_info_data (
2279 id BIGINT(10) NOT NULL auto_increment,
2280 userid BIGINT(10) unsigned NOT NULL default 0,
2281 fieldid BIGINT(10) unsigned NOT NULL default 0,
2282 data LONGTEXT NOT NULL,
2283 CONSTRAINT PRIMARY KEY (id));", true);
2284
2285 execute_sql("ALTER TABLE {$CFG->prefix}user_info_data COMMENT='Data for the customisable user fields';", true);
2286
2287
2288 }
2289
aad2ba95 2290 if ($oldversion < 2006092200) {
01c65218 2291 table_column('context', 'aggregatelevel', 'contextlevel', 'int', '10', 'unsigned', '0', 'not null', '');
1cc309df 2292/* execute_sql("ALTER TABLE `{$CFG->prefix}context` DROP INDEX `aggregatelevel-instanceid`;",false);
01c65218 2293 execute_sql("ALTER TABLE `{$CFG->prefix}context` ADD UNIQUE INDEX `contextlevel-instanceid` (`contextlevel`, `instanceid`)",false); // see 2006092409 below */
aad2ba95 2294 }
2295
0e1412ce 2296 if ($oldversion < 2006092201) {
2297 execute_sql('TRUNCATE TABLE '.$CFG->prefix.'cache_text', true);
2298 table_column('cache_text','formattedtext','formattedtext','longtext','','','','not null');
2299 }
2300
ece4945b 2301 if ($oldversion < 2006092302) {
2302 // fix sortorder first if needed
2303 if ($roles = get_all_roles()) {
2304 $i = 0;
2305 foreach ($roles as $rolex) {
2306 if ($rolex->sortorder != $i) {
2307 $r = new object();
2308 $r->id = $rolex->id;
2309 $r->sortorder = $i;
2310 update_record('role', $r);
2311 }
2312 $i++;
2313 }
2314 }
1cc309df 2315/* execute_sql("ALTER TABLE {$CFG->prefix}role DROP INDEX {$CFG->prefix}role_sor_ix;", false);
2316 execute_sql("ALTER TABLE {$CFG->prefix}role ADD UNIQUE INDEX {$CFG->prefix}role_sor_uix (sortorder)", false);*/
ece4945b 2317 }
2318
25d8040e 2319 if ($oldversion < 2006092400) {
2320 table_column('user', '', 'trustbitmask', 'INTEGER', '10', 'unsigned', '0', 'not null', '');
2321 }
2322
1cc309df 2323 if ($oldversion < 2006092409) {
2324 // ok, once more and now correctly!
2325 execute_sql("DROP INDEX `aggregatelevel-instanceid` ON {$CFG->prefix}context ;", false);
2326 execute_sql("DROP INDEX `contextlevel-instanceid` ON {$CFG->prefix}context ;", false);
2327 execute_sql("CREATE UNIQUE INDEX {$CFG->prefix}cont_conins_uix ON {$CFG->prefix}context (contextlevel, instanceid);", false);
2328
2329 execute_sql("DROP INDEX {$CFG->prefix}role_sor_ix ON {$CFG->prefix}role ;", false);
2330 execute_sql("DROP INDEX {$CFG->prefix}role_sor_uix ON {$CFG->prefix}role ;", false);
2331 execute_sql("CREATE UNIQUE INDEX {$CFG->prefix}role_sor_uix ON {$CFG->prefix}role (sortorder);", false);
2332 }
2333
2efd67bf 2334 if ($oldversion < 2006092601) {
2335 table_column('log_display', 'field', 'field', 'varchar', '200', '', '', 'not null', '');
2336 }
adecf1c4 2337
b61efafb 2338 return $result;
31f0900c 2339}
2340
31513aa1 2341?>