d13535ec59526d09f74f814323a9f3af9c03dded
[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     /// glossary savepoint reached
204         upgrade_mod_savepoint(true, 2009042006, 'glossary');
205     }
206     if ($oldversion < 2009110800) {
207         require_once($CFG->dirroot . '/comment/lib.php');
208         upgrade_set_timeout(60*20);
210     /// Define table glossary_comments to be dropped
211         $table = new xmldb_table('glossary_comments');
213     /// Conditionally launch drop table for glossary_comments
214         if ($dbman->table_exists($table)) {
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";
227             $lastglossaryid = null;
228             $lastcourseid   = null;
229             $modcontext     = null;
231         /// move glossary comments to comments table
232             if ($rs = $DB->get_recordset_sql($sql)) {
233                 foreach($rs as $res) {
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);
238                         }
239                         $lastglossaryid = $res->glossaryid;
240                         $lastcourseid   = $res->courseid;
241                     }
242                     $cmt = new stdclass;
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)) {
252                         $DB->delete_records('glossary_comments', array('id'=>$res->old_id));
253                     }
254                 }
255             }
256             $dbman->drop_table($table);
257         }
259     /// glossary savepoint reached
260         upgrade_mod_savepoint(true, 2009110800, 'glossary');
261     }
263     if ($oldversion < 2010042800) {
264         //migrate glossary_ratings to the central rating table
265         $table = new xmldb_table('glossary_ratings');
266         if ($dbman->table_exists($table)) {
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)
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";
279             $params['modname'] = 'glossary';
280             $params['contextlevel'] = CONTEXT_MODULE;
282             $DB->execute($sql, $params);
284             //now drop glossary_ratings
285             $dbman->drop_table($table);
286         }
288         upgrade_mod_savepoint(true, 2010042800, 'glossary');
289     }
291     return true;