Passed
Push — master ( eba669...1a8a38 )
by Julito
10:56
created

AttendanceController::attendance_edit()   B

Complexity

Conditions 9
Paths 19

Size

Total Lines 64
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 46
nc 19
nop 1
dl 0
loc 64
rs 7.6226
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * This file contains class used like controller,
6
 * it should be included inside a dispatcher file (e.g: index.php).
7
 *
8
 * !!! WARNING !!! : ALL DATES IN THIS MODULE ARE STORED IN UTC !
9
 * DO NOT CONVERT DURING THE TRANSITION FROM CHAMILO 1.8.x TO 2.0
10
 *
11
 * @author Christian Fasanando <[email protected]>
12
 * @author Julio Montoya <[email protected]> lot of bugfixes + improvements
13
 *
14
 * @package chamilo.attendance
15
 */
16
class AttendanceController
17
{
18
    /**
19
     * Constructor.
20
     */
21
    public function __construct()
22
    {
23
        $this->toolname = 'attendance';
24
        $this->view = new View($this->toolname);
25
    }
26
27
    /**
28
     * It's used for listing attendance,
29
     * render to attendance_list view.
30
     */
31
    public function attendance_list()
32
    {
33
        // render to the view
34
        $this->view->set_data([]);
35
        $this->view->set_layout('layout');
36
        $this->view->set_template('attendance_list');
37
        $this->view->render();
38
    }
39
40
    /**
41
     * It's used for adding attendace,
42
     * render to attendance_add or attendance_list view.
43
     */
44
    public function attendance_add()
45
    {
46
        $attendance = new Attendance();
47
        $data = [];
48
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
49
            if (!empty($_POST['title'])) {
50
                $check = Security::check_token();
51
                $attendanceId = 0;
52
                if ($check) {
53
                    $attendance->set_name($_POST['title']);
54
                    $attendance->set_description($_POST['description']);
55
                    $attendance->set_attendance_qualify_title($_POST['attendance_qualify_title']);
56
                    $attendance->set_attendance_weight($_POST['attendance_weight']);
57
                    $link_to_gradebook = false;
58
                    if (isset($_POST['attendance_qualify_gradebook']) &&
59
                        $_POST['attendance_qualify_gradebook'] == 1
60
                    ) {
61
                        $link_to_gradebook = true;
62
                    }
63
                    $attendance->category_id = isset($_POST['category_id']) ? $_POST['category_id'] : 0;
64
                    $attendanceId = $attendance->attendance_add($link_to_gradebook);
65
66
                    if ($attendanceId) {
67
                        $form = new FormValidator('attendance_add');
68
                        Skill::saveSkills($form, ITEM_TYPE_ATTENDANCE, $attendanceId);
69
                    }
70
                    Security::clear_token();
71
                }
72
                header('Location: index.php?action=calendar_add&attendance_id='.$attendanceId.'&'.api_get_cidreq());
73
                exit;
74
            } else {
75
                $data['error'] = true;
76
                $this->view->set_data($data);
77
                $this->view->set_layout('layout');
78
                $this->view->set_template('attendance_add');
79
                $this->view->render();
80
            }
81
        } else {
82
            $this->view->set_data($data);
83
            $this->view->set_layout('layout');
84
            $this->view->set_template('attendance_add');
85
            $this->view->render();
86
        }
87
    }
88
89
    /**
90
     * It's used for editing attendance,
91
     * render to attendance_edit or attendance_list view.
92
     *
93
     * @param int $attendance_id
94
     */
95
    public function attendance_edit($attendance_id)
