Passed
Push — master ( 15378b...a2dd4f )
by Julito
09:24
created

customDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 17
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
require_once __DIR__.'/../inc/global.inc.php';
5
6
api_block_anonymous_users();
7
8
$is_allowedToTrack = api_is_platform_admin(true, true) ||
9
    api_is_teacher() || api_is_course_tutor();
10
11
if (!$is_allowedToTrack) {
12
    api_not_allowed(true);
13
    exit;
14
}
15
16
// the section (for the tabs)
17
$this_section = SECTION_TRACKING;
18
$quote_simple = "'";
19
20
$userId = isset($_REQUEST['user_id']) ? (int) $_REQUEST['user_id'] : 0;
21
$userInfo = api_get_user_info($userId);
22
if (empty($userInfo)) {
23
    api_not_allowed(true);
24
}
25
26
/**
27
 * @param string $dateTime
28
 * @param bool   $showTime
29
 *
30
 * @return string
31
 */
32
function customDate($dateTime, $showTime = false)
33
{
34
    $format = 'd/m/Y';
35
    if ($showTime) {
36
        $format = 'd/m/Y H:i:s';
37
    }
38
    $dateTime = api_get_local_time(
39
        $dateTime,
40
        null,
41
        null,
42
        true,
43
        false,
44
        true,
45
        $format
46
    );
47
48
    return $dateTime;
49
}
50
51
$sessions = SessionManager::getSessionsFollowedByUser($userId,
52
    null,
53
    null,
54
    null,
55
    false,
56
    false,
57
    false,
58
    'ORDER BY s.access_end_date'
59
);
60
61
$startDate = '';
62
$endDate = '';
63
if (!empty($sessions)) {
64
    foreach ($sessions as $session) {
65
        $startDate = customDate($session['access_start_date']);
66
        $endDate = customDate($session['access_end_date']);
67
    }
68
}
69
70
$form = new FormValidator(
71
    'myform',
72
    'get',
73
    api_get_self().'?user_id='.$userId,
74
    null,
75
    ['id' => 'myform']
76
);
77
$form->addElement('text', 'from', get_lang('From'));
78
$form->addElement('text', 'to', get_lang('Until'));
79
$form->addElement('hidden', 'user_id', $userId);
80
$form->addRule('from', get_lang('ThisFieldIsRequired'), 'required');
81
$form->addRule('from', get_lang('ThisFieldIsRequired').' dd/mm/yyyy', 'callback', 'validateDate');
82
$form->addRule('to', get_lang('ThisFieldIsRequired'), 'required');
83
$form->addRule('to', get_lang('ThisFieldIsRequired').' dd/mm/yyyy', 'callback', 'validateDate');
84
$form->addButtonSearch(get_lang('GenerateReport'));
85
86
/**
87
 * @param string $value
88
 *
89
 * @return bool
90
 */
