Increase length of City field MDL-19629 - Taumata­whakatangihanga­koauau­o­tamatea...
[moodle.git] / mod / scorm / db / upgrade.php
CommitLineData
e5dd8e3b 1<?php
b8a342d7 2
9528568b 3// This file keeps track of upgrades to
b8a342d7 4// the scorm module
5//
6// Sometimes, changes between versions involve
7// alterations to database structures and other
8// major things that may break installations.
9//
10// The upgrade function in this file will attempt
11// to perform all the necessary actions to upgrade
2e0406a5 12// your older installation to the current version.
b8a342d7 13//
14// If there's something it cannot do itself, it
15// will tell you what you need to do.
16//
17// The commands in here will all be database-neutral,
b1f93b15 18// using the methods of database_manager class
775f811a 19//
20// Please do not forget to use upgrade_set_timeout()
21// before any action that may take longer time to finish.
b8a342d7 22
775f811a 23function xmldb_scorm_upgrade($oldversion) {
24 global $CFG, $DB;
b8a342d7 25
c57ce9f2 26 $dbman = $DB->get_manager();
b8a342d7 27
219f652b 28//===== 1.9.0 upgrade line ======//
9858605e 29
c57ce9f2 30 // Adding missing 'whatgrade' field to table scorm
a4cdd6d2 31 if ($oldversion < 2008073000) {
c57ce9f2 32 $table = new xmldb_table('scorm');
33 $field = new xmldb_field('whatgrade');
2a88f626 34 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'grademethod');
9528568b 35
c57ce9f2 36 /// Launch add field whatgrade
775f811a 37 if (!$dbman->field_exists($table,$field)) {
c57ce9f2 38 $dbman->add_field($table, $field);
07b21d04
DM
39 /// fix bad usage of whatgrade/grading method.
40 $scorms = $DB->get_records('scorm');
41 foreach ($scorms as $scorm) {
42 $scorm->whatgrade = $scorm->grademethod/10;
43 $DB->update_record('scorm', $scorm);
44 }
c57ce9f2 45 }
9528568b 46
a4cdd6d2 47 upgrade_mod_savepoint(true, 2008073000, 'scorm');
c57ce9f2 48 }
9528568b 49
a4cdd6d2 50 if ($oldversion < 2008082500) {
9528568b 51
52 /// Define field scormtype to be added to scorm
53 $table = new xmldb_table('scorm');
2a88f626 54 $field = new xmldb_field('scormtype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, 'local', 'name');
9528568b 55
56 /// Launch add field scormtype
57 $dbman->add_field($table, $field);
58
59 /// scorm savepoint reached
a4cdd6d2 60 upgrade_mod_savepoint(true, 2008082500, 'scorm');
9528568b 61 }
62
a4cdd6d2 63 if ($oldversion < 2008090300) {
9528568b 64
65 /// Define field sha1hash to be added to scorm
66 $table = new xmldb_table('scorm');
2a88f626 67 $field = new xmldb_field('sha1hash', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'updatefreq');
9528568b 68
69 /// Launch add field sha1hash
70 $dbman->add_field($table, $field);
71
72 /// scorm savepoint reached
a4cdd6d2 73 upgrade_mod_savepoint(true, 2008090300, 'scorm');
9528568b 74 }
75
a4cdd6d2 76 if ($oldversion < 2008090301) {
9528568b 77
78 /// Define field revision to be added to scorm
79 $table = new xmldb_table('scorm');
2a88f626 80 $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'md5hash');
9528568b 81
82 /// Launch add field revision
83 $dbman->add_field($table, $field);
84
85 /// scorm savepoint reached
a4cdd6d2 86 upgrade_mod_savepoint(true, 2008090301, 'scorm');
9528568b 87 }
88
a4cdd6d2 89 if ($oldversion < 2008090302) {
9528568b 90 $sql = "UPDATE {scorm}
91 SET scormtype = 'external'
92 WHERE reference LIKE ? OR reference LIKE ? OR reference LIKE ?";
93 $DB->execute($sql, array('http://%imsmanifest.xml', 'https://%imsmanifest.xml', 'www.%imsmanifest.xml'));
94
95 $sql = "UPDATE {scorm}
96 SET scormtype = 'localsync'
97 WHERE reference LIKE ? OR reference LIKE ? OR reference LIKE ?
98 OR reference LIKE ? OR reference LIKE ? OR reference LIKE ?";
99 $DB->execute($sql, array('http://%.zip', 'https://%.zip', 'www.%.zip', 'http://%.pif', 'https://%.pif', 'www.%.pif'));
100
101 $sql = "UPDATE {scorm} SET scormtype = 'imsrepository' WHERE reference LIKE ?";
102 $DB->execute($sql, array('#%'));
103
104 /// scorm savepoint reached
a4cdd6d2 105 upgrade_mod_savepoint(true, 2008090302, 'scorm');
9528568b 106 }
107
a4cdd6d2 108 if ($oldversion < 2008090303) {
9528568b 109 //remove obsoleted config settings
110 unset_config('scorm_advancedsettings');
111 unset_config('scorm_windowsettings');
112
113 /// scorm savepoint reached
a4cdd6d2 114 upgrade_mod_savepoint(true, 2008090303, 'scorm');
9528568b 115 }
116
a4cdd6d2 117 if ($oldversion < 2008090304) {
9528568b 118
119 /////////////////////////////////////
120 /// new file storage upgrade code ///
121 /////////////////////////////////////
122
123 function scorm_migrate_content_files($context, $base, $path) {
6aff538a 124 global $CFG, $OUTPUT;
9528568b 125
126 $fullpathname = $base.$path;
127 $fs = get_file_storage();
64f93798 128 $filearea = 'content';
9528568b 129 $items = new DirectoryIterator($fullpathname);
130
131 foreach ($items as $item) {
132 if ($item->isDot()) {
133 unset($item); // release file handle
134 continue;
135 }
136
137 if ($item->isLink()) {
138 // do not follow symlinks - they were never supported in moddata, sorry
139 unset($item); // release file handle
140 continue;
141 }
142
143 if ($item->isFile()) {
144 if (!$item->isReadable()) {
6aff538a 145 echo $OUTPUT->notification(" File not readable, skipping: ".$fullpathname.$item->getFilename());
9528568b 146 unset($item); // release file handle
147 continue;
148 }
149
150 $filepath = clean_param($path, PARAM_PATH);
151 $filename = clean_param($item->getFilename(), PARAM_FILE);
152 $oldpathname = $fullpathname.$item->getFilename();
153
154 if ($filename === '') {
155 continue;
156 unset($item); // release file handle
157 }
158
64f93798
PS
159 if (!$fs->file_exists($context->id, 'mod_scorm', $filearea, '0', $filepath, $filename)) {
160 $file_record = array('contextid'=>$context->id, 'component'=>'mod_scorm', 'filearea'=>$filearea, 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename,
9528568b 161 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
162 unset($item); // release file handle
163 if ($fs->create_file_from_pathname($file_record, $oldpathname)) {
164 @unlink($oldpathname);
165 }
166 } else {
167 unset($item); // release file handle
168 }
169
170 } else {
171 //migrate recursively all subdirectories
172 $oldpathname = $fullpathname.$item->getFilename().'/';
173 $subpath = $path.$item->getFilename().'/';
174 unset($item); // release file handle
175 scorm_migrate_content_files($context, $base, $subpath);
176 @rmdir($oldpathname); // deletes dir if empty
177 }
178 }
179 unset($items); //release file handles
180 }
181
182 $fs = get_file_storage();
183
184 $sqlfrom = "FROM {scorm} s
185 JOIN {modules} m ON m.name = 'scorm'
186 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = s.id)";
187
188 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
189
190 if ($rs = $DB->get_recordset_sql("SELECT s.id, s.scormtype, s.reference, s.course, cm.id AS cmid $sqlfrom ORDER BY s.course, s.id")) {
191
192 $pbar = new progress_bar('migratescormfiles', 500, true);
193
9528568b 194 $i = 0;
195 foreach ($rs as $scorm) {
196 $i++;
197 upgrade_set_timeout(180); // set up timeout, may also abort execution
198 $pbar->update($i, $count, "Migrating scorm files - $i/$count.");
199
200 $context = get_context_instance(CONTEXT_MODULE, $scorm->cmid);
201 $coursecontext = get_context_instance(CONTEXT_COURSE, $scorm->course);
202
203 // first copy local packages if found - do not delete in case they are shared ;-)
204 if ($scorm->scormtype === 'local' and preg_match('/.*(\.zip|\.pif)$/i', $scorm->reference)) {
64f93798
PS
205 $packagefile = clean_param($scorm->reference, PARAM_PATH);
206 $pathnamehash = sha1("/$coursecontext->id/course/legacy/0/$packagefile");
9528568b 207 if ($file = $fs->get_file_by_hash($pathnamehash)) {
64f93798 208 $file_record = array('scontextid'=>$context->id, 'component'=>'mod_scorm', 'filearea'=>'package',
9528568b 209 'itemid'=>0, 'filepath'=>'/');
84683aba
DM
210 try {
211 $fs->create_file_from_storedfile($file_record, $file);
212 } catch (Exception $x) {
213 }
214 $scorm->reference = $file->get_filepath().$file->get_filename();
215
9528568b 216 } else {
217 $scorm->reference = '';
218 }
219 $DB->update_record('scorm', $scorm);
220 }
221
222 // now migrate the extracted package
223 $basepath = "$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id";
224 if (!is_dir($basepath)) {
225 //no files?
226 continue;
227 }
228
229 scorm_migrate_content_files($context, $basepath, '/');
230
231 // remove dirs if empty
232 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id/");
233 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/");
234 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/");
235 }
9528568b 236 $rs->close();
237 }
238
239 /// scorm savepoint reached
a4cdd6d2 240 upgrade_mod_savepoint(true, 2008090304, 'scorm');
9528568b 241 }
242
6381fa56 243
a4cdd6d2 244 if ($oldversion < 2008090305) {
6381fa56 245
246 /// Define new fields forcecompleted, forcenewattempt, displayattemptstatus, and displaycoursestructure to be added to scorm
247 $table = new xmldb_table('scorm');
2a88f626 248 $field = new xmldb_field('forcecompleted', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'maxattempt');
6381fa56 249 if (!$dbman->field_exists($table,$field)) {
250 $dbman->add_field($table, $field);
251 }
2a88f626 252 $field = new xmldb_field('forcenewattempt', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'forcecompleted');
6381fa56 253 if (!$dbman->field_exists($table,$field)) {
254 $dbman->add_field($table, $field);
255 }
2a88f626 256 $field = new xmldb_field('lastattemptlock', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'forcenewattempt');
6381fa56 257 if (!$dbman->field_exists($table,$field)) {
258 $dbman->add_field($table, $field);
259 }
2a88f626 260 $field = new xmldb_field('displayattemptstatus', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'lastattemptlock');
6381fa56 261 if (!$dbman->field_exists($table,$field)) {
262 $dbman->add_field($table, $field);
263 }
2a88f626 264 $field = new xmldb_field('displaycoursestructure', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'displayattemptstatus');
6381fa56 265 if (!$dbman->field_exists($table,$field)) {
266 $dbman->add_field($table, $field);
267 }
65478eea 268
6381fa56 269 /// scorm savepoint reached
a4cdd6d2 270 upgrade_mod_savepoint(true, 2008090305, 'scorm');
6381fa56 271 }
65478eea 272
30fc6e2d 273
274 // remove redundant config values
a4cdd6d2 275 if ($oldversion < 2008090306) {
65478eea 276 /*
277 * comment this out as it is handled by the update mark 2008090310 below
278 * left for historical documentation as some early adopters may have done
279 * this already.
30fc6e2d 280 $redundant_config = array(
281 'scorm_allowapidebug',
282 'scorm_allowtypeexternal',
283 'scorm_allowtypeimsrepository',
65478eea 284 'scorm_allowtypelocalsync',
285 'scorm_apidebugmask',
286 'scorm_frameheight',
30fc6e2d 287 'scorm_framewidth',
288 'scorm_maxattempts',
289 'scorm_updatetime');
290 foreach ($redundant_config as $rcfg) {
291 if (isset($CFG->$rcfg)) {
292 unset_config($rcfg);
293 }
294 }
65478eea 295 */
1adc77e6 296 /// scorm savepoint reached
a4cdd6d2 297 upgrade_mod_savepoint(true, 2008090306, 'scorm');
1adc77e6 298 }
65478eea 299
300
1adc77e6 301
302 // remove redundant config values
a4cdd6d2 303 if ($oldversion < 2008090307) {
65478eea 304 /*
305 * comment this out as it is handled by the update mark 2008090310 below
306 * left for historical documentation as some early adopters may have done
307 * this already.
1adc77e6 308 $redundant_config = array(
309 'scorm_allowapidebug',
310 'scorm_allowtypeexternal',
311 'scorm_allowtypeimsrepository',
65478eea 312 'scorm_allowtypelocalsync',
313 'scorm_apidebugmask',
314 'scorm_frameheight',
1adc77e6 315 'scorm_framewidth',
316 'scorm_maxattempts',
317 'scorm_updatetime',
65478eea 318 'scorm_resizable',
319 'scorm_scrollbars',
320 'scorm_directories',
1adc77e6 321 'scorm_location',
65478eea 322 'scorm_menubar',
323 'scorm_toolbar',
1adc77e6 324 'scorm_status',
325 'scorm_grademethod',
326 'scorm_maxgrade',
327 'scorm_whatgrade',
328 'scorm_popup',
329 'scorm_skipview',
330 'scorm_hidebrowse',
331 'scorm_hidetoc',
332 'scorm_hidenav',
333 'scorm_auto',
334 'scorm_updatefreq'
335 );
336 foreach ($redundant_config as $rcfg) {
337 if (isset($CFG->$rcfg)) {
338 unset_config($rcfg);
30fc6e2d 339 }
340 }
65478eea 341 */
342
30fc6e2d 343 /// scorm savepoint reached
a4cdd6d2 344 upgrade_mod_savepoint(true, 2008090307, 'scorm');
30fc6e2d 345 }
65478eea 346
a4cdd6d2 347 if ($oldversion < 2008090308) {
d54e2145 348 $table = new xmldb_table('scorm');
2a88f626 349 $field = new xmldb_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'height');
d54e2145 350 if (!$dbman->field_exists($table,$field)) {
351 $dbman->add_field($table, $field);
352 }
2a88f626 353 $field = new xmldb_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timeopen');
d54e2145 354 if (!$dbman->field_exists($table,$field)) {
355 $dbman->add_field($table, $field);
356 }
65478eea 357
d54e2145 358 /// scorm savepoint reached
a4cdd6d2 359 upgrade_mod_savepoint(true, 2008090308, 'scorm');
d54e2145 360 }
65478eea 361
362
a4cdd6d2 363 if ($oldversion < 2008090310) {
65478eea 364 // take above blocks that delete config and move the values in to config_plugins
365
366 $redundant_config = array(
367 'scorm_allowapidebug',
368 'scorm_allowtypeexternal',
369 'scorm_allowtypeimsrepository',
370 'scorm_allowtypelocalsync',
371 'scorm_apidebugmask',
372 'scorm_frameheight',
373 'scorm_framewidth',
374 'scorm_maxattempts',
375 'scorm_updatetime',
376 'scorm_resizable',
377 'scorm_scrollbars',
378 'scorm_directories',
379 'scorm_location',
380 'scorm_menubar',
381 'scorm_toolbar',
382 'scorm_status',
383 'scorm_grademethod',
384 'scorm_maxgrade',
385 'scorm_whatgrade',
386 'scorm_popup',
387 'scorm_skipview',
388 'scorm_hidebrowse',
389 'scorm_hidetoc',
390 'scorm_hidenav',
391 'scorm_auto',
392 'scorm_updatefreq',
393 'scorm_displayattemptstatus',
394 'scorm_displaycoursestructure',
395 'scorm_forcecompleted',
396 'scorm_forcenewattempt',
397 'scorm_lastattemptlock'
398 );
399
400 foreach ($redundant_config as $rcfg) {
401 if (isset($CFG->$rcfg)) {
402 $shortname = substr($rcfg, 6);
a4cdd6d2
PS
403 set_config($shortname, $CFG->$rcfg, 'scorm');
404 unset_config($rcfg);
65478eea 405 }
406 }
407
408 /// scorm savepoint reached
a4cdd6d2 409 upgrade_mod_savepoint(true, 2008090310, 'scorm');
65478eea 410 }
411
a4cdd6d2 412 if ($oldversion < 2009042000) {
d0bcf735 413
414 /// Rename field summary on table scorm to intro
415 $table = new xmldb_table('scorm');
2a88f626 416 $field = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, 'reference');
d0bcf735 417
418 /// Launch rename field summary
419 $dbman->rename_field($table, $field, 'intro');
420
421 /// scorm savepoint reached
a4cdd6d2 422 upgrade_mod_savepoint(true, 2009042000, 'scorm');
d0bcf735 423 }
424
a4cdd6d2 425 if ($oldversion < 2009042001) {
d0bcf735 426
427 /// Define field introformat to be added to scorm
428 $table = new xmldb_table('scorm');
2a88f626 429 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
d0bcf735 430
431 /// Launch add field introformat
432 $dbman->add_field($table, $field);
433
434 /// scorm savepoint reached
a4cdd6d2 435 upgrade_mod_savepoint(true, 2009042001, 'scorm');
d0bcf735 436 }
437
a4cdd6d2 438 if ($oldversion < 2009042002) {
1bd51a6c
DM
439
440 /// Define field introformat to be added to scorm
441 $table = new xmldb_table('scorm_scoes');
442 $field = new xmldb_field('launch', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
443
444 /// Launch add field introformat
445 $dbman->change_field_type($table, $field);
446
447 /// scorm savepoint reached
a4cdd6d2 448 upgrade_mod_savepoint(true, 2009042002, 'scorm');
1bd51a6c 449 }
03751efe
DM
450 if ($oldversion < 2010070800) {
451 /// fix bad usage of whatgrade/grading method. - I hope this works in all dbs
07b21d04
DM
452 $scorms = $DB->get_records('scorm');
453 foreach ($scorms as $scorm) {
454 $scorm->grademethod = $scorm->grademethod%10;
455 $DB->update_record('scorm', $scorm);
456 }
03751efe
DM
457 /// scorm savepoint reached
458 upgrade_mod_savepoint(true, 2010070800, 'scorm');
459 }
a4cdd6d2 460 return true;
b8a342d7 461}
462
e5dd8e3b 463