Skip to content

Commit 300e9ca

Browse files
Internal: Import Moodle label as chapter in lesson - refs BT#21977
1 parent 881bea1 commit 300e9ca

File tree

1 file changed

+104
-34
lines changed

1 file changed

+104
-34
lines changed

main/inc/lib/MoodleImport.php

Lines changed: 104 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ public function import($uploadedFile)
153153
if ($debug) {
154154
error_log('Loading activities: '.count($activities));
155155
}
156-
$n = 1;
156+
157+
$previousItemId = 0;
158+
$lpItemId = null;
157159
foreach ($activities as $activity) {
158160
if (empty($activity->childNodes->length)) {
159161
continue;
@@ -185,8 +187,7 @@ public function import($uploadedFile)
185187

186188
// It is added as item in Learnpath
187189
if (!empty($currentItem['sectionid']) && !empty($assignId)) {
188-
$this->processSectionItem($sectionLpValues[$currentItem['sectionid']]['lpId'], 'student_publication', $assignId, $moduleValues['name'], $n);
189-
$n++;
190+
$lpItemId = $this->processSectionItem($sectionLpValues[$currentItem['sectionid']]['lpId'], 'student_publication', $assignId, $moduleValues['name'], $previousItemId);
190191
}
191192
break;
192193
case 'scorm':
@@ -202,6 +203,44 @@ public function import($uploadedFile)
202203
$this->processGlossary($moduleValues, $currentItem['moduleid'], $allFiles, '');
203204
break;
204205
case 'label':
206+
$moduleDir = $currentItem['directory'];
207+
$moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml');
208+
$moduleValues = $this->readHtmlModule($moduleXml, $moduleName);
209+
$sectionPath = isset($currentItem['sectionid']) ? '/'.$sectionLpValues[$currentItem['sectionid']]['sectionPath'].'/' : '/';
210+
$contextId = $moduleValues['attributes']['contextid'];
211+
if (isset($currentItem['sectionid'])) {
212+
$sectionLp = $sectionLpValues[$currentItem['sectionid']];
213+
$lpId = $sectionLp['lpId'];
214+
$chapterTitle = $moduleValues['name'] ?? 'Capítulo sin título';
215+
if (!empty($lpId)) {
216+
$lp = new \learnpath(
217+
api_get_course_id(),
218+
$lpId,
219+
api_get_user_id()
220+
);
221+
$lpItemId = $lp->add_item(
222+
0,
223+
$previousItemId,
224+
'dir',
225+
0,
226+
$chapterTitle,
227+
'',
228+
0,
229+
0,
230+
0,
231+
0
232+
);
233+
}
234+
} else {
235+
if (isset($allFiles[$contextId])) {
236+
$importedFiles = $this->processSectionMultimedia($allFiles[$contextId], $sectionPath);
237+
}
238+
$documentId = $this->processHtmlDocument($moduleValues, $moduleName, $importedFiles, $sectionPath);
239+
if (!empty($currentItem['sectionid']) && !empty($documentId)) {
240+
$lpItemId = $this->processSectionItem($sectionLpValues[$currentItem['sectionid']]['lpId'], 'document', $documentId, $moduleValues['name'], $previousItemId);
241+
}
242+
}
243+
break;
205244
case 'page':
206245
$moduleDir = $currentItem['directory'];
207246
$moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml');
@@ -215,8 +254,7 @@ public function import($uploadedFile)
215254

216255
// It is added as item in Learnpath
217256
if (!empty($currentItem['sectionid']) && !empty($documentId)) {
218-
$this->processSectionItem($sectionLpValues[$currentItem['sectionid']]['lpId'], 'document', $documentId, $moduleValues['name'], $n);
219-
$n++;
257+
$lpItemId = $this->processSectionItem($sectionLpValues[$currentItem['sectionid']]['lpId'], 'document', $documentId, $moduleValues['name'], $previousItemId);
220258
}
221259
break;
222260
case 'forum':
@@ -288,8 +326,7 @@ public function import($uploadedFile)
288326
}
289327
// It is added as item in Learnpath
290328
if (!empty($currentItem['sectionid']) && !empty($forumId)) {
291-
$this->processSectionItem($sectionLpValues[$currentItem['sectionid']]['lpId'], 'forum', $forumId, $moduleValues['name'], $n);
292-
$n++;
329+
$lpItemId = $this->processSectionItem($sectionLpValues[$currentItem['sectionid']]['lpId'], 'forum', $forumId, $moduleValues['name'], $previousItemId);
293330
}
294331
break;
295332
case 'quiz':
@@ -410,8 +447,7 @@ public function import($uploadedFile)
410447

411448
// Add to learnpath if applicable
412449
if (!empty($currentItem['sectionid']) && !empty($exercise->iid)) {
413-
$this->processSectionItem($sectionLpValues[$currentItem['sectionid']]['lpId'], 'quiz', $exercise->iid, $title, $n);
414-
$n++;
450+
$lpItemId = $this->processSectionItem($sectionLpValues[$currentItem['sectionid']]['lpId'], 'quiz', $exercise->iid, $title, $previousItemId);
415451
}
416452
break;
417453
case 'folder':
@@ -445,8 +481,7 @@ public function import($uploadedFile)
445481
}
446482
$sectionPath = isset($sectionLpValues[$currentItem['sectionid']]) ? $sectionLpValues[$currentItem['sectionid']]['sectionPath'] : '';
447483
$lpId = (int) $sectionLpValues[$currentItem['sectionid']]['lpId'];
448-
$this->processSectionFolderModule($mainFileModuleValues, $sectionPath, $moduleValues['name'], $resourcesFiles, $lpId, $n);
449-
$n++;
484+
$this->processSectionFolderModule($mainFileModuleValues, $sectionPath, $moduleValues['name'], $resourcesFiles, $lpId, $previousItemId);
450485

