Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/course_progress/thematic_controller.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use ChamiloSession as Session;
5
6
/**
7
 * Thematic Controller script.
8
 * Prepares the common background variables to give to the scripts corresponding to
9
 * the requested action.
10
 *
11
 * This file contains class used like controller for thematic,
12
 * it should be included inside a dispatcher file (e.g: index.php)
13
 *
14
 * !!! WARNING !!! : ALL DATES IN THIS MODULE ARE STORED IN UTC !
15
 * DO NOT CONVERT DURING THE TRANSITION FROM CHAMILO 1.8.x TO 2.0
16
 *
17
 * @author Christian Fasanando <[email protected]>
18
 * @author Julio Montoya <[email protected]> token support improving UI
19
 *
20
 * @package chamilo.course_progress
21
 */
22
class ThematicController
23
{
24
    /**
25
     * Constructor.
26
     */
27
    public function __construct()
28
    {
29
        $this->toolname = 'course_progress';
30
        $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

30
        $this->view = /** @scrutinizer ignore-deprecated */ new View($this->toolname);
Loading history...
31
    }
32
33
    /**
34
     * This method is used for thematic control (update, insert or listing).
35
     *
36
     * @param string $action
37
     *                       render to thematic.php
38
     */
39
    public function thematic($action)
40
    {
41
        $thematic = new Thematic();
42
        $data = [];
43
        $check = Security::check_token('request');
44
        $thematic_id = isset($_REQUEST['thematic_id']) ? intval($_REQUEST['thematic_id']) : null;
45
        $displayHeader = !empty($_REQUEST['display']) && $_REQUEST['display'] === 'no_header' ? false : true;
46
        $courseId = api_get_course_int_id();
47
48
        if ($check) {
49
            switch ($action) {
50
                case 'thematic_add':
51
                case 'thematic_edit':
52
                    // insert or update a thematic
53
                    if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
54
                        if (trim($_POST['title']) !== '') {
55
                            if (api_is_allowed_to_edit(null, true)) {
56
                                $id = isset($_POST['thematic_id']) ? $_POST['thematic_id'] : null;
57
                                $title = trim($_POST['title']);
58
                                $content = trim($_POST['content']);
59
                                $session_id = api_get_session_id();
60
                                $thematic->set_thematic_attributes($id, $title, $content, $session_id);
61
                                $last_id = $thematic->thematic_save();
62
                                if ($_POST['action'] == 'thematic_add') {
63
                                    $action = 'thematic_details';
64
                                    $thematic_id = null;
65
                                    if ($last_id) {
66
                                        $data['last_id'] = $last_id;
67
                                    }
68
                                } else {
69
                                    $action = 'thematic_details';
70
                                    $thematic_id = null;
71
                                }
72
                                Display::addFlash(Display::return_message(get_lang('Updated')));
73
                            }
74
                        } else {
75
                            $error = true;
76
                            $data['error'] = $error;
77
                            $data['action'] = $_POST['action'];
78
                            $data['thematic_id'] = $_POST['thematic_id'];
79
                            // render to the view
80
                            $this->view->set_data($data);
81
                            $this->view->set_layout('layout');
82
                            $this->view->set_template('thematic');
83
                            $this->view->render();
84
                        }
85
                    }
86
                    break;
87
                case 'thematic_copy':
88
                    // Copy a thematic to a session
89
                    $thematic->copy($thematic_id);
90
                    $thematic_id = null;
91
                    $action = 'thematic_details';
92
                    break;
93
                case 'thematic_delete_select':
94
                    // Delete many thematics
95
                    if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
96
                        if (api_is_allowed_to_edit(null, true)) {
97
                            $thematic_ids = $_POST['id'];
98
                            $thematic->delete($thematic_ids);
99
                            Display::addFlash(Display::return_message(get_lang('Deleted')));
100
                        }
101
                        $action = 'thematic_details';
102
                    }
103
                    break;
104
                case 'thematic_delete':
105
                    // Delete a thematic
106
                    if (isset($thematic_id)) {
107
                        if (api_is_allowed_to_edit(null, true)) {
108
                            $thematic->delete($thematic_id);
109
                            Display::addFlash(Display::return_message(get_lang('Deleted')));
110
                        }
111
                        $thematic_id = null;
112
                        $action = 'thematic_details';
113
                    }
114
                    break;
115
                case 'thematic_import_select':
116
                    break;
117
                case 'thematic_import':
118
                    $csv_import_array = Import::csv_reader($_FILES['file']['tmp_name'], false);
119
120
                    if (isset($_POST['replace']) && $_POST['replace']) {
121
                        // Remove current thematic.
122
                        $list = $thematic->get_thematic_list();
123
                        foreach ($list as $i) {
124
                            $thematic->delete($i);
125
                        }
126
                    }
127
128
                    // Import the progress.
129
                    $current_thematic = null;
130
                    foreach ($csv_import_array as $key => $item) {
131
                        if (!$key) {
132
                            continue;
133
                        }
134
135
                        switch ($item[0]) {
136
                            case 'title':
137
                                $thematic->set_thematic_attributes(
138
                                    null,
139
                                    $item[1],
140
                                    $item[2],
141
                                    api_get_session_id()
142
                                );
143
                                $current_thematic = $thematic->thematic_save();
144
                                $description_type = 1;
145
                                break;
146
                            case 'plan':
147
                                $thematic->set_thematic_plan_attributes(
148
                                    $current_thematic,
149
                                    $item[1],
150
                                    $item[2],
151
                                    $description_type
152
                                );
153
                                $thematic->thematic_plan_save();
154
                                $description_type++;
155
                                break;
156
                            case 'progress':
157
                                $thematic->set_thematic_advance_attributes(
158
                                    null,
159
                                    $current_thematic,
160
                                    0,
161
                                    $item[3],
162
                                    $item[1],
163
                                    $item[2]
164
                                );
165
                                $thematic->thematic_advance_save();
166
                                break;
167
                        }
168
                    }
169
170
                    $action = 'thematic_details';
171
                    break;
172
                case 'thematic_export':
173
                    $list = $thematic->get_thematic_list();
174
                    $csv = [];
175
                    $csv[] = ['type', 'data1', 'data2', 'data3'];
176
                    foreach ($list as $theme) {
177
                        $csv[] = ['title', strip_tags($theme['title']), strip_tags($theme['content'])];
178
                        $data = $thematic->get_thematic_plan_data($theme['id']);
179
                        if (!empty($data)) {
180
                            foreach ($data as $plan) {
181
                                if (empty($plan['description'])) {
182
                                    continue;
183
                                }
184
185
                                $csv[] = [
186
                                    'plan',
187
                                    strip_tags($plan['title']),
188
                                    strip_tags($plan['description']),
189
                                ];
190
                            }
191
                        }
192
                        $data = $thematic->get_thematic_advance_by_thematic_id($theme['id']);
193
                        if (!empty($data)) {
194
                            foreach ($data as $advance) {
195
                                $csv[] = [
196
                                    'progress',
197
                                    strip_tags($advance['start_date']),
198
                                    strip_tags($advance['duration']),
199
                                    strip_tags($advance['content']),
200
                                ];
201
                            }
202
                        }
203
                    }
204
                    Export::arrayToCsv($csv);
205
                    exit;
206
                    // Don't continue building a normal page.
207
                    return;
208
                case 'export_documents':
209
                case 'thematic_export_pdf':
210
                    $pdfOrientation = api_get_configuration_value('thematic_pdf_orientation');
211
212
                    $list = $thematic->get_thematic_list();
213
                    $item = [];
214
                    $listFinish = [];
215
                    foreach ($list as $theme) {
216
                        $dataPlan = $thematic->get_thematic_plan_data($theme['id']);
217
                        if (!empty($dataPlan)) {
218
                            foreach ($dataPlan as $plan) {
219
                                if (empty($plan['description'])) {
220
                                    continue;
221
                                }
222
                                $item[] = [
223
                                    'title' => $plan['title'],
224
                                    'description' => $plan['description'],
225
                                ];
226
                            }
227
                            $theme['thematic_plan'] = $item;
228
                        }
229
                        $dataAdvance = $thematic->get_thematic_advance_by_thematic_id($theme['id']);
230
                        if (!empty($dataAdvance)) {
231
                            $theme['thematic_advance'] = $dataAdvance;
232
                        }
233
                        $listFinish[] = $theme;
234
                    }
235
236
                    $view = new Template('', false, false, false, true, false, false);
237
                    $view->assign('data', $listFinish);
238
                    $template = $view->get_template('course_progress/pdf_general_thematic.tpl');
239
240
                    $format = $pdfOrientation !== 'portrait' ? 'A4-L' : 'A4-P';
241
                    $orientation = $pdfOrientation !== 'portrait' ? 'L' : 'P';
242
                    $fileName = get_lang('Thematic').'-'.api_get_local_time();
243
                    $title = get_lang('Thematic');
244
                    $signatures = ['Drh', 'Teacher', 'Date'];
245
246
                    if ($action === 'export_documents') {
247
                        $pdf = new PDF(
248
                            $format,
249
                            $orientation,
250
                            [
251
                                'filename' => $fileName,
252
                                'pdf_title' => $fileName,
253
                                'add_signatures' => $signatures,
254
                            ]
255
                        );
256
                        $pdf->exportFromHtmlToDocumentsArea($view->fetch($template), $fileName, $courseId);
257
258
                        header('Location: '.api_get_self().'?'.api_get_cidreq());
259
                        exit;
260
                    }
261
262
                    Export::export_html_to_pdf(
263
                        $view->fetch($template),
264
                        [
265
                            'filename' => $fileName,
266
                            'pdf_title' => $title,
267
                            'add_signatures' => $signatures,
268
                            'format' => $format,
269
                            'orientation' => $orientation,
270
                        ]
271
                    );
272
                    break;
273
                case 'export_single_documents':
274
                case 'export_single_thematic':
275
                    $theme = $thematic->get_thematic_list($thematic_id);
276
                    $plans = $thematic->get_thematic_plan_data($theme['id']);
277
                    $plans = array_filter(
278
                        $plans,
279
                        function ($plan) {
280
                            return !empty($plan['description']);
281
                        }
282
                    );
283
                    $advances = $thematic->get_thematic_advance_by_thematic_id($theme['id']);
284
285
                    $view = new Template('', false, false, false, true, false, false);
286
                    $view->assign('theme', $theme);
287
                    $view->assign('plans', $plans);
288
                    $view->assign('advances', $advances);
289
290
                    $template = $view->get_template('course_progress/pdf_single_thematic.tpl');
291
292
                    $pdfOrientation = api_get_configuration_value('thematic_pdf_orientation');
293
                    $format = $pdfOrientation !== 'portrait' ? 'A4-L' : 'A4-P';
294
                    $orientation = $pdfOrientation !== 'portrait' ? 'L' : 'P';
295
                    $title = get_lang('Thematic').'-'.$theme['title'];
296
                    $fileName = $title.'-'.api_get_local_time();
297
                    $signatures = ['Drh', 'Teacher', 'Date'];
298
299
                    if ($action === 'export_single_documents') {
300
                        $pdf = new PDF(
301
                            $format,
302
                            $orientation,
303
                            [
304
                                'filename' => $fileName,
305
                                'pdf_title' => $fileName,
306
                                'add_signatures' => $signatures,
307
                            ]
308
                        );
309
                        $pdf->exportFromHtmlToDocumentsArea(
310
                            $view->fetch($template),
311
                            $fileName,
312
                            $courseId
313
                        );
314
315
                        header('Location: '.api_get_self().'?'.api_get_cidreq());
316
                        exit;
317
                    }
318
319
                    Export::export_html_to_pdf(
320
                        $view->fetch($template),
321
                        [
322
                            'filename' => $fileName,
323
                            'pdf_title' => $title,
324
                            'add_signatures' => $signatures,
325
                            'format' => $format,
326
                            'orientation' => $orientation,
327
                        ]
328
                    );
329
                    break;
330
                case 'moveup':
331
                    $thematic->move_thematic('up', $thematic_id);
332
                    $action = 'thematic_details';
333
                    $thematic_id = null;
334
                    break;
335
                case 'movedown':
336
                    $thematic->move_thematic('down', $thematic_id);
337
                    $action = 'thematic_details';
338
                    $thematic_id = null;
339
                    break;
340
            }
341
            Security::clear_token();
342
        } else {
343
            $action = 'thematic_details';
344
            $thematic_id = null;
345
        }
