Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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.5.0 release upgrade line.
  51      // Put any upgrade step following this.
  52  
  53      // Automatically generated Moodle v3.6.0 release upgrade line.
  54      // Put any upgrade step following this.
  55  
  56      if ($oldversion < 2019031200) {
  57          // Define field privatereplyto to be added to forum_posts.
  58          $table = new xmldb_table('forum_posts');
  59          $field = new xmldb_field('privatereplyto', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'mailnow');
  60  
  61          // Conditionally launch add field privatereplyto.
  62          if (!$dbman->field_exists($table, $field)) {
  63              $dbman->add_field($table, $field);
  64          }
  65  
  66          upgrade_mod_savepoint(true, 2019031200, 'forum');
  67      }
  68  
  69      if ($oldversion < 2019040400) {
  70  
  71          $table = new xmldb_table('forum');
  72  
  73          // Define field duedate to be added to forum.
  74          $field = new xmldb_field('duedate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'introformat');
  75  
  76          // Conditionally launch add field duedate.
  77          if (!$dbman->field_exists($table, $field)) {
  78              $dbman->add_field($table, $field);
  79          }
  80  
  81          // Define field cutoffdate to be added to forum.
  82          $field = new xmldb_field('cutoffdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'duedate');
  83  
  84          // Conditionally launch add field cutoffdate.
  85          if (!$dbman->field_exists($table, $field)) {
  86              $dbman->add_field($table, $field);
  87          }
  88  
  89          upgrade_mod_savepoint(true, 2019040400, 'forum');
  90      }
  91  
  92      if ($oldversion < 2019040402) {
  93          // Define field deleted to be added to forum_posts.
  94          $table = new xmldb_table('forum_discussions');
  95          $field = new xmldb_field('timelocked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'pinned');
  96  
  97          // Conditionally launch add field deleted.
  98          if (!$dbman->field_exists($table, $field)) {
  99              $dbman->add_field($table, $field);
 100          }
 101  
 102          // Forum savepoint reached.
 103          upgrade_mod_savepoint(true, 2019040402, 'forum');
 104      }
 105  
 106      // Automatically generated Moodle v3.7.0 release upgrade line.
 107      // Put any upgrade step following this.
 108  
 109      if ($oldversion < 2019071901) {
 110  
 111          // Define field wordcount to be added to forum_posts.
 112          $table = new xmldb_table('forum_posts');
 113          $field = new xmldb_field('wordcount', XMLDB_TYPE_INTEGER, '20', null, null, null, null, 'privatereplyto');
 114  
 115          // Conditionally launch add field wordcount.
 116          if (!$dbman->field_exists($table, $field)) {
 117              $dbman->add_field($table, $field);
 118          }
 119  
 120          // Define field charcount to be added to forum_posts.
 121          $table = new xmldb_table('forum_posts');
 122          $field = new xmldb_field('charcount', XMLDB_TYPE_INTEGER, '20', null, null, null, null, 'wordcount');
 123  
 124          // Conditionally launch add field charcount.
 125          if (!$dbman->field_exists($table, $field)) {
 126              $dbman->add_field($table, $field);
 127          }
 128  
 129          // Forum savepoint reached.
 130          upgrade_mod_savepoint(true, 2019071901, 'forum');
 131      }
 132  
 133      if ($oldversion < 2019071902) {
 134          // Create adhoc task for upgrading of existing forum_posts.
 135          $record = new \stdClass();
 136          $record->classname = '\mod_forum\task\refresh_forum_post_counts';
 137          $record->component = 'mod_forum';
 138  
 139          // Next run time based from nextruntime computation in \core\task\manager::queue_adhoc_task().
 140          $nextruntime = time() - 1;
 141          $record->nextruntime = $nextruntime;
 142          $DB->insert_record('task_adhoc', $record);
 143  
 144          // Main savepoint reached.
 145          upgrade_mod_savepoint(true, 2019071902, 'forum');
 146      }
 147  
 148      if ($oldversion < 2019081100) {
 149  
 150          // Define field grade_forum to be added to forum.
 151          $table = new xmldb_table('forum');
 152          $field = new xmldb_field('grade_forum', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'scale');
 153  
 154          // Conditionally launch add field grade_forum.
 155          if (!$dbman->field_exists($table, $field)) {
 156              $dbman->add_field($table, $field);
 157          }
 158  
 159          // Forum savepoint reached.
 160          upgrade_mod_savepoint(true, 2019081100, 'forum');
 161  
 162      }
 163  
 164      if ($oldversion < 2019100100) {
 165          // Define table forum_grades to be created.
 166          $table = new xmldb_table('forum_grades');
 167  
 168          // Adding fields to table forum_grades.
 169          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 170          $table->add_field('forum', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 171          $table->add_field('itemnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 172          $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 173          $table->add_field('grade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
 174          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 175          $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 176  
 177          // Adding keys to table forum_grades.
 178          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 179          $table->add_key('forum', XMLDB_KEY_FOREIGN, ['forum'], 'forum', ['id']);
 180  
 181          // Adding indexes to table forum_grades.
 182          $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, ['userid']);
 183          $table->add_index('forumusergrade', XMLDB_INDEX_UNIQUE, ['forum', 'itemnumber', 'userid']);
 184  
 185          // Conditionally launch create table for forum_grades.
 186          if (!$dbman->table_exists($table)) {
 187              $dbman->create_table($table);
 188          }
 189  
 190          // Forum savepoint reached.
 191          upgrade_mod_savepoint(true, 2019100100, 'forum');
 192      }
 193  
 194      if ($oldversion < 2019100108) {
 195  
 196          // Define field sendstudentnotifications_forum to be added to forum.
 197          $table = new xmldb_table('forum');
 198          $field = new xmldb_field('sendstudentnotifications_forum', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0',
 199                  'grade_forum');
 200  
 201          // Conditionally launch add field sendstudentnotifications_forum.
 202          if (!$dbman->field_exists($table, $field)) {
 203              $dbman->add_field($table, $field);
 204          }
 205  
 206          // Forum savepoint reached.
 207          upgrade_mod_savepoint(true, 2019100108, 'forum');
 208      }
 209  
 210      if ($oldversion < 2019100109) {
 211  
 212          $table = new xmldb_table('forum');
 213          $field = new xmldb_field('sendstudentnotifications_forum');
 214          if ($dbman->field_exists($table, $field)) {
 215              $field->set_attributes(XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'grade_forum');
 216              $dbman->rename_field($table, $field, 'grade_forum_notify');
 217          }
 218  
 219          // Forum savepoint reached.
 220          upgrade_mod_savepoint(true, 2019100109, 'forum');
 221  
 222      }
 223  
 224      // Automatically generated Moodle v3.8.0 release upgrade line.
 225      // Put any upgrade step following this.
 226  
 227      if ($oldversion < 2019111801) {
 228          $sql = "SELECT d.id AS discussionid, p.userid AS correctuser
 229                  FROM {forum_discussions} d
 230                  INNER JOIN {forum_posts} p ON p.id = d.firstpost
 231                  WHERE d.userid <> p.userid";
 232          $recordset = $DB->get_recordset_sql($sql);
 233          foreach ($recordset as $record) {
 234              $object = new stdClass();
 235              $object->id = $record->discussionid;
 236              $object->userid = $record->correctuser;
 237              $DB->update_record('forum_discussions', $object);
 238          }
 239  
 240          $recordset->close();
 241  
 242          // Forum savepoint reached.
 243          upgrade_mod_savepoint(true, 2019111801, 'forum');
 244      }
 245  
 246      // Automatically generated Moodle v3.9.0 release upgrade line.
 247      // Put any upgrade step following this.
 248  
 249      if ($oldversion < 2020072100) {
 250          // Add index privatereplyto (not unique) to the forum_posts table.
 251          $table = new xmldb_table('forum_posts');
 252          $index = new xmldb_index('privatereplyto', XMLDB_INDEX_NOTUNIQUE, ['privatereplyto']);
 253  
 254          if (!$dbman->index_exists($table, $index)) {
 255              $dbman->add_index($table, $index);
 256          }
 257  
 258          upgrade_mod_savepoint(true, 2020072100, 'forum');
 259      }
 260  
 261      // Automatically generated Moodle v3.10.0 release upgrade line.
 262      // Put any upgrade step following this.
 263  
 264      if ($oldversion < 2020110901) {
 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, 2020110901, 'forum');
 291      }
 292  
 293      return true;
 294  }