Completed
Push — master ( 395485...bbad3a )
by Julito
43:12
created

ExerciseResult::exportCompleteReportXLS()   F

Complexity

Conditions 16
Paths 660

Size

Total Lines 240
Code Lines 180

Duplication

Lines 46
Ratio 19.17 %

Importance

Changes 0
Metric Value
cc 16
eloc 180
nc 660
nop 6
dl 46
loc 240
rs 2.3966
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
     * @param null $hotpotato_name
40
     * @return bool
41
     */
42
    public function getExercisesReporting(
43
        $document_path,
44
        $user_id = null,
45
        $filter = 0,
46
        $exercise_id = 0,
47
        $hotpotato_name = null
48
    ) {
49
		$return = array();
50
51
        $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
52
        $TBL_TABLE_LP_MAIN = Database::get_course_table(TABLE_LP_MAIN);
53
54
        $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
55
        $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
56
        $TBL_TRACK_ATTEMPT_RECORDING = Database:: get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
57
58
        $cid = api_get_course_id();
59
        $course_id = api_get_course_int_id();
60
        $user_id = intval($user_id);
61
        $sessionId = api_get_session_id();
62
        $session_id_and = ' AND te.session_id = ' . $sessionId . ' ';
63
        $exercise_id = intval($exercise_id);
64
65
        if (!empty($exercise_id)) {
66
            $session_id_and .= " AND exe_exo_id = $exercise_id ";
67
        }
68
69
        if (empty($user_id)) {
70
            $user_id_and = null;
71
            $sql = "SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").",
72
                        official_code,
73
                        ce.title as extitle,
74
                        te.exe_result as exresult ,
75
                        te.exe_weighting as exweight,
76
                        te.exe_date as exdate,
77
                        te.exe_id as exid,
78
                        email as exemail,
79
                        te.start_date as exstart,
80
                        steps_counter as exstep,
81
                        exe_user_id as excruid,
82
                        te.exe_duration as duration,
83
                        te.orig_lp_id as orig_lp_id,
84
                        tlm.name as lp_name
85
                FROM $TBL_EXERCISES  AS ce
86
                INNER JOIN $TBL_TRACK_EXERCISES AS te 
87
                ON (te.exe_exo_id = ce.id)
88
                INNER JOIN $TBL_USER AS user 
89
                ON (user.user_id = exe_user_id)
90
                LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm 
91
                ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id)
92
                WHERE
93
                    ce.c_id = $course_id AND
94
                    te.status != 'incomplete' AND
95
                    te.c_id = ce.c_id $user_id_and  $session_id_and AND
96
                    ce.active <>-1";
97
        } else {
98
            $user_id_and = ' AND te.exe_user_id = ' . api_get_user_id() . ' ';
99
            // get only this user's results
100
            $sql="SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").",
101
                    official_code,
102
                    ce.title as extitle,
103
                    te.exe_result as exresult,
104
                    te.exe_weighting as exweight,
105
                    te.exe_date as exdate,
106
                    te.exe_id as exid,
107
                    email as exemail,
108
                    te.start_date as exstart,
109
                    steps_counter as exstep,
110
                    exe_user_id as excruid,
111
                    te.exe_duration as duration,
112
                    ce.results_disabled as exdisabled,
113
                    te.orig_lp_id as orig_lp_id,
114
                    tlm.name as lp_name
115
                    FROM $TBL_EXERCISES  AS ce
116
                    INNER JOIN $TBL_TRACK_EXERCISES AS te 
117
                    ON (te.exe_exo_id = ce.id)
118
                    INNER JOIN $TBL_USER AS user 
119
                    ON (user.user_id = exe_user_id)
120
                    LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm 
121
                    ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id)
122
                    WHERE
123
                        ce.c_id = $course_id AND
124
                        te.status != 'incomplete' AND
125
                        te.c_id = ce.c_id $user_id_and $session_id_and AND
126
                        ce.active <>-1 AND
127
                    ORDER BY userpart2, te.c_id ASC, ce.title ASC, te.exe_date DESC";
128
        }
129
130
        $results = array();
131
        $resx = Database::query($sql);
132
        $bestAttemptPerUser = array();
