Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/exercise/exercise_result.class.php (1 issue)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Class ExerciseResult
7
 * which allows you to export exercises results in multiple presentation forms.
8
 *
9
 * @author Yannick Warnier
10
 */
11
class ExerciseResult
12
{
13
    public $includeAllUsers = false;
14
    public $onlyBestAttempts = false;
15
    private $results = [];
16
17
    /**
18
     * @param bool $includeAllUsers
19
     */
20
    public function setIncludeAllUsers($includeAllUsers)
21
    {
22
        $this->includeAllUsers = $includeAllUsers;
23
    }
24
25
    /**
26
     * @param bool $value
27
     */
28
    public function setOnlyBestAttempts($value)
29
    {
30
        $this->onlyBestAttempts = $value;
31
    }
32
33
    /**
34
     * Gets the results of all students (or just one student if access is limited).
35
     *
36
     * @param string $document_path The document path (for HotPotatoes retrieval)
37
     * @param int    $user_id       User ID. Optional. If no user ID is provided, we take all the results. Defauts to null
38
     * @param int    $filter
39
     * @param int    $exercise_id
40
     *
41
     * @return bool
42
     */
43
    public function getExercisesReporting(
44
        $document_path,
45
        $user_id = null,
46
        $filter = 0,
47
        $exercise_id = 0
48
    ) {
49
        $return = [];
50
        $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
51
        $TBL_TABLE_LP_MAIN = Database::get_course_table(TABLE_LP_MAIN);
52
        $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
53
        $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
54
        $TBL_TRACK_ATTEMPT_RECORDING = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
55
56
        $cid = api_get_course_id();
57
        $course_id = api_get_course_int_id();
58
        $user_id = (int) $user_id;
59
        $sessionId = api_get_session_id();
60
        $session_id_and = ' AND te.session_id = '.$sessionId.' ';
61
        $exercise_id = (int) $exercise_id;
62
63
        if (empty($sessionId) &&
64
            api_get_configuration_value('show_exercise_session_attempts_in_base_course')
65
        ) {
66
            $session_id_and = '';
67
        }
68
69
        if (!empty($exercise_id)) {
70
            $session_id_and .= " AND exe_exo_id = $exercise_id ";
71
        }
72
73
        if (empty($user_id)) {
74
            $user_id_and = null;
75
            $sql = "SELECT
76
                    firstname,
77
                    lastname,
78
                    official_code,
79
                    ce.title as extitle,
80
                    te.exe_result as exresult ,
81
                    te.exe_weighting as exweight,
82
                    te.exe_date as exdate,
83
                    te.exe_id as exid,
84
                    email as exemail,
85
                    te.start_date as exstart,
86
                    steps_counter as exstep,
87
                    exe_user_id as excruid,
88
                    te.exe_duration as duration,
89
                    te.orig_lp_id as orig_lp_id,
90
                    tlm.name as lp_name,
91
                    user.username,
92
                    te.status as exstatus
93
                FROM $TBL_EXERCISES  AS ce
94
                INNER JOIN $TBL_TRACK_EXERCISES AS te
95
                ON (te.exe_exo_id = ce.iid)
96
                INNER JOIN $TBL_USER AS user
97
                ON (user.user_id = exe_user_id)
98
                LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm
99
                ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id)
100
                WHERE
101
                    ce.c_id = $course_id AND
102
                    te.c_id = ce.c_id $user_id_and $session_id_and AND
103
                    ce.active <> -1";
104
        } else {
105
            $user_id_and = ' AND te.exe_user_id = '.api_get_user_id().' ';
106
            $orderBy = 'lastname';
107
            if (api_is_western_name_order()) {
108
                $orderBy = 'firstname';
109
            }
110
            // get only this user's results
111
            $sql = "SELECT
112
                        firstname,
113
                        lastname,
114
                        official_code,
115
                        ce.title as extitle,
116
                        te.exe_result as exresult,
117
                        te.exe_weighting as exweight,
118
                        te.exe_date as exdate,
119
                        te.exe_id as exid,
120
                        email as exemail,
121
                        te.start_date as exstart,
122
                        steps_counter as exstep,
123
                        exe_user_id as excruid,
124
                        te.exe_duration as duration,
125
                        ce.results_disabled as exdisabled,
126
                        te.orig_lp_id as orig_lp_id,
127
                        tlm.name as lp_name,
128
                        user.username,
129
                        te.status as exstatus
130
                    FROM $TBL_EXERCISES  AS ce
131
                    INNER JOIN $TBL_TRACK_EXERCISES AS te
132
                    ON (te.exe_exo_id = ce.iid)
133
                    INNER JOIN $TBL_USER AS user
134
                    ON (user.user_id = exe_user_id)
135
                    LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm
136
                    ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id)
137
                    WHERE
138
                        ce.c_id = $course_id AND
139
                        te.c_id = ce.c_id $user_id_and $session_id_and AND
140
                        ce.active <>-1 AND
141
                    ORDER BY $orderBy, te.c_id ASC, ce.title ASC, te.exe_date DESC";
142
        }