346
        if (isset($thematic_id)) {
347
            $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
348
            $data['thematic_id'] = $thematic_id;
349
        }
350
351
        if ($action == 'thematic_details') {
352
            if (isset($thematic_id)) {
353
                $thematic_data_result = $thematic->get_thematic_list($thematic_id);
354
                if (!empty($thematic_data_result)) {
355
                    $thematic_data[$thematic_id] = $thematic_data_result;
356
                }
357
                $data['total_average_of_advances'] = $thematic->get_average_of_advances_by_thematic($thematic_id);
358
            } else {
359
                $thematic_data = $thematic->get_thematic_list(null, api_get_course_id(), api_get_session_id());
360
                $data['max_thematic_item'] = $thematic->get_max_thematic_item();
361
                $data['last_done_thematic_advance'] = $thematic->get_last_done_thematic_advance();
362
                $data['total_average_of_advances'] = $thematic->get_total_average_of_thematic_advances();
363
            }
364
365
            // Second column
366
            $thematic_plan_data = $thematic->get_thematic_plan_data();
367
368
            // Third column
369
            $thematic_advance_data = $thematic->get_thematic_advance_list(null, null, true, true);
370
371
            $data['thematic_plan_div'] = $thematic->get_thematic_plan_array($thematic_plan_data);
372
            $data['thematic_advance_div'] = $thematic->get_thematic_advance_div($thematic_advance_data);
373
            $data['thematic_plan_data'] = $thematic_plan_data;
374
            $data['thematic_advance_data'] = $thematic_advance_data;
375
            $data['thematic_data'] = $thematic_data;
376
        }
