Completed
Push — master ( 27e209...a08afa )
by Julito
186:04 queued 150:53
created

ExerciseResult::exportCompleteReportXLS()   F

Complexity

Conditions 17
Paths 1260

Size

Total Lines 256
Code Lines 183

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 183
nc 1260
nop 6
dl 0
loc 256
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * Class ExerciseResult
6
 * which allows you to export exercises results in multiple presentation forms
7
 * @package chamilo.exercise
8
 * @author Yannick Warnier
9
*/
10
class ExerciseResult
11
{
12
    private $results = [];
13
    public $includeAllUsers = false;
14
    public $onlyBestAttempts = false;
15
16
    /**
17
     * @param bool $includeAllUsers
18
     */
19
    public function setIncludeAllUsers($includeAllUsers)
20
    {
21
        $this->includeAllUsers = $includeAllUsers;
22
    }
23
24
    /**
25
     * @param bool $value
26
     */
27
    public function setOnlyBestAttempts($value)
28
    {
29
        $this->onlyBestAttempts = $value;
30
    }
31
32
    /**
33
     * Gets the results of all students (or just one student if access is limited)
34
     *
35
     * @param string $document_path The document path (for HotPotatoes retrieval)
36
     * @param int $user_id User ID. Optional. If no user ID is provided, we take all the results. Defauts to null
37
     * @param int $filter
38
     * @param int $exercise_id
39
     *
40
     * @return bool
41
     */
42
    public function getExercisesReporting(
43
        $document_path,
0 ignored issues
show
Unused Code introduced by
The parameter $document_path is not used and could be removed. ( Ignorable by Annotation )

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

43
        /** @scrutinizer ignore-unused */ $document_path,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
        $user_id = null,
45
        $filter = 0,
46
        $exercise_id = 0
47
    ) {
48
        $return = [];
49
        $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
50
        $TBL_TABLE_LP_MAIN = Database::get_course_table(TABLE_LP_MAIN);
51
        $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
52
        $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
53
        $TBL_TRACK_ATTEMPT_RECORDING = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
54
55
        $cid = api_get_course_id();
56
        $course_id = api_get_course_int_id();
57
        $user_id = intval($user_id);
58
        $sessionId = api_get_session_id();
59
        $session_id_and = ' AND te.session_id = '.$sessionId.' ';
60
        $exercise_id = intval($exercise_id);
61
62
        if (!empty($exercise_id)) {
63
            $session_id_and .= " AND exe_exo_id = $exercise_id ";
64
        }
65
66
        if (empty($user_id)) {
67
            $user_id_and = null;
68
            $sql = "SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").",
69
                    official_code,
70
                    ce.title as extitle,
71
                    te.exe_result as exresult ,
72
                    te.exe_weighting as exweight,
73
                    te.exe_date as exdate,
74
                    te.exe_id as exid,
75
                    email as exemail,
76
                    te.start_date as exstart,
77
                    steps_counter as exstep,
78
                    exe_user_id as excruid,
79
                    te.exe_duration as duration,
80
                    te.orig_lp_id as orig_lp_id,
81
                    tlm.name as lp_name
82
                FROM $TBL_EXERCISES  AS ce
83
                INNER JOIN $TBL_TRACK_EXERCISES AS te 
84
                ON (te.exe_exo_id = ce.id)
85
                INNER JOIN $TBL_USER AS user 
86
                ON (user.user_id = exe_user_id)
87
                LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm 
88
                ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id)
89
                WHERE
90
                    ce.c_id = $course_id AND
91
                    te.status != 'incomplete' AND
92
                    te.c_id = ce.c_id $user_id_and  $session_id_and AND
93
                    ce.active <>-1";
94
        } else {
95
            $user_id_and = ' AND te.exe_user_id = '.api_get_user_id().' ';
96
            // get only this user's results
97
            $sql = "SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").",
98
                        official_code,
99
                        ce.title as extitle,
100
                        te.exe_result as exresult,
101
                        te.exe_weighting as exweight,
102
                        te.exe_date as exdate,