96
    {
97
        $attendance = new Attendance();
98
        $data = [];
99
        $attendance_id = intval($attendance_id);
100
101
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
102
            if (!empty($_POST['title'])) {
103
                $check = Security::check_token();
104
                if ($check) {
105
                    $attendance->set_name($_POST['title']);
106
                    $attendance->set_description($_POST['description']);
107
                    if (isset($_POST['attendance_qualify_title'])) {
108
                        $attendance->set_attendance_qualify_title(
109
                            $_POST['attendance_qualify_title']
110
                        );
111
                    }
112
113
                    if (isset($_POST['attendance_weight'])) {
114
                        $attendance->set_attendance_weight(
115
                            $_POST['attendance_weight']
116
                        );
117
                    }
118
119
                    $attendance->category_id = isset($_POST['category_id']) ? $_POST['category_id'] : '';
120
                    $link_to_gradebook = false;
121
                    if (isset($_POST['attendance_qualify_gradebook']) &&
122
                        $_POST['attendance_qualify_gradebook'] == 1
123
                    ) {
124
                        $link_to_gradebook = true;
125
                    }
126
                    $attendance->attendance_edit($attendance_id, $link_to_gradebook);
127
128
                    $form = new FormValidator('attendance_edit');
129
                    Skill::saveSkills($form, ITEM_TYPE_ATTENDANCE, $attendance_id);
130
                    Display::addFlash(Display::return_message(get_lang('Updated')));
131
132
                    Security::clear_token();
133
                    header('Location:index.php?action=attendance_list&'.api_get_cidreq());
134
                    exit;
135
                }
136
            } else {
137
                $data['attendance_id'] = $_POST['attendance_id'];
138
                $data['error'] = true;
139
                $this->view->set_data($data);
140
                $this->view->set_layout('layout');
141
                $this->view->set_template('attendance_edit');
142
                $this->view->render();
143
            }
144
        } else {
145
            // default values
146
            $attendance_data = $attendance->get_attendance_by_id(
147
                $attendance_id
148
            );
149
            $data['attendance_id'] = $attendance_data['id'];
150
            $data['title'] = $attendance_data['name'];
151
            $data['description'] = $attendance_data['description'];
152
            $data['attendance_qualify_title'] = $attendance_data['attendance_qualify_title'];
153
            $data['attendance_weight'] = $attendance_data['attendance_weight'];
154
155
            $this->view->set_data($data);
156
            $this->view->set_layout('layout');
157
            $this->view->set_template('attendance_edit');
158
            $this->view->render();
159
        }
160
    }
161
162
    /**
163
     * It's used for delete attendaces
164
     * render to attendance_list view.
165
     *
166
     * @param int $attendance_id
167
     *
168
     * @return bool
169
     */
170
    public function attendance_delete($attendance_id)
171
    {
172
        $allowDeleteAttendance = api_get_setting('allow_delete_attendance');
173
        if ($allowDeleteAttendance !== 'true') {
174
            $this->attendance_list();
175
176
            return false;
177
        }
178
179
        $attendance = new Attendance();
180
        if (!empty($attendance_id)) {
181
            $affected_rows = $attendance->attendance_delete($attendance_id);
182
            Skill::deleteSkillsFromItem($attendance_id, ITEM_TYPE_ATTENDANCE);
183
        }
184
185
        if ($affected_rows) {
186
            $message['message_attendance_delete'] = true;
187
        }
188
        $this->attendance_list();
189
190
        return true;
191
    }
192
193
    /**
194
     * It's used for make attendance visible
195
     * render to attendance_list view.
196
     *
197
     * @param int $attendanceId
198
     */
199
    public function attendanceSetVisible($attendanceId)
200
    {
201
        $attendance = new Attendance();
202
        $affectedRows = null;
203
        if (!empty($attendanceId)) {
204
            $affectedRows = $attendance->changeVisibility($attendanceId, 1);
205
        }
206
        if ($affectedRows) {
207
            $message['message_attendance_delete'] = true;
208
        }
209
        $this->attendance_list();
210
    }
211
212
    /**
213
     * It's used for make attendance invisible
214
     * render to attendance_list view.
215
     *
216
     * @param int $attendanceId
217
     */
218
    public function attendanceSetInvisible($attendanceId)
219
    {
220
        $attendance = new Attendance();
221
        if (!empty($attendanceId)) {
222
            $affectedRows = $attendance->changeVisibility($attendanceId, 0);
223
        }
224
        if ($affectedRows) {
225
            $message['message_attendance_delete'] = true;
226
        }
227
        $this->attendance_list();
228
    }
229
230
    /**
231
     * Restores an attendance entry and fallback to attendances rendering.
232
     *
233
     * @param int $attendance_id
234
     */
235
    public function attendance_restore($attendance_id)
236
    {
237
        $attendance = new Attendance();
238
        $affected_rows = false;
239
        if (!empty($attendance_id)) {
240
            $affected_rows = $attendance->attendance_restore($attendance_id);
241
        }
242
        if ($affected_rows) {
243
            $message['message_attendance_restore'] = true;
244
        }
245
        $this->attendance_list();
246
    }
