Passed
Push — master ( fea574...954af2 )
by Julito
10:02 queued 01:26
created

CourseDescriptionController   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 404
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 243
dl 0
loc 404
rs 9.44
c 0
b 0
f 0
wmc 37

6 Methods

Rating   Name   Duplication   Size   Complexity  
F edit() 0 181 22
A destroy() 0 15 2
A listing() 0 49 1
A __construct() 0 3 1
B add() 0 67 6
A getToolbar() 0 44 5
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;
0 ignored issues
show
introduced by
The private property $view is not used, and could be removed.
Loading history...
18
19
    /**
20
     * Constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->toolname = 'course_description';
25
    }
26
27
    public function getToolbar()
28
    {
29
        $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
30
        $course_description = new CourseDescription();
31
        $list = $course_description->get_default_description_title();
32
        $iconList = $course_description->get_default_description_icon();
33
        $actions = '';
34
        $actionLeft = '';
35
        if ($is_allowed_to_edit) {
36
            $categories = [];
37
            foreach ($list as $id => $title) {
38
                $categories[$id] = $title;
39
            }
40
            $categories[ADD_BLOCK] = get_lang('Other');
41
            $i = 1;
42
43
            ksort($categories);
44
            foreach ($categories as $id => $title) {
45
                if (ADD_BLOCK == $i) {
46
                    $actionLeft .= '<a href="index.php?'.api_get_cidreq().'&action=add">'.
47
                        Display::return_icon(
48
                            $iconList[$id],
49
                            $title,
50
                            '',
51
                            ICON_SIZE_MEDIUM
52
                        ).
53
                        '</a>';
54
                    break;
55
                } else {
56
                    $actionLeft .= '<a href="index.php?action=edit&'.api_get_cidreq().'&description_type='.$id.'">'.
57
                        Display::return_icon(
58
                            $iconList[$id],
59
                            $title,
60
                            '',
61
                            ICON_SIZE_MEDIUM
62
                        ).
63
                        '</a>';
64
                    $i++;
65
                }
66
            }
67
            $actions = Display::toolbarAction('toolbar', [0 => $actionLeft]);
68
        }
69
70
        return $actions;
71
    }
72
73
    /**
74
     * It's used for listing course description,
75
     * render to listing view.
76
     *
77
     * @param bool    true for listing history (optional)
78
     * @param array    message for showing by action['edit','add','destroy'] (optional)
79
     */
80
    public function listing($history = false, $messages = [])