133
        while ($rowx = Database::fetch_array($resx, 'ASSOC')) {
134
            if ($this->onlyBestAttempts) {
135
                if (!isset($bestAttemptPerUser[$rowx['excruid']])) {
136
                    $bestAttemptPerUser[$rowx['excruid']] = $rowx;
137
                } else {
138
                    if ($rowx['exresult'] > $bestAttemptPerUser[$rowx['excruid']]['exresult']) {
139
                        $bestAttemptPerUser[$rowx['excruid']] = $rowx;
140
                    }
141
                }
142
            } else {
143
                $results[] = $rowx;
144
            }
145
        }
146
147
        if ($this->onlyBestAttempts) {
148
            $results = $bestAttemptPerUser;
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);
173
174
        // Print the results of tests
175
        $userWithResults = array();
176
        if (is_array($results)) {
177
            $i = 0;
178
            foreach ($results as $result) {
179
                $revised = false;
180
181
                //revised or not
182
                $sql_exe = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT_RECORDING
183
                            WHERE author != '' AND exe_id = ".Database :: escape_string($result['exid'])."
184
                            LIMIT 1";
185
                $query = Database::query($sql_exe);
186
187
                if (Database:: num_rows($query) > 0) {
188
                    $revised = true;
189
                }
190
191
                if ($filter_by_not_revised && $revised) {
192
                    continue;
193
                }
194
195
                if ($filter_by_revised && !$revised) {
196
                    continue;
197
                }
198
199
                $return[$i] = array();
200
                if (empty($user_id)) {
201
                    $return[$i]['official_code'] = $result['official_code'];
202
                    if (api_is_western_name_order()) {
203
                        $return[$i]['first_name'] = $results[$i]['userpart1'];
204
                        $return[$i]['last_name'] = $results[$i]['userpart2'];
205
                    } else {
206
                        $return[$i]['first_name'] = $results[$i]['userpart2'];
207
                        $return[$i]['last_name'] = $results[$i]['userpart1'];
208
                    }
209
                    $return[$i]['user_id'] = $results[$i]['excruid'];
210
                    $return[$i]['email'] = $results[$i]['exemail'];
211
                }
212
                $return[$i]['title'] = $result['extitle'];
213
                $return[$i]['start_date'] = api_get_local_time($result['exstart']);
214
                $return[$i]['end_date'] = api_get_local_time($result['exdate']);
215
                $return[$i]['duration'] = $result['duration'];
216
                $return[$i]['result'] = $result['exresult'];
217
                $return[$i]['max'] = $result['exweight'];
218
                $return[$i]['status'] = $revised ? get_lang('Validated') : get_lang('NotValidated');
219
                $return[$i]['lp_id'] = $result['orig_lp_id'];
220
                $return[$i]['lp_name'] = $result['lp_name'];
221
222
                if (in_array($result['excruid'], $studentsUserIdList)) {
223
                    $return[$i]['is_user_subscribed'] = get_lang('Yes');
224
                } else {
225
                    $return[$i]['is_user_subscribed'] = get_lang('No');
226
                }
227
228
                $userWithResults[$result['excruid']] = 1;
229
                $i++;
230
            }
231
        }
232
233
        if ($this->includeAllUsers) {
234
            $latestId = count($return);
235
            $userWithResults = array_keys($userWithResults);
236
237
            if (!empty($students)) {
238
                foreach ($students as $student) {
0 ignored issues
show
Bug introduced by
The expression $students of type array|integer is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

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

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

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

Loading history...
239
                    if (!in_array($student['user_id'], $userWithResults)) {
240
                        $i = $latestId;
241
                        $isWestern = api_is_western_name_order();
242
243
                        if (empty($user_id)) {
244
                            $return[$i]['official_code'] = $student['official_code'];
245
                            if ($isWestern) {
246
                                $return[$i]['first_name'] = $student['firstname'];
247
                                $return[$i]['last_name'] = $student['lastname'];
248
                            } else {
249
                                $return[$i]['first_name'] = $student['lastname'];
250
                                $return[$i]['last_name'] = $student['firstname'];
251
                            }
252
253
                            $return[$i]['user_id'] = $student['user_id'];
254
                            $return[$i]['email'] = $student['email'];
255
                        }
256
                        $return[$i]['title'] = null;
257
                        $return[$i]['start_date'] = null;
258
                        $return[$i]['end_date'] = null;
259
                        $return[$i]['duration'] = null;
260
                        $return[$i]['result'] = null;
261
                        $return[$i]['max'] = null;
262
                        $return[$i]['status'] = get_lang('NotAttempted');
263
                        $return[$i]['lp_id'] = null;
264
                        $return[$i]['lp_name'] = null;
265
                        $return[$i]['is_user_subscribed'] = get_lang('Yes');
266
267
                        $latestId++;
268
                    }
269
                }
270
            }
271
        }
