Issues (2037)

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['type']) {
136
                            case 'title':
137
                                $thematic->set_thematic_attributes(
138
                                    null,
139
                                    $item['data1'],
140
                                    $item['data2'],
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['data1'],
150
                                    $item['data2'],
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['data3'],
162
                                    $item['data1'],
163
                                    $item['data2']
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
                    if ($list === false) {
214
                        header('Location: '.api_get_self().'?'.api_get_cidreq());
215
                        exit;
216
                    }
217
                    $item = [];
218
                    $listFinish = [];
219
                    foreach ($list as $theme) {
220
                        $dataPlan = $thematic->get_thematic_plan_data($theme['id']);
221
                        if (!empty($dataPlan)) {
222
                            foreach ($dataPlan as $plan) {
223
                                if (empty($plan['description'])) {
224
                                    continue;
225
                                }
226
                                $item[] = [
227
                                    'title' => $plan['title'],
228
                                    'description' => $plan['description'],
229
                                ];
230
                            }
231
                            $theme['thematic_plan'] = $item;
232
                        }
233
                        $dataAdvance = $thematic->get_thematic_advance_by_thematic_id($theme['id']);
234
                        if (!empty($dataAdvance)) {
235
                            $theme['thematic_advance'] = $dataAdvance;
236
                        }
237
                        $listFinish[] = $theme;
238
                    }
239
240
                    $view = new Template('', false, false, false, true, false, false);
241
                    $view->assign('data', $listFinish);
242
                    $template = $view->get_template('course_progress/pdf_general_thematic.tpl');
243
244
                    $format = $pdfOrientation !== 'portrait' ? 'A4-L' : 'A4-P';
245
                    $orientation = $pdfOrientation !== 'portrait' ? 'L' : 'P';
246
                    $fileName = get_lang('Thematic').'-'.api_get_local_time();
247
                    $title = get_lang('Thematic');
248
                    $signatures = ['Drh', 'Teacher', 'Date'];
249
250
                    if ($action === 'export_documents') {
251
                        $pdf = new PDF(
252
                            $format,
253
                            $orientation,
254
                            [
255
                                'filename' => $fileName,
256
                                'pdf_title' => $fileName,
257
                                'add_signatures' => $signatures,
258
                            ]
259
                        );
260
                        $pdf->exportFromHtmlToDocumentsArea($view->fetch($template), $fileName, $courseId);
261
262
                        header('Location: '.api_get_self().'?'.api_get_cidreq());
263
                        exit;
264
                    }
265
266
                    Export::export_html_to_pdf(
267
                        $view->fetch($template),
268
                        [
269
                            'filename' => $fileName,
270
                            'pdf_title' => $title,
271
                            'add_signatures' => $signatures,
272
                            'format' => $format,
273
                            'orientation' => $orientation,
274
                        ]
275
                    );
276
                    break;
277
                case 'export_single_documents':
278
                case 'export_single_thematic':
279
                    $theme = $thematic->get_thematic_list($thematic_id);
280
                    $plans = $thematic->get_thematic_plan_data($theme['id']);
281
                    $plans = array_filter(
282
                        $plans,
283
                        function ($plan) {
284
                            return !empty($plan['description']);
285
                        }
286
                    );
287
                    $advances = $thematic->get_thematic_advance_by_thematic_id($theme['id']);
288
289
                    $view = new Template('', false, false, false, true, false, false);
290
                    $view->assign('theme', $theme);
291
                    $view->assign('plans', $plans);
292
                    $view->assign('advances', $advances);
293
294
                    $template = $view->get_template('course_progress/pdf_single_thematic.tpl');
295
296
                    $pdfOrientation = api_get_configuration_value('thematic_pdf_orientation');
297
                    $format = $pdfOrientation !== 'portrait' ? 'A4-L' : 'A4-P';
298
                    $orientation = $pdfOrientation !== 'portrait' ? 'L' : 'P';
299
                    $title = get_lang('Thematic').'-'.$theme['title'];
300
                    $fileName = $title.'-'.api_get_local_time();
301
                    $signatures = ['Drh', 'Teacher', 'Date'];
302
303
                    if ($action === 'export_single_documents') {
304
                        $pdf = new PDF(
305
                            $format,
306
                            $orientation,
307
                            [
308
                                'filename' => $fileName,
309
                                'pdf_title' => $fileName,
310
                                'add_signatures' => $signatures,
311
                            ]
312
                        );
313
                        $pdf->exportFromHtmlToDocumentsArea(
314
                            $view->fetch($template),
315
                            $fileName,
316
                            $courseId
317
                        );
318
319
                        header('Location: '.api_get_self().'?'.api_get_cidreq());
320
                        exit;
321
                    }
322
323
                    Export::export_html_to_pdf(
324
                        $view->fetch($template),
325
                        [
326
                            'filename' => $fileName,
327
                            'pdf_title' => $title,
328
                            'add_signatures' => $signatures,
329
                            'format' => $format,
330
                            'orientation' => $orientation,
331
                        ]
332
                    );
333
                    break;
334
                case 'moveup':
335
                    $thematic->move_thematic('up', $thematic_id);
336
                    $action = 'thematic_details';
337
                    $thematic_id = null;
338
                    break;
339
                case 'movedown':
340
                    $thematic->move_thematic('down', $thematic_id);
341
                    $action = 'thematic_details';
342
                    $thematic_id = null;
343
                    break;
344
            }
345
            Security::clear_token();
346
        } else {
347
            $action = 'thematic_details';
348
            $thematic_id = null;
349
        }
