weekly version bump
[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);
d682eadf 39 $whatgradefixed = get_config('scorm', 'whatgradefixed');
b326c85a
DM
40 if (empty($whatgradefixed)) {
41 /// fix bad usage of whatgrade/grading method.
42 $scorms = $DB->get_records('scorm');
0744fded
PS
43 foreach ($scorms as $scorm) {
44 $scorm->whatgrade = $scorm->grademethod/10;
45 $DB->update_record('scorm', $scorm);
b326c85a 46 }
07b21d04 47 }
b326c85a
DM
48 } else {
49 //dump this config var as it isn't needed anymore.
50 unset_config('whatgradefixed', 'scorm');
c57ce9f2 51 }
9528568b 52
a4cdd6d2 53 upgrade_mod_savepoint(true, 2008073000, 'scorm');
c57ce9f2 54 }
9528568b 55
a4cdd6d2 56 if ($oldversion < 2008082500) {
9528568b 57
58 /// Define field scormtype to be added to scorm
59 $table = new xmldb_table('scorm');
2a88f626 60 $field = new xmldb_field('scormtype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, 'local', 'name');
9528568b 61
62 /// Launch add field scormtype
63 $dbman->add_field($table, $field);
64
65 /// scorm savepoint reached
a4cdd6d2 66 upgrade_mod_savepoint(true, 2008082500, 'scorm');
9528568b 67 }
68
a4cdd6d2 69 if ($oldversion < 2008090300) {
9528568b 70
71 /// Define field sha1hash to be added to scorm
72 $table = new xmldb_table('scorm');
2a88f626 73 $field = new xmldb_field('sha1hash', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'updatefreq');
9528568b 74
75 /// Launch add field sha1hash
76 $dbman->add_field($table, $field);
77
78 /// scorm savepoint reached
a4cdd6d2 79 upgrade_mod_savepoint(true, 2008090300, 'scorm');
9528568b 80 }
81
a4cdd6d2 82 if ($oldversion < 2008090301) {
9528568b 83
84 /// Define field revision to be added to scorm
85 $table = new xmldb_table('scorm');
2a88f626 86 $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'md5hash');
9528568b 87
88 /// Launch add field revision
89 $dbman->add_field($table, $field);
90
91 /// scorm savepoint reached
a4cdd6d2 92 upgrade_mod_savepoint(true, 2008090301, 'scorm');
9528568b 93 }
94
a4cdd6d2 95 if ($oldversion < 2008090302) {
9528568b 96 $sql = "UPDATE {scorm}
97 SET scormtype = 'external'
98 WHERE reference LIKE ? OR reference LIKE ? OR reference LIKE ?";
99 $DB->execute($sql, array('http://%imsmanifest.xml', 'https://%imsmanifest.xml', 'www.%imsmanifest.xml'));
100
101 $sql = "UPDATE {scorm}
102 SET scormtype = 'localsync'
103 WHERE reference LIKE ? OR reference LIKE ? OR reference LIKE ?
104 OR reference LIKE ? OR reference LIKE ? OR reference LIKE ?";
105 $DB->execute($sql, array('http://%.zip', 'https://%.zip', 'www.%.zip', 'http://%.pif', 'https://%.pif', 'www.%.pif'));
106
107 $sql = "UPDATE {scorm} SET scormtype = 'imsrepository' WHERE reference LIKE ?";
108 $DB->execute($sql, array('#%'));
109
110 /// scorm savepoint reached
a4cdd6d2 111 upgrade_mod_savepoint(true, 2008090302, 'scorm');
9528568b 112 }
113
a4cdd6d2 114 if ($oldversion < 2008090303) {
9528568b 115 //remove obsoleted config settings
116 unset_config('scorm_advancedsettings');
117 unset_config('scorm_windowsettings');
118
119 /// scorm savepoint reached
a4cdd6d2 120 upgrade_mod_savepoint(true, 2008090303, 'scorm');
9528568b 121 }
122
a4cdd6d2 123 if ($oldversion < 2008090304) {
9528568b 124 /////////////////////////////////////
125 /// new file storage upgrade code ///
126 /////////////////////////////////////
127
80cfa453 128 require_once("$CFG->dirroot/mod/scorm/db/upgradelib.php");
9528568b 129
130 $fs = get_file_storage();
131
132 $sqlfrom = "FROM {scorm} s
133 JOIN {modules} m ON m.name = 'scorm'
134 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = s.id)";
135
136 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
137
138 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")) {
139
140 $pbar = new progress_bar('migratescormfiles', 500, true);
141
9528568b 142 $i = 0;
143 foreach ($rs as $scorm) {
144 $i++;
145 upgrade_set_timeout(180); // set up timeout, may also abort execution
146 $pbar->update($i, $count, "Migrating scorm files - $i/$count.");
147
148 $context = get_context_instance(CONTEXT_MODULE, $scorm->cmid);
149 $coursecontext = get_context_instance(CONTEXT_COURSE, $scorm->course);
150
9528568b 151 if ($scorm->scormtype === 'local' and preg_match('/.*(\.zip|\.pif)$/i', $scorm->reference)) {
80cfa453 152 // first copy local packages if found - do not delete in case they are shared ;-)
64f93798
PS
153 $packagefile = clean_param($scorm->reference, PARAM_PATH);
154 $pathnamehash = sha1("/$coursecontext->id/course/legacy/0/$packagefile");
9528568b 155 if ($file = $fs->get_file_by_hash($pathnamehash)) {
80cfa453 156 $file_record = array('contextid'=>$context->id, 'component'=>'mod_scorm', 'filearea'=>'package',
9528568b 157 'itemid'=>0, 'filepath'=>'/');
84683aba
DM
158 try {
159 $fs->create_file_from_storedfile($file_record, $file);
160 } catch (Exception $x) {
80cfa453 161 // ignore any errors, we can not do much anyway
84683aba
DM
162 }
163 $scorm->reference = $file->get_filepath().$file->get_filename();
164
9528568b 165 } else {
166 $scorm->reference = '';
167 }
168 $DB->update_record('scorm', $scorm);
80cfa453
PS
169 // the package should be already extracted, we need to move the files there
170 // just in case somebody modified it directly there
171 scorm_migrate_moddata_files($scorm, $context);
9528568b 172
80cfa453
PS
173 } else if ($scorm->scormtype === 'local' and preg_match('/.*\/imsmanifest\.xml$/i', $scorm->reference)) {
174 // ignore imsmanifest in course root because we would be duplicating all course files which is not acceptable
175 // moddata dir is not used at all, ignore any rubbish there
176 $manifest = clean_param($scorm->reference, PARAM_PATH);
9528568b 177
80cfa453
PS
178 $pathnamehash = sha1("/$coursecontext->id/course/legacy/0/$manifest");
179 if ($file = $fs->get_file_by_hash($pathnamehash)) {
180 $scorm->reference = $file->get_filepath().$file->get_filename();
181
182 $manifestdir = '/'.str_ireplace('/imsmanifest.xml', '', $manifest).'/';
183 $pregmanifestdir = preg_quote($manifestdir, '/');
184 $file_record = array('contextid'=>$context->id, 'component'=>'mod_scorm', 'filearea'=>'content', 'itemid'=>0);
185 if ($files = $fs->get_directory_files($coursecontext->id, 'course', 'legacy', 0, $manifestdir, true)) {
186 foreach ($files as $file) {
187 $file_record['filepath'] = preg_replace("/^$pregmanifestdir/", '/', $file->get_filepath());
188 try {
189 $fs->create_file_from_storedfile($file_record, $file);
190 } catch (Exception $x) {
191 // ignore any errors, we can not do much anyway
192 }
193 }
194 }
195
196 } else {
197 $scorm->reference = '';
198 }
199 $DB->update_record('scorm', $scorm);
200
201 } else {
202 // just try to migrate anything from moddata
203 scorm_migrate_moddata_files($scorm, $context);
204 }
9528568b 205
206 // remove dirs if empty
207 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id/");
208 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/");
209 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/");
210 }
9528568b 211 $rs->close();
212 }
213
214 /// scorm savepoint reached
a4cdd6d2 215 upgrade_mod_savepoint(true, 2008090304, 'scorm');
9528568b 216 }
217
6381fa56 218
a4cdd6d2 219 if ($oldversion < 2008090305) {
6381fa56 220
221 /// Define new fields forcecompleted, forcenewattempt, displayattemptstatus, and displaycoursestructure to be added to scorm
222 $table = new xmldb_table('scorm');
2a88f626 223 $field = new xmldb_field('forcecompleted', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'maxattempt');
6381fa56 224 if (!$dbman->field_exists($table,$field)) {
225 $dbman->add_field($table, $field);
226 }
2a88f626 227 $field = new xmldb_field('forcenewattempt', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'forcecompleted');
6381fa56 228 if (!$dbman->field_exists($table,$field)) {
229 $dbman->add_field($table, $field);
230 }
2a88f626 231 $field = new xmldb_field('lastattemptlock', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'forcenewattempt');
6381fa56 232 if (!$dbman->field_exists($table,$field)) {
233 $dbman->add_field($table, $field);
234 }
2a88f626 235 $field = new xmldb_field('displayattemptstatus', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'lastattemptlock');
6381fa56 236 if (!$dbman->field_exists($table,$field)) {
237 $dbman->add_field($table, $field);
238 }
2a88f626 239 $field = new xmldb_field('displaycoursestructure', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'displayattemptstatus');
6381fa56 240 if (!$dbman->field_exists($table,$field)) {
241 $dbman->add_field($table, $field);
242 }
65478eea 243
6381fa56 244 /// scorm savepoint reached
a4cdd6d2 245 upgrade_mod_savepoint(true, 2008090305, 'scorm');
6381fa56 246 }
65478eea 247
30fc6e2d 248
249 // remove redundant config values
a4cdd6d2 250 if ($oldversion < 2008090306) {
65478eea 251 /*
252 * comment this out as it is handled by the update mark 2008090310 below
253 * left for historical documentation as some early adopters may have done
254 * this already.
30fc6e2d 255 $redundant_config = array(
256 'scorm_allowapidebug',
257 'scorm_allowtypeexternal',
258 'scorm_allowtypeimsrepository',
65478eea 259 'scorm_allowtypelocalsync',
260 'scorm_apidebugmask',
261 'scorm_frameheight',
30fc6e2d 262 'scorm_framewidth',
263 'scorm_maxattempts',
264 'scorm_updatetime');
265 foreach ($redundant_config as $rcfg) {
266 if (isset($CFG->$rcfg)) {
267 unset_config($rcfg);
268 }
269 }
65478eea 270 */
1adc77e6 271 /// scorm savepoint reached
a4cdd6d2 272 upgrade_mod_savepoint(true, 2008090306, 'scorm');
1adc77e6 273 }
65478eea 274
275
1adc77e6 276
277 // remove redundant config values
a4cdd6d2 278 if ($oldversion < 2008090307) {
65478eea 279 /*
280 * comment this out as it is handled by the update mark 2008090310 below
281 * left for historical documentation as some early adopters may have done
282 * this already.
1adc77e6 283 $redundant_config = array(
284 'scorm_allowapidebug',
285 'scorm_allowtypeexternal',
286 'scorm_allowtypeimsrepository',
65478eea 287 'scorm_allowtypelocalsync',
288 'scorm_apidebugmask',
289 'scorm_frameheight',
1adc77e6 290 'scorm_framewidth',
291 'scorm_maxattempts',
292 'scorm_updatetime',
65478eea 293 'scorm_resizable',
294 'scorm_scrollbars',
295 'scorm_directories',
1adc77e6 296 'scorm_location',
65478eea 297 'scorm_menubar',
298 'scorm_toolbar',
1adc77e6 299 'scorm_status',
300 'scorm_grademethod',
301 'scorm_maxgrade',
302 'scorm_whatgrade',
303 'scorm_popup',
304 'scorm_skipview',
305 'scorm_hidebrowse',
306 'scorm_hidetoc',
307 'scorm_hidenav',
308 'scorm_auto',
309 'scorm_updatefreq'
310 );
311 foreach ($redundant_config as $rcfg) {
312 if (isset($CFG->$rcfg)) {
313 unset_config($rcfg);
30fc6e2d 314 }
315 }
65478eea 316 */
317
30fc6e2d 318 /// scorm savepoint reached
a4cdd6d2 319 upgrade_mod_savepoint(true, 2008090307, 'scorm');
30fc6e2d 320 }
65478eea 321
a4cdd6d2 322 if ($oldversion < 2008090308) {
d54e2145 323 $table = new xmldb_table('scorm');
2a88f626 324 $field = new xmldb_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'height');
d54e2145 325 if (!$dbman->field_exists($table,$field)) {
326 $dbman->add_field($table, $field);
327 }
2a88f626 328 $field = new xmldb_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timeopen');
d54e2145 329 if (!$dbman->field_exists($table,$field)) {
330 $dbman->add_field($table, $field);
331 }
65478eea 332
d54e2145 333 /// scorm savepoint reached
a4cdd6d2 334 upgrade_mod_savepoint(true, 2008090308, 'scorm');
d54e2145 335 }
65478eea 336
337
a4cdd6d2 338 if ($oldversion < 2008090310) {
65478eea 339 // take above blocks that delete config and move the values in to config_plugins
340
341 $redundant_config = array(
342 'scorm_allowapidebug',
343 'scorm_allowtypeexternal',
344 'scorm_allowtypeimsrepository',
345 'scorm_allowtypelocalsync',
346 'scorm_apidebugmask',
347 'scorm_frameheight',
348 'scorm_framewidth',
349 'scorm_maxattempts',
350 'scorm_updatetime',
351 'scorm_resizable',
352 'scorm_scrollbars',
353 'scorm_directories',
354 'scorm_location',
355 'scorm_menubar',
356 'scorm_toolbar',
357 'scorm_status',
358 'scorm_grademethod',
359 'scorm_maxgrade',
360 'scorm_whatgrade',
361 'scorm_popup',
362 'scorm_skipview',
363 'scorm_hidebrowse',
364 'scorm_hidetoc',
365 'scorm_hidenav',
366 'scorm_auto',
367 'scorm_updatefreq',
368 'scorm_displayattemptstatus',
369 'scorm_displaycoursestructure',
370 'scorm_forcecompleted',
371 'scorm_forcenewattempt',
372 'scorm_lastattemptlock'
373 );
374
375 foreach ($redundant_config as $rcfg) {
376 if (isset($CFG->$rcfg)) {
377 $shortname = substr($rcfg, 6);
a4cdd6d2
PS
378 set_config($shortname, $CFG->$rcfg, 'scorm');
379 unset_config($rcfg);
65478eea 380 }
381 }
382
383 /// scorm savepoint reached
a4cdd6d2 384 upgrade_mod_savepoint(true, 2008090310, 'scorm');
65478eea 385 }
386
a4cdd6d2 387 if ($oldversion < 2009042000) {
d0bcf735 388
389 /// Rename field summary on table scorm to intro
390 $table = new xmldb_table('scorm');
2a88f626 391 $field = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, 'reference');
d0bcf735 392
393 /// Launch rename field summary
394 $dbman->rename_field($table, $field, 'intro');
395
396 /// scorm savepoint reached
a4cdd6d2 397 upgrade_mod_savepoint(true, 2009042000, 'scorm');
d0bcf735 398 }
399
a4cdd6d2 400 if ($oldversion < 2009042001) {
d0bcf735 401
402 /// Define field introformat to be added to scorm
403 $table = new xmldb_table('scorm');
2a88f626 404 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
d0bcf735 405
406 /// Launch add field introformat
d682eadf
PS
407 if (!$dbman->field_exists($table, $field)) {
408 $dbman->add_field($table, $field);
409 }
410
411 // conditionally migrate to html format in intro
412 if ($CFG->texteditors !== 'textarea') {
39ac3f6e 413 $rs = $DB->get_recordset('scorm', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
d682eadf
PS
414 foreach ($rs as $s) {
415 $s->intro = text_to_html($s->intro, false, false, true);
416 $s->introformat = FORMAT_HTML;
39ac3f6e 417 $DB->update_record('scorm', $s);
d682eadf
PS
418 upgrade_set_timeout();
419 }
420 $rs->close();
421 }
d0bcf735 422
423 /// scorm savepoint reached
a4cdd6d2 424 upgrade_mod_savepoint(true, 2009042001, 'scorm');
d0bcf735 425 }
426
a4cdd6d2 427 if ($oldversion < 2009042002) {
1bd51a6c
DM
428
429 /// Define field introformat to be added to scorm
430 $table = new xmldb_table('scorm_scoes');
431 $field = new xmldb_field('launch', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
432
433 /// Launch add field introformat
434 $dbman->change_field_type($table, $field);
435
436 /// scorm savepoint reached
a4cdd6d2 437 upgrade_mod_savepoint(true, 2009042002, 'scorm');
1bd51a6c 438 }
03751efe 439 if ($oldversion < 2010070800) {
b326c85a
DM
440 //check to see if this has already been tidied up by a 1.9 upgrade.
441 $grademethodfixed = get_config('scorm', 'grademethodfixed');
442 if (empty($grademethodfixed)) {
443 /// fix bad usage of whatgrade/grading method.
07b21d04 444 $scorms = $DB->get_records('scorm');
9d49a142
DM
445 if (!empty($scorm)) {
446 foreach ($scorms as $scorm) {
447 $scorm->grademethod = $scorm->grademethod%10;
448 $DB->update_record('scorm', $scorm);
449 }
07b21d04 450 }
b326c85a
DM
451 } else {
452 //dump this config var as it isn't needed anymore.
453 unset_config('grademethodfixed', 'scorm');
454 }
d682eadf 455
03751efe
DM
456 /// scorm savepoint reached
457 upgrade_mod_savepoint(true, 2010070800, 'scorm');
458 }
bea5ad66
DM
459 if ($oldversion < 2010092400) {
460 $count = $DB->count_records('scorm', array('scormtype'=>'external'));
461 if (!empty($count)) {
462 set_config('allowtypeexternal', '1', 'scorm');
463 }
464 $count = $DB->count_records('scorm', array('scormtype'=>'localsync'));
465 if (!empty($count)) {
466 set_config('allowtypelocalsync', '1', 'scorm');
467 }
468 $count = $DB->count_records('scorm', array('scormtype'=>'imsrepository'));
469 if (!empty($count)) {
470 set_config('allowtypeimsrepository', '1', 'scorm');
471 }
472 /// scorm savepoint reached
473 upgrade_mod_savepoint(true, 2010092400, 'scorm');
474 }
a4cdd6d2 475 return true;
b8a342d7 476}
477
e5dd8e3b 478