Passed
Push — 1.11.x ( 31fff3...86e7ae )
by Yannick
15:37 queued 10s
created

main/mySpace/session_filter.php (1 issue)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Report for current courses followed by the user.
7
 */
8
$cidReset = true;
9
require_once __DIR__.'/../inc/global.inc.php';
10
$this_section = SECTION_TRACKING;
11
12
if (!api_is_allowed_to_create_course() && !api_is_drh()) {
13
    api_not_allowed(true);
14
}
15
16
$allowCustomCertificate = 'true' === api_get_plugin_setting('customcertificate', 'enable_plugin_customcertificate');
17
$plugin = CustomCertificatePlugin::create();
18
19
$tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
20
$tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
21
$tblSessionRelCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
22
$tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER);
23
24
define('NO_DATE_FILTER', 0);
25
define('DATE_BEGIN_FILTER', 1);
26
define('DATE_END_FILTER', 2);
27
define('ALL_DATE_FILTER', 3);
28
29
$certificateList = [];
30
$urlParam = '';
31
$form = new FormValidator('search_user', 'GET', api_get_self());
32
$innerJoinSessionRelUser = '';
33
$whereCondictionDRH = '';
34
$whereCondictionMultiUrl = '';
35
if (api_is_drh()) {
36
    $innerJoinSessionRelUser = "INNER JOIN $tblSessionRelUser as session_rel_user
37
                                ON (s.id = session_rel_user.session_id)";
38
    $whereCondictionDRH = "WHERE session_rel_user.user_id = ".api_get_user_id();
39
    $whereCondictionMultiUrl = " AND session_rel_user.user_id = ".api_get_user_id();
40
}
41
42
// Select of sessions.
43
$sql = "SELECT s.id, name FROM $tblSession s
44
        $innerJoinSessionRelUser
45
        $whereCondictionDRH
46
        ORDER BY name";
