3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This file keeps track of upgrades to
22 * Sometimes, changes between versions involve
23 * alterations to database structures and other
24 * major things that may break installations.
26 * The upgrade function in this file will attempt
27 * to perform all the necessary actions to upgrade
28 * your older installation to the current version.
30 * If there's something it cannot do itself, it
31 * will tell you what you need to do.
33 * The commands in here will all be database-neutral,
34 * using the methods of database_manager class
36 * Please do not forget to use upgrade_set_timeout()
37 * before any action that may take longer time to finish.
40 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 function xmldb_forum_upgrade($oldversion) {
45 global $CFG, $DB, $OUTPUT;
47 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
49 //===== 1.9.0 upgrade line ======//
51 if ($oldversion < 2007101511) {
52 //MDL-13866 - send forum ratins to gradebook again
53 require_once($CFG->dirroot.'/mod/forum/lib.php');
54 forum_upgrade_grades();
55 upgrade_mod_savepoint(true, 2007101511, 'forum');
58 if ($oldversion < 2008072800) {
59 /// Define field completiondiscussions to be added to forum
60 $table = new xmldb_table('forum');
61 $field = new xmldb_field('completiondiscussions');
62 $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'blockperiod');
64 /// Launch add field completiondiscussions
65 if(!$dbman->field_exists($table,$field)) {
66 $dbman->add_field($table, $field);
69 $field = new xmldb_field('completionreplies');
70 $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiondiscussions');
72 /// Launch add field completionreplies
73 if(!$dbman->field_exists($table,$field)) {
74 $dbman->add_field($table, $field);
77 /// Define field completionposts to be added to forum
78 $field = new xmldb_field('completionposts');
79 $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionreplies');
81 /// Launch add field completionposts
82 if(!$dbman->field_exists($table,$field)) {
83 $dbman->add_field($table, $field);
85 upgrade_mod_savepoint(true, 2008072800, 'forum');
88 if ($oldversion < 2008081900) {
90 /////////////////////////////////////
91 /// new file storage upgrade code ///
92 /////////////////////////////////////
94 $fs = get_file_storage();
96 $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
98 $sqlfrom = "FROM {forum_posts} p
99 JOIN {forum_discussions} d ON d.id = p.discussion
100 JOIN {forum} f ON f.id = d.forum
101 JOIN {modules} m ON m.name = 'forum'
102 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = f.id)
103 WHERE p.attachment <> '$empty' AND p.attachment <> '1'";
105 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
107 if ($rs = $DB->get_recordset_sql("SELECT p.id, p.attachment, p.userid, d.forum, f.course, cm.id AS cmid $sqlfrom ORDER BY f.course, f.id, d.id")) {
109 $pbar = new progress_bar('migrateforumfiles', 500, true);
112 foreach ($rs as $post) {
114 upgrade_set_timeout(60); // set up timeout, may also abort execution
115 $pbar->update($i, $count, "Migrating forum posts - $i/$count.");
117 $filepath = "$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum/$post->id/$post->attachment";
118 if (!is_readable($filepath)) {
120 echo $OUTPUT->notification("File not readable, skipping: ".$filepath);
121 $post->attachment = '';
122 $DB->update_record('forum_posts', $post);
125 $context = get_context_instance(CONTEXT_MODULE, $post->cmid);
127 $filearea = 'attachment';
128 $filename = clean_param($post->attachment, PARAM_FILE);
129 if ($filename === '') {
130 echo $OUTPUT->notification("Unsupported post filename, skipping: ".$filepath);
131 $post->attachment = '';
132 $DB->update_record('forum_posts', $post);
135 if (!$fs->file_exists($context->id, 'mod_form', $filearea, $post->id, '/', $filename)) {
136 $file_record = array('contextid'=>$context->id, 'component'=>'mod_forum', 'filearea'=>$filearea, 'itemid'=>$post->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$post->userid);
137 if ($fs->create_file_from_pathname($file_record, $filepath)) {
138 $post->attachment = '1';
139 $DB->update_record('forum_posts', $post);
144 // remove dirs if empty
145 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum/$post->id");
146 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum");
147 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum");
152 upgrade_mod_savepoint(true, 2008081900, 'forum');
155 if ($oldversion < 2008090800) {
157 /// Define field maxattachments to be added to forum
158 $table = new xmldb_table('forum');
159 $field = new xmldb_field('maxattachments', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'maxbytes');
161 /// Conditionally launch add field maxattachments
162 if (!$dbman->field_exists($table, $field)) {
163 $dbman->add_field($table, $field);
166 /// forum savepoint reached
167 upgrade_mod_savepoint(true, 2008090800, 'forum');
170 if ($oldversion < 2009042000) {
172 /// Rename field format on table forum_posts to messageformat
173 $table = new xmldb_table('forum_posts');
174 $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'message');
176 /// Launch rename field format
177 $dbman->rename_field($table, $field, 'messageformat');
179 /// forum savepoint reached
180 upgrade_mod_savepoint(true, 2009042000, 'forum');
183 if ($oldversion < 2009042001) {
185 /// Define field messagetrust to be added to forum_posts
186 $table = new xmldb_table('forum_posts');
187 $field = new xmldb_field('messagetrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'messageformat');
189 /// Launch add field messagetrust
190 $dbman->add_field($table, $field);
192 /// forum savepoint reached
193 upgrade_mod_savepoint(true, 2009042001, 'forum');
196 if ($oldversion < 2009042002) {
197 $trustmark = '#####TRUSTTEXT#####';
198 $rs = $DB->get_recordset_sql("SELECT * FROM {forum_posts} WHERE message LIKE '$trustmark%'");
199 foreach ($rs as $post) {
200 if (strpos($post->entrycomment, $trustmark) !== 0) {
201 // probably lowercase in some DBs
204 $post->message = trusttext_strip($post->message);
205 $post->messagetrust = 1;
206 $DB->update_record('forum_posts', $post);
210 /// forum savepoint reached
211 upgrade_mod_savepoint(true, 2009042002, 'forum');
214 if ($oldversion < 2009042003) {
216 /// Define field introformat to be added to forum
217 $table = new xmldb_table('forum');
218 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
220 /// Launch add field introformat
221 if (!$dbman->field_exists($table, $field)) {
222 $dbman->add_field($table, $field);
225 // conditionally migrate to html format in intro
226 if ($CFG->texteditors !== 'textarea') {
227 $rs = $DB->get_recordset('forum', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
228 foreach ($rs as $f) {
229 $f->intro = text_to_html($f->intro, false, false, true);
230 $f->introformat = FORMAT_HTML;
231 $DB->update_record('forum', $f);
232 upgrade_set_timeout();
237 /// forum savepoint reached
238 upgrade_mod_savepoint(true, 2009042003, 'forum');
241 /// Dropping all enums/check contraints from core. MDL-18577
242 if ($oldversion < 2009042700) {
244 /// Changing list of values (enum) of field type on table forum to none
245 $table = new xmldb_table('forum');
246 $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general', 'course');
248 /// Launch change of list of values for field type
249 $dbman->drop_enum_from_field($table, $field);
251 /// forum savepoint reached
252 upgrade_mod_savepoint(true, 2009042700, 'forum');
255 if ($oldversion < 2009050400) {
257 /// Clean existing wrong rates. MDL-18227
258 $DB->delete_records('forum_ratings', array('post' => 0));
260 /// forum savepoint reached
261 upgrade_mod_savepoint(true, 2009050400, 'forum');
264 if ($oldversion < 2010042800) {
265 //migrate forumratings to the central rating table
266 $table = new xmldb_table('forum_ratings');
267 if ($dbman->table_exists($table)) {
268 //forum ratings only have a single time column so use it for both time created and modified
269 $sql = "INSERT INTO {rating} (contextid, scaleid, itemid, rating, userid, timecreated, timemodified)
271 SELECT cxt.id, f.scale, r.post AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified
272 FROM {forum_ratings} r
273 JOIN {forum_posts} p ON p.id=r.post
274 JOIN {forum_discussions} d ON d.id=p.discussion
275 JOIN {forum} f ON f.id=d.forum
276 JOIN {course_modules} cm ON cm.instance=f.id
277 JOIN {context} cxt ON cxt.instanceid=cm.id
278 JOIN {modules} m ON m.id=cm.module
279 WHERE m.name = :modname AND cxt.contextlevel = :contextlevel";
280 $params['modname'] = 'forum';
281 $params['contextlevel'] = CONTEXT_MODULE;
283 $DB->execute($sql, $params);
285 //now drop forum_ratings
286 $dbman->drop_table($table);
289 upgrade_mod_savepoint(true, 2010042800, 'forum');
292 if ($oldversion < 2010070800) {
294 // Remove the forum digests message provider MDL-23145
295 $DB->delete_records('message_providers', array('name' => 'digests','component'=>'mod_forum'));
297 // forum savepoint reached
298 upgrade_mod_savepoint(true, 2010070800, 'forum');
301 if ($oldversion < 2010091900) {
302 // rename files from borked upgrade in 2.0dev
303 $fs = get_file_storage();
304 $rs = $DB->get_recordset('files', array('component'=>'mod_form'));
305 foreach ($rs as $oldrecord) {
306 $file = $fs->get_file_instance($oldrecord);
307 $newrecord = array('component'=>'mod_forum');
308 if (!$fs->file_exists($oldrecord->contextid, 'mod_forum', $oldrecord->filearea, $oldrecord->itemid, $oldrecord->filepath, $oldrecord->filename)) {
309 $fs->create_file_from_storedfile($newrecord, $file);
314 upgrade_mod_savepoint(true, 2010091900, 'forum');