Passed
Pull Request — 1.11.x (#6146)
by
unknown
14:17
created

PageExport::getData()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 50
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 50
rs 8.3786
cc 7
nc 7
nop 2
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
     * Get page data dynamically from the course.
44
     */
45
    public function getData(int $pageId, int $sectionId): ?array
46
    {
47
        $contextid = $this->course->info['real_id'];
48
        if ($pageId === 0) {
49
            if (
50
                isset($this->course->resources[RESOURCE_TOOL_INTRO]['course_homepage']) &&
51
                is_object($this->course->resources[RESOURCE_TOOL_INTRO]['course_homepage']) &&
52
                !empty($this->course->resources[RESOURCE_TOOL_INTRO]['course_homepage']->intro_text)
53
            ) {
54
55
                return [
56
                    'id' => 0,
57
                    'moduleid' => 0,
58
                    'modulename' => 'page',
59
                    'contextid' => $contextid,
60
                    'name' => get_lang('Introduction'),
61
                    'intro' => '',
62
                    'content' => trim($this->course->resources[RESOURCE_TOOL_INTRO]['course_homepage']->intro_text),
63
                    'sectionid' => $sectionId,
64
                    'sectionnumber' => 1,
65
                    'display' => 0,
66
                    'timemodified' => time(),
67
                    'users' => [],
68
                    'files' => [],
69
                ];
70
            }
71
        }
72
73
        $pageResources = $this->course->resources[RESOURCE_DOCUMENT] ?? [];
74
        foreach ($pageResources as $page) {
75
            if ($page->source_id == $pageId) {
76
                return [
77
                    'id' => $page->source_id,
78
                    'moduleid' => $page->source_id,
79
                    'modulename' => 'page',
80
                    'contextid' => $contextid,
81
                    'name' => $page->title,
82
                    'intro' => $page->comment ?? '',
83
                    'content' => $this->getPageContent($page),
84
                    'sectionid' => $sectionId,
85
                    'sectionnumber' => 1,
86
                    'display' => 0,
87
                    'timemodified' => time(),
88
                    'users' => [],
89
                    'files' => [],
90
                ];
91
            }
92
        }
93
94
        return null;
95
    }
96
97
    /**
98
     * Create the XML file for the page.
99
     */
100
    private function createPageXml(array $pageData, string $pageDir): void
101
    {
102
        $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
103
        $xmlContent .= '<activity id="'.$pageData['id'].'" moduleid="'.$pageData['moduleid'].'" modulename="page" contextid="'.$pageData['contextid'].'">'.PHP_EOL;
104
        $xmlContent .= '  <page id="'.$pageData['id'].'">'.PHP_EOL;
105
        $xmlContent .= '    <name>'.htmlspecialchars($pageData['name']).'</name>'.PHP_EOL;
106
        $xmlContent .= '    <intro>'.htmlspecialchars($pageData['intro']).'</intro>'.PHP_EOL;
107
        $xmlContent .= '    <introformat>1</introformat>'.PHP_EOL;
108
        $xmlContent .= '    <content>'.htmlspecialchars($pageData['content']).'</content>'.PHP_EOL;
109
        $xmlContent .= '    <contentformat>1</contentformat>'.PHP_EOL;
110
        $xmlContent .= '    <legacyfiles>0</legacyfiles>'.PHP_EOL;
111
        $xmlContent .= '    <display>5</display>'.PHP_EOL;
112
        $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;
113
        $xmlContent .= '    <revision>1</revision>'.PHP_EOL;
114
        $xmlContent .= '    <timemodified>'.$pageData['timemodified'].'</timemodified>'.PHP_EOL;
115
        $xmlContent .= '  </page>'.PHP_EOL;
116
        $xmlContent .= '</activity>';
117
118
        $this->createXmlFile('page', $xmlContent, $pageDir);
119
    }
120
121
    /**
122
     * Retrieves the content of the page.
123
     */
124
    private function getPageContent(object $page): string
125
    {
126
        if ($page->file_type === 'file') {
127
            return file_get_contents($this->course->path.$page->path);
128
        }
129
130
        return '';
131
    }
132
}
133