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 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      // Automatically generated Moodle v4.2.0 release upgrade line.
  49      // Put any upgrade step following this.
  50  
  51      if ($oldversion < 2023042401) {
  52          // Clean orphan data_records.
  53          $sql = "SELECT d.id FROM {data} d
  54              LEFT JOIN {data_fields} f ON d.id = f.dataid
  55              WHERE f.id IS NULL";
  56          $emptydatas = $DB->get_records_sql($sql);
  57          if (!empty($emptydatas)) {
  58              $dataids = array_keys($emptydatas);
  59              list($datainsql, $dataparams) = $DB->get_in_or_equal($dataids, SQL_PARAMS_NAMED, 'data');
  60              $DB->delete_records_select('data_records', "dataid $datainsql", $dataparams);
  61          }
  62  
  63          // Data savepoint reached.
  64          upgrade_mod_savepoint(true, 2023042401, 'data');
  65      }
  66  
  67      return true;
  68  }