|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
namespace moodleexport; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ForumExport. |
|
9
|
|
|
* |
|
10
|
|
|
* Handles the export of forums within a course. |
|
11
|
|
|
*/ |
|
12
|
|
|
class ForumExport extends ActivityExport |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Export all forum data into a single Moodle forum activity. |
|
16
|
|
|
* |
|
17
|
|
|
* @param int $activityId The ID of the forum. |
|
18
|
|
|
* @param string $exportDir The directory where the forum 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 forum export will be saved |
|
25
|
|
|
$forumDir = $this->prepareActivityDirectory($exportDir, 'forum', $moduleId); |
|
26
|
|
|
|
|
27
|
|
|
// Retrieve forum data |
|
28
|
|
|
$forumData = $this->getData($activityId, $sectionId); |
|
29
|
|
|
|
|
30
|
|
|
// Generate XML files for the forum |
|
31
|
|
|
$this->createForumXml($forumData, $forumDir); |
|
32
|
|
|
$this->createModuleXml($forumData, $forumDir); |
|
33
|
|
|
$this->createGradesXml($forumData, $forumDir); |
|
34
|
|
|
$this->createGradeHistoryXml($forumData, $forumDir); |
|
35
|
|
|
$this->createInforefXml($forumData, $forumDir); |
|
36
|
|
|
$this->createRolesXml($forumData, $forumDir); |
|
37
|
|
|
$this->createCalendarXml($forumData, $forumDir); |
|
38
|
|
|
$this->createCommentsXml($forumData, $forumDir); |
|
39
|
|
|
$this->createCompetenciesXml($forumData, $forumDir); |
|
40
|
|
|
$this->createFiltersXml($forumData, $forumDir); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get all forum data from the course. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getData(int $forumId, int $sectionId): ?array |
|
47
|
|
|
{ |
|
48
|
|
|
$forum = $this->course->resources[RESOURCE_FORUM][$forumId]->obj; |
|
49
|
|
|
|
|
50
|
|
|
$adminData = MoodleExport::getAdminUserData(); |
|
51
|
|
|
$adminId = $adminData['id']; |
|
52
|
|
|
|
|
53
|
|
|
$threads = []; |
|
54
|
|
|
foreach ($this->course->resources['thread'] as $threadId => $thread) { |
|
55
|
|
|
if ($thread->obj->forum_id == $forumId) { |
|
56
|
|
|
// Get the posts for each thread |
|
57
|
|
|
$posts = []; |
|
58
|
|
|
foreach ($this->course->resources['post'] as $postId => $post) { |
|
59
|
|
|
if ($post->obj->thread_id == $threadId) { |
|
60
|
|
|
$posts[] = [ |
|
61
|
|
|
'id' => $post->obj->post_id, |
|
62
|
|
|
'userid' => $adminId, |
|
63
|
|
|
'message' => $post->obj->post_text, |
|
64
|
|
|
'created' => strtotime($post->obj->post_date), |
|
65
|
|
|
'modified' => strtotime($post->obj->post_date), |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$threads[] = [ |
|
71
|
|
|
'id' => $thread->obj->thread_id, |
|
72
|
|
|
'title' => $thread->obj->thread_title, |
|
73
|
|
|
'userid' => $adminId, |
|
74
|
|
|
'timemodified' => strtotime($thread->obj->thread_date), |
|
75
|
|
|
'usermodified' => $adminId, |
|
76
|
|
|
'posts' => $posts, |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$name = $forum->forum_title ?? ''; |
|
82
|
|
|
if ($sectionId > 0) { |
|
83
|
|
|
$name = $this->lpItemTitle($sectionId, RESOURCE_FORUM, $forumId, $name); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return [ |
|
87
|
|
|
'id' => $forumId, |
|
88
|
|
|
'moduleid' => $forumId, |
|
89
|
|
|
'modulename' => 'forum', |
|
90
|
|
|
'contextid' => $this->course->info['real_id'], |
|
91
|
|
|
'name' => $name, |
|
92
|
|
|
'description' => $forum->forum_comment, |
|
93
|
|
|
'timecreated' => time(), |
|
94
|
|
|
'timemodified' => time(), |
|
95
|
|
|
'sectionid' => $sectionId, |
|
96
|
|
|
'sectionnumber' => 1, |
|
97
|
|
|
'userid' => $adminId, |
|
98
|
|
|
'threads' => $threads, |
|
99
|
|
|
'users' => [$adminId], |
|
100
|
|
|
'files' => [], |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Create the forum.xml file with all forum data. |
|
106
|
|
|
*/ |
|
107
|
|
|
private function createForumXml(array $forumData, string $forumDir): void |
|
108
|
|
|
{ |
|
109
|
|
|
$xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
|
110
|
|
|
$xmlContent .= '<activity id="'.$forumData['id'].'" moduleid="'.$forumData['moduleid'].'" modulename="'.$forumData['modulename'].'" contextid="'.$forumData['contextid'].'">'.PHP_EOL; |
|
111
|
|
|
$xmlContent .= ' <forum id="'.$forumData['id'].'">'.PHP_EOL; |
|
112
|
|
|
$xmlContent .= ' <type>general</type>'.PHP_EOL; |
|
113
|
|
|
$xmlContent .= ' <name>'.htmlspecialchars($forumData['name']).'</name>'.PHP_EOL; |
|
114
|
|
|
$xmlContent .= ' <intro>'.htmlspecialchars($forumData['description']).'</intro>'.PHP_EOL; |
|
115
|
|
|
$xmlContent .= ' <introformat>1</introformat>'.PHP_EOL; |
|
116
|
|
|
$xmlContent .= ' <duedate>0</duedate>'.PHP_EOL; |
|
117
|
|
|
$xmlContent .= ' <cutoffdate>0</cutoffdate>'.PHP_EOL; |
|
118
|
|
|
$xmlContent .= ' <assessed>0</assessed>'.PHP_EOL; |
|
119
|
|
|
$xmlContent .= ' <assesstimestart>0</assesstimestart>'.PHP_EOL; |
|
120
|
|
|
$xmlContent .= ' <assesstimefinish>0</assesstimefinish>'.PHP_EOL; |
|
121
|
|
|
$xmlContent .= ' <scale>100</scale>'.PHP_EOL; |
|
122
|
|
|
$xmlContent .= ' <maxbytes>512000</maxbytes>'.PHP_EOL; |
|
123
|
|
|
$xmlContent .= ' <maxattachments>9</maxattachments>'.PHP_EOL; |
|
124
|
|
|
$xmlContent .= ' <forcesubscribe>0</forcesubscribe>'.PHP_EOL; |
|
125
|
|
|
$xmlContent .= ' <trackingtype>1</trackingtype>'.PHP_EOL; |
|
126
|
|
|
$xmlContent .= ' <rsstype>0</rsstype>'.PHP_EOL; |
|
127
|
|
|
$xmlContent .= ' <rssarticles>0</rssarticles>'.PHP_EOL; |
|
128
|
|
|
$xmlContent .= ' <timemodified>'.$forumData['timemodified'].'</timemodified>'.PHP_EOL; |
|
129
|
|
|
$xmlContent .= ' <warnafter>0</warnafter>'.PHP_EOL; |
|
130
|
|
|
$xmlContent .= ' <blockafter>0</blockafter>'.PHP_EOL; |
|
131
|
|
|
$xmlContent .= ' <blockperiod>0</blockperiod>'.PHP_EOL; |
|
132
|
|
|
$xmlContent .= ' <completiondiscussions>0</completiondiscussions>'.PHP_EOL; |
|
133
|
|
|
$xmlContent .= ' <completionreplies>0</completionreplies>'.PHP_EOL; |
|
134
|
|
|
$xmlContent .= ' <completionposts>0</completionposts>'.PHP_EOL; |
|
135
|
|
|
$xmlContent .= ' <displaywordcount>0</displaywordcount>'.PHP_EOL; |
|
136
|
|
|
$xmlContent .= ' <lockdiscussionafter>0</lockdiscussionafter>'.PHP_EOL; |
|
137
|
|
|
$xmlContent .= ' <grade_forum>0</grade_forum>'.PHP_EOL; |
|
138
|
|
|
|
|
139
|
|
|
// Add forum threads |
|
140
|
|
|
$xmlContent .= ' <discussions>'.PHP_EOL; |
|
141
|
|
|
foreach ($forumData['threads'] as $thread) { |
|
142
|
|
|
$xmlContent .= ' <discussion id="'.$thread['id'].'">'.PHP_EOL; |
|
143
|
|
|
$xmlContent .= ' <name>'.htmlspecialchars($thread['title']).'</name>'.PHP_EOL; |
|
144
|
|
|
$xmlContent .= ' <firstpost>'.$thread['firstpost'].'</firstpost>'.PHP_EOL; |
|
145
|
|
|
$xmlContent .= ' <userid>'.$thread['userid'].'</userid>'.PHP_EOL; |
|
146
|
|
|
$xmlContent .= ' <groupid>-1</groupid>'.PHP_EOL; |
|
147
|
|
|
$xmlContent .= ' <assessed>0</assessed>'.PHP_EOL; |
|
148
|
|
|
$xmlContent .= ' <timemodified>'.$thread['timemodified'].'</timemodified>'.PHP_EOL; |
|
149
|
|
|
$xmlContent .= ' <usermodified>'.$thread['usermodified'].'</usermodified>'.PHP_EOL; |
|
150
|
|
|
$xmlContent .= ' <timestart>0</timestart>'.PHP_EOL; |
|
151
|
|
|
$xmlContent .= ' <timeend>0</timeend>'.PHP_EOL; |
|
152
|
|
|
$xmlContent .= ' <pinned>0</pinned>'.PHP_EOL; |
|
153
|
|
|
$xmlContent .= ' <timelocked>0</timelocked>'.PHP_EOL; |
|
154
|
|
|
|
|
155
|
|
|
// Add forum posts to the thread |
|
156
|
|
|
$xmlContent .= ' <posts>'.PHP_EOL; |
|
157
|
|
|
foreach ($thread['posts'] as $post) { |
|
158
|
|
|
$xmlContent .= ' <post id="'.$post['id'].'">'.PHP_EOL; |
|
159
|
|
|
$xmlContent .= ' <parent>'.$post['parent'].'</parent>'.PHP_EOL; |
|
160
|
|
|
$xmlContent .= ' <userid>'.$post['userid'].'</userid>'.PHP_EOL; |
|
161
|
|
|
$xmlContent .= ' <created>'.$post['created'].'</created>'.PHP_EOL; |
|
162
|
|
|
$xmlContent .= ' <modified>'.$post['modified'].'</modified>'.PHP_EOL; |
|
163
|
|
|
$xmlContent .= ' <mailed>'.$post['mailed'].'</mailed>'.PHP_EOL; |
|
164
|
|
|
$xmlContent .= ' <subject>'.htmlspecialchars($post['subject']).'</subject>'.PHP_EOL; |
|
165
|
|
|
$xmlContent .= ' <message>'.htmlspecialchars($post['message']).'</message>'.PHP_EOL; |
|
166
|
|
|
$xmlContent .= ' <messageformat>1</messageformat>'.PHP_EOL; |
|
167
|
|
|
$xmlContent .= ' <messagetrust>0</messagetrust>'.PHP_EOL; |
|
168
|
|
|
$xmlContent .= ' <attachment></attachment>'.PHP_EOL; |
|
169
|
|
|
$xmlContent .= ' <totalscore>0</totalscore>'.PHP_EOL; |
|
170
|
|
|
$xmlContent .= ' <mailnow>0</mailnow>'.PHP_EOL; |
|
171
|
|
|
$xmlContent .= ' <privatereplyto>0</privatereplyto>'.PHP_EOL; |
|
172
|
|
|
$xmlContent .= ' <ratings>'.PHP_EOL; |
|
173
|
|
|
$xmlContent .= ' </ratings>'.PHP_EOL; |
|
174
|
|
|
$xmlContent .= ' </post>'.PHP_EOL; |
|
175
|
|
|
} |
|
176
|
|
|
$xmlContent .= ' </posts>'.PHP_EOL; |
|
177
|
|
|
$xmlContent .= ' <discussion_subs>'.PHP_EOL; |
|
178
|
|
|
$xmlContent .= ' <discussion_sub id="'.$thread['id'].'">'.PHP_EOL; |
|
179
|
|
|
$xmlContent .= ' <userid>'.$thread['userid'].'</userid>'.PHP_EOL; |
|
180
|
|
|
$xmlContent .= ' <preference>'.$thread['timemodified'].'</preference>'.PHP_EOL; |
|
181
|
|
|
$xmlContent .= ' </discussion_sub>'.PHP_EOL; |
|
182
|
|
|
$xmlContent .= ' </discussion_subs>'.PHP_EOL; |
|
183
|
|
|
$xmlContent .= ' </discussion>'.PHP_EOL; |
|
184
|
|
|
} |
|
185
|
|
|
$xmlContent .= ' </discussions>'.PHP_EOL; |
|
186
|
|
|
$xmlContent .= ' </forum>'.PHP_EOL; |
|
187
|
|
|
$xmlContent .= '</activity>'; |
|
188
|
|
|
|
|
189
|
|
|
$this->createXmlFile('forum', $xmlContent, $forumDir); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|