webservice MDL-12886 clean SOAP client
[moodle.git] / mod / forum / db / upgrade.php
CommitLineData
b8a342d7 1<?php //$Id$
2
4e445355 3// This file keeps track of upgrades to
b8a342d7 4// the forum 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 installtion 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,
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.
b8a342d7 22
775f811a 23function xmldb_forum_upgrade($oldversion) {
24 global $CFG, $DB;
b8a342d7 25
4e781c7b 26 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
b8a342d7 27 $result = true;
28
f33e1ed4 29//===== 1.9.0 upgrade line ======//
f3c3a4d3 30
347037d8 31 if ($result and $oldversion < 2007101511) {
b30f2e92 32 //MDL-13866 - send forum ratins to gradebook again
33 require_once($CFG->dirroot.'/mod/forum/lib.php');
775f811a 34 forum_upgrade_grades();
04264aed 35 upgrade_mod_savepoint($result, 2007101511, 'forum');
b30f2e92 36 }
f3c3a4d3 37
90f4745c 38 if ($result && $oldversion < 2007101512) {
39
40 /// Cleanup the forum subscriptions
41 notify('Removing stale forum subscriptions', 'notifysuccess');
42
43 $roles = get_roles_with_capability('moodle/course:view', CAP_ALLOW);
44 $roles = array_keys($roles);
90f4745c 45
4e445355 46 list($usql, $params) = $DB->get_in_or_equal($roles);
90f4745c 47 $sql = "SELECT fs.userid, f.id AS forumid
219f652b 48 FROM {forum} f
49 JOIN {course} c ON c.id = f.course
50 JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = ".CONTEXT_COURSE.")
51 JOIN {forum_subscriptions} fs ON fs.forum = f.id
4e445355 52 LEFT JOIN {role_assignments} ra ON (ra.contextid = ctx.id AND ra.userid = fs.userid AND ra.roleid $usql)
90f4745c 53 WHERE ra.id IS NULL";
54
4e445355 55 if ($rs = $DB->get_recordset_sql($sql, $params)) {
219f652b 56 foreach ($rs as $remove) {
57 $DB->delete_records('forum_subscriptions', array('userid'=>$remove->userid, 'forum'=>$remove->forumid));
90f4745c 58 echo '.';
59 }
ca3c8973 60 $rs->close();
90f4745c 61 }
04264aed 62
63 upgrade_mod_savepoint($result, 2007101512, 'forum');
90f4745c 64 }
4e781c7b 65
4e781c7b 66 if ($result && $oldversion < 2008072800) {
67 /// Define field completiondiscussions to be added to forum
ba6d1638 68 $table = new xmldb_table('forum');
69 $field = new xmldb_field('completiondiscussions');
2a88f626 70 $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'blockperiod');
4e781c7b 71
72 /// Launch add field completiondiscussions
73 if(!$dbman->field_exists($table,$field)) {
74 $dbman->add_field($table, $field);
75 }
76
ba6d1638 77 $field = new xmldb_field('completionreplies');
2a88f626 78 $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiondiscussions');
4e781c7b 79
80 /// Launch add field completionreplies
81 if(!$dbman->field_exists($table,$field)) {
82 $dbman->add_field($table, $field);
83 }
84
85 /// Define field completionposts to be added to forum
ba6d1638 86 $field = new xmldb_field('completionposts');
2a88f626 87 $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionreplies');
4e781c7b 88
89 /// Launch add field completionposts
90 if(!$dbman->field_exists($table,$field)) {
91 $dbman->add_field($table, $field);
92 }
93 upgrade_mod_savepoint($result, 2008072800, 'forum');
94 }
90f4745c 95
6b04ddc5 96 if ($result && $oldversion < 2008081900) {
0faf6791 97
98 /////////////////////////////////////
99 /// new file storage upgrade code ///
100 /////////////////////////////////////
101
102 $fs = get_file_storage();
103
104 $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
105
106 $sqlfrom = "FROM {forum_posts} p
107 JOIN {forum_discussions} d ON d.id = p.discussion
108 JOIN {forum} f ON f.id = d.forum
109 JOIN {modules} m ON m.name = 'forum'
110 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = f.id)
39f13edb 111 WHERE p.attachment <> '$empty' AND p.attachment <> '1'";
0faf6791 112
113 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
114
39f13edb 115 if ($rs = $DB->get_recordset_sql("SELECT p.id, p.attachment, d.forum, f.course, cm.id AS cmid $sqlfrom ORDER BY f.course, f.id, d.id")) {
0faf6791 116
117 $pbar = new progress_bar('migrateforumfiles', 500, true);
118
0faf6791 119 $i = 0;
120 foreach ($rs as $post) {
121 $i++;
122 upgrade_set_timeout(60); // set up timeout, may also abort execution
123 $pbar->update($i, $count, "Migrating forum posts - $i/$count.");
124
125 $filepath = "$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum/$post->id/$post->attachment";
126 if (!is_readable($filepath)) {
127 //file missing??
128 notify("File not readable, skipping: ".$filepath);
129 $post->attachment = '';
130 $DB->update_record('forum_posts', $post);
131 continue;
132 }
133 $context = get_context_instance(CONTEXT_MODULE, $post->cmid);
134
135 $filearea = 'forum_attachment';
136 $filename = clean_param($post->attachment, PARAM_FILE);
137 if ($filename === '') {
138 notify("Unsupported post filename, skipping: ".$filepath);
139 $post->attachment = '';
140 $DB->update_record('forum_posts', $post);
141 continue;
142 }
143 if (!$fs->file_exists($context->id, $filearea, $post->id, '/', $filename)) {
144 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$post->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$post->userid);
145 if ($fs->create_file_from_pathname($file_record, $filepath)) {
146 $post->attachment = '1';
147 if ($DB->update_record('forum_posts', $post)) {
148 unlink($filepath);
149 }
150 }
151 }
152
153 // remove dirs if empty
154 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum/$post->id");
155 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum");
156 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum");
157 }
0faf6791 158 $rs->close();
159 }
160
6b04ddc5 161 upgrade_mod_savepoint($result, 2008081900, 'forum');
0faf6791 162 }
163
30a9aff5 164 if ($result && $oldversion < 2008090800) {
165
166 /// Define field maxattachments to be added to forum
167 $table = new xmldb_table('forum');
2a88f626 168 $field = new xmldb_field('maxattachments', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'maxbytes');
30a9aff5 169
170 /// Conditionally launch add field maxattachments
171 if (!$dbman->field_exists($table, $field)) {
172 $dbman->add_field($table, $field);
173 }
174
175 /// forum savepoint reached
176 upgrade_mod_savepoint($result, 2008090800, 'forum');
177 }
178
e2d7687f 179 if ($result && $oldversion < 2009042000) {
30a9aff5 180
e2d7687f 181 /// Rename field format on table forum_posts to messageformat
182 $table = new xmldb_table('forum_posts');
2a88f626 183 $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'message');
90f4745c 184
e2d7687f 185 /// Launch rename field format
186 $dbman->rename_field($table, $field, 'messageformat');
187
188 /// forum savepoint reached
189 upgrade_mod_savepoint($result, 2009042000, 'forum');
190 }
191
192 if ($result && $oldversion < 2009042001) {
193
194 /// Define field messagetrust to be added to forum_posts
195 $table = new xmldb_table('forum_posts');
2a88f626 196 $field = new xmldb_field('messagetrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'messageformat');
e2d7687f 197
198 /// Launch add field messagetrust
199 $dbman->add_field($table, $field);
200
201 /// forum savepoint reached
202 upgrade_mod_savepoint($result, 2009042001, 'forum');
203 }
702dc57b 204
e2d7687f 205 if ($result && $oldversion < 2009042002) {
206 $trustmark = '#####TRUSTTEXT#####';
207 $rs = $DB->get_recordset_sql("SELECT * FROM {forum_posts} WHERE message LIKE '$trustmark%'");
208 foreach ($rs as $post) {
209 if (strpos($post->entrycomment, $trustmark) !== 0) {
210 // probably lowercase in some DBs
211 continue;
212 }
213 $post->message = trusttext_strip($post->message);
214 $post->messagetrust = 1;
215 $DB->update_record('forum_posts', $post);
216 }
217 $rs->close();
218
219 /// forum savepoint reached
220 upgrade_mod_savepoint($result, 2009042002, 'forum');
221 }
702dc57b 222
6507d1a9 223 if ($result && $oldversion < 2009042003) {
224
225 /// Define field introformat to be added to forum
226 $table = new xmldb_table('forum');
2a88f626 227 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
6507d1a9 228
229 /// Launch add field introformat
230 $dbman->add_field($table, $field);
231
232 /// forum savepoint reached
233 upgrade_mod_savepoint($result, 2009042003, 'forum');
234 }
702dc57b 235
18b5df91 236 if ($result && $oldversion < 2009042004) {
237 /// set format to current
238 $DB->set_field('forum', 'introformat', FORMAT_MOODLE, array());
239
240 /// quiz savepoint reached
241 upgrade_mod_savepoint($result, 2009042004, 'forum');
242 }
243
e37cd84a 244 /// Dropping all enums/check contraints from core. MDL-18577
245 if ($result && $oldversion < 2009042700) {
246
247 /// Changing list of values (enum) of field type on table forum to none
248 $table = new xmldb_table('forum');
2a88f626 249 $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general', 'course');
e37cd84a 250
251 /// Launch change of list of values for field type
2a88f626 252 $dbman->drop_enum_from_field($table, $field);
e37cd84a 253
254 /// forum savepoint reached
255 upgrade_mod_savepoint($result, 2009042700, 'forum');
256 }
257
b8a342d7 258 return $result;
259}
260
261?>