Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions main/inc/lib/moodleexport/ActivityExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
abstract class ActivityExport
{
protected $course;
public const DOCS_MODULE_ID = 1000000;

public function __construct($course)
{
Expand Down Expand Up @@ -252,4 +253,26 @@ protected function createCalendarXml(array $activityData, string $destinationDir

$this->createXmlFile('calendar', $xmlContent, $destinationDir);
}

/**
* Returns the title of the item in the LP (if it exists); otherwise, $fallback.
*/
protected function lpItemTitle(int $sectionId, string $itemType, int $resourceId, ?string $fallback): string
{
if (!isset($this->course->resources[RESOURCE_LEARNPATH])) {
return $fallback ?? '';
}
foreach ($this->course->resources[RESOURCE_LEARNPATH] as $lp) {
if ((int) $lp->source_id !== $sectionId || empty($lp->items)) {
continue;
}
foreach ($lp->items as $it) {
$type = $it['item_type'] === 'student_publication' ? 'work' : $it['item_type'];
if ($type === $itemType && (int) $it['path'] === $resourceId) {
return $it['title'] ?? ($fallback ?? '');
}
}
}
return $fallback ?? '';
}
}
9 changes: 5 additions & 4 deletions main/inc/lib/moodleexport/FileExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,23 @@ private function getFileData(object $document): array
/**
* Get file data for files inside a folder.
*/
private function getFolderFileData(array $file, int $sourceId, string $parentPath = '/Documents/'): array
private function getFolderFileData(array $file, int $sourceId, string $parentPath = '/'): array
{
$adminData = MoodleExport::getAdminUserData();
$adminId = $adminData['id'];
$contenthash = hash('sha1', basename($file['path']));
$mimetype = $this->getMimeType($file['path']);
$filename = basename($file['path']);
$filepath = $this->ensureTrailingSlash($parentPath);
$relDir = dirname($file['path']);
$filepath = $this->ensureTrailingSlash($relDir === '.' ? '/' : '/'.$relDir.'/');

return [
'id' => $file['id'],
'contenthash' => $contenthash,
'contextid' => $sourceId,
'contextid' => ActivityExport::DOCS_MODULE_ID,
'component' => 'mod_folder',
'filearea' => 'content',
'itemid' => (int) $file['id'],
'itemid' => ActivityExport::DOCS_MODULE_ID,
'filepath' => $filepath,
'documentpath' => 'document/'.$file['path'],
'filename' => $filename,
Expand Down
22 changes: 9 additions & 13 deletions main/inc/lib/moodleexport/FolderExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public function export($activityId, $exportDir, $moduleId, $sectionId): void
*/
public function getData(int $folderId, int $sectionId): ?array
{
if ($folderId === 0) {
if ($folderId === 0 || $folderId === ActivityExport::DOCS_MODULE_ID) {
return [
'id' => 0,
'moduleid' => 0,
'id' => ActivityExport::DOCS_MODULE_ID,
'moduleid' => ActivityExport::DOCS_MODULE_ID,
'modulename' => 'folder',
'contextid' => 0,
'contextid' => ActivityExport::DOCS_MODULE_ID,
'name' => 'Documents',
'sectionid' => $sectionId,
'timemodified' => time(),
Expand Down Expand Up @@ -98,17 +98,13 @@ private function createFolderXml(array $folderData, string $folderDir): void
private function getFilesForFolder(int $folderId): array
{
$files = [];
if ($folderId === 0) {
if ($folderId === ActivityExport::DOCS_MODULE_ID) {
foreach ($this->course->resources[RESOURCE_DOCUMENT] as $doc) {
if ($doc->file_type === 'file') {
$files[] = [
'id' => (int) $doc->source_id,
'contenthash' => hash('sha1', basename($doc->path)),
'filename' => basename($doc->path),
'filepath' => '/Documents/',
'filesize' => (int) $doc->size,
'mimetype' => $this->getMimeType($doc->path),
];
$ext = strtolower(pathinfo($doc->path, PATHINFO_EXTENSION));
if (!in_array($ext, ['html', 'htm'], true)) {
$files[] = ['id' => (int) $doc->source_id];
}
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions main/inc/lib/moodleexport/ForumExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function export($activityId, $exportDir, $moduleId, $sectionId): void
*/
public function getData(int $forumId, int $sectionId): ?array
{
$forum = $this->course->resources['forum'][$forumId]->obj;
$forum = $this->course->resources[RESOURCE_FORUM][$forumId]->obj;

$adminData = MoodleExport::getAdminUserData();
$adminId = $adminData['id'];
Expand Down Expand Up @@ -78,14 +78,17 @@ public function getData(int $forumId, int $sectionId): ?array
}
}

$fileIds = [];
$name = $forum->forum_title ?? '';
if ($sectionId > 0) {
$name = $this->lpItemTitle($sectionId, RESOURCE_FORUM, $forumId, $name);
}

return [
'id' => $forumId,
'moduleid' => $forumId,
'modulename' => 'forum',
'contextid' => $this->course->info['real_id'],
'name' => $forum->forum_title,
'name' => $name,
'description' => $forum->forum_comment,
'timecreated' => time(),
'timemodified' => time(),
Expand All @@ -94,7 +97,7 @@ public function getData(int $forumId, int $sectionId): ?array
'userid' => $adminId,
'threads' => $threads,
'users' => [$adminId],
'files' => $fileIds,
'files' => [],
];
}

Expand Down
Loading
Loading