Skip to content

Commit 3de6268

Browse files
Course: Export HTML documents as Moodle page activities - refs BT#21977
1 parent f9726b4 commit 3de6268

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

main/inc/lib/moodleexport/FileExport.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,23 @@ private function createFileXmlEntry(array $file): string
186186
*/
187187
private function processDocument(array $filesData, object $document): array
188188
{
189+
if (
190+
$document->file_type === 'file' &&
191+
pathinfo($document->path, PATHINFO_EXTENSION) === 'html' &&
192+
substr_count($document->path, '/') === 1
193+
) {
194+
return $filesData;
195+
}
196+
189197
if ($document->file_type === 'file') {
190-
$fileData = $this->getFileData($document);
191-
$fileData['filepath'] = '/Documents/';
192-
$fileData['contextid'] = 0;
193-
$fileData['component'] = 'mod_folder';
194-
$filesData['files'][] = $fileData;
198+
$extension = pathinfo($document->path, PATHINFO_EXTENSION);
199+
if (!in_array(strtolower($extension), ['html', 'htm'])) {
200+
$fileData = $this->getFileData($document);
201+
$fileData['filepath'] = '/Documents/';
202+
$fileData['contextid'] = 0;
203+
$fileData['component'] = 'mod_folder';
204+
$filesData['files'][] = $fileData;
205+
}
195206
} elseif ($document->file_type === 'folder') {
196207
$folderFiles = \DocumentManager::getAllDocumentsByParentId($this->course->info, $document->source_id);
197208
foreach ($folderFiles as $file) {

main/inc/lib/moodleexport/MoodleExport.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ private function getActivities(): array
440440
'title' => 'Documents',
441441
];
442442
$activities[] = $documentsFolder;
443+
$htmlPageIds = [];
443444
foreach ($this->course->resources as $resourceType => $resources) {
444445
foreach ($resources as $resource) {
445446
$exportClass = null;
@@ -479,13 +480,14 @@ private function getActivities(): array
479480
// Handle documents (HTML pages)
480481
elseif ($resourceType === RESOURCE_DOCUMENT && $resource->source_id > 0) {
481482
$document = \DocumentManager::get_document_data_by_id($resource->source_id, $this->course->code);
482-
if ('html' === pathinfo($document['path'], PATHINFO_EXTENSION)) {
483+
if ('html' === pathinfo($document['path'], PATHINFO_EXTENSION) && substr_count($resource->path, '/') === 1) {
483484
$exportClass = PageExport::class;
484485
$moduleName = 'page';
485486
$id = $resource->source_id;
486487
$title = $document['title'];
488+
$htmlPageIds[] = $id;
487489
}
488-
if ('file' === $resource->file_type) {
490+
if ('file' === $resource->file_type && !in_array($resource->source_id, $htmlPageIds)) {
489491
$resourceExport = new ResourceExport($this->course);
490492
if ($resourceExport->getSectionIdForActivity($resource->source_id, $resourceType) > 0) {
491493
$isRoot = substr_count($resource->path, '/') === 1;

main/inc/lib/moodleexport/SectionExport.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,19 +273,24 @@ private function addActivityToList(array $item, int $sectionId, array &$activiti
273273
break;
274274

275275
case 'document':
276-
if ($sectionId > 0) {
277-
$documentId = (int) $item['path'];
278-
$document = \DocumentManager::get_document_data_by_id($documentId, $this->course->code);
276+
$documentId = (int) $item['path'];
277+
$document = \DocumentManager::get_document_data_by_id($documentId, $this->course->code);
279278

280-
// Determine the type of document and get the corresponding export class
279+
if ($document) {
280+
$isRoot = substr_count($document['path'], '/') === 1;
281281
$documentType = $this->getDocumentType($document['filetype'], $document['path']);
282-
if ($documentType) {
282+
if ($documentType === 'page' && $isRoot) {
283+
$activityClass = $activityClassMap['page'];
284+
$exportInstance = new $activityClass($this->course);
285+
$activityData = $exportInstance->getData($item['path'], $sectionId);
286+
}
287+
elseif ($sectionId > 0 && $documentType && isset($activityClassMap[$documentType])) {
283288
$activityClass = $activityClassMap[$documentType];
284289
$exportInstance = new $activityClass($this->course);
285290
$activityData = $exportInstance->getData($item['path'], $sectionId);
286291
}
287-
break;
288292
}
293+
break;
289294
}
290295

291296
// Add the activity to the list if the data exists

0 commit comments

Comments
 (0)