Passed
Push — 1.11.x ( 120e79...41af88 )
by Yannick
15:40 queued 06:52
created

ForumExport::createForumXml()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 83
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 74
c 1
b 0
f 0
dl 0
loc 83
rs 8.5672
cc 3
nc 3
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * Create the forum.xml file with all forum data.
45
     */
46
    private function createForumXml(array $forumData, string $forumDir): void
47
    {
48
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
49
        $xmlContent .= '<activity id="' . $forumData['id'] . '" moduleid="' . $forumData['moduleid'] . '" modulename="'.$forumData['modulename'].'" contextid="' . $forumData['contextid'] . '">' . PHP_EOL;
50
        $xmlContent .= '  <forum id="' . $forumData['id'] . '">' . PHP_EOL;
51
        $xmlContent .= '    <type>general</type>' . PHP_EOL;
52
        $xmlContent .= '    <name>' . htmlspecialchars($forumData['name']) . '</name>' . PHP_EOL;
53
        $xmlContent .= '    <intro>' . htmlspecialchars($forumData['description']) . '</intro>' . PHP_EOL;
54
        $xmlContent .= '    <introformat>1</introformat>' . PHP_EOL;
55
        $xmlContent .= '    <duedate>0</duedate>' . PHP_EOL;
56
        $xmlContent .= '    <cutoffdate>0</cutoffdate>' . PHP_EOL;
57
        $xmlContent .= '    <assessed>0</assessed>' . PHP_EOL;
58
        $xmlContent .= '    <assesstimestart>0</assesstimestart>' . PHP_EOL;
59
        $xmlContent .= '    <assesstimefinish>0</assesstimefinish>' . PHP_EOL;
60
        $xmlContent .= '    <scale>100</scale>' . PHP_EOL;
61
        $xmlContent .= '    <maxbytes>512000</maxbytes>' . PHP_EOL;
62
        $xmlContent .= '    <maxattachments>9</maxattachments>' . PHP_EOL;
63
        $xmlContent .= '    <forcesubscribe>0</forcesubscribe>' . PHP_EOL;
64
        $xmlContent .= '    <trackingtype>1</trackingtype>' . PHP_EOL;
65
        $xmlContent .= '    <rsstype>0</rsstype>' . PHP_EOL;
66
        $xmlContent .= '    <rssarticles>0</rssarticles>' . PHP_EOL;
67
        $xmlContent .= '    <timemodified>' . $forumData['timemodified'] . '</timemodified>' . PHP_EOL;
68
        $xmlContent .= '    <warnafter>0</warnafter>' . PHP_EOL;
69
        $xmlContent .= '    <blockafter>0</blockafter>' . PHP_EOL;
70
        $xmlContent .= '    <blockperiod>0</blockperiod>' . PHP_EOL;
71
        $xmlContent .= '    <completiondiscussions>0</completiondiscussions>' . PHP_EOL;
72
        $xmlContent .= '    <completionreplies>0</completionreplies>' . PHP_EOL;
73
        $xmlContent .= '    <completionposts>0</completionposts>' . PHP_EOL;
74
        $xmlContent .= '    <displaywordcount>0</displaywordcount>' . PHP_EOL;
75
        $xmlContent .= '    <lockdiscussionafter>0</lockdiscussionafter>' . PHP_EOL;
76
        $xmlContent .= '    <grade_forum>0</grade_forum>' . PHP_EOL;
77
78
        // Add forum threads
79
        $xmlContent .= '    <discussions>' . PHP_EOL;
80
        foreach ($forumData['threads'] as $thread) {
81
            $xmlContent .= '      <discussion id="' . $thread['id'] . '">' . PHP_EOL;
82
            $xmlContent .= '        <name>' . htmlspecialchars($thread['title']) . '</name>' . PHP_EOL;
83
            $xmlContent .= '        <firstpost>' . $thread['firstpost'] . '</firstpost>' . PHP_EOL;
84
            $xmlContent .= '        <userid>' . $thread['userid'] . '</userid>' . PHP_EOL;
85
            $xmlContent .= '        <groupid>-1</groupid>' . PHP_EOL;
86
            $xmlContent .= '        <assessed>0</assessed>' . PHP_EOL;
87
            $xmlContent .= '        <timemodified>' . $thread['timemodified'] . '</timemodified>' . PHP_EOL;
88
            $xmlContent .= '        <usermodified>' . $thread['usermodified'] . '</usermodified>' . PHP_EOL;
89
            $xmlContent .= '        <timestart>0</timestart>' . PHP_EOL;
90
            $xmlContent .= '        <timeend>0</timeend>' . PHP_EOL;
91
            $xmlContent .= '        <pinned>0</pinned>' . PHP_EOL;
92
            $xmlContent .= '        <timelocked>0</timelocked>' . PHP_EOL;
93
94
            // Add forum posts to the thread
95
            $xmlContent .= '        <posts>' . PHP_EOL;
96
            foreach ($thread['posts'] as $post) {
97
                $xmlContent .= '          <post id="' . $post['id'] . '">' . PHP_EOL;
98
                $xmlContent .= '            <parent>' . $post['parent'] . '</parent>' . PHP_EOL;
99
                $xmlContent .= '            <userid>' . $post['userid'] . '</userid>' . PHP_EOL;
100
                $xmlContent .= '            <created>' . $post['created'] . '</created>' . PHP_EOL;
101
                $xmlContent .= '            <modified>' . $post['modified'] . '</modified>' . PHP_EOL;
102
                $xmlContent .= '            <mailed>' . $post['mailed'] . '</mailed>' . PHP_EOL;
103
                $xmlContent .= '            <subject>' . htmlspecialchars($post['subject']) . '</subject>' . PHP_EOL;
104
                $xmlContent .= '            <message>' . htmlspecialchars($post['message']) . '</message>' . PHP_EOL;
105
                $xmlContent .= '            <messageformat>1</messageformat>' . PHP_EOL;
106
                $xmlContent .= '            <messagetrust>0</messagetrust>' . PHP_EOL;
107
                $xmlContent .= '            <attachment></attachment>' . PHP_EOL;
108
                $xmlContent .= '            <totalscore>0</totalscore>' . PHP_EOL;
109
                $xmlContent .= '            <mailnow>0</mailnow>' . PHP_EOL;
110
                $xmlContent .= '            <privatereplyto>0</privatereplyto>' . PHP_EOL;
111
                $xmlContent .= '            <ratings>' . PHP_EOL;
112
                $xmlContent .= '            </ratings>' . PHP_EOL;
113
                $xmlContent .= '          </post>' . PHP_EOL;
114
            }
115
            $xmlContent .= '        </posts>' . PHP_EOL;
116
            $xmlContent .= '        <discussion_subs>' . PHP_EOL;
117
            $xmlContent .= '            <discussion_sub id="'.$thread['id'].'">' . PHP_EOL;
118
            $xmlContent .= '              <userid>' . $thread['userid'] . '</userid>' . PHP_EOL;
119
            $xmlContent .= '              <preference>' . $thread['timemodified'] . '</preference>' . PHP_EOL;
120
            $xmlContent .= '            </discussion_sub>' . PHP_EOL;
121
            $xmlContent .= '        </discussion_subs>' . PHP_EOL;
122
            $xmlContent .= '      </discussion>' . PHP_EOL;
123
        }
124
        $xmlContent .= '    </discussions>' . PHP_EOL;
125
        $xmlContent .= '  </forum>' . PHP_EOL;
126
        $xmlContent .= '</activity>';
127
128
        $this->createXmlFile('forum', $xmlContent, $forumDir);
129
    }