247
248
    /**
249
     * Lock or unlock an attendance
250
     * render to attendance_list view.
251
     *
252
     * @param string $action        (lock_attendance or unlock_attendance)
253
     * @param int    $attendance_id
254
     *                              render to attendance_list view
255
     */
256
    public function lock_attendance($action, $attendance_id)
257
    {
258
        $attendance = new Attendance();
259
        $attendance_id = intval($attendance_id);
260
261
        if ($action == 'lock_attendance') {
262
            $result = $attendance->lock_attendance($attendance_id);
263
        } else {
264
            $result = $attendance->lock_attendance($attendance_id, false);
265
        }
266
        if ($result) {
267
            $message['message_locked_attendance'] = true;
268
        }
269
        $this->attendance_list();
270
    }
271
272
    public function export($id, $type = 'pdf')
273
    {
274
        $attendance = new Attendance();
275
    }
276
277
    /**
278
     * It's used for controlling attendance sheet (list, add),
279
     * render to attendance_sheet view.
280
     *
281
     * @param string $action
282
     * @param int    $attendance_id
283
     * @param int    $student_id
284
     * @param bool   $edit
285
     */
286
    public function attendance_sheet(
287
        $action,
288
        $attendance_id,
289
        $student_id = 0,
290
        $edit = true
291
    ) {
292
        $attendance = new Attendance();
293
        $data = [];
294
        $data['attendance_id'] = $attendance_id;
295
        $groupId = isset($_REQUEST['group_id']) ? $_REQUEST['group_id'] : null;
296
        $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id, $groupId);
297
298
        $filter_type = 'today';
299
        if (!empty($_REQUEST['filter'])) {
300
            $filter_type = $_REQUEST['filter'];
301
        }
302
303
        $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
304
            api_get_user_id(),
305
            api_get_course_info()
306
        ) || api_is_drh();
307
308
        if ($edit == true) {
309
            if (api_is_allowed_to_edit(null, true) || $isDrhOfCourse) {
310
                $data['users_presence'] = $attendance->get_users_attendance_sheet(
311
                    $attendance_id,
312
                    0,
313
                    $groupId
314
                );
315
            }
316
        } else {
317
            if (!empty($student_id)) {
318
                $user_id = intval($student_id);
319
            } else {
320
                $user_id = api_get_user_id();
321
            }
322
323
            if (api_is_allowed_to_edit(null, true) ||
324
                api_is_coach(api_get_session_id(), api_get_course_int_id()) ||
325
                $isDrhOfCourse
326
            ) {
327
                $data['users_presence'] = $attendance->get_users_attendance_sheet(
328
                    $attendance_id,
329
                    0,
330
                    $groupId
331
                );
332
            } else {
333
                $data['users_presence'] = $attendance->get_users_attendance_sheet(
334
                    $attendance_id,
335
                    $user_id,
336
                    $groupId
337
                );
338
            }
339
340
            $data['faults'] = $attendance->get_faults_of_user($user_id, $attendance_id, $groupId);
341
            $data['user_id'] = $user_id;
342
        }
343
344
        $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id(
345
            $attendance_id
346
        );
347
        $data['next_attendance_calendar_datetime'] = $attendance->getNextAttendanceCalendarDatetime(
348
            $attendance_id
349
        );
350
351
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
352
            if (isset($_POST['hidden_input'])) {
353
                foreach ($_POST['hidden_input'] as $cal_id) {
354
                    $users_present = [];
355
                    if (isset($_POST['check_presence'][$cal_id])) {
356
                        $users_present = $_POST['check_presence'][$cal_id];
357
                    }
358
                    $attendance->attendance_sheet_add(
359
                        $cal_id,
360
                        $users_present,
361
                        $attendance_id
362
                    );
363
                }
364
            }
365
366
            $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id, $groupId);
367
            $my_calendar_id = null;
368
            if (is_numeric($filter_type)) {
369
                $my_calendar_id = $filter_type;
370
                $filter_type = 'calendar_id';
371
            }
372
            $data['attendant_calendar'] = $attendance->get_attendance_calendar(
373
                $attendance_id,
374
                $filter_type,
375
                $my_calendar_id,
376
                $groupId
377
            );
378
            $data['attendant_calendar_all'] = $attendance->get_attendance_calendar(
379
                $attendance_id,
380
                'all',
381
                null,
382
                $groupId
383
            );
384
            $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, 0, $groupId);
385
            $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