81
    {
82
        $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
83
        $course_description = new CourseDescription();
84
        $session_id = api_get_session_id();
85
        $data = [];
86
        $course_description->set_session_id($session_id);
87
        $data['descriptions'] = $course_description->get_description_data();
88
        $data['default_description_titles'] = $course_description->get_default_description_title();
89
        $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
90
        $data['default_description_icon'] = $course_description->get_default_description_icon();
91
        $data['messages'] = $messages;
92
93
        api_protect_course_script(true);
94
95
        // Prepare confirmation code for item deletion
96
        global $htmlHeadXtra;
97
        $htmlHeadXtra[] = "<script>
98
        function confirmation(name) {
99
            if (confirm(\" ".trim(get_lang('Are you sure to delete'))." \"+name+\"?\")) {
100
                return true;
101
            } else {
102
                return false;
103
            }
104
        }
105
        </script>";
106
107
        /*foreach ($data['descriptions'] as $id => $description) {
108
            if (!empty($description['content'])
109
                && false !== strpos($description['content'], '<iframe')
110
            ) {
111
                header("X-XSS-Protection: 0");
112
            }
113
            // Add an escape version for the JS code of delete confirmation
114
            if ($description) {
115
                $data['descriptions'][$id]['title_js'] = addslashes(strip_tags($description['title']));
116
            }
117
        }*/
118
        $actions = self::getToolbar();
0 ignored issues
show
Bug Best Practice introduced by
The method CourseDescriptionController::getToolbar() is not static, but was called statically. ( Ignorable by Annotation )

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

118
        /** @scrutinizer ignore-call */ 
119
        $actions = self::getToolbar();
Loading history...
119
120
        $tpl = new Template(get_lang('Description'));
121
        $tpl->assign('listing', $data);
122
        $tpl->assign('is_allowed_to_edit', $is_allowed_to_edit);
123
        $tpl->assign('actions', $actions);
124
        $tpl->assign('session_id', $session_id);
125
        $templateName = $tpl->get_template('course_description/index.tpl');
126
        $content = $tpl->fetch($templateName);
127
        $tpl->assign('content', $content);
128
        $tpl->display_one_col_template();
129
    }
130
131
    /**
132
     * It's used for editing a course description,
133
     * render to listing or edit view.
134
     *
135
     * @param int $id               description item id
136
     * @param int $description_type description type id
137
     */
138
    public function edit($id, $description_type)
139
    {
140
        $course_description = new CourseDescription();
141
        $session_id = api_get_session_id();
142
        $course_description->set_session_id($session_id);
143
        $data = [];
144
        $data['id'] = $id;
145
        $affected_rows = null;
146
        if ('POST' === strtoupper($_SERVER['REQUEST_METHOD'])) {
147
            if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
148
                $title = $_POST['title'];
149
                $content = $_POST['contentDescription'];
150
                $description_type = $_POST['description_type'];
151
                $id = $_POST['id'];
152
                if (empty($id)) {
153
                    // If the ID was not provided, find the first matching description item given the item type
154
                    $description = $course_description->get_data_by_description_type(
155
                        $description_type
156
                    );
157
                    if (count($description) > 0) {
158
                        $id = $description['iid'];
159
                    }
160
                    // If no corresponding description is found, edit a new one
161
                }
162
                $progress = isset($_POST['progress']) ? $_POST['progress'] : 0;
163
                $repo = Container::getCourseDescriptionRepository();
164
165
                /** @var CCourseDescription $courseDescription */
166
                $courseDescription = $repo->find($id);
167
                if ($courseDescription) {
0 ignored issues
show
introduced by
$courseDescription is of type Chamilo\CourseBundle\Entity\CCourseDescription, thus it always evaluated to true.
Loading history...
168
                    $courseDescription
169
                        ->setTitle($title)
170
                        ->setProgress($progress)
171
                        ->setContent($content)
172
                    ;
173
                    $repo->update($courseDescription);
174
                } else {
175
                    $course_description->set_description_type($description_type);
176
                    $course_description->set_title($title);
177
                    $course_description->set_progress($progress);
178
                    $course_description->set_content($content);
179
                    $course_description->insert(api_get_course_int_id());
180
                }
181
182
                Display::addFlash(
183
                    Display::return_message(
184
                        get_lang('The description has been updated')
185
                    )
186
                );
187
188
                $url = api_get_path(WEB_CODE_PATH).'course_description/index.php?'.api_get_cidreq();
189
                api_location($url);
190
            }
191
        } else {
192
            $default_description_titles = $course_description->get_default_description_title();
193
            $default_description_title_editable = $course_description->get_default_description_title_editable();
194
            $default_description_icon = $course_description->get_default_description_icon();
195
            $question = $course_description->get_default_question();
196
            $information = $course_description->get_default_information();
197
            $description_type = $description_type;
198
            if (empty($id)) {
199
                // If the ID was not provided, find the first matching description item given the item type
200
                $description = $course_description->get_data_by_description_type($description_type);
201
                if (count($description) > 0) {
202
                    $id = $description['id'];
203
                }
204
                // If no corresponding description is found, edit a new one
205
            }
206
            if (!empty($id)) {
207
                if (isset($_GET['id_session'])) {
208
                    $session_id = intval($_GET['id_session']);
209
                }
210
                $course_description_data = $course_description->get_data_by_id(
211
                    $id,
212
                    null,
213
                    $session_id
214
                );
215
                $description_type = $course_description_data['description_type'];
216
                $description_title = $course_description_data['description_title'];
217
                $description_content = $course_description_data['description_content'];
218
                $progress = $course_description_data['progress'];
219
                $descriptions = $course_description->get_data_by_description_type(
220
                    $description_type,
221
                    null,
222
                    $session_id
223
                );
224
            }
225
226
            // render to the view
227
            /*$this->view->set_data($data);
228
            $this->view->set_layout('layout');
229
            $this->view->set_template('edit');
230
            $this->view->render();*/
231
232
            if (empty($id)) {
233
                $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : '';
234
                if (empty($id)) {
235
                    // If the ID was not provided, find the first matching description item given the item type
236
                    $course_description = new CourseDescription();
237
                    $description = $course_description->get_data_by_description_type($description_type);
238
                    if (count($description) > 0) {
239
                        $id = $description['id'];
240
                    }
241
                    // If no corresponding description is found, edit a new one
242
                    unset($course_description);
243
                }
244
            }
245
            $original_id = $id;
246
            // display categories
247
            $categories = [];
248
            foreach ($default_description_titles as $id => $title) {
0 ignored issues
show
introduced by
$id is overwriting one of the parameters of this function.
Loading history...
249
                $categories[$id] = $title;
250
            }
251
            $categories[ADD_BLOCK] = get_lang('Other');
252
253
            // default header title form
254
            $description_type = intval($description_type);
255
            $header = $default_description_titles[$description_type];
256
            if ($description_type >= ADD_BLOCK) {
257
                $header = $default_description_titles[ADD_BLOCK];
258
            }
259
260
            // display form
261
            $form = new FormValidator(
262
                'course_description',
263
                'POST',
264
                'index.php?action=edit&id='.$original_id.'&description_type='.$description_type.'&'.api_get_cidreq()
265
            );
266
            $form->addElement('header', $header);
267
            $form->addElement('hidden', 'id', $original_id);
268
            $form->addElement('hidden', 'description_type', $description_type);
269
            //$form->addElement('hidden', 'sec_token', $token);
270
271
            if (api_get_configuration_value('save_titles_as_html')) {
272
                $form->addHtmlEditor(
273
                    'title',
274
                    get_lang('Title'),
275
                    true,
276
                    false,
277
                    ['ToolbarSet' => 'TitleAsHtml']
278
                );
279
            } else {
280
                $form->addText('title', get_lang('Title'));
281
                $form->applyFilter('title', 'html_filter');
282
            }
283
            $form->addHtmlEditor(
284
                'contentDescription',
285
                get_lang('Content'),
286
                true,
287
                false,
288
                [
289
                    'ToolbarSet' => 'Basic',
290
                    'Width' => '100%',
291
                    'Height' => '200',
292
                ]
293
            );
294
            $form->addButtonCreate(get_lang('Save'));
295
296
            $actions = self::getToolbar();
0 ignored issues
show
Bug Best Practice introduced by
The method CourseDescriptionController::getToolbar() is not static, but was called statically. ( Ignorable by Annotation )

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

296
            /** @scrutinizer ignore-call */ 
297
            $actions = self::getToolbar();
Loading history...
297
            // Set some default values
298
            if (!empty($description_title)) {
299
                $default['title'] = Security::remove_XSS($description_title);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$default was never initialized. Although not strictly required by PHP, it is generally a good practice to add $default = array(); before regardless.
Loading history...
300
            }
301
            if (!empty($description_content)) {
302
                $default['contentDescription'] = Security::remove_XSS($description_content, COURSEMANAGERLOWSECURITY);
303
            }
304
            $default['description_type'] = $description_type;
305
306
            $form->setDefaults($default);
307
308
            if (isset($question[$description_type])) {
309
                $message = '<strong>'.get_lang('Help').'</strong><br />';
310
                $message .= $question[$description_type];
311
                Display::addFlash(Display::return_message($message, 'normal', false));
312
            }
313
            $tpl = new Template(get_lang('Description'));
314
            //$tpl->assign('is_allowed_to_edit', $is_allowed_to_edit);
315
            $tpl->assign('actions', $actions);
316
            $tpl->assign('session_id', $session_id);
317
            $tpl->assign('content', $form->returnForm());
318
            $tpl->display_one_col_template();
319
320
        }
321
    }
