MDL-68683 competencies: step to delete orphaned evidence files
authorSimey Lameze <simey@moodle.com>
Fri, 22 May 2020 08:20:53 +0000 (16:20 +0800)
committerSimey Lameze <simey@moodle.com>
Thu, 20 Aug 2020 01:09:37 +0000 (09:09 +0800)
lib/db/upgrade.php
version.php

index 96867f6..7ce3c1e 100644 (file)
@@ -2530,5 +2530,24 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2020061501.04);
     }
 
+    if ($oldversion < 2020061501.08) {
+        // Delete all user evidence files from users that have been deleted.
+        $sql = "SELECT DISTINCT f.*
+                  FROM {files} f
+             LEFT JOIN {context} c ON f.contextid = c.id
+             LEFT JOIN {user} u ON c.instanceid = u.id
+                 WHERE f.component = :component
+                   AND f.filearea = :filearea
+                   AND u.deleted = 1";
+        $stalefiles = $DB->get_records_sql($sql, ['component' => 'core_competency', 'filearea' => 'userevidence']);
+
+        $fs = get_file_storage();
+        foreach ($stalefiles as $stalefile) {
+            $fs->get_file_instance($stalefile)->delete();
+        }
+
+        upgrade_main_savepoint(true, 2020061501.08);
+    }
+
     return true;
 }
index e2315b2..ae78361 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2020061501.07;              // 20200615      = branching date YYYYMMDD - do not modify!
+$version  = 2020061501.08;              // 20200615      = branching date YYYYMMDD - do not modify!
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 $release  = '3.9.1+ (Build: 20200814)'; // Human-friendly version name