103
                        te.exe_id as exid,
104
                        email as exemail,
105
                        te.start_date as exstart,
106
                        steps_counter as exstep,
107
                        exe_user_id as excruid,
108
                        te.exe_duration as duration,
109
                        ce.results_disabled as exdisabled,
110
                        te.orig_lp_id as orig_lp_id,
111
                        tlm.name as lp_name
112
                    FROM $TBL_EXERCISES  AS ce
113
                    INNER JOIN $TBL_TRACK_EXERCISES AS te 
114
                    ON (te.exe_exo_id = ce.id)
115
                    INNER JOIN $TBL_USER AS user 
116
                    ON (user.user_id = exe_user_id)
117
                    LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm 
118
                    ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id)
119
                    WHERE
120
                        ce.c_id = $course_id AND
121
                        te.status != 'incomplete' AND
122
                        te.c_id = ce.c_id $user_id_and $session_id_and AND
123
                        ce.active <>-1 AND
124
                    ORDER BY userpart2, te.c_id ASC, ce.title ASC, te.exe_date DESC";
125
        }
126
127
        $results = [];
128
        $resx = Database::query($sql);
129
        $bestAttemptPerUser = [];
130
        while ($rowx = Database::fetch_array($resx, 'ASSOC')) {
131
            if ($this->onlyBestAttempts) {
132
                if (!isset($bestAttemptPerUser[$rowx['excruid']])) {
133
                    $bestAttemptPerUser[$rowx['excruid']] = $rowx;
134
                } else {
135
                    if ($rowx['exresult'] > $bestAttemptPerUser[$rowx['excruid']]['exresult']) {
136
                        $bestAttemptPerUser[$rowx['excruid']] = $rowx;
137
                    }
138
                }
139
            } else {
140
                $results[] = $rowx;
141
            }
142
        }
143
144
        if ($this->onlyBestAttempts) {
145
            // Remove userId indexes to avoid issues in output
146
            foreach ($bestAttemptPerUser as $attempt) {
147
                $results[] = $attempt;
148
            }
149
        }
150
151
        $filter_by_not_revised = false;
152
        $filter_by_revised = false;
153
154
        if ($filter) {
155
            switch ($filter) {
156
                case 1:
157
                    $filter_by_not_revised = true;
158
                    break;
159
                case 2:
160
                    $filter_by_revised = true;
161
                    break;
162
                default:
163
                    null;
164
            }
165
        }
166
167
        if (empty($sessionId)) {
168
            $students = CourseManager::get_user_list_from_course_code($cid);
169
        } else {
170
            $students = CourseManager::get_user_list_from_course_code($cid, $sessionId);
171
        }
172
        $studentsUserIdList = array_keys($students);
0 ignored issues
show
Bug introduced by
It seems like $students can also be of type integer; however, parameter $input of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

172
        $studentsUserIdList = array_keys(/** @scrutinizer ignore-type */ $students);
Loading history...
173
174
        // Print the results of tests
175
        $userWithResults = [];