322
323
    /**
324
     * It's used for adding a course description,
325
     * render to listing or add view.
326
     */
327
    public function add()
328
    {
329
        $course_description = new CourseDescription();
330
        $session_id = api_get_session_id();
331
        $course_description->set_session_id($session_id);
332
        $actions = self::getToolbar();
0 ignored issues
show
Bug Best Practice introduced by
The method CourseDescriptionController::getToolbar() is not static, but was called statically. ( Ignorable by Annotation )

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

332
        /** @scrutinizer ignore-call */ 
333
        $actions = self::getToolbar();
Loading history...
333
334
        if ('POST' === strtoupper($_SERVER['REQUEST_METHOD'])) {
335
            if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
336
                $title = $_POST['title'];
337
                $content = $_POST['contentDescription'];
338
                $description_type = $_POST['description_type'];
339
                if ($description_type >= ADD_BLOCK) {
340
                    $course_description->set_description_type($description_type);
341
                    $course_description->set_title($title);
342
                    $course_description->set_content($content);
343
                    $course_description->insert(api_get_course_int_id());
344
                }
345
346
                Display::addFlash(
347
                    Display::return_message(
348
                        get_lang('The description has been added')
349
                    )
350
                );
351
                $url = api_get_path(WEB_CODE_PATH).'course_description/index.php?'.api_get_cidreq();
352
                api_location($url);
353
            }
354
        } else {
355
            // display form
356
            $form = new FormValidator(
357
                'course_description',
358
                'POST',
359
                'index.php?action=add&'.api_get_cidreq()
360
            );
361
            //$form->addElement('header', $header);
362
            $form->addElement('hidden', 'description_type', ADD_BLOCK);
363
            if (api_get_configuration_value('save_titles_as_html')) {
364
                $form->addHtmlEditor(
365
                    'title',
366
                    get_lang('Title'),
367
                    true,
368
                    false,
369
                    ['ToolbarSet' => 'TitleAsHtml']
370
                );
371
            } else {
372
                $form->addText('title', get_lang('Title'));
373
                $form->applyFilter('title', 'html_filter');
374
            }
375
            $form->addHtmlEditor(
376
                'contentDescription',
377
                get_lang('Content'),
378
                true,
379
                false,
380
                [
381
                    'ToolbarSet' => 'Basic',
382
                    'Width' => '100%',
383
                    'Height' => '200',
384
                ]
385
            );
386
            $form->addButtonCreate(get_lang('Save'));
387
388
            $tpl = new Template(get_lang('Description'));
389
            //$tpl->assign('is_allowed_to_edit', $is_allowed_to_edit);
390
            $tpl->assign('actions', $actions);
391
            $tpl->assign('session_id', $session_id);
392
            $tpl->assign('content', $form->returnForm());
393
            $tpl->display_one_col_template();
394
        }
395
    }
396
397
    /**
398
     * It's used for destroy a course description,
399
     * render to listing view.
400
     *
401
     * @param int $id description type
402
     */
403
    public function destroy($id)
404
    {
405
        $course_description = new CourseDescription();
406
        $session_id = api_get_session_id();
407
        $course_description->set_session_id($session_id);
408
        if (!empty($id)) {
409
            $course_description->set_id($id);
410
            $course_description->delete();
411
            Display::addFlash(
412
                Display::return_message(get_lang('Description has been deleted'))
413
            );
414
        }
415
416
        $url = api_get_path(WEB_CODE_PATH).'course_description/index.php?'.api_get_cidreq();
417
        api_location($url);
418
    }
419
}
420