|
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 Chamilo\CourseBundle\Component\CourseCopy\Moodle\Builder\MoodleExport; |
|
10
|
|
|
|
|
11
|
|
|
use const PHP_EOL; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Handles the export of glossaries within a course. |
|
15
|
|
|
*/ |
|
16
|
|
|
class GlossaryExport extends ActivityExport |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Export all glossary terms into a single Moodle glossary. |
|
20
|
|
|
* |
|
21
|
|
|
* @param int $activityId the ID of the glossary (logical wrapper id) |
|
22
|
|
|
* @param string $exportDir destination base directory for the export |
|
23
|
|
|
* @param int $moduleId module id used to name the activity folder |
|
24
|
|
|
* @param int $sectionId moodle section id where the activity will live |
|
25
|
|
|
*/ |
|
26
|
|
|
public function export($activityId, $exportDir, $moduleId, $sectionId): void |
|
27
|
|
|
{ |
|
28
|
|
|
// Prepare destination directory for the activity |
|
29
|
|
|
$glossaryDir = $this->prepareActivityDirectory($exportDir, 'glossary', (int) $moduleId); |
|
30
|
|
|
|
|
31
|
|
|
// Collect data |
|
32
|
|
|
$glossaryData = $this->getData((int) $activityId, (int) $sectionId); |
|
33
|
|
|
|
|
34
|
|
|
// Generate XML files for the glossary |
|
35
|
|
|
$this->createGlossaryXml($glossaryData, $glossaryDir); |
|
36
|
|
|
$this->createModuleXml($glossaryData, $glossaryDir); |
|
37
|
|
|
$this->createGradesXml($glossaryData, $glossaryDir); |
|
38
|
|
|
$this->createGradeHistoryXml($glossaryData, $glossaryDir); |
|
39
|
|
|
$this->createInforefXml($glossaryData, $glossaryDir); // relies on 'users' and 'files' keys |
|
40
|
|
|
$this->createRolesXml($glossaryData, $glossaryDir); |
|
41
|
|
|
$this->createCalendarXml($glossaryData, $glossaryDir); |
|
42
|
|
|
$this->createCommentsXml($glossaryData, $glossaryDir); |
|
43
|
|
|
$this->createCompetenciesXml($glossaryData, $glossaryDir); |
|
44
|
|
|
$this->createFiltersXml($glossaryData, $glossaryDir); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Gather all terms from the course and group them under a single glossary activity. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getData(int $glossaryId, int $sectionId): array |
|
51
|
|
|
{ |
|
52
|
|
|
$adminData = MoodleExport::getAdminUserData(); |
|
53
|
|
|
$adminId = (int) ($adminData['id'] ?? 0); |
|
54
|
|
|
|
|
55
|
|
|
$entries = []; |
|
56
|
|
|
if (!empty($this->course->resources['glossary'])) { |
|
57
|
|
|
foreach ($this->course->resources['glossary'] as $g) { |
|
58
|
|
|
$entries[] = [ |
|
59
|
|
|
'id' => (int) ($g->glossary_id ?? 0), |
|
60
|
|
|
'userid' => $adminId, |
|
61
|
|
|
'concept' => (string) ($g->name ?? ''), |
|
62
|
|
|
'definition' => (string) ($g->description ?? ''), |
|
63
|
|
|
'timecreated' => time(), |
|
64
|
|
|
'timemodified' => time(), |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return [ |
|
70
|
|
|
'id' => $glossaryId, |
|
71
|
|
|
'moduleid' => $glossaryId, |
|
72
|
|
|
'modulename' => 'glossary', |
|
73
|
|
|
'contextid' => (int) ($this->course->info['real_id'] ?? 0), |
|
74
|
|
|
'name' => get_lang('Glossary'), |
|
75
|
|
|
'description' => '', |
|
76
|
|
|
'timecreated' => time(), |
|
77
|
|
|
'timemodified' => time(), |
|
78
|
|
|
'sectionid' => $sectionId, |
|
79
|
|
|
'sectionnumber' => 0, |
|
80
|
|
|
'userid' => $adminId, |
|
81
|
|
|
'entries' => $entries, |
|
82
|
|
|
'users' => [$adminId], |
|
83
|
|
|
'files' => [], // no file refs for glossary entries (plain text) |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Create glossary.xml with all entries combined. |
|
89
|
|
|
*/ |
|
90
|
|
|
private function createGlossaryXml(array $glossaryData, string $glossaryDir): void |
|
91
|
|
|
{ |
|
92
|
|
|
$xml = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
|
93
|
|
|
$xml .= '<activity id="'.$glossaryData['id'].'" moduleid="'.$glossaryData['moduleid'].'" modulename="'.$glossaryData['modulename'].'" contextid="'.$glossaryData['contextid'].'">'.PHP_EOL; |
|
94
|
|
|
$xml .= ' <glossary id="'.$glossaryData['id'].'">'.PHP_EOL; |
|
95
|
|
|
$xml .= ' <name>'.htmlspecialchars((string) $glossaryData['name']).'</name>'.PHP_EOL; |
|
96
|
|
|
$xml .= ' <intro></intro>'.PHP_EOL; |
|
97
|
|
|
$xml .= ' <introformat>1</introformat>'.PHP_EOL; |
|
98
|
|
|
$xml .= ' <allowduplicatedentries>0</allowduplicatedentries>'.PHP_EOL; |
|
99
|
|
|
$xml .= ' <displayformat>dictionary</displayformat>'.PHP_EOL; |
|
100
|
|
|
$xml .= ' <mainglossary>0</mainglossary>'.PHP_EOL; |
|
101
|
|
|
$xml .= ' <showspecial>1</showspecial>'.PHP_EOL; |
|
102
|
|
|
$xml .= ' <showalphabet>1</showalphabet>'.PHP_EOL; |
|
103
|
|
|
$xml .= ' <showall>1</showall>'.PHP_EOL; |
|
104
|
|
|
$xml .= ' <allowcomments>0</allowcomments>'.PHP_EOL; |
|
105
|
|
|
$xml .= ' <allowprintview>1</allowprintview>'.PHP_EOL; |
|
106
|
|
|
$xml .= ' <usedynalink>1</usedynalink>'.PHP_EOL; |
|
107
|
|
|
$xml .= ' <defaultapproval>1</defaultapproval>'.PHP_EOL; |
|
108
|
|
|
$xml .= ' <globalglossary>0</globalglossary>'.PHP_EOL; |
|
109
|
|
|
$xml .= ' <entbypage>10</entbypage>'.PHP_EOL; |
|
110
|
|
|
$xml .= ' <editalways>0</editalways>'.PHP_EOL; |
|
111
|
|
|
$xml .= ' <rsstype>0</rsstype>'.PHP_EOL; |
|
112
|
|
|
$xml .= ' <rssarticles>0</rssarticles>'.PHP_EOL; |
|
113
|
|
|
$xml .= ' <assessed>0</assessed>'.PHP_EOL; |
|
114
|
|
|
$xml .= ' <assesstimestart>0</assesstimestart>'.PHP_EOL; |
|
115
|
|
|
$xml .= ' <assesstimefinish>0</assesstimefinish>'.PHP_EOL; |
|
116
|
|
|
$xml .= ' <scale>100</scale>'.PHP_EOL; |
|
117
|
|
|
$xml .= ' <timecreated>'.$glossaryData['timecreated'].'</timecreated>'.PHP_EOL; |
|
118
|
|
|
$xml .= ' <timemodified>'.$glossaryData['timemodified'].'</timemodified>'.PHP_EOL; |
|
119
|
|
|
$xml .= ' <completionentries>0</completionentries>'.PHP_EOL; |
|
120
|
|
|
|
|
121
|
|
|
// Entries |
|
122
|
|
|
$xml .= ' <entries>'.PHP_EOL; |
|
123
|
|
|
foreach ($glossaryData['entries'] as $entry) { |
|
124
|
|
|
$xml .= ' <entry id="'.$entry['id'].'">'.PHP_EOL; |
|
125
|
|
|
$xml .= ' <userid>'.$entry['userid'].'</userid>'.PHP_EOL; |
|
126
|
|
|
$xml .= ' <concept>'.htmlspecialchars((string) $entry['concept']).'</concept>'.PHP_EOL; |
|
127
|
|
|
$xml .= ' <definition><![CDATA['.$entry['definition'].']]></definition>'.PHP_EOL; |
|
128
|
|
|
$xml .= ' <definitionformat>1</definitionformat>'.PHP_EOL; |
|
129
|
|
|
$xml .= ' <definitiontrust>0</definitiontrust>'.PHP_EOL; |
|
130
|
|
|
$xml .= ' <attachment></attachment>'.PHP_EOL; |
|
131
|
|
|
$xml .= ' <timecreated>'.$entry['timecreated'].'</timecreated>'.PHP_EOL; |
|
132
|
|
|
$xml .= ' <timemodified>'.$entry['timemodified'].'</timemodified>'.PHP_EOL; |
|
133
|
|
|
$xml .= ' <teacherentry>1</teacherentry>'.PHP_EOL; |
|
134
|
|
|
$xml .= ' <sourceglossaryid>0</sourceglossaryid>'.PHP_EOL; |
|
135
|
|
|
$xml .= ' <usedynalink>0</usedynalink>'.PHP_EOL; |
|
136
|
|
|
$xml .= ' <casesensitive>0</casesensitive>'.PHP_EOL; |
|
137
|
|
|
$xml .= ' <fullmatch>0</fullmatch>'.PHP_EOL; |
|
138
|
|
|
$xml .= ' <approved>1</approved>'.PHP_EOL; |
|
139
|
|
|
$xml .= ' <ratings>'.PHP_EOL; |
|
140
|
|
|
$xml .= ' </ratings>'.PHP_EOL; |
|
141
|
|
|
$xml .= ' </entry>'.PHP_EOL; |
|
142
|
|
|
} |
|
143
|
|
|
$xml .= ' </entries>'.PHP_EOL; |
|
144
|
|
|
|
|
145
|
|
|
$xml .= ' <entriestags></entriestags>'.PHP_EOL; |
|
146
|
|
|
$xml .= ' <categories></categories>'.PHP_EOL; |
|
147
|
|
|
$xml .= ' </glossary>'.PHP_EOL; |
|
148
|
|
|
$xml .= '</activity>'; |
|
149
|
|
|
|
|
150
|
|
|
$this->createXmlFile('glossary', $xml, $glossaryDir); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|