4
4
5
5
namespace moodleexport ;
6
6
7
+ use Chamilo \CourseBundle \Component \CourseCopy \CourseBuilder ;
7
8
use Exception ;
8
9
use RecursiveDirectoryIterator ;
9
10
use RecursiveIteratorIterator ;
@@ -25,7 +26,18 @@ class MoodleExport
25
26
*/
26
27
public function __construct (object $ course )
27
28
{
29
+ // Build the complete course object
30
+ $ cb = new CourseBuilder ('complete ' );
31
+ $ complete = $ cb ->build ();
32
+
33
+ // Store the selected course
28
34
$ this ->course = $ course ;
35
+
36
+ // Fill missing resources from learnpath
37
+ $ this ->fillResourcesFromLearnpath ($ complete );
38
+
39
+ // Fill missing quiz questions
40
+ $ this ->fillQuestionsFromQuiz ($ complete );
29
41
}
30
42
31
43
/**
@@ -177,6 +189,67 @@ public static function getAdminUserData(): array
177
189
return self ::$ adminUserData ;
178
190
}
179
191
192
+ /**
193
+ * Fills missing resources from the learnpath into the course structure.
194
+ *
195
+ * This method checks if the course has a learnpath and ensures that all
196
+ * referenced resources (documents, quizzes, etc.) exist in the course's
197
+ * resources array by pulling them from the complete course object.
198
+ */
199
+ private function fillResourcesFromLearnpath (object $ complete ): void
200
+ {
201
+ // Check if the course has learnpath
202
+ if (!isset ($ this ->course ->resources ['learnpath ' ])) {
203
+ return ;
204
+ }
205
+
206
+ foreach ($ this ->course ->resources ['learnpath ' ] as $ learnpathId => $ learnpath ) {
207
+ if (!isset ($ learnpath ->items )) {
208
+ continue ;
209
+ }
210
+
211
+ foreach ($ learnpath ->items as $ item ) {
212
+ $ type = $ item ['item_type ' ]; // Resource type (document, quiz, etc.)
213
+ $ resourceId = $ item ['path ' ]; // Resource ID in resources
214
+
215
+ // Check if the resource exists in the complete object and is not yet in the course resources
216
+ if (isset ($ complete ->resources [$ type ][$ resourceId ]) && !isset ($ this ->course ->resources [$ type ][$ resourceId ])) {
217
+ // Add the resource directly to the original course resources structure
218
+ $ this ->course ->resources [$ type ][$ resourceId ] = $ complete ->resources [$ type ][$ resourceId ];
219
+ }
220
+ }
221
+ }
222
+ }
223
+
224
+ /**
225
+ * Fills missing exercise questions related to quizzes in the course.
226
+ *
227
+ * This method checks if the course has quizzes and ensures that all referenced
228
+ * questions exist in the course's resources array by pulling them from the complete
229
+ * course object.
230
+ */
231
+ private function fillQuestionsFromQuiz (object $ complete ): void
232
+ {
233
+ // Check if the course has quizzes
234
+ if (!isset ($ this ->course ->resources ['quiz ' ])) {
235
+ return ;
236
+ }
237
+
238
+ foreach ($ this ->course ->resources ['quiz ' ] as $ quizId => $ quiz ) {
239
+ if (!isset ($ quiz ->obj ->question_ids )) {
240
+ continue ;
241
+ }
242
+
243
+ foreach ($ quiz ->obj ->question_ids as $ questionId ) {
244
+ // Check if the question exists in the complete object and is not yet in the course resources
245
+ if (isset ($ complete ->resources ['Exercise_Question ' ][$ questionId ]) && !isset ($ this ->course ->resources ['Exercise_Question ' ][$ questionId ])) {
246
+ // Add the question directly to the original course resources structure
247
+ $ this ->course ->resources ['Exercise_Question ' ][$ questionId ] = $ complete ->resources ['Exercise_Question ' ][$ questionId ];
248
+ }
249
+ }
250
+ }
251
+ }
252
+
180
253
/**
181
254
* Export root XML files such as badges, completion, gradebook, etc.
182
255
*/
@@ -359,6 +432,14 @@ private function getActivities(): array
359
432
$ activities = [];
360
433
$ glossaryAdded = false ;
361
434
435
+ $ documentsFolder = [
436
+ 'id ' => 0 ,
437
+ 'sectionid ' => 0 ,
438
+ 'modulename ' => 'folder ' ,
439
+ 'moduleid ' => 0 ,
440
+ 'title ' => 'Documents ' ,
441
+ ];
442
+ $ activities [] = $ documentsFolder ;
362
443
foreach ($ this ->course ->resources as $ resourceType => $ resources ) {
363
444
foreach ($ resources as $ resource ) {
364
445
$ exportClass = null ;
@@ -403,36 +484,27 @@ private function getActivities(): array
403
484
$ moduleName = 'page ' ;
404
485
$ id = $ resource ->source_id ;
405
486
$ title = $ document ['title ' ];
406
- } elseif ('file ' === $ resource ->file_type ) {
407
- $ isRoot = substr_count ($ resource ->path , '/ ' ) === 1 ;
408
-
409
- if ($ isRoot ) {
410
- $ exportClass = ResourceExport::class;
411
- $ moduleName = 'resource ' ;
412
- $ id = $ resource ->source_id ;
413
- $ title = $ resource ->title ;
414
- }
415
- } elseif ('folder ' === $ resource ->file_type ) {
416
- $ isEmpty = true ;
417
- $ folderPath = $ resource ->path .'/ ' ;
418
-
419
- foreach ($ this ->course ->resources ['document ' ] as $ childResource ) {
420
- if (str_starts_with ($ childResource ->path , $ folderPath ) && $ childResource ->path !== $ resource ->path ) {
421
- $ isEmpty = false ;
422
- break ;
487
+ }
488
+ if ('file ' === $ resource ->file_type ) {
489
+ $ resourceExport = new ResourceExport ($ this ->course );
490
+ if ($ resourceExport ->getSectionIdForActivity ($ resource ->source_id , $ resourceType ) > 0 ) {
491
+ $ isRoot = substr_count ($ resource ->path , '/ ' ) === 1 ;
492
+ if ($ isRoot ) {
493
+ $ exportClass = ResourceExport::class;
494
+ $ moduleName = 'resource ' ;
495
+ $ id = $ resource ->source_id ;
496
+ $ title = $ resource ->title ;
423
497
}
424
498
}
425
-
426
- $ isRoot = substr_count ($ resource ->path , '/ ' ) === 1 ;
427
-
428
- if (!$ isEmpty && $ isRoot ) {
429
- $ exportClass = FolderExport::class;
430
- $ moduleName = 'folder ' ;
431
- $ id = $ resource ->source_id ;
432
- $ title = $ resource ->title ;
433
- }
434
499
}
435
500
}
501
+ // Handle course introduction (page)
502
+ elseif ($ resourceType === RESOURCE_TOOL_INTRO && $ resource ->source_id == 'course_homepage ' ) {
503
+ $ exportClass = PageExport::class;
504
+ $ moduleName = 'page ' ;
505
+ $ id = 0 ;
506
+ $ title = get_lang ('Introduction ' );
507
+ }
436
508
// Handle assignments (work)
437
509
elseif ($ resourceType === RESOURCE_WORK && $ resource ->source_id > 0 ) {
438
510
$ exportClass = AssignExport::class;
0 commit comments