91
function validateDate($value)
92
{
93
    $value = DateTime::createFromFormat('d/m/Y', $value);
94
95
    if ($value === false) {
96
        return false;
97
    }
98
99
    return true;
100
}
101
102
if ($form->validate()) {
103
    $values = $form->getSubmitValues();
104
    $from = $values['from'];
105
    $to = $values['to'];
106
107
    $from = DateTime::createFromFormat('d/m/Y', $from);
108
    $to = DateTime::createFromFormat('d/m/Y', $to);
109
110
    $from = api_get_utc_datetime($from->format('Y-m-d'));
111
    $to = api_get_utc_datetime($to->format('Y-m-d'));
112
113
    $sessionCategories = UserManager::get_sessions_by_category($userId, false);
114
    $report = [];
115
    $minLogin = 0;
116
    $maxLogin = 0;
117
    $totalDuration = 0;
118
119
    foreach ($sessionCategories as $category) {
120
        foreach ($category['sessions'] as $session) {
121
            $sessionId = $session['session_id'];
122
            $courseList = $session['courses'];
123
            foreach ($courseList as $course) {
124
                $courseInfo = api_get_course_info_by_id($course['real_id']);
125
                $result = MySpace::get_connections_to_course_by_date(
126
                    $userId,
127
                    $courseInfo,
128
                    $sessionId,
129
                    $from,
130
                    $to
131
                );
132
133
                $partialMinLogin = 0;
134
                $partialMaxLogin = 0;
135
                $partialDuration = 0;
136
137
                foreach ($result as $item) {
138
                    $record = [
139
                        customDate($item['login'], true),
140
                        customDate($item['logout'], true),
141
                        api_format_time($item['duration'], 'js'),
142
                    ];
143
144
                    $totalDuration += $item['duration'];
145
146
                    if (empty($minLogin)) {
147
                        $minLogin = api_strtotime($item['login'], 'UTC');
148
                    }
149
                    if ($minLogin > api_strtotime($item['login'], 'UTC')) {
150
                        $minLogin = api_strtotime($item['login'], 'UTC');
151
                    }
152
                    if (api_strtotime($item['logout']) > $maxLogin) {
153
                        $maxLogin = api_strtotime($item['logout'], 'UTC');
154
                    }
155
156
                    // Partials
157
                    $partialDuration += $item['duration'];
158
                    if (empty($partialMinLogin)) {
159
                        $partialMinLogin = api_strtotime($item['login'], 'UTC');
160
                    }
161
                    if ($partialMinLogin > api_strtotime($item['login'], 'UTC')) {
162
                        $partialMinLogin = api_strtotime($item['login'], 'UTC');
163
                    }
164
                    if (api_strtotime($item['logout'], 'UTC') > $partialMaxLogin) {
165
                        $partialMaxLogin = api_strtotime($item['logout'], 'UTC');
166
                    }
167
168
                    $report[$sessionId]['courses'][$course['real_id']][] = $record;
169
                    $report[$sessionId]['name'][$course['real_id']] = $courseInfo['title'].'&nbsp; ('.$session['session_name'].')';
170
                }
171
172
                if (!empty($result)) {
173
                    $record = [
174
                        customDate($partialMinLogin, true),
175
                        customDate($partialMaxLogin, true),
176
                        api_format_time($partialDuration, 'js'),
177
                    ];
178
                    $report[$sessionId]['courses'][$course['real_id']][] = $record;
179
                    $report[$sessionId]['name'][$course['real_id']] = $courseInfo['title'].'&nbsp; ('.$session['session_name'].')';
180
                }
181
            }
182
        }
183
    }
184
185
    $courses = CourseManager::returnCourses($userId);
186
    $courses = array_merge($courses['in_category'], $courses['not_category']);
187
188
    foreach ($courses as $course) {
189
        $result = MySpace::get_connections_to_course_by_date(
190
            $userId,
191
            $course,
192
            0,
193
            $from,
194
            $to
195
        );
196
197
        $partialMinLogin = 0;
198
        $partialMaxLogin = 0;
199
        $partialDuration = 0;
200
201
        foreach ($result as $item) {
202
            $record = [
203
                customDate($item['login'], true),
204
                customDate($item['logout'], true),
205
                api_format_time($item['duration'], 'js'),
206
            ];
207
            $report[0]['courses'][$course['course_id']][] = $record;
208
            $report[0]['name'][$course['course_id']] = $course['title'];
209
210
            $totalDuration += $item['duration'];
211
212
            if (empty($minLogin)) {
213
                $minLogin = api_strtotime($item['login'], 'UTC');
214
            }
215
            if ($minLogin > api_strtotime($item['login'], 'UTC')) {
216
                $minLogin = api_strtotime($item['login'], 'UTC');
217
            }
218
            if (api_strtotime($item['logout'], 'UTC') > $maxLogin) {
219
                $maxLogin = api_strtotime($item['logout'], 'UTC');
220
            }
221
222
            // Partials
223
            $partialDuration += $item['duration'];
224
            if (empty($partialMinLogin)) {
225
                $partialMinLogin = api_strtotime($item['login'], 'UTC');
226
            }
227
            if ($partialMinLogin > api_strtotime($item['login'], 'UTC')) {
228
                $partialMinLogin = api_strtotime($item['login'], 'UTC');
229
            }
230
            if (api_strtotime($item['logout'], 'UTC') > $partialMaxLogin) {
231
                $partialMaxLogin = api_strtotime($item['logout'], 'UTC');
232
            }
233
        }
234
235
        if (!empty($result)) {
236
            $record = [
237
                customDate($partialMinLogin, true),
238
                customDate($partialMaxLogin, true),
239
                api_format_time($partialDuration, 'js'),
240
            ];
241
242
            $report[0]['courses'][$course['course_id']][] = $record;
243
            $report[0]['name'][$course['course_id']] = $course['title'];
244
        }
245
    }
246
247
    $table = new HTML_Table(['class' => 'data_table']);
248
    $headers = [
249
        get_lang('MinStartDate'),
250
        get_lang('MaxEndDate'),
251
        get_lang('TotalDuration'),
252
    ];
253
    $row = 0;
254
    $column = 0;
255
    foreach ($headers as $header) {
256
        $table->setHeaderContents($row, $column, $header);
257
        $column++;
258
    }
259
    $row++;
260
    $column = 0;
261
    $table->setCellContents($row, $column++, customDate($minLogin));
262
    $table->setCellContents($row, $column++, customDate($maxLogin));
263
    $table->setCellContents($row, $column++, api_format_time($totalDuration, 'js'));
264
    $content = Display::page_subheader3(get_lang('Total')).$table->toHtml();
265
266
    foreach ($report as $sessionId => $data) {
267
        foreach ($data['courses'] as $courseId => $courseData) {
268
            $content .= Display::page_subheader3($data['name'][$courseId]);
269
            $table = new HTML_Table(['class' => 'data_table']);
270
            $headers = [
271
                get_lang('StartDate'),
272
                get_lang('EndDate'),
273
                get_lang('Duration'),
274
            ];
275
            $row = 0;
276
            $column = 0;
277
            foreach ($headers as $header) {
278
                $table->setHeaderContents($row, $column, $header);
279
                $column++;
280
            }
281
            $row++;
282
            $countData = count($courseData);
283
            foreach ($courseData as $record) {
284
                $column = 0;
285
                foreach ($record as $item) {
286
                    $table->setCellContents($row, $column++, $item);
287
                    if ($row == $countData) {
288
                        $table->setRowAttributes($row, ['style' => 'font-weight:bold']);
289
                    }
290
                }
291
                $row++;
292
            }
293
            $content .= $table->toHtml();
294
        }
295
    }
296
297
    $tpl = new Template('', false, false, false, true, false, false);
298
    $tpl->assign('title', get_lang('AttestationOfAttendance'));
299
    $tpl->assign('student', $userInfo['complete_name']);
300
    $tpl->assign('table_progress', $content);
301
302
    $content = $tpl->fetch($tpl->get_template('my_space/pdf_export_student.tpl'));
303
    $params = [
304
        'pdf_title' => get_lang('Resume'),
305
        //'session_info' => $sessionInfo,
306
        'course_info' => '',
307
        'pdf_date' => '',
308
        'student_info' => $userInfo,
309
        'show_grade_generated_date' => true,
310
        'show_real_course_teachers' => false,
311
        'show_teacher_as_myself' => false,
312
        'orientation' => 'P',
313
    ];
314
315
    @$pdf = new PDF('A4', $params['orientation'], $params);
316
317
    $pdf->setBackground($tpl->theme);
0 ignored issues
show
Bug introduced by
The method setBackground() does not exist on PDF. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

317
    $pdf->/** @scrutinizer ignore-call */ 
318
          setBackground($tpl->theme);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
318
    @$pdf->content_to_pdf(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for content_to_pdf(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

318
    /** @scrutinizer ignore-unhandled */ @$pdf->content_to_pdf(

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
319
        $content,
320
        '',
321
        '',
322
        null,
323
        'D',
324
        false,
325
        null,
326
        false,
327
        true,
328
        false
329
    );
330
    exit;
331
}
332
333
$interbreadcrumb[] = ['url' => '#', 'name' => get_lang('AccessDetails')];
334
335
Display::display_header('');
336
$userInfo = api_get_user_info($userId);
337
$result_to_print = '';
338
echo Display::page_header(get_lang('DetailsStudentInCourse'));
339
echo Display::page_subheader(
340
    get_lang('User').': '.$userInfo['complete_name']
341
);
342
343
$form->setDefaults(['from' => $startDate, 'to' => $endDate]);
344
$form->display();
345
Display::display_footer();
346