Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/forum/db/upgrade.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00044 function xmldb_forum_upgrade($oldversion) {
00045     global $CFG, $DB, $OUTPUT;
00046 
00047     $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
00048 
00049 //===== 1.9.0 upgrade line ======//
00050 
00051     if ($oldversion < 2007101511) {
00052         //MDL-13866 - send forum ratins to gradebook again
00053         require_once($CFG->dirroot.'/mod/forum/lib.php');
00054         forum_upgrade_grades();
00055         upgrade_mod_savepoint(true, 2007101511, 'forum');
00056     }
00057 
00058     if ($oldversion < 2008072800) {
00060         $table = new xmldb_table('forum');
00061         $field = new xmldb_field('completiondiscussions');
00062         $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'blockperiod');
00063 
00065         if(!$dbman->field_exists($table,$field)) {
00066             $dbman->add_field($table, $field);
00067         }
00068 
00069         $field = new xmldb_field('completionreplies');
00070         $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiondiscussions');
00071 
00073         if(!$dbman->field_exists($table,$field)) {
00074             $dbman->add_field($table, $field);
00075         }
00076 
00078         $field = new xmldb_field('completionposts');
00079         $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionreplies');
00080 
00082         if(!$dbman->field_exists($table,$field)) {
00083             $dbman->add_field($table, $field);
00084         }
00085         upgrade_mod_savepoint(true, 2008072800, 'forum');
00086     }
00087 
00088     if ($oldversion < 2008081900) {
00089 
00093 
00094         $fs = get_file_storage();
00095 
00096         $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
00097 
00098         $sqlfrom = "FROM {forum_posts} p
00099                     JOIN {forum_discussions} d ON d.id = p.discussion
00100                     JOIN {forum} f ON f.id = d.forum
00101                     JOIN {modules} m ON m.name = 'forum'
00102                     JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = f.id)
00103                    WHERE p.attachment <> '$empty' AND p.attachment <> '1'";
00104 
00105         $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
00106 
00107         $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");
00108         if ($rs->valid()) {
00109 
00110             $pbar = new progress_bar('migrateforumfiles', 500, true);
00111 
00112             $i = 0;
00113             foreach ($rs as $post) {
00114                 $i++;
00115                 upgrade_set_timeout(60); // set up timeout, may also abort execution
00116                 $pbar->update($i, $count, "Migrating forum posts - $i/$count.");
00117 
00118                 $filepath = "$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum/$post->id/$post->attachment";
00119                 if (!is_readable($filepath)) {
00120                     //file missing??
00121                     echo $OUTPUT->notification("File not readable, skipping: ".$filepath);
00122                     $post->attachment = '';
00123                     $DB->update_record('forum_posts', $post);
00124                     continue;
00125                 }
00126                 $context = get_context_instance(CONTEXT_MODULE, $post->cmid);
00127 
00128                 $filearea = 'attachment';
00129                 $filename = clean_param($post->attachment, PARAM_FILE);
00130                 if ($filename === '') {
00131                     echo $OUTPUT->notification("Unsupported post filename, skipping: ".$filepath);
00132                     $post->attachment = '';
00133                     $DB->update_record('forum_posts', $post);
00134                     continue;
00135                 }
00136                 if (!$fs->file_exists($context->id, 'mod_forum', $filearea, $post->id, '/', $filename)) {
00137                     $file_record = array('contextid'=>$context->id, 'component'=>'mod_forum', 'filearea'=>$filearea, 'itemid'=>$post->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$post->userid);
00138                     if ($fs->create_file_from_pathname($file_record, $filepath)) {
00139                         $post->attachment = '1';
00140                         $DB->update_record('forum_posts', $post);
00141                         unlink($filepath);
00142                     }
00143                 }
00144 
00145                 // remove dirs if empty
00146                 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum/$post->id");
00147                 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum");
00148                 @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum");
00149             }
00150         }
00151         $rs->close();
00152 
00153         upgrade_mod_savepoint(true, 2008081900, 'forum');
00154     }
00155 
00156     if ($oldversion < 2008090800) {
00157 
00159         $table = new xmldb_table('forum');
00160         $field = new xmldb_field('maxattachments', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'maxbytes');
00161 
00163         if (!$dbman->field_exists($table, $field)) {
00164             $dbman->add_field($table, $field);
00165         }
00166 
00168         upgrade_mod_savepoint(true, 2008090800, 'forum');
00169     }
00170 
00171     if ($oldversion < 2009042000) {
00172 
00174         $table = new xmldb_table('forum_posts');
00175         $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'message');
00176 
00178         $dbman->rename_field($table, $field, 'messageformat');
00179 
00181         upgrade_mod_savepoint(true, 2009042000, 'forum');
00182     }
00183 
00184     if ($oldversion < 2009042001) {
00185 
00187         $table = new xmldb_table('forum_posts');
00188         $field = new xmldb_field('messagetrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'messageformat');
00189 
00191         $dbman->add_field($table, $field);
00192 
00194         upgrade_mod_savepoint(true, 2009042001, 'forum');
00195     }
00196 
00197     if ($oldversion < 2009042002) {
00198         $trustmark = '#####TRUSTTEXT#####';
00199         $rs = $DB->get_recordset_sql("SELECT * FROM {forum_posts} WHERE message LIKE ?", array($trustmark.'%'));
00200         foreach ($rs as $post) {
00201             if (strpos($post->message, $trustmark) !== 0) {
00202                 // probably lowercase in some DBs?
00203                 continue;
00204             }
00205             $post->message      = str_replace($trustmark, '', $post->message);
00206             $post->messagetrust = 1;
00207             $DB->update_record('forum_posts', $post);
00208         }
00209         $rs->close();
00210 
00212         upgrade_mod_savepoint(true, 2009042002, 'forum');
00213     }
00214 
00215     if ($oldversion < 2009042003) {
00216 
00218         $table = new xmldb_table('forum');
00219         $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
00220 
00222         if (!$dbman->field_exists($table, $field)) {
00223             $dbman->add_field($table, $field);
00224         }
00225 
00226         // conditionally migrate to html format in intro
00227         if ($CFG->texteditors !== 'textarea') {
00228             $rs = $DB->get_recordset('forum', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
00229             foreach ($rs as $f) {
00230                 $f->intro       = text_to_html($f->intro, false, false, true);
00231                 $f->introformat = FORMAT_HTML;
00232                 $DB->update_record('forum', $f);
00233                 upgrade_set_timeout();
00234             }
00235             $rs->close();
00236         }
00237 
00239         upgrade_mod_savepoint(true, 2009042003, 'forum');
00240     }
00241 
00243     if ($oldversion < 2009042700) {
00244 
00246         $table = new xmldb_table('forum');
00247         $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general', 'course');
00248 
00250         $dbman->drop_enum_from_field($table, $field);
00251 
00253         upgrade_mod_savepoint(true, 2009042700, 'forum');
00254     }
00255 
00256     if ($oldversion < 2009050400) {
00257 
00259         $DB->delete_records('forum_ratings', array('post' => 0));
00260 
00262         upgrade_mod_savepoint(true, 2009050400, 'forum');
00263     }
00264 
00265     if ($oldversion < 2010042800) {
00266         //migrate forumratings to the central rating table
00267         $table = new xmldb_table('forum_ratings');
00268         if ($dbman->table_exists($table)) {
00269             //forum ratings only have a single time column so use it for both time created and modified
00270             $sql = "INSERT INTO {rating} (contextid, scaleid, itemid, rating, userid, timecreated, timemodified)
00271 
00272                     SELECT cxt.id, f.scale, r.post AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified
00273                       FROM {forum_ratings} r
00274                       JOIN {forum_posts} p ON p.id=r.post
00275                       JOIN {forum_discussions} d ON d.id=p.discussion
00276                       JOIN {forum} f ON f.id=d.forum
00277                       JOIN {course_modules} cm ON cm.instance=f.id
00278                       JOIN {context} cxt ON cxt.instanceid=cm.id
00279                       JOIN {modules} m ON m.id=cm.module
00280                      WHERE m.name = :modname AND cxt.contextlevel = :contextlevel";
00281             $params['modname'] = 'forum';
00282             $params['contextlevel'] = CONTEXT_MODULE;
00283 
00284             $DB->execute($sql, $params);
00285 
00286             //now drop forum_ratings
00287             $dbman->drop_table($table);
00288         }
00289 
00290         upgrade_mod_savepoint(true, 2010042800, 'forum');
00291     }
00292 
00293     if ($oldversion < 2010070800) {
00294 
00295         // Remove the forum digests message provider MDL-23145
00296         $DB->delete_records('message_providers', array('name' => 'digests','component'=>'mod_forum'));
00297 
00298         // forum savepoint reached
00299         upgrade_mod_savepoint(true, 2010070800, 'forum');
00300     }
00301 
00302     if ($oldversion < 2010091900) {
00303         // rename files from borked upgrade in 2.0dev
00304         $fs = get_file_storage();
00305         $rs = $DB->get_recordset('files', array('component'=>'mod_form'));
00306         foreach ($rs as $oldrecord) {
00307             $file = $fs->get_file_instance($oldrecord);
00308             $newrecord = array('component'=>'mod_forum');
00309             if (!$fs->file_exists($oldrecord->contextid, 'mod_forum', $oldrecord->filearea, $oldrecord->itemid, $oldrecord->filepath, $oldrecord->filename)) {
00310                 $fs->create_file_from_storedfile($newrecord, $file);
00311             }
00312             $file->delete();
00313         }
00314         $rs->close();
00315         upgrade_mod_savepoint(true, 2010091900, 'forum');
00316     }
00317 
00318     if ($oldversion < 2011052300) {
00319         // rating.component and rating.ratingarea have now been added as mandatory fields.
00320         // Presently you can only rate forum posts so component = 'mod_forum' and ratingarea = 'post'
00321         // for all ratings with a forum context.
00322         // We want to update all ratings that belong to a forum context and don't already have a
00323         // component set.
00324         // This could take a while reset upgrade timeout to 5 min
00325         upgrade_set_timeout(60 * 20);
00326         $sql = "UPDATE {rating}
00327                 SET component = 'mod_forum', ratingarea = 'post'
00328                 WHERE contextid IN (
00329                     SELECT ctx.id
00330                       FROM {context} ctx
00331                       JOIN {course_modules} cm ON cm.id = ctx.instanceid
00332                       JOIN {modules} m ON m.id = cm.module
00333                      WHERE ctx.contextlevel = 70 AND
00334                            m.name = 'forum'
00335                 ) AND component = 'unknown'";
00336         $DB->execute($sql);
00337 
00338         upgrade_mod_savepoint(true, 2011052300, 'forum');
00339     }
00340 
00341     // Moodle v2.1.0 release upgrade line
00342     // Put any upgrade step following this
00343 
00344     // Moodle v2.2.0 release upgrade line
00345     // Put any upgrade step following this
00346 
00347     return true;
00348 }
00349 
00350 
 All Data Structures Namespaces Files Functions Variables Enumerations