451486
break;
452487
case 'resource':
@@ -487,8 +522,7 @@ public function import($uploadedFile)
487522
if (!empty($currentItem['sectionid'])) {
488523
$lpId = $sectionLpValues[$currentItem['sectionid']]['lpId'];
489524
}
490-
$importedFiles = $this->processSectionMultimedia($resourcesFiles, $sectionPath, $lpId, $n);
491-
$n++;
525+
$importedFiles = $this->processSectionMultimedia($resourcesFiles, $sectionPath, $lpId, $previousItemId);
492526
}
493527

494528
break;
@@ -527,11 +561,16 @@ public function import($uploadedFile)
527561
$linkId = Link::addlinkcategory('link');
528562
// It is added as item in Learnpath
529563
if (!empty($currentItem['sectionid']) && !empty($linkId)) {
530-
$this->processSectionItem($sectionLpValues[$currentItem['sectionid']]['lpId'], 'link', $linkId, $moduleValues['name'], $n);
531-
$n++;
564+
$lpItemId = $this->processSectionItem($sectionLpValues[$currentItem['sectionid']]['lpId'], 'link', $linkId, $moduleValues['name'], $previousItemId);
532565
}
533566
break;
534567
}
568+
569+
if (!empty($previousItemId)) {
570+
$this->updateLpItemNextId($previousItemId, $lpItemId);
571+
$this->updateLpItemPreviousId($lpItemId, $previousItemId);
572+
}
573+
$previousItemId = $lpItemId;
535574
}
536575

537576
if (!empty($sectionLpValues)) {
@@ -612,17 +651,17 @@ public function replaceMoodleChamiloCoursePath($text, $sectionPath = '')
612651
}
613652

