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