Passed
Push — 1.11.x ( 3fb452...83a2b9 )
by Angel Fernando Quiroz
10:36
created

LpIndexGenerator::generateToc()   C

Complexity

Conditions 15
Paths 128

Size

Total Lines 74
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 50
c 1
b 0
f 0
nc 128
nop 0
dl 0
loc 74
rs 5.6833

How to fix   Long Method    Complexity   

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
/**
6
 * Class LpIndexGenerator
7
 */
8
class LpIndexGenerator
9
{
10
    /**
11
     * @var learnpath
12
     */
13
    private $lp;
14
    /**
15
     * @var array
16
     */
17
    private $courseInfo;
18
    /**
19
     * @var DOMDocument
20
     */
21
    private $domDocument;
22
23
    public function __construct(learnpath $lp)
24
    {
25
        $this->lp = $lp;
26
        $this->courseInfo = api_get_course_info();
27
        $this->domDocument = new DOMDocument();
28
29
        $this->generateHtml();
30
    }
31
32
    private function generateHtml()
33
    {
34
        $iso = api_get_language_isocode();
35
        $title = api_utf8_encode($this->lp->get_name());
36
        $bootstrapCs = file_get_contents(api_get_path(SYS_PUBLIC_PATH).'assets/bootstrap/dist/css/bootstrap.min.css');
37
38
        $this->domDocument->loadHTML(
39
            '<!doctype html>
40
            <html lang="'.$iso.'">
41
            <head>
42
            <meta charset="UTF-8">
43
            <meta name="viewport"
44
                  content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0">
45
            <meta http-equiv="X-UA-Compatible" content="ie=edge">
46
            <title>'.$title.'</title>
47
            <style>
48
                '.$bootstrapCs.'
49
                .page-header { margin-top: 0; padding-top: 10px; }
50
            </style>
51
            </head>
52
            <body>
53
            <div class="container-fluid">
54
                <h1 class="page-header">'.$title.'</h1>
55
                <div class="row">
56
                    <div class="col-md-3">
57
                        <ul id="toc__ul"></ul>
58
                    </div>
59
                    <div class="col-md-9">
60
                        <div class="embed-responsive embed-responsive-16by9">
61
                            <iframe class="embed-responsive-item" id="content__iframe" name="content-frame"
62
                                    src="" frameborder="0"></iframe>
63
                        </div>
64
                    </div>
65
                </div>
66
            </div>
67
            </body>
68
            </html>'
69
        );
70
    }
71
72
    private function generateToc()
73
    {
74
        $ulNode = $this->domDocument->getElementById('toc__ul');
75
76
        $folderName = 'document';
77
        $pathToRemove = '';
78
        $pathToReplace = '';
79
        $result = $this->lp->generate_lp_folder($this->courseInfo);
80
81
        if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
82
            $pathToRemove = 'document'.$result['dir'];
83
            $pathToReplace = $folderName;
84
        }
85
86
        if ($this->lp->ref === 'chamilo_scorm_export') {
87
            $pathToRemove = 'scorm/'.$this->lp->path.'/document/';
88
        }
89
90
        foreach ($this->lp->ordered_items as $itemId) {
91
            $item = $this->lp->items[$itemId];
92
93
            if (!in_array($item->type, [TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION])) {
94
                $myFilePath = $item->get_file_path('scorm/'.$this->lp->path.'/');
95
                $itemFilePath = $myFilePath;
96
97
                if (!empty($pathToRemove)) {
98
                    $itemFilePath = str_replace($pathToRemove, $pathToReplace, $myFilePath);
99
100
                    if ($this->lp->ref === 'chamilo_scorm_export') {
101
                        $pathToRemove = 'scorm/'.$this->lp->path.'/';
102
                        $itemFilePath = str_replace($pathToRemove, '', $myFilePath);
103
                    }
104
                }
105
            } elseif (TOOL_LINK === $item->type) {
106
                $itemFilePath = "link_{$item->get_id()}.html";
107
            } elseif (TOOL_QUIZ === $item->type) {
108
                $itemFilePath = "quiz_{$item->get_id()}.html";
109
            } else {
110
                continue;
111
            }
112
113
            $itemText = htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES);
114
115
            $liNode = $this->domDocument->createElement('li');
116
            $liNode->setAttribute('id', "item_{$item->get_id()}");
117
118
            if (!empty($item->parent) && $item->parent != 0) {
119
                $possibleItemParent = $this->lp->get_scorm_xml_node(
120
                    $ulNode->childNodes,
121
                    "item_$item->parent",
122
                    'li',
123
                    'id'
124
                );
125
126
                if ($possibleItemParent instanceof DOMElement) {
127
                    $innerUlNode = $possibleItemParent->getElementsByTagName('ul')->item(0)
128
                        ?: $this->domDocument->createElement('ul');
129
                    $innerUlNode->appendChild($liNode);
130
131
                    $possibleItemParent->appendChild($innerUlNode);
132
                }
133
            } else {
134
                $ulNode->appendChild($liNode);
135
            }
136
137
            if (!empty($itemFilePath)) {
138
                $aNode = $this->domDocument->createElement('a', $itemText);
139
                $aNode->setAttribute('href', $itemFilePath);
140
                $aNode->setAttribute('target', 'content-frame');
141
142
                $liNode->appendChild($aNode);
143
            } else {
144
                $liNode->appendChild(
145
                    $this->domDocument->createTextNode($itemText)
146
                );
147
            }
148
        }
149
    }
150
151
    public function generate(): string
152
    {
153
        $this->generateToc();
154
155
        $indexHtml = @$this->domDocument->saveHTML();
156
157
        return api_utf8_decode_xml($indexHtml);
158
    }
159
}
160