From 3f625caa2d988062bcc99060714996c69a8be9c2 Mon Sep 17 00:00:00 2001
From: Christian Beeznest <christian.fasanando@beeznest.com>
Date: Tue, 3 Jun 2025 19:23:36 -0500
Subject: [PATCH] Course: Export HTML documents as Moodle page activities -
 refs BT#21977

---
 main/inc/lib/moodleexport/FileExport.php    | 21 ++++++++++++++++-----
 main/inc/lib/moodleexport/MoodleExport.php  |  6 ++++--
 main/inc/lib/moodleexport/SectionExport.php | 17 +++++++++++------
 3 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/main/inc/lib/moodleexport/FileExport.php b/main/inc/lib/moodleexport/FileExport.php
index a210b6101b5..77ff305d72a 100644
--- a/main/inc/lib/moodleexport/FileExport.php
+++ b/main/inc/lib/moodleexport/FileExport.php
@@ -186,12 +186,23 @@ private function createFileXmlEntry(array $file): string
      */
     private function processDocument(array $filesData, object $document): array
     {
+        if (
+            $document->file_type === 'file' &&
+            pathinfo($document->path, PATHINFO_EXTENSION) === 'html' &&
+            substr_count($document->path, '/') === 1
+        ) {
+            return $filesData;
+        }
+
         if ($document->file_type === 'file') {
-            $fileData = $this->getFileData($document);
-            $fileData['filepath'] = '/Documents/';
-            $fileData['contextid'] = 0;
-            $fileData['component'] = 'mod_folder';
-            $filesData['files'][] = $fileData;
+            $extension = pathinfo($document->path, PATHINFO_EXTENSION);
+            if (!in_array(strtolower($extension), ['html', 'htm'])) {
+                $fileData = $this->getFileData($document);
+                $fileData['filepath'] = '/Documents/';
+                $fileData['contextid'] = 0;
+                $fileData['component'] = 'mod_folder';
+                $filesData['files'][] = $fileData;
+            }
         } elseif ($document->file_type === 'folder') {
             $folderFiles = \DocumentManager::getAllDocumentsByParentId($this->course->info, $document->source_id);
             foreach ($folderFiles as $file) {
diff --git a/main/inc/lib/moodleexport/MoodleExport.php b/main/inc/lib/moodleexport/MoodleExport.php
index a2bead79ffe..af26de5b19d 100644
--- a/main/inc/lib/moodleexport/MoodleExport.php
+++ b/main/inc/lib/moodleexport/MoodleExport.php
@@ -440,6 +440,7 @@ private function getActivities(): array
             'title' => 'Documents',
         ];
         $activities[] = $documentsFolder;
+        $htmlPageIds = [];
         foreach ($this->course->resources as $resourceType => $resources) {
             foreach ($resources as $resource) {
                 $exportClass = null;
@@ -479,13 +480,14 @@ private function getActivities(): array
                 // Handle documents (HTML pages)
                 elseif ($resourceType === RESOURCE_DOCUMENT && $resource->source_id > 0) {
                     $document = \DocumentManager::get_document_data_by_id($resource->source_id, $this->course->code);
-                    if ('html' === pathinfo($document['path'], PATHINFO_EXTENSION)) {
+                    if ('html' === pathinfo($document['path'], PATHINFO_EXTENSION) && substr_count($resource->path, '/') === 1) {
                         $exportClass = PageExport::class;
                         $moduleName = 'page';
                         $id = $resource->source_id;
                         $title = $document['title'];
+                        $htmlPageIds[] = $id;
                     }
-                    if ('file' === $resource->file_type) {
+                    if ('file' === $resource->file_type && !in_array($resource->source_id, $htmlPageIds)) {
                         $resourceExport = new ResourceExport($this->course);
                         if ($resourceExport->getSectionIdForActivity($resource->source_id, $resourceType) > 0) {
                             $isRoot = substr_count($resource->path, '/') === 1;
diff --git a/main/inc/lib/moodleexport/SectionExport.php b/main/inc/lib/moodleexport/SectionExport.php
index 278caae04f9..e87a23262ef 100644
--- a/main/inc/lib/moodleexport/SectionExport.php
+++ b/main/inc/lib/moodleexport/SectionExport.php
@@ -273,19 +273,24 @@ private function addActivityToList(array $item, int $sectionId, array &$activiti
                 break;
 
             case 'document':
-                if ($sectionId > 0) {
-                    $documentId = (int) $item['path'];
-                    $document = \DocumentManager::get_document_data_by_id($documentId, $this->course->code);
+                $documentId = (int) $item['path'];
+                $document = \DocumentManager::get_document_data_by_id($documentId, $this->course->code);
 
-                    // Determine the type of document and get the corresponding export class
+                if ($document) {
+                    $isRoot = substr_count($document['path'], '/') === 1;
                     $documentType = $this->getDocumentType($document['filetype'], $document['path']);
-                    if ($documentType) {
+                    if ($documentType === 'page' && $isRoot) {
+                        $activityClass = $activityClassMap['page'];
+                        $exportInstance = new $activityClass($this->course);
+                        $activityData = $exportInstance->getData($item['path'], $sectionId);
+                    }
+                    elseif ($sectionId > 0 && $documentType && isset($activityClassMap[$documentType])) {
                         $activityClass = $activityClassMap[$documentType];
                         $exportInstance = new $activityClass($this->course);
                         $activityData = $exportInstance->getData($item['path'], $sectionId);
                     }
-                    break;
                 }
+                break;
         }
 
         // Add the activity to the list if the data exists