Some blank index.html files to prevent browsing
[moodle.git] / lib / db / postgres7.php
CommitLineData
31f0900c 1<?PHP //$Id$
2//
3// This file keeps track of upgrades to Moodle.
4//
5// Sometimes, changes between versions involve
6// alterations to database structures and other
7// major things that may break installations.
8//
9// The upgrade function in this file will attempt
10// to perform all the necessary actions to upgrade
11// your older installtion to the current version.
12//
13// If there's something it cannot do itself, it
14// will tell you what you need to do.
15//
16// Versions are defined by /version.php
17//
18// This file is tailored to PostgreSQL 7
19
e7311a0a 20function main_upgrade($oldversion=0) {
5867bfb5 21
4d744a22 22 global $CFG, $THEME, $db;
5867bfb5 23
e40488f8 24 $result = true;
5867bfb5 25
26
db70b54b 27 if ($oldversion < 2003010101) {
28 delete_records("log_display", "module", "user");
29 $new->module = "user";
30 $new->action = "view";
31 $new->mtable = "user";
32 $new->field = "CONCAT(firstname,\" \",lastname)";
33 insert_record("log_display", $new);
34
35 delete_records("log_display", "module", "course");
36 $new->module = "course";
37 $new->action = "view";
38 $new->mtable = "course";
39 $new->field = "fullname";
40 insert_record("log_display", $new);
41 $new->action = "update";
42 insert_record("log_display", $new);
43 $new->action = "enrol";
44 insert_record("log_display", $new);
45 }
1924074c 46
047d30d1 47 //support user based course creating
1924074c 48 if ($oldversion < 2003032400) {
a273084d 49 execute_sql("CREATE TABLE {$CFG->prefix}user_coursecreators (
1924074c 50 id int8 SERIAL PRIMARY KEY,
51 userid int8 NOT NULL default '0'
52 )");
047d30d1 53 }
1acfbce5 54
047d30d1 55 if ($oldversion < 2003041400) {
1acfbce5 56 table_column("course_modules", "", "visible", "integer", "1", "unsigned", "1", "not null", "score");
57 }
718ad19f 58
047d30d1 59 if ($oldversion < 2003042104) { // Try to update permissions of all files
718ad19f 60 if ($files = get_directory_list($CFG->dataroot)) {
61 echo "Attempting to update permissions for all files... ignore any errors.";
62 foreach ($files as $file) {
63 echo "$CFG->dataroot/$file<br>";
b369ff55 64 @chmod("$CFG->dataroot/$file", $CFG->directorypermissions);
718ad19f 65 }
66 }
67 }
68
13df5aee 69 if ($oldversion < 2003042400) {
70 // Rebuild all course caches, because of changes to do with visible variable
71 if ($courses = get_records_sql("SELECT * FROM {$CFG->prefix}course")) {
72 require_once("$CFG->dirroot/course/lib.php");
73 foreach ($courses as $course) {
74 $modinfo = serialize(get_array_of_activities($course->id));
75
76 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
77 notify("Could not cache module information for course '$course->fullname'!");
78 }
79 }
80 }
81 }
31f0900c 82
047d30d1 83 if ($oldversion < 2003042500) {
84 // Convert all usernames to lowercase.
85 $users = get_records_sql("SELECT id, username FROM {$CFG->prefix}user");
86 $cerrors = "";
87 $rarray = array();
88
89 foreach ($users as $user) { // Check for possible conflicts
90 $lcname = trim(moodle_strtolower($user->username));
91 if (in_array($lcname, $rarray)) {
e40488f8 92 $cerrors .= $user->id."->".$lcname.'<br/>' ;
047d30d1 93 } else {
94 array_push($rarray,$lcname);
95 }
e40488f8 96 }
047d30d1 97
e40488f8 98 if ($cerrors != '') {
047d30d1 99 notify("Error: Cannot convert usernames to lowercase.
100 Following usernames would overlap (id->username):<br/> $cerrors .
101 Please resolve overlapping errors.");
e40488f8 102 $result = false;
047d30d1 103 }
104
105 $cerrors = "";
106 echo "Checking userdatabase:<br>";
107 foreach ($users as $user) {
108 $lcname = trim(moodle_strtolower($user->username));
109 if ($lcname != $user->username) {
110 $convert = set_field("user" , "username" , $lcname, "id", $user->id);
111 if (!$convert) {
e40488f8 112 if ($cerrors){
113 $cerrors .= ", ";
114 }
115 $cerrors .= $item;
047d30d1 116 } else {
117 echo ".";
e40488f8 118 }
119 }
047d30d1 120 }
121 if ($cerrors != '') {
122 notify("There were errors when converting following usernames to lowercase.
123 '$cerrors' . Sorry, but you will need to fix your database by hand.");
124 $result = false;
e40488f8 125 }
126 }
047d30d1 127
e209e0b0 128 if ($oldversion < 2003042700) {
129 /// Changing to multiple indexes
ea563f8f 130 execute_sql(" CREATE INDEX {$CFG->prefix}log_coursemoduleaction_idx ON {$CFG->prefix}log (course,module,action) ");
131 execute_sql(" CREATE INDEX {$CFG->prefix}log_courseuserid_idx ON {$CFG->prefix}log (course,userid) ");
e209e0b0 132 }
b86fc0e2 133
134 if ($oldversion < 2003042801) {
135 execute_sql("CREATE TABLE {$CFG->prefix}course_display (
136 id SERIAL PRIMARY KEY,
137 course integer NOT NULL default '0',
138 userid integer NOT NULL default '0',
139 display integer NOT NULL default '0'
140 )");
141
ea563f8f 142 execute_sql("CREATE INDEX {$CFG->prefix}course_display_courseuserid_idx ON {$CFG->prefix}course_display (course,userid)");
b86fc0e2 143 }
7d99d695 144
145 if ($oldversion < 2003050400) {
146 table_column("course_sections", "", "visible", "integer", "1", "unsigned", "1", "", "");
147 }
e209e0b0 148
465fd00e 149 if ($oldversion < 2003050401) {
150 table_column("user", "", "lang", "VARCHAR", "5", "", "$CFG->lang" ,"NOT NULL","");
151 }
7e6b0b3b 152
153 if ($oldversion < 2003050900) {
154 table_column("modules", "", "visible", "integer", "1", "unsigned", "1", "", "");
155 }
156
5867bfb5 157 if ($oldversion < 2003050902) {
158 if (get_records("modules", "name", "pgassignment")) {
159 print_simple_box("Note: the pgassignment module will soon be deleted from CVS! Go to the new 'Manage Modules' page and DELETE IT from your system", "center", "50%", "$THEME->cellheading", "20", "noticebox");
160 }
161 }
162
7adf6787 163 if ($oldversion < 2003051600) {
164 print_simple_box("Thanks for upgrading!<p>There are many changes since the last release. Please read the release notes carefully. If you are using CUSTOM themes you will need to edit them. You will also need to check your site's config.php file.", "center", "50%", "$THEME->cellheading", "20", "noticebox");
165 }
166
7f2a3e67 167 if ($oldversion < 2003052300) {
168 table_column("user", "", "autosubscribe", "integer", "1", "unsigned", "1", "", "htmleditor");
169 }
170
a6d82c3f 171 if ($oldversion < 2003072100) {
172 table_column("course", "", "visible", "integer", "1", "unsigned", "1", "", "marker");
173 }
174
dd0bd508 175 if ($oldversion < 2003072101) {
176 table_column("course_sections", "sequence", "sequence", "text", "", "", "", "", "");
177 }
178
a8fa25d1 179 if ($oldversion < 2003072800) {
180 print_simple_box("The following database index improves performance, but can be quite large - if you are upgrading and you have problems with a limited quota you may want to delete this index later from the '{$CFG->prefix}log' table in your database", "center", "50%", "$THEME->cellheading", "20", "noticebox");
1dbb6e50 181 flush();
a8fa25d1 182 execute_sql(" CREATE INDEX {$CFG->prefix}log_timecoursemoduleaction_idx ON {$CFG->prefix}log (time,course,module,action) ");
183 execute_sql(" CREATE INDEX {$CFG->prefix}user_students_courseuserid_idx ON {$CFG->prefix}user_students (course,userid) ");
184 execute_sql(" CREATE INDEX {$CFG->prefix}user_teachers_courseuserid_idx ON {$CFG->prefix}user_teachers (course,userid) ");
185 }
186
c2cb4545 187 if ($oldversion < 2003072802) {
188 table_column("course_categories", "", "description", "text", "", "", "");
189 table_column("course_categories", "", "parent", "integer", "10", "unsigned");
190 table_column("course_categories", "", "sortorder", "integer", "10", "unsigned");
191 table_column("course_categories", "", "courseorder", "text", "", "", "");
192 table_column("course_categories", "", "visible", "integer", "1", "unsigned", "1");
193 table_column("course_categories", "", "timemodified", "integer", "10", "unsigned");
194 }
195
d2b6ba70 196 if ($oldversion < 2003080400) {
c7ce6d80 197 notify("If the following command fails you may want to change the type manually, from TEXT to INTEGER. Moodle should keep working even if you don't.");
d2b6ba70 198 table_column("course_categories", "courseorder", "courseorder", "integer", "10", "unsigned");
199 table_column("course", "", "sortorder", "integer", "10", "unsigned", "0", "", "category");
200 }
201
a142b3b4 202 if ($oldversion < 2003081502) {
203 execute_sql(" CREATE TABLE {$CFG->prefix}scale (
204 id SERIAL PRIMARY KEY,
205 courseid integer NOT NULL default '0',
206 userid integer NOT NULL default '0',
207 name varchar(255) NOT NULL default '',
208 scale text,
209 description text,
210 timemodified integer NOT NULL default '0'
211 )");
212 }
213
11402bbd 214 if ($oldversion < 2003081503) {
6f4f04df 215 table_column("forum", "", "scale", "integer", "10", "unsigned", "0", "", "assessed");
11402bbd 216 get_scales_menu(0); // Just to force the default scale to be created
217 }
218
73047f2f 219 if ($oldversion < 2003081600) {
220 table_column("user_teachers", "", "editall", "integer", "1", "unsigned", "1", "", "role");
221 table_column("user_teachers", "", "timemodified", "integer", "10", "unsigned", "0", "", "editall");
222 }
50b5487c 223
9936fe81 224 if ($oldversion < 2003081900) {
225 table_column("course_categories", "courseorder", "coursecount", "integer", "10", "unsigned", "0");
226 }
227
f63823b3 228 if ($oldversion < 2003080700) {
229 notify("Cleaning up categories and course ordering...");
230 if ($categories = get_categories()) {
231 foreach ($categories as $category) {
232 fix_course_sortorder($category->id);
233 }
234 }
235 }
236
237
fff79722 238 if ($oldversion < 2003082001) {
239 table_column("course", "", "showgrades", "integer", "2", "unsigned", "1", "", "format");
240 }
241
3052e775 242 if ($oldversion < 2003082101) {
243 execute_sql(" CREATE INDEX {$CFG->prefix}course_category_idx ON {$CFG->prefix}course (category) ");
244 }
d0117715 245 if ($oldversion < 2003082702) {
246 execute_sql(" INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'user report', 'user', 'CONCAT(firstname,\" \",lastname)') ");
247 }
3052e775 248
e71f132d 249 if ($oldversion < 2003091000) {
250 # Old field that was never added!
251 table_column("course", "", "showrecent", "integer", "10", "unsigned", "1", "", "numsections");
252 }
253
aac94fd0 254 if ($oldversion < 2003091400) {
255 table_column("course_modules", "", "indent", "integer", "5", "unsigned", "0", "", "score");
256 }
257
4909e176 258 if ($oldversion < 2003092900) {
259 table_column("course", "", "maxbytes", "integer", "10", "unsigned", "0", "", "marker");
260 }
aac94fd0 261
4d744a22 262 if ($oldversion < 2003102700) {
263 table_column("user_students", "", "timeaccess", "integer", "10", "unsigned", "0", "", "time");
264 table_column("user_teachers", "", "timeaccess", "integer", "10", "unsigned", "0", "", "timemodified");
265
4d744a22 266 $db->debug = false;
29b25b04 267 $CFG->debug = 0;
268 notify("Calculating access times. Please wait - this may take a long time on big sites...", "green");
269 flush();
270
271 if ($courses = get_records_select("course", "category > 0")) {
272 foreach ($courses as $course) {
273 notify("Processing $course->fullname ...", "green");
274 flush();
275 if ($users = get_records_select("user_teachers", "course = '$course->id'",
276 "id", "id, userid, timeaccess")) {
277 foreach ($users as $user) {
278 $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log WHERE course = '$course->id' and userid = '$user->userid' ORDER by time DESC");
279 if (empty($loginfo->time)) {
280 $loginfo->time = 0;
281 }
282 execute_sql("UPDATE {$CFG->prefix}user_teachers SET timeaccess = '$loginfo->time'
283 WHERE userid = '$user->userid' AND course = '$course->id'", false);
284
285 }
286 }
287
288 if ($users = get_records_select("user_students", "course = '$course->id'",
289 "id", "id, userid, timeaccess")) {
290 foreach ($users as $user) {
291 $loginfo = get_record_sql("SELECT id, time FROM {$CFG->prefix}log
292 WHERE course = '$course->id' and userid = '$user->userid'
293 ORDER by time DESC");
294 if (empty($loginfo->time)) {
295 $loginfo->time = 0;
296 }
297 execute_sql("UPDATE {$CFG->prefix}user_students
298 SET timeaccess = '$loginfo->time'
299 WHERE userid = '$user->userid' AND course = '$course->id'", false);
300
301 }
302 }
303 }
4d744a22 304 }
29b25b04 305 notify("All courses complete.", "green");
4d744a22 306 $db->debug = true;
307 }
308
3f125001 309 if ($oldversion < 2003103100) {
310 table_column("course", "", "showreports", "integer", "4", "unsigned", "0", "", "maxbytes");
311 }
312
4d744a22 313
f374fb10 314 if ($oldversion < 2003121600) {
0da33e07 315 execute_sql("CREATE TABLE {$CFG->prefix}groups (
f374fb10 316 id SERIAL PRIMARY KEY,
317 courseid integer NOT NULL default '0',
318 name varchar(255) NOT NULL default '',
319 description text,
320 lang varchar(10) NOT NULL default '',
321 picture integer NOT NULL default '0',
322 timecreated integer NOT NULL default '0',
323 timemodified integer NOT NULL default '0'
324 )");
325
0da33e07 326 execute_sql("CREATE INDEX {$CFG->prefix}groups_idx ON {$CFG->prefix}groups (courseid) ");
f374fb10 327
0da33e07 328 execute_sql("CREATE TABLE {$CFG->prefix}groups_members (
f374fb10 329 id SERIAL PRIMARY KEY,
330 groupid integer NOT NULL default '0',
331 userid integer NOT NULL default '0',
332 timeadded integer NOT NULL default '0'
333 )");
334
0da33e07 335 execute_sql("CREATE INDEX {$CFG->prefix}groups_members_idx ON {$CFG->prefix}groups_members (groupid) ");
f374fb10 336 }
337
338 if ($oldversion < 2003122600) {
339 table_column("course", "", "groupmode", "integer", "4", "unsigned", "0", "", "visible");
340 table_column("course", "", "groupmodeforce", "integer", "4", "unsigned", "0", "", "groupmode");
341 }
342
ddc7afad 343 if ($oldversion < 2004010900) {
344 table_column("course_modules", "", "groupmode", "integer", "4", "unsigned", "0", "", "visible");
345 }
346
5fba04fb 347 if ($oldversion < 2004011700) {
348 modify_database("", "CREATE TABLE prefix_event (
349 id SERIAL PRIMARY KEY,
350 name varchar(255) NOT NULL default '',
351 description text,
352 courseid integer NOT NULL default '0',
353 groupid integer NOT NULL default '0',
354 userid integer NOT NULL default '0',
355 modulename varchar(20) NOT NULL default '',
356 instance integer NOT NULL default '0',
357 eventtype varchar(20) NOT NULL default '',
358 timestart integer NOT NULL default '0',
359 timeduration integer NOT NULL default '0',
360 timemodified integer NOT NULL default '0'
361 ); ");
362
363 modify_database("", "CREATE INDEX prefix_event_courseid_idx ON prefix_event (courseid);");
364 modify_database("", "CREATE INDEX prefix_event_userid_idx ON prefix_event (userid);");
365 }
366
367
70812e39 368 if ($oldversion < 2004012800) {
369 modify_database("", "CREATE TABLE prefix_user_preferences (
370 id SERIAL PRIMARY KEY,
371 userid integer NOT NULL default '0',
372 name varchar(50) NOT NULL default '',
373 value varchar(255) NOT NULL default ''
374 ); ");
375
376 modify_database("", "CREATE INDEX prefix_user_preferences_useridname_idx ON prefix_user_preferences (userid,name);");
377 }
378
ba39fa10 379 if ($oldversion < 2004012900) {
380 table_column("config", "value", "value", "text", "", "", "");
381 }
69d79bc3 382
383 if ($oldversion < 2004013101) {
384 table_column("log", "", "cmid", "integer", "10", "unsigned", "0", "", "module");
4da1a0a1 385 set_config("upgrade", "logs");
69d79bc3 386 }
95d45757 387
b3153e4b 388 if ($oldversion < 2004020900) {
389 table_column("course", "", "lang", "varchar", "5", "", "", "", "groupmodeforce");
390 }
391
45121ffb 392 if ($oldversion < 2004020903) {
393 modify_database("", "CREATE TABLE prefix_cache_text (
d363047e 394 id SERIAL PRIMARY KEY,
395 md5key varchar(32) NOT NULL default '',
396 formattedtext text,
397 timemodified integer NOT NULL default '0'
398 );");
399 }
400
d523d2ea 401 if ($oldversion < 2004021000) {
402 $textfilters = array();
403 for ($i=1; $i<=10; $i++) {
404 $variable = "textfilter$i";
405 if (!empty($CFG->$variable)) { /// No more filters
406 if (is_readable("$CFG->dirroot/".$CFG->$variable)) {
407 $textfilters[] = $CFG->$variable;
408 }
409 }
410 }
411 $textfilters = implode(',', $textfilters);
412 if (empty($textfilters)) {
413 $textfilters = 'mod/glossary/dynalink.php';
414 }
415 set_config('textfilters', $textfilters);
416 }
417
f1d604cb 418 if ($oldversion < 2004021201) {
419 modify_database("", "CREATE TABLE prefix_cache_filters (
420 id SERIAL PRIMARY KEY,
421 filter varchar(32) NOT NULL default '',
422 version integer NOT NULL default '0',
423 md5key varchar(32) NOT NULL default '',
424 rawtext text,
425 timemodified integer NOT NULL default '0'
426 );");
427
428 modify_database("", "CREATE INDEX prefix_cache_filters_filtermd5key_idx ON prefix_cache_filters (filter,md5key);");
429 modify_database("", "CREATE INDEX prefix_cache_text_md5key_idx ON prefix_cache_text (md5key);");
430 }
d523d2ea 431
3c0561cf 432 if ($oldversion < 2004021500) {
433 table_column("groups", "", "hidepicture", "integer", "2", "unsigned", "0", "", "picture");
434 }
435
9e353ce7 436 if ($oldversion < 2004021700) {
437 if (!empty($CFG->textfilters)) {
438 $CFG->textfilters = str_replace("tex_filter.php", "filter.php", $CFG->textfilters);
439 $CFG->textfilters = str_replace("multilang.php", "filter.php", $CFG->textfilters);
440 $CFG->textfilters = str_replace("censor.php", "filter.php", $CFG->textfilters);
441 $CFG->textfilters = str_replace("mediaplugin.php", "filter.php", $CFG->textfilters);
442 $CFG->textfilters = str_replace("algebra_filter.php", "filter.php", $CFG->textfilters);
828aeff2 443 $CFG->textfilters = str_replace("dynalink.php", "filter.php", $CFG->textfilters);
9e353ce7 444 set_config("textfilters", $CFG->textfilters);
445 }
446 }
447
8199e3f0 448 if ($oldversion < 2004022000) {
449 table_column("user", "", "emailstop", "integer", "1", "unsigned", "0", "not null", "email");
450 }
451
828aeff2 452 if ($oldversion < 2004022200) { /// Final renaming I hope. :-)
453 if (!empty($CFG->textfilters)) {
454 $CFG->textfilters = str_replace("/filter.php", "", $CFG->textfilters);
a4dda51f 455 $CFG->textfilters = str_replace("mod/glossary/dynalink.php", "mod/glossary", $CFG->textfilters);
828aeff2 456 $textfilters = explode(',', $CFG->textfilters);
457 foreach ($textfilters as $key => $textfilter) {
458 $textfilters[$key] = trim($textfilter);
459 }
460 set_config("textfilters", implode(',',$textfilters));
461 }
462 }
463
4bf58667 464 if ($oldversion < 2004030702) { /// Because of the renaming of Czech language pack
465 execute_sql("UPDATE {$CFG->prefix}user SET lang = 'cs' WHERE lang = 'cz'");
466 execute_sql("UPDATE {$CFG->prefix}course SET lang = 'cs' WHERE lang = 'cz'");
7277e306 467 }
468
e40488f8 469 return $result;
95d45757 470
e40488f8 471}
f374fb10 472
95d45757 473?>