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 keeps track of upgrades to
   3  // the data module
   4  //
   5  // Sometimes, changes between versions involve
   6  // alterations to database structures and other
   7  // major things that may break installations.
   8  //
   9  // The upgrade function in this file will attempt
  10  // to perform all the necessary actions to upgrade
  11  // your older installation to the current version.
  12  //
  13  // If there's something it cannot do itself, it
  14  // will tell you what you need to do.
  15  //
  16  // The commands in here will all be database-neutral,
  17  // using the methods of database_manager class
  18  //
  19  // Please do not forget to use upgrade_set_timeout()
  20  // before any action that may take longer time to finish.
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  function xmldb_data_upgrade($oldversion) {
  25      global $DB;
  26  
  27      $dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.
  28  
  29      // Automatically generated Moodle v3.9.0 release upgrade line.
  30      // Put any upgrade step following this.
  31  
  32      // Automatically generated Moodle v4.0.0 release upgrade line.
  33      // Put any upgrade step following this.
  34      if ($oldversion < 2022081600) {
  35          // Define key userid (foreign) to be added to data_records.
  36          $table = new xmldb_table('data_records');
  37          $key = new xmldb_key('userid', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']);
  38          // Launch add key userid.
  39          $dbman->add_key($table, $key);
  40  
  41          // Data savepoint reached.
  42          upgrade_mod_savepoint(true, 2022081600, 'data');
  43      }
  44  
  45      // Automatically generated Moodle v4.1.0 release upgrade line.
  46      // Put any upgrade step following this.
  47  
  48      if ($oldversion < 2022112801) {
  49          // Clean orphan data_records.
  50          $sql = "SELECT d.id FROM {data} d
  51              LEFT JOIN {data_fields} f ON d.id = f.dataid
  52              WHERE f.id IS NULL";
  53          $emptydatas = $DB->get_records_sql($sql);
  54          if (!empty($emptydatas)) {
  55              $dataids = array_keys($emptydatas);
  56              list($datainsql, $dataparams) = $DB->get_in_or_equal($dataids, SQL_PARAMS_NAMED, 'data');
  57              $DB->delete_records_select('data_records', "dataid $datainsql", $dataparams);
  58          }
  59  
  60          // Data savepoint reached.
  61          upgrade_mod_savepoint(true, 2022112801, 'data');
  62      }
  63  
  64      return true;
  65  }