From 33e385a5fb224c30055068f780bec8bde5afa9d0 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Thu, 16 Nov 2017 11:02:55 +0800 Subject: [PATCH] MDL-37757 repositories: Deleting a repository deletes files. If the user decides to remove a repository all of the linked files are now deleted along with file references. This was causing problems such as the course page displaying an error with no recourse to fix the problem. --- repository/lib.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/repository/lib.php b/repository/lib.php index 6f6759c0475..95976657352 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -2004,6 +2004,8 @@ abstract class repository implements cacheable_object { global $DB; if ($downloadcontents) { $this->convert_references_to_local(); + } else { + $this->remove_files(); } cache::make('core', 'repositories')->purge(); try { @@ -2668,6 +2670,17 @@ abstract class repository implements cacheable_object { } } + /** + * Find all external files linked to this repository and delete them. + */ + public function remove_files() { + $fs = get_file_storage(); + $files = $fs->get_external_files($this->id); + foreach ($files as $storedfile) { + $storedfile->delete(); + } + } + /** * Function repository::reset_caches() is deprecated, cache is handled by MUC now. * @deprecated since Moodle 2.6 MDL-42016 - please do not use this function any more.