176
        if (is_array($results)) {
177
            $i = 0;
178
            foreach ($results as $result) {
179
                $revised = false;
180
                //revised or not
181
                $sql_exe = "SELECT exe_id 
182
                            FROM $TBL_TRACK_ATTEMPT_RECORDING
183
                            WHERE 
184
                                author != '' AND 
185
                                exe_id = ".Database::escape_string($result['exid'])."
186
                            LIMIT 1";
187
                $query = Database::query($sql_exe);
188
189
                if (Database:: num_rows($query) > 0) {
190
                    $revised = true;
191
                }
192
193
                if ($filter_by_not_revised && $revised) {
194
                    continue;
195
                }
196
197
                if ($filter_by_revised && !$revised) {
198
                    continue;
199
                }
200
201
                $return[$i] = [];
202
                if (empty($user_id)) {
203
                    $return[$i]['official_code'] = $result['official_code'];
204
                    if (api_is_western_name_order()) {
205
                        $return[$i]['first_name'] = $results[$i]['userpart1'];
206
                        $return[$i]['last_name'] = $results[$i]['userpart2'];
207
                    } else {
208
                        $return[$i]['first_name'] = $results[$i]['userpart2'];
209
                        $return[$i]['last_name'] = $results[$i]['userpart1'];
210
                    }
211
                    $return[$i]['user_id'] = $results[$i]['excruid'];
212
                    $return[$i]['email'] = $results[$i]['exemail'];
213
                }
214
                $return[$i]['title'] = $result['extitle'];
215
                $return[$i]['start_date'] = api_get_local_time($result['exstart']);
216
                $return[$i]['end_date'] = api_get_local_time($result['exdate']);
217
                $return[$i]['duration'] = $result['duration'];
218
                $return[$i]['result'] = $result['exresult'];
219
                $return[$i]['max'] = $result['exweight'];
220
                $return[$i]['status'] = $revised ? get_lang('Validated') : get_lang('NotValidated');
221
                $return[$i]['lp_id'] = $result['orig_lp_id'];
222
                $return[$i]['lp_name'] = $result['lp_name'];
223
224
                if (in_array($result['excruid'], $studentsUserIdList)) {
225
                    $return[$i]['is_user_subscribed'] = get_lang('Yes');
226
                } else {
227
                    $return[$i]['is_user_subscribed'] = get_lang('No');
228
                }
229
230
                $userWithResults[$result['excruid']] = 1;
231
                $i++;
232
            }
233
        }
234
235
        if ($this->includeAllUsers) {
236
            $latestId = count($return);
237
            $userWithResults = array_keys($userWithResults);
238
239
            if (!empty($students)) {
240
                foreach ($students as $student) {
241
                    if (!in_array($student['user_id'], $userWithResults)) {
242
                        $i = $latestId;
243
                        $isWestern = api_is_western_name_order();
244
245
                        if (empty($user_id)) {
246
                            $return[$i]['official_code'] = $student['official_code'];
247
                            if ($isWestern) {
248
                                $return[$i]['first_name'] = $student['firstname'];
249
                                $return[$i]['last_name'] = $student['lastname'];
250
                            } else {
251
                                $return[$i]['first_name'] = $student['lastname'];
252
                                $return[$i]['last_name'] = $student['firstname'];
253
                            }
254
255
                            $return[$i]['user_id'] = $student['user_id'];
256
                            $return[$i]['email'] = $student['email'];
257
                        }
258
                        $return[$i]['title'] = null;
259
                        $return[$i]['start_date'] = null;
260
                        $return[$i]['end_date'] = null;
261
                        $return[$i]['duration'] = null;
262
                        $return[$i]['result'] = null;
263
                        $return[$i]['max'] = null;
264
                        $return[$i]['status'] = get_lang('NotAttempted');
265
                        $return[$i]['lp_id'] = null;
266
                        $return[$i]['lp_name'] = null;
267
                        $return[$i]['is_user_subscribed'] = get_lang('Yes');
268
269
                        $latestId++;
270
                    }
271
                }
272
            }
273
        }
274
275
        $this->results = $return;
276
277
        return true;
278
    }
279
280
    /**
281
     * Exports the complete report as a CSV file
282
     * @param    string $document_path Document path inside the document tool
283
     * @param    integer $user_id Optional user ID
284
     * @param    boolean $export_user_fields Whether to include user fields or not
285
     * @param    int $export_filter
286
     * @param    int $exercise_id
287
     *
288
     * @return  boolean False on error
289
     */
