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

GlossaryExport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A export() 0 19 1
A createGlossaryXml() 0 60 2
A getData() 0 33 2
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace moodleexport;
6
7
/**
8
 * Class GlossaryExport.
9
 *
10
 * Handles the export of glossaries within a course.
11
 */
12
class GlossaryExport extends ActivityExport
13
{
14
    /**
15
     * Export all glossary terms into a single Moodle glossary.
16
     *
17
     * @param int $activityId The ID of the glossary.
18
     * @param string $exportDir The directory where the glossary 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 glossary export will be saved
25
        $glossaryDir = $this->prepareActivityDirectory($exportDir, 'glossary', $moduleId);
26
27
        // Retrieve glossary data
28
        $glossaryData = $this->getData($activityId, $sectionId);
29
30
        // Generate XML files for the glossary
31
        $this->createGlossaryXml($glossaryData, $glossaryDir);
32
        $this->createModuleXml($glossaryData, $glossaryDir);
33
        $this->createGradesXml($glossaryData, $glossaryDir);
34
        $this->createGradeHistoryXml($glossaryData, $glossaryDir);
35
        $this->createInforefXml($glossaryData, $glossaryDir);
36
        $this->createRolesXml($glossaryData, $glossaryDir);
37
        $this->createCalendarXml($glossaryData, $glossaryDir);
38
        $this->createCommentsXml($glossaryData, $glossaryDir);
39
        $this->createCompetenciesXml($glossaryData, $glossaryDir);
40
        $this->createFiltersXml($glossaryData, $glossaryDir);
41
    }
42
43
    /**
44
     * Create the XML file for the glossary with all terms combined.
45
     */
46
    private function createGlossaryXml(array $glossaryData, string $glossaryDir): void
47
    {
48
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
49
        $xmlContent .= '<activity id="' . $glossaryData['id'] . '" moduleid="' . $glossaryData['moduleid'] . '" modulename="'.$glossaryData['modulename'].'" contextid="' . $glossaryData['contextid'] . '">' . PHP_EOL;
50
        $xmlContent .= '  <glossary id="' . $glossaryData['id'] . '">' . PHP_EOL;
51
        $xmlContent .= '    <name>' . htmlspecialchars($glossaryData['name']) . '</name>' . PHP_EOL;
52
        $xmlContent .= '    <intro></intro>' . PHP_EOL;
53
        $xmlContent .= '    <introformat>1</introformat>' . PHP_EOL;
54
        $xmlContent .= '    <allowduplicatedentries>0</allowduplicatedentries>' . PHP_EOL;
55
        $xmlContent .= '    <displayformat>dictionary</displayformat>' . PHP_EOL;
56
        $xmlContent .= '    <mainglossary>0</mainglossary>' . PHP_EOL;
57
        $xmlContent .= '    <showspecial>1</showspecial>' . PHP_EOL;
58
        $xmlContent .= '    <showalphabet>1</showalphabet>' . PHP_EOL;
59
        $xmlContent .= '    <showall>1</showall>' . PHP_EOL;
60
        $xmlContent .= '    <allowcomments>0</allowcomments>' . PHP_EOL;
61
        $xmlContent .= '    <allowprintview>1</allowprintview>' . PHP_EOL;
62
        $xmlContent .= '    <usedynalink>1</usedynalink>' . PHP_EOL;
63
        $xmlContent .= '    <defaultapproval>1</defaultapproval>' . PHP_EOL;
64
        $xmlContent .= '    <globalglossary>0</globalglossary>' . PHP_EOL;
65
        $xmlContent .= '    <entbypage>10</entbypage>' . PHP_EOL;
66
        $xmlContent .= '    <editalways>0</editalways>' . PHP_EOL;
67
        $xmlContent .= '    <rsstype>0</rsstype>' . PHP_EOL;
68
        $xmlContent .= '    <rssarticles>0</rssarticles>' . PHP_EOL;
69
        $xmlContent .= '    <assessed>0</assessed>' . PHP_EOL;
70
        $xmlContent .= '    <assesstimestart>0</assesstimestart>' . PHP_EOL;
71
        $xmlContent .= '    <assesstimefinish>0</assesstimefinish>' . PHP_EOL;
72
        $xmlContent .= '    <scale>100</scale>' . PHP_EOL;
73
        $xmlContent .= '    <timecreated>' . $glossaryData['timecreated'] . '</timecreated>' . PHP_EOL;
74
        $xmlContent .= '    <timemodified>' . $glossaryData['timemodified'] . '</timemodified>' . PHP_EOL;
75
        $xmlContent .= '    <completionentries>0</completionentries>' . PHP_EOL;
76
        $xmlContent .= '    <entries>' . PHP_EOL;
77
78
        // Add glossary terms (entries)
79
        foreach ($glossaryData['entries'] as $entry) {
80
            $xmlContent .= '      <entry id="' . $entry['id'] . '">' . PHP_EOL;
81
            $xmlContent .= '        <userid>' . $entry['userid'] . '</userid>' . PHP_EOL;
82
            $xmlContent .= '        <concept>' . htmlspecialchars($entry['concept']) . '</concept>' . PHP_EOL;
83
            $xmlContent .= '        <definition><![CDATA[' . $entry['definition'] . ']]></definition>' . PHP_EOL;
84
            $xmlContent .= '        <definitionformat>1</definitionformat>' . PHP_EOL;
85
            $xmlContent .= '        <definitiontrust>0</definitiontrust>' . PHP_EOL;
86
            $xmlContent .= '        <attachment></attachment>' . PHP_EOL;
87
            $xmlContent .= '        <timecreated>' . $entry['timecreated'] . '</timecreated>' . PHP_EOL;
88
            $xmlContent .= '        <timemodified>' . $entry['timemodified'] . '</timemodified>' . PHP_EOL;
89
            $xmlContent .= '        <teacherentry>1</teacherentry>' . PHP_EOL;
90
            $xmlContent .= '        <sourceglossaryid>0</sourceglossaryid>' . PHP_EOL;
91
            $xmlContent .= '        <usedynalink>0</usedynalink>' . PHP_EOL;
92
            $xmlContent .= '        <casesensitive>0</casesensitive>' . PHP_EOL;
93
            $xmlContent .= '        <fullmatch>0</fullmatch>' . PHP_EOL;
94
            $xmlContent .= '        <approved>1</approved>' . PHP_EOL;
95
            $xmlContent .= '        <ratings>' . PHP_EOL;
96
            $xmlContent .= '        </ratings>' . PHP_EOL;
97
            $xmlContent .= '      </entry>' . PHP_EOL;
98
        }
99
        $xmlContent .= '    </entries>' . PHP_EOL;
100
        $xmlContent .= '    <entriestags></entriestags>' . PHP_EOL;
101
        $xmlContent .= '    <categories></categories>' . PHP_EOL;
102
        $xmlContent .= '  </glossary>' . PHP_EOL;
103
        $xmlContent .= '</activity>';
104
105
        $this->createXmlFile('glossary', $xmlContent, $glossaryDir);
106
    }
107
108
    /**
109
     * Get all terms from the course and group them into a single glossary.
110
     */
111
    public function getData(int $glossaryId, int $sectionId): ?array
112
    {
113
        $adminData = MoodleExport::getAdminUserData();
114
        $adminId = $adminData['id'];
115
116
        $glossaryEntries = [];
117
        foreach ($this->course->resources['glossary'] as $glossary) {
118
            $glossaryEntries[] = [
119
                'id' => $glossary->glossary_id,
120
                'userid' => $adminId,
121
                'concept' => $glossary->name,
122
                'definition' => $glossary->description,
123
                'timecreated' => time(),
124
                'timemodified' => time(),
125
            ];
126
        }
127
128
        // Return the glossary data with all terms included
129
        return [
130
            'id' => $glossaryId,
131
            'moduleid' => $glossaryId,
132
            'modulename' => 'glossary',
133
            'contextid' => $this->course->info['real_id'],
134
            'name' => get_lang('Glossary'),
135
            'description' => '',
136
            'timecreated' => time(),
137
            'timemodified' => time(),
138
            'sectionid' => $sectionId,
139
            'sectionnumber' => 0,
140
            'userid' => $adminId,
141
            'entries' => $glossaryEntries,
142
            'users' => [$adminId],
143
            'files' => [],
144
        ];
145
    }
146
}
147