MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / scorm / db / postgres7.php
blob9398ad729af07c3b44df2f37fa7def11ebe661eb
1 <?php // $Id$
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 scorm_upgrade($oldversion) {
9 // This function does anything necessary to upgrade
10 // older versions to match current functionality
11 global $CFG;
12 if ($oldversion < 2004033000) {
13 table_column("scorm", "", "auto", "integer", "1", "", "0", "NOT NULL", "summary");
15 if ($oldversion < 2004040900) {
16 table_column("scorm_sco_users", "", "cmi_core_score_raw", "real", "3", "", "0", "NOT NULL", "cmi_core_session_time");
18 if ($oldversion < 2004061800) {
19 table_column("scorm", "", "popup", "varchar", "255", "", "", "NOT NULL", "auto");
20 table_column("scorm", "reference", "reference", "varchar", "255", "", "", "NOT NULL");
22 if ($oldversion < 2004070800) {
23 table_column("scorm_scoes", "", "datafromlms", "TEXT", "", "", "", "NOT NULL", "title");
24 modify_database("", "ALTER TABLE {$CFG->prefix}scorm_sco_users DROP cmi_launch_data;");
26 if ($oldversion < 2004071700) {
27 table_column("scorm_scoes", "", "manifest", "VARCHAR", "255", "", "", "NOT NULL", "scorm");
28 table_column("scorm_scoes", "", "organization", "VARCHAR", "255", "", "", "NOT NULL", "manifest");
30 if ($oldversion < 2004071900) {
31 table_column("scorm", "", "maxgrade", "real", "3", "", "0", "NOT NULL", "reference");
32 table_column("scorm", "", "grademethod", "integer", "", "", "0", "NOT NULL", "maxgrade");
35 if ($oldversion < 2004111200) {
36 execute_sql("DROP INDEX {$CFG->prefix}scorm_course_idx;",false);
37 execute_sql("DROP INDEX {$CFG->prefix}scorm_scoes_scorm_idx;",false);
38 execute_sql("DROP INDEX {$CFG->prefix}scorm_sco_users_userid_idx;",false);
39 execute_sql("DROP INDEX {$CFG->prefix}scorm_sco_users_scormid_idx;",false);
40 execute_sql("DROP INDEX {$CFG->prefix}scorm_sco_users_scoid_idx;",false);
42 modify_database('','CREATE INDEX prefix_scorm_course_idx ON prefix_scorm (course);');
43 modify_database('','CREATE INDEX prefix_scorm_scoes_scorm_idx ON prefix_scorm_scoes (scorm);');
44 modify_database('','CREATE INDEX prefix_scorm_sco_users_userid_idx ON prefix_scorm_sco_users (userid);');
45 modify_database('','CREATE INDEX prefix_scorm_sco_users_scormid_idx ON prefix_scorm_sco_users (scormid);');
46 modify_database('','CREATE INDEX prefix_scorm_sco_users_scoid_idx ON prefix_scorm_sco_users (scoid);');
49 if ($oldversion < 2005031300) {
50 table_column("scorm_scoes", "", "prerequisites", "VARCHAR", "200", "", "", "NOT NULL", "title");
51 table_column("scorm_scoes", "", "maxtimeallowed", "VARCHAR", "13", "", "", "NOT NULL", "prerequisites");
52 table_column("scorm_scoes", "", "timelimitaction", "VARCHAR", "19", "", "", "NOT NULL", "maxtimeallowed");
53 table_column("scorm_scoes", "", "masteryscore", "VARCHAR", "200", "", "", "NOT NULL", "datafromlms");
55 $oldscoes = get_records_select("scorm_scoes",null,"id ASC");
56 table_column("scorm_scoes", "type", "scormtype", "VARCHAR", "5", "", "", "NOT NULL");
57 if(!empty($oldscoes)) {
58 foreach ($oldscoes as $sco) {
59 $sco->scormtype = $sco->type;
60 unset($sco->type);
61 update_record("scorm_scoes",$sco);
65 execute_sql("CREATE TABLE {$CFG->prefix}scorm_scoes_track (
66 id SERIAL,
67 userid integer NOT NULL default '0',
68 scormid integer NOT NULL default '0',
69 scoid integer NOT NULL default '0',
70 element varchar(255) NOT NULL default '',
71 value text NOT NULL default '',
72 PRIMARY KEY (userid, scormid, scoid, element),
73 UNIQUE (userid, scormid, scoid, element)
74 );",true);
76 modify_database('','CREATE INDEX prefix_scorm_scoes_track_userdata_idx ON prefix_scorm_scoes_track (userid, scormid, scoid);');
78 $oldtracking = get_records_select('scorm_sco_users',null,'id ASC');
79 $oldelements = array ('cmi_core_lesson_location',
80 'cmi_core_lesson_status',
81 'cmi_core_exit',
82 'cmi_core_total_time',
83 'cmi_core_score_raw',
84 'cmi_suspend_data');
86 if(!empty($oldtrackings)) {
87 foreach ($oldtrackings as $oldtrack) {
88 $newtrack = '';
89 $newtrack->userid = $oldtrack->userid;
90 $newtrack->scormid = $oldtrack->scormid;
91 $newtrack->scoid = $oldtrack->scoid;
93 foreach ( $oldelements as $element) {
94 $newtrack->element = $element;
95 $newtrack->value = $oldtrack->$element;
96 if ($newtrack->value == NULL) {
97 $newtrack->value = '';
99 insert_record('scorm_scoes_track',$newtrack,false);
104 modify_database('',"DROP TABLE prefix_scorm_sco_users");
105 modify_database('',"INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('resource', 'review', 'resource', 'name')"); // Wrong line! See MDL-6516
108 if ($oldversion < 2005040200) {
109 execute_sql('ALTER TABLE '.$CFG->prefix.'scorm DROP popup'); // Old field
112 if ($oldversion < 2005040400) {
113 table_column('scorm_scoes', '', 'parameters', 'VARCHAR', '255', '', '', 'NOT NULL', 'launch');
116 if ($oldversion < 2005040700) {
117 //execute_sql('DROP PRIMARY KEY '.$CFG->prefix.'scorm_scoes_track_pkey;',false);
118 execute_sql('DROP INDEX '.$CFG->prefix.'scorm_scoes_track_userdata_idx;',false);
119 execute_sql('DROP INDEX '.$CFG->prefix.'scorm_scoes_track_userid_key;',false);
120 modify_database('','CREATE UNIQUE INDEX prefix_scorm_scoes_track_track_idx ON prefix_scorm_scoes_track (userid, scormid, scoid, element);');
121 execute_sql('ALTER TABLE '.$CFG->prefix.'scorm_scoes_track ADD PRIMARY KEY ("id");',false);
122 modify_database('','CREATE INDEX prefix_scorm_scoes_track_scormid_idx ON prefix_scorm_scoes_track (scormid);');
123 modify_database('','CREATE INDEX prefix_scorm_scoes_track_userid_idx ON prefix_scorm_scoes_track (userid);');
124 modify_database('','CREATE INDEX prefix_scorm_scoes_track_scoid_idx ON prefix_scorm_scoes_track (scoid);');
125 modify_database('','CREATE INDEX prefix_scorm_scoes_track_element_idx ON prefix_scorm_scoes_track (element);');
128 if ($oldversion < 2005041500) {
129 if ($scorms = get_records_select('scorm',null,'id ASC')) {
130 foreach ($scorms as $scorm) {
131 if (strlen($scorm->datadir) == 14) {
132 $basedir = $CFG->dataroot.'/'.$scorm->course;
133 $scormdir = '/moddata/scorm';
134 rename($basedir.$scormdir.$scorm->datadir,$basedir.$scormdir.'/'.$scorm->id);
138 execute_sql('ALTER TABLE '.$CFG->prefix.'scorm DROP datadir'); // Old field
141 if ($oldversion < 2005041600) {
142 table_column("scorm", "", "version", "VARCHAR", "9", "", "SCORM_1.2", "NOT NULL", "reference");
145 if ($oldversion < 2005042700) {
146 $trackingdata = get_records_select("scorm_scoes_track",null,"id ASC");
147 if (!empty($trackingdata)) {
148 $oldelements = array ('cmi_core_lesson_location',
149 'cmi_core_lesson_status',
150 'cmi_core_exit',
151 'cmi_core_total_time',
152 'cmi_core_score_raw',
153 'cmi_suspend_data');
154 $newelements = array ('cmi.core.lesson_location',
155 'cmi.core.lesson_status',
156 'cmi.core.exit',
157 'cmi.core.total_time',
158 'cmi.core.score.raw',
159 'cmi.suspend_data');
160 foreach ($trackingdata as $track) {
161 if (($pos = array_search($track->element,$oldelements)) !== false) {
162 $track->element = $newelements[$pos];
163 update_record('scorm_scoes_track',$track);
169 if ($oldversion < 2005042800) {
170 table_column("scorm", "", "browsemode", "integer", "", "", "1", "NOT NULL", "summary");
173 if ($oldversion < 2005050800) {
174 table_column("scorm", "", "width", "integer", "", "", "800", "NOT NULL", "auto");
175 table_column("scorm", "", "height", "integer", "", "", "600", "NOT NULL", "width");
178 if ($oldversion < 2005052200) {
179 table_column("scorm_scoes_track", "", "timemodified", "integer", "", "", "0", "NOT NULL", "value");
182 if ($oldversion < 2005052301) { // Mass cleanup of bad upgrade scripts
183 execute_sql("DROP INDEX {$CFG->prefix}scorm_scoes_track_scormid_idx", false);
184 execute_sql("DROP INDEX {$CFG->prefix}scorm_scoes_track_userid_idx", false);
185 execute_sql("DROP INDEX {$CFG->prefix}scorm_scoes_track_scoid_idx", false);
186 modify_database('','CREATE INDEX prefix_scorm_scoes_track_scorm_idx ON prefix_scorm_scoes_track (scormid);');
187 modify_database('','CREATE INDEX prefix_scorm_scoes_track_user_idx ON prefix_scorm_scoes_track (userid);');
188 modify_database('','CREATE INDEX prefix_scorm_scoes_track_sco_idx ON prefix_scorm_scoes_track (scoid);');
189 notify('The above errors can be ignored if the indexes already exists, its possible that it was cleaned up already before running this upgrade');
190 execute_sql("DROP INDEX {$CFG->prefix}scorm_scoes_track_track_idx", false);
191 modify_database('','ALTER TABLE ONLY prefix_scorm_scoes_track DROP CONSTRAINT prefix_scorm_scoes_track_pkey');
192 modify_database('','ALTER TABLE ONLY prefix_scorm_scoes_track ADD CONSTRAINT prefix_scorm_scoes_track_pkey PRIMARY KEY (id)');
193 modify_database('','ALTER TABLE ONLY prefix_scorm_scoes_track ADD UNIQUE (userid, scormid, scoid, element)');
194 modify_database('','ALTER TABLE prefix_scorm ALTER browsemode SET DEFAULT 0');
195 modify_database('','ALTER TABLE prefix_scorm DROP version');
196 notify('You can ignore the error above as version column does not exists, this will be properly upgraded later');
197 table_column('scorm_scoes','datafromlms','datafromlms','VARCHAR','255','','');
198 table_column('scorm_scoes','maxtimeallowed','maxtimeallowed','VARCHAR','19','','');
201 if ($oldversion < 2005062700) {
202 table_column("scorm", "", "popup", "integer", "", "", "0", "NOT NULL", "auto");
205 if ($oldversion < 2005070600) {
206 table_column("scorm", "", "hidetoc", "integer", "", "", "0", "NOT NULL", "browsemode");
207 $scorms = get_records_select("scorm","","id ASC");
208 table_column("scorm", "browsemode", "hidebrowse", "integer", "", "", "0", "NOT NULL", "");
209 if (!empty($scorms)) {
210 foreach($scorms as $scorm) {
211 if ($scorm->browsemode = 1) {
212 $scorm->hidebrowse = 0;
213 } else {
214 $scorm->hidebrowse = 1;
216 update_record('scorm',$scorm);
221 if ($oldversion < 2005092500) {
222 table_column("scorm", "", "hidenav", "integer", "", "", "0", "NOT NULL", "hidetoc");
223 table_column("scorm", "", "options", "varchar", "255", "", "", "NOT NULL","popup");
226 if ($oldversion < 2005092600) {
227 table_column("scorm_scoes_track", "", "attempt", "integer", "", "", "1", "NOT NULL", "scoid");
228 execute_sql("DROP INDEX {$CFG->prefix}scorm_scoes_track_scormid_idx", false);
229 modify_database('',"CREATE UNIQUE INDEX prefix_scorm_scoes_track_uk ON prefix_scorm_scoes_track(userid,scormid,scoid,attempt,element);");
232 if ($oldversion < 2005102800) {
233 table_column("scorm", "", "maxattempt", "integer", "", "UNSIGNED", "1", "NOT NULL", "maxgrade");
237 if ($oldversion < 2006021401) {
238 table_column("scorm", "", "version", "VARCHAR", "9", "", "", "NOT NULL", "reference");
240 modify_database('', 'ALTER TABLE prefix_scorm ALTER COLUMN width SET DEFAULT 100');
241 modify_database('', 'ALTER TABLE prefix_scorm ALTER COLUMN height SET DEFAULT 500');
242 // there is an extra constraint key1, we should drop this
243 modify_database('', 'ALTER TABLE prefix_scorm_scoes_track DROP CONSTRAINT prefix_scorm_scoes_track_userid_key1');
244 modify_database('', 'ALTER TABLE prefix_scorm_scoes_track DROP CONSTRAINT prefix_scorm_scoes_track_userid_key');
245 modify_database('', 'ALTER TABLE prefix_scorm_scoes_track ADD CONSTRAINT prefix_scorm_scoes_track_userid_key UNIQUE (userid, scormid, scoid, attempt, element)');
248 if ($oldversion < 2006102600) {
249 table_column("scorm", "", "skipview", "integer", "", "", "1", "NOT NULL");
252 if ($oldversion < 2006102702) { // This is a month in advance!!!!!
253 execute_sql("DELETE FROM {$CFG->prefix}log_display WHERE module = 'resource' AND action = 'review' AND mtable = 'resource' AND field = 'name';", false); // See MDL-6516
254 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('scorm', 'review', 'scorm', 'name');", false);
257 ////// DO NOT ADD NEW THINGS HERE!! USE upgrade.php and the lib/ddllib.php functions.
259 return true;