MDL-15348 Add changes for filepicker to support the display of existing files
[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)) {
f33e1ed4 56 $DB->set_debug(false);
219f652b 57 foreach ($rs as $remove) {
58 $DB->delete_records('forum_subscriptions', array('userid'=>$remove->userid, 'forum'=>$remove->forumid));
90f4745c 59 echo '.';
60 }
f33e1ed4 61 $DB->set_debug(true);
ca3c8973 62 $rs->close();
90f4745c 63 }
04264aed 64
65 upgrade_mod_savepoint($result, 2007101512, 'forum');
90f4745c 66 }
4e781c7b 67
4e781c7b 68 if ($result && $oldversion < 2008072800) {
69 /// Define field completiondiscussions to be added to forum
ba6d1638 70 $table = new xmldb_table('forum');
71 $field = new xmldb_field('completiondiscussions');
ee6a5a9d 72 $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'blockperiod');
4e781c7b 73
74 /// Launch add field completiondiscussions
75 if(!$dbman->field_exists($table,$field)) {
76 $dbman->add_field($table, $field);
77 }
78
ba6d1638 79 $field = new xmldb_field('completionreplies');
80 $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'completiondiscussions');
4e781c7b 81
82 /// Launch add field completionreplies
83 if(!$dbman->field_exists($table,$field)) {
84 $dbman->add_field($table, $field);
85 }
86
87 /// Define field completionposts to be added to forum
ba6d1638 88 $field = new xmldb_field('completionposts');
89 $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'completionreplies');
4e781c7b 90
91 /// Launch add field completionposts
92 if(!$dbman->field_exists($table,$field)) {
93 $dbman->add_field($table, $field);
94 }
95 upgrade_mod_savepoint($result, 2008072800, 'forum');
96 }
90f4745c 97
6b04ddc5 98 if ($result && $oldversion < 2008081900) {
0faf6791 99
100 /////////////////////////////////////
101 /// new file storage upgrade code ///
102 /////////////////////////////////////
103
104 $fs = get_file_storage();
105
106 $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
107
108 $sqlfrom = "FROM {forum_posts} p
109 JOIN {forum_discussions} d ON d.id = p.discussion
110 JOIN {forum} f ON f.id = d.forum
111 JOIN {modules} m ON m.name = 'forum'
112 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = f.id)
39f13edb 113 WHERE p.attachment <> '$empty' AND p.attachment <> '1'";
0faf6791 114
115 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
116
39f13edb 117 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 118
119 $pbar = new progress_bar('migrateforumfiles', 500, true);
120
121 $olddebug = $DB->get_debug();
122 $DB->set_debug(false); // lower debug level, there might be very many files
123 $i = 0;
124 foreach ($rs as $post) {
125 $i++;
126 upgrade_set_timeout(60); // set up timeout, may also abort execution
127 $pbar->update($i, $count, "Migrating forum posts - $i/$count.");
128
129 $filepath = "$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum/$post->id/$post->attachment";
130 if (!is_readable($filepath)) {
131 //file missing??
132 notify("File not readable, skipping: ".$filepath);
133 $post->attachment = '';
134 $DB->update_record('forum_posts', $post);
135 continue;
136 }
137 $context = get_context_instance(CONTEXT_MODULE, $post->cmid);
138
139 $filearea = 'forum_attachment';
140 $filename = clean_param($post->attachment, PARAM_FILE);
141 if ($filename === '') {
142 notify("Unsupported post filename, skipping: ".$filepath);
143 $post->attachment = '';
144 $DB->update_record('forum_posts', $post);
145 continue;
146 }
147 if (!$fs->file_exists($context->id, $filearea, $post->id, '/', $filename)) {
148 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$post->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$post->userid);
149 if ($fs->create_file_from_pathname($file_record, $filepath)) {
150 $post->attachment = '1';
151 if ($DB->update_record('forum_posts', $post)) {
152 unlink($filepath);
153 }
154 }
155 }
156
157 // remove dirs if empty
158 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum/$post->id");
159 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum");
160 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum");
161 }
162 $DB->set_debug($olddebug); // reset debug level
163 $rs->close();
164 }
165
6b04ddc5 166 upgrade_mod_savepoint($result, 2008081900, 'forum');
0faf6791 167 }
168
90f4745c 169
b8a342d7 170 return $result;
171}
172
173?>