350
        if (isset($thematic_id)) {
351
            $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
352
            $data['thematic_id'] = $thematic_id;
353
        }
354
355
        if ($action == 'thematic_details') {
356
            if (isset($thematic_id)) {
357
                $thematic_data_result = $thematic->get_thematic_list($thematic_id);
358
                if (!empty($thematic_data_result)) {
359
                    $thematic_data[$thematic_id] = $thematic_data_result;
360
                }
361
                $data['total_average_of_advances'] = $thematic->get_average_of_advances_by_thematic($thematic_id);
362
            } else {
363
                $thematic_data = $thematic->get_thematic_list(null, api_get_course_id(), api_get_session_id());
364
                $data['max_thematic_item'] = $thematic->get_max_thematic_item();
365
                $data['last_done_thematic_advance'] = $thematic->get_last_done_thematic_advance();
366
                $data['total_average_of_advances'] = $thematic->get_total_average_of_thematic_advances();
367
            }
368
369
            // Second column
370
            $thematic_plan_data = $thematic->get_thematic_plan_data();
371
372
            // Third column
373
            $thematic_advance_data = $thematic->get_thematic_advance_list(null, null, true, true);
374
375
            $data['thematic_plan_div'] = $thematic->get_thematic_plan_array($thematic_plan_data);
376
            $data['thematic_advance_div'] = $thematic->get_thematic_advance_div($thematic_advance_data);
377
            $data['thematic_plan_data'] = $thematic_plan_data;
378
            $data['thematic_advance_data'] = $thematic_advance_data;
379
            $data['thematic_data'] = $thematic_data;
380
        }
381
382
        $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
383
384
        $data['action'] = $action;
385
        $layoutName = $displayHeader ? 'layout' : 'layout_no_header';
386
387
        // render to the view
388
        $this->view->set_data($data);
389
        $this->view->set_layout($layoutName);
390
        $this->view->set_template('thematic');
391
        $this->view->render();
392
    }
393
394
    /**
395
     * This method is used for thematic plan control (update, insert or listing).
396
     *
397
     * @param string $action
398
     *                       render to thematic_plan.php
399
     */
400
    public function thematic_plan($action)
