Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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      // Automatically generated Moodle v3.10.0 release upgrade line.
 259      // Put any upgrade step following this.
 260  
 261      // Automatically generated Moodle v3.11.0 release upgrade line.
 262      // Put any upgrade step following this.
 263  
 264      if ($oldversion < 2021051701) {
 265          // Add custom data to digest tasks to stop duplicates being created after this patch.
 266          $timenow = time();
 267  
 268          $sitetimezone = \core_date::get_server_timezone();
 269          $servermidnight = usergetmidnight($timenow, $sitetimezone);
 270          $digesttime = $servermidnight + ($CFG->digestmailtime * 3600);
 271          if ($digesttime < $timenow) {
 272              // Digest time is in the past. set for tomorrow.
 273              $servermidnight = usergetmidnight($timenow + DAYSECS, $sitetimezone);
 274          }
 275  
 276          $customdata = json_encode(['servermidnight' => $servermidnight]);
 277  
 278          $params = [
 279              'component' => 'mod_forum',
 280              'classname' => '\mod_forum\task\send_user_digests',
 281              'customdata' => '', // We do not want to overwrite any tasks that already have the custom data.
 282          ];
 283  
 284          $textfield = $DB->sql_compare_text('customdata', 1);
 285  
 286          $sql = "component = :component AND classname = :classname AND $textfield = :customdata";
 287  
 288          $DB->set_field_select('task_adhoc', 'customdata', $customdata, $sql, $params);
 289  
 290          upgrade_mod_savepoint(true, 2021051701, 'forum');
 291      }
 292  
 293      return true;
 294  }