From 542765cb6335eefe34f9dafee81beb8b35bd9396 Mon Sep 17 00:00:00 2001 From: Christian Beeznest Date: Sat, 21 Dec 2024 16:08:34 -0500 Subject: [PATCH] Course: Fix export mbz validation, root-only resources, skip empty folders - refs BT#21977 --- main/inc/lib/moodleexport/FolderExport.php | 27 ++++++++++------- main/inc/lib/moodleexport/MoodleExport.php | 34 +++++++++++++++++----- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/main/inc/lib/moodleexport/FolderExport.php b/main/inc/lib/moodleexport/FolderExport.php index 9db86b25d3..178cac2620 100644 --- a/main/inc/lib/moodleexport/FolderExport.php +++ b/main/inc/lib/moodleexport/FolderExport.php @@ -42,19 +42,26 @@ class FolderExport extends ActivityExport /** * Get folder data dynamically from the course. */ - public function getData(int $folderId, int $sectionId): array + public function getData(int $folderId, int $sectionId): ?array { $folder = $this->course->resources['document'][$folderId]; - return [ - 'id' => $folderId, - 'moduleid' => $folder->source_id, - 'modulename' => 'folder', - 'contextid' => $folder->source_id, - 'name' => $folder->title, - 'sectionid' => $sectionId, - 'timemodified' => time(), - ]; + $folderPath = $folder->path . '/'; + foreach ($this->course->resources['document'] as $resource) { + if ($resource->path !== $folder->path && str_starts_with($resource->path, $folderPath)) { + return [ + 'id' => $folderId, + 'moduleid' => $folder->source_id, + 'modulename' => 'folder', + 'contextid' => $folder->source_id, + 'name' => $folder->title, + 'sectionid' => $sectionId, + 'timemodified' => time(), + ]; + } + } + + return null; } /** diff --git a/main/inc/lib/moodleexport/MoodleExport.php b/main/inc/lib/moodleexport/MoodleExport.php index 58d395bfcd..6e98220bdc 100644 --- a/main/inc/lib/moodleexport/MoodleExport.php +++ b/main/inc/lib/moodleexport/MoodleExport.php @@ -404,15 +404,33 @@ class MoodleExport $id = $resource->source_id; $title = $document['title']; } elseif ('file' === $resource->file_type) { - $exportClass = ResourceExport::class; - $moduleName = 'resource'; - $id = $resource->source_id; - $title = $resource->title; + $isRoot = substr_count($resource->path, '/') === 1; + + if ($isRoot) { + $exportClass = ResourceExport::class; + $moduleName = 'resource'; + $id = $resource->source_id; + $title = $resource->title; + } } elseif ('folder' === $resource->file_type) { - $exportClass = FolderExport::class; - $moduleName = 'folder'; - $id = $resource->source_id; - $title = $resource->title; + $isEmpty = true; + $folderPath = $resource->path . '/'; + + foreach ($this->course->resources['document'] as $childResource) { + if (str_starts_with($childResource->path, $folderPath) && $childResource->path !== $resource->path) { + $isEmpty = false; + break; + } + } + + $isRoot = substr_count($resource->path, '/') === 1; + + if (!$isEmpty && $isRoot) { + $exportClass = FolderExport::class; + $moduleName = 'folder'; + $id = $resource->source_id; + $title = $resource->title; + } } } // Handle assignments (work)