Passed
Push — 1.11.x ( 120e79...41af88 )
by Yannick
15:40 queued 06:52
created

ActivityExport   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
eloc 111
c 1
b 0
f 0
dl 0
loc 238
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareActivityDirectory() 0 7 2
A createGradesXml() 0 8 1
A createXmlFile() 0 5 2
A __construct() 0 3 1
A getSectionIdForActivity() 0 11 5
A createModuleXml() 0 26 1
A createCompetenciesXml() 0 10 1
A createCommentsXml() 0 11 1
A createCompletionXml() 0 13 1
A createGradeHistoryXml() 0 9 1
B createInforefXml() 0 31 7
A createCalendarXml() 0 11 1
A createFiltersXml() 0 11 1
A createRolesXml() 0 6 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace moodleexport;
6
7
use Exception;
8
9
/**
10
 * Class ActivityExport.
11
 *
12
 * Base class for exporting common activities.
13
 */
14
abstract class ActivityExport
15
{
16
    protected $course;
17
18
    public function __construct($course)
19
    {
20
        $this->course = $course;
21
    }
22
23
    /**
24
     * Abstract method for exporting the activity.
25
     * Must be implemented by child classes.
26
     */
27
    abstract public function export($activityId, $exportDir, $moduleId, $sectionId);
28
29
    /**
30
     * Prepares the directory for the activity.
31
     */
32
    protected function prepareActivityDirectory(string $exportDir, string $activityType, int $moduleId): string
33
    {
34
        $activityDir = "{$exportDir}/activities/{$activityType}_{$moduleId}";
35
        if (!is_dir($activityDir)) {
36
            mkdir($activityDir, 0777, true);
37
        }
38
        return $activityDir;
39
    }
40
41
    /**
42
     * Get the section ID for a given activity ID.
43
     */
44
    public function getSectionIdForActivity(int $activityId, string $itemType): int
45
    {
46
        foreach ($this->course->resources[RESOURCE_LEARNPATH] as $learnpath) {
47
            foreach ($learnpath->items as $item) {
48
                if ($item['item_type'] == $itemType && $item['path'] == $activityId) {
49
                    return $learnpath->source_id;
50
                }
51
            }
52
        }
53
54
        return 0;
55
    }
56
57
    /**
58
     * Creates a generic XML file.
59
     */
60
    protected function createXmlFile(string $fileName, string $xmlContent, string $directory): void
61
    {
62
        $filePath = $directory . '/' . $fileName . '.xml';
63
        if (file_put_contents($filePath, $xmlContent) === false) {
64
            throw new Exception("Error creating {$fileName}.xml");
65
        }
66
    }
67
68
    /**
69
     * Creates the module.xml file.
70
     */
71
    protected function createModuleXml(array $data, string $directory): void
72
    {
73
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
74
        $xmlContent .= '<module id="' . $data['moduleid'] . '" version="2021051700">' . PHP_EOL;
75
        $xmlContent .= '  <modulename>' .$data['modulename']. '</modulename>' . PHP_EOL;
76
        $xmlContent .= '  <sectionid>' . $data['sectionid'] . '</sectionid>' . PHP_EOL;
77
        $xmlContent .= '  <sectionnumber>' . $data['sectionnumber'] . '</sectionnumber>' . PHP_EOL;
78
        $xmlContent .= '  <idnumber></idnumber>' . PHP_EOL;
79
        $xmlContent .= '  <added>' . time() . '</added>' . PHP_EOL;
80
        $xmlContent .= '  <score>0</score>' . PHP_EOL;
81
        $xmlContent .= '  <indent>0</indent>' . PHP_EOL;
82
        $xmlContent .= '  <visible>1</visible>' . PHP_EOL;
83
        $xmlContent .= '  <visibleoncoursepage>1</visibleoncoursepage>' . PHP_EOL;
84
        $xmlContent .= '  <visibleold>1</visibleold>' . PHP_EOL;
85
        $xmlContent .= '  <groupmode>0</groupmode>' . PHP_EOL;
86
        $xmlContent .= '  <groupingid>0</groupingid>' . PHP_EOL;
87
        $xmlContent .= '  <completion>1</completion>' . PHP_EOL;
88
        $xmlContent .= '  <completiongradeitemnumber>$@NULL@$</completiongradeitemnumber>' . PHP_EOL;
89
        $xmlContent .= '  <completionview>0</completionview>' . PHP_EOL;
90
        $xmlContent .= '  <completionexpected>0</completionexpected>' . PHP_EOL;
91
        $xmlContent .= '  <availability>$@NULL@$</availability>' . PHP_EOL;
92
        $xmlContent .= '  <showdescription>0</showdescription>' . PHP_EOL;
93
        $xmlContent .= '  <tags></tags>' . PHP_EOL;
94
        $xmlContent .= '</module>' . PHP_EOL;
95
96
        $this->createXmlFile('module', $xmlContent, $directory);
97
    }
98
99
    /**
100
     * Creates the grades.xml file.
101
     */
102
    protected function createGradesXml(array $data, string $directory): void
103
    {
104
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
105
        $xmlContent .= '<activity_gradebook>' . PHP_EOL;
106
        $xmlContent .= '  <grade_items></grade_items>' . PHP_EOL;
107
        $xmlContent .= '</activity_gradebook>' . PHP_EOL;
108
109
        $this->createXmlFile('grades', $xmlContent, $directory);
110
    }
111
112
    /**
113
     * Creates the inforef.xml file, referencing users and files associated with the activity.
114
     */
115
    protected function createInforefXml(array $references, string $directory): void
116
    {
117
        // Start the XML content
118
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
119
        $xmlContent .= '<inforef>' . PHP_EOL;
120
121
        // Add user references if provided
122
        if (isset($references['users']) && is_array($references['users'])) {
123
            $xmlContent .= '  <userref>' . PHP_EOL;
124
            foreach ($references['users'] as $userId) {
125
                $xmlContent .= '    <user>' . PHP_EOL;
126
                $xmlContent .= '      <id>' . htmlspecialchars($userId) . '</id>' . PHP_EOL;
127
                $xmlContent .= '    </user>' . PHP_EOL;
128
            }
129
            $xmlContent .= '  </userref>' . PHP_EOL;
130
        }
131
132
        // Add file references if provided
133
        if (isset($references['files']) && is_array($references['files'])) {
134
            $xmlContent .= '  <fileref>' . PHP_EOL;
135
            foreach ($references['files'] as $file) {
136
                $xmlContent .= '    <file>' . PHP_EOL;
137
                $xmlContent .= '      <id>' . htmlspecialchars($file['id']) . '</id>' . PHP_EOL;
138
                $xmlContent .= '    </file>' . PHP_EOL;
139
            }
140
            $xmlContent .= '  </fileref>' . PHP_EOL;
141
        }
142
143
        $xmlContent .= '</inforef>' . PHP_EOL;
144
145
        $this->createXmlFile('inforef', $xmlContent, $directory);
146
    }
147
148
    /**
149
     * Creates the roles.xml file.
150
     */
151
    protected function createRolesXml(array $activityData, string $directory): void
152
    {
153
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
154
        $xmlContent .= '<roles></roles>' . PHP_EOL;
155
156
        $this->createXmlFile('roles', $xmlContent, $directory);
157
    }
158
159
    /**
160
     * Creates the filters.xml file for the activity.
161
     */
162
    protected function createFiltersXml(array $activityData, string $destinationDir): void
163
    {
164
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
165
        $xmlContent .= '<filters>' . PHP_EOL;
166
        $xmlContent .= '  <filter_actives>' . PHP_EOL;
167
        $xmlContent .= '  </filter_actives>' . PHP_EOL;
168
        $xmlContent .= '  <filter_configs>' . PHP_EOL;
169
        $xmlContent .= '  </filter_configs>' . PHP_EOL;
170
        $xmlContent .= '</filters>' . PHP_EOL;
171
172
        $this->createXmlFile('filters', $xmlContent, $destinationDir);
173
    }
174
175
    /**
176
     * Creates the grade_history.xml file for the activity.
177
     */
178
    protected function createGradeHistoryXml(array $activityData, string $destinationDir): void
179
    {
180
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
181
        $xmlContent .= '<grade_history>' . PHP_EOL;
182
        $xmlContent .= '  <grade_grades>' . PHP_EOL;
183
        $xmlContent .= '  </grade_grades>' . PHP_EOL;
184
        $xmlContent .= '</grade_history>' . PHP_EOL;
185
186
        $this->createXmlFile('grade_history', $xmlContent, $destinationDir);
187
    }
188
189
    /**
190
     * Creates the completion.xml file.
191
     */
192
    protected function createCompletionXml(array $activityData, string $destinationDir): void
193
    {
194
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
195
        $xmlContent .= '<completion>' . PHP_EOL;
196
        $xmlContent .= '  <completiondata>' . PHP_EOL;
197
        $xmlContent .= '    <completion>' . PHP_EOL;
198
        $xmlContent .= '      <timecompleted>0</timecompleted>' . PHP_EOL;
199
        $xmlContent .= '      <completionstate>1</completionstate>' . PHP_EOL;
200
        $xmlContent .= '    </completion>' . PHP_EOL;
201
        $xmlContent .= '  </completiondata>' . PHP_EOL;
202
        $xmlContent .= '</completion>' . PHP_EOL;
203
204
        $this->createXmlFile('completion', $xmlContent, $destinationDir);
205
    }
206
207
    /**
208
     * Creates the comments.xml file.
209
     */
210
    protected function createCommentsXml(array $activityData, string $destinationDir): void
211
    {
212
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
213
        $xmlContent .= '<comments>' . PHP_EOL;
214
        $xmlContent .= '  <comment>' . PHP_EOL;
215
        $xmlContent .= '    <content>This is a sample comment</content>' . PHP_EOL;
216
        $xmlContent .= '    <author>Professor</author>' . PHP_EOL;
217
        $xmlContent .= '  </comment>' . PHP_EOL;
218
        $xmlContent .= '</comments>' . PHP_EOL;
219
220
        $this->createXmlFile('comments', $xmlContent, $destinationDir);
221
    }
222
223
    /**
224
     * Creates the competencies.xml file.
225
     */
226
    protected function createCompetenciesXml(array $activityData, string $destinationDir): void
227
    {
228
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
229
        $xmlContent .= '<competencies>' . PHP_EOL;
230
        $xmlContent .= '  <competency>' . PHP_EOL;
231
        $xmlContent .= '    <name>Sample Competency</name>' . PHP_EOL;
232
        $xmlContent .= '  </competency>' . PHP_EOL;
233
        $xmlContent .= '</competencies>' . PHP_EOL;
234
235
        $this->createXmlFile('competencies', $xmlContent, $destinationDir);
236
    }
237
238
    /**
239
     * Creates the calendar.xml file.
240
     */
241
    protected function createCalendarXml(array $activityData, string $destinationDir): void
242
    {
243
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
244
        $xmlContent .= '<calendar>' . PHP_EOL;
245
        $xmlContent .= '  <event>' . PHP_EOL;
246
        $xmlContent .= '    <name>Due Date</name>' . PHP_EOL;
247
        $xmlContent .= '    <timestart>' . time() . '</timestart>' . PHP_EOL;
248
        $xmlContent .= '  </event>' . PHP_EOL;
249
        $xmlContent .= '</calendar>' . PHP_EOL;
250
251
        $this->createXmlFile('calendar', $xmlContent, $destinationDir);
252
    }
253
}
254