1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
namespace moodleexport; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class FolderExport. |
9
|
|
|
* |
10
|
|
|
* Handles the export of folders within a course. |
11
|
|
|
*/ |
12
|
|
|
class FolderExport extends ActivityExport |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Export a folder to the specified directory. |
16
|
|
|
* |
17
|
|
|
* @param int $activityId The ID of the folder. |
18
|
|
|
* @param string $exportDir The directory where the folder will be exported. |
19
|
|
|
* @param int $moduleId The ID of the module. |
20
|
|
|
* @param int $sectionId The ID of the section. |
21
|
|
|
*/ |
22
|
|
|
public function export($activityId, $exportDir, $moduleId, $sectionId): void |
23
|
|
|
{ |
24
|
|
|
// Prepare the directory where the folder export will be saved |
25
|
|
|
$folderDir = $this->prepareActivityDirectory($exportDir, 'folder', $moduleId); |
26
|
|
|
|
27
|
|
|
// Retrieve folder data |
28
|
|
|
$folderData = $this->getData($activityId, $sectionId); |
29
|
|
|
|
30
|
|
|
// Generate XML files |
31
|
|
|
$this->createFolderXml($folderData, $folderDir); |
32
|
|
|
$this->createModuleXml($folderData, $folderDir); |
33
|
|
|
$this->createGradesXml($folderData, $folderDir); |
34
|
|
|
$this->createFiltersXml($folderData, $folderDir); |
35
|
|
|
$this->createGradeHistoryXml($folderData, $folderDir); |
36
|
|
|
$this->createInforefXml($this->getFilesForFolder($activityId), $folderDir); |
37
|
|
|
$this->createRolesXml($folderData, $folderDir); |
38
|
|
|
$this->createCommentsXml($folderData, $folderDir); |
39
|
|
|
$this->createCalendarXml($folderData, $folderDir); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create the XML file for the folder. |
44
|
|
|
*/ |
45
|
|
|
private function createFolderXml(array $folderData, string $folderDir): void |
46
|
|
|
{ |
47
|
|
|
$xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL; |
48
|
|
|
$xmlContent .= '<activity id="' . $folderData['id'] . '" moduleid="' . $folderData['moduleid'] . '" modulename="folder" contextid="' . $folderData['contextid'] . '">' . PHP_EOL; |
49
|
|
|
$xmlContent .= ' <folder id="' . $folderData['id'] . '">' . PHP_EOL; |
50
|
|
|
$xmlContent .= ' <name>' . htmlspecialchars($folderData['name']) . '</name>' . PHP_EOL; |
51
|
|
|
$xmlContent .= ' <intro></intro>' . PHP_EOL; |
52
|
|
|
$xmlContent .= ' <introformat>1</introformat>' . PHP_EOL; |
53
|
|
|
$xmlContent .= ' <revision>1</revision>' . PHP_EOL; |
54
|
|
|
$xmlContent .= ' <timemodified>' . $folderData['timemodified'] . '</timemodified>' . PHP_EOL; |
55
|
|
|
$xmlContent .= ' <display>0</display>' . PHP_EOL; |
56
|
|
|
$xmlContent .= ' <showexpanded>1</showexpanded>' . PHP_EOL; |
57
|
|
|
$xmlContent .= ' <showdownloadfolder>1</showdownloadfolder>' . PHP_EOL; |
58
|
|
|
$xmlContent .= ' <forcedownload>1</forcedownload>' . PHP_EOL; |
59
|
|
|
$xmlContent .= ' </folder>' . PHP_EOL; |
60
|
|
|
$xmlContent .= '</activity>'; |
61
|
|
|
|
62
|
|
|
$this->createXmlFile('folder', $xmlContent, $folderDir); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get folder data dynamically from the course. |
67
|
|
|
*/ |
68
|
|
|
public function getData(int $folderId, int $sectionId): array |
69
|
|
|
{ |
70
|
|
|
$folder = $this->course->resources['document'][$folderId]; |
71
|
|
|
|
72
|
|
|
return [ |
73
|
|
|
'id' => $folderId, |
74
|
|
|
'moduleid' => $folder->source_id, |
75
|
|
|
'modulename' => 'folder', |
76
|
|
|
'contextid' => $folder->source_id, |
77
|
|
|
'name' => $folder->title, |
78
|
|
|
'sectionid' => $sectionId, |
79
|
|
|
'timemodified' => time(), |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the list of files for a folder. |
85
|
|
|
*/ |
86
|
|
|
private function getFilesForFolder(int $folderId): array |
87
|
|
|
{ |
88
|
|
|
$documentData = \DocumentManager::getAllDocumentsByParentId($this->course->info, $folderId); |
89
|
|
|
|
90
|
|
|
$files = []; |
91
|
|
|
foreach ($documentData as $doc) { |
92
|
|
|
if ($doc['filetype'] === 'file') { |
93
|
|
|
$files[] = [ |
94
|
|
|
'id' => (int) $doc['id'], |
95
|
|
|
'contenthash' => 'hash' . $doc['id'], |
96
|
|
|
'filename' => $doc['basename'], |
97
|
|
|
'filepath' => $doc['path'], |
98
|
|
|
'filesize' => (int) $doc['size'], |
99
|
|
|
'mimetype' => $this->getMimeType($doc['basename']), |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return ['files' => $files]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the MIME type for a given file. |
109
|
|
|
*/ |
110
|
|
|
private function getMimeType(string $filename): string |
111
|
|
|
{ |
112
|
|
|
$ext = pathinfo($filename, PATHINFO_EXTENSION); |
113
|
|
|
$mimetypes = [ |
114
|
|
|
'pdf' => 'application/pdf', |
115
|
|
|
'png' => 'image/png', |
116
|
|
|
'jpg' => 'image/jpeg', |
117
|
|
|
'jpeg' => 'image/jpeg', |
118
|
|
|
'doc' => 'application/msword', |
119
|
|
|
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
return $mimetypes[$ext] ?? 'application/octet-stream'; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|