272
273
        $this->results = $return;
274
275
        return true;
276
    }
277
278
	/**
279
	 * Exports the complete report as a CSV file
280
     *
281
     * @param    string $document_path Document path inside the document tool
282
     * @param    integer $user_id Optional user ID
283
     * @param    boolean $export_user_fields Whether to include user fields or not
284
     * @param    int $export_filter
285
     * @param    int $exercise_id
286
     * @param    string $hotpotato_name
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
        $hotpotato_name = null
297
    ) {
298
        global $charset;
299
        $this->getExercisesReporting($document_path, $user_id, $export_filter, $exercise_id, $hotpotato_name);
0 ignored issues
show
Bug introduced by
It seems like $hotpotato_name defined by parameter $hotpotato_name on line 296 can also be of type string; however, ExerciseResult::getExercisesReporting() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

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

An additional type check may prevent trouble.

Loading history...
300
        $now = api_get_local_time();
301
        $filename = 'exercise_results_'.$now.'.csv';
302
        if (!empty($user_id)) {
303
            $filename = 'exercise_results_user_'.$user_id.'_'.$now.'.csv';
304
        }
305
306
        $filename = api_replace_dangerous_char($filename);
307
308
        $data = '';
309 View Code Duplication
        if (api_is_western_name_order()) {
310
            if (!empty($this->results[0]['first_name'])) {
311
                $data .= get_lang('FirstName').';';
312
            }
313
            if (!empty($this->results[0]['last_name'])) {
314
                $data .= get_lang('LastName').';';
315
            }
316
        } else {
317
            if (!empty($this->results[0]['last_name'])) {
318
                $data .= get_lang('LastName').';';
319
            }
320
            if (!empty($this->results[0]['first_name'])) {
321
                $data .= get_lang('FirstName').';';
322
            }
323
        }
324
        $officialCodeInList = api_get_setting('show_official_code_exercise_result_list');
325
        if ($officialCodeInList === 'true') {
326
            $data .= get_lang('OfficialCode').';';
327
        }
328
329
        $data .= get_lang('Email').';';
330
        $data .= get_lang('Groups').';';
331
332
        if ($export_user_fields) {
333
            //show user fields section with a big th colspan that spans over all fields
334
            $extra_user_fields = UserManager::get_extra_fields(
335
                0,
336
                1000,
337
                5,
338
                'ASC',
339
                false,
340
                1
341
            );
342
            if (!empty($extra_user_fields)) {
343
                foreach ($extra_user_fields as $field) {
344
                    $data .= '"'.str_replace("\r\n", '  ', api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset)).'";';
345
                }
346
            }
347
        }
348
349
        $data .= get_lang('Title').';';
350
        $data .= get_lang('StartDate').';';
351
        $data .= get_lang('EndDate').';';
352
        $data .= get_lang('Duration'). ' ('.get_lang('MinMinutes').') ;';
353
        $data .= get_lang('Score').';';
354
        $data .= get_lang('Total').';';
355
        $data .= get_lang('Status').';';
356
        $data .= get_lang('ToolLearnpath').';';
357
        $data .= get_lang('UserIsCurrentlySubscribed').';';
358
        $data .= "\n";
359
360
        //results
361
        foreach ($this->results as $row) {
362 View Code Duplication
            if (api_is_western_name_order()) {
363
                $data .= str_replace("\r\n",'  ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
364
                $data .= str_replace("\r\n",'  ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
365
            } else {
366
                $data .= str_replace("\r\n",'  ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
367
                $data .= str_replace("\r\n",'  ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
368
            }
369
370
            // Official code
371
            if ($officialCodeInList === 'true') {
372
                $data .= $row['official_code'].';';
373
            }
374
375
            // Email
376
            $data .= str_replace("\r\n",'  ',api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset)).';';
377
            $data .= str_replace("\r\n",'  ',implode(", ", GroupManager :: get_user_group_name($row['user_id']))).';';
378
379 View Code Duplication
            if ($export_user_fields) {
380
                //show user fields data, if any, for this user
381
                $user_fields_values = UserManager::get_extra_user_data(
382
                    $row['user_id'],
383
                    false,
384
                    false,
385
                    false,
386
                    true
387
                );
388
                if (!empty($user_fields_values)) {
389
                    foreach($user_fields_values as $value) {
390
                        $data .= '"'.str_replace('"','""',api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)).'";';
391
                    }
392
                }
393
            }
394
395
            $data .= str_replace("\r\n",'  ', api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)).';';
396
            $data .= str_replace("\r\n", '  ', $row['start_date']).';';
397
            $data .= str_replace("\r\n", '  ', $row['end_date']).';';
398
            $data .= str_replace("\r\n", '  ', $row['duration']).';';
399
            $data .= str_replace("\r\n", '  ', $row['result']).';';
400
            $data .= str_replace("\r\n", '  ', $row['max']).';';
401
            $data .= str_replace("\r\n", '  ', $row['status']).';';
402
            $data .= str_replace("\r\n", '  ', $row['lp_name']).';';
403
            $data .= str_replace("\r\n", '  ', $row['is_user_subscribed']).';';
404
            $data .= "\n";
405
        }
406
407
        //output the results
408
        $len = strlen($data);
409
        header('Content-type: application/octet-stream');
410
        header('Content-Type: application/force-download');
411
        header('Content-length: '.$len);
412 View Code Duplication
        if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
413
            header('Content-Disposition: filename= '.$filename);
414
        } else {
415
            header('Content-Disposition: attachment; filename= '.$filename);
416
        }
417
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
418
            header('Pragma: ');
419
            header('Cache-Control: ');
420
            header('Cache-Control: public'); // IE cannot download from sessions without a cache
421
        }
422
        header('Content-Description: '.$filename);
423
        header('Content-transfer-encoding: binary');
424
        echo $data;
425
        return true;
426
    }
427
428
    /**
429
     * Exports the complete report as an XLS file
430
     * @return	boolean		False on error
431
     */
