prepareActivityDirectory($exportDir, 'folder', $moduleId); // Retrieve folder data $folderData = $this->getData($activityId, $sectionId); // Generate XML files $this->createFolderXml($folderData, $folderDir); $this->createModuleXml($folderData, $folderDir); $this->createGradesXml($folderData, $folderDir); $this->createFiltersXml($folderData, $folderDir); $this->createGradeHistoryXml($folderData, $folderDir); $this->createInforefXml($this->getFilesForFolder($activityId), $folderDir); $this->createRolesXml($folderData, $folderDir); $this->createCommentsXml($folderData, $folderDir); $this->createCalendarXml($folderData, $folderDir); } /** * Get folder data dynamically from the course. */ public function getData(int $folderId, int $sectionId): ?array { $folder = $this->course->resources['document'][$folderId]; $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; } /** * Create the XML file for the folder. */ private function createFolderXml(array $folderData, string $folderDir): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.htmlspecialchars($folderData['name']).''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' '.$folderData['timemodified'].''.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ''; $this->createXmlFile('folder', $xmlContent, $folderDir); } /** * Get the list of files for a folder. */ private function getFilesForFolder(int $folderId): array { $documentData = \DocumentManager::getAllDocumentsByParentId($this->course->info, $folderId); $files = []; foreach ($documentData as $doc) { if ($doc['filetype'] === 'file') { $files[] = [ 'id' => (int) $doc['id'], 'contenthash' => 'hash'.$doc['id'], 'filename' => $doc['basename'], 'filepath' => $doc['path'], 'filesize' => (int) $doc['size'], 'mimetype' => $this->getMimeType($doc['basename']), ]; } } return ['files' => $files]; } /** * Get the MIME type for a given file. */ private function getMimeType(string $filename): string { $ext = pathinfo($filename, PATHINFO_EXTENSION); $mimetypes = [ 'pdf' => 'application/pdf', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'doc' => 'application/msword', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', ]; return $mimetypes[$ext] ?? 'application/octet-stream'; } }