Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   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  /**
  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
  27   * your older installation to the current version.
  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   *
  38   * @package   mod_forum
  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   */
  42  
  43  defined('MOODLE_INTERNAL') || die();
  44  
  45  function xmldb_forum_upgrade($oldversion) {
  46      global $CFG, $DB;
  47  
  48      $dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.
  49  
  50      // Automatically generated Moodle v3.6.0 release upgrade line.
  51      // Put any upgrade step following this.
  52  
  53      if ($oldversion < 2019031200) {
  54          // Define field privatereplyto to be added to forum_posts.
  55          $table = new xmldb_table('forum_posts');
  56          $field = new xmldb_field('privatereplyto', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'mailnow');
  57  
  58          // Conditionally launch add field privatereplyto.
  59          if (!$dbman->field_exists($table, $field)) {
  60              $dbman->add_field($table, $field);
  61          }
  62  
  63          upgrade_mod_savepoint(true, 2019031200, 'forum');
  64      }
  65  
  66      if ($oldversion < 2019040400) {
  67  
  68          $table = new xmldb_table('forum');
  69  
  70          // Define field duedate to be added to forum.
  71          $field = new xmldb_field('duedate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'introformat');
  72  
  73          // Conditionally launch add field duedate.
  74          if (!$dbman->field_exists($table, $field)) {
  75              $dbman->add_field($table, $field);
  76          }
  77  
  78          // Define field cutoffdate to be added to forum.
  79          $field = new xmldb_field('cutoffdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'duedate');
  80  
  81          // Conditionally launch add field cutoffdate.
  82          if (!$dbman->field_exists($table, $field)) {
  83              $dbman->add_field($table, $field);
  84          }
  85  
  86          upgrade_mod_savepoint(true, 2019040400, 'forum');
  87      }
  88  
  89      if ($oldversion < 2019040402) {
  90          // Define field deleted to be added to forum_posts.
  91          $table = new xmldb_table('forum_discussions');
  92          $field = new xmldb_field('timelocked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'pinned');
  93  
  94          // Conditionally launch add field deleted.
  95          if (!$dbman->field_exists($table, $field)) {
  96              $dbman->add_field($table, $field);
  97          }
  98  
  99          // Forum savepoint reached.
 100          upgrade_mod_savepoint(true, 2019040402, 'forum');
 101      }
 102  
 103      // Automatically generated Moodle v3.7.0 release upgrade line.
 104      // Put any upgrade step following this.
 105  
 106      if ($oldversion < 2019071901) {
 107  
 108          // Define field wordcount to be added to forum_posts.
 109          $table = new xmldb_table('forum_posts');
 110          $field = new xmldb_field('wordcount', XMLDB_TYPE_INTEGER, '20', null, null, null, null, 'privatereplyto');
 111  
 112          // Conditionally launch add field wordcount.
 113          if (!$dbman->field_exists($table, $field)) {
 114              $dbman->add_field($table, $field);
 115          }
 116  
 117          // Define field charcount to be added to forum_posts.
 118          $table = new xmldb_table('forum_posts');
 119          $field = new xmldb_field('charcount', XMLDB_TYPE_INTEGER, '20', null, null, null, null, 'wordcount');
 120  
 121          // Conditionally launch add field charcount.
 122          if (!$dbman->field_exists($table, $field)) {
 123              $dbman->add_field($table, $field);
 124          }
 125  
 126          // Forum savepoint reached.
 127          upgrade_mod_savepoint(true, 2019071901, 'forum');
 128      }
 129  
 130      if ($oldversion < 2019071902) {
 131          // Create adhoc task for upgrading of existing forum_posts.
 132          $record = new \stdClass();
 133          $record->classname = '\mod_forum\task\refresh_forum_post_counts';
 134          $record->component = 'mod_forum';
 135  
 136          // Next run time based from nextruntime computation in \core\task\manager::queue_adhoc_task().
 137          $nextruntime = time() - 1;
 138          $record->nextruntime = $nextruntime;
 139          $DB->insert_record('task_adhoc', $record);
 140  
 141          // Main savepoint reached.
 142          upgrade_mod_savepoint(true, 2019071902, 'forum');
 143      }
 144  
 145      if ($oldversion < 2019081100) {
 146  
 147          // Define field grade_forum to be added to forum.
 148          $table = new xmldb_table('forum');
 149          $field = new xmldb_field('grade_forum', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'scale');
 150  
 151          // Conditionally launch add field grade_forum.
 152          if (!$dbman->field_exists($table, $field)) {
 153              $dbman->add_field($table, $field);
 154          }
 155  
 156          // Forum savepoint reached.
 157          upgrade_mod_savepoint(true, 2019081100, 'forum');
 158  
 159      }
 160  
 161      if ($oldversion < 2019100100) {
 162          // Define table forum_grades to be created.
 163          $table = new xmldb_table('forum_grades');
 164  
 165          // Adding fields to table forum_grades.
 166          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 167          $table->add_field('forum', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 168          $table->add_field('itemnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 169          $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 170          $table->add_field('grade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
 171          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 172          $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 173  
 174          // Adding keys to table forum_grades.
 175          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 176          $table->add_key('forum', XMLDB_KEY_FOREIGN, ['forum'], 'forum', ['id']);
 177  
 178          // Adding indexes to table forum_grades.
 179          $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, ['userid']);
 180          $table->add_index('forumusergrade', XMLDB_INDEX_UNIQUE, ['forum', 'itemnumber', 'userid']);
 181  
 182          // Conditionally launch create table for forum_grades.
 183          if (!$dbman->table_exists($table)) {
 184              $dbman->create_table($table);
 185          }
 186  
 187          // Forum savepoint reached.
 188          upgrade_mod_savepoint(true, 2019100100, 'forum');
 189      }
 190  
 191      if ($oldversion < 2019100108) {
 192  
 193          // Define field sendstudentnotifications_forum to be added to forum.
 194          $table = new xmldb_table('forum');
 195          $field = new xmldb_field('sendstudentnotifications_forum', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0',
 196                  'grade_forum');
 197  
 198          // Conditionally launch add field sendstudentnotifications_forum.
 199          if (!$dbman->field_exists($table, $field)) {
 200              $dbman->add_field($table, $field);
 201          }
 202  
 203          // Forum savepoint reached.
 204          upgrade_mod_savepoint(true, 2019100108, 'forum');
 205      }
 206  
 207      if ($oldversion < 2019100109) {
 208  
 209          $table = new xmldb_table('forum');
 210          $field = new xmldb_field('sendstudentnotifications_forum');
 211          if ($dbman->field_exists($table, $field)) {
 212              $field->set_attributes(XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'grade_forum');
 213              $dbman->rename_field($table, $field, 'grade_forum_notify');
 214          }
 215  
 216          // Forum savepoint reached.
 217          upgrade_mod_savepoint(true, 2019100109, 'forum');
 218  
 219      }
 220  
 221      // Automatically generated Moodle v3.8.0 release upgrade line.
 222      // Put any upgrade step following this.
 223  
 224      if ($oldversion < 2019111801) {
 225          $sql = "SELECT d.id AS discussionid, p.userid AS correctuser
 226                  FROM {forum_discussions} d
 227                  INNER JOIN {forum_posts} p ON p.id = d.firstpost
 228                  WHERE d.userid <> p.userid";
 229          $recordset = $DB->get_recordset_sql($sql);
 230          foreach ($recordset as $record) {
 231              $object = new stdClass();
 232              $object->id = $record->discussionid;
 233              $object->userid = $record->correctuser;
 234              $DB->update_record('forum_discussions', $object);
 235          }
 236  
 237          $recordset->close();
 238  
 239          // Forum savepoint reached.
 240          upgrade_mod_savepoint(true, 2019111801, 'forum');
 241      }
 242  
 243      // Automatically generated Moodle v3.9.0 release upgrade line.
 244      // Put any upgrade step following this.
 245  
 246      if ($oldversion < 2020072100) {
 247          // Add index privatereplyto (not unique) to the forum_posts table.
 248          $table = new xmldb_table('forum_posts');
 249          $index = new xmldb_index('privatereplyto', XMLDB_INDEX_NOTUNIQUE, ['privatereplyto']);
 250  
 251          if (!$dbman->index_exists($table, $index)) {
 252              $dbman->add_index($table, $index);
 253          }
 254  
 255          upgrade_mod_savepoint(true, 2020072100, 'forum');
 256      }
 257  
 258      if ($oldversion < 2021101100) {
 259          // Add custom data to digest tasks to stop duplicates being created after this patch.
 260          $timenow = time();
 261  
 262          $sitetimezone = \core_date::get_server_timezone();
 263          $servermidnight = usergetmidnight($timenow, $sitetimezone);
 264          $digesttime = $servermidnight + ($CFG->digestmailtime * 3600);
 265          if ($digesttime < $timenow) {
 266              // Digest time is in the past. set for tomorrow.
 267              $servermidnight = usergetmidnight($timenow + DAYSECS, $sitetimezone);
 268          }
 269  
 270          $customdata = json_encode(['servermidnight' => $servermidnight]);
 271  
 272          $params = [
 273              'component' => 'mod_forum',
 274              'classname' => '\mod_forum\task\send_user_digests',
 275              'customdata' => '', // We do not want to overwrite any tasks that already have the custom data.
 276          ];
 277  
 278          $textfield = $DB->sql_compare_text('customdata', 1);
 279  
 280          $sql = "component = :component AND classname = :classname AND $textfield = :customdata";
 281  
 282          $DB->set_field_select('task_adhoc', 'customdata', $customdata, $sql, $params);
 283  
 284          upgrade_mod_savepoint(true, 2021101100, 'forum');
 285      }
 286  
 287      if ($oldversion < 2021101101) {
 288          // Remove the userid-forumid index as it gets replaces with forumid-userid.
 289          $table = new xmldb_table('forum_read');
 290          $index = new xmldb_index('userid-forumid', XMLDB_INDEX_NOTUNIQUE, ['userid', 'forumid']);
 291  
 292          // Conditionally launch drop index userid-forumid.
 293          if ($dbman->index_exists($table, $index)) {
 294              $dbman->drop_index($table, $index);
 295          }
 296  
 297          // Remove the userid-discussionid index as it gets replaced with discussionid-userid.
 298          $table = new xmldb_table('forum_read');
 299          $index = new xmldb_index('userid-discussionid', XMLDB_INDEX_NOTUNIQUE, ['userid', 'discussionid']);
 300  
 301          // Conditionally launch drop index userid-discussionid.
 302          if ($dbman->index_exists($table, $index)) {
 303              $dbman->drop_index($table, $index);
 304          }
 305  
 306          // Define index userid (not unique) to be added to forum_read.
 307          $table = new xmldb_table('forum_read');
 308          $index = new xmldb_index('userid', XMLDB_INDEX_NOTUNIQUE, ['userid']);
 309  
 310          // Conditionally launch add index userid.
 311          if (!$dbman->index_exists($table, $index)) {
 312              $dbman->add_index($table, $index);
 313          }
 314  
 315          // Build replacement indexes to replace the two dropped earlier.
 316          // Define index forumid-userid (not unique) to be added to forum_read.
 317          $table = new xmldb_table('forum_read');
 318          $index = new xmldb_index('forumid-userid', XMLDB_INDEX_NOTUNIQUE, ['forumid', 'userid']);
 319  
 320          // Conditionally launch add index forumid-userid.
 321          if (!$dbman->index_exists($table, $index)) {
 322              $dbman->add_index($table, $index);
 323          }
 324  
 325          // Define index discussionid-userid (not unique) to be added to forum_read.
 326          $table = new xmldb_table('forum_read');
 327          $index = new xmldb_index('discussionid-userid', XMLDB_INDEX_NOTUNIQUE, ['discussionid', 'userid']);
 328  
 329          // Conditionally launch add index discussionid-userid.
 330          if (!$dbman->index_exists($table, $index)) {
 331              $dbman->add_index($table, $index);
 332          }
 333  
 334          // Forum savepoint reached.
 335          upgrade_mod_savepoint(true, 2021101101, 'forum');
 336      }
 337  
 338      // Automatically generated Moodle v4.0.0 release upgrade line.
 339      // Put any upgrade step following this.
 340  
 341      if ($oldversion < 2022041901) {
 342          // Some very old discussions from early Moodle versions may have the usermodified set to zero.
 343          $DB->execute("UPDATE {forum_discussions} SET usermodified = userid WHERE usermodified = 0");
 344  
 345          upgrade_mod_savepoint(true, 2022041901, 'forum');
 346      }
 347  
 348      return true;
 349  }