432
    public function exportCompleteReportXLS(
433
        $document_path = '',
434
        $user_id = null,
435
        $export_user_fields = false,
436
        $export_filter = 0,
437
        $exercise_id = 0,
438
        $hotpotato_name = null
439
    ) {
440
        global $charset;
441
        $this->getExercisesReporting($document_path, $user_id, $export_filter, $exercise_id, $hotpotato_name);
442
        $filename = 'exercise_results_'.api_get_local_time().'.xlsx';
443
        if (!empty($user_id)) {
444
            $filename = 'exercise_results_user_'.$user_id.'_'.api_get_local_time().'.xlsx';
445
        }
446
447
        $spreadsheet = new PHPExcel();
448
        $spreadsheet->setActiveSheetIndex(0);
449
        $worksheet = $spreadsheet->getActiveSheet();
450
451
        $line = 0;
452
        $column = 0; //skip the first column (row titles)
453
454
        // check if exists column 'user'
455
        $with_column_user = false;
456 View Code Duplication
        foreach ($this->results as $result) {
457
            if (!empty($result['last_name']) && !empty($result['first_name'])) {
458
                $with_column_user = true;
459
                break;
460
            }
461
        }
462
463
        $officialCodeInList = api_get_setting('show_official_code_exercise_result_list');
464
465
        if ($with_column_user) {
466 View Code Duplication
            if (api_is_western_name_order()) {
467
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName'));
468
                $column++;
469
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName'));
470
                $column++;
471
            } else {
472
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('LastName'));
473
                $column++;
474
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('FirstName'));
475
                $column++;
476
            }
477
478
            if ($officialCodeInList === 'true') {
479
                $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('OfficialCode'));
480
                $column++;
481
            }
482
483
            $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Email'));
484
            $column++;
485
        }
486
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Groups'));
487
        $column++;
488
489 View Code Duplication
        if ($export_user_fields) {
490
            //show user fields section with a big th colspan that spans over all fields
491
            $extra_user_fields = UserManager::get_extra_fields(
492
                0,
493
                1000,
494
                5,
495
                'ASC',
496
                false,
497
                1
498
            );
499
500
            //show the fields names for user fields
501
            foreach ($extra_user_fields as $field) {
502
                $worksheet->setCellValueByColumnAndRow(
503
                    $column,
504
                    $line,
505
                    api_html_entity_decode(
506
                        strip_tags($field[3]),
507
                        ENT_QUOTES,
508
                        $charset
509
                    )
510
                );
511
                $column++;
512
            }
513
        }
514
515
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Title'));
516
        $column++;
517
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('StartDate'));
518
        $column++;
