Passed
Push — master ( 45bdfa...db3c2b )
by Julito
10:09
created

LearnPathItemForm::setForm()   F

Complexity

Conditions 26
Paths > 20000

Size

Total Lines 149
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 26
eloc 90
c 1
b 0
f 0
nc 72576
nop 4
dl 0
loc 149
rs 0

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
use Chamilo\CourseBundle\Entity\CLpItem;
6
use ChamiloSession as Session;
7
8
class LearnPathItemForm
9
{
10
    public static function setForm(FormValidator $form, $action, learnpath $lp, CLpItem $lpItem)
11
    {
12
        $itemId = $lpItem->getIid();
13
        $item_title = $lpItem->getTitle();
14
        $item_description = $lpItem->getDescription();
15
        $parent = $lpItem->getParentItemId();
16
        $item_type = $lpItem->getItemType();
17
18
        $arrLP = $lp->getItemsForForm();
19
        $lp->tree_array($arrLP);
20
        $arrLP = isset($lp->arrMenu) ? $lp->arrMenu : [];
21
22
        switch ($action) {
23
            case 'add':
24
                $form->addHeader(get_lang('Add'));
25
26
                self::setItemTitle($form);
27
28
                break;
29
30
            case 'edit':
31
                $form->addHeader(get_lang('Edit'));
32
                /*if (isset($data['id'])) {
33
                    $defaults['directory_parent_id'] = $data['id'];
34
                }*/
35
                self::setItemTitle($form);
36
37
                break;
38
39
            case 'move':
40
                $form->addHeader(get_lang('Move'));
41
42
                break;
43
        }
44
45
        $id = $lpItem->getIid();
46
        $arrHide = [];
47
        $count = count($arrLP);
48
        $sections = [];
49
        for ($i = 0; $i < $count; $i++) {
50
            if ('add' !== $action) {
51
                if ('dir' === $arrLP[$i]['item_type'] &&
52
                    !in_array($arrLP[$i]['id'], $arrHide) &&
53
                    !in_array($arrLP[$i]['parent_item_id'], $arrHide)
54
                ) {
55
                    $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
56
                    $arrHide[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
57
                }
58
            }
59
60
            if ('dir' === $arrLP[$i]['item_type']) {
61
                $sections[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
62
                $sections[$arrLP[$i]['id']]['padding'] = 20 + $arrLP[$i]['depth'] * 20;
63
            }
64
        }
65
66
        $parentSelect = $form->addSelect(
67
            'parent',
68
            get_lang('Parent'),
69
            [],
70
            [
71
                'id' => 'idParent',
72
                'onchange' => 'javascript:load_cbo(this.value);',
73
            ]
74
        );
75
        $parentSelect->addOption($lp->name, 0);
76
77
        $arrHide = [];
78
        for ($i = 0; $i < $count; $i++) {
79
            if ($arrLP[$i]['id'] != $id && 'dir' != $arrLP[$i]['item_type']) {
80
                $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
81
            }
82
        }
83
84
        $sectionCount = 0;
85
        foreach ($sections as $key => $value) {
86
            if (0 != $sectionCount) {
87
                // The LP name is also the first section and is not in the same charset like the other sections.
88
                $value['value'] = Security::remove_XSS($value['value']);
89
                $parentSelect->addOption(
90
                    $value['value'],
91
                    $key
92
                    //,'style="padding-left:'.$value['padding'].'px;"'
93
                );
94
            } else {
95
                $value['value'] = Security::remove_XSS($value['value']);
96
                $parentSelect->addOption(
97
                    $value['value'],
98
                    $key
99
                    //'style="padding-left:'.$value['padding'].'px;"'
100
                );
101
            }
102
            $sectionCount++;
103
        }
104
105
        if (!empty($itemId)) {
106
            $parentSelect->setSelected($parent);
107
        } else {
108
            $parent_item_id = Session::read('parent_item_id', 0);
109
            $parentSelect->setSelected($parent_item_id);
110
        }
111
112
        if (is_array($arrLP)) {
113
            reset($arrLP);
114
        }
115
116
        $arrHide = [];
117
        // POSITION
118
        for ($i = 0; $i < $count; $i++) {
119
            if (($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $itemId) ||
120
                TOOL_LP_FINAL_ITEM == $arrLP[$i]['item_type']
121
            ) {
122
                $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
123
            }
124
        }
125
126
        $selectedPosition = $lpItem ? $lpItem->getPreviousItemId() : 0;
0 ignored issues
show
introduced by
$lpItem is of type Chamilo\CourseBundle\Entity\CLpItem, thus it always evaluated to true.
Loading history...
127
128
        $position = $form->addSelect(
129
            'previous',
130
            get_lang('Position'),
131
            [],
132
            ['id' => 'previous']
133
        );
134
135
        $position->addOption(get_lang('First position'), 0);
136
137
        foreach ($arrHide as $key => $value) {
138
            $padding = isset($value['padding']) ? $value['padding'] : 20;
139
            $position->addOption(
140
                $value['value'],
141
                $key,
142
                'style="padding-left:'.$padding.'px;"'
143
            );
144
        }
145
146
        $position->setSelected($selectedPosition);
147
148
        if (is_array($arrLP)) {
149
            reset($arrLP);
150
        }
151
152
        $form->setDefault('title', $item_title);
153
        $form->setDefault('description', $item_description);
154
155
        $form->addHidden('id', $itemId);
156
        $form->addHidden('type', $item_type);
157
        $form->addHidden('post_time', time());
158
        $form->addHidden('path', $lpItem->getPath());
159
    }
160
161
    public static function setItemTitle(FormValidator $form)
162
    {
163
        if (api_get_configuration_value('save_titles_as_html')) {
164
            $form->addHtmlEditor(
165
                'title',
166
                get_lang('Title'),
167
                true,
168
                false,
169
                ['ToolbarSet' => 'TitleAsHtml', 'id' => uniqid('editor')]
170
            );
171
        } else {
172
            $form->addText('title', get_lang('Title'), true, ['id' => 'idTitle', 'class' => 'learnpath_item_form']);
173
            $form->applyFilter('title', 'trim');
174
            $form->applyFilter('title', 'html_filter');
175
        }
176
    }
177
}
178