386
            $data['next_attendance_calendar_datetime'] = $attendance->getNextAttendanceCalendarDatetime($attendance_id);
387
        } else {
388
            $data['attendant_calendar_all'] = $attendance->get_attendance_calendar(
389
                $attendance_id,
390
                'all',
391
                null,
392
                $groupId
393
            );
394
            $data['attendant_calendar'] = $attendance->get_attendance_calendar(
395
                $attendance_id,
396
                $filter_type,
397
                null,
398
                $groupId
399
            );
400
        }
401
402
        $data['edit_table'] = intval($edit);
403
        $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
404
        $this->view->set_data($data);
405
        $this->view->set_layout('layout');
406
        $this->view->set_template('attendance_sheet');
407
        $this->view->render();
408
    }
409
410
    /**
411
     * It's used for controlling attendance calendar (list, add, edit, delete),
412
     * render to attendance_calendar view.
413
     *
414
     * @param string $action        (optional, by default 'calendar_list')
415
     * @param int    $attendance_id (optional)
416
     * @param int    $calendar_id   (optional)
417
     */
418
    public function attendance_calendar($action = 'calendar_list', $attendance_id = 0, $calendar_id = 0)
419
    {
420
        $attendance = new Attendance();
421
        $calendar_id = intval($calendar_id);
422
        $data = [];
423
        $data['attendance_id'] = $attendance_id;
424
        $attendance_id = intval($attendance_id);
425
        $groupList = isset($_POST['groups']) ? [$_POST['groups']] : [];
426
427
        if ($action == 'calendar_add') {
428
            if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
429
                if (!isset($_POST['cancel'])) {
430
                    if (isset($_POST['repeat'])) {
431
                        //@todo  check this error_logs
432
                        $start_datetime = api_strtotime(
433
                            api_get_utc_datetime($_POST['date_time']),
434
                            'UTC'
435
                        );
436
437
                        $end_datetime = api_strtotime(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59'), 'UTC');
438
                        $checkdate = api_is_valid_date(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59'));
439
440
                        $repeat_type = $_POST['repeat_type'];
441
                        if (($end_datetime > $start_datetime) && $checkdate) {
442
                            $attendance->attendance_repeat_calendar_add(
443
                                $attendance_id,
444
                                $start_datetime,
445
                                $end_datetime,
446
                                $repeat_type,
447
                                $groupList
448
                            );
449
                            $action = 'calendar_list';
450
                        } else {
451
                            if (!$checkdate) {
452
                                $data['error_checkdate'] = true;
453
                            } else {
454
                                $data['error_repeat_date'] = true;
455
                            }
456
                            $data['repeat'] = true;
457
                            $action = 'calendar_add';
458
                        }
459
                    } else {
460
                        $datetime = $_POST['date_time'];
461
                        $datetimezone = api_get_utc_datetime($datetime);
462
                        if (!empty($datetime)) {
463
                            $attendance->set_date_time($datetimezone);
464
                            $attendance->attendance_calendar_add($attendance_id, $groupList);
465
                            $action = 'calendar_list';
466
                        } else {
467
                            $data['error_date'] = true;
468
                            $action = 'calendar_add';
469
                        }
470
                    }
471
                } else {
472
                    $action = 'calendar_list';
473
                }
474
            }
475
        } elseif ($action === 'calendar_edit') {
476
            $data['calendar_id'] = $calendar_id;
477
            if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
478
                if (!isset($_POST['cancel'])) {
479
                    $datetime = $_POST['date_time'];
480
                    $datetimezone = api_get_utc_datetime($datetime);
481
                    $attendance->set_date_time($datetimezone);
482
                    $attendance->attendance_calendar_edit($calendar_id, $attendance_id);
483
                    $data['calendar_id'] = 0;
484
                    $action = 'calendar_list';
485
                } else {
486
                    $action = 'calendar_list';
487
                }
488
            }
489
        } elseif ($action == 'calendar_delete') {
490
            $attendance->attendance_calendar_delete($calendar_id, $attendance_id);
491
            $action = 'calendar_list';
492
        } elseif ($action == 'calendar_all_delete') {
493
            $attendance->attendance_calendar_delete(0, $attendance_id, true);
494
            $action = 'calendar_list';
495
        }
496
497
        $data['action'] = $action;
498
        $data['attendance_calendar'] = $attendance->get_attendance_calendar(
499
            $attendance_id,
500
            'all',
501
            null,
502
            null,
503
            true
504
        );
505
        $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
506
        // render to the view
507
        $this->view->set_data($data);
508
        $this->view->set_layout('layout');
509
        $this->view->set_template('attendance_calendar');
510
        $this->view->render();
511
    }