143
144
        $results = [];
145
        $resx = Database::query($sql);
146
        $bestAttemptPerUser = [];
147
        while ($rowx = Database::fetch_array($resx, 'ASSOC')) {
148
            if ($this->onlyBestAttempts) {
149
                if (!isset($bestAttemptPerUser[$rowx['excruid']])) {
150
                    $bestAttemptPerUser[$rowx['excruid']] = $rowx;
151
                } else {
152
                    if ($rowx['exresult'] > $bestAttemptPerUser[$rowx['excruid']]['exresult']) {
153
                        $bestAttemptPerUser[$rowx['excruid']] = $rowx;
154
                    }
155
                }
156
            } else {
157
                $results[] = $rowx;
158
            }
159
        }
160
161
        if ($this->onlyBestAttempts) {
162
            // Remove userId indexes to avoid issues in output
163
            foreach ($bestAttemptPerUser as $attempt) {
164
                $results[] = $attempt;
165
            }
166
        }
167
168
        $filter_by_not_revised = false;
169
        $filter_by_revised = false;
170
171
        if ($filter) {
172
            switch ($filter) {
173
                case 1:
174
                    $filter_by_not_revised = true;
175
                    break;
176
                case 2:
177
                    $filter_by_revised = true;
178
                    break;
179
                default:
180
                    null;
181
            }
182
        }
183
184
        if (empty($sessionId)) {
185
            $students = CourseManager::get_user_list_from_course_code($cid);
186
        } else {
187
            $students = CourseManager::get_user_list_from_course_code($cid, $sessionId);
188
        }
189
        $studentsUserIdList = array_keys($students);
190
191
        // Print the results of tests
192
        $userWithResults = [];
193
        if (is_array($results)) {
194
            $i = 0;
195
            foreach ($results as $result) {
196
                $revised = 0;
197
                if ($result['exstatus'] === 'incomplete') {
198
                    $revised = -1;
199
                } else {
200
                    //revised or not
201
                    $sql_exe = "SELECT exe_id
202
                                FROM $TBL_TRACK_ATTEMPT_RECORDING
203
                                WHERE
204
                                    author != '' AND
205
                                    exe_id = ".intval($result['exid'])."
206
                                LIMIT 1";
207
                    $query = Database::query($sql_exe);
208
209
                    if (Database:: num_rows($query) > 0) {
210
                        $revised = 1;
211
                    }
212
                }
213
214
                if ($filter_by_not_revised && $revised === 1) {
215
                    continue;
216
                }
217
218
                if ($filter_by_revised && $revised < 1) {
219
                    continue;
220
                }
221
222
                $return[$i] = [];
223
                if (empty($user_id)) {
224
                    $return[$i]['official_code'] = $result['official_code'];
225
                    $return[$i]['firstname'] = $results[$i]['firstname'];
226
                    $return[$i]['lastname'] = $results[$i]['lastname'];
227
                    $return[$i]['user_id'] = $results[$i]['excruid'];
228
                    $return[$i]['email'] = $results[$i]['exemail'];
229
                    $return[$i]['username'] = $results[$i]['username'];
230
                }
231
                $return[$i]['title'] = $result['extitle'];
232
                $return[$i]['start_date'] = api_get_local_time($result['exstart']);
233
                $return[$i]['end_date'] = api_get_local_time($result['exdate']);
234
                $return[$i]['duration'] = $result['duration'];
235
                $return[$i]['result'] = $result['exresult'];
236
                $return[$i]['max'] = $result['exweight'];
237
                // Revised: 1 = revised, 0 = not revised, -1 = not even finished by user
238
                $return[$i]['status'] = $revised === 1 ? get_lang('Validated') : ($revised === 0 ? get_lang('NotValidated') : get_lang('Unclosed'));
239
                $return[$i]['lp_id'] = $result['orig_lp_id'];
240
                $return[$i]['lp_name'] = $result['lp_name'];
241
242
                if (in_array($result['excruid'], $studentsUserIdList)) {
243
                    $return[$i]['is_user_subscribed'] = get_lang('Yes');
244
                } else {
245
                    $return[$i]['is_user_subscribed'] = get_lang('No');
246
                }
247
248
                $userWithResults[$result['excruid']] = 1;
249
                $i++;
250
            }
251
        }