377
378
        $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
379
380
        $data['action'] = $action;
381
        $layoutName = $displayHeader ? 'layout' : 'layout_no_header';
382
383
        // render to the view
384
        $this->view->set_data($data);
385
        $this->view->set_layout($layoutName);
386
        $this->view->set_template('thematic');
387
        $this->view->render();
388
    }
389
390
    /**
391
     * This method is used for thematic plan control (update, insert or listing).
392
     *
393
     * @param string $action
394
     *                       render to thematic_plan.php
395
     */
396
    public function thematic_plan($action)
397
    {
398
        $thematic = new Thematic();
399
        $data = [];
400
        if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
401
            if (isset($_POST['action']) &&
402
                ($_POST['action'] == 'thematic_plan_add' || $_POST['action'] == 'thematic_plan_edit')
403
            ) {
404
                if (isset($_POST['title'])) {
405
                    $token = Session::read('thematic_plan_token');
406
                    if ($_POST['thematic_plan_token'] == $token) {
407
                        if (api_is_allowed_to_edit(null, true)) {
408
                            $title_list = $_REQUEST['title'];
409
                            $description_list = $_REQUEST['description'];
410
                            $description_type = $_REQUEST['description_type'];
411
                            for ($i = 1; $i < count($title_list) + 1; $i++) {
412
                                $thematic->set_thematic_plan_attributes(
413
                                    $_REQUEST['thematic_id'],
414
                                    $title_list[$i],
415
                                    $description_list[$i],
416
                                    $description_type[$i]
417
                                );
418
                                $thematic->thematic_plan_save();
419
                            }
420
421
                            $saveRedirect = api_get_path(WEB_PATH).'main/course_progress/index.php?';
422
                            $saveRedirect .= api_get_cidreq().'&';
423
424
                            if (isset($_REQUEST['add_item'])) {
425
                                $thematic->set_thematic_plan_attributes(
426
                                    $_REQUEST['thematic_id'],
427
                                    '',
428
                                    '',
429
                                    $i
430
                                );
431
                                $thematic->thematic_plan_save();
432
433
                                $saveRedirect .= http_build_query([
434
                                    'action' => 'thematic_plan_list',
435
                                    'thematic_id' => $_REQUEST['thematic_id'],
436
                                ]);
437
                            } else {
438
                                $saveRedirect .= 'thematic_plan_save_message=ok';
439
                                Session::erase('thematic_plan_token');
440
                                $data['message'] = 'ok';
441
                            }
442
443
                            header("Location: $saveRedirect");
444
                            exit;
445
                        }
446
                        $data['action'] = 'thematic_plan_list';
447
                    }
448
                } else {
449
                    $error = true;
450
                    $action = $_POST['action'];
451
                    $data['error'] = $error;
452
                    $data['thematic_plan_data'] = $thematic->get_thematic_plan_data(
453
                        $_POST['thematic_id'],
454
                        $_POST['description_type']
455
                    );
456
                    $data['thematic_id'] = $_POST['thematic_id'];
457
                    $data['description_type'] = $_POST['description_type'];
458
                    $data['action'] = $action;
459
                    $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
460
                    $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
461
                    $data['default_thematic_plan_question'] = $thematic->get_default_question();
462
                    $data['next_description_type'] = $thematic->get_next_description_type($_POST['thematic_id']);
463
                    // render to the view
464
                    $this->view->set_data($data);
465
                    $this->view->set_layout('layout');
466
                    $this->view->set_template('thematic_plan');
467
                    $this->view->render();
468
                }
469
            }
470
        }