512
513
    /**
514
     * It's used to print attendance sheet.
515
     *
516
     * @param string $action
517
     * @param int    $attendance_id
518
     */
519
    public function attendance_sheet_export_to_pdf(
520
        $action,
521
        $attendance_id,
522
        $student_id = 0,
523
        $course_id = ''
524
    ) {
525
        $attendance = new Attendance();
526
        $courseInfo = api_get_course_info($course_id);
527
        $attendance->set_course_id($courseInfo['code']);
528
        $groupId = isset($_REQUEST['group_id']) ? $_REQUEST['group_id'] : null;
529
        $data_array = [];
530
        $data_array['attendance_id'] = $attendance_id;
531
        $data_array['users_in_course'] = $attendance->get_users_rel_course($attendance_id, $groupId);
532
533
        $filter_type = 'today';
534
535
        if (!empty($_REQUEST['filter'])) {
536
            $filter_type = $_REQUEST['filter'];
537
        }
538
539
        $my_calendar_id = null;
540
        if (is_numeric($filter_type)) {
541
            $my_calendar_id = $filter_type;
542
            $filter_type = 'calendar_id';
543
        }
544
545
        $data_array['attendant_calendar'] = $attendance->get_attendance_calendar(
546
            $attendance_id,
547
            $filter_type,
548
            $my_calendar_id,
549
            $groupId
550
        );
551
552
        if (api_is_allowed_to_edit(null, true) || api_is_drh()) {
553
            $data_array['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, 0, $groupId);
554
        } else {
555
            if (!empty($student_id)) {
556
                $user_id = intval($student_id);
557
            } else {
558
                $user_id = api_get_user_id();
559
            }
560
            $data_array['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, $user_id, $groupId);
561
            $data_array['faults'] = $attendance->get_faults_of_user($user_id, $attendance_id, $groupId);
562
            $data_array['user_id'] = $user_id;
563
        }
564
565
        $data_array['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
566
567
        // Set headers pdf.
568
        $courseCategory = CourseManager::get_course_category($courseInfo['categoryCode']);
569
        $teacherInfo = CourseManager::get_teacher_list_from_course_code($courseInfo['code']);
570
        $teacherName = null;
571
        foreach ($teacherInfo as $teacherData) {
572
            if ($teacherName != null) {
573
                $teacherName = $teacherName." / ";
574
            }
575
            $teacherName .= api_get_person_name($teacherData['firstname'], $teacherData['lastname']);
576
        }
577
578
        // Get data table
579
        $data_table = [];
580
        $head_table = ['#', get_lang('Name')];
581
        foreach ($data_array['attendant_calendar'] as $class_day) {
582
            $head_table[] =
583
                api_format_date($class_day['date_time'], DATE_FORMAT_NUMBER_NO_YEAR).' '.
584
                api_format_date($class_day['date_time'], TIME_NO_SEC_FORMAT);
585
        }
586
        $data_table[] = $head_table;
587
        $data_attendant_calendar = $data_array['attendant_calendar'];
588
        $data_users_presence = $data_array['users_presence'];
589
        $count = 1;
590
591
        if (!empty($data_array['users_in_course'])) {
592
            foreach ($data_array['users_in_course'] as $user) {
593
                $cols = 1;
594
                $result = [];
595
                $result['count'] = $count;
596
                $result['full_name'] = api_get_person_name($user['firstname'], $user['lastname']);
597
                foreach ($data_array['attendant_calendar'] as $class_day) {
598
                    if ($class_day['done_attendance'] == 1) {
599
                        if ($data_users_presence[$user['user_id']][$class_day['id']]['presence'] == 1) {
600
                            $result[$class_day['id']] = get_lang('UserAttendedSymbol');
601
                        } else {
602
                            $result[$class_day['id']] = '<span style="color:red">'.get_lang('UserNotAttendedSymbol').'</span>';
603
                        }
604
                    } else {
605
                        $result[$class_day['id']] = ' ';
606
                    }
607
                    $cols++;
608
                }
609
                $count++;
610
                $data_table[] = $result;
611
            }
612
        }
613
        $max_cols_per_page = 12; //10 dates + 2 name and number
614
        $max_dates_per_page = $max_dates_per_page_original = $max_cols_per_page - 2; //10
615
        $rows = count($data_table);
616
617
        if ($cols > $max_cols_per_page) {
618
            $number_tables = round(($cols - 2) / $max_dates_per_page);
619
            $headers = $data_table[0];
620
            $all = [];
621
            $tables = [];
622
            $changed = 1;
623
624
            for ($i = 0; $i <= $rows; $i++) {
625
                $row = isset($data_table[$i]) ? $data_table[$i] : null;
626
                $key = 1;
627
                $max_dates_per_page = 10;
628
                $item = isset($data_table[$i]) ? $data_table[$i] : null;
629
                $count_j = 0;
630
631
                if (!empty($item)) {
632
                    foreach ($item as $value) {
633
                        if ($count_j >= $max_dates_per_page) {
634
                            $key++;
635
                            $max_dates_per_page = $max_dates_per_page_original * $key;
636
                            //magic hack
637
                            $tables[$key][$i][] = $tables[1][$i][0];
638
                            $tables[$key][$i][] = $tables[1][$i][1];
639
                        }
640
                        $tables[$key][$i][] = $value;
641
                        $count_j++;
642
                    }
643
                }
644
            }
645
646
            $content = null;
647
            if (!empty($tables)) {
648
                foreach ($tables as $sub_table) {
649
                    $content .= Export::convert_array_to_html($sub_table).'<br /><br />';
650
                }
651
            }
652
        } else {
653
            $content = Export::convert_array_to_html(
654
                $data_table,
655
                ['header_attributes' => ['align' => 'center']]
656
            );
657
        }
658
659
        $params = [
660
            'filename' => get_lang('Attendance').'-'.api_get_local_time(),
661
            'pdf_title' => $courseInfo['title'],
662
            'course_code' => $courseInfo['code'],
663
            'add_signatures' => ['Drh', 'Teacher', 'Date'],
664
            'orientation' => 'landscape',
665
            'pdf_teachers' => $teacherName,
666
            'pdf_course_category' => $courseCategory['name'],
667
            'format' => 'A4-L',
668
            'orientation' => 'L',
669
        ];
670
671
        Export::export_html_to_pdf($content, $params);
672
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
673
    }
674
675
    /**
676
     * Gets attendance base in the table:
677
     * TABLE_STATISTIC_TRACK_E_COURSE_ACCESS.
678
     *
679
     * @param bool $showForm
680
     * @param bool $exportToPdf
681
     */
682
    public function getAttendanceBaseInLogin($showForm = false, $exportToPdf = true)
683
    {
684
        $table = null;
685
        $formToDisplay = null;
686
        $startDate = null;
687
        $endDate = null;
688
689
        $sessionId = api_get_session_id();
690
        if ($showForm) {
691
            $form = new FormValidator(
692
                'search',
693
                'post',
694
                api_get_self().'?'.api_get_cidreq().'&action=calendar_logins'
695
            );
696
            $form->addDateRangePicker('range', get_lang('DateRange'));
697
            $form->addButton('submit', get_lang('Submit'));
698
699
            if ($form->validate()) {
700
                $values = $form->getSubmitValues();
701
702
                $startDate = api_get_utc_datetime($values['range_start']);
703
                $endDate = api_get_utc_datetime($values['range_end']);
704
            }
705
            $formToDisplay = $form->returnForm();
706
        } else {
707
            if (!empty($sessionId)) {
708
                $sessionInfo = api_get_session_info($sessionId);
709
                $startDate = $sessionInfo['access_start_date'];
710
                $endDate = $sessionInfo['access_end_date'];
711
            }
712
        }
713
714
        $attendance = new Attendance();
715
        if ($exportToPdf) {
716
            $result = $attendance->exportAttendanceLogin($startDate, $endDate);
717
            if (empty($result)) {
718
                api_not_allowed(true, get_lang('NoDataAvailable'));
719
            }
720
        }
721
        $table = $attendance->getAttendanceLoginTable($startDate, $endDate);
722
        $data = [
723
            'form' => $formToDisplay,
724
            'table' => $table,
725
        ];
726
        $this->view->set_data($data);
727
        $this->view->set_layout('layout');
728
        $this->view->set_template('calendar_logins');
729
        $this->view->render();
730
    }
731
}
732