252
253
        if ($this->includeAllUsers) {
254
            $latestId = count($return);
255
            $userWithResults = array_keys($userWithResults);
256
257
            if (!empty($students)) {
258
                foreach ($students as $student) {
259
                    if (!in_array($student['user_id'], $userWithResults)) {
260
                        $i = $latestId;
261
                        $isWestern = api_is_western_name_order();
262
263
                        if (empty($user_id)) {
264
                            $return[$i]['official_code'] = $student['official_code'];
265
                            $return[$i]['firstname'] = $student['firstname'];
266
                            $return[$i]['lastname'] = $student['lastname'];
267
                            $return[$i]['user_id'] = $student['user_id'];
268
                            $return[$i]['email'] = $student['email'];
269
                            $return[$i]['username'] = $student['username'];
270
                        }
271
                        $return[$i]['title'] = null;
272
                        $return[$i]['start_date'] = null;
273
                        $return[$i]['end_date'] = null;
274
                        $return[$i]['duration'] = null;
275
                        $return[$i]['result'] = null;
276
                        $return[$i]['max'] = null;
277
                        $return[$i]['status'] = get_lang('NotAttempted');
278
                        $return[$i]['lp_id'] = null;
279
                        $return[$i]['lp_name'] = null;
280
                        $return[$i]['is_user_subscribed'] = get_lang('Yes');
281
282
                        $latestId++;
283
                    }
284
                }
285
            }
286
        }
287
288
        $this->results = $return;
289
290
        return true;
291
    }
292
293
    /**
294
     * Exports the complete report as a CSV file.
295
     *
296
     * @param string $document_path      Document path inside the document tool
297
     * @param int    $user_id            Optional user ID
298
     * @param bool   $export_user_fields Whether to include user fields or not
299
     * @param int    $export_filter
300
     * @param int    $exercise_id
301
     *
302
     * @return bool False on error
303
     */
304
    public function exportCompleteReportCSV(
305
        $document_path = '',
306
        $user_id = null,
307
        $export_user_fields = false,
308
        $export_filter = 0,
309
        $exercise_id = 0
310
    ) {
311
        global $charset;
312
        $this->getExercisesReporting(
313
            $document_path,
314
            $user_id,
315
            $export_filter,
316
            $exercise_id
317
        );
318
        $now = api_get_local_time();
319
        $filename = 'exercise_results_'.$now.'.csv';
320
        if (!empty($user_id)) {
321
            $filename = 'exercise_results_user_'.$user_id.'_'.$now.'.csv';
322
        }
323
324
        $filename = api_replace_dangerous_char($filename);
325
        $data = '';
326
        if (api_is_western_name_order()) {
327
            if (!empty($this->results[0]['firstname'])) {
328
                $data .= get_lang('FirstName').';';
329
            }
330
            if (!empty($this->results[0]['lastname'])) {
331
                $data .= get_lang('LastName').';';
332
            }
333
        } else {
334
            if (!empty($this->results[0]['lastname'])) {
335
                $data .= get_lang('LastName').';';
336
            }
337
            if (!empty($this->results[0]['firstname'])) {
338
                $data .= get_lang('FirstName').';';
339
            }
340
        }
341
        $officialCodeInList = api_get_setting('show_official_code_exercise_result_list');
342
        if ($officialCodeInList === 'true') {
343
            $data .= get_lang('OfficialCode').';';
344
        }
345
346
        $data .= get_lang('LoginName').';';
347
        $data .= get_lang('Email').';';
348
        $data .= get_lang('Groups').';';
349
350
        if ($export_user_fields) {
351
            //show user fields section with a big th colspan that spans over all fields
352
            $extra_user_fields = UserManager::get_extra_fields(
353
                0,
354
                1000,
355
                5,
356
                'ASC',
357
                false,
358
                1
359
            );
360
            if (!empty($extra_user_fields)) {
361
                foreach ($extra_user_fields as $field) {
362
                    $data .= '"'.str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset)).'";';
363
                }
364
            }