471
472
        $thematic_id = intval($_GET['thematic_id']);
473
        if ($action == 'thematic_plan_list') {
474
            $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
475
        }
476
477
        $description_type = isset($_GET['description_type']) ? intval($_GET['description_type']) : null;
478
        if (!empty($thematic_id) && !empty($description_type)) {
479
            if ($action === 'thematic_plan_delete') {
480
                if (api_is_allowed_to_edit(null, true)) {
481
                    $thematic->thematic_plan_destroy(
482
                        $thematic_id,
483
                        $description_type
484
                    );
485
                }
486
                $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
487
                $action = 'thematic_plan_list';
488
            } else {
489
                $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id, $description_type);
490
            }
491
            $data['thematic_id'] = $thematic_id;
492
            $data['description_type'] = $description_type;
493
        } elseif (!empty($thematic_id) && $action === 'thematic_plan_list') {
494
            $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
495
            $data['thematic_id'] = $thematic_id;
496
        }
497
498
        $data['thematic_id'] = $thematic_id;
499
        $data['action'] = $action;
500
        $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
501
        $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
502
        $data['next_description_type'] = $thematic->get_next_description_type($thematic_id);
503
        $data['default_thematic_plan_question'] = $thematic->get_default_question();
504
        $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
505
506
        // render to the view
