MDL-23204 convert text to html in folder 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
203 /// glossary savepoint reached
a4cdd6d2 204 upgrade_mod_savepoint(true, 2009042006, 'glossary');
6507d1a9 205 }
a4cdd6d2 206 if ($oldversion < 2009110800) {
36051c9e 207 require_once($CFG->dirroot . '/comment/lib.php');
94522199 208 upgrade_set_timeout(60*20);
03221650 209
c8092ea5
DC
210 /// Define table glossary_comments to be dropped
211 $table = new xmldb_table('glossary_comments');
212
213 /// Conditionally launch drop table for glossary_comments
214 if ($dbman->table_exists($table)) {
fc9d2fe1
PS
215 $sql = "SELECT e.glossaryid AS glossaryid,
216 g.course AS courseid,
217 c.userid,
218 e.id AS itemid,
219 c.id AS old_id,
220 c.entrycomment AS commentcontent,
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
225 WHERE c.entryid=e.id AND e.glossaryid=g.id
226 ORDER BY glossaryid, courseid";
94522199
DC
227 $lastglossaryid = null;
228 $lastcourseid = null;
229 $modcontext = null;
230
866354a9 231 /// move glossary comments to comments table
c8092ea5
DC
232 if ($rs = $DB->get_recordset_sql($sql)) {
233 foreach($rs as $res) {
94522199
DC
234 if ($res->glossaryid != $lastglossaryid || $res->courseid != $lastcourseid) {
235 $cm = get_coursemodule_from_instance('glossary', $res->glossaryid, $res->courseid);
236 if ($cm) {
237 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
c8092ea5 238 }
94522199
DC
239 $lastglossaryid = $res->glossaryid;
240 $lastcourseid = $res->courseid;
241 }
242 $cmt = new stdclass;
51f93f4a
DC
243 $cmt->contextid = $modcontext->id;
244 $cmt->commentarea = 'glossary_entry';
245 $cmt->itemid = $res->itemid;
246 $cmt->content = $res->commentcontent;
247 $cmt->format = $res->format;
248 $cmt->userid = $res->userid;
249 $cmt->timecreated = $res->timemodified;
250 $cmt_id = $DB->insert_record('comments', $cmt);
251 if (!empty($cmt_id)) {
94522199 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
a4cdd6d2 260 upgrade_mod_savepoint(true, 2009110800, 'glossary');
c8092ea5 261 }
6507d1a9 262
a4cdd6d2 263 if ($oldversion < 2010042800) {
9288633d 264 //migrate glossary_ratings to the central rating table
c506e354
AD
265 $table = new xmldb_table('glossary_ratings');
266 if ($dbman->table_exists($table)) {
8546cdd4
AD
267 //glossary ratings only have a single time column so use it for both time created and modified
268 $sql = "INSERT INTO {rating} (contextid, scaleid, itemid, rating, userid, timecreated, timemodified)
fc9d2fe1
PS
269
270 SELECT cxt.id, g.scale, r.entryid AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified
271 FROM {glossary_ratings} r
272 JOIN {glossary_entries} ge ON ge.id=r.entryid
273 JOIN {glossary} g ON g.id=ge.glossaryid
274 JOIN {course_modules} cm ON cm.instance=g.id
275 JOIN {context} cxt ON cxt.instanceid=cm.id
276 JOIN {modules} m ON m.id=cm.module
277 WHERE m.name = :modname AND cxt.contextlevel = :contextlevel";
8546cdd4
AD
278
279 $params['modname'] = 'glossary';
280 $params['contextlevel'] = CONTEXT_MODULE;
281
282 $DB->execute($sql, $params);
283
284 //now drop glossary_ratings
c506e354
AD
285 $dbman->drop_table($table);
286 }
9288633d 287
a4cdd6d2 288 upgrade_mod_savepoint(true, 2010042800, 'glossary');
9288633d 289 }
8b69c341 290
a4cdd6d2 291 return true;
ecfeb03a 292}
293
86342d63 294