PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/forum/db/upgrade.php

https://github.com/plymouthstate/moodle
PHP | 350 lines | 195 code | 67 blank | 88 comment | 28 complexity | 0574590517adea130bd5eab4b2955f66 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This file keeps track of upgrades to
  18. * the forum module
  19. *
  20. * Sometimes, changes between versions involve
  21. * alterations to database structures and other
  22. * major things that may break installations.
  23. *
  24. * The upgrade function in this file will attempt
  25. * to perform all the necessary actions to upgrade
  26. * your older installation to the current version.
  27. *
  28. * If there's something it cannot do itself, it
  29. * will tell you what you need to do.
  30. *
  31. * The commands in here will all be database-neutral,
  32. * using the methods of database_manager class
  33. *
  34. * Please do not forget to use upgrade_set_timeout()
  35. * before any action that may take longer time to finish.
  36. *
  37. * @package mod-forum
  38. * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  39. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40. */
  41. function xmldb_forum_upgrade($oldversion) {
  42. global $CFG, $DB, $OUTPUT;
  43. $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
  44. //===== 1.9.0 upgrade line ======//
  45. if ($oldversion < 2007101511) {
  46. //MDL-13866 - send forum ratins to gradebook again
  47. require_once($CFG->dirroot.'/mod/forum/lib.php');
  48. forum_upgrade_grades();
  49. upgrade_mod_savepoint(true, 2007101511, 'forum');
  50. }
  51. if ($oldversion < 2008072800) {
  52. /// Define field completiondiscussions to be added to forum
  53. $table = new xmldb_table('forum');
  54. $field = new xmldb_field('completiondiscussions');
  55. $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'blockperiod');
  56. /// Launch add field completiondiscussions
  57. if(!$dbman->field_exists($table,$field)) {
  58. $dbman->add_field($table, $field);
  59. }
  60. $field = new xmldb_field('completionreplies');
  61. $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiondiscussions');
  62. /// Launch add field completionreplies
  63. if(!$dbman->field_exists($table,$field)) {
  64. $dbman->add_field($table, $field);
  65. }
  66. /// Define field completionposts to be added to forum
  67. $field = new xmldb_field('completionposts');
  68. $field->set_attributes(XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionreplies');
  69. /// Launch add field completionposts
  70. if(!$dbman->field_exists($table,$field)) {
  71. $dbman->add_field($table, $field);
  72. }
  73. upgrade_mod_savepoint(true, 2008072800, 'forum');
  74. }
  75. if ($oldversion < 2008081900) {
  76. /////////////////////////////////////
  77. /// new file storage upgrade code ///
  78. /////////////////////////////////////
  79. $fs = get_file_storage();
  80. $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
  81. $sqlfrom = "FROM {forum_posts} p
  82. JOIN {forum_discussions} d ON d.id = p.discussion
  83. JOIN {forum} f ON f.id = d.forum
  84. JOIN {modules} m ON m.name = 'forum'
  85. JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = f.id)
  86. WHERE p.attachment <> '$empty' AND p.attachment <> '1'";
  87. $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
  88. $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");
  89. if ($rs->valid()) {
  90. $pbar = new progress_bar('migrateforumfiles', 500, true);
  91. $i = 0;
  92. foreach ($rs as $post) {
  93. $i++;
  94. upgrade_set_timeout(60); // set up timeout, may also abort execution
  95. $pbar->update($i, $count, "Migrating forum posts - $i/$count.");
  96. $filepath = "$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum/$post->id/$post->attachment";
  97. if (!is_readable($filepath)) {
  98. //file missing??
  99. echo $OUTPUT->notification("File not readable, skipping: ".$filepath);
  100. $post->attachment = '';
  101. $DB->update_record('forum_posts', $post);
  102. continue;
  103. }
  104. $context = get_context_instance(CONTEXT_MODULE, $post->cmid);
  105. $filearea = 'attachment';
  106. $filename = clean_param($post->attachment, PARAM_FILE);
  107. if ($filename === '') {
  108. echo $OUTPUT->notification("Unsupported post filename, skipping: ".$filepath);
  109. $post->attachment = '';
  110. $DB->update_record('forum_posts', $post);
  111. continue;
  112. }
  113. if (!$fs->file_exists($context->id, 'mod_forum', $filearea, $post->id, '/', $filename)) {
  114. $file_record = array('contextid'=>$context->id, 'component'=>'mod_forum', 'filearea'=>$filearea, 'itemid'=>$post->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$post->userid);
  115. if ($fs->create_file_from_pathname($file_record, $filepath)) {
  116. $post->attachment = '1';
  117. $DB->update_record('forum_posts', $post);
  118. unlink($filepath);
  119. }
  120. }
  121. // remove dirs if empty
  122. @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum/$post->id");
  123. @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum");
  124. @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum");
  125. }
  126. }
  127. $rs->close();
  128. upgrade_mod_savepoint(true, 2008081900, 'forum');
  129. }
  130. if ($oldversion < 2008090800) {
  131. /// Define field maxattachments to be added to forum
  132. $table = new xmldb_table('forum');
  133. $field = new xmldb_field('maxattachments', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'maxbytes');
  134. /// Conditionally launch add field maxattachments
  135. if (!$dbman->field_exists($table, $field)) {
  136. $dbman->add_field($table, $field);
  137. }
  138. /// forum savepoint reached
  139. upgrade_mod_savepoint(true, 2008090800, 'forum');
  140. }
  141. if ($oldversion < 2009042000) {
  142. /// Rename field format on table forum_posts to messageformat
  143. $table = new xmldb_table('forum_posts');
  144. $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'message');
  145. /// Launch rename field format
  146. $dbman->rename_field($table, $field, 'messageformat');
  147. /// forum savepoint reached
  148. upgrade_mod_savepoint(true, 2009042000, 'forum');
  149. }
  150. if ($oldversion < 2009042001) {
  151. /// Define field messagetrust to be added to forum_posts
  152. $table = new xmldb_table('forum_posts');
  153. $field = new xmldb_field('messagetrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'messageformat');
  154. /// Launch add field messagetrust
  155. $dbman->add_field($table, $field);
  156. /// forum savepoint reached
  157. upgrade_mod_savepoint(true, 2009042001, 'forum');
  158. }
  159. if ($oldversion < 2009042002) {
  160. $trustmark = '#####TRUSTTEXT#####';
  161. $rs = $DB->get_recordset_sql("SELECT * FROM {forum_posts} WHERE message LIKE ?", array($trustmark.'%'));
  162. foreach ($rs as $post) {
  163. if (strpos($post->message, $trustmark) !== 0) {
  164. // probably lowercase in some DBs?
  165. continue;
  166. }
  167. $post->message = str_replace($trustmark, '', $post->message);
  168. $post->messagetrust = 1;
  169. $DB->update_record('forum_posts', $post);
  170. }
  171. $rs->close();
  172. /// forum savepoint reached
  173. upgrade_mod_savepoint(true, 2009042002, 'forum');
  174. }
  175. if ($oldversion < 2009042003) {
  176. /// Define field introformat to be added to forum
  177. $table = new xmldb_table('forum');
  178. $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
  179. /// Launch add field introformat
  180. if (!$dbman->field_exists($table, $field)) {
  181. $dbman->add_field($table, $field);
  182. }
  183. // conditionally migrate to html format in intro
  184. if ($CFG->texteditors !== 'textarea') {
  185. $rs = $DB->get_recordset('forum', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
  186. foreach ($rs as $f) {
  187. $f->intro = text_to_html($f->intro, false, false, true);
  188. $f->introformat = FORMAT_HTML;
  189. $DB->update_record('forum', $f);
  190. upgrade_set_timeout();
  191. }
  192. $rs->close();
  193. }
  194. /// forum savepoint reached
  195. upgrade_mod_savepoint(true, 2009042003, 'forum');
  196. }
  197. /// Dropping all enums/check contraints from core. MDL-18577
  198. if ($oldversion < 2009042700) {
  199. /// Changing list of values (enum) of field type on table forum to none
  200. $table = new xmldb_table('forum');
  201. $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general', 'course');
  202. /// Launch change of list of values for field type
  203. $dbman->drop_enum_from_field($table, $field);
  204. /// forum savepoint reached
  205. upgrade_mod_savepoint(true, 2009042700, 'forum');
  206. }
  207. if ($oldversion < 2009050400) {
  208. /// Clean existing wrong rates. MDL-18227
  209. $DB->delete_records('forum_ratings', array('post' => 0));
  210. /// forum savepoint reached
  211. upgrade_mod_savepoint(true, 2009050400, 'forum');
  212. }
  213. if ($oldversion < 2010042800) {
  214. //migrate forumratings to the central rating table
  215. $table = new xmldb_table('forum_ratings');
  216. if ($dbman->table_exists($table)) {
  217. //forum ratings only have a single time column so use it for both time created and modified
  218. $sql = "INSERT INTO {rating} (contextid, scaleid, itemid, rating, userid, timecreated, timemodified)
  219. SELECT cxt.id, f.scale, r.post AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified
  220. FROM {forum_ratings} r
  221. JOIN {forum_posts} p ON p.id=r.post
  222. JOIN {forum_discussions} d ON d.id=p.discussion
  223. JOIN {forum} f ON f.id=d.forum
  224. JOIN {course_modules} cm ON cm.instance=f.id
  225. JOIN {context} cxt ON cxt.instanceid=cm.id
  226. JOIN {modules} m ON m.id=cm.module
  227. WHERE m.name = :modname AND cxt.contextlevel = :contextlevel";
  228. $params['modname'] = 'forum';
  229. $params['contextlevel'] = CONTEXT_MODULE;
  230. $DB->execute($sql, $params);
  231. //now drop forum_ratings
  232. $dbman->drop_table($table);
  233. }
  234. upgrade_mod_savepoint(true, 2010042800, 'forum');
  235. }
  236. if ($oldversion < 2010070800) {
  237. // Remove the forum digests message provider MDL-23145
  238. $DB->delete_records('message_providers', array('name' => 'digests','component'=>'mod_forum'));
  239. // forum savepoint reached
  240. upgrade_mod_savepoint(true, 2010070800, 'forum');
  241. }
  242. if ($oldversion < 2010091900) {
  243. // rename files from borked upgrade in 2.0dev
  244. $fs = get_file_storage();
  245. $rs = $DB->get_recordset('files', array('component'=>'mod_form'));
  246. foreach ($rs as $oldrecord) {
  247. $file = $fs->get_file_instance($oldrecord);
  248. $newrecord = array('component'=>'mod_forum');
  249. if (!$fs->file_exists($oldrecord->contextid, 'mod_forum', $oldrecord->filearea, $oldrecord->itemid, $oldrecord->filepath, $oldrecord->filename)) {
  250. $fs->create_file_from_storedfile($newrecord, $file);
  251. }
  252. $file->delete();
  253. }
  254. $rs->close();
  255. upgrade_mod_savepoint(true, 2010091900, 'forum');
  256. }
  257. if ($oldversion < 2011052300) {
  258. // rating.component and rating.ratingarea have now been added as mandatory fields.
  259. // Presently you can only rate forum posts so component = 'mod_forum' and ratingarea = 'post'
  260. // for all ratings with a forum context.
  261. // We want to update all ratings that belong to a forum context and don't already have a
  262. // component set.
  263. // This could take a while reset upgrade timeout to 5 min
  264. upgrade_set_timeout(60 * 20);
  265. $sql = "UPDATE {rating}
  266. SET component = 'mod_forum', ratingarea = 'post'
  267. WHERE contextid IN (
  268. SELECT ctx.id
  269. FROM {context} ctx
  270. JOIN {course_modules} cm ON cm.id = ctx.instanceid
  271. JOIN {modules} m ON m.id = cm.module
  272. WHERE ctx.contextlevel = 70 AND
  273. m.name = 'forum'
  274. ) AND component = 'unknown'";
  275. $DB->execute($sql);
  276. upgrade_mod_savepoint(true, 2011052300, 'forum');
  277. }
  278. // Moodle v2.1.0 release upgrade line
  279. // Put any upgrade step following this
  280. // Moodle v2.2.0 release upgrade line
  281. // Put any upgrade step following this
  282. return true;
  283. }