@@ -46,25 +46,64 @@ public function getData(int $pageId, int $sectionId): ?array
46
46
{
47
47
$ contextid = $ this ->course ->info ['real_id ' ];
48
48
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
+
54
93
return [
55
94
'id ' => 0 ,
56
95
'moduleid ' => 0 ,
57
96
'modulename ' => 'page ' ,
58
97
'contextid ' => $ contextid ,
59
98
'name ' => get_lang ('Introduction ' ),
60
99
'intro ' => '' ,
61
- 'content ' => trim ( $ this ->course -> resources [ RESOURCE_TOOL_INTRO ][ ' course_homepage ' ]-> intro_text ),
100
+ 'content ' => $ this ->normalizeContent ( $ introText ),
62
101
'sectionid ' => $ sectionId ,
63
102
'sectionnumber ' => 1 ,
64
103
'display ' => 0 ,
65
104
'timemodified ' => time (),
66
105
'users ' => [],
67
- 'files ' => [] ,
106
+ 'files ' => $ files ,
68
107
];
69
108
}
70
109
}
@@ -79,7 +118,7 @@ public function getData(int $pageId, int $sectionId): ?array
79
118
'contextid ' => $ contextid ,
80
119
'name ' => $ page ->title ,
81
120
'intro ' => $ page ->comment ?? '' ,
82
- 'content ' => $ this ->getPageContent ($ page ),
121
+ 'content ' => $ this ->normalizeContent ( $ this -> getPageContent ($ page) ),
83
122
'sectionid ' => $ sectionId ,
84
123
'sectionnumber ' => 1 ,
85
124
'display ' => 0 ,
@@ -117,6 +156,24 @@ private function createPageXml(array $pageData, string $pageDir): void
117
156
$ this ->createXmlFile ('page ' , $ xmlContent , $ pageDir );
118
157
}
119
158
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
+
120
177
/**
121
178
* Retrieves the content of the page.
122
179
*/
0 commit comments