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