47
48
if (api_is_multiple_url_enabled()) {
49
    $tblSessionRelAccessUrl = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
50
    $accessUrlId = api_get_current_access_url_id();
51
    if ($accessUrlId != -1) {
52
        $sql = "SELECT s.id, name FROM $tblSession s
53
                INNER JOIN $tblSessionRelAccessUrl as session_rel_url
54
                ON (s.id = session_rel_url.session_id)
55
                $innerJoinSessionRelUser
56
                WHERE access_url_id = $accessUrlId
57
                $whereCondictionMultiUrl
58
                ORDER BY name";
59
    }
60
}
61
$result = Database::query($sql);
62
$Sessions = Database::store_result($result);
63
$options = [];
64
$options['0'] = '';
65
foreach ($Sessions as $enreg) {
66
    $options[$enreg['id']] = $enreg['name'];
67
}
68
69
$form->addElement('select', 'session_id', get_lang('SessionList'), $options, ['id' => 'session-id']);
70
$form->addDatePicker('date_begin', get_lang('DateStart'), ['id' => 'date-begin']);
71
$form->addDatePicker('date_end', get_lang('DateEnd'), ['id' => 'date-end']);
72
73
// EXTRA FIELDS
74
$extraField = new ExtraField('user');
75
$returnParams = $extraField->addElements(
76
    $form,
77
    0,
78
    [],
79
    true,
80
    false,
81
    [],
82
    [],
83
    [],
84
    false,
85
    true
86
);
87
88
$form->addElement('hidden', 'formSent', 1);
89
$form->addButtonSearch(get_lang('Search'));
90
$form->addButtonExport(get_lang('ExportAsCSV'), 'export');
91
92
if ($form->validate()) {
93
    $values = $form->getSubmitValues();
94
    $exportToCsv = isset($values['export']);
95
    $sessionId = (int) $_REQUEST['session_id'];
96
    $dateBegin = isset($_REQUEST['date_begin']) ? strtotime($_REQUEST['date_begin']) : null;
97
    $dateEnd = isset($_REQUEST['date_end']) ? strtotime($_REQUEST['date_end'].' 23:59:59') : null;
98
99
    $filterDate = 0;
100
    if (!empty($dateBegin)) {
101
        $filterDate += DATE_BEGIN_FILTER;
102
    }
103
    if (!empty($dateEnd)) {
104
        $filterDate += DATE_END_FILTER;
105
    }
106
107
    $filterCheckList = [];
108
    $extraField = new ExtraField('user');
109
    $extraFieldsAll = $extraField->get_all(['filter = ?' => 1], 'option_order');
110
    foreach ($extraFieldsAll as $field) {
111
        if (!empty($_REQUEST['extra_'.$field['variable']])) {
112
            $filterCheckList[$field['id']] = $field;
113
        }
114
    }
115
116
    $result = Database::select(
117
        'c.id, c.code',
118
        "$tbl_course c INNER JOIN  $tblSessionRelCourse r ON c.id = r.c_id",
119
        [
120
            'where' => [
121
                "r.session_id = ? " => [$sessionId],
122
            ],
123
        ]
124
    );
125
126
    foreach ($result as $value) {
127
        $courseId = $value['id'];
128
        $courseCode = $value['code'];
129
130
        $cats = Category::load(
131
            null,
132
            null,
133
            $courseCode,
134
            null,
135
            null,
136
            $sessionId,
137
            'ORDER BY id'
138
        );
139
140
        if (empty($cats)) {
141
            // first time
142
            $cats = Category::load(
143
                0,
144
                null,
145
                $courseCode,
146
                null,
147
                null,
148
                $sessionId,
149
                'ORDER BY id'
150
            );
151
        }
152
153
        $selectCat = (int) $cats[0]->get_id();
154
        $certificateListAux = [];
155
        if (!empty($selectCat)) {
156
            $certificateListAux = GradebookUtils::get_list_users_certificates($selectCat);
157
        }
158
159
        foreach ($certificateListAux as $value) {
0 ignored issues
show
Comprehensibility Bug introduced by
$value is overwriting a variable from outer foreach loop.
Loading history...
160
            $createdAt = strtotime(api_get_local_time($value['created_at']));
161
            $value['category_id'] = $selectCat;
162
            $value['c_id'] = $courseId;
163
            $value['course_code'] = $courseCode;
164
            switch ($filterDate) {
165
                case NO_DATE_FILTER:
166
                    $certificateList[] = $value;
167
                    break;
168
                case DATE_BEGIN_FILTER:
169
                    if ($createdAt >= $dateBegin) {
170
                        $certificateList[] = $value;
171
                    }
172
                    break;
173
                case DATE_END_FILTER:
174
                    if ($createdAt <= $dateEnd) {
175
                        $certificateList[] = $value;
176
                    }
177
                    break;
178
                case ALL_DATE_FILTER:
179
                    if ($createdAt >= $dateBegin && $createdAt <= $dateEnd) {
180
                        $certificateList[] = $value;
181
                    }
182
                    break;
183
            }
184
        }
185
186
        // Filter extra field
187
        foreach ($certificateList as $key => $value) {
188
            foreach ($filterCheckList as $fieldId => $field) {
189
                $extraFieldValue = new ExtraFieldValue('user');
190
                $extraFieldValueData = $extraFieldValue->get_values_by_handler_and_field_id(
191
                    $value['user_id'],
192
                    $fieldId
193
                );
194
195
                if (empty($extraFieldValueData)) {
196
                    unset($certificateList[$key]);
197
                    break;
198
                }
199
200
                switch ($field['field_type']) {
201
                    case ExtraField::FIELD_TYPE_TEXT:
202
                    case ExtraField::FIELD_TYPE_ALPHANUMERIC:
203
                        $pos = stripos($extraFieldValueData['value'], $_REQUEST['extra_'.$field['variable']]);
204
                        if ($pos === false) {
205
                            unset($certificateList[$key]);
206
                        }
207
                        break;
208
                    case ExtraField::FIELD_TYPE_RADIO:
209
                        $valueRadio = $_REQUEST['extra_'.$field['variable']]['extra_'.$field['variable']];
210
                        if ($extraFieldValueData['value'] != $valueRadio) {
211
                            unset($certificateList[$key]);
212
                        }
213
                        break;
214
                    case ExtraField::FIELD_TYPE_SELECT:
215
                        if ($extraFieldValueData['value'] != $_REQUEST['extra_'.$field['variable']]) {
216
                            unset($certificateList[$key]);
217
                        }
218
                        break;
219
                 }
220
            }
221
        }
222
    }
223
224
    $params = [
225
        'session_id' => (int) $_REQUEST['session_id'],
226
        'date_begin' => Security::remove_XSS($_REQUEST['date_begin']),
227
        'date_end' => Security::remove_XSS($_REQUEST['date_end']),
228
    ];
229
230
    foreach ($filterCheckList as $field) {
231
        $params['extra_'.$field['variable']] = Security::remove_XSS($_REQUEST['extra_'.$field['variable']]);
232
    }
233
    $urlParam = http_build_query($params);
234
235
    $dataToExport = [];
236
    if ($exportToCsv) {
237
        $headers = [
238
            get_lang('Session'),
239
            get_lang('Course'),
240
            get_lang('FirstName'),
241
            get_lang('LastName'),
242
            get_lang('Score'),
243
            get_lang('Date'),
244
        ];
245
246
        $extraField = new ExtraField('user');
247
        foreach ($extraFieldsAll as $field) {
248
            $headers[] = $field['display_text'];
249
        }
250
        $dataToExport[] = $headers;
251
252
        $sessionInfo = api_get_session_info($sessionId);
253
        foreach ($certificateList as $index => $value) {
254
            $categoryId = $value['category_id'];
255
            $courseCode = $value['course_code'];
256
            $courseInfo = api_get_course_info($courseCode);
257
            $extraFields = [];
258
            foreach ($extraFieldsAll as $field) {
259
                $extraFieldValue = new ExtraFieldValue('user');
260
                $extraFieldValueData = $extraFieldValue->get_values_by_handler_and_field_id(
261
                    $value['user_id'],
262
                    $field['id']
263
                );
264
                $fieldValue = isset($extraFieldValueData['value']) ? $extraFieldValueData['value'] : '';
265
                if ('true' === $fieldValue) {
266
                    $fieldValue = get_lang('Yes');
267
                }
268
                if ('false' === $fieldValue) {
269
                    $fieldValue = get_lang('No');
270
                }
271
                $extraFields[] = $fieldValue;
272
            }
273
274
            $list = GradebookUtils::get_list_gradebook_certificates_by_user_id($value['user_id'], $categoryId);
275
            foreach ($list as $valueCertificate) {
276
                $item = [];
277
                $item[] = $sessionInfo['name'];
278
                $item[] = $courseInfo['title'];
279
                $item[] = $value['firstname'];
280
                $item[] = $value['lastname'];
281
                $item[] = $valueCertificate['score_certificate'];
282
                $item[] = api_get_local_time($valueCertificate['created_at']);
283
                $item = array_merge($item, $extraFields);
284
                $dataToExport[] = $item;
285
            }
286
        }
287
        Export::arrayToCsv($dataToExport, 'export');
288
    }
289
}
290
291
$htmlHeadXtra[] = "<script>
292
    $(function () {
293
        $('#export_pdf').click(function(e) {
294
            e.preventDefault();
295
            e.stopPropagation();
296
297
            var session_id = $('#session-id').val();
298
            var date_begin = $('#date-begin').val();
299
            var date_end = $('#date-end').val();
300
301
            if (confirm('".$plugin->get_lang('OnlyCustomCertificates')."')) {
302
                var url = '".api_get_path(WEB_PLUGIN_PATH)."' +
303
                    'customcertificate/src/export_pdf_all_in_one.php?' +
304
                    '".$urlParam."&' +
305
                    'export_pdf=1';
306
307
                $(location).attr('href',url);
308
            }
309
        });
310
311
        $('#export_zip').click(function(e) {
312
            e.preventDefault();
313
            e.stopPropagation();
314
315
            var session_id = $('#session-id').val();
316
            var date_begin = $('#date-begin').val();
317
            var date_end = $('#date-end').val();
318
            if (confirm('".$plugin->get_lang('OnlyCustomCertificates')."')) {
319
                var url = '".api_get_path(WEB_PLUGIN_PATH)."' +
320
                    'customcertificate/src/export_pdf_all_in_one.php?' +
321
                    '".$urlParam."&' +
322
                    'export_zip=1';
323
324
                $(location).attr('href',url);
325
            }
326
        });
327
    });