290
    public function exportCompleteReportCSV(
291
        $document_path = '',
292
        $user_id = null,
293
        $export_user_fields = false,
294
        $export_filter = 0,
295
        $exercise_id = 0
296
    ) {
297
        global $charset;
298
        $this->getExercisesReporting(
299
            $document_path,
300
            $user_id,
301
            $export_filter,
302
            $exercise_id
303
        );
304
        $now = api_get_local_time();
305
        $filename = 'exercise_results_'.$now.'.csv';
306
        if (!empty($user_id)) {
307
            $filename = 'exercise_results_user_'.$user_id.'_'.$now.'.csv';
308
        }
309
310
        $filename = api_replace_dangerous_char($filename);
311
        $data = '';
312
        if (api_is_western_name_order()) {
313
            if (!empty($this->results[0]['first_name'])) {
314
                $data .= get_lang('FirstName').';';
315
            }
316
            if (!empty($this->results[0]['last_name'])) {
317
                $data .= get_lang('LastName').';';
318
            }
319
        } else {
320
            if (!empty($this->results[0]['last_name'])) {
321
                $data .= get_lang('LastName').';';
322
            }
323
            if (!empty($this->results[0]['first_name'])) {
324
                $data .= get_lang('FirstName').';';
325
            }
326
        }
327
        $officialCodeInList = api_get_setting('show_official_code_exercise_result_list');
328
        if ($officialCodeInList === 'true') {
329
            $data .= get_lang('OfficialCode').';';
330
        }
331
332
        $data .= get_lang('Email').';';
333
        $data .= get_lang('Groups').';';
334
335
        if ($export_user_fields) {
336
            //show user fields section with a big th colspan that spans over all fields
337
            $extra_user_fields = UserManager::get_extra_fields(
338
                0,
339
                1000,
340
                5,
341
                'ASC',
342
                false,
343
                1
344
            );
345
            if (!empty($extra_user_fields)) {
346
                foreach ($extra_user_fields as $field) {
347
                    $data .= '"'.str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset)).'";';
348
                }
349
            }
350
        }
351
352
        $data .= get_lang('Title').';';
353
        $data .= get_lang('StartDate').';';
354
        $data .= get_lang('EndDate').';';
355
        $data .= get_lang('Duration').' ('.get_lang('MinMinutes').') ;';
356
        $data .= get_lang('Score').';';
357
        $data .= get_lang('Total').';';
358
        $data .= get_lang('Status').';';
359
        $data .= get_lang('ToolLearnpath').';';
360
        $data .= get_lang('UserIsCurrentlySubscribed').';';
361
        $data .= "\n";
362
363
        //results
364
        foreach ($this->results as $row) {
365
            if (api_is_western_name_order()) {
366
                $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
367
                $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
368
            } else {
369
                $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
370
                $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
371
            }
372
373
            // Official code
374
            if ($officialCodeInList === 'true') {
375
                $data .= $row['official_code'].';';
376
            }
377
378
            // Email
379
            $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset)).';';
380
            $data .= str_replace("\r\n", '  ', implode(", ", GroupManager::get_user_group_name($row['user_id']))).';';
381
382
            if ($export_user_fields) {
383
                //show user fields data, if any, for this user
384
                $user_fields_values = UserManager::get_extra_user_data(
385
                    $row['user_id'],
386
                    false,
387
                    false,
388
                    false,
389
                    true
390
                );
391
                if (!empty($user_fields_values)) {
392
                    foreach ($user_fields_values as $value) {
393
                        $data .= '"'.str_replace('"', '""', api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)).'";';
394
                    }
395
                }
396
            }
397
            $duration = !empty($row['duration']) ? round($row['duration'] / 60) : 0;
398
399
            $data .= str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)).';';
400
            $data .= str_replace("\r\n", '  ', $row['start_date']).';';
401
            $data .= str_replace("\r\n", '  ', $row['end_date']).';';
402
            $data .= str_replace("\r\n", '  ', $duration).';';
403
            $data .= str_replace("\r\n", '  ', $row['result']).';';
404
            $data .= str_replace("\r\n", '  ', $row['max']).';';
405
            $data .= str_replace("\r\n", '  ', $row['status']).';';
406
            $data .= str_replace("\r\n", '  ', $row['lp_name']).';';
407
            $data .= str_replace("\r\n", '  ', $row['is_user_subscribed']).';';
408
            $data .= "\n";
409
        }
410
411
        // output the results
412
        $len = strlen($data);
413
        header('Content-type: application/octet-stream');
414
        header('Content-Type: application/force-download');
415
        header('Content-length: '.$len);