614653
/**
615-
* It adds module item by section as learnpath item.
654+
* Adds an item to a learning path section and processes its relationships.
616655
*
617-
* @param $lpId
618-
* @param $itemType
619-
* @param $itemId
620-
* @param $itemTitle
621-
* @param int $dspOrder
656+
* @param int $lpId The ID of the learning path.
657+
* @param string $itemType The type of the item (e.g., quiz, document).
658+
* @param int $itemId The ID of the item to be added.
659+
* @param string $itemTitle The title of the item.
660+
* @param int|null $previousItemId The ID of the previous item (optional).
622661
*
623-
* @return void
662+
* @return int The ID of the newly added learning path item.
624663
*/
625-
public function processSectionItem($lpId, $itemType, $itemId, $itemTitle, $dspOrder = 0)
664+
public function processSectionItem($lpId, $itemType, $itemId, $itemTitle, $previousItemId = null): int
626665
{
627666
$lp = new \learnpath(
628667
api_get_course_id(),
@@ -632,16 +671,18 @@ public function processSectionItem($lpId, $itemType, $itemId, $itemTitle, $dspOr
632671

633672
$lpItemId = $lp->add_item(
634673
0,
635-
0,
674+
$previousItemId,
636675
$itemType,
637676
$itemId,
638677
$itemTitle,
639678
'',
640679
0,
641680
0,
642681
0,
643-
$dspOrder
682+
0
644683
);
684+
685+
return $lpItemId;
645686
}
646687

647688
/**
@@ -1178,7 +1219,7 @@ public function processSectionFolderModule($mainFileModuleValues, $sectionPath,
11781219
*
11791220
* @return array
11801221
*/
1181-
public function processSectionMultimedia($files, $sectionPath, $lpId = 0, $n = 0)
1222+
public function processSectionMultimedia($files, $sectionPath, $lpId = 0, $previousItemId = 0)
11821223
{
11831224
$importedFiles = [];
11841225
if (!empty($files)) {
@@ -1220,7 +1261,13 @@ public function processSectionMultimedia($files, $sectionPath, $lpId = 0, $n = 0
12201261
$importedFiles[$file['file']['name']] = basename($data['path']);
12211262
// It is added as item in Learnpath
12221263
if (!empty($lpId) && !empty($data['iid'])) {
1223-
$this->processSectionItem($lpId, 'document', $data['iid'], $title, $n);
1264+
$currentItemId = $this->processSectionItem($lpId, 'document', $data['iid'], $title, $previousItemId);
1265+
if (!empty($previousItemId)) {
1266+
$this->updateLpItemNextId($previousItemId, $currentItemId);
1267+
$this->updateLpItemPreviousId($currentItemId, $previousItemId);
1268+
}
1269+
1270+
$previousItemId = $currentItemId;
12241271
}
12251272
}
12261273
}
@@ -1290,7 +1337,7 @@ public function processLesson($moduleValues, $allFiles = [])
12901337
$pageValues['content'] = $item['contents'];
12911338
$sectionPath = '/'.$dirName.'/';
12921339
$documentId = $this->processHtmlDocument($pageValues, 'page', $importedFiles, $sectionPath);
1293-
$this->processSectionItem($lpId, 'document', $documentId, $pageValues['name']);
1340+
$lpItemId = $this->processSectionItem($lpId, 'document', $documentId, $pageValues['name']);
12941341
break;
12951342
case 'essay':
12961343
case 'match':
@@ -1344,7 +1391,7 @@ public function processLesson($moduleValues, $allFiles = [])
13441391
// Create the new Quiz
13451392
$exercise->save();
13461393

1347-
$this->processSectionItem($lpId, 'quiz', $exercise->iid, $quizLpName);
1394+
$lpItemId = $this->processSectionItem($lpId, 'quiz', $exercise->iid, $quizLpName);
13481395

13491396
// Ok, we got the Quiz and create it, now its time to add the Questions
13501397
foreach ($questionList as $question) {
@@ -2584,7 +2631,7 @@ public function processTrueFalse(
25842631
$importedFiles,
25852632
$sectionPath = ''
25862633
) {
2587-
$correct = (int) $answerValues['fraction'] ? (int) $answerValues['fraction'] : 0;
2634+
$correct = isset($answerValues['fraction']) ? (int) $answerValues['fraction'] : 0;
25882635
$answer = $answerValues['answertext'];
25892636
$comment = $answerValues['feedback'];
25902637
$weighting = $answerValues['fraction'];
@@ -2603,10 +2650,7 @@ public function processTrueFalse(
26032650
$goodAnswer,
26042651
$comment,
26052652
$weighting,
2606-
$position,
2607-
null,
2608-
null,
2609-
''
2653+
$position
26102654
);
26112655
}
26122656

@@ -2701,6 +2745,32 @@ public function processFillBlanks(
27012745
}
27022746
}
27032747

2748+
/**
2749+
* Updates the previous item ID for a given learning path item.
2750+
*
2751+
* @param int $currentItemId The ID of the current item.
2752+
* @param int $previousItemId The ID of the previous item to be set.
2753+
*/
2754+
public function updateLpItemPreviousId($currentItemId, $previousItemId)
2755+
{
2756+
$table = Database::get_course_table(TABLE_LP_ITEM);
2757+
$sql = "UPDATE $table SET previous_item_id = $previousItemId WHERE id = $currentItemId";
2758+
Database::query($sql);
2759+
}
2760+
2761+
/**
2762+
* Updates the next item ID for a given learning path item.
2763+
*
2764+
* @param int $previousItemId The ID of the previous item.
2765+
* @param int $currentItemId The ID of the current item to be set as next.
2766+
*/
2767+
public function updateLpItemNextId($previousItemId, $currentItemId)
2768+
{
2769+
$table = Database::get_course_table(TABLE_LP_ITEM);
2770+
$sql = "UPDATE $table SET next_item_id = $currentItemId WHERE id = $previousItemId";
2771+
Database::query($sql);
2772+
}
2773+
27042774
/**
27052775
* get All files associated with a question.
27062776
*

0 commit comments

Comments
 (0)