328
</script>";
329
330
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('MySpace')];
331
Display::display_header(get_lang('CertificatesSessions'));
332
echo Display::page_header(get_lang('CertificatesSessions'));
333
$actions = '';
334
$actions .= Display::url(
335
    Display::return_icon('back.png', get_lang('Back'), [], 32),
336
    api_get_path(WEB_CODE_PATH).'mySpace'
337
);
338
339
if ($allowCustomCertificate) {
340
    $url = api_get_path(WEB_PLUGIN_PATH).'customcertificate/src/export_pdf_all_in_one.php';
341
    $actions .= Display::url(
342
        Display::return_icon('pdf.png', get_lang('ExportAllCertificatesToPDF'), [], ICON_SIZE_MEDIUM),
343
        $url,
344
        ['id' => 'export_pdf']
345
    );
346
347
    $actions .= Display::url(
348
        Display::return_icon('file_zip.png', get_lang('ExportAllCertificatesToZIP'), [], ICON_SIZE_MEDIUM),
349
        $url,
350
        ['id' => 'export_zip']
351
    );
352
}
353
354
echo Display::toolbarAction('actions', [$actions]);
355
echo $form->returnForm();
356
357
if (0 == count($certificateList)) {
358
    echo Display::return_message(get_lang('NoResultsAvailable'), 'warning');
359
} else {
360
    echo '<table class="table table-hover table-striped  data_table">';
361
    echo '<tbody>';
362
    foreach ($certificateList as $index => $value) {
363
        $categoryId = $value['category_id'];
364
        $courseCode = $value['course_code'];
365
        $courseInfo = api_get_course_info($courseCode);
366
        echo '<tr>';
367
        echo '<td width="50%" class="actions">';
368
        echo get_lang('Student').' : ';
369
        echo api_get_person_name($value['firstname'], $value['lastname']).' ('.$value['username'].')';
370
        echo '</td>';
371
        echo '<td width="50%" class="actions">'.$courseInfo['title'].'</td>';
372
        echo '</tr>';
373
        echo '<tr><td colspan="2">
374
            <table class="table table-hover table-striped  data_table">
375
                <tbody>';
376
377
        $list = GradebookUtils::get_list_gradebook_certificates_by_user_id($value['user_id'], $categoryId);
378
        foreach ($list as $valueCertificate) {
379
            echo '<tr>';
380
            echo '<td width="50%">'.get_lang('Score').' : '.$valueCertificate['score_certificate'].'</td>';
381
            echo '<td width="30%">';
382
            echo get_lang('Date').' : '.api_convert_and_format_date($valueCertificate['created_at']);
383
            echo '</td>';
384
            echo '<td width="20%">';
385
            $url = api_get_path(WEB_PATH).'certificates/index.php?'.
386
                'id='.$valueCertificate['id'].
387
                '&user_id='.$value['user_id'];
388
            $certificateUrl = Display::url(
389
                get_lang('Certificate'),
390
                $url,
391
                ['target' => '_blank', 'class' => 'btn btn-default']
392
            );
393
            echo $certificateUrl.PHP_EOL;
394
395
            $url .= '&action=export';
396
            $pdf = Display::url(
397
                Display::return_icon('pdf.png', get_lang('Download')),
398
                $url,
399
                ['target' => '_blank']
400
            );
401
            echo $pdf.PHP_EOL;
402
403
            echo '<a onclick="return confirmation();" href="gradebook_display_certificate.php?'.
404
                'sec_token='.$token.
405
                '&'.api_get_cidreq().
406
                '&action=delete'.
407
                '&cat_id='.$categoryId.
408
                '&certificate_id='.$valueCertificate['id'].'">
409
                    '.Display::return_icon('delete.png', get_lang('Delete')).'
410
                  </a>'.PHP_EOL;
411
            echo '</td></tr>';
412
        }
413
        echo '</tbody>';
414
        echo '</table>';
415
        echo '</td></tr>';
416
    }
417
    echo '</tbody>';
418
    echo '</table>';
419
}
420
421
Display::display_footer();
422