Completed
Push — master ( 1ab056...1759c8 )
by Julito
55:42 queued 24:21
created

AttendanceController::getAttendanceBaseInLogin()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 37
nc 12
nop 2
dl 0
loc 51
rs 8.6588
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
        $data = array();
34
35
        // render to the view
36
        $this->view->set_data($data);
37
        $this->view->set_layout('layout');
38
        $this->view->set_template('attendance_list');
39
        $this->view->render();
40
    }
41
42
    /**
43
     * It's used for adding attendace,
44
     * render to attendance_add or attendance_list view
45
     */
46
    public function attendance_add()
47
    {
48
        $attendance = new Attendance();
49
        $data = array();
50
51
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
52
            if (!empty($_POST['title'])) {
53
                $check = Security::check_token();
54
                $last_id = 0;
55
                if ($check) {
56
                    $attendance->set_name($_POST['title']);
57
                    $attendance->set_description($_POST['description']);
58
                    $attendance->set_attendance_qualify_title($_POST['attendance_qualify_title']);
59
                    $attendance->set_attendance_weight($_POST['attendance_weight']);
60
                    $link_to_gradebook = false;
61
                    if (isset($_POST['attendance_qualify_gradebook']) &&
62
                        $_POST['attendance_qualify_gradebook'] == 1
63
                    ) {
64
                        $link_to_gradebook = true;
65
                    }
66
                    $attendance->category_id = isset($_POST['category_id']) ? $_POST['category_id'] : 0;
67
                    $last_id = $attendance->attendance_add($link_to_gradebook);
68
                    Security::clear_token();
69
                }
70
                header('Location: index.php?action=calendar_add&attendance_id='.$last_id.'&'.api_get_cidreq());
71
                exit;
72 View Code Duplication
            } else {
73
                $data['error'] = true;
74
                $this->view->set_data($data);
75
                $this->view->set_layout('layout');
76
                $this->view->set_template('attendance_add');
77
                $this->view->render();
78
            }
79 View Code Duplication
        } else {
80
            $this->view->set_data($data);
81
            $this->view->set_layout('layout');
82
            $this->view->set_template('attendance_add');
83
            $this->view->render();
84
        }
85
    }
86
87
    /**
88
     * It's used for editing attendance,
89
     * render to attendance_edit or attendance_list view
90
     * @param int	$attendance_id
91
     */
92
    public function attendance_edit($attendance_id)
93
    {
94
        $attendance = new Attendance();
95
        $data = array();
96
        $attendance_id = intval($attendance_id);
97
98
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
99
            if (!empty($_POST['title'])) {
100
                $check = Security::check_token();
101
                if ($check) {
102
                    $attendance->set_name($_POST['title']);
103
                    $attendance->set_description($_POST['description']);
104
                    if (isset($_POST['attendance_qualify_title'])) {
105
                        $attendance->set_attendance_qualify_title(
106
                            $_POST['attendance_qualify_title']
107
                        );
108
                    }
109
110
                    if (isset($_POST['attendance_weight'])) {
111
                        $attendance->set_attendance_weight(
112
                            $_POST['attendance_weight']
113
                        );
114
                    }
115
116
                    $attendance->category_id = isset($_POST['category_id']) ? $_POST['category_id'] : '';
117
                    $link_to_gradebook = false;
118
                    if (isset($_POST['attendance_qualify_gradebook']) &&
119
                        $_POST['attendance_qualify_gradebook'] == 1
120
                    ) {
121
                        $link_to_gradebook = true;
122
                    }
123
                    $attendance->attendance_edit($attendance_id, $link_to_gradebook);
124
                    Security::clear_token();
125
                    header('location:index.php?action=attendance_list&'.api_get_cidreq());
126
                    exit;
127
                }
128
            } else {
129
                $data['attendance_id'] = $_POST['attendance_id'];
130
                $data['error'] = true;
131
                $this->view->set_data($data);
132
                $this->view->set_layout('layout');
133
                $this->view->set_template('attendance_edit');
134
                $this->view->render();
135
            }
136
        } else {
137
            // default values
138
            $attendance_data = $attendance->get_attendance_by_id(
139
                $attendance_id
140
            );
141
            $data['attendance_id'] = $attendance_data['id'];
142
            $data['title'] = $attendance_data['name'];
143
            $data['description'] = $attendance_data['description'];
144
            $data['attendance_qualify_title'] = $attendance_data['attendance_qualify_title'];
145
            $data['attendance_weight'] = $attendance_data['attendance_weight'];
146
147
            $this->view->set_data($data);
148
            $this->view->set_layout('layout');
149
            $this->view->set_template('attendance_edit');
150
            $this->view->render();
151
        }
