Passed
Pull Request — 1.11.x (#6923)
by
unknown
10:03
created

ActivityExport::lpItemTitle()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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