401
    {
402
        $thematic = new Thematic();
403
        $data = [];
404
        if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
405
            if (isset($_POST['action']) &&
406
                ($_POST['action'] == 'thematic_plan_add' || $_POST['action'] == 'thematic_plan_edit')
407
            ) {
408
                if (isset($_POST['title'])) {
409
                    $token = Session::read('thematic_plan_token');
410
                    if ($_POST['thematic_plan_token'] == $token) {
411
                        if (api_is_allowed_to_edit(null, true)) {
412
                            $title_list = $_REQUEST['title'];
413
                            $description_list = $_REQUEST['description'];
414
                            $description_type = $_REQUEST['description_type'];
415
                            for ($i = 1; $i < count($title_list) + 1; $i++) {
416
                                $thematic->set_thematic_plan_attributes(
417
                                    $_REQUEST['thematic_id'],
418
                                    $title_list[$i],
419
                                    $description_list[$i],
420
                                    $description_type[$i]
421
                                );
422
                                $thematic->thematic_plan_save();
423
                            }
424
425
                            $saveRedirect = api_get_path(WEB_PATH).'main/course_progress/index.php?';
426
                            $saveRedirect .= api_get_cidreq().'&';
427
428
                            if (isset($_REQUEST['add_item'])) {
429
                                $thematic->set_thematic_plan_attributes(
430
                                    $_REQUEST['thematic_id'],
431
                                    '',
432
                                    '',
433
                                    $i
434
                                );
435
                                $thematic->thematic_plan_save();
436
437
                                $saveRedirect .= http_build_query([
438
                                    'action' => 'thematic_plan_list',
439
                                    'thematic_id' => $_REQUEST['thematic_id'],
440
                                ]);
441
                            } else {
442
                                $saveRedirect .= 'thematic_plan_save_message=ok';
443
                                Session::erase('thematic_plan_token');
444
                                $data['message'] = 'ok';
445
                            }
446
447
                            header("Location: $saveRedirect");
448
                            exit;
449
                        }
450
                        $data['action'] = 'thematic_plan_list';
451
                    }
452
                } else {
453
                    $error = true;
454
                    $action = $_POST['action'];
455
                    $data['error'] = $error;
456
                    $data['thematic_plan_data'] = $thematic->get_thematic_plan_data(
457
                        $_POST['thematic_id'],
458
                        $_POST['description_type']
459
                    );
460
                    $data['thematic_id'] = $_POST['thematic_id'];
461
                    $data['description_type'] = $_POST['description_type'];
462
                    $data['action'] = $action;
463
                    $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
464
                    $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
465
                    $data['default_thematic_plan_question'] = $thematic->get_default_question();
466
                    $data['next_description_type'] = $thematic->get_next_description_type($_POST['thematic_id']);
467
                    // render to the view
468
                    $this->view->set_data($data);
469
                    $this->view->set_layout('layout');
470
                    $this->view->set_template('thematic_plan');
471
                    $this->view->render();
472
                }
473
            }
474
        }
475
476
        $thematic_id = intval($_GET['thematic_id']);
477
        if ($action == 'thematic_plan_list') {
478
            $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
479
        }
480
481
        $description_type = isset($_GET['description_type']) ? intval($_GET['description_type']) : null;
482
        if (!empty($thematic_id) && !empty($description_type)) {
483
            if ($action === 'thematic_plan_delete') {
484
                if (api_is_allowed_to_edit(null, true)) {
485
                    $thematic->thematic_plan_destroy(
486
                        $thematic_id,
487
                        $description_type
488
                    );
489
                }
490
                $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
491
                $action = 'thematic_plan_list';
492
            } else {
493
                $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id, $description_type);
494
            }
495
            $data['thematic_id'] = $thematic_id;
496
            $data['description_type'] = $description_type;
497
        } elseif (!empty($thematic_id) && $action === 'thematic_plan_list') {
498
            $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
499
            $data['thematic_id'] = $thematic_id;
500
        }
501
502
        $data['thematic_id'] = $thematic_id;
503
        $data['action'] = $action;
504
        $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
505
        $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
506
        $data['next_description_type'] = $thematic->get_next_description_type($thematic_id);