152
    }
153
154
    /**
155
     * It's used for delete attendaces
156
     * render to attendance_list view
157
     * @param int	$attendance_id
158
     */
159
    public function attendance_delete($attendance_id)
160
    {
161
        $allowDeleteAttendance = api_get_setting('allow_delete_attendance');
162
        if ($allowDeleteAttendance !== 'true') {
163
            $this->attendance_list();
164
165
            return false;
166
        }
167
168
        $attendance = new Attendance();
169
        if (!empty($attendance_id)) {
170
            $affected_rows = $attendance->attendance_delete($attendance_id);
171
        }
172
173
        if ($affected_rows) {
174
            $message['message_attendance_delete'] = true;
175
        }
176
        $this->attendance_list();
177
    }
178
179
    /**
180
     * It's used for make attendance visible
181
     * render to attendance_list view
182
     * @param int $attendanceId
183
     */
184 View Code Duplication
    public function attendanceSetVisible($attendanceId)
185
    {
186
        $attendance = new Attendance();
187
        $affectedRows = null;
188
        if (!empty($attendanceId)) {
189
            $affectedRows = $attendance->changeVisibility($attendanceId, 1);
190
        }
191
        if ($affectedRows) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $affectedRows of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
192
            $message['message_attendance_delete'] = true;
193
        }
194
        $this->attendance_list();
195
    }
196
197
    /**
198
     * It's used for make attendance invisible
199
     * render to attendance_list view
200
     * @param int $attendanceId
201
     */
202 View Code Duplication
    public function attendanceSetInvisible($attendanceId)
203
    {
204
        $attendance = new Attendance();
205
        if (!empty($attendanceId)) {
206
            $affectedRows = $attendance->changeVisibility($attendanceId, 0);
207
        }
208
        if ($affectedRows) {
209
            $message['message_attendance_delete'] = true;
210
        }
211
        $this->attendance_list();
212
    }
213
214
    /**
215
     * Restores an attendance entry and fallback to attendances rendering
216
     * @param int $attendance_id
217
     */
218 View Code Duplication
    public function attendance_restore($attendance_id)
219
    {
220
        $attendance = new Attendance();
221
        $affected_rows = false;
222
        if (!empty($attendance_id)) {
223
            $affected_rows = $attendance->attendance_restore($attendance_id);
224
        }
225
        if ($affected_rows) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $affected_rows of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
226
            $message['message_attendance_restore'] = true;
227
        }
228
        $this->attendance_list();
229
    }
230
231
    /**
232
     * Lock or unlock an attendance
233
     * render to attendance_list view
234
     * @param string  $action (lock_attendance or unlock_attendance)
235
     * @param int     $attendance_id
236
     * render to attendance_list view
237
     */
238
    public function lock_attendance($action, $attendance_id)
239
    {
240
        $attendance = new Attendance();
241
        $attendance_id = intval($attendance_id);
242
243
        if ($action == 'lock_attendance') {
244
            $result = $attendance->lock_attendance($attendance_id);
245
        } else {
246
            $result = $attendance->lock_attendance($attendance_id, false);
247
        }
248
        if ($result) {
249
            $message['message_locked_attendance'] = true;
250
        }
251
        $this->attendance_list();
252
    }
253
254
    public function export($id, $type = 'pdf')
255
    {
256
        $attendance = new Attendance();
257
    }
258
259
    /**
260
     * It's used for controlling attendance sheet (list, add),
261
     * render to attendance_sheet view
262
     * @param string $action
263
     * @param int    $attendance_id
264
     * @param int    $student_id
265
     * @param bool   $edit
266
     */
267
    public function attendance_sheet($action, $attendance_id, $student_id = 0, $edit = true)
