MDL-23204 convert text to html in glossary intro
[moodle.git] / mod / glossary / db / upgrade.php
1 <?php
3 // This file keeps track of upgrades to
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 installation 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,
18 // using the methods of database_manager class
19 //
20 // Please do not forget to use upgrade_set_timeout()
21 // before any action that may take longer time to finish.
23 function xmldb_glossary_upgrade($oldversion) {
24     global $CFG, $DB, $OUTPUT;
26     $dbman = $DB->get_manager();
28 //===== 1.9.0 upgrade line ======//
30     if ($oldversion < 2008081900) {
32         /////////////////////////////////////
33         /// new file storage upgrade code ///
34         /////////////////////////////////////
36         $fs = get_file_storage();
38         $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
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)
44                    WHERE ge.attachment <> '$empty' AND ge.attachment <> '1'";
46         $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
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")) {
50             $pbar = new progress_bar('migrateglossaryfiles', 500, true);
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.");
58                 $filepath = "$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id/$entry->attachment";
59                 if ($entry->sourceglossaryid and !is_readable($filepath)) {
60                     //eh - try the second possible location
61                     $filepath = "$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->sourceglossaryid/$entry->id/$entry->attachment";
63                 }
64                 if (!is_readable($filepath)) {
65                     //file missing??
66                     echo $OUTPUT->notification("File not readable, skipping: $filepath");
67                     $entry->attachment = '';
68                     $DB->update_record('glossary_entries', $entry);
69                     continue;
70                 }
71                 $context = get_context_instance(CONTEXT_MODULE, $entry->cmid);
73                 $filearea = 'attachment';
74                 $filename = clean_param($entry->attachment, PARAM_FILE);
75                 if ($filename === '') {
76                     echo $OUTPUT->notification("Unsupported entry filename, skipping: ".$filepath);
77                     $entry->attachment = '';
78                     $DB->update_record('glossary_entries', $entry);
79                     continue;
80                 }
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);
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                 }
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             }
96             $rs->close();
97         }
99         upgrade_mod_savepoint(true, 2008081900, 'glossary');
100     }
102     if ($oldversion < 2009042000) {
104     /// Rename field definitionformat on table glossary_entries to definitionformat
105         $table = new xmldb_table('glossary_entries');
106         $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'definition');
108     /// Launch rename field definitionformat
109         $dbman->rename_field($table, $field, 'definitionformat');
111     /// glossary savepoint reached
112         upgrade_mod_savepoint(true, 2009042000, 'glossary');
113     }
115     if ($oldversion < 2009042001) {
117     /// Define field definitiontrust to be added to glossary_entries
118         $table = new xmldb_table('glossary_entries');
119         $field = new xmldb_field('definitiontrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'definitionformat');
121     /// Launch add field definitiontrust
122         $dbman->add_field($table, $field);
124     /// glossary savepoint reached
125         upgrade_mod_savepoint(true, 2009042001, 'glossary');
126     }
128     if ($oldversion < 2009042002) {
130     /// Rename field format on table glossary_comments to NEWNAMEGOESHERE
131         $table = new xmldb_table('glossary_comments');
132         $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'entrycomment');
134     /// Launch rename field format
135         $dbman->rename_field($table, $field, 'entrycommentformat');
137     /// glossary savepoint reached
138         upgrade_mod_savepoint(true, 2009042002, 'glossary');
139     }
141     if ($oldversion < 2009042003) {
143     /// Define field entrycommenttrust to be added to glossary_comments
144         $table = new xmldb_table('glossary_comments');
145         $field = new xmldb_field('entrycommenttrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'entrycommentformat');
147     /// Conditionally launch add field entrycommenttrust
148         if (!$dbman->field_exists($table, $field)) {
149             $dbman->add_field($table, $field);
150         }
152     /// glossary savepoint reached
153         upgrade_mod_savepoint(true, 2009042003, 'glossary');
154     }
156     if ($oldversion < 2009042004) {
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();
170     /// glossary savepoint reached
171         upgrade_mod_savepoint(true, 2009042004, 'glossary');
172     }
174     if ($oldversion < 2009042005) {
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();
188     /// glossary savepoint reached
189         upgrade_mod_savepoint(true, 2009042005, 'glossary');
190     }
192     if ($oldversion < 2009042006) {
194     /// Define field introformat to be added to glossary
195         $table = new xmldb_table('glossary');
196         $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
198     /// Conditionally launch add field introformat
199         if (!$dbman->field_exists($table, $field)) {
200             $dbman->add_field($table, $field);
201         }
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         }
215     /// glossary savepoint reached
216         upgrade_mod_savepoint(true, 2009042006, 'glossary');
217     }
218     if ($oldversion < 2009110800) {
219         require_once($CFG->dirroot . '/comment/lib.php');
220         upgrade_set_timeout(60*20);
222     /// Define table glossary_comments to be dropped
223         $table = new xmldb_table('glossary_comments');
225     /// Conditionally launch drop table for glossary_comments
226         if ($dbman->table_exists($table)) {
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";
239             $lastglossaryid = null;
240             $lastcourseid   = null;
241             $modcontext     = null;
243         /// move glossary comments to comments table
244             if ($rs = $DB->get_recordset_sql($sql)) {
245                 foreach($rs as $res) {
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);
250                         }
251                         $lastglossaryid = $res->glossaryid;
252                         $lastcourseid   = $res->courseid;
253                     }
254                     $cmt = new stdclass;
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)) {
264                         $DB->delete_records('glossary_comments', array('id'=>$res->old_id));
265                     }
266                 }
267             }
268             $dbman->drop_table($table);
269         }
271     /// glossary savepoint reached
272         upgrade_mod_savepoint(true, 2009110800, 'glossary');
273     }
275     if ($oldversion < 2010042800) {
276         //migrate glossary_ratings to the central rating table
277         $table = new xmldb_table('glossary_ratings');
278         if ($dbman->table_exists($table)) {
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)
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";
291             $params['modname'] = 'glossary';
292             $params['contextlevel'] = CONTEXT_MODULE;
294             $DB->execute($sql, $params);
296             //now drop glossary_ratings
297             $dbman->drop_table($table);
298         }
300         upgrade_mod_savepoint(true, 2010042800, 'glossary');
301     }
303     return true;