365
        }
366
367
        $data .= get_lang('Title').';';
368
        $data .= get_lang('StartDate').';';
369
        $data .= get_lang('EndDate').';';
370
        $data .= get_lang('Duration').' ('.get_lang('MinMinutes').') ;';
371
        $data .= get_lang('Score').';';
372
        $data .= get_lang('Total').';';
373
        $data .= get_lang('Status').';';
374
        $data .= get_lang('ToolLearnpath').';';
375
        $data .= get_lang('UserIsCurrentlySubscribed').';';
376
        $data .= get_lang('CourseCode').';';
377
        $data .= "\n";
378
379
        //results
380
        foreach ($this->results as $row) {
381
            if (api_is_western_name_order()) {
382
                $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['firstname']), ENT_QUOTES, $charset)).';';
383
                $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['lastname']), ENT_QUOTES, $charset)).';';
384
            } else {
385
                $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['lastname']), ENT_QUOTES, $charset)).';';
386
                $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['firstname']), ENT_QUOTES, $charset)).';';
387
            }
388
389
            // Official code
390
            if ($officialCodeInList === 'true') {
391
                $data .= $row['official_code'].';';
392
            }
393
394
            // Email
395
            $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['username']), ENT_QUOTES, $charset)).';';
396
            $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset)).';';
397
            $data .= str_replace("\r\n", '  ', implode(", ", GroupManager::get_user_group_name($row['user_id']))).';';
398
399
            if ($export_user_fields) {
400
                //show user fields data, if any, for this user
401
                $user_fields_values = UserManager::get_extra_user_data(
402
                    $row['user_id'],
403
                    false,
404
                    false,
405
                    false,
406
                    true
407
                );
408
                if (!empty($user_fields_values)) {
409
                    foreach ($user_fields_values as $value) {
410
                        $data .= '"'.str_replace('"', '""', api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)).'";';
411
                    }
412
                }
413
            }
414
            $duration = !empty($row['duration']) ? round($row['duration'] / 60) : 0;
415
416
            $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)).';';
417
            $data .= str_replace("\r\n", '  ', $row['start_date']).';';
418
            $data .= str_replace("\r\n", '  ', $row['end_date']).';';
419
            $data .= str_replace("\r\n", '  ', $duration).';';
420
            $data .= str_replace("\r\n", '  ', $row['result']).';';
421
            $data .= str_replace("\r\n", '  ', $row['max']).';';
422
            $data .= str_replace("\r\n", '  ', $row['status']).';';
423
            $data .= str_replace("\r\n", '  ', $row['lp_name']).';';
424
            $data .= str_replace("\r\n", '  ', $row['is_user_subscribed']).';';
425
            $data .= str_replace("\r\n", '  ', api_get_course_id()).';';
426
427
            $data .= "\n";
428
        }
429
430
        // output the results
431
        $len = strlen($data);
432
        header('Content-type: application/octet-stream');
433
        header('Content-Type: application/force-download');
434
        header('Content-length: '.$len);
435
        if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
436
            header('Content-Disposition: filename= '.$filename);
437
        } else {
438
            header('Content-Disposition: attachment; filename= '.$filename);
439
        }
440
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
441
            header('Pragma: ');
442
            header('Cache-Control: ');
443
            header('Cache-Control: public'); // IE cannot download from sessions without a cache
