Issues (2037)

course_description_controller.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class CourseDescriptionController
6
 * This file contains class used like controller,
7
 * it should be included inside a dispatcher file (e.g: index.php).
8
 *
9
 * @author Christian Fasanando <[email protected]>
10
 */
11
class CourseDescriptionController
12
{
13
    private $toolname;
14
    private $view;
15
16
    /**
17
     * Constructor.
18
     */
19
    public function __construct()
20
    {
21
        $this->toolname = 'course_description';
22
        $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

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