MDL-23351 - removed Moodle orange backgrounds from help and comments
[moodle.git] / mod / scorm / db / upgrade.php
1 <?php
3 // This file keeps track of upgrades to
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
12 // your older installation to the current version.
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,
18 // using the methods of database_manager class
19 //
20 // Please do not forget to use upgrade_set_timeout()
21 // before any action that may take longer time to finish.
23 function xmldb_scorm_upgrade($oldversion) {
24     global $CFG, $DB;
26     $dbman = $DB->get_manager();
28 //===== 1.9.0 upgrade line ======//
30     // Adding missing 'whatgrade' field to table scorm
31     if ($oldversion < 2008073000) {
32         $table = new xmldb_table('scorm');
33         $field = new xmldb_field('whatgrade');
34         $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'grademethod');
36         /// Launch add field whatgrade
37         if (!$dbman->field_exists($table,$field)) {
38             $dbman->add_field($table, $field);
39             $whatgradefixed = get_config('scorm', 'whatgradefixed');
40             if (empty($whatgradefixed)) {
41                 /// fix bad usage of whatgrade/grading method.
42                 $scorms = $DB->get_records('scorm');
43                 foreach ($scorms as $scorm) {
44                     $scorm->whatgrade = $scorm->grademethod/10;
45                     $DB->update_record('scorm', $scorm);
46                 }
47             }
48         } else {
49             //dump this config var as it isn't needed anymore.
50             unset_config('whatgradefixed', 'scorm');
51         }
53         upgrade_mod_savepoint(true, 2008073000, 'scorm');
54     }
56      if ($oldversion < 2008082500) {
58     /// Define field scormtype to be added to scorm
59         $table = new xmldb_table('scorm');
60         $field = new xmldb_field('scormtype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, 'local', 'name');
62     /// Launch add field scormtype
63         $dbman->add_field($table, $field);
65     /// scorm savepoint reached
66         upgrade_mod_savepoint(true, 2008082500, 'scorm');
67     }
69     if ($oldversion < 2008090300) {
71     /// Define field sha1hash to be added to scorm
72         $table = new xmldb_table('scorm');
73         $field = new xmldb_field('sha1hash', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'updatefreq');
75     /// Launch add field sha1hash
76         $dbman->add_field($table, $field);
78     /// scorm savepoint reached
79         upgrade_mod_savepoint(true, 2008090300, 'scorm');
80     }
82     if ($oldversion < 2008090301) {
84     /// Define field revision to be added to scorm
85         $table = new xmldb_table('scorm');
86         $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'md5hash');
88     /// Launch add field revision
89         $dbman->add_field($table, $field);
91     /// scorm savepoint reached
92         upgrade_mod_savepoint(true, 2008090301, 'scorm');
93     }
95     if ($oldversion < 2008090302) {
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'));
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'));
107         $sql = "UPDATE {scorm} SET scormtype = 'imsrepository' WHERE reference LIKE ?";
108         $DB->execute($sql, array('#%'));
110     /// scorm savepoint reached
111         upgrade_mod_savepoint(true, 2008090302, 'scorm');
112     }
114     if ($oldversion < 2008090303) {
115         //remove obsoleted config settings
116         unset_config('scorm_advancedsettings');
117         unset_config('scorm_windowsettings');
119     /// scorm savepoint reached
120         upgrade_mod_savepoint(true, 2008090303, 'scorm');
121     }
123     if ($oldversion < 2008090304) {
125         /////////////////////////////////////
126         /// new file storage upgrade code ///
127         /////////////////////////////////////
129         function scorm_migrate_content_files($context, $base, $path) {
130             global $CFG, $OUTPUT;
132             $fullpathname = $base.$path;
133             $fs           = get_file_storage();
134             $filearea     = 'content';
135             $items        = new DirectoryIterator($fullpathname);
137             foreach ($items as $item) {
138                 if ($item->isDot()) {
139                     unset($item); // release file handle
140                     continue;
141                 }
143                 if ($item->isLink()) {
144                     // do not follow symlinks - they were never supported in moddata, sorry
145                     unset($item); // release file handle
146                     continue;
147                 }
149                 if ($item->isFile()) {
150                     if (!$item->isReadable()) {
151                         echo $OUTPUT->notification(" File not readable, skipping: ".$fullpathname.$item->getFilename());
152                         unset($item); // release file handle
153                         continue;
154                     }
156                     $filepath    = clean_param($path, PARAM_PATH);
157                     $filename    = clean_param($item->getFilename(), PARAM_FILE);
158                     $oldpathname = $fullpathname.$item->getFilename();
160                     if ($filename === '') {
161                         continue;
162                         unset($item); // release file handle
163                     }
165                     if (!$fs->file_exists($context->id, 'mod_scorm', $filearea, '0', $filepath, $filename)) {
166                         $file_record = array('contextid'=>$context->id, 'component'=>'mod_scorm', 'filearea'=>$filearea, 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename,
167                                              'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
168                         unset($item); // release file handle
169                         if ($fs->create_file_from_pathname($file_record, $oldpathname)) {
170                             @unlink($oldpathname);
171                         }
172                     } else {
173                         unset($item); // release file handle
174                     }
176                 } else {
177                     //migrate recursively all subdirectories
178                     $oldpathname = $fullpathname.$item->getFilename().'/';
179                     $subpath     = $path.$item->getFilename().'/';
180                     unset($item);  // release file handle
181                     scorm_migrate_content_files($context, $base, $subpath);
182                     @rmdir($oldpathname); // deletes dir if empty
183                 }
184             }
185             unset($items); //release file handles
186         }
188         $fs = get_file_storage();
190         $sqlfrom = "FROM {scorm} s
191                     JOIN {modules} m ON m.name = 'scorm'
192                     JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = s.id)";
194         $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
196         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")) {
198             $pbar = new progress_bar('migratescormfiles', 500, true);
200             $i = 0;
201             foreach ($rs as $scorm) {
202                 $i++;
203                 upgrade_set_timeout(180); // set up timeout, may also abort execution
204                 $pbar->update($i, $count, "Migrating scorm files - $i/$count.");
206                 $context       = get_context_instance(CONTEXT_MODULE, $scorm->cmid);
207                 $coursecontext = get_context_instance(CONTEXT_COURSE, $scorm->course);
209                 // first copy local packages if found - do not delete in case they are shared ;-)
210                 if ($scorm->scormtype === 'local' and preg_match('/.*(\.zip|\.pif)$/i', $scorm->reference)) {
211                     $packagefile = clean_param($scorm->reference, PARAM_PATH);
212                     $pathnamehash = sha1("/$coursecontext->id/course/legacy/0/$packagefile");
213                     if ($file = $fs->get_file_by_hash($pathnamehash)) {
214                         $file_record = array('scontextid'=>$context->id, 'component'=>'mod_scorm', 'filearea'=>'package',
215                                              'itemid'=>0, 'filepath'=>'/');
216                         try {
217                             $fs->create_file_from_storedfile($file_record, $file);
218                         } catch (Exception $x) {
219                         }
220                         $scorm->reference = $file->get_filepath().$file->get_filename();
222                     } else {
223                         $scorm->reference = '';
224                     }
225                     $DB->update_record('scorm', $scorm);
226                 }
228                 // now migrate the extracted package
229                 $basepath = "$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id";
230                 if (!is_dir($basepath)) {
231                     //no files?
232                     continue;
233                 }
235                 scorm_migrate_content_files($context, $basepath, '/');
237                 // remove dirs if empty
238                 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id/");
239                 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/");
240                 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/");
241             }
242             $rs->close();
243         }
245     /// scorm savepoint reached
246         upgrade_mod_savepoint(true, 2008090304, 'scorm');
247     }
250     if ($oldversion < 2008090305) {
252     /// Define new fields forcecompleted, forcenewattempt, displayattemptstatus, and displaycoursestructure to be added to scorm
253         $table = new xmldb_table('scorm');
254         $field = new xmldb_field('forcecompleted', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'maxattempt');
255         if (!$dbman->field_exists($table,$field)) {
256             $dbman->add_field($table, $field);
257         }
258         $field = new xmldb_field('forcenewattempt', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'forcecompleted');
259         if (!$dbman->field_exists($table,$field)) {
260             $dbman->add_field($table, $field);
261         }
262         $field = new xmldb_field('lastattemptlock', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'forcenewattempt');
263         if (!$dbman->field_exists($table,$field)) {
264             $dbman->add_field($table, $field);
265         }
266         $field = new xmldb_field('displayattemptstatus', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'lastattemptlock');
267         if (!$dbman->field_exists($table,$field)) {
268             $dbman->add_field($table, $field);
269         }
270         $field = new xmldb_field('displaycoursestructure', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'displayattemptstatus');
271         if (!$dbman->field_exists($table,$field)) {
272             $dbman->add_field($table, $field);
273         }
275     /// scorm savepoint reached
276         upgrade_mod_savepoint(true, 2008090305, 'scorm');
277     }
280     // remove redundant config values
281     if ($oldversion < 2008090306) {
282         /*
283          * comment this out as it is handled by the update mark 2008090310 below
284          * left for historical documentation as some early adopters may have done
285          * this already.
286          $redundant_config = array(
287                          'scorm_allowapidebug',
288                          'scorm_allowtypeexternal',
289                          'scorm_allowtypeimsrepository',
290                          'scorm_allowtypelocalsync',
291                          'scorm_apidebugmask',
292                          'scorm_frameheight',
293                          'scorm_framewidth',
294                          'scorm_maxattempts',
295                          'scorm_updatetime');
296         foreach ($redundant_config as $rcfg) {
297             if (isset($CFG->$rcfg)) {
298                 unset_config($rcfg);
299             }
300         }
301          */
302         /// scorm savepoint reached
303         upgrade_mod_savepoint(true, 2008090306, 'scorm');
304     }
308     // remove redundant config values
309     if ($oldversion < 2008090307) {
310         /*
311          * comment this out as it is handled by the update mark 2008090310 below
312          * left for historical documentation as some early adopters may have done
313          * this already.
314          $redundant_config = array(
315                          'scorm_allowapidebug',
316                          'scorm_allowtypeexternal',
317                          'scorm_allowtypeimsrepository',
318                          'scorm_allowtypelocalsync',
319                          'scorm_apidebugmask',
320                          'scorm_frameheight',
321                          'scorm_framewidth',
322                          'scorm_maxattempts',
323                          'scorm_updatetime',
324                          'scorm_resizable',
325                          'scorm_scrollbars',
326                          'scorm_directories',
327                          'scorm_location',
328                          'scorm_menubar',
329                          'scorm_toolbar',
330                          'scorm_status',
331                          'scorm_grademethod',
332                          'scorm_maxgrade',
333                          'scorm_whatgrade',
334                          'scorm_popup',
335                          'scorm_skipview',
336                          'scorm_hidebrowse',
337                          'scorm_hidetoc',
338                          'scorm_hidenav',
339                          'scorm_auto',
340                          'scorm_updatefreq'
341          );
342         foreach ($redundant_config as $rcfg) {
343             if (isset($CFG->$rcfg)) {
344                 unset_config($rcfg);
345             }
346         }
347          */
349         /// scorm savepoint reached
350         upgrade_mod_savepoint(true, 2008090307, 'scorm');
351     }
353     if ($oldversion < 2008090308) {
354         $table = new xmldb_table('scorm');
355         $field = new xmldb_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'height');
356         if (!$dbman->field_exists($table,$field)) {
357             $dbman->add_field($table, $field);
358         }
359         $field = new xmldb_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timeopen');
360         if (!$dbman->field_exists($table,$field)) {
361             $dbman->add_field($table, $field);
362         }
364         /// scorm savepoint reached
365         upgrade_mod_savepoint(true, 2008090308, 'scorm');
366     }
369     if ($oldversion < 2008090310) {
370         // take above blocks that delete config and move the values in to config_plugins
372         $redundant_config = array(
373                          'scorm_allowapidebug',
374                          'scorm_allowtypeexternal',
375                          'scorm_allowtypeimsrepository',
376                          'scorm_allowtypelocalsync',
377                          'scorm_apidebugmask',
378                          'scorm_frameheight',
379                          'scorm_framewidth',
380                          'scorm_maxattempts',
381                          'scorm_updatetime',
382                          'scorm_resizable',
383                          'scorm_scrollbars',
384                          'scorm_directories',
385                          'scorm_location',
386                          'scorm_menubar',
387                          'scorm_toolbar',
388                          'scorm_status',
389                          'scorm_grademethod',
390                          'scorm_maxgrade',
391                          'scorm_whatgrade',
392                          'scorm_popup',
393                          'scorm_skipview',
394                          'scorm_hidebrowse',
395                          'scorm_hidetoc',
396                          'scorm_hidenav',
397                          'scorm_auto',
398                          'scorm_updatefreq',
399                          'scorm_displayattemptstatus',
400                          'scorm_displaycoursestructure',
401                          'scorm_forcecompleted',
402                          'scorm_forcenewattempt',
403                          'scorm_lastattemptlock'
404          );
406         foreach ($redundant_config as $rcfg) {
407             if (isset($CFG->$rcfg)) {
408                 $shortname = substr($rcfg, 6);
409                 set_config($shortname, $CFG->$rcfg, 'scorm');
410                 unset_config($rcfg);
411             }
412         }
414         /// scorm savepoint reached
415         upgrade_mod_savepoint(true, 2008090310, 'scorm');
416     }
418     if ($oldversion < 2009042000) {
420     /// Rename field summary on table scorm to intro
421         $table = new xmldb_table('scorm');
422         $field = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, 'reference');
424     /// Launch rename field summary
425         $dbman->rename_field($table, $field, 'intro');
427     /// scorm savepoint reached
428         upgrade_mod_savepoint(true, 2009042000, 'scorm');
429     }
431     if ($oldversion < 2009042001) {
433     /// Define field introformat to be added to scorm
434         $table = new xmldb_table('scorm');
435         $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
437     /// Launch add field introformat
438         if (!$dbman->field_exists($table, $field)) {
439             $dbman->add_field($table, $field);
440         }
442         // conditionally migrate to html format in intro
443         if ($CFG->texteditors !== 'textarea') {
444             $rs = $DB->get_recordset('scorm', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
445             foreach ($rs as $s) {
446                 $s->intro       = text_to_html($s->intro, false, false, true);
447                 $s->introformat = FORMAT_HTML;
448                 $DB->update_record('scorm', $s);
449                 upgrade_set_timeout();
450             }
451             $rs->close();
452         }
454     /// scorm savepoint reached
455         upgrade_mod_savepoint(true, 2009042001, 'scorm');
456     }
458     if ($oldversion < 2009042002) {
460     /// Define field introformat to be added to scorm
461         $table = new xmldb_table('scorm_scoes');
462         $field = new xmldb_field('launch', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
464     /// Launch add field introformat
465         $dbman->change_field_type($table, $field);
467     /// scorm savepoint reached
468         upgrade_mod_savepoint(true, 2009042002, 'scorm');
469     }
470     if ($oldversion < 2010070800) {
471     //check to see if this has already been tidied up by a 1.9 upgrade.
472         $grademethodfixed = get_config('scorm', 'grademethodfixed');
473         if (empty($grademethodfixed)) {
474             /// fix bad usage of whatgrade/grading method.
475             $scorms = $DB->get_records('scorm');
476             foreach ($scorms as $scorm) {
477                 $scorm->grademethod = $scorm->grademethod%10;
478                 $DB->update_record('scorm', $scorm);
479             }
480         } else {
481             //dump this config var as it isn't needed anymore.
482             unset_config('grademethodfixed', 'scorm');
483         }
485     /// scorm savepoint reached
486         upgrade_mod_savepoint(true, 2010070800, 'scorm');
487     }
488     return true;