Passed
Push — master ( 7ade69...469b77 )
by Julito
11:15
created

CourseDescriptionController::listing()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 85
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 52
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 85
rs 8.7361

How to fix   Long Method   

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
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Framework\Container;
5
use Chamilo\CourseBundle\Entity\CCourseDescription;
6
7
/**
8
 * Class CourseDescriptionController
9
 * This file contains class used like controller,
10
 * it should be included inside a dispatcher file (e.g: index.php).
11
 *
12
 * @author Christian Fasanando <[email protected]>
13
 */
14
class CourseDescriptionController
15
{
16
    private $toolname;
17
    private $view;
18
19
    /**
20
     * Constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->toolname = 'course_description';
25
        $this->view = new View($this->toolname);
0 ignored issues
show
Deprecated Code introduced by
The class View has been deprecated: use Template class ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

25
        $this->view = /** @scrutinizer ignore-deprecated */ new View($this->toolname);
Loading history...
26
    }
27
28
    /**
29
     * It's used for listing course description,
30
     * render to listing view.
31
     *
32
     * @param bool    true for listing history (optional)
33
     * @param array    message for showing by action['edit','add','destroy'] (optional)
34
     */
35
    public function listing($history = false, $messages = [])
36
    {
37
        $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
38
        $course_description = new CourseDescription();
39
        $session_id = api_get_session_id();
40
        $data = [];
41
        $course_description->set_session_id($session_id);
42
        $data['descriptions'] = $course_description->get_description_data();
43
        $data['default_description_titles'] = $course_description->get_default_description_title();
44
        $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
45
        $data['default_description_icon'] = $course_description->get_default_description_icon();
46
        $data['messages'] = $messages;
47
48
        api_protect_course_script(true);
49
50
        // Prepare confirmation code for item deletion
51
        global $htmlHeadXtra;
52
        $htmlHeadXtra[] = "<script>
53
        function confirmation(name) {
54
            if (confirm(\" ".trim(get_lang('Are you sure to delete'))." \"+name+\"?\")) {
55
                return true;
56
            } else {
57
                return false;
58
            }
59
        }
60
        </script>";
61
62
        /*foreach ($data['descriptions'] as $id => $description) {
63
            if (!empty($description['content'])
64
                && false !== strpos($description['content'], '<iframe')
65
            ) {
66
                header("X-XSS-Protection: 0");
67
            }
68
            // Add an escape version for the JS code of delete confirmation
69
            if ($description) {
70
                $data['descriptions'][$id]['title_js'] = addslashes(strip_tags($description['title']));
71
            }
72
        }*/
73
        $actions = null;
74
        $actionLeft = null;
75
        // display actions menu
76
        if ($is_allowed_to_edit) {
77
            $categories = [];
78
            foreach ($data['default_description_titles'] as $id => $title) {
79
                $categories[$id] = $title;
80
            }
81
            $categories[ADD_BLOCK] = get_lang('Other');
82
            $i = 1;
83
84
            ksort($categories);
85
            foreach ($categories as $id => $title) {
86
                if (ADD_BLOCK == $i) {
87
                    $actionLeft .= '<a href="index.php?'.api_get_cidreq().'&action=add">'.
88
                        Display::return_icon(
89
                            $data['default_description_icon'][$id],
90
                            $title,
91
                            '',
92
                            ICON_SIZE_MEDIUM
93
                        ).
94
                        '</a>';
95
                    break;
96
                } else {
97
                    $actionLeft .= '<a href="index.php?action=edit&'.api_get_cidreq().'&description_type='.$id.'">'.
98
                        Display::return_icon(
99
                            $data['default_description_icon'][$id],
100
                            $title,
101
                            '',
102
                            ICON_SIZE_MEDIUM
103
                        ).
104
                        '</a>';
105
                    $i++;
106
                }
107
            }
108
            $actions = Display::toolbarAction('toolbar', [0 => $actionLeft]);
109
        }
110
111
        $tpl = new Template(get_lang('Description'));
112
        $tpl->assign('listing', $data);
113
        $tpl->assign('is_allowed_to_edit', $is_allowed_to_edit);
114
        $tpl->assign('actions', $actions);
115
        $tpl->assign('session_id', $session_id);
116
        $templateName = $tpl->get_template('course_description/index.tpl');
117
        $content = $tpl->fetch($templateName);
118
        $tpl->assign('content', $content);
119
        $tpl->display_one_col_template();
120
    }
121
122
    /**
123
     * It's used for editing a course description,
124
     * render to listing or edit view.
125
     *
126
     * @param int $id               description item id
127
     * @param int $description_type description type id
128
     */
129
    public function edit($id, $description_type)
130
    {
131
        $course_description = new CourseDescription();
132
        $session_id = api_get_session_id();
133
        $course_description->set_session_id($session_id);
134
        $data = [];
135
        $data['id'] = $id;
136
        $affected_rows = null;
137
        if ("POST" == strtoupper($_SERVER['REQUEST_METHOD'])) {
138
            if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
139
                if (1) {
140
                    $title = $_POST['title'];
141
                    $content = $_POST['contentDescription'];
142
                    $description_type = $_POST['description_type'];
143
                    $id = $_POST['id'];
144
                    if (empty($id)) {
145
                        // If the ID was not provided, find the first matching description item given the item type
146
                        $description = $course_description->get_data_by_description_type(
147
                            $description_type
148
                        );
149
                        if (count($description) > 0) {
150
                            $id = $description['iid'];
151
                        }
152
                        // If no corresponding description is found, edit a new one
153
                    }
154
                    $progress = isset($_POST['progress']) ? $_POST['progress'] : '';
155
                    $repo = Container::getCourseDescriptionRepository();
156
157
                    /** @var CCourseDescription $courseDescription */
158
                    $courseDescription = $repo->find($id);
159
                    $courseDescription
160
                        ->setTitle($title)
161
                        ->setProgress($progress)
162
                        ->setContent($content)
163
                    ;
164
                    $repo->getEntityManager()->persist($courseDescription);
165
                    $repo->getEntityManager()->flush();
166
167
                    /*$course_description->set_description_type($description_type);
168
                    $course_description->set_title($title);
169
                    $course_description->set_content($content);
170
                    $course_description->set_progress($progress);
171
                    $thematic_advance = $course_description->get_data_by_id($id);
172
173
                    if (!empty($thematic_advance)) {
174
                        $course_description->set_id($id);
175
                        $course_description->update();
176
                    } else {
177
                        $course_description->insert();
178
                    }*/
179
180
                    Display::addFlash(
181
                        Display::return_message(
182
                            get_lang('The description has been updated')
183
                        )
184
                    );
185
                }
186
                $this->listing(false);
187
            } else {
188
                $data['error'] = 1;
189
                $data['default_description_titles'] = $course_description->get_default_description_title();
190
                $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
191
                $data['default_description_icon'] = $course_description->get_default_description_icon();
192
                $data['question'] = $course_description->get_default_question();
193
                $data['information'] = $course_description->get_default_information();
194
                $data['description_title'] = $_POST['title'];
195
                $data['description_content'] = $_POST['contentDescription'];
196
                $data['description_type'] = $_POST['description_type'];
197
                $data['progress'] = $_POST['progress'];
198
                $data['descriptions'] = $course_description->get_data_by_id($_POST['id']);
199
                // render to the view
200
                $this->view->set_data($data);
201
                $this->view->set_layout('layout');
202
                $this->view->set_template('edit');
203
                $this->view->render();
204
            }
205
        } else {
206
            $data['default_description_titles'] = $course_description->get_default_description_title();
207
            $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
208
            $data['default_description_icon'] = $course_description->get_default_description_icon();
209
            $data['question'] = $course_description->get_default_question();
210
            $data['information'] = $course_description->get_default_information();
211
212
            $data['description_type'] = $description_type;
213
            if (empty($id)) {
214
                // If the ID was not provided, find the first matching description item given the item type
215
                $description = $course_description->get_data_by_description_type($description_type);
216
                if (count($description) > 0) {
217
                    $id = $description['id'];
218
                }
219
                // If no corresponding description is found, edit a new one
220
            }
221
            if (!empty($id)) {
222
                if (isset($_GET['id_session'])) {
223
                    $session_id = intval($_GET['id_session']);
224
                }
225
                $course_description_data = $course_description->get_data_by_id(
226
                    $id,
227
                    null,
228
                    $session_id
229
                );
230
                $data['description_type'] = $course_description_data['description_type'];
231
                $data['description_title'] = $course_description_data['description_title'];
232
                $data['description_content'] = $course_description_data['description_content'];
233
                $data['progress'] = $course_description_data['progress'];
234
                $data['descriptions'] = $course_description->get_data_by_description_type(
235
                    $description_type,
236
                    null,
237
                    $session_id
238
                );
239
            }
240
241
            // render to the view
242
            $this->view->set_data($data);
243
            $this->view->set_layout('layout');
244
            $this->view->set_template('edit');
245
            $this->view->render();
246
        }
247
    }