268
    {
269
        $attendance = new Attendance();
270
        $data = array();
271
        $data['attendance_id'] = $attendance_id;
272
        $groupId = isset($_REQUEST['group_id']) ? $_REQUEST['group_id'] : null;
273
        $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id, $groupId);
274
275
        $filter_type = 'today';
276
        if (!empty($_REQUEST['filter'])) {
277
            $filter_type = $_REQUEST['filter'];
278
        }
279
280
        $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
281
            api_get_user_id(),
282
            api_get_course_info()
283
        );
284
285
        if ($edit == true) {
286
            if (api_is_allowed_to_edit(null, true) || $isDrhOfCourse) {
287
                $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, 0, $groupId);
288
            }
289
        } else {
290
            if (!empty($student_id)) {
291
                $user_id = intval($student_id);
292
            } else {
293
                $user_id = api_get_user_id();
294
            }
295
296
            if (api_is_allowed_to_edit(null, true) ||
297
                api_is_coach(api_get_session_id(), api_get_course_int_id()) ||
298
                $isDrhOfCourse
299
            ) {
300
                $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, 0, $groupId);
301
            } else {
302
                $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, $user_id, $groupId);
303
            }
304
305
            $data['faults']  = $attendance->get_faults_of_user($user_id, $attendance_id, $groupId);
306
            $data['user_id'] = $user_id;
307
        }
308
309
        $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
310
        $data['next_attendance_calendar_datetime'] = $attendance->get_next_attendance_calendar_datetime($attendance_id);
311
312
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
313
            if (isset($_POST['hidden_input'])) {
314
                foreach ($_POST['hidden_input'] as $cal_id) {
315
                    $users_present = array();
316
                    if (isset($_POST['check_presence'][$cal_id])) {
317
                        $users_present = $_POST['check_presence'][$cal_id];
318
                    }
319
                    $attendance->attendance_sheet_add(
320
                        $cal_id,
321
                        $users_present,
322
                        $attendance_id
323
                    );
324
                }
325
            }
326
327
            $data['users_in_course'] = $attendance->get_users_rel_course($attendance_id, $groupId);
328
            $my_calendar_id = null;
329
            if (is_numeric($filter_type)) {
330
                $my_calendar_id = $filter_type;
331
                $filter_type = 'calendar_id';
332
            }
333
            $data['attendant_calendar'] = $attendance->get_attendance_calendar(
334
                $attendance_id,
335
                $filter_type,
336
                $my_calendar_id,
337
                $groupId
338
            );
339
            $data['attendant_calendar_all'] = $attendance->get_attendance_calendar($attendance_id, 'all', null, $groupId);
340
            $data['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, 0, $groupId);
341
            $data['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
342
            $data['next_attendance_calendar_datetime'] = $attendance->get_next_attendance_calendar_datetime($attendance_id);
343
        } else {
344
            $data['attendant_calendar_all'] = $attendance->get_attendance_calendar($attendance_id, 'all', null, $groupId);
345
            $data['attendant_calendar'] = $attendance->get_attendance_calendar($attendance_id, $filter_type, null, $groupId);
346
        }
347
348
        $data['edit_table'] = intval($edit);
349
        $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
350
        $this->view->set_data($data);
351
        $this->view->set_layout('layout');
352
        $this->view->set_template('attendance_sheet');
353
        $this->view->render();
354
    }
355
356
    /**
357
     * It's used for controlling attendance calendar (list, add, edit, delete),
358
     * render to attendance_calendar view
359
     * @param string $action (optional, by default 'calendar_list')
360
     * @param int	 $attendance_id (optional)
361
     * @param int	 $calendar_id (optional)
362
     */
363
    public function attendance_calendar($action = 'calendar_list', $attendance_id = 0, $calendar_id = 0)
