Passed
Push — master ( 68f627...939207 )
by
unknown
12:17 queued 03:34
created

ResourceExport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
c 1
b 0
f 0
dl 0
loc 115
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A export() 0 15 1
A createInforefXml() 0 14 1
A createResourceXml() 0 20 1
A getData() 0 33 2
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Component\CourseCopy\Moodle\Activities;
8
9
use const PHP_EOL;
10
11
/**
12
 * Handles the export of file resources within a course.
13
 */
14
class ResourceExport extends ActivityExport
15
{
16
    /**
17
     * Export a resource to the specified directory.
18
     *
19
     * @param int    $activityId the ID of the resource
20
     * @param string $exportDir  the directory where the resource will be exported
21
     * @param int    $moduleId   the ID of the module
22
     * @param int    $sectionId  the ID of the section
23
     */
24
    public function export($activityId, $exportDir, $moduleId, $sectionId): void
25
    {
26
        $resourceDir = $this->prepareActivityDirectory((string) $exportDir, 'resource', (int) $moduleId);
27
28
        $resourceData = $this->getData((int) $activityId, (int) $sectionId);
29
30
        $this->createResourceXml($resourceData, $resourceDir);
31
        $this->createModuleXml($resourceData, $resourceDir);
32
        $this->createGradesXml($resourceData, $resourceDir);
33
        $this->createFiltersXml($resourceData, $resourceDir);
34
        $this->createGradeHistoryXml($resourceData, $resourceDir);
35
        $this->createInforefXml($resourceData, $resourceDir);
36
        $this->createRolesXml($resourceData, $resourceDir);
37
        $this->createCommentsXml($resourceData, $resourceDir);
38
        $this->createCalendarXml($resourceData, $resourceDir);
39
    }
40
41
    /**
42
     * Get resource data dynamically from the course.
43
     *
44
     * @return array<string,mixed>
45
     */
46
    public function getData(int $resourceId, int $sectionId): array
47
    {
48
        $docBucket = $this->course->resources[RESOURCE_DOCUMENT] ?? [];
49
        $resource = $docBucket[$resourceId] ?? null;
50
51
        if (null === $resource) {
52
            return [
53
                'id' => $resourceId,
54
                'moduleid' => $resourceId,
55
                'modulename' => 'resource',
56
                'contextid' => 0,
57
                'name' => 'Resource '.$resourceId,
58
                'intro' => '',
59
                'sectionid' => $sectionId,
60
                'sectionnumber' => 1,
61
                'timemodified' => time(),
62
                'users' => [],
63
                'files' => [],
64
            ];
65
        }
66
67
        return [
68
            'id' => (int) $resourceId,
69
            'moduleid' => (int) $resource->source_id,
70
            'modulename' => 'resource',
71
            'contextid' => (int) $resource->source_id,
72
            'name' => (string) $resource->title,
73
            'intro' => (string) ($resource->comment ?? ''),
74
            'sectionid' => $sectionId,
75
            'sectionnumber' => 1,
76
            'timemodified' => time(),
77
            'users' => [],
78
            'files' => [],
79
        ];
80
    }
81
82
    /**
83
     * Creates the inforef.xml file. En V1 se escribía un único <file><id/> con $references['id'].
84
     * Mantenemos el comportamiento para compatibilidad.
85
     *
86
     * @param array<string,mixed> $references
87
     */
88
    protected function createInforefXml(array $references, string $directory): void
89
    {
90
        $fileId = (int) ($references['id'] ?? 0);
91
92
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
93
        $xmlContent .= '<inforef>'.PHP_EOL;
94
        $xmlContent .= '  <fileref>'.PHP_EOL;
95
        $xmlContent .= '    <file>'.PHP_EOL;
96
        $xmlContent .= '      <id>'.$fileId.'</id>'.PHP_EOL;
97
        $xmlContent .= '    </file>'.PHP_EOL;
98
        $xmlContent .= '  </fileref>'.PHP_EOL;
99
        $xmlContent .= '</inforef>'.PHP_EOL;
100
101
        $this->createXmlFile('inforef', $xmlContent, $directory);
102
    }
103
104
    /**
105
     * Create the XML file for the resource.
106
     *
107
     * @param array<string,mixed> $resourceData
108
     */
109
    private function createResourceXml(array $resourceData, string $resourceDir): void
110
    {
111
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
112
        $xmlContent .= '<activity id="'.$resourceData['id'].'" moduleid="'.$resourceData['moduleid'].'" modulename="resource" contextid="'.$resourceData['contextid'].'">'.PHP_EOL;
113
        $xmlContent .= '  <resource id="'.$resourceData['id'].'">'.PHP_EOL;
114
        $xmlContent .= '    <name>'.htmlspecialchars((string) $resourceData['name']).'</name>'.PHP_EOL;
115
        $xmlContent .= '    <intro>'.htmlspecialchars((string) $resourceData['intro']).'</intro>'.PHP_EOL;
116
        $xmlContent .= '    <introformat>1</introformat>'.PHP_EOL;
117
        $xmlContent .= '    <tobemigrated>0</tobemigrated>'.PHP_EOL;
118
        $xmlContent .= '    <legacyfiles>0</legacyfiles>'.PHP_EOL;
119
        $xmlContent .= '    <legacyfileslast>$@NULL@$</legacyfileslast>'.PHP_EOL;
120
        $xmlContent .= '    <display>0</display>'.PHP_EOL;
121
        $xmlContent .= '    <displayoptions>a:1:{s:10:"printintro";i:1;}</displayoptions>'.PHP_EOL;
122
        $xmlContent .= '    <filterfiles>0</filterfiles>'.PHP_EOL;
123
        $xmlContent .= '    <revision>1</revision>'.PHP_EOL;
124
        $xmlContent .= '    <timemodified>'.(int) $resourceData['timemodified'].'</timemodified>'.PHP_EOL;
125
        $xmlContent .= '  </resource>'.PHP_EOL;
126
        $xmlContent .= '</activity>'.PHP_EOL;
127
128
        $this->createXmlFile('resource', $xmlContent, $resourceDir);
129
    }
130
}
131