130
131
    /**
132
     * Get all forum data from the course.
133
     */
134
    public function getData(int $forumId, int $sectionId): ?array
135
    {
136
        $forum = $this->course->resources['forum'][$forumId]->obj;
137
138
        $adminData = MoodleExport::getAdminUserData();
139
        $adminId = $adminData['id'];
140
141
        $threads = [];
142
        foreach ($this->course->resources['thread'] as $threadId => $thread) {
143
            if ($thread->obj->forum_id == $forumId) {
144
                // Get the posts for each thread
145
                $posts = [];
146
                foreach ($this->course->resources['post'] as $postId => $post) {
147
                    if ($post->obj->thread_id == $threadId) {
148
                        $posts[] = [
149
                            'id' => $post->obj->post_id,
150
                            'userid' => $adminId,
151
                            'message' => $post->obj->post_text,
152
                            'created' => strtotime($post->obj->post_date),
153
                            'modified' => strtotime($post->obj->post_date),
154
                        ];
155
                    }
156
                }
157
158
                $threads[] = [
159
                    'id' => $thread->obj->thread_id,
160
                    'title' => $thread->obj->thread_title,
161
                    'userid' => $adminId,
162
                    'timemodified' => strtotime($thread->obj->thread_date),
163
                    'usermodified' => $adminId,
164
                    'posts' => $posts,
165
                ];
166
            }
167
        }
168
169
        $fileIds = [];
170
171
        return [
172
            'id' => $forumId,
173
            'moduleid' => $forumId,
174
            'modulename' => 'forum',
175
            'contextid' => $this->course->info['real_id'],
176
            'name' => $forum->forum_title,
177
            'description' => $forum->forum_comment,
178
            'timecreated' => time(),
179
            'timemodified' => time(),
180
            'sectionid' => $sectionId,
181
            'sectionnumber' => 1,
182
            'userid' => $adminId,
183
            'threads' => $threads,
184
            'users' => [$adminId],
185
            'files' => $fileIds,
186
        ];
187
    }
188
}
189