|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
namespace moodleexport; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ResourceExport. |
|
9
|
|
|
* |
|
10
|
|
|
* Handles the export of resources within a course. |
|
11
|
|
|
*/ |
|
12
|
|
|
class ResourceExport extends ActivityExport |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Export a resource to the specified directory. |
|
16
|
|
|
* |
|
17
|
|
|
* @param int $activityId The ID of the resource. |
|
18
|
|
|
* @param string $exportDir The directory where the resource 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 resource export will be saved |
|
25
|
|
|
$resourceDir = $this->prepareActivityDirectory($exportDir, 'resource', $moduleId); |
|
26
|
|
|
|
|
27
|
|
|
// Retrieve resource data |
|
28
|
|
|
$resourceData = $this->getData($activityId, $sectionId); |
|
29
|
|
|
|
|
30
|
|
|
// Generate XML files |
|
31
|
|
|
$this->createResourceXml($resourceData, $resourceDir); |
|
32
|
|
|
$this->createModuleXml($resourceData, $resourceDir); |
|
33
|
|
|
$this->createGradesXml($resourceData, $resourceDir); |
|
34
|
|
|
$this->createFiltersXml($resourceData, $resourceDir); |
|
35
|
|
|
$this->createGradeHistoryXml($resourceData, $resourceDir); |
|
36
|
|
|
$this->createInforefXml($resourceData, $resourceDir); |
|
37
|
|
|
$this->createRolesXml($resourceData, $resourceDir); |
|
38
|
|
|
$this->createCommentsXml($resourceData, $resourceDir); |
|
39
|
|
|
$this->createCalendarXml($resourceData, $resourceDir); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Create the XML file for the resource. |
|
44
|
|
|
*/ |
|
45
|
|
|
private function createResourceXml(array $resourceData, string $resourceDir): void |
|
46
|
|
|
{ |
|
47
|
|
|
$xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL; |
|
48
|
|
|
$xmlContent .= '<activity id="' . $resourceData['id'] . '" moduleid="' . $resourceData['moduleid'] . '" modulename="resource" contextid="' . $resourceData['contextid'] . '">' . PHP_EOL; |
|
49
|
|
|
$xmlContent .= ' <resource id="' . $resourceData['id'] . '">' . PHP_EOL; |
|
50
|
|
|
$xmlContent .= ' <name>' . htmlspecialchars($resourceData['name']) . '</name>' . PHP_EOL; |
|
51
|
|
|
$xmlContent .= ' <intro>' . htmlspecialchars($resourceData['intro']) . '</intro>' . PHP_EOL; |
|
52
|
|
|
$xmlContent .= ' <introformat>1</introformat>' . PHP_EOL; |
|
53
|
|
|
$xmlContent .= ' <tobemigrated>0</tobemigrated>' . PHP_EOL; |
|
54
|
|
|
$xmlContent .= ' <legacyfiles>0</legacyfiles>' . PHP_EOL; |
|
55
|
|
|
$xmlContent .= ' <legacyfileslast>$@NULL@$</legacyfileslast>' . PHP_EOL; |
|
56
|
|
|
$xmlContent .= ' <display>0</display>' . PHP_EOL; |
|
57
|
|
|
$xmlContent .= ' <displayoptions>a:1:{s:10:"printintro";i:1;}</displayoptions>' . PHP_EOL; |
|
58
|
|
|
$xmlContent .= ' <filterfiles>0</filterfiles>' . PHP_EOL; |
|
59
|
|
|
$xmlContent .= ' <revision>1</revision>' . PHP_EOL; |
|
60
|
|
|
$xmlContent .= ' <timemodified>' . $resourceData['timemodified'] . '</timemodified>' . PHP_EOL; |
|
61
|
|
|
$xmlContent .= ' </resource>' . PHP_EOL; |
|
62
|
|
|
$xmlContent .= '</activity>' . PHP_EOL; |
|
63
|
|
|
|
|
64
|
|
|
$this->createXmlFile('resource', $xmlContent, $resourceDir); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Creates the inforef.xml file, referencing users and files associated with the activity. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $references Contains 'users' and 'files' arrays to reference in the XML. |
|
71
|
|
|
* @param string $directory The directory where the XML file will be saved. |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function createInforefXml(array $references, string $directory): void |
|
74
|
|
|
{ |
|
75
|
|
|
$xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL; |
|
76
|
|
|
$xmlContent .= '<inforef>' . PHP_EOL; |
|
77
|
|
|
|
|
78
|
|
|
$xmlContent .= ' <fileref>' . PHP_EOL; |
|
79
|
|
|
$xmlContent .= ' <file>' . PHP_EOL; |
|
80
|
|
|
$xmlContent .= ' <id>' . htmlspecialchars($references['id']) . '</id>' . PHP_EOL; |
|
81
|
|
|
$xmlContent .= ' </file>' . PHP_EOL; |
|
82
|
|
|
$xmlContent .= ' </fileref>' . PHP_EOL; |
|
83
|
|
|
|
|
84
|
|
|
$xmlContent .= '</inforef>' . PHP_EOL; |
|
85
|
|
|
|
|
86
|
|
|
// Save the XML content to the directory |
|
87
|
|
|
$this->createXmlFile('inforef', $xmlContent, $directory); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get resource data dynamically from the course. |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getData(int $resourceId, int $sectionId): array |
|
94
|
|
|
{ |
|
95
|
|
|
$resource = $this->course->resources[RESOURCE_DOCUMENT][$resourceId]; |
|
96
|
|
|
|
|
97
|
|
|
return [ |
|
98
|
|
|
'id' => $resourceId, |
|
99
|
|
|
'moduleid' => $resource->source_id, |
|
100
|
|
|
'modulename' => 'resource', |
|
101
|
|
|
'contextid' => $resource->source_id, |
|
102
|
|
|
'name' => $resource->title, |
|
103
|
|
|
'intro' => $resource->comment ?? '', |
|
104
|
|
|
'sectionid' => $sectionId, |
|
105
|
|
|
'sectionnumber' => 1, |
|
106
|
|
|
'timemodified' => time(), |
|
107
|
|
|
'users' => [], |
|
108
|
|
|
'files' => [], |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|