Passed
Push — master ( c1fbd4...fea574 )
by Julito
11:12
created

CourseDescriptionController::getToolbar()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 44
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 34
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 44
rs 9.0648
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
    public function getToolbar()
29
    {
30
        $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
31
        $course_description = new CourseDescription();
32
        $list = $course_description->get_default_description_title();
33
        $iconList = $course_description->get_default_description_icon();
34
        $actions = '';
35
        $actionLeft = '';
36
        if ($is_allowed_to_edit) {
37
            $categories = [];
38
            foreach ($list as $id => $title) {
39
                $categories[$id] = $title;
40
            }
41
            $categories[ADD_BLOCK] = get_lang('Other');
42
            $i = 1;
43
44
            ksort($categories);
45
            foreach ($categories as $id => $title) {
46
                if (ADD_BLOCK == $i) {
47
                    $actionLeft .= '<a href="index.php?'.api_get_cidreq().'&action=add">'.
48
                        Display::return_icon(
49
                            $iconList[$id],
50
                            $title,
51
                            '',
52
                            ICON_SIZE_MEDIUM
53
                        ).
54
                        '</a>';
55
                    break;
56
                } else {
57
                    $actionLeft .= '<a href="index.php?action=edit&'.api_get_cidreq().'&description_type='.$id.'">'.
58
                        Display::return_icon(
59
                            $iconList[$id],
60
                            $title,
61
                            '',
62
                            ICON_SIZE_MEDIUM
63
                        ).
64
                        '</a>';
65
                    $i++;
66
                }
67
            }
68
            $actions = Display::toolbarAction('toolbar', [0 => $actionLeft]);
69
        }
70
71
        return $actions;
72
    }
73
74
    /**
75
     * It's used for listing course description,
76
     * render to listing view.
77
     *
78
     * @param bool    true for listing history (optional)
79
     * @param array    message for showing by action['edit','add','destroy'] (optional)
80
     */
81
    public function listing($history = false, $messages = [])
82
    {
83
        $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
84
        $course_description = new CourseDescription();
85
        $session_id = api_get_session_id();
86
        $data = [];
87
        $course_description->set_session_id($session_id);
88
        $data['descriptions'] = $course_description->get_description_data();
89
        $data['default_description_titles'] = $course_description->get_default_description_title();
90
        $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
91
        $data['default_description_icon'] = $course_description->get_default_description_icon();
92
        $data['messages'] = $messages;
93
94
        api_protect_course_script(true);
95
96
        // Prepare confirmation code for item deletion
97
        global $htmlHeadXtra;
98
        $htmlHeadXtra[] = "<script>
99
        function confirmation(name) {
100
            if (confirm(\" ".trim(get_lang('Are you sure to delete'))." \"+name+\"?\")) {
101
                return true;
102
            } else {
103
                return false;
104
            }
105
        }
106
        </script>";
107
108
        /*foreach ($data['descriptions'] as $id => $description) {
109
            if (!empty($description['content'])
110
                && false !== strpos($description['content'], '<iframe')
111
            ) {
112
                header("X-XSS-Protection: 0");
113
            }
114
            // Add an escape version for the JS code of delete confirmation
115
            if ($description) {
116
                $data['descriptions'][$id]['title_js'] = addslashes(strip_tags($description['title']));
117
            }
118
        }*/
119
        $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

119
        /** @scrutinizer ignore-call */ 
120
        $actions = self::getToolbar();
Loading history...
120
121
        $tpl = new Template(get_lang('Description'));
122
        $tpl->assign('listing', $data);
123
        $tpl->assign('is_allowed_to_edit', $is_allowed_to_edit);
124
        $tpl->assign('actions', $actions);
125
        $tpl->assign('session_id', $session_id);
126
        $templateName = $tpl->get_template('course_description/index.tpl');
127
        $content = $tpl->fetch($templateName);
128
        $tpl->assign('content', $content);
129
        $tpl->display_one_col_template();
130
    }
131
132
    /**
133
     * It's used for editing a course description,
134
     * render to listing or edit view.
135
     *
136
     * @param int $id               description item id
137
     * @param int $description_type description type id
138
     */
139
    public function edit($id, $description_type)
140
    {
141
        $course_description = new CourseDescription();
142
        $session_id = api_get_session_id();
143
        $course_description->set_session_id($session_id);
144
        $data = [];
145
        $data['id'] = $id;
146
        $affected_rows = null;
147
        if ('POST' === strtoupper($_SERVER['REQUEST_METHOD'])) {
148
            if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
149
                $title = $_POST['title'];
150
                $content = $_POST['contentDescription'];
151
                $description_type = $_POST['description_type'];
152
                $id = $_POST['id'];
153
                if (empty($id)) {
154
                    // If the ID was not provided, find the first matching description item given the item type
155
                    $description = $course_description->get_data_by_description_type(
156
                        $description_type
157
                    );
158
                    if (count($description) > 0) {
159
                        $id = $description['iid'];
160
                    }
161
                    // If no corresponding description is found, edit a new one
162
                }
163
                $progress = isset($_POST['progress']) ? $_POST['progress'] : 0;
164
                $repo = Container::getCourseDescriptionRepository();
165
166
                /** @var CCourseDescription $courseDescription */
167
                $courseDescription = $repo->find($id);
168
                if ($courseDescription) {
0 ignored issues
show
introduced by
$courseDescription is of type Chamilo\CourseBundle\Entity\CCourseDescription, thus it always evaluated to true.
Loading history...
169
                    $courseDescription
170
                        ->setTitle($title)
171
                        ->setProgress($progress)
172
                        ->setContent($content)
173
                    ;
174
                    $repo->update($courseDescription);
175
                } else {
176
                    $course_description->set_description_type($description_type);
177
                    $course_description->set_title($title);
178
                    $course_description->set_progress($progress);
179
                    $course_description->set_content($content);
180
                    $course_description->insert(api_get_course_int_id());
181
                }
182
183
                Display::addFlash(
184
                    Display::return_message(
185
                        get_lang('The description has been updated')
186
                    )
187
                );
188
189
                $url = api_get_path(WEB_CODE_PATH).'course_description/index.php?'.api_get_cidreq();
190
                api_location($url);
191
            }
192
        } else {
193
            $data['default_description_titles'] = $course_description->get_default_description_title();
194
            $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
195
            $data['default_description_icon'] = $course_description->get_default_description_icon();
196
            $data['question'] = $course_description->get_default_question();
197
            $data['information'] = $course_description->get_default_information();
198
            $data['description_type'] = $description_type;
199
            if (empty($id)) {
200
                // If the ID was not provided, find the first matching description item given the item type
201
                $description = $course_description->get_data_by_description_type($description_type);
202
                if (count($description) > 0) {
203
                    $id = $description['id'];
204
                }
205
                // If no corresponding description is found, edit a new one
206
            }
207
            if (!empty($id)) {
208
                if (isset($_GET['id_session'])) {
209
                    $session_id = intval($_GET['id_session']);
210
                }
211
                $course_description_data = $course_description->get_data_by_id(
212
                    $id,
213
                    null,
214
                    $session_id
215
                );
216
                $data['description_type'] = $course_description_data['description_type'];
217
                $data['description_title'] = $course_description_data['description_title'];
218
                $data['description_content'] = $course_description_data['description_content'];
219
                $data['progress'] = $course_description_data['progress'];
220
                $data['descriptions'] = $course_description->get_data_by_description_type(
221
                    $description_type,
222
                    null,
223
                    $session_id
224
                );
225
            }
226
227
            // render to the view
228
            $this->view->set_data($data);
229
            $this->view->set_layout('layout');
230
            $this->view->set_template('edit');
231
            $this->view->render();
232
        }
233
    }