507
        $this->view->set_data($data);
508
        $this->view->set_layout('layout');
509
        $this->view->set_template('thematic_plan');
510
        $this->view->render();
511
        exit;
512
    }
513
514
    /**
515
     * This method is used for thematic advance control (update, insert or listing)
516
     * render to thematic_advance.php.
517
     *
518
     * @param string $action
519
     */
520
    public function thematic_advance($action)
521
    {
522
        $thematic = new Thematic();
523
        $attendance = new Attendance();
524
        $data = [];
525
        $displayHeader = !empty($_REQUEST['display']) && $_REQUEST['display'] === 'no_header' ? false : true;
526
527
        // get data for attendance input select
528
        $attendance_list = $attendance->get_attendances_list();
529
        $attendance_select = [];
530
        $attendance_select[0] = get_lang('SelectAnAttendance');
531
        foreach ($attendance_list as $attendance_id => $attendance_data) {
532
            $attendance_select[$attendance_id] = $attendance_data['name'];
533
        }
534
535
        $thematic_id = intval($_REQUEST['thematic_id']);
536
        $thematic_advance_id = isset($_REQUEST['thematic_advance_id']) ? (int) $_REQUEST['thematic_advance_id'] : null;
537
        $thematic_advance_data = [];
538
        switch ($action) {
539
            case 'thematic_advance_delete':
540
                if (!empty($thematic_advance_id)) {
541
                    if (api_is_allowed_to_edit(null, true)) {
542
                        $thematic->thematic_advance_destroy($thematic_advance_id);
543
                    }
544
                    Display::addFlash(Display::return_message(get_lang('Deleted')));
545
                    header('Location: index.php');
546
                    exit;
547
                }
548
                break;
549
            case 'thematic_advance_list':
550
                if (!api_is_allowed_to_edit(null, true)) {
551
                    echo '';
552
                    exit;
553
                }
554
555
                $data['action'] = $_REQUEST['action'];
556
                $data['thematic_id'] = $_REQUEST['thematic_id'];
557
                $data['attendance_select'] = $attendance_select;
558
                if (isset($_REQUEST['thematic_advance_id'])) {
559
                    $data['thematic_advance_id'] = $_REQUEST['thematic_advance_id'];
560
                    $thematic_advance_data = $thematic->get_thematic_advance_list($_REQUEST['thematic_advance_id']);
561
                    $data['thematic_advance_data'] = $thematic_advance_data;
562
                }
563
                break;
564
            default:
565
                $thematic_advance_data = $thematic->get_thematic_advance_list($thematic_advance_id);
566
                break;
567
        }
568
569
        // get calendar select by attendance id
570
        $calendar_select = [];
571
        if (!empty($thematic_advance_data)) {
572
            if (!empty($thematic_advance_data['attendance_id'])) {
573
                $attendance_calendar = $attendance->get_attendance_calendar($thematic_advance_data['attendance_id']);
574
                if (!empty($attendance_calendar)) {
575
                    foreach ($attendance_calendar as $calendar) {
576
                        $calendar_select[$calendar['date_time']] = $calendar['date_time'];
577
                    }
578
                }
579
            }
580
        }
581
582
        $data['action'] = $action;
583
        $data['thematic_id'] = $thematic_id;
584
        $data['thematic_advance_id'] = $thematic_advance_id;
585
        $data['attendance_select'] = $attendance_select;
586
        $data['thematic_advance_data'] = $thematic_advance_data;
587
        $data['calendar_select'] = $calendar_select;
588
        $layoutName = $displayHeader ? 'layout' : 'layout_no_header';
589
590
        // render to the view
591
        $this->view->set_data($data);
592
        $this->view->set_layout($layoutName);
593
        $this->view->set_template('thematic_advance');
594
        $this->view->render();
595
    }
596
}
597