MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / lesson / db / postgres7.php
blobc644e7a70a10d8235ff36bb3567ca26e7f03049c
1 <?PHP
3 // THIS FILE IS DEPRECATED! PLEASE DO NOT MAKE CHANGES TO IT!
4 //
5 // IT IS USED ONLY FOR UPGRADES FROM BEFORE MOODLE 1.7, ALL
6 // LATER CHANGES SHOULD USE upgrade.php IN THIS DIRECTORY.
8 function lesson_upgrade($oldversion) {
9 /// This function does anything necessary to upgrade
10 /// older versions to match current functionality
12 global $CFG;
14 if ($oldversion < 2004021600) {
16 delete_records("log_display", "module", "lesson");
18 modify_database ("", "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('lesson', 'start', 'lesson', 'name');");
19 modify_database ("", "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('lesson', 'end', 'lesson', 'name');");
20 modify_database ("", "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('lesson', 'view', 'lesson_pages', 'title');");
24 if ($oldversion < 2004022200) {
26 table_column("lesson", "", "maxattempts", "INTEGER", "4", "UNSIGNED", "0", "NOT NULL", "maxanswers");
27 table_column("lesson", "", "nextpagedefault", "INTEGER", "4", "UNSIGNED", "0", "NOT NULL", "maxattempts");
28 table_column("lesson", "", "maxpages", "INTEGER", "4", "UNSIGNED", "0", "NOT NULL", "nextpagedefault");
29 table_column("lesson_pages", "", "qtype", "INTEGER", "4", "UNSIGNED", "0", "NOT NULL", "lessonid");
30 table_column("lesson_pages", "", "qoption", "INTEGER", "4", "UNSIGNED", "0", "NOT NULL", "qtype");
31 table_column("lesson_answers", "", "grade", "INTEGER", "4", "UNSIGNED", "0", "NOT NULL", "jumpto");
35 if ($oldversion < 2004032000) { // Upgrade some old beta lessons
36 execute_sql(" UPDATE \"{$CFG->prefix}lesson_pages\" SET qtype = 3 WHERE qtype = 0");
39 if ($oldversion < 2004032400) {
40 table_column("lesson", "", "usemaxgrade", "INTEGER", "4", "UNSIGNED", "0", "NOT NULL", "grade");
41 table_column("lesson", "", "minquestions", "INTEGER", "4", "UNSIGNED", "0", "NOT NULL", "nextpagedefault");
44 if ($oldversion < 2004032700) {
45 table_column("lesson_answers", "", "flags", "INTEGER", "4", "UNSIGNED", "0", "NOT NULL", "grade");
48 if ($oldversion < 2004060501) {
49 // matching questions need 2 records for responses and the
50 // 2 records must appear before the old ones. So, delete the old ones,
51 // create the 2 needed, then re-insert the old ones for each matching question.
52 if ($matchingquestions = get_records('lesson_pages', 'qtype', 5)) { // get our matching questions
53 foreach ($matchingquestions as $matchingquestion) {
54 if ($answers = get_records('lesson_answers', 'pageid', $matchingquestion->id)) { // get answers
55 if (delete_records('lesson_answers', 'pageid', $matchingquestion->id)) { // delete them
56 $time = time();
57 // make our 2 response answers
58 $newanswer->lessonid = $matchingquestion->lessonid;
59 $newanswer->pageid = $matchingquestion->id;
60 $newanswer->timecreated = $time;
61 $newanswer->timemodified = 0;
62 insert_record('lesson_answers', $newanswer);
63 insert_record('lesson_answers', $newanswer);
64 // insert our old answers
65 foreach ($answers as $answer) {
66 $answer->timecreated = $time;
67 $answer->timemodified = 0;
68 insert_record('lesson_answers', (object) array_map('addslashes', (array)$answer));
76 if ($oldversion < 2004072100) {
77 execute_sql(" create table ".$CFG->prefix."lesson_high_scores
78 ( id serial8 primary key,
79 lessonid int8 not null default '0',
80 userid int8 not null default '0',
81 gradeid int8 not null default '0',
82 nickname varchar(5) not null default ''
83 )");
85 execute_sql(" create table ".$CFG->prefix."lesson_essay
86 ( id serial8 primary key,
87 lessonid int8 not null default '0',
88 userid int8 not null default '0',
89 pageid int8 not null default '0',
90 answerid int8 not null default '0',
91 try int8 not null default '0',
92 answer text not null default '',
93 graded int4 not null default 0,
94 score int8 not null default 0,
95 response text not null default '',
96 sent int4 not null default 0,
97 timesubmitted int8 not null default '0'
98 )");
100 execute_sql(" create table ".$CFG->prefix."lesson_branch
101 ( id serial8 primary key,
102 lessonid int8 not null default '0',
103 userid int8 not null default '0',
104 pageid int8 not null default '0',
105 retry int8 not null default '0',
106 flag int4 not null default '0',
107 timeseen int8 not null default '0'
108 )");
111 execute_sql(" create table ".$CFG->prefix."lesson_timer
112 ( id serial8 primary key,
113 lessonid int8 not null default '0',
114 userid int8 not null default '0',
115 starttime int8 not null default '0',
116 lessontime int8 not nul default '0'l
117 )");
121 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson_pages ADD layout TINYINT(3) UNSIGNED NOT NULL DEFAULT '1' AFTER qoption");
122 table_column('lesson_pages','','layout','int','3','unsigned', '1', 'not null', 'qoption');
123 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson_pages ADD display TINYINT(3) UNSIGNED NOT NULL DEFAULT '1' AFTER layout");
124 table_column('lesson_pages','','display','int','3','unsigned', '1', 'not null', 'layout');
126 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson_answers ADD score INT(10) NOT NULL DEFAULT '0' AFTER grade");
127 table_column('lesson_answers','','score','int','10','unsigned', '1', 'not null', 'grade');
129 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD usepassword TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER name");
130 table_column('lesson','','usepassword','int','3','unsigned', '0', 'not null', 'name');
132 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD password VARCHAR(32) NOT NULL DEFAULT '' AFTER usepassword");
133 table_column('lesson','','password','varchar','32','', '', 'not null', 'usepassword');
135 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD custom TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER grade");
136 table_column('lesson','','custom','int','3','unsigned', '0', 'not null', 'grade');
138 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD ongoing TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER custom");
139 table_column('lesson','','ongoing','int','3','unsigned', '0', 'not null', 'custom');
141 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD timed TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER maxpages");
142 table_column('lesson','','timed','int','3','unsigned', '0', 'not null', 'maxpages');
144 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD maxtime INT(10) UNSIGNED NOT NULL DEFAULT '0' AFTER timed");
145 table_column('lesson','','maxtime','int','10','unsigned', '0', 'not null', 'timed');
147 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD tree TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER retake");
148 table_column('lesson','','tree','int','3','unsigned', '0', 'not null', 'retake');
150 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD slideshow TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER tree");
151 table_column('lesson','','slideshow','int','3','unsigned', '0', 'not null', 'tree');
153 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD width INT(10) UNSIGNED NOT NULL DEFAULT '640' AFTER slideshow");
154 table_column('lesson','','width','int','10','unsigned', '640', 'not null', 'slideshow');
156 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD height INT(10) UNSIGNED NOT NULL DEFAULT '480' AFTER width");
157 table_column('lesson','','height','int','10','unsigned', '480', 'not null', 'width');
159 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD bgcolor CHAR(7) NOT NULL DEFAULT '#FFFFFF' AFTER height");
160 table_column('lesson','','bgcolor','varchar','7','unsigned', '#FFFFFF', 'not null', 'height');
162 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD displayleft TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER bgcolor");
163 table_column('lesson','','displayleft','int','3','unsigned', '0', 'not null', 'bgcolor');
165 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD highscores TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER displayleft");
166 table_column('lesson','','highscores','int','3','unsigned', '0', 'not null', 'displayleft');
168 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD maxhighscores INT(10) UNSIGNED NOT NULL DEFAULT '0' AFTER highscores");
169 table_column('lesson','','maxhighscores','int','10','unsigned', '0', 'not null', 'highscores');
173 if ($oldversion < 2004081100) {
174 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD practice TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER name");
175 table_column('lesson','','practice','int','3','unsigned', '0', 'not null', 'name');
176 //execute_sql(" ALTER TABLE {$CFG->prefix}lesson ADD review TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER maxattempts");
177 table_column('lesson','','review','int','3','unsigned', '0', 'not null', 'maxattempts');
180 if ($oldversion < 2004081700) {
181 execute_sql("CREATE TABLE {$CFG->prefix}lesson_default
182 ( id serial8 primary key,
183 course int8 NOT NULL default '0',
184 practice int4 NOT NULL default '0',
185 password varchar(32) NOT NULL default '',
186 usepassword int4 NOT NULL default '0',
187 grade int4 NOT NULL default '0',
188 custom int4 NOT NULL default '0',
189 ongoing int4 NOT NULL default '0',
190 usemaxgrade int4 NOT NULL default '0',
191 maxanswers int4 NOT NULL default '4',
192 maxattempts int4 NOT NULL default '5',
193 review int4 NOT NULL default '0',
194 nextpagedefault int4 NOT NULL default '0',
195 minquestions int4 NOT NULL default '0',
196 maxpages int4 NOT NULL default '0',
197 timed int4 NOT NULL default '0',
198 maxtime int8 NOT NULL default '0',
199 retake int4 NOT NULL default '1',
200 tree int4 NOT NULL default '0',
201 slideshow int4 NOT NULL default '0',
202 width int8 NOT NULL default '640',
203 height int8 NOT NULL default '480',
204 bgcolor varchar(7) default '#FFFFFF',
205 displayleft int4 NOT NULL default '0',
206 highscores int4 NOT NULL default '0',
207 maxhighscores int8 NOT NULL default '0'
208 )");
211 if ($oldversion < 2004100400) {
212 //execute_sql(" ALTER TABLE `{$CFG->prefix}lesson_attempts` ADD `useranswer` text NOT NULL AFTER correct");
213 table_column('lesson_attempts', '', 'useranswer', 'text', '', '', '', 'NOT NULL', 'correct');
216 if ($oldversion < 2004100700) {
217 //execute_sql(" ALTER TABLE `{$CFG->prefix}lesson` ADD `modattempts` tinyint(3) unsigned NOT NULL default '0' AFTER practice");
218 table_column('lesson', '', 'modattempts', 'INT', '4', 'unsigned', '0', 'NOT NULL', 'practice');
221 if ($oldversion < 2004102600) {
222 //execute_sql(" ALTER TABLE `{$CFG->prefix}lesson_default` ADD `modattempts` tinyint(3) unsigned NOT NULL default '0' AFTER practice");
223 table_column('lesson_default', '', 'modattempts', 'INT', '4', 'unsigned', '0', 'NOT NULL', 'practice');
226 if ($oldversion < 2004111200) {
227 execute_sql("DROP INDEX {$CFG->prefix}lesson_course_idx;",false);
228 execute_sql("DROP INDEX {$CFG->prefix}lesson_answers_lessonid_idx;",false);
229 execute_sql("DROP INDEX {$CFG->prefix}lesson_answers_pageid_idx;",false);
230 execute_sql("DROP INDEX {$CFG->prefix}lesson_attempts_lessonid_idx;",false);
231 execute_sql("DROP INDEX {$CFG->prefix}lesson_attempts_pageid_idx;",false);
232 execute_sql("DROP INDEX {$CFG->prefix}lesson_attempts_userid_idx;",false);
233 execute_sql("DROP INDEX {$CFG->prefix}lesson_grades_lessonid_idx;",false);
234 execute_sql("DROP INDEX {$CFG->prefix}lesson_grades_userid_idx;",false);
235 execute_sql("DROP INDEX {$CFG->prefix}lesson_pages_lessonid_idx;",false);
237 modify_database('','CREATE INDEX prefix_lesson_course_idx ON prefix_lesson (course);');
238 modify_database('','CREATE INDEX prefix_lesson_answers_lessonid_idx ON prefix_lesson_answers (lessonid);');
239 modify_database('','CREATE INDEX prefix_lesson_answers_pageid_idx ON prefix_lesson_answers (pageid);');
240 modify_database('','CREATE INDEX prefix_lesson_attempts_lessonid_idx ON prefix_lesson_attempts (lessonid);');
241 modify_database('','CREATE INDEX prefix_lesson_attempts_pageid_idx ON prefix_lesson_attempts (pageid);');
242 modify_database('','CREATE INDEX prefix_lesson_attempts_userid_idx ON prefix_lesson_attempts (userid);');
243 modify_database('','CREATE INDEX prefix_lesson_grades_lessonid_idx ON prefix_lesson_grades (lessonid);');
244 modify_database('','CREATE INDEX prefix_lesson_grades_userid_idx ON prefix_lesson_grades (userid);');
245 modify_database('','CREATE INDEX prefix_lesson_pages_lessonid_idx ON prefix_lesson_pages (lessonid);');
248 if ($oldversion < 2005060900) {
249 table_column('lesson_grades', 'grade', 'grade', 'real', '', 'unsigned', '0', 'not null');
252 if ($oldversion < 2005060901) { // Mass cleanup of bad postgres upgrade scripts
253 modify_database('','ALTER TABLE prefix_lesson ALTER bgcolor SET NOT NULL');
254 modify_database('','ALTER TABLE prefix_lesson ALTER custom SET NOT NULL');
255 table_column('lesson','height','height','integer','16','unsigned','480');
256 modify_database('','ALTER TABLE prefix_lesson ALTER highscores SET NOT NULL');
257 modify_database('','ALTER TABLE prefix_lesson ALTER maxattempts SET DEFAULT 5');
258 table_column('lesson','maxhighscores','maxhighscores','integer','16');
259 modify_database('','ALTER TABLE prefix_lesson ALTER displayleft SET NOT NULL');
260 table_column('lesson','','minquestions','integer','8');
261 notify('The above error can be ignored if the column already exists, its possible that it was cleaned up already before running this upgrade');
262 table_column('lesson','maxtime','maxtime','integer','16');
263 modify_database('','ALTER TABLE prefix_lesson ALTER ongoing SET NOT NULL');
264 modify_database('','ALTER TABLE prefix_lesson ALTER password SET NOT NULL');
265 modify_database('','ALTER TABLE prefix_lesson ALTER practice SET NOT NULL');
266 modify_database('','ALTER TABLE prefix_lesson ALTER review SET NOT NULL');
267 modify_database('','ALTER TABLE prefix_lesson ALTER slideshow SET NOT NULL');
268 modify_database('','ALTER TABLE prefix_lesson ALTER timed SET NOT NULL');
269 modify_database('','ALTER TABLE prefix_lesson ALTER tree SET NOT NULL');
270 modify_database('','ALTER TABLE prefix_lesson ALTER usepassword SET NOT NULL');
271 modify_database('','ALTER TABLE prefix_lesson ALTER width SET NOT NULL');
272 table_column('lesson','width','width','integer','16','unsigned','640');
273 table_column('lesson_answers','flags','flags','integer','8');
274 table_column('lesson_answers','grade','grade','integer','8');
275 table_column('lesson_answers','score','score','integer','16');
276 modify_database('','ALTER TABLE prefix_lesson_grades ALTER grade SET NOT NULL');
277 modify_database('','ALTER TABLE prefix_lesson_pages ALTER display SET NOT NULL');
278 modify_database('','ALTER TABLE prefix_lesson_pages ALTER layout SET NOT NULL');
279 table_column('lesson_pages','qoption','qoption','integer','8');
280 table_column('lesson_pages','qtype','qtype','integer','8');
283 if ($oldversion < 2005061500) {
284 table_column('lesson', '', 'mediafile', 'varchar', '255', '', '', 'not null', 'tree');
287 if ($oldversion < 2005063000) {
288 table_column('lesson', '', 'dependency', 'INT', '8', 'unsigned', '0', 'not null', 'usepassword');
289 table_column('lesson', '', 'conditions', 'text', '', '', '', 'not null', 'dependency');
292 if ($oldversion < 2005101900) {
293 table_column('lesson', '', 'progressbar', 'INT', '3', 'unsigned', '0', 'not null', 'displayleft');
294 table_column('lesson', '', 'displayleftif', 'INT', '3', 'unsigned', '0', 'not null', 'displayleft');
297 if ($oldversion < 2005102800) {
298 table_column('lesson', '', 'mediaclose', 'INT', '3', 'unsigned', '0', 'not null', 'mediafile');
299 table_column('lesson', '', 'mediaheight', 'INT', '10', 'unsigned', '100', 'not null', 'mediafile');
300 table_column('lesson', '', 'mediawidth', 'INT', '10', 'unsigned', '650', 'not null', 'mediafile');
303 if ($oldversion < 2005110200) {
304 table_column('lesson', '', 'activitylink', 'INT', '10', 'unsigned', '0', 'not null', 'tree');
307 if ($oldversion < 2006031900) {
308 execute_sql('ALTER TABLE '. $CFG->prefix . 'lesson DROP COLUMN tree');
309 execute_sql('ALTER TABLE '. $CFG->prefix . 'lesson_default DROP COLUMN tree');
312 if ($oldversion < 2006050100) {
313 table_column('lesson_default', '', 'conditions', 'text', '', '', '', 'not null', 'password');
314 table_column('lesson_default', '', 'progressbar', 'tinyint', '3', 'unsigned', '0', 'not null', 'displayleft');
315 table_column('lesson_default', '', 'displayleftif', 'int', '3', 'unsigned', '0', 'not null', 'displayleft');
316 table_column('lesson_default', '', 'mediaclose', 'tinyint', '3', 'unsigned', '0', 'not null', 'retake');
317 table_column('lesson_default', '', 'mediaheight', 'int', '10', 'unsigned', '100', 'not null', 'retake');
318 table_column('lesson_default', '', 'mediawidth', 'int', '10', 'unsigned', '650', 'not null', 'retake');
321 if ($oldversion < 2006050101) {
322 // drop the unused table
323 execute_sql('DROP TABLE '.$CFG->prefix.'lesson_essay', false);
325 // properly set the correct default values
326 table_column('lesson', 'activitylink', 'activitylink', 'integer', '8', '', '0');
327 table_column('lesson', 'dependency', 'dependency', 'integer', '8', '', '0');
329 modify_database('', 'ALTER TABLE prefix_lesson_timer
330 ALTER COLUMN lessontime SET DEFAULT 0');
331 modify_database('', 'ALTER TABLE prefix_lesson_timer
332 ALTER COLUMN lessonid SET DEFAULT 0');
333 modify_database('', 'ALTER TABLE prefix_lesson_timer
334 ALTER COLUMN userid SET DEFAULT 0');
335 modify_database('', 'ALTER TABLE prefix_lesson_timer
336 ALTER COLUMN starttime SET DEFAULT 0');
338 modify_database('', 'ALTER TABLE prefix_lesson_branch
339 ALTER COLUMN lessonid SET DEFAULT 0');
340 modify_database('', 'ALTER TABLE prefix_lesson_branch
341 ALTER COLUMN timeseen SET DEFAULT 0');
342 modify_database('', 'ALTER TABLE prefix_lesson_branch
343 ALTER COLUMN userid SET DEFAULT 0');
344 modify_database('', 'ALTER TABLE prefix_lesson_branch
345 ALTER COLUMN retry SET DEFAULT 0');
346 modify_database('', 'ALTER TABLE prefix_lesson_branch
347 ALTER COLUMN pageid SET DEFAULT 0');
348 modify_database('', 'ALTER TABLE prefix_lesson_branch
349 ALTER COLUMN flag SET DEFAULT 0');
351 modify_database('', 'ALTER TABLE prefix_lesson_high_scores
352 ALTER COLUMN nickname SET DEFAULT \'\'');
353 modify_database('', 'ALTER TABLE prefix_lesson_high_scores
354 ALTER COLUMN lessonid SET DEFAULT 0');
355 modify_database('', 'ALTER TABLE prefix_lesson_high_scores
356 ALTER COLUMN gradeid SET DEFAULT 0');
357 modify_database('', 'ALTER TABLE prefix_lesson_high_scores
358 ALTER COLUMN userid SET DEFAULT 0');
361 if ($oldversion < 2006091202) {
362 table_column('lesson', '', 'feedback', 'int', '3', 'unsigned', '1', 'not null', 'nextpagedefault');
363 table_column('lesson_default', '', 'feedback', 'int', '3', 'unsigned', '1', 'not null', 'nextpagedefault');
366 ////// DO NOT ADD NEW THINGS HERE!! USE upgrade.php and the lib/ddllib.php functions.
368 return true;