416
        if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
417
            header('Content-Disposition: filename= '.$filename);
418
        } else {
419
            header('Content-Disposition: attachment; filename= '.$filename);
420
        }
421
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
422
            header('Pragma: ');
423
            header('Cache-Control: ');
424
            header('Cache-Control: public'); // IE cannot download from sessions without a cache
425
        }
426
        header('Content-Description: '.$filename);
427
        header('Content-transfer-encoding: binary');
428
        echo $data;
429
        return true;
430
    }
431
432
    /**
433
     * Exports the complete report as an XLS file
434
     *
435
     * @param string $document_path
436
     * @param null $user_id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $user_id is correct as it would always require null to be passed?
Loading history...
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...
437
     * @param bool $export_user_fields
438
     * @param int $export_filter
439
     * @param int $exercise_id
440
     * @param null $hotpotato_name
441
     * @return bool
442
     */
443
    public function exportCompleteReportXLS(
444
        $document_path = '',
445
        $user_id = null,
446
        $export_user_fields = false,
447
        $export_filter = 0,
448
        $exercise_id = 0,
449
        $hotpotato_name = null
450
    ) {
451
        global $charset;
452
        $this->getExercisesReporting(
453
            $document_path,
454
            $user_id,
455
            $export_filter,
456
            $exercise_id,
457
            $hotpotato_name
458
        );
459
        $now = api_get_local_time();
460
        $filename = 'exercise_results_'.$now.'.xlsx';
461
        if (!empty($user_id)) {
462
            $filename = 'exercise_results_user_'.$user_id.'_'.$now.'.xlsx';
463
        }
464
465
        $spreadsheet = new PHPExcel();
466
        $spreadsheet->setActiveSheetIndex(0);
467
        $worksheet = $spreadsheet->getActiveSheet();
468
469
        $line = 1; // Skip first line
470
        $column = 0; //skip the first column (row titles)
471
472
        // check if exists column 'user'
473
        $with_column_user = false;
474
        foreach ($this->results as $result) {
475
            if (!empty($result['last_name']) && !empty($result['first_name'])) {
476
                $with_column_user = true;
477
                break;
478
            }
479
        }
480
481
        $officialCodeInList = api_get_setting('show_official_code_exercise_result_list');
482
483
        if ($with_column_user) {
484
            if (api_is_western_name_order()) {
485
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName'));
486
                $column++;
487
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName'));
488
                $column++;
489
            } else {
490
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName'));
491
                $column++;
492
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName'));
493
                $column++;
494
            }
495
496
            if ($officialCodeInList === 'true') {
497
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('OfficialCode'));
498
                $column++;
499
            }
500
501
            $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Email'));
502
            $column++;
503
        }
504
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Groups'));
505
        $column++;
506
507
        if ($export_user_fields) {
508
            //show user fields section with a big th colspan that spans over all fields
509
            $extra_user_fields = UserManager::get_extra_fields(
510
                0,
511
                1000,
512
                5,
513
                'ASC',
514
                false,
515
                1
516
            );
517
518
            //show the fields names for user fields
519
            foreach ($extra_user_fields as $field) {
520
                $worksheet->setCellValueByColumnAndRow(
521
                    $column,
522
                    $line,
523
                    api_html_entity_decode(
524
                        strip_tags($field[3]),
525
                        ENT_QUOTES,
526
                        $charset
527
                    )
528
                );
529
                $column++;
530
            }
531
        }
532
533
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Title'));
534
        $column++;
535
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('StartDate'));
536
        $column++;
537
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('EndDate'));
538
        $column++;
539
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Duration').' ('.get_lang('MinMinutes').')');
540
        $column++;
541
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Score'));
542
        $column++;
543
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Total'));
544
        $column++;
545
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Status'));
546
        $column++;
547
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('ToolLearnpath'));
548
        $column++;
549
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('UserIsCurrentlySubscribed'));
550
        $line++;
