b8a342d7 |
1 | <?php //$Id$ |
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 |
12 | // your older installtion 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, |
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 |
23 | function xmldb_scorm_upgrade($oldversion) { |
24 | global $CFG, $DB; |
b8a342d7 |
25 | |
c57ce9f2 |
26 | $dbman = $DB->get_manager(); |
b8a342d7 |
27 | $result = true; |
28 | |
219f652b |
29 | //===== 1.9.0 upgrade line ======// |
9858605e |
30 | |
c57ce9f2 |
31 | // Adding missing 'whatgrade' field to table scorm |
32 | if ($result && $oldversion < 2008073000) { |
33 | $table = new xmldb_table('scorm'); |
34 | $field = new xmldb_field('whatgrade'); |
35 | $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'grademethod'); |
9528568b |
36 | |
c57ce9f2 |
37 | /// Launch add field whatgrade |
775f811a |
38 | if (!$dbman->field_exists($table,$field)) { |
c57ce9f2 |
39 | $dbman->add_field($table, $field); |
40 | } |
9528568b |
41 | |
c57ce9f2 |
42 | upgrade_mod_savepoint($result, 2008073000, 'scorm'); |
43 | } |
9528568b |
44 | |
45 | if ($result && $oldversion < 2008082500) { |
46 | |
47 | /// Define field scormtype to be added to scorm |
48 | $table = new xmldb_table('scorm'); |
49 | $field = new xmldb_field('scormtype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, null, 'local', 'name'); |
50 | |
51 | /// Launch add field scormtype |
52 | $dbman->add_field($table, $field); |
53 | |
54 | /// scorm savepoint reached |
55 | upgrade_mod_savepoint($result, 2008082500, 'scorm'); |
56 | } |
57 | |
58 | if ($result && $oldversion < 2008090300) { |
59 | |
60 | /// Define field sha1hash to be added to scorm |
61 | $table = new xmldb_table('scorm'); |
62 | $field = new xmldb_field('sha1hash', XMLDB_TYPE_CHAR, '40', null, null, null, null, null, null, 'updatefreq'); |
63 | |
64 | /// Launch add field sha1hash |
65 | $dbman->add_field($table, $field); |
66 | |
67 | /// scorm savepoint reached |
68 | upgrade_mod_savepoint($result, 2008090300, 'scorm'); |
69 | } |
70 | |
71 | if ($result && $oldversion < 2008090301) { |
72 | |
73 | /// Define field revision to be added to scorm |
74 | $table = new xmldb_table('scorm'); |
75 | $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'md5hash'); |
76 | |
77 | /// Launch add field revision |
78 | $dbman->add_field($table, $field); |
79 | |
80 | /// scorm savepoint reached |
81 | upgrade_mod_savepoint($result, 2008090301, 'scorm'); |
82 | } |
83 | |
84 | if ($result && $oldversion < 2008090302) { |
85 | $sql = "UPDATE {scorm} |
86 | SET scormtype = 'external' |
87 | WHERE reference LIKE ? OR reference LIKE ? OR reference LIKE ?"; |
88 | $DB->execute($sql, array('http://%imsmanifest.xml', 'https://%imsmanifest.xml', 'www.%imsmanifest.xml')); |
89 | |
90 | $sql = "UPDATE {scorm} |
91 | SET scormtype = 'localsync' |
92 | WHERE reference LIKE ? OR reference LIKE ? OR reference LIKE ? |
93 | OR reference LIKE ? OR reference LIKE ? OR reference LIKE ?"; |
94 | $DB->execute($sql, array('http://%.zip', 'https://%.zip', 'www.%.zip', 'http://%.pif', 'https://%.pif', 'www.%.pif')); |
95 | |
96 | $sql = "UPDATE {scorm} SET scormtype = 'imsrepository' WHERE reference LIKE ?"; |
97 | $DB->execute($sql, array('#%')); |
98 | |
99 | /// scorm savepoint reached |
100 | upgrade_mod_savepoint($result, 2008090302, 'scorm'); |
101 | } |
102 | |
103 | if ($result && $oldversion < 2008090303) { |
104 | //remove obsoleted config settings |
105 | unset_config('scorm_advancedsettings'); |
106 | unset_config('scorm_windowsettings'); |
107 | |
108 | /// scorm savepoint reached |
109 | upgrade_mod_savepoint($result, 2008090303, 'scorm'); |
110 | } |
111 | |
112 | if ($result && $oldversion < 2008090304) { |
113 | |
114 | ///////////////////////////////////// |
115 | /// new file storage upgrade code /// |
116 | ///////////////////////////////////// |
117 | |
118 | function scorm_migrate_content_files($context, $base, $path) { |
119 | global $CFG; |
120 | |
121 | $fullpathname = $base.$path; |
122 | $fs = get_file_storage(); |
123 | $filearea = 'scorm_content'; |
124 | $items = new DirectoryIterator($fullpathname); |
125 | |
126 | foreach ($items as $item) { |
127 | if ($item->isDot()) { |
128 | unset($item); // release file handle |
129 | continue; |
130 | } |
131 | |
132 | if ($item->isLink()) { |
133 | // do not follow symlinks - they were never supported in moddata, sorry |
134 | unset($item); // release file handle |
135 | continue; |
136 | } |
137 | |
138 | if ($item->isFile()) { |
139 | if (!$item->isReadable()) { |
140 | notify(" File not readable, skipping: ".$fullpathname.$item->getFilename()); |
141 | unset($item); // release file handle |
142 | continue; |
143 | } |
144 | |
145 | $filepath = clean_param($path, PARAM_PATH); |
146 | $filename = clean_param($item->getFilename(), PARAM_FILE); |
147 | $oldpathname = $fullpathname.$item->getFilename(); |
148 | |
149 | if ($filename === '') { |
150 | continue; |
151 | unset($item); // release file handle |
152 | } |
153 | |
154 | if (!$fs->file_exists($context->id, $filearea, '0', $filepath, $filename)) { |
155 | $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename, |
156 | 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime()); |
157 | unset($item); // release file handle |
158 | if ($fs->create_file_from_pathname($file_record, $oldpathname)) { |
159 | @unlink($oldpathname); |
160 | } |
161 | } else { |
162 | unset($item); // release file handle |
163 | } |
164 | |
165 | } else { |
166 | //migrate recursively all subdirectories |
167 | $oldpathname = $fullpathname.$item->getFilename().'/'; |
168 | $subpath = $path.$item->getFilename().'/'; |
169 | unset($item); // release file handle |
170 | scorm_migrate_content_files($context, $base, $subpath); |
171 | @rmdir($oldpathname); // deletes dir if empty |
172 | } |
173 | } |
174 | unset($items); //release file handles |
175 | } |
176 | |
177 | $fs = get_file_storage(); |
178 | |
179 | $sqlfrom = "FROM {scorm} s |
180 | JOIN {modules} m ON m.name = 'scorm' |
181 | JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = s.id)"; |
182 | |
183 | $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom"); |
184 | |
185 | 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")) { |
186 | |
187 | $pbar = new progress_bar('migratescormfiles', 500, true); |
188 | |
189 | $olddebug = $DB->get_debug(); |
190 | $DB->set_debug(false); // lower debug level, there might be many files |
191 | $i = 0; |
192 | foreach ($rs as $scorm) { |
193 | $i++; |
194 | upgrade_set_timeout(180); // set up timeout, may also abort execution |
195 | $pbar->update($i, $count, "Migrating scorm files - $i/$count."); |
196 | |
197 | $context = get_context_instance(CONTEXT_MODULE, $scorm->cmid); |
198 | $coursecontext = get_context_instance(CONTEXT_COURSE, $scorm->course); |
199 | |
200 | // first copy local packages if found - do not delete in case they are shared ;-) |
201 | if ($scorm->scormtype === 'local' and preg_match('/.*(\.zip|\.pif)$/i', $scorm->reference)) { |
202 | $packagefile = '/'.clean_param($scorm->reference, PARAM_PATH); |
203 | $pathnamehash = sha1($coursecontext->id.'course_content0'.$packagefile); |
204 | if ($file = $fs->get_file_by_hash($pathnamehash)) { |
205 | $file_record = array('scontextid'=>$context->id, 'filearea'=>'scorm_pacakge', |
206 | 'itemid'=>0, 'filepath'=>'/'); |
207 | $packagefile = $fs->create_file_from_storedfile($file_record, $file); |
208 | $scorm->reference = $packagefile->get_filename(); |
209 | } else { |
210 | $scorm->reference = ''; |
211 | } |
212 | $DB->update_record('scorm', $scorm); |
213 | } |
214 | |
215 | // now migrate the extracted package |
216 | $basepath = "$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id"; |
217 | if (!is_dir($basepath)) { |
218 | //no files? |
219 | continue; |
220 | } |
221 | |
222 | scorm_migrate_content_files($context, $basepath, '/'); |
223 | |
224 | // remove dirs if empty |
225 | @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id/"); |
226 | @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/"); |
227 | @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/"); |
228 | } |
229 | $DB->set_debug($olddebug); // reset debug level |
230 | $rs->close(); |
231 | } |
232 | |
233 | /// scorm savepoint reached |
234 | upgrade_mod_savepoint($result, 2008090304, 'scorm'); |
235 | } |
236 | |
b8a342d7 |
237 | return $result; |
238 | } |
239 | |
c57ce9f2 |
240 | ?> |