2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file keeps track of upgrades to
21 * Sometimes, changes between versions involve
22 * alterations to database structures and other
23 * major things that may break installations.
25 * The upgrade function in this file will attempt
26 * to perform all the necessary actions to upgrade
27 * your older installation to the current version.
29 * If there's something it cannot do itself, it
30 * will tell you what you need to do.
32 * The commands in here will all be database-neutral,
33 * using the methods of database_manager class
35 * Please do not forget to use upgrade_set_timeout()
36 * before any action that may take longer time to finish.
39 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 defined('MOODLE_INTERNAL') ||
die();
45 function xmldb_forum_upgrade($oldversion) {
48 $dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.
50 if ($oldversion < 2016091200) {
52 // Define field lockdiscussionafter to be added to forum.
53 $table = new xmldb_table('forum');
54 $field = new xmldb_field('lockdiscussionafter', XMLDB_TYPE_INTEGER
, '10', null, XMLDB_NOTNULL
, null, '0', 'displaywordcount');
56 // Conditionally launch add field lockdiscussionafter.
57 if (!$dbman->field_exists($table, $field)) {
58 $dbman->add_field($table, $field);
61 // Forum savepoint reached.
62 upgrade_mod_savepoint(true, 2016091200, 'forum');
65 // Automatically generated Moodle v3.2.0 release upgrade line.
66 // Put any upgrade step following this.
68 // Automatically generated Moodle v3.3.0 release upgrade line.
69 // Put any upgrade step following this.
71 if ($oldversion < 2017092200) {
73 // Remove duplicate entries from forum_subscriptions.
74 // Find records with multiple userid/forum combinations and find the highest ID.
75 // Later we will remove all those entries.
77 SELECT MIN(id) as minid, userid, forum
78 FROM {forum_subscriptions}
79 GROUP BY userid, forum
80 HAVING COUNT(id) > 1";
82 if ($duplicatedrows = $DB->get_recordset_sql($sql)) {
83 foreach ($duplicatedrows as $row) {
84 $DB->delete_records_select('forum_subscriptions',
85 'userid = :userid AND forum = :forum AND id <> :minid', (array)$row);
88 $duplicatedrows->close();
90 // Define key useridforum (primary) to be added to forum_subscriptions.
91 $table = new xmldb_table('forum_subscriptions');
92 $key = new xmldb_key('useridforum', XMLDB_KEY_UNIQUE
, array('userid', 'forum'));
94 // Launch add key useridforum.
95 $dbman->add_key($table, $key);
97 // Forum savepoint reached.
98 upgrade_mod_savepoint(true, 2017092200, 'forum');
101 // Automatically generated Moodle v3.4.0 release upgrade line.
102 // Put any upgrade step following this.
104 if ($oldversion < 2018032900) {
106 // Define field deleted to be added to forum_posts.
107 $table = new xmldb_table('forum_posts');
108 $field = new xmldb_field('deleted', XMLDB_TYPE_INTEGER
, '1', null, XMLDB_NOTNULL
, null, '0', 'mailnow');
110 // Conditionally launch add field deleted.
111 if (!$dbman->field_exists($table, $field)) {
112 $dbman->add_field($table, $field);
115 // Forum savepoint reached.
116 upgrade_mod_savepoint(true, 2018032900, 'forum');
119 // Automatically generated Moodle v3.5.0 release upgrade line.
120 // Put any upgrade step following this.