we usually use include instead of require for plugin files
[moodle.git] / mod / glossary / db / upgrade.php
CommitLineData
86342d63 1<?php
ecfeb03a 2
cbc2b5df 3// This file keeps track of upgrades to
ecfeb03a 4// the glossary 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.
ecfeb03a 22
775f811a 23function xmldb_glossary_upgrade($oldversion) {
ee9e63b5 24 global $CFG, $DB, $OUTPUT;
ecfeb03a 25
775f811a 26 $dbman = $DB->get_manager();
ecfeb03a 27 $result = true;
28
219f652b 29//===== 1.9.0 upgrade line ======//
ecfeb03a 30
6b04ddc5 31 if ($result && $oldversion < 2008081900) {
49bcd737 32
33 /////////////////////////////////////
34 /// new file storage upgrade code ///
35 /////////////////////////////////////
36
37 $fs = get_file_storage();
38
39 $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
40
41 $sqlfrom = "FROM {glossary_entries} ge
42 JOIN {glossary} g ON g.id = ge.glossaryid
43 JOIN {modules} m ON m.name = 'glossary'
44 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = g.id)
c48068f8 45 WHERE ge.attachment <> '$empty' AND ge.attachment <> '1'";
49bcd737 46
47 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
48
c48068f8 49 if ($rs = $DB->get_recordset_sql("SELECT ge.id, ge.userid, ge.attachment, ge.glossaryid, ge.sourceglossaryid, g.course, cm.id AS cmid $sqlfrom ORDER BY g.course, g.id")) {
49bcd737 50
51 $pbar = new progress_bar('migrateglossaryfiles', 500, true);
52
49bcd737 53 $i = 0;
54 foreach ($rs as $entry) {
55 $i++;
56 upgrade_set_timeout(60); // set up timeout, may also abort execution
57 $pbar->update($i, $count, "Migrating glossary entries - $i/$count.");
58
59 $filepath = "$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id/$entry->attachment";
0d18a9d6 60 if ($entry->sourceglossaryid and !is_readable($filepath)) {
49bcd737 61 //eh - try the second possible location
49bcd737 62 $filepath = "$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->sourceglossaryid/$entry->id/$entry->attachment";
cbc2b5df 63
49bcd737 64 }
65 if (!is_readable($filepath)) {
66 //file missing??
ee9e63b5 67 echo $OUTPUT->notification("File not readable, skipping: $filepath");
49bcd737 68 $entry->attachment = '';
69 $DB->update_record('glossary_entries', $entry);
70 continue;
71 }
72 $context = get_context_instance(CONTEXT_MODULE, $entry->cmid);
73
64f93798 74 $filearea = 'attachment';
49bcd737 75 $filename = clean_param($entry->attachment, PARAM_FILE);
76 if ($filename === '') {
ee9e63b5 77 echo $OUTPUT->notification("Unsupported entry filename, skipping: ".$filepath);
49bcd737 78 $entry->attachment = '';
79 $DB->update_record('glossary_entries', $entry);
80 continue;
81 }
64f93798
PS
82 if (!$fs->file_exists($context->id, 'mod_glossary', $filearea, $entry->id, '/', $filename)) {
83 $file_record = array('contextid'=>$context->id, 'component'=>'mod_glossary', 'filearea'=>$filearea, 'itemid'=>$entry->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$entry->userid);
49bcd737 84 if ($fs->create_file_from_pathname($file_record, $filepath)) {
85 $entry->attachment = '1';
86 if ($DB->update_record('glossary_entries', $entry)) {
87 unlink($filepath);
88 }
89 }
90 }
91
92 // remove dirs if empty
93 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id");
94 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid");
95 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary");
96 }
49bcd737 97 $rs->close();
98 }
99
6b04ddc5 100 upgrade_mod_savepoint($result, 2008081900, 'glossary');
49bcd737 101 }
102
cbc2b5df 103 if ($result && $oldversion < 2009042000) {
104
2a88f626 105 /// Rename field definitionformat on table glossary_entries to definitionformat
cbc2b5df 106 $table = new xmldb_table('glossary_entries');
2a88f626 107 $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'definition');
cbc2b5df 108
109 /// Launch rename field definitionformat
110 $dbman->rename_field($table, $field, 'definitionformat');
111
112 /// glossary savepoint reached
113 upgrade_mod_savepoint($result, 2009042000, 'glossary');
114 }
115
116 if ($result && $oldversion < 2009042001) {
117
118 /// Define field definitiontrust to be added to glossary_entries
119 $table = new xmldb_table('glossary_entries');
2a88f626 120 $field = new xmldb_field('definitiontrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'definitionformat');
cbc2b5df 121
122 /// Launch add field definitiontrust
123 $dbman->add_field($table, $field);
124
125 /// glossary savepoint reached
126 upgrade_mod_savepoint($result, 2009042001, 'glossary');
127 }
128
129 if ($result && $oldversion < 2009042002) {
130
131 /// Rename field format on table glossary_comments to NEWNAMEGOESHERE
132 $table = new xmldb_table('glossary_comments');
2a88f626 133 $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'entrycomment');
cbc2b5df 134
135 /// Launch rename field format
136 $dbman->rename_field($table, $field, 'entrycommentformat');
137
138 /// glossary savepoint reached
139 upgrade_mod_savepoint($result, 2009042002, 'glossary');
140 }
141
142 if ($result && $oldversion < 2009042003) {
143
144 /// Define field entrycommenttrust to be added to glossary_comments
145 $table = new xmldb_table('glossary_comments');
2a88f626 146 $field = new xmldb_field('entrycommenttrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'entrycommentformat');
cbc2b5df 147
148 /// Conditionally launch add field entrycommenttrust
149 if (!$dbman->field_exists($table, $field)) {
150 $dbman->add_field($table, $field);
151 }
152
153 /// glossary savepoint reached
154 upgrade_mod_savepoint($result, 2009042003, 'glossary');
155 }
156
157 if ($result && $oldversion < 2009042004) {
158 $trustmark = '#####TRUSTTEXT#####';
159 $rs = $DB->get_recordset_sql("SELECT * FROM {glossary_entries} WHERE definition LIKE '$trustmark%'");
160 foreach ($rs as $entry) {
161 if (strpos($entry->definition, $trustmark) !== 0) {
162 // probably lowercase in some DBs
163 continue;
164 }
165 $entry->definition = trusttext_strip($entry->definition);
166 $entry->definitiontrust = 1;
167 $DB->update_record('glossary_entries', $entry);
168 }
169 $rs->close();
170
171 /// glossary savepoint reached
172 upgrade_mod_savepoint($result, 2009042004, 'glossary');
173 }
174
175 if ($result && $oldversion < 2009042005) {
176 $trustmark = '#####TRUSTTEXT#####';
177 $rs = $DB->get_recordset_sql("SELECT * FROM {glossary_comments} WHERE entrycomment LIKE '$trustmark%'");
178 foreach ($rs as $comment) {
179 if (strpos($comment->entrycomment, $trustmark) !== 0) {
180 // probably lowercase in some DBs
181 continue;
182 }
183 $comment->entrycomment = trusttext_strip($comment->entrycomment);
184 $comment->entrycommenttrust = 1;
185 $DB->update_record('glossary_comments', $comment);
186 }
187 $rs->close();
188
189 /// glossary savepoint reached
190 upgrade_mod_savepoint($result, 2009042005, 'glossary');
191 }
192
6507d1a9 193 if ($result && $oldversion < 2009042006) {
194
195 /// Define field introformat to be added to glossary
196 $table = new xmldb_table('glossary');
2a88f626 197 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
6507d1a9 198
199 /// Conditionally launch add field introformat
200 if (!$dbman->field_exists($table, $field)) {
201 $dbman->add_field($table, $field);
202 }
203
204 /// glossary savepoint reached
205 upgrade_mod_savepoint($result, 2009042006, 'glossary');
206 }
c8092ea5 207 if ($result && $oldversion < 2009110800) {
36051c9e 208 require_once($CFG->dirroot . '/comment/lib.php');
94522199 209 upgrade_set_timeout(60*20);
03221650 210
c8092ea5
DC
211 /// Define table glossary_comments to be dropped
212 $table = new xmldb_table('glossary_comments');
213
214 /// Conditionally launch drop table for glossary_comments
215 if ($dbman->table_exists($table)) {
fc9d2fe1
PS
216 $sql = "SELECT e.glossaryid AS glossaryid,
217 g.course AS courseid,
218 c.userid,
219 e.id AS itemid,
220 c.id AS old_id,
221 c.entrycomment AS commentcontent,
222 c.entrycommentformat AS format,
223 c.entrycommenttrust AS trust,
224 c.timemodified AS timemodified
225 FROM {glossary_comments} c, {glossary_entries} e, {glossary} g
226 WHERE c.entryid=e.id AND e.glossaryid=g.id
227 ORDER BY glossaryid, courseid";
94522199
DC
228 $lastglossaryid = null;
229 $lastcourseid = null;
230 $modcontext = null;
231
866354a9 232 /// move glossary comments to comments table
c8092ea5
DC
233 if ($rs = $DB->get_recordset_sql($sql)) {
234 foreach($rs as $res) {
94522199
DC
235 if ($res->glossaryid != $lastglossaryid || $res->courseid != $lastcourseid) {
236 $cm = get_coursemodule_from_instance('glossary', $res->glossaryid, $res->courseid);
237 if ($cm) {
238 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
c8092ea5 239 }
94522199
DC
240 $lastglossaryid = $res->glossaryid;
241 $lastcourseid = $res->courseid;
242 }
243 $cmt = new stdclass;
51f93f4a
DC
244 $cmt->contextid = $modcontext->id;
245 $cmt->commentarea = 'glossary_entry';
246 $cmt->itemid = $res->itemid;
247 $cmt->content = $res->commentcontent;
248 $cmt->format = $res->format;
249 $cmt->userid = $res->userid;
250 $cmt->timecreated = $res->timemodified;
251 $cmt_id = $DB->insert_record('comments', $cmt);
252 if (!empty($cmt_id)) {
94522199 253 $DB->delete_records('glossary_comments', array('id'=>$res->old_id));
c8092ea5
DC
254 }
255 }
256 }
94522199 257 $dbman->drop_table($table);
c8092ea5
DC
258 }
259
260 /// glossary savepoint reached
261 upgrade_mod_savepoint($result, 2009110800, 'glossary');
262 }
6507d1a9 263
e404dffd 264 if ($result && $oldversion < 2010042800) {
9288633d 265 //migrate glossary_ratings to the central rating table
5e1bccd2 266 require_once($CFG->dirroot . '/lib/db/upgradelib.php');
67e4713c 267
c506e354
AD
268 $table = new xmldb_table('glossary_ratings');
269 if ($dbman->table_exists($table)) {
8546cdd4
AD
270 //glossary ratings only have a single time column so use it for both time created and modified
271 $sql = "INSERT INTO {rating} (contextid, scaleid, itemid, rating, userid, timecreated, timemodified)
fc9d2fe1
PS
272
273 SELECT cxt.id, g.scale, r.entryid AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified
274 FROM {glossary_ratings} r
275 JOIN {glossary_entries} ge ON ge.id=r.entryid
276 JOIN {glossary} g ON g.id=ge.glossaryid
277 JOIN {course_modules} cm ON cm.instance=g.id
278 JOIN {context} cxt ON cxt.instanceid=cm.id
279 JOIN {modules} m ON m.id=cm.module
280 WHERE m.name = :modname AND cxt.contextlevel = :contextlevel";
8546cdd4
AD
281
282 $params['modname'] = 'glossary';
283 $params['contextlevel'] = CONTEXT_MODULE;
284
285 $DB->execute($sql, $params);
286
287 //now drop glossary_ratings
c506e354
AD
288 $dbman->drop_table($table);
289 }
9288633d 290
c506e354 291 upgrade_mod_savepoint($result, 2010042800, 'glossary');
9288633d 292 }
8b69c341 293
ecfeb03a 294 return $result;
295}
296
86342d63 297