MDL-55982 mod_forum: Add time-based discussion locking
[moodle.git] / mod / forum / db / upgrade.php
CommitLineData
1adbd2c3 1<?php
8f685009 2// This file is part of Moodle - http://moodle.org/
b8a342d7 3//
8f685009
SH
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.
b8a342d7 8//
8f685009
SH
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.
b8a342d7 13//
8f685009
SH
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/**
18 * This file keeps track of upgrades to
19 * the forum module
20 *
21 * Sometimes, changes between versions involve
22 * alterations to database structures and other
23 * major things that may break installations.
24 *
25 * The upgrade function in this file will attempt
26 * to perform all the necessary actions to upgrade
2e0406a5 27 * your older installation to the current version.
8f685009
SH
28 *
29 * If there's something it cannot do itself, it
30 * will tell you what you need to do.
31 *
32 * The commands in here will all be database-neutral,
33 * using the methods of database_manager class
34 *
35 * Please do not forget to use upgrade_set_timeout()
36 * before any action that may take longer time to finish.
37 *
01030f1b 38 * @package mod_forum
8f685009
SH
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
41 */
b8a342d7 42
e8c82aac
EL
43defined('MOODLE_INTERNAL') || die();
44
775f811a 45function xmldb_forum_upgrade($oldversion) {
e8c82aac 46 global $CFG, $DB;
b8a342d7 47
506522d5 48 $dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.
b8a342d7 49
7a98bbab
KW
50 if ($oldversion < 2014051201) {
51
52 // Incorrect values that need to be replaced.
53 $replacements = array(
54 11 => 20,
55 12 => 50,
56 13 => 100
57 );
58
59 // Run the replacements.
60 foreach ($replacements as $old => $new) {
61 $DB->set_field('forum', 'maxattachments', $new, array('maxattachments' => $old));
62 }
63
64 // Forum savepoint reached.
65 upgrade_mod_savepoint(true, 2014051201, 'forum');
66 }
67
ba774b42
DP
68 if ($oldversion < 2014081500) {
69
70 // Define index course (not unique) to be added to forum_discussions.
71 $table = new xmldb_table('forum_discussions');
72 $index = new xmldb_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
73
74 // Conditionally launch add index course.
75 if (!$dbman->index_exists($table, $index)) {
76 $dbman->add_index($table, $index);
77 }
78
79 // Forum savepoint reached.
80 upgrade_mod_savepoint(true, 2014081500, 'forum');
81 }
82
83 if ($oldversion < 2014081900) {
361a41d3
AN
84
85 // Define table forum_discussion_subs to be created.
86 $table = new xmldb_table('forum_discussion_subs');
87
88 // Adding fields to table forum_discussion_subs.
89 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
90 $table->add_field('forum', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
91 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
92 $table->add_field('discussion', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
93 $table->add_field('preference', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1');
94
95 // Adding keys to table forum_discussion_subs.
96 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
97 $table->add_key('forum', XMLDB_KEY_FOREIGN, array('forum'), 'forum', array('id'));
98 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
99 $table->add_key('discussion', XMLDB_KEY_FOREIGN, array('discussion'), 'forum_discussions', array('id'));
100 $table->add_key('user_discussions', XMLDB_KEY_UNIQUE, array('userid', 'discussion'));
101
102 // Conditionally launch create table for forum_discussion_subs.
103 if (!$dbman->table_exists($table)) {
104 $dbman->create_table($table);
105 }
106
107 // Forum savepoint reached.
ba774b42 108 upgrade_mod_savepoint(true, 2014081900, 'forum');
73de21ff
MG
109 }
110
611a5ef7
AN
111 if ($oldversion < 2014103000) {
112 // Find records with multiple userid/postid combinations and find the lowest ID.
113 // Later we will remove all those which don't match this ID.
114 $sql = "
115 SELECT MIN(id) as lowid, userid, postid
116 FROM {forum_read}
117 GROUP BY userid, postid
118 HAVING COUNT(id) > 1";
119
120 if ($duplicatedrows = $DB->get_recordset_sql($sql)) {
121 foreach ($duplicatedrows as $row) {
122 $DB->delete_records_select('forum_read', 'userid = ? AND postid = ? AND id <> ?', array(
123 $row->userid,
124 $row->postid,
125 $row->lowid,
126 ));
127 }
128 }
129 $duplicatedrows->close();
130
131 // Forum savepoint reached.
132 upgrade_mod_savepoint(true, 2014103000, 'forum');
133 }
134
1a27142a 135 if ($oldversion < 2014110300) {
eb451c79
AN
136
137 // Changing precision of field preference on table forum_discussion_subs to (10).
138 $table = new xmldb_table('forum_discussion_subs');
139 $field = new xmldb_field('preference', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '1', 'discussion');
140
141 // Launch change of precision for field preference.
142 $dbman->change_field_precision($table, $field);
143
144 // Forum savepoint reached.
1a27142a 145 upgrade_mod_savepoint(true, 2014110300, 'forum');
73de21ff
MG
146 }
147
147bae8f
EL
148 // Moodle v2.8.0 release upgrade line.
149 // Put any upgrade step following this.
150
b758ab30
EL
151 // Moodle v2.9.0 release upgrade line.
152 // Put any upgrade step following this.
7774cf96
AN
153 if ($oldversion < 2015102900) {
154 // Groupid = 0 is never valid.
155 $DB->set_field('forum_discussions', 'groupid', -1, array('groupid' => 0));
156
157 // Forum savepoint reached.
158 upgrade_mod_savepoint(true, 2015102900, 'forum');
159 }
b758ab30 160
6d29c4ac
EL
161 // Moodle v3.0.0 release upgrade line.
162 // Put any upgrade step following this.
163
5f219cf1
BK
164 if ($oldversion < 2015120800) {
165
166 // Add support for pinned discussions.
167 $table = new xmldb_table('forum_discussions');
168 $field = new xmldb_field('pinned', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'timeend');
169
170 if (!$dbman->field_exists($table, $field)) {
171 $dbman->add_field($table, $field);
172 }
173
174 // Forum savepoint reached.
175 upgrade_mod_savepoint(true, 2015120800, 'forum');
176 }
4da854a6
EL
177 // Moodle v3.1.0 release upgrade line.
178 // Put any upgrade step following this.
179
0f3bbfd4
AN
180 if ($oldversion < 2016091200) {
181
182 // Define field lockdiscussionafter to be added to forum.
183 $table = new xmldb_table('forum');
184 $field = new xmldb_field('lockdiscussionafter', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'displaywordcount');
185
186 // Conditionally launch add field lockdiscussionafter.
187 if (!$dbman->field_exists($table, $field)) {
188 $dbman->add_field($table, $field);
189 }
190
191 // Forum savepoint reached.
192 upgrade_mod_savepoint(true, 2016091200, 'forum');
193 }
194
a4cdd6d2 195 return true;
b8a342d7 196}