Skip to content

Commit

Permalink
Course Import - Improve import resources and document folders from Mo…
Browse files Browse the repository at this point in the history
…odle - refs BT#20060
  • Loading branch information
cfasanando committed Dec 12, 2022
1 parent 3c35788 commit 25250c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 39 deletions.
48 changes: 15 additions & 33 deletions main/inc/lib/MoodleImport.php
Expand Up @@ -1120,9 +1120,10 @@ public function processSectionFolderModule($mainFileModuleValues, $sectionPath,
$path = $file['file']['filepath'];
if (1 == count(explode('/', trim($path, '/')))) {
$safePath = api_replace_dangerous_char($path);
$newSafePath = !empty($safePath) ? '/'.$sectionPath.'/'.$safeMainFolderName.'/'.$safePath : '/'.$sectionPath.'/'.$safeMainFolderName;
$data = DocumentManager::upload_document(
$file,
'/'.$sectionPath.'/'.$safeMainFolderName.'/'.$safePath,
$newSafePath,
$title,
'',
null,
Expand Down Expand Up @@ -1887,48 +1888,29 @@ public function readMainFilesXml($filesXml, $contextId)
}

$activities = $moduleDoc->getElementsByTagName('file');
$currentItem = [];
$filesInfo = [];
$i = 0;
foreach ($activities as $activity) {
if (empty($activity->childNodes->length)) {
continue;
}
$isThisItemThatIWant = false;
foreach ($activity->childNodes as $item) {
if (!$isThisItemThatIWant && $item->nodeName == 'contenthash') {
$currentItem['contenthash'] = $item->nodeValue;
}
if ($item->nodeName == 'contextid' &&
(int) $item->nodeValue == (int) $contextId &&
!$isThisItemThatIWant
) {
$isThisItemThatIWant = true;
continue;
}

if ($isThisItemThatIWant && $item->nodeName == 'filename') {
$currentItem['filename'] = $item->nodeValue;
}

if ($isThisItemThatIWant && $item->nodeName == 'filesize') {
$currentItem['filesize'] = $item->nodeValue;
}

if ($isThisItemThatIWant && $item->nodeName == 'mimetype' &&
$item->nodeValue == 'document/unknown'
) {
break;
if (in_array($item->nodeName, ['filename', 'filesize', 'contenthash', 'contextid', 'filesize', 'mimetype'])) {
$filesInfo[$i][$item->nodeName] = $item->nodeValue;
}

if ($isThisItemThatIWant && $item->nodeName == 'mimetype' &&
$item->nodeValue !== 'document/unknown'
) {
$currentItem['mimetype'] = $item->nodeValue;
break 2;
}
$i++;
}
$currentItem = [];
if (!empty($filesInfo)) {
foreach ($filesInfo as $info) {
if (!empty($info['filesize'])) {
$currentItem[$info['contextid']] = $info;
}
}
}

return $currentItem;
return $currentItem[$contextId];
}

/**
Expand Down
13 changes: 7 additions & 6 deletions main/inc/lib/document.lib.php
Expand Up @@ -5148,19 +5148,20 @@ public static function build_directory_selector(
$label = ' — '.$folder_titles[$folder];
}
$label = Security::remove_XSS($label);
$foldersSortedByTitles[$folder_titles[$folder]] = [
$foldersSortedByTitles[$folder_id] = [
'id' => $folder_id,
'title' => $folder_titles[$folder],
'selected' => $selected,
'label' => $label,
];
}
foreach ($folder_titles as $title) {
foreach ($folders as $id => $title) {
$parent_select->addOption(
$foldersSortedByTitles[$title]['label'],
$foldersSortedByTitles[$title]['id']
$foldersSortedByTitles[$id]['label'],
$foldersSortedByTitles[$id]['id']
);
if ($foldersSortedByTitles[$title]['selected'] != '') {
$parent_select->setSelected($foldersSortedByTitles[$title]['id']);
if ($foldersSortedByTitles[$id]['selected'] != '') {
$parent_select->setSelected($foldersSortedByTitles[$id]['id']);
}
}
}
Expand Down

0 comments on commit 25250c8

Please sign in to comment.