519
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('EndDate'));
520
        $column++;
521
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Duration').' ('.get_lang('MinMinutes').')');
522
        $column++;
523
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Score'));
524
        $column++;
525
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Total'));
526
        $column++;
527
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('Status'));
528
        $column++;
529
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('ToolLearnpath'));
530
        $column++;
531
        $worksheet->setCellValueByColumnAndRow($column, $line, get_lang('UserIsCurrentlySubscribed'));
532
        $line++;
533
534
        foreach ($this->results as $row) {
535
            $column = 0;
536
            if ($with_column_user) {
537 View Code Duplication
                if (api_is_western_name_order()) {
538
                    $worksheet->setCellValueByColumnAndRow(
539
                        $column,
540
                        $line,
541
                        api_html_entity_decode(
542
                            strip_tags($row['first_name']),
543
                            ENT_QUOTES,
544
                            $charset
545
                        )
546
                    );
547
                    $column++;
548
                    $worksheet->setCellValueByColumnAndRow(
549
                        $column,
550
                        $line,
551
                        api_html_entity_decode(
552
                            strip_tags($row['last_name']),
553
                            ENT_QUOTES,
554
                            $charset
555
                        )
556
                    );
557
                    $column++;
558
                } else {
559
                    $worksheet->setCellValueByColumnAndRow(
560
                        $column,
561
                        $line,
562
                        api_html_entity_decode(
563
                            strip_tags($row['last_name']),
564
                            ENT_QUOTES,
565
                            $charset
566
                        )
567
                    );
568
                    $column++;
569
                    $worksheet->setCellValueByColumnAndRow(
570
                        $column,
571
                        $line,
572
                        api_html_entity_decode(
573
                            strip_tags($row['first_name']),
574
                            ENT_QUOTES,
575
                            $charset
576
                        )
577
                    );
578
                    $column++;
579
                }
580
581
                if ($officialCodeInList === 'true') {
582
                    $worksheet->setCellValueByColumnAndRow(
583
                        $column,
584
                        $line,
585
                        api_html_entity_decode(
586
                            strip_tags($row['official_code']),
587
                            ENT_QUOTES,
588
                            $charset
589
                        )
590
                    );
591
                    $column++;
592
                }
593
594
                $worksheet->setCellValueByColumnAndRow(
595
                    $column,
596
                    $line,
597
                    api_html_entity_decode(
598
                        strip_tags($row['email']),
599
                        ENT_QUOTES,
600
                        $charset
601
                    )
602
                );
603
                $column++;
604
            }
605
606
            $worksheet->setCellValueByColumnAndRow(
607
                $column,
608
                $line,
609
                api_html_entity_decode(
610
                    strip_tags(
611
                        implode(
612
                            ", ",
613
                            GroupManager:: get_user_group_name($row['user_id'])
614
                        )
615
                    ),
616
                    ENT_QUOTES,
617
                    $charset
618
                )
619
            );
620
            $column++;
621
622 View Code Duplication
            if ($export_user_fields) {
623
                //show user fields data, if any, for this user
624
                $user_fields_values = UserManager::get_extra_user_data(
625
                    $row['user_id'],
626
                    false,
627
                    false,
628
                    false,
629
                    true
630
                );
631
                foreach ($user_fields_values as $value) {
632
                    $worksheet->setCellValueByColumnAndRow(
633
                        $column,
634
                        $line,
635
                        api_html_entity_decode(
636
                            strip_tags($value),
637
                            ENT_QUOTES,
638
                            $charset
639
                        )
640
                    );
641
                    $column++;
642
                }
643
            }
644
645
            $worksheet->setCellValueByColumnAndRow($column, $line, api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset));
646
            $column++;
647
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['start_date']);
648
            $column++;
649
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['end_date']);
650
            $column++;
651
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['duration']);
652
            $column++;
653
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['result']);
654
            $column++;
655
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['max']);
656
            $column++;
657
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['status']);
658
            $column++;
659
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['lp_name']);
660
            $column++;
661
            $worksheet->setCellValueByColumnAndRow($column, $line, $row['is_user_subscribed']);
662
            $line++;
663
        }
664
665
        $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename);
666
        $writer = new PHPExcel_Writer_Excel2007($spreadsheet);
667
        $writer->save($file);
668
        DocumentManager::file_send_for_download($file, true, $filename);
669
670
        return true;
671
    }
672
}
673