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

PageExport   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 50
c 1
b 0
f 0
dl 0
loc 96
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createPageXml() 0 19 1
A getPageContent() 0 7 2
A export() 0 18 1
A getData() 0 27 3
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace moodleexport;
6
7
/**
8
 * Class PageExport.
9
 *
10
 * Handles the export of pages within a course.
11
 */
12
class PageExport extends ActivityExport
13
{
14
    /**
15
     * Export a page to the specified directory.
16
     *
17
     * @param int $activityId The ID of the page.
18
     * @param string $exportDir The directory where the page 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 page export will be saved
25
        $pageDir = $this->prepareActivityDirectory($exportDir, 'page', $moduleId);
26
27
        // Retrieve page data
28
        $pageData = $this->getData($activityId, $sectionId);
29
30
        // Generate XML files
31
        $this->createPageXml($pageData, $pageDir);
32
        $this->createModuleXml($pageData, $pageDir);
33
        $this->createGradesXml($pageData, $pageDir);
34
        $this->createFiltersXml($pageData, $pageDir);
35
        $this->createGradeHistoryXml($pageData, $pageDir);
36
        $this->createInforefXml($pageData, $pageDir);
37
        $this->createRolesXml($pageData, $pageDir);
38
        $this->createCommentsXml($pageData, $pageDir);
39
        $this->createCalendarXml($pageData, $pageDir);
40
    }
41
42
    /**
43
     * Create the XML file for the page.
44
     */
45
    private function createPageXml(array $pageData, string $pageDir): void
46
    {
47
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
48
        $xmlContent .= '<activity id="' . $pageData['id'] . '" moduleid="' . $pageData['moduleid'] . '" modulename="page" contextid="' . $pageData['contextid'] . '">' . PHP_EOL;
49
        $xmlContent .= '  <page id="' . $pageData['id'] . '">' . PHP_EOL;
50
        $xmlContent .= '    <name>' . htmlspecialchars($pageData['name']) . '</name>' . PHP_EOL;
51
        $xmlContent .= '    <intro>' . htmlspecialchars($pageData['intro']) . '</intro>' . PHP_EOL;
52
        $xmlContent .= '    <introformat>1</introformat>' . PHP_EOL;
53
        $xmlContent .= '    <content>' . htmlspecialchars($pageData['content']) . '</content>' . PHP_EOL;
54
        $xmlContent .= '    <contentformat>1</contentformat>' . PHP_EOL;
55
        $xmlContent .= '    <legacyfiles>0</legacyfiles>' . PHP_EOL;
56
        $xmlContent .= '    <display>5</display>' . PHP_EOL;
57
        $xmlContent .= '    <displayoptions>a:3:{s:12:"printheading";s:1:"1";s:10:"printintro";s:1:"0";s:17:"printlastmodified";s:1:"1";}</displayoptions>' . PHP_EOL;
58
        $xmlContent .= '    <revision>1</revision>' . PHP_EOL;
59
        $xmlContent .= '    <timemodified>' . $pageData['timemodified'] . '</timemodified>' . PHP_EOL;
60
        $xmlContent .= '  </page>' . PHP_EOL;
61
        $xmlContent .= '</activity>';
62
63
        $this->createXmlFile('page', $xmlContent, $pageDir);
64
    }
65
66
    /**
67
     * Get page data dynamically from the course.
68
     */
69
    public function getData(int $pageId, int $sectionId): ?array
70
    {
71
        $pageResources = $this->course->resources[RESOURCE_DOCUMENT];
72
73
        foreach ($pageResources as $page) {
74
            if ($page->source_id == $pageId) {
75
                $contextid = $this->course->info['real_id'];
76
77
                return [
78
                    'id' => $page->source_id,
79
                    'moduleid' => $page->source_id,
80
                    'modulename' => 'page',
81
                    'contextid' => $contextid,
82
                    'name' => $page->title,
83
                    'intro' => $page->comment ?? '',
84
                    'content' => $this->getPageContent($page),
85
                    'sectionid' => $sectionId,
86
                    'sectionnumber' => 1,
87
                    'display' => 0,
88
                    'timemodified' => time(),
89
                    'users' => [],
90
                    'files' => [],
91
                ];
92
            }
93
        }
94
95
        return null;
96
    }
97
98
    /**
99
     * Retrieves the content of the page.
100
     */
101
    private function getPageContent(object $page): string
102
    {
103
        if ($page->file_type === 'file') {
104
            return file_get_contents($this->course->path . $page->path);
105
        }
106
107
        return '';
108
    }
109
}
110