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 the myoverview block
  19   *
  20   * @since 3.8
  21   * @package block_myoverview
  22   * @copyright 2019 Jake Dallimore <jrhdallimore@gmail.com>
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once("{$CFG->dirroot}/my/lib.php");
  29  require_once("{$CFG->libdir}/db/upgradelib.php");
  30  
  31  /**
  32   * Upgrade code for the MyOverview block.
  33   *
  34   * @param int $oldversion
  35   */
  36  function xmldb_block_myoverview_upgrade($oldversion) {
  37      global $DB, $CFG, $OUTPUT;
  38  
  39      if ($oldversion < 2019091800) {
  40          // Remove orphaned course favourites, which weren't being deleted when the course was deleted.
  41          $sql = 'SELECT f.id
  42                    FROM {favourite} f
  43               LEFT JOIN {course} c
  44                      ON (c.id = f.itemid)
  45                   WHERE f.component = :component
  46                     AND f.itemtype = :itemtype
  47                     AND c.id IS NULL';
  48          $params = ['component' => 'core_course', 'itemtype' => 'courses'];
  49  
  50          if ($records = $DB->get_fieldset_sql($sql, $params)) {
  51              $chunks = array_chunk($records, 1000);
  52              foreach ($chunks as $chunk) {
  53                  list($insql, $inparams) = $DB->get_in_or_equal($chunk);
  54                  $DB->delete_records_select('favourite', "id $insql", $inparams);
  55              }
  56          }
  57  
  58          upgrade_block_savepoint(true, 2019091800, 'myoverview', false);
  59      }
  60  
  61      // Automatically generated Moodle v3.8.0 release upgrade line.
  62      // Put any upgrade step following this.
  63  
  64      if ($oldversion < 2019111801) {
  65          // Renaming the setting from displaygroupingstarred to displaygroupingfavourites to match Moodle convention.
  66  
  67          // Check to see if record exists. get_config doesn't allow differentiation between not exists and false.
  68          $dbval = $DB->get_field('config_plugins', 'value', ['plugin' => 'block_myoverview', 'name' => 'displaygroupingstarred']);
  69          if ($dbval !== false) {
  70              set_config('displaygroupingfavourites', $dbval, 'block_myoverview');
  71              unset_config('displaygroupingstarred', 'block_myoverview');
  72          }
  73  
  74          if (isset($CFG->forced_plugin_settings['block_myoverview']['displaygroupingstarred'])) {
  75              // Check to see if the starred setting is defined in the config file. Display a warning if so.
  76              $warn = 'Setting block_myoverview->displaygroupingstarred has been renamed '.
  77                      'to block_myoverview->displaygroupingfavourites. Old setting present in config.php.';
  78              echo $OUTPUT->notification($warn, 'notifyproblem');
  79          }
  80  
  81          upgrade_block_savepoint(true, 2019111801, 'myoverview', false);
  82      }
  83  
  84      // Automatically generated Moodle v3.9.0 release upgrade line.
  85      // Put any upgrade step following this.
  86  
  87      if ($oldversion < 2021052504) {
  88          upgrade_block_delete_instances('myoverview', '__default', 'my-index');
  89  
  90          // Add new instance to the /my/courses.php page.
  91          $subpagepattern = $DB->get_record('my_pages', [
  92              'userid' => null,
  93              'name' => MY_PAGE_COURSES,
  94              'private' => MY_PAGE_PUBLIC,
  95          ], 'id', IGNORE_MULTIPLE)->id;
  96  
  97          $blockname = 'myoverview';
  98          $pagetypepattern = 'my-index';
  99  
 100          $blockparams = [
 101              'blockname' => $blockname,
 102              'pagetypepattern' => $pagetypepattern,
 103              'subpagepattern' => $subpagepattern,
 104          ];
 105  
 106          // See if this block already somehow exists, it should not but who knows.
 107          if (!$DB->record_exists('block_instances', $blockparams)) {
 108              $page = new moodle_page();
 109              $page->set_context(context_system::instance());
 110              // Add the block to the default /my/courses.
 111              $page->blocks->add_region('content');
 112              $page->blocks->add_block($blockname, 'content', 0, false, $pagetypepattern, $subpagepattern);
 113          }
 114  
 115          upgrade_block_savepoint(true, 2021052504, 'myoverview', false);
 116      }
 117  
 118      if ($oldversion < 2022041901) {
 119          upgrade_block_set_my_user_parent_context('myoverview', '__default', 'my-index');
 120          upgrade_block_savepoint(true, 2022041901, 'myoverview', false);
 121      }
 122  
 123      // Automatically generated Moodle v4.0.0 release upgrade line.
 124      // Put any upgrade step following this.
 125  
 126      return true;
 127  }