Passed
Pull Request — 1.11.x (#5783)
by Yannick
08:32
created

UrlExport::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 20
rs 9.7666
cc 1
nc 1
nop 2
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace moodleexport;
6
7
/**
8
 * Class UrlExport.
9
 *
10
 * Handles the export of URLs within a course.
11
 */
12
class UrlExport extends ActivityExport
13
{
14
    /**
15
     * Export all URL resources into a single Moodle activity.
16
     *
17
     * @param int $activityId The ID of the URL.
18
     * @param string $exportDir The directory where the URL 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 URL export will be saved
25
        $urlDir = $this->prepareActivityDirectory($exportDir, 'url', $moduleId);
26
27
        // Retrieve URL data
28
        $urlData = $this->getData($activityId, $sectionId);
29
30
        // Generate XML file for the URL
31
        $this->createUrlXml($urlData, $urlDir);
32
        $this->createModuleXml($urlData, $urlDir);
33
        $this->createGradesXml($urlData, $urlDir);
34
        $this->createGradeHistoryXml($urlData, $urlDir);
35
        $this->createInforefXml($urlData, $urlDir);
36
        $this->createRolesXml($urlData, $urlDir);
37
        $this->createCommentsXml($urlData, $urlDir);
38
        $this->createCalendarXml($urlData, $urlDir);
39
        $this->createFiltersXml($urlData, $urlDir);
40
    }
41
42
    /**
43
     * Create the XML file for the URL.
44
     */
45
    private function createUrlXml(array $urlData, string $urlDir): void
46
    {
47
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
48
        $xmlContent .= '<activity id="' . $urlData['id'] . '" moduleid="' . $urlData['moduleid'] . '" modulename="'.$urlData['modulename'].'" contextid="' . $urlData['contextid'] . '">' . PHP_EOL;
49
        $xmlContent .= '  <url id="' . $urlData['id'] . '">' . PHP_EOL;
50
        $xmlContent .= '    <name>' . htmlspecialchars($urlData['name']) . '</name>' . PHP_EOL;
51
        $xmlContent .= '    <intro><![CDATA[' . htmlspecialchars($urlData['description']) . ']]></intro>' . PHP_EOL;
52
        $xmlContent .= '    <introformat>1</introformat>' . PHP_EOL;
53
        $xmlContent .= '    <externalurl>' . htmlspecialchars($urlData['externalurl']) . '</externalurl>' . PHP_EOL;
54
        $xmlContent .= '    <display>0</display>' . PHP_EOL;
55
        $xmlContent .= '    <displayoptions>a:1:{s:10:"printintro";i:1;}</displayoptions>' . PHP_EOL;
56
        $xmlContent .= '    <parameters>a:0:{}</parameters>' . PHP_EOL;
57
        $xmlContent .= '    <timemodified>' . $urlData['timemodified'] . '</timemodified>' . PHP_EOL;
58
        $xmlContent .= '  </url>' . PHP_EOL;
59
        $xmlContent .= '</activity>';
60
61
        $this->createXmlFile('url', $xmlContent, $urlDir);
62
    }
63
64
    /**
65
     * Get all URL data for the course.
66
     */
67
    public function getData(int $activityId, int $sectionId): ?array
68
    {
69
        // Extract the URL information from the course data
70
        $url = $this->course->resources['link'][$activityId];
71
72
        // Return the URL data formatted for export
73
        return [
74
            'id' => $activityId,
75
            'moduleid' => $activityId,
76
            'modulename' => 'url',
77
            'contextid' => $this->course->info['real_id'],
78
            'name' => $url->title,
79
            'description' => $url->description,
80
            'externalurl' => $url->url,
81
            'timecreated' => time(),
82
            'timemodified' => time(),
83
            'sectionid' => $sectionId,
84
            'sectionnumber' => 0,
85
            'users' => [],
86
            'files' => [],
87
        ];
88
    }
89
}
90