Merge branch 'MDL-69166-310-4' of git://github.com/rezaies/moodle into MOODLE_310_STABLE
[moodle.git] / lib / db / upgrade.php
index b2f9bb8..3976d5f 100644 (file)
@@ -2774,6 +2774,7 @@ function xmldb_main_upgrade($oldversion) {
 
         // Add example field.
         $field = new xmldb_field('example', XMLDB_TYPE_TEXT, null, null, null, null, null, 'tutorial');
+
         if (!$dbman->field_exists($table, $field)) {
             $dbman->add_field($table, $field);
         }
@@ -2862,7 +2863,61 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2020102100.02);
     }
 
-    if ($oldversion < 2020102700.01) {
+    if ($oldversion < 2020102300.01) {
+        // Define field downloadcontent to be added to course.
+        $table = new xmldb_table('course');
+        $field = new xmldb_field('downloadcontent', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'visibleold');
+
+        if (!$dbman->field_exists($table, $field)) {
+            $dbman->add_field($table, $field);
+        }
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2020102300.01);
+    }
+
+    if ($oldversion < 2020102300.02) {
+        $table = new xmldb_table('badge_backpack');
+
+        // There is no key_exists, so test the equivalent index.
+        $oldindex = new xmldb_index('backpackcredentials', XMLDB_KEY_UNIQUE, ['userid', 'externalbackpackid']);
+        if (!$dbman->index_exists($table, $oldindex)) {
+            // All external backpack providers/hosts are now exclusively stored in badge_external_backpack.
+            // All credentials are stored in badge_backpack and are unique per user, backpack.
+            $uniquekey = new xmldb_key('backpackcredentials', XMLDB_KEY_UNIQUE, ['userid', 'externalbackpackid']);
+            $dbman->add_key($table, $uniquekey);
+        }
+
+        // Drop the password field as this is moved to badge_backpack.
+        $table = new xmldb_table('badge_external_backpack');
+        $field = new xmldb_field('password', XMLDB_TYPE_CHAR, '50');
+        if ($dbman->field_exists($table, $field)) {
+            // If there is a current backpack set then copy it across to the new structure.
+            if ($CFG->badges_defaultissuercontact) {
+                // Get the currently used site backpacks.
+                $records = $DB->get_records_select('badge_external_backpack', "password IS NOT NULL AND password != ''");
+                $backpack = [
+                    'userid' => '0',
+                    'email' => $CFG->badges_defaultissuercontact,
+                    'backpackuid' => -1
+                ];
+
+                // Create records corresponding to the site backpacks.
+                foreach ($records as $record) {
+                    $backpack['password'] = $record->password;
+                    $backpack['externalbackpackid'] = $record->id;
+                    $DB->insert_record('badge_backpack', (object) $backpack);
+                }
+            }
+
+            $dbman->drop_field($table, $field);
+        }
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2020102300.02);
+    }
+
+    if ($oldversion < 2020102700.04) {
 
         // Define table payment_accounts to be created.
         $table = new xmldb_table('payment_accounts');
@@ -2885,12 +2940,6 @@ function xmldb_main_upgrade($oldversion) {
             $dbman->create_table($table);
         }
 
-        // Main savepoint reached.
-        upgrade_main_savepoint(true, 2020102700.01);
-    }
-
-    if ($oldversion < 2020102700.02) {
-
         // Define table payment_gateways to be created.
         $table = new xmldb_table('payment_gateways');
 
@@ -2912,12 +2961,6 @@ function xmldb_main_upgrade($oldversion) {
             $dbman->create_table($table);
         }
 
-        // Main savepoint reached.
-        upgrade_main_savepoint(true, 2020102700.02);
-    }
-
-    if ($oldversion < 2020102700.03) {
-
         // Define table payments to be created.
         $table = new xmldb_table('payments');
 
@@ -2949,7 +2992,7 @@ function xmldb_main_upgrade($oldversion) {
         }
 
         // Main savepoint reached.
-        upgrade_main_savepoint(true, 2020102700.03);
+        upgrade_main_savepoint(true, 2020102700.04);
     }
 
     return true;