@@ -46,25 +46,64 @@ public function getData(int $pageId, int $sectionId): ?array
4646 {
4747 $ contextid = $ this ->course ->info ['real_id ' ];
4848 if ($ pageId === 0 ) {
49- if (
50- isset ($ this ->course ->resources [RESOURCE_TOOL_INTRO ]['course_homepage ' ]) &&
51- is_object ($ this ->course ->resources [RESOURCE_TOOL_INTRO ]['course_homepage ' ]) &&
52- !empty ($ this ->course ->resources [RESOURCE_TOOL_INTRO ]['course_homepage ' ]->intro_text )
53- ) {
49+ $ introText = trim ($ this ->course ->resources [RESOURCE_TOOL_INTRO ]['course_homepage ' ]->intro_text ?? '' );
50+
51+ if (!empty ($ introText )) {
52+ $ files = [];
53+ $ resources = \DocumentManager::get_resources_from_source_html ($ introText );
54+ $ courseInfo = api_get_course_info ($ this ->course ->code );
55+ $ adminId = MoodleExport::getAdminUserData ()['id ' ];
56+
57+ foreach ($ resources as [$ src ]) {
58+ if (preg_match ('#/document(/[^" \']+)# ' , $ src , $ matches )) {
59+ $ path = $ matches [1 ];
60+ $ docId = \DocumentManager::get_document_id ($ courseInfo , $ path );
61+ if ($ docId ) {
62+ $ this ->course ->used_page_doc_ids [] = $ docId ;
63+ $ document = \DocumentManager::get_document_data_by_id ($ docId , $ this ->course ->code );
64+ if ($ document ) {
65+ $ contenthash = hash ('sha1 ' , basename ($ document ['path ' ]));
66+ $ mimetype = (new FileExport ($ this ->course ))->getMimeType ($ document ['path ' ]);
67+
68+ $ files [] = [
69+ 'id ' => $ document ['id ' ],
70+ 'contenthash ' => $ contenthash ,
71+ 'contextid ' => $ contextid ,
72+ 'component ' => 'mod_page ' ,
73+ 'filearea ' => 'content ' ,
74+ 'itemid ' => 1 ,
75+ 'filepath ' => '/Documents/ ' ,
76+ 'documentpath ' => 'document ' . $ document ['path ' ],
77+ 'filename ' => basename ($ document ['path ' ]),
78+ 'userid ' => $ adminId ,
79+ 'filesize ' => $ document ['size ' ],
80+ 'mimetype ' => $ mimetype ,
81+ 'status ' => 0 ,
82+ 'timecreated ' => time () - 3600 ,
83+ 'timemodified ' => time (),
84+ 'source ' => $ document ['title ' ],
85+ 'author ' => 'Unknown ' ,
86+ 'license ' => 'allrightsreserved ' ,
87+ ];
88+ }
89+ }
90+ }
91+ }
92+
5493 return [
5594 'id ' => 0 ,
5695 'moduleid ' => 0 ,
5796 'modulename ' => 'page ' ,
5897 'contextid ' => $ contextid ,
5998 'name ' => get_lang ('Introduction ' ),
6099 'intro ' => '' ,
61- 'content ' => trim ( $ this ->course -> resources [ RESOURCE_TOOL_INTRO ][ ' course_homepage ' ]-> intro_text ),
100+ 'content ' => $ this ->normalizeContent ( $ introText ),
62101 'sectionid ' => $ sectionId ,
63102 'sectionnumber ' => 1 ,
64103 'display ' => 0 ,
65104 'timemodified ' => time (),
66105 'users ' => [],
67- 'files ' => [] ,
106+ 'files ' => $ files ,
68107 ];
69108 }
70109 }
@@ -79,7 +118,7 @@ public function getData(int $pageId, int $sectionId): ?array
79118 'contextid ' => $ contextid ,
80119 'name ' => $ page ->title ,
81120 'intro ' => $ page ->comment ?? '' ,
82- 'content ' => $ this ->getPageContent ($ page ),
121+ 'content ' => $ this ->normalizeContent ( $ this -> getPageContent ($ page) ),
83122 'sectionid ' => $ sectionId ,
84123 'sectionnumber ' => 1 ,
85124 'display ' => 0 ,
@@ -117,6 +156,24 @@ private function createPageXml(array $pageData, string $pageDir): void
117156 $ this ->createXmlFile ('page ' , $ xmlContent , $ pageDir );
118157 }
119158
159+ private function normalizeContent (string $ html ): string
160+ {
161+ return preg_replace_callback (
162+ '#<img[^>]+src=[" \'](?<url>[^" \']+)[" \']#i ' ,
163+ function ($ match ) {
164+ $ src = $ match ['url ' ];
165+
166+ if (preg_match ('#/courses/[^/]+/document/(.+)$# ' , $ src , $ parts )) {
167+ $ filename = basename ($ parts [1 ]);
168+ return str_replace ($ src , '@@PLUGINFILE@@/Documents/ ' . $ filename , $ match [0 ]);
169+ }
170+
171+ return $ match [0 ];
172+ },
173+ $ html
174+ );
175+ }
176+
120177 /**
121178 * Retrieves the content of the page.
122179 */
0 commit comments