551
552
        foreach ($this->results as $row) {
553
            $column = 0;
554
            if ($with_column_user) {
555
                if (api_is_western_name_order()) {
556
                    $worksheet->setCellValueByColumnAndRow(
557
                        $column,
558
                        $line,
559
                        api_html_entity_decode(
560
                            strip_tags($row['first_name']),
561
                            ENT_QUOTES,
562
                            $charset
563
                        )
564
                    );
565
                    $column++;
566
                    $worksheet->setCellValueByColumnAndRow(
567
                        $column,
568
                        $line,
569
                        api_html_entity_decode(
570
                            strip_tags($row['last_name']),
571
                            ENT_QUOTES,
572
                            $charset
573
                        )
574
                    );
575
                    $column++;
576
                } else {
577
                    $worksheet->setCellValueByColumnAndRow(
578
                        $column,
579
                        $line,
580
                        api_html_entity_decode(
581
                            strip_tags($row['last_name']),
582
                            ENT_QUOTES,
583
                            $charset
584
                        )
585
                    );
586
                    $column++;
587
                    $worksheet->setCellValueByColumnAndRow(
588
                        $column,
589
                        $line,
590
                        api_html_entity_decode(
591
                            strip_tags($row['first_name']),
592
                            ENT_QUOTES,
593
                            $charset
594
                        )
595
                    );
596
                    $column++;
597
                }
598
599
                if ($officialCodeInList === 'true') {
600
                    $worksheet->setCellValueByColumnAndRow(
601
                        $column,
602
                        $line,
603
                        api_html_entity_decode(
604
                            strip_tags($row['official_code']),
605
                            ENT_QUOTES,
606
                            $charset
607
                        )
608
                    );
609
                    $column++;
610
                }
611
612
                $worksheet->setCellValueByColumnAndRow(
613
                    $column,
614
                    $line,
615
                    api_html_entity_decode(
616
                        strip_tags($row['email']),
617
                        ENT_QUOTES,
618
                        $charset
619
                    )
620
                );
621
                $column++;
622
            }
623
624
            $worksheet->setCellValueByColumnAndRow(
625
                $column,
626
                $line,
627
                api_html_entity_decode(
628
                    strip_tags(
629
                        implode(
630
                            ", ",
631
                            GroupManager:: get_user_group_name($row['user_id'])
632
                        )
633
                    ),
634
                    ENT_QUOTES,
635
                    $charset
636
                )
637
            );
638
            $column++;
639
640
            if ($export_user_fields) {
641
                //show user fields data, if any, for this user
642
                $user_fields_values = UserManager::get_extra_user_data(
643
                    $row['user_id'],
644
                    false,
645
                    false,
646
                    false,
647
                    true
648
                );
649
                foreach ($user_fields_values as $value) {
650
                    $worksheet->setCellValueByColumnAndRow(
651
                        $column,
652
                        $line,
653
                        api_html_entity_decode(
654
                            strip_tags($value),
655
                            ENT_QUOTES,
656
                            $charset
657
                        )
658
                    );
659
                    $column++;
660
                }
661
            }
662
663
            $worksheet->setCellValueByColumnAndRow(
664
                $column,
665
                $line,
666
                api_html_entity_decode(
667
                    strip_tags($row['title']),
668
                    ENT_QUOTES,
669
                    $charset
670
                )
671
            );
672
673
            $column++;
674
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['start_date']);
675
            $column++;
676
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['end_date']);
677
            $column++;
678
            $duration = !empty($row['duration']) ? round($row['duration'] / 60) : 0;
679
            $worksheet->setCellValueByColumnAndRow($column, $line, $duration);
680
            $column++;
681
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['result']);
682
            $column++;
683
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['max']);
684
            $column++;
685
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['status']);
686
            $column++;
687
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['lp_name']);
688
            $column++;
689
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['is_user_subscribed']);
690
            $line++;
691
        }
692
693
        $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename);
694
        $writer = new PHPExcel_Writer_Excel2007($spreadsheet);
695
        $writer->save($file);
696
        DocumentManager::file_send_for_download($file, true, $filename);
697
698
        return true;
699
    }
700
}
701