234
235
    /**
236
     * It's used for adding a course description,
237
     * render to listing or add view.
238
     */
239
    public function add()
240
    {
241
        $course_description = new CourseDescription();
242
        $session_id = api_get_session_id();
243
        $course_description->set_session_id($session_id);
244
        $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

244
        /** @scrutinizer ignore-call */ 
245
        $actions = self::getToolbar();
Loading history...
245
246
        if ('POST' === strtoupper($_SERVER['REQUEST_METHOD'])) {
247
            if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
248
                $title = $_POST['title'];
249
                $content = $_POST['contentDescription'];
250
                $description_type = $_POST['description_type'];
251
                if ($description_type >= ADD_BLOCK) {
252
                    $course_description->set_description_type($description_type);
253
                    $course_description->set_title($title);
254
                    $course_description->set_content($content);
255
                    $course_description->insert(api_get_course_int_id());
256
                }
257
258
                Display::addFlash(
259
                    Display::return_message(
260
                        get_lang('The description has been added')
261
                    )
262
                );
263
                $url = api_get_path(WEB_CODE_PATH).'course_description/index.php?'.api_get_cidreq();
264
                api_location($url);
265
            }
266
        } else {
267
            // display form
268
            $form = new FormValidator(
269
                'course_description',
270
                'POST',
271
                'index.php?action=add&'.api_get_cidreq()
272
            );
273
            //$form->addElement('header', $header);
274
            $form->addElement('hidden', 'description_type', ADD_BLOCK);
275
            if (api_get_configuration_value('save_titles_as_html')) {
276
                $form->addHtmlEditor(
277
                    'title',
278
                    get_lang('Title'),
279
                    true,
280
                    false,
281
                    ['ToolbarSet' => 'TitleAsHtml']
282
                );
283
            } else {
284
                $form->addText('title', get_lang('Title'));
285
                $form->applyFilter('title', 'html_filter');
286
            }
287
            $form->addHtmlEditor(
288
                'contentDescription',
289
                get_lang('Content'),
290
                true,
291
                false,
292
                [
293
                    'ToolbarSet' => 'Basic',
294
                    'Width' => '100%',
295
                    'Height' => '200',
296
                ]
297
            );
298
            $form->addButtonCreate(get_lang('Save'));
299
300
            $tpl = new Template(get_lang('Description'));
301
            //$tpl->assign('is_allowed_to_edit', $is_allowed_to_edit);
302
            $tpl->assign('actions', $actions);
303
            $tpl->assign('session_id', $session_id);
304
            $tpl->assign('content', $form->returnForm());
305
            $tpl->display_one_col_template();
306
        }
307
    }
308
309
    /**
310
     * It's used for destroy a course description,
311
     * render to listing view.
312
     *
313
     * @param int $id description type
314
     */
315
    public function destroy($id)
316
    {
317
        $course_description = new CourseDescription();
318
        $session_id = api_get_session_id();
319
        $course_description->set_session_id($session_id);
320
        if (!empty($id)) {
321
            $course_description->set_id($id);
322
            $course_description->delete();
323
            Display::addFlash(
324
                Display::return_message(get_lang('Description has been deleted'))
325
            );
326
        }
327
328
        $url = api_get_path(WEB_CODE_PATH).'course_description/index.php?'.api_get_cidreq();
329
        api_location($url);
330
    }
331
}
332