248
249
    /**
250
     * It's used for adding a course description,
251
     * render to listing or add view.
252
     */
253
    public function add()
254
    {
255
        $course_description = new CourseDescription();
256
        $session_id = api_get_session_id();
257
        $course_description->set_session_id($session_id);
258
259
        $data = [];
260
        if ("POST" == strtoupper($_SERVER['REQUEST_METHOD'])) {
261
            if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
262
                if (1) {
263
                    $title = $_POST['title'];
264
                    $content = $_POST['contentDescription'];
265
                    $description_type = $_POST['description_type'];
266
                    if ($description_type >= ADD_BLOCK) {
267
                        $course_description->set_description_type($description_type);
268
                        $course_description->set_title($title);
269
                        $course_description->set_content($content);
270
                        $course_description->insert(api_get_course_int_id());
271
                    }
272
273
                    Display::addFlash(
274
                        Display::return_message(
275
                            get_lang('The description has been updated')
276
                        )
277
                    );
278
                }
279
                $this->listing(false);
280
            } else {
281
                $data['error'] = 1;
282
                $data['default_description_titles'] = $course_description->get_default_description_title();
283
                $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
284
                $data['default_description_icon'] = $course_description->get_default_description_icon();
285
                $data['question'] = $course_description->get_default_question();
286
                $data['information'] = $course_description->get_default_information();
287
                $data['description_title'] = $_POST['title'];
288
                $data['description_content'] = $_POST['contentDescription'];
289
                $data['description_type'] = $_POST['description_type'];
290
                $this->view->set_data($data);
291
                $this->view->set_layout('layout');
292
                $this->view->set_template('add');
293
                $this->view->render();
294
            }
295
        } else {
296
            $data['default_description_titles'] = $course_description->get_default_description_title();
297
            $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
298
            $data['default_description_icon'] = $course_description->get_default_description_icon();
299
            $data['question'] = $course_description->get_default_question();
300
            $data['information'] = $course_description->get_default_information();
301
            $data['description_type'] = $course_description->get_max_description_type();
302
            // render to the view
303
            $this->view->set_data($data);
304
            $this->view->set_layout('layout');
305
            $this->view->set_template('add');
306
            $this->view->render();
307
        }
308
    }
309
310
    /**
311
     * It's used for destroy a course description,
312
     * render to listing view.
313
     *
314
     * @param int $id description type
315
     */
316
    public function destroy($id)
317
    {
318
        $course_description = new CourseDescription();
319
        $session_id = api_get_session_id();
320
        $course_description->set_session_id($session_id);
321
        if (!empty($id)) {
322
            $course_description->set_id($id);
323
            $course_description->delete();
324
            Display::addFlash(
325
                Display::return_message(get_lang('Description has been deleted'))
326
            );
327
        }
328
        $this->listing(false);
329
    }
330
}
331