Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 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.9.0 release upgrade line.
  51      // Put any upgrade step following this.
  52  
  53      if ($oldversion < 2020072100) {
  54          // Add index privatereplyto (not unique) to the forum_posts table.
  55          $table = new xmldb_table('forum_posts');
  56          $index = new xmldb_index('privatereplyto', XMLDB_INDEX_NOTUNIQUE, ['privatereplyto']);
  57  
  58          if (!$dbman->index_exists($table, $index)) {
  59              $dbman->add_index($table, $index);
  60          }
  61  
  62          upgrade_mod_savepoint(true, 2020072100, 'forum');
  63      }
  64  
  65      if ($oldversion < 2021101100) {
  66          // Add custom data to digest tasks to stop duplicates being created after this patch.
  67          $timenow = time();
  68  
  69          $sitetimezone = \core_date::get_server_timezone();
  70          $servermidnight = usergetmidnight($timenow, $sitetimezone);
  71          $digesttime = $servermidnight + ($CFG->digestmailtime * 3600);
  72          if ($digesttime < $timenow) {
  73              // Digest time is in the past. set for tomorrow.
  74              $servermidnight = usergetmidnight($timenow + DAYSECS, $sitetimezone);
  75          }
  76  
  77          $customdata = json_encode(['servermidnight' => $servermidnight]);
  78  
  79          $params = [
  80              'component' => 'mod_forum',
  81              'classname' => '\mod_forum\task\send_user_digests',
  82              'customdata' => '', // We do not want to overwrite any tasks that already have the custom data.
  83          ];
  84  
  85          $textfield = $DB->sql_compare_text('customdata', 1);
  86  
  87          $sql = "component = :component AND classname = :classname AND $textfield = :customdata";
  88  
  89          $DB->set_field_select('task_adhoc', 'customdata', $customdata, $sql, $params);
  90  
  91          upgrade_mod_savepoint(true, 2021101100, 'forum');
  92      }
  93  
  94      if ($oldversion < 2021101101) {
  95          // Remove the userid-forumid index as it gets replaces with forumid-userid.
  96          $table = new xmldb_table('forum_read');
  97          $index = new xmldb_index('userid-forumid', XMLDB_INDEX_NOTUNIQUE, ['userid', 'forumid']);
  98  
  99          // Conditionally launch drop index userid-forumid.
 100          if ($dbman->index_exists($table, $index)) {
 101              $dbman->drop_index($table, $index);
 102          }
 103  
 104          // Remove the userid-discussionid index as it gets replaced with discussionid-userid.
 105          $table = new xmldb_table('forum_read');
 106          $index = new xmldb_index('userid-discussionid', XMLDB_INDEX_NOTUNIQUE, ['userid', 'discussionid']);
 107  
 108          // Conditionally launch drop index userid-discussionid.
 109          if ($dbman->index_exists($table, $index)) {
 110              $dbman->drop_index($table, $index);
 111          }
 112  
 113          // Define index userid (not unique) to be added to forum_read.
 114          $table = new xmldb_table('forum_read');
 115          $index = new xmldb_index('userid', XMLDB_INDEX_NOTUNIQUE, ['userid']);
 116  
 117          // Conditionally launch add index userid.
 118          if (!$dbman->index_exists($table, $index)) {
 119              $dbman->add_index($table, $index);
 120          }
 121  
 122          // Build replacement indexes to replace the two dropped earlier.
 123          // Define index forumid-userid (not unique) to be added to forum_read.
 124          $table = new xmldb_table('forum_read');
 125          $index = new xmldb_index('forumid-userid', XMLDB_INDEX_NOTUNIQUE, ['forumid', 'userid']);
 126  
 127          // Conditionally launch add index forumid-userid.
 128          if (!$dbman->index_exists($table, $index)) {
 129              $dbman->add_index($table, $index);
 130          }
 131  
 132          // Define index discussionid-userid (not unique) to be added to forum_read.
 133          $table = new xmldb_table('forum_read');
 134          $index = new xmldb_index('discussionid-userid', XMLDB_INDEX_NOTUNIQUE, ['discussionid', 'userid']);
 135  
 136          // Conditionally launch add index discussionid-userid.
 137          if (!$dbman->index_exists($table, $index)) {
 138              $dbman->add_index($table, $index);
 139          }
 140  
 141          // Forum savepoint reached.
 142          upgrade_mod_savepoint(true, 2021101101, 'forum');
 143      }
 144  
 145      // Automatically generated Moodle v4.0.0 release upgrade line.
 146      // Put any upgrade step following this.
 147  
 148      if ($oldversion < 2022062700) {
 149          // Unset $CFG->forum_usecoursefullname.
 150          unset_config('forum_usecoursefullname');
 151  
 152          // Define field usecoursefullname to be dropped from forum.
 153          $table = new xmldb_table('forum');
 154          $field = new xmldb_field('usecoursefullname');
 155  
 156          // Conditionally launch drop field usecoursefullname.
 157          if ($dbman->field_exists($table, $field)) {
 158              $dbman->drop_field($table, $field);
 159          }
 160  
 161          // Forum savepoint reached.
 162          upgrade_mod_savepoint(true, 2022062700, 'forum');
 163      }
 164  
 165      if ($oldversion < 2022072900) {
 166          // Define key usermodified (foreign) to be added to forum_discussions.
 167          $table = new xmldb_table('forum_discussions');
 168          $key = new xmldb_key('usermodified', XMLDB_KEY_FOREIGN, ['usermodified'], 'user', ['id']);
 169          // Launch add key usermodified.
 170          $dbman->add_key($table, $key);
 171  
 172          // Forum savepoint reached.
 173          upgrade_mod_savepoint(true, 2022072900, 'forum');
 174      }
 175  
 176      // Automatically generated Moodle v4.1.0 release upgrade line.
 177      // Put any upgrade step following this.
 178  
 179      if ($oldversion < 2022112801) {
 180          // Some very old discussions from early Moodle versions may have the usermodified set to zero.
 181          $DB->execute("UPDATE {forum_discussions} SET usermodified = userid WHERE usermodified = 0");
 182  
 183          upgrade_mod_savepoint(true, 2022112801, 'forum');
 184      }
 185  
 186      return true;
 187  }