507
        $data['default_thematic_plan_question'] = $thematic->get_default_question();
508
        $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
509
510
        // render to the view
511
        $this->view->set_data($data);
512
        $this->view->set_layout('layout');
513
        $this->view->set_template('thematic_plan');
514
        $this->view->render();
515
        exit;
516
    }
517
518
    /**
519
     * This method is used for thematic advance control (update, insert or listing)
520
     * render to thematic_advance.php.
521
     *
522
     * @param string $action
523
     */
524
    public function thematic_advance($action)
525
    {
526
        $thematic = new Thematic();
527
        $attendance = new Attendance();
528
        $data = [];
529
        $displayHeader = !empty($_REQUEST['display']) && $_REQUEST['display'] === 'no_header' ? false : true;
530
531
        // get data for attendance input select
532
        $attendance_list = $attendance->get_attendances_list();
533
        $attendance_select = [];
534
        $attendance_select[0] = get_lang('SelectAnAttendance');
535
        foreach ($attendance_list as $attendance_id => $attendance_data) {
536
            $attendance_select[$attendance_id] = $attendance_data['name'];
537
        }
538
539
        $thematic_id = intval($_REQUEST['thematic_id']);
540
        $thematic_advance_id = isset($_REQUEST['thematic_advance_id']) ? (int) $_REQUEST['thematic_advance_id'] : null;
541
        $thematic_advance_data = [];
542
        switch ($action) {
543
            case 'thematic_advance_delete':
544
                if (!empty($thematic_advance_id)) {
545
                    if (api_is_allowed_to_edit(null, true)) {
546
                        $thematic->thematic_advance_destroy($thematic_advance_id);
547
                    }
548
                    Display::addFlash(Display::return_message(get_lang('Deleted')));
549
                    header('Location: index.php');
550
                    exit;
551
                }
552
                break;
553
            case 'thematic_advance_list':
554
                if (!api_is_allowed_to_edit(null, true)) {
555
                    echo '';
556
                    exit;
557
                }
558
559
                $data['action'] = $_REQUEST['action'];
560
                $data['thematic_id'] = $_REQUEST['thematic_id'];
561
                $data['attendance_select'] = $attendance_select;
562
                if (isset($_REQUEST['thematic_advance_id'])) {
563
                    $data['thematic_advance_id'] = $_REQUEST['thematic_advance_id'];
564
                    $thematic_advance_data = $thematic->get_thematic_advance_list($_REQUEST['thematic_advance_id']);
565
                    $data['thematic_advance_data'] = $thematic_advance_data;
566
                }
567
                break;
568
            default:
569
                $thematic_advance_data = $thematic->get_thematic_advance_list($thematic_advance_id);
570
                break;
571
        }
572
573
        // get calendar select by attendance id
574
        $calendar_select = [];
575
        if (!empty($thematic_advance_data)) {
576
            if (!empty($thematic_advance_data['attendance_id'])) {
577
                $attendance_calendar = $attendance->get_attendance_calendar($thematic_advance_data['attendance_id']);
578
                if (!empty($attendance_calendar)) {
579
                    foreach ($attendance_calendar as $calendar) {
580
                        $calendar_select[$calendar['date_time']] = $calendar['date_time'];
581
                    }
582
                }
583
            }
584
        }
585
586
        $data['action'] = $action;
587
        $data['thematic_id'] = $thematic_id;
588
        $data['thematic_advance_id'] = $thematic_advance_id;
589
        $data['attendance_select'] = $attendance_select;
590
        $data['thematic_advance_data'] = $thematic_advance_data;
591
        $data['calendar_select'] = $calendar_select;
592
        $layoutName = $displayHeader ? 'layout' : 'layout_no_header';
593
594
        // render to the view
595
        $this->view->set_data($data);
596
        $this->view->set_layout($layoutName);
597
        $this->view->set_template('thematic_advance');
598
        $this->view->render();
599
    }
600
}
601