prepareActivityDirectory($exportDir, 'page', $moduleId); // Retrieve page data $pageData = $this->getData($activityId, $sectionId); // Generate XML files $this->createPageXml($pageData, $pageDir); $this->createModuleXml($pageData, $pageDir); $this->createGradesXml($pageData, $pageDir); $this->createFiltersXml($pageData, $pageDir); $this->createGradeHistoryXml($pageData, $pageDir); $this->createInforefXml($pageData, $pageDir); $this->createRolesXml($pageData, $pageDir); $this->createCommentsXml($pageData, $pageDir); $this->createCalendarXml($pageData, $pageDir); } /** * Get page data dynamically from the course. */ public function getData(int $pageId, int $sectionId): ?array { $contextid = $this->course->info['real_id']; if ($pageId === 0) { $introText = trim($this->course->resources[RESOURCE_TOOL_INTRO]['course_homepage']->intro_text ?? ''); if (!empty($introText)) { $files = []; $resources = \DocumentManager::get_resources_from_source_html($introText); $courseInfo = api_get_course_info($this->course->code); $adminId = MoodleExport::getAdminUserData()['id']; foreach ($resources as [$src]) { if (preg_match('#/document(/[^"\']+)#', $src, $matches)) { $path = $matches[1]; $docId = \DocumentManager::get_document_id($courseInfo, $path); if ($docId) { $this->course->used_page_doc_ids[] = $docId; $document = \DocumentManager::get_document_data_by_id($docId, $this->course->code); if ($document) { $contenthash = hash('sha1', basename($document['path'])); $mimetype = (new FileExport($this->course))->getMimeType($document['path']); $files[] = [ 'id' => $document['id'], 'contenthash' => $contenthash, 'contextid' => $contextid, 'component' => 'mod_page', 'filearea' => 'content', 'itemid' => 1, 'filepath' => '/Documents/', 'documentpath' => 'document' . $document['path'], 'filename' => basename($document['path']), 'userid' => $adminId, 'filesize' => $document['size'], 'mimetype' => $mimetype, 'status' => 0, 'timecreated' => time() - 3600, 'timemodified' => time(), 'source' => $document['title'], 'author' => 'Unknown', 'license' => 'allrightsreserved', ]; } } } } return [ 'id' => 0, 'moduleid' => 0, 'modulename' => 'page', 'contextid' => $contextid, 'name' => get_lang('Introduction'), 'intro' => '', 'content' => $this->normalizeContent($introText), 'sectionid' => $sectionId, 'sectionnumber' => 1, 'display' => 0, 'timemodified' => time(), 'users' => [], 'files' => $files, ]; } } $pageResources = $this->course->resources[RESOURCE_DOCUMENT] ?? []; foreach ($pageResources as $page) { if ($page->source_id == $pageId) { return [ 'id' => $page->source_id, 'moduleid' => $page->source_id, 'modulename' => 'page', 'contextid' => $contextid, 'name' => $page->title, 'intro' => $page->comment ?? '', 'content' => $this->normalizeContent($this->getPageContent($page)), 'sectionid' => $sectionId, 'sectionnumber' => 1, 'display' => 0, 'timemodified' => time(), 'users' => [], 'files' => [], ]; } } return null; } /** * Create the XML file for the page. */ private function createPageXml(array $pageData, string $pageDir): void { $xmlContent = ''.PHP_EOL; $xmlContent .= ''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ' '.htmlspecialchars($pageData['name']).''.PHP_EOL; $xmlContent .= ' '.htmlspecialchars($pageData['intro']).''.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' '.htmlspecialchars($pageData['content']).''.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' 0'.PHP_EOL; $xmlContent .= ' 5'.PHP_EOL; $xmlContent .= ' a:3:{s:12:"printheading";s:1:"1";s:10:"printintro";s:1:"0";s:17:"printlastmodified";s:1:"1";}'.PHP_EOL; $xmlContent .= ' 1'.PHP_EOL; $xmlContent .= ' '.$pageData['timemodified'].''.PHP_EOL; $xmlContent .= ' '.PHP_EOL; $xmlContent .= ''; $this->createXmlFile('page', $xmlContent, $pageDir); } private function normalizeContent(string $html): string { return preg_replace_callback( '#]+src=["\'](?[^"\']+)["\']#i', function ($match) { $src = $match['url']; if (preg_match('#/courses/[^/]+/document/(.+)$#', $src, $parts)) { $filename = basename($parts[1]); return str_replace($src, '@@PLUGINFILE@@/Documents/' . $filename, $match[0]); } return $match[0]; }, $html ); } /** * Retrieves the content of the page. */ private function getPageContent(object $page): string { if ($page->file_type === 'file') { return file_get_contents($this->course->path.$page->path); } return ''; } }