Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

import/src/converter/Cc13Forum.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
5
6
class Cc13Forum extends Cc13Entities
7
{
8
    public function fullPath($path, $dir_sep = DIRECTORY_SEPARATOR)
9
    {
10
        $token = '$IMS-CC-FILEBASE$';
11
        $path = str_replace($token, '', $path);
12
13
        if (is_string($path) && ($path != '')) {
14
            $dot_dir = '.';
15
            $up_dir = '..';
16
            $length = strlen($path);
17
            $rtemp = trim($path);
18
            $start = strrpos($path, $dir_sep);
19
            $can_continue = ($start !== false);
20
            $result = $can_continue ? '' : $path;
21
            $rcount = 0;
22
23
            while ($can_continue) {
24
                $dir_part = ($start !== false) ? substr($rtemp, $start + 1, $length - $start) : $rtemp;
25
                $can_continue = ($dir_part !== false);
26
27
                if ($can_continue) {
28
                    if ($dir_part != $dot_dir) {
29
                        if ($dir_part == $up_dir) {
30
                            $rcount++;
31
                        } else {
32
                            if ($rcount > 0) {
33
                                $rcount--;
34
                            } else {
35
                                $result = ($result == '') ? $dir_part : $dir_part.$dir_sep.$result;
36
                            }
37
                        }
38
                    }
39
                    $rtemp = substr($path, 0, $start);
40
                    $start = strrpos($rtemp, $dir_sep);
41
                    $can_continue = (($start !== false) || (strlen($rtemp) > 0));
42
                }
43
            }
44
        }
45
46
        return $result;
47
    }
48
49
    public function generateData()
50
    {
51
        $data = [];
52
        if (!empty(Cc1p3Convert::$instances['instances']['forum'])) {
53
            foreach (Cc1p3Convert::$instances['instances']['forum'] as $instance) {
54
                $data[] = $this->getForumData($instance);
55
            }
56
        }
57
58
        return $data;
59
    }
60
61
    public function getForumData($instance)
62
    {
63
        $topic_data = $this->getTopicData($instance);
64
65
        $values = [];
66
        if (!empty($topic_data)) {
67
            $values = [
68
                'instance' => $instance['instance'],
69
                'title' => self::safexml($topic_data['title']),
70
                'description' => self::safexml($topic_data['description']),
71
            ];
72
        }
73
74
        return $values;
75
    }
76
77
    public function storeForums($forums)
78
    {
79
        // Create a Forum category for the import CC 1.3.
80
        $courseInfo = api_get_course_info();
81
        $catForumValues['forum_category_title'] = 'CC1p3';
82
        $catForumValues['forum_category_comment'] = '';
83
        $catId = store_forumcategory(
84
            $catForumValues,
85
            $courseInfo,
86
            false
87
        );
88
89
        foreach ($forums as $forum) {
90
            $forumValues = [];
91
            $forumValues['forum_title'] = $forum['title'];
92
            $forumValues['forum_image'] = '';
93
            $forumValues['forum_comment'] = strip_tags($forum['description']);
94
            $forumValues['forum_category'] = $catId;
95
            $forumValues['moderated'] = 0;
96
            store_forum($forumValues, $courseInfo);
97
        }
98
99
        return true;
100
    }
101
102
    public function getTopicData($instance)
103
    {
104
        $topic_data = [];
105
106
        $topic_file = $this->getExternalXml($instance['resource_indentifier']);
107
108
        if (!empty($topic_file)) {
109
            $topic_file_path = Cc1p3Convert::$pathToManifestFolder.DIRECTORY_SEPARATOR.$topic_file;
110
            $topic_file_dir = dirname($topic_file_path);
111
            $topic = $this->loadXmlResource($topic_file_path);
112
113
            if (!empty($topic)) {
114
                $xpath = Cc1p3Convert::newxPath($topic, Cc1p3Convert::$forumns);
115
116
                $topic_title = $xpath->query('/dt:topic/dt:title');
117
                if ($topic_title->length > 0 && !empty($topic_title->item(0)->nodeValue)) {
118
                    $topic_title = $topic_title->item(0)->nodeValue;
119
                } else {
120
                    $topic_title = 'Untitled Topic';
121
                }
122
123
                $topic_text = $xpath->query('/dt:topic/dt:text');
124
                $topic_text = !empty($topic_text->item(0)->nodeValue) ? $this->updateSources($topic_text->item(0)->nodeValue, dirname($topic_file)) : '';
125
                $topic_text = !empty($topic_text) ? str_replace("%24", "\$", $this->includeTitles($topic_text)) : '';
126
127
                if (!empty($topic_title)) {
128
                    $topic_data['title'] = $topic_title;
129
                    $topic_data['description'] = $topic_text;
130
                }
131
            }
132
133
            $topic_attachments = $xpath->query('/dt:topic/dt:attachments/dt:attachment/@href');
134
135
            if ($topic_attachments->length > 0) {
136
                $attachment_html = '';
137
138
                foreach ($topic_attachments as $file) {
139
                    $attachment_html .= $this->generateAttachmentHtml($this->fullPath($file->nodeValue, '/'));
140
                }
141
142
                $topic_data['description'] = !empty($attachment_html) ? $topic_text.'<p>Attachments:</p>'.$attachment_html : $topic_text;
143
            }
144
        }
145
146
        return $topic_data;
147
    }
148
149
    private function generateAttachmentHtml($filename)
150
    {
151
        $images_extensions = ['gif', 'jpeg', 'jpg', 'jif', 'jfif', 'png', 'bmp'];
152
153
        $fileinfo = pathinfo($filename);
154
155
        if (in_array($fileinfo['extension'], $images_extensions)) {
156
            return '<img src="$@FILEPHP@$/'.$filename.'" title="'.$fileinfo['basename'].'" alt="'.$fileinfo['basename'].'" /><br />';
157
        } else {
158
            return '<a href="$@FILEPHP@$/'.$filename.'" title="'.$fileinfo['basename'].'" alt="'.$fileinfo['basename'].'">'.$fileinfo['basename'].'</a><br />';
159
        }
160
161
        return '';
0 ignored issues
show
return '' is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
162
    }
163
}
164