MDL-15252 integrate qtype_dependent.php strings in qtype_calculated.php and replace...
[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
74 $filearea = 'glossary_attachment';
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 }
82 if (!$fs->file_exists($context->id, $filearea, $entry->id, '/', $filename)) {
83 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$entry->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$entry->userid);
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)) {
216 $sql = 'SELECT e.glossaryid AS glossaryid,
217 g.course AS courseid,
218 e.id AS itemid,
219 c.id AS old_id,
cea304d4 220 c.entrycomment AS commentcontent,
c8092ea5
DC
221 c.entrycommentformat AS format,
222 c.entrycommenttrust AS trust,
223 c.timemodified AS timemodified
224 FROM {glossary_comments} c, {glossary_entries} e, {glossary} g
94522199
DC
225 WHERE c.entryid=e.id AND e.glossaryid=g.id ORDER BY glossaryid, courseid';
226 $lastglossaryid = null;
227 $lastcourseid = null;
228 $modcontext = null;
229
866354a9 230 /// move glossary comments to comments table
c8092ea5
DC
231 if ($rs = $DB->get_recordset_sql($sql)) {
232 foreach($rs as $res) {
94522199
DC
233 if ($res->glossaryid != $lastglossaryid || $res->courseid != $lastcourseid) {
234 $cm = get_coursemodule_from_instance('glossary', $res->glossaryid, $res->courseid);
235 if ($cm) {
236 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
c8092ea5 237 }
94522199
DC
238 $lastglossaryid = $res->glossaryid;
239 $lastcourseid = $res->courseid;
240 }
866354a9 241 list($context, $course, $cm) = get_context_info_array($contextid);
94522199 242 $cmt = new stdclass;
866354a9
DC
243 $cmt->pluginname = 'glossary';
244 $cmt->context = $modcontext;
245 $cmt->courseid = $res->courseid;
246 $cmt->cm = $cm;
247 $cmt->area = 'glossary_entry';
248 $cmt->itemid = $res->itemid;
94522199 249 $comment = new comment($cmt);
cea304d4 250 $cmt = $comment->add($res->commentcontent, $res->format);
94522199
DC
251 if (!empty($cmt)) {
252 $DB->delete_records('glossary_comments', array('id'=>$res->old_id));
c8092ea5
DC
253 }
254 }
255 }
94522199 256 $dbman->drop_table($table);
c8092ea5
DC
257 }
258
259 /// glossary savepoint reached
260 upgrade_mod_savepoint($result, 2009110800, 'glossary');
261 }
6507d1a9 262
c506e354 263 if($result && $oldversion < 2010042800) {
9288633d 264 //migrate glossary_ratings to the central rating table
5e1bccd2 265 require_once($CFG->dirroot . '/lib/db/upgradelib.php');
67e4713c 266
9288633d 267 //glossary ratings only have a single time column so use it for both time created and modified
62b1d51b 268 $sql = "INSERT INTO {rating} (contextid, scaleid, itemid, rating, userid, timecreated, timemodified)
63e87951 269SELECT cxt.id, g.scale, r.entryid AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified
9288633d
AD
270FROM {glossary_ratings} r
271JOIN {glossary_entries} ge ON ge.id=r.entryid
63e87951
AD
272JOIN {glossary} g ON g.id=ge.glossaryid
273JOIN {course_modules} cm ON cm.instance=g.id
274JOIN {context} cxt ON cxt.instanceid=cm.id
275JOIN {modules} m ON m.id=cm.module
276WHERE m.name = :modname AND cxt.contextlevel = :contextlevel";
9288633d 277
63e87951
AD
278 $params['modname'] = 'glossary';
279 $params['contextlevel'] = CONTEXT_MODULE;
280
281 $DB->execute($sql, $params);
9288633d 282
c506e354
AD
283 //now drop glossary_ratings
284 $table = new xmldb_table('glossary_ratings');
285 if ($dbman->table_exists($table)) {
286 $dbman->drop_table($table);
287 }
9288633d 288
c506e354 289 upgrade_mod_savepoint($result, 2010042800, 'glossary');
9288633d 290 }
8b69c341 291
ecfeb03a 292 return $result;
293}
294
86342d63 295