Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 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   * Upgrade code for install
  19   *
  20   * @package   tool_usertours
  21   * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use tool_usertours\manager;
  28  use tool_usertours\tour;
  29  
  30  /**
  31   * Upgrade the user tours plugin.
  32   *
  33   * @param int $oldversion The old version of the user tours plugin
  34   * @return bool
  35   */
  36  function xmldb_tool_usertours_upgrade($oldversion) {
  37      global $CFG, $DB;
  38  
  39      $dbman = $DB->get_manager();
  40  
  41      // Automatically generated Moodle v3.9.0 release upgrade line.
  42      // Put any upgrade step following this.
  43  
  44      if ($oldversion < 2021052501) {
  45          // Clean up user preferences of deleted tours.
  46          $select = $DB->sql_like('name', ':lastcompleted') . ' OR ' . $DB->sql_like('name', ':requested');
  47          $params = [
  48              'lastcompleted' => tour::TOUR_LAST_COMPLETED_BY_USER . '%',
  49              'requested' => tour::TOUR_REQUESTED_BY_USER . '%',
  50          ];
  51  
  52          $preferences = $DB->get_records_select('user_preferences', $select, $params, '', 'DISTINCT name');
  53          foreach ($preferences as $preference) {
  54              // Match tour ID at the end of the preference name, remove all of that preference type if tour ID doesn't exist.
  55              if (preg_match('/(?<tourid>\d+)$/', $preference->name, $matches) &&
  56                      !$DB->record_exists('tool_usertours_tours', ['id' => $matches['tourid']])) {
  57  
  58                  $DB->delete_records('user_preferences', ['name' => $preference->name]);
  59              }
  60          }
  61  
  62          upgrade_plugin_savepoint(true, 2021052501, 'tool', 'usertours');
  63      }
  64  
  65      if ($oldversion < 2021092300) {
  66          // Define field endtourlabel to be added to tool_usertours_tours.
  67          $table = new xmldb_table('tool_usertours_tours');
  68          $field = new xmldb_field('endtourlabel', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'sortorder');
  69  
  70          // Conditionally launch add field endtourlabel.
  71          if (!$dbman->field_exists($table, $field)) {
  72              $dbman->add_field($table, $field);
  73          }
  74  
  75          // Usertours savepoint reached.
  76          upgrade_plugin_savepoint(true, 2021092300, 'tool', 'usertours');
  77      }
  78  
  79      if ($oldversion < 2021100700) {
  80  
  81          // Define field displaystepnumbers to be added to tool_usertours_tours.
  82          $table = new xmldb_table('tool_usertours_tours');
  83          $field = new xmldb_field('displaystepnumbers', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'configdata');
  84  
  85          // Conditionally launch add field displaystepnumbers.
  86          if (!$dbman->field_exists($table, $field)) {
  87              $dbman->add_field($table, $field);
  88          }
  89  
  90          // Usertours savepoint reached.
  91          upgrade_plugin_savepoint(true, 2021100700, 'tool', 'usertours');
  92      }
  93  
  94      if ($oldversion < 2022040601) {
  95          // Define field contentformat to be added to tool_usertours_steps.
  96          $table = new xmldb_table('tool_usertours_steps');
  97          $field = new xmldb_field('contentformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, FORMAT_MOODLE, 'content');
  98  
  99          // Conditionally launch add field contentformat.
 100          if (!$dbman->field_exists($table, $field)) {
 101              $dbman->add_field($table, $field);
 102          } else {
 103              // Field was added by previous upgrade step with the default value is FORMAT_HTML.
 104              // Need to drop the field and re-create with the new structure to make sure all the existing tours use FORMAT_MOODLE.
 105              // FORMAT_MOODLE will force the \core_external\util::format_text method to use nl2br to
 106              // convert the new line to line break tag.
 107              $dbman->drop_field($table, $field);
 108              // Add the field again.
 109              $dbman->add_field($table, $field);
 110          }
 111  
 112          // Usertours savepoint reached.
 113          upgrade_plugin_savepoint(true, 2022040601, 'tool', 'usertours');
 114      }
 115  
 116      if ($oldversion < 2022040602) {
 117          // Update shipped tours.
 118          // Normally, we just bump the version numbers because we need to call update_shipped_tours only once.
 119          manager::update_shipped_tours();
 120  
 121          upgrade_plugin_savepoint(true, 2022040602, 'tool', 'usertours');
 122      }
 123  
 124      if ($oldversion < 2022061600) {
 125          // Update shipped tours.
 126          // Normally, we just bump the version numbers because we need to call update_shipped_tours only once.
 127          manager::update_shipped_tours();
 128  
 129          upgrade_plugin_savepoint(true, 2022061600, 'tool', 'usertours');
 130      }
 131  
 132      // Automatically generated Moodle v4.0.0 release upgrade line.
 133      // Put any upgrade step following this.
 134  
 135      // Automatically generated Moodle v4.1.0 release upgrade line.
 136      // Put any upgrade step following this.
 137  
 138      // Automatically generated Moodle v4.2.0 release upgrade line.
 139      // Put any upgrade step following this.
 140  
 141      return true;
 142  }