364
    {
365
        $attendance = new Attendance();
366
        $calendar_id = intval($calendar_id);
367
        $data = array();
368
        $data['attendance_id'] = $attendance_id;
369
        $attendance_id = intval($attendance_id);
370
        $groupList = isset($_POST['groups']) ? array($_POST['groups']) : array();
371
372
        if ($action == 'calendar_add') {
373
            if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
374
                if (!isset($_POST['cancel'])) {
375
                    if (isset($_POST['repeat'])) {
376
                        //@todo  check this error_logs
377
                        $start_datetime = api_strtotime(
378
                            api_get_utc_datetime($_POST['date_time']), 'UTC'
0 ignored issues
show
Bug introduced by
It seems like api_get_utc_datetime($_POST['date_time']) targeting api_get_utc_datetime() can also be of type null or object<DateTime>; however, api_strtotime() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
379
                        );
380
381
                        $end_datetime = api_strtotime(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59'), 'UTC');
0 ignored issues
show
Bug introduced by
It seems like api_get_utc_datetime($_P...e_time'] . ' 23:59:59') targeting api_get_utc_datetime() can also be of type null or object<DateTime>; however, api_strtotime() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
382
                        $checkdate = api_is_valid_date(api_get_utc_datetime($_POST['end_date_time'].' 23:59:59'));
0 ignored issues
show
Bug introduced by
It seems like api_get_utc_datetime($_P...e_time'] . ' 23:59:59') targeting api_get_utc_datetime() can also be of type null or object<DateTime>; however, api_is_valid_date() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
383
384
                        $repeat_type = $_POST['repeat_type'];
385
                        if (($end_datetime > $start_datetime) && $checkdate) {
386
                            $attendance->attendance_repeat_calendar_add(
387
                                $attendance_id,
388
                                $start_datetime,
389
                                $end_datetime,
390
                                $repeat_type,
391
                                $groupList
392
                            );
393
                            $action = 'calendar_list';
394
                        } else {
395
                            if (!$checkdate) {
396
                                $data['error_checkdate'] = true;
397
                            } else {
398
                                $data['error_repeat_date'] = true;
399
                            }
400
                            $data['repeat'] = true;
401
                            $action = 'calendar_add';
402
                        }
403
                    } else {
404
                        $datetime = $_POST['date_time'];
405
                        $datetimezone = api_get_utc_datetime($datetime);
406
                        if (!empty($datetime)) {
407
                            $attendance->set_date_time($datetimezone);
408
                            $attendance->attendance_calendar_add($attendance_id, $groupList);
409
                            $action = 'calendar_list';
410
                        } else {
411
                            $data['error_date'] = true;
412
                            $action = 'calendar_add';
413
                        }
414
                    }
415
                } else {
416
                    $action = 'calendar_list';
417
                }
418
            }
419
        } else if ($action === 'calendar_edit') {
420
            $data['calendar_id'] = $calendar_id;
421
            if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
422
                if (!isset($_POST['cancel'])) {
423
                    $datetime = $_POST['date_time'];
424
                    $datetimezone = api_get_utc_datetime($datetime);
425
                    $attendance->set_date_time($datetimezone);
426
                    $attendance->attendance_calendar_edit($calendar_id, $attendance_id);
427
                    $data['calendar_id'] = 0;
428
                    $action = 'calendar_list';
429
                } else {
430
                    $action = 'calendar_list';
431
                }
432
            }
433
        } else if ($action == 'calendar_delete') {
434
            $attendance->attendance_calendar_delete($calendar_id, $attendance_id);
435
            $action = 'calendar_list';
436
        } else if ($action == 'calendar_all_delete') {
437
            $attendance->attendance_calendar_delete(0, $attendance_id, true);
438
            $action = 'calendar_list';
439
        }
440
441
        $data['action'] = $action;
442
        $data['attendance_calendar'] = $attendance->get_attendance_calendar(
443
            $attendance_id,
444
            'all',
445
            null,
446
            null,
447
            true
448
        );
449
        $data['is_locked_attendance'] = $attendance->is_locked_attendance($attendance_id);
450
        // render to the view
451
        $this->view->set_data($data);
452
        $this->view->set_layout('layout');
453
        $this->view->set_template('attendance_calendar');
454
        $this->view->render();
455
    }
456
457
    /**
458
     * It's used to print attendance sheet
459
     * @param string $action
460
     * @param int    $attendance_id
461
     */
462
    public function attendance_sheet_export_to_pdf($action, $attendance_id, $student_id = 0, $course_id = '')
463
    {
464
        $attendance = new Attendance();
465
        $courseInfo = CourseManager::get_course_information($course_id);
466
        $attendance->set_course_id($courseInfo['code']);
467
        $groupId = isset($_REQUEST['group_id']) ? $_REQUEST['group_id'] : null;
468
        $data_array = array();
469
        $data_array['attendance_id'] = $attendance_id;
470
        $data_array['users_in_course'] = $attendance->get_users_rel_course($attendance_id, $groupId);
471
472
        $filter_type = 'today';
473
474
        if (!empty($_REQUEST['filter'])) {
475
            $filter_type = $_REQUEST['filter'];
476
        }
477
478
        $my_calendar_id = null;
479
        if (is_numeric($filter_type)) {
480
            $my_calendar_id = $filter_type;
481
            $filter_type = 'calendar_id';
482
        }
483
484
        $data_array['attendant_calendar'] = $attendance->get_attendance_calendar(
485
            $attendance_id,
486
            $filter_type,
487
            $my_calendar_id,
488
            $groupId
489
        );
490
491
        if (api_is_allowed_to_edit(null, true) || api_is_drh()) {
492
            $data_array['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, 0, $groupId);
493
        } else {
494
            if (!empty($student_id)) {
495
                $user_id = intval($student_id);
496
            } else {
497
                $user_id = api_get_user_id();
498
            }
499
            $data_array['users_presence'] = $attendance->get_users_attendance_sheet($attendance_id, $user_id, $groupId);
500
            $data_array['faults'] = $attendance->get_faults_of_user($user_id, $attendance_id, $groupId);
501
            $data_array['user_id'] = $user_id;
502
        }
503
504
        $data_array['next_attendance_calendar_id'] = $attendance->get_next_attendance_calendar_id($attendance_id);
505
506
        // Set headers pdf.
507
        $courseCategory = CourseManager::get_course_category($courseInfo['category_code']);
508
        $teacherInfo = CourseManager::get_teacher_list_from_course_code($courseInfo['code']);
509
        $teacherName = null;
510
        foreach ($teacherInfo as $teacherData) {
0 ignored issues
show
Bug introduced by
The expression $teacherInfo of type false|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
511
            if ($teacherName != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $teacherName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
512
                $teacherName = $teacherName." / ";
513
            }
514
            $teacherName .= api_get_person_name($teacherData['firstname'], $teacherData['lastname']);
515
        }
516
517
        // Get data table
518
        $data_table = array();
519
        $head_table = array('#', get_lang('Name'));
520
        foreach ($data_array['attendant_calendar'] as $class_day) {
521
            $head_table[] =
522
                api_format_date($class_day['date_time'], DATE_FORMAT_NUMBER_NO_YEAR).' '.
523
                api_format_date($class_day['date_time'], TIME_NO_SEC_FORMAT);
524
        }
525
        $data_table[] = $head_table;
526
        $data_attendant_calendar = $data_array['attendant_calendar'];
527
        $data_users_presence = $data_array['users_presence'];
528
        $count = 1;
529
530
        if (!empty($data_array['users_in_course'])) {
531
            foreach ($data_array['users_in_course'] as $user) {
0 ignored issues
show
Bug introduced by
The expression $data_array['users_in_course'] of type integer|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
532
                $cols = 1;
533
                $result = array();
534
                $result['count'] = $count;
535
                $result['full_name'] = api_get_person_name($user['firstname'], $user['lastname']);
536
                foreach ($data_array['attendant_calendar'] as $class_day) {
537
                    if ($class_day['done_attendance'] == 1) {
538
                        if ($data_users_presence[$user['user_id']][$class_day['id']]['presence'] == 1) {
539
                            $result[$class_day['id']] = get_lang('UserAttendedSymbol');
540
                        } else {
541
                            $result[$class_day['id']] = '<span style="color:red">'.get_lang('UserNotAttendedSymbol').'</span>';
542
                        }
543
                    } else {
544
                        $result[$class_day['id']] = ' ';
545
                    }
546
                    $cols++;
547
                }
548
                $count++;
549
                $data_table[] = $result;
550
            }
551
        }
552
        $max_cols_per_page = 12; //10 dates + 2 name and number
553
        $max_dates_per_page = $max_dates_per_page_original = $max_cols_per_page - 2;//10
554
        $rows = count($data_table);
555
556
        if ($cols > $max_cols_per_page) {
557
            $number_tables = round(($cols-2)/$max_dates_per_page);
558
            $headers = $data_table[0];
559
            $all = array();
560
            $tables = array();
561
            $changed = 1;
562
563
            for ($i= 0; $i <= $rows; $i++) {
564
                $row = isset($data_table[$i]) ? $data_table[$i] : null;
565
                $key = 1;
566
                $max_dates_per_page = 10;
567
                $item = isset($data_table[$i]) ? $data_table[$i] : null;
568
                $count_j = 0;
569
570
                if (!empty($item)) {
571
                    foreach ($item as $value) {
572
                        if ($count_j >= $max_dates_per_page) {
573
                            $key++;
574
                            $max_dates_per_page = $max_dates_per_page_original*$key;
575
                            //magic hack
576
                            $tables[$key][$i][] = $tables[1][$i][0];
577
                            $tables[$key][$i][] = $tables[1][$i][1];
578
                        }
579
                        $tables[$key][$i][] = $value;
580
                        $count_j++;
581
                    }
582
                }
583
            }
584
585
            $content = null;
586
            if (!empty($tables)) {
587
                foreach ($tables as $sub_table) {
588
                    $content .= Export::convert_array_to_html($sub_table).'<br /><br />';
589
                }
590
            }
591
        } else {
592
            $content = Export::convert_array_to_html(
593
                $data_table,
594
                array('header_attributes' => array('align' => 'center'))
595
            );
596
        }
597
598
        $params = array(
599
            'filename' => get_lang('Attendance').'-'.api_get_local_time(),
600
            'pdf_title' => $courseInfo['title'],
601
            'course_code' => $courseInfo['code'],
602
            'add_signatures' => true,
603
            'orientation' => 'landscape',
604
            'pdf_teachers' => $teacherName,
605
            'pdf_course_category' => $courseCategory['name'],
606
            'format' => 'A4-L',
607
            'orientation' => 'L'
608
        );
609
610
        Export::export_html_to_pdf($content, $params);
611
        exit;
612
    }
613
614
    /**
615
     * Gets attendance base in the table:
616
     * TABLE_STATISTIC_TRACK_E_COURSE_ACCESS
617
     * @param bool $showForm
618
     */
619
    public function getAttendanceBaseInLogin($showForm = false, $exportToPdf = true)
620
    {
621
        $table = null;
622
        $formToDisplay = null;
623
        $startDate = null;
624
        $endDate = null;
625
626
        $sessionId = api_get_session_id();
627
        if ($showForm) {
628
            $form = new FormValidator(
629
                'search',
630
                'post',
631
                api_get_self() . '?' . api_get_cidreq(
632
                ) . '&action=calendar_logins'
633
            );
634
            $form->addDateRangePicker('range', get_lang('DateRange'));
635
            $form->addButton('submit', get_lang('Submit'));
636
637
            if ($form->validate()) {
638
                $values = $form->getSubmitValues();
639
640
                $startDate = api_get_utc_datetime($values['range_start']);
641
                $endDate = api_get_utc_datetime($values['range_end']);
642
            }
643
            $formToDisplay = $form->returnForm();
644
        } else {
645
           if (!empty($sessionId)) {
646
               $sessionInfo = api_get_session_info($sessionId);
647
               $startDate = $sessionInfo['access_start_date'];
648
               $endDate = $sessionInfo['access_end_date'];
649
           }
650
        }
651
652
        $attendance = new Attendance();
653
654
        if ($exportToPdf) {
655
            $result = $attendance->exportAttendanceLogin($startDate, $endDate);
656
            if (empty($result)) {
657
                api_not_allowed(true, get_lang('NoDataAvailable'));
658
            }
659
        }
660
        $table = $attendance->getAttendanceLoginTable($startDate, $endDate);
661
        $data = array(
662
            'form' => $formToDisplay,
663
            'table' => $table
664
        );
665
        $this->view->set_data($data);
666
        $this->view->set_layout('layout');
667
        $this->view->set_template('calendar_logins');
668
        $this->view->render();
669
    }
670
}
671