444
        }
445
        header('Content-Description: '.$filename);
446
        header('Content-transfer-encoding: binary');
447
        echo $data;
448
449
        return true;
450
    }
451
452
    /**
453
     * Exports the complete report as an XLS file.
454
     *
455
     * @param string $document_path
456
     * @param null   $user_id
457
     * @param bool   $export_user_fields
458
     * @param int    $export_filter
459
     * @param int    $exercise_id
460
     * @param null   $hotpotato_name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $hotpotato_name is correct as it would always require null to be passed?
Loading history...
461
     *
462
     * @return bool
463
     */
464
    public function exportCompleteReportXLS(
465
        $document_path = '',
466
        $user_id = null,
467
        $export_user_fields = false,
468
        $export_filter = 0,
469
        $exercise_id = 0,
470
        $hotpotato_name = null
471
    ) {
472
        global $charset;
473
        $this->getExercisesReporting(
474
            $document_path,
475
            $user_id,
476
            $export_filter,
477
            $exercise_id,
478
            $hotpotato_name
479
        );
480
        $now = api_get_local_time();
481
        $filename = 'exercise_results_'.$now.'.xlsx';
482
        if (!empty($user_id)) {
483
            $filename = 'exercise_results_user_'.$user_id.'_'.$now.'.xlsx';
484
        }
485
486
        $spreadsheet = new PHPExcel();
487
        $spreadsheet->setActiveSheetIndex(0);
488
        $worksheet = $spreadsheet->getActiveSheet();
489
490
        $line = 1; // Skip first line
491
        $column = 0; //skip the first column (row titles)
492
493
        // check if exists column 'user'
494
        $with_column_user = false;
495
        foreach ($this->results as $result) {
496
            if (!empty($result['lastname']) && !empty($result['firstname'])) {
497
                $with_column_user = true;
498
                break;
499
            }
500
        }
501
502
        $officialCodeInList = api_get_setting('show_official_code_exercise_result_list');
503
504
        if ($with_column_user) {
505
            if (api_is_western_name_order()) {
506
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName'));
507
                $column++;
508
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName'));
509
                $column++;
510
            } else {
511
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName'));
512
                $column++;
513
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName'));
514
                $column++;
515
            }
516
517
            if ($officialCodeInList === 'true') {
518
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('OfficialCode'));
519
                $column++;
520
            }
521
522
            $worksheet->setCellValueByColumnAndRow($column++, $line, get_lang('LoginName'));
523
            $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Email'));
524
            $column++;
525
        }
526
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Groups'));
527
        $column++;
528
529
        if ($export_user_fields) {
530
            //show user fields section with a big th colspan that spans over all fields
531
            $extra_user_fields = UserManager::get_extra_fields(
532
                0,
533
                1000,
534
                5,
535
                'ASC',
536
                false,
537
                1
538
            );
539
540
            //show the fields names for user fields
541
            foreach ($extra_user_fields as $field) {
542
                $worksheet->setCellValueByColumnAndRow(
543
                    $column,
544
                    $line,
545
                    api_html_entity_decode(
546
                        strip_tags($field[3]),
547
                        ENT_QUOTES,
548
                        $charset
549
                    )
550
                );
551
                $column++;
552
            }
553
        }
554
555
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Title'));
556
        $column++;
557
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('StartDate'));
558
        $column++;
559
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('EndDate'));
560
        $column++;
561
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Duration').' ('.get_lang('MinMinutes').')');
562
        $column++;
563
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Score'));
564
        $column++;
565
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Total'));
566
        $column++;
567
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Status'));
568
        $column++;
569
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('ToolLearnpath'));
570
        $column++;
571
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('UserIsCurrentlySubscribed'));
572
        $column++;
573
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('CourseCode'));
574
        $line++;
575
576
        foreach ($this->results as $row) {
577
            $column = 0;
578
            if ($with_column_user) {
579
                if (api_is_western_name_order()) {
580
                    $worksheet->setCellValueByColumnAndRow(
581
                        $column,
582
                        $line,
583
                        api_html_entity_decode(
584
                            strip_tags($row['firstname']),
585
                            ENT_QUOTES,
586
                            $charset
587
                        )
588
                    );
589
                    $column++;
590
                    $worksheet->setCellValueByColumnAndRow(
591
                        $column,
592
                        $line,
593
                        api_html_entity_decode(
594
                            strip_tags($row['lastname']),
595
                            ENT_QUOTES,
596
                            $charset
597
                        )
598
                    );
599
                    $column++;
600
                } else {
601
                    $worksheet->setCellValueByColumnAndRow(
602
                        $column,
603
                        $line,
604
                        api_html_entity_decode(
605
                            strip_tags($row['lastname']),
606
                            ENT_QUOTES,
607
                            $charset
608
                        )
609
                    );
610
                    $column++;
611
                    $worksheet->setCellValueByColumnAndRow(
612
                        $column,
613
                        $line,
614
                        api_html_entity_decode(
615
                            strip_tags($row['firstname']),
616
                            ENT_QUOTES,
617
                            $charset
618
                        )
619
                    );
620
                    $column++;
621
                }
622
623
                if ($officialCodeInList === 'true') {
624
                    $worksheet->setCellValueByColumnAndRow(
625
                        $column,
626
                        $line,
627
                        api_html_entity_decode(
628
                            strip_tags($row['official_code']),
629
                            ENT_QUOTES,
630
                            $charset
631
                        )
632
                    );
633
                    $column++;
634
                }
635
636
                $worksheet->setCellValueByColumnAndRow(
637
                    $column++,
638
                    $line,
639
                    api_html_entity_decode(
640
                        strip_tags($row['username']),
641
                        ENT_QUOTES,
642
                        $charset
643
                    )
644
                );
645
                $worksheet->setCellValueByColumnAndRow(
646
                    $column,
647
                    $line,
648
                    api_html_entity_decode(
649
                        strip_tags($row['email']),
650
                        ENT_QUOTES,
651
                        $charset
652
                    )
653
                );
654
                $column++;
655
            }
656
657
            $worksheet->setCellValueByColumnAndRow(
658
                $column,
659
                $line,
660
                api_html_entity_decode(
661
                    strip_tags(
662
                        implode(
663
                            ", ",
664
                            GroupManager:: get_user_group_name($row['user_id'])
665
                        )
666
                    ),
667
                    ENT_QUOTES,
668
                    $charset
669
                )
670
            );
671
            $column++;
672
673
            if ($export_user_fields) {
674
                //show user fields data, if any, for this user
675
                $user_fields_values = UserManager::get_extra_user_data(
676
                    $row['user_id'],
677
                    false,
678
                    false,
679
                    false,
680
                    true
681
                );
682
                foreach ($user_fields_values as $value) {
683
                    $worksheet->setCellValueByColumnAndRow(
684
                        $column,
685
                        $line,
686
                        api_html_entity_decode(
687
                            strip_tags($value),
688
                            ENT_QUOTES,
689
                            $charset
690
                        )
691
                    );
692
                    $column++;
693
                }
694
            }
695
696
            $worksheet->setCellValueByColumnAndRow(
697
                $column,
698
                $line,
699
                api_html_entity_decode(
700
                    strip_tags($row['title']),
701
                    ENT_QUOTES,
702
                    $charset
703
                )
704
            );
705
706
            $column++;
707
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['start_date']);
708
            $column++;
709
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['end_date']);
710
            $column++;
711
            $duration = !empty($row['duration']) ? round($row['duration'] / 60) : 0;
712
            $worksheet->setCellValueByColumnAndRow($column, $line, $duration);
713
            $column++;
714
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['result']);
715
            $column++;
716
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['max']);
717
            $column++;
718
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['status']);
719
            $column++;
720
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['lp_name']);
721
            $column++;
722
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['is_user_subscribed']);
723
            $column++;
724
            $worksheet->setCellValueByColumnAndRow($column, $line, api_get_course_id());
725
            $line++;
726
        }
727
728
        $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename);
729
        $writer = new PHPExcel_Writer_Excel2007($spreadsheet);
730
        $writer->save($file);
731
        DocumentManager::file_send_for_download($file, true, $filename);
732
733
        return true;
734
    }
735
}
736