Minor changes
[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
202 if ($oldversion < 2003080700) {
203 notify("Cleaning up categories and course ordering...");
204 if ($categories = get_categories()) {
205 foreach ($categories as $category) {
206 fix_course_sortorder($category->id);
207 }
208 }
209 }
210
a142b3b4 211 if ($oldversion < 2003081502) {
212 execute_sql(" CREATE TABLE {$CFG->prefix}scale (
213 id SERIAL PRIMARY KEY,
214 courseid integer NOT NULL default '0',
215 userid integer NOT NULL default '0',
216 name varchar(255) NOT NULL default '',
217 scale text,
218 description text,
219 timemodified integer NOT NULL default '0'
220 )");
221 }
222
11402bbd 223 if ($oldversion < 2003081503) {
6f4f04df 224 table_column("forum", "", "scale", "integer", "10", "unsigned", "0", "", "assessed");
11402bbd 225 get_scales_menu(0); // Just to force the default scale to be created
226 }
227
73047f2f 228 if ($oldversion < 2003081600) {
229 table_column("user_teachers", "", "editall", "integer", "1", "unsigned", "1", "", "role");
230 table_column("user_teachers", "", "timemodified", "integer", "10", "unsigned", "0", "", "editall");
231 }
50b5487c 232
9936fe81 233 if ($oldversion < 2003081900) {
234 table_column("course_categories", "courseorder", "coursecount", "integer", "10", "unsigned", "0");
235 }
236
fff79722 237 if ($oldversion < 2003082001) {
238 table_column("course", "", "showgrades", "integer", "2", "unsigned", "1", "", "format");
239 }
240
3052e775 241 if ($oldversion < 2003082101) {
242 execute_sql(" CREATE INDEX {$CFG->prefix}course_category_idx ON {$CFG->prefix}course (category) ");
243 }
d0117715 244 if ($oldversion < 2003082702) {
245 execute_sql(" INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('course', 'user report', 'user', 'CONCAT(firstname,\" \",lastname)') ");
246 }
3052e775 247
e71f132d 248 if ($oldversion < 2003091000) {
249 # Old field that was never added!
250 table_column("course", "", "showrecent", "integer", "10", "unsigned", "1", "", "numsections");
251 }
252
aac94fd0 253 if ($oldversion < 2003091400) {
254 table_column("course_modules", "", "indent", "integer", "5", "unsigned", "0", "", "score");
255 }
256
4909e176 257 if ($oldversion < 2003092900) {
258 table_column("course", "", "maxbytes", "integer", "10", "unsigned", "0", "", "marker");
259 }
aac94fd0 260
4d744a22 261 if ($oldversion < 2003102700) {
262 table_column("user_students", "", "timeaccess", "integer", "10", "unsigned", "0", "", "time");
263 table_column("user_teachers", "", "timeaccess", "integer", "10", "unsigned", "0", "", "timemodified");
264
265 $users = get_records_select("user", "id > 0", "", "id, lastaccess");
266
267 $db->debug = false;
268 foreach ($users as $user) {
269 execute_sql("UPDATE {$CFG->prefix}user_students SET timeaccess = '$user->lastaccess' WHERE userid = '$user->id'", false);
270 execute_sql("UPDATE {$CFG->prefix}user_teachers SET timeaccess = '$user->lastaccess' WHERE userid = '$user->id'", false);
271 }
272 $db->debug = true;
273 }
274
275
e40488f8 276 return $result;
277}
278?>