1 | <?php |
||
2 | /* For licensing terms, see /license.txt */ |
||
3 | |||
4 | /** |
||
5 | * Class HotpotatoesExerciseResult |
||
6 | * Allows you to export exercises results in multiple presentation forms. |
||
7 | * |
||
8 | * @package chamilo.exercise |
||
9 | */ |
||
10 | class HotpotatoesExerciseResult |
||
11 | { |
||
12 | //stores the list of exercises |
||
13 | private $exercises_list = []; |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
14 | |||
15 | //stores the results |
||
16 | private $results = []; |
||
17 | |||
18 | /** |
||
19 | * Gets the results of all students (or just one student if access is limited). |
||
20 | * |
||
21 | * @param string $document_path The document path (for HotPotatoes retrieval) |
||
22 | * @param int User ID. Optional. If no user ID is provided, we take all the results. Defauts to null |
||
23 | * |
||
24 | * @return bool |
||
25 | */ |
||
26 | public function getExercisesReporting($document_path, $hotpotato_name) |
||
27 | { |
||
28 | $return = []; |
||
29 | $TBL_USER = Database::get_main_table(TABLE_MAIN_USER); |
||
30 | $TBL_TRACK_HOTPOTATOES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES); |
||
31 | $course_id = api_get_course_int_id(); |
||
32 | $user_id = null; |
||
33 | $session_id_and = ' AND te.session_id = '.api_get_session_id().' '; |
||
34 | $hotpotato_name = Database::escape_string($hotpotato_name); |
||
35 | |||
36 | if (!empty($exercise_id)) { |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
37 | $session_id_and .= " AND exe_exo_id = $exercise_id "; |
||
38 | } |
||
39 | |||
40 | if (empty($user_id)) { |
||
41 | $sql = "SELECT firstname as userpart1, lastname as userpart2 , |
||
42 | email, |
||
43 | tth.exe_name, |
||
44 | tth.exe_result, |
||
45 | tth.exe_weighting, |
||
46 | tth.exe_date |
||
47 | FROM $TBL_TRACK_HOTPOTATOES tth, $TBL_USER tu |
||
48 | WHERE tu.user_id=tth.exe_user_id AND |
||
49 | tth.c_id = $course_id AND |
||
50 | tth.exe_name = '$hotpotato_name' |
||
51 | ORDER BY tth.c_id ASC, tth.exe_date ASC"; |
||
52 | } else { |
||
53 | // get only this user's results |
||
54 | $sql = "SELECT '', exe_name, exe_result , exe_weighting, exe_date |
||
55 | FROM $TBL_TRACK_HOTPOTATOES |
||
56 | WHERE |
||
57 | exe_user_id = $user_id AND |
||
58 | c_id = $course_id AND |
||
59 | tth.exe_name = '$hotpotato_name' |
||
60 | ORDER BY c_id ASC, exe_date ASC"; |
||
61 | } |
||
62 | |||
63 | $results = []; |
||
64 | |||
65 | $resx = Database::query($sql); |
||
66 | while ($rowx = Database::fetch_array($resx, 'ASSOC')) { |
||
67 | $results[] = $rowx; |
||
68 | } |
||
69 | |||
70 | $hpresults = []; |
||
71 | $resx = Database::query($sql); |
||
72 | while ($rowx = Database::fetch_array($resx, 'ASSOC')) { |
||
73 | $hpresults[] = $rowx; |
||
74 | } |
||
75 | |||
76 | // Print the Result of Hotpotatoes Tests |
||
77 | if (is_array($hpresults)) { |
||
78 | for ($i = 0; $i < sizeof($hpresults); $i++) { |
||
0 ignored issues
–
show
It seems like you are calling the size function
sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
|
|||
79 | $return[$i] = []; |
||
80 | $title = GetQuizName($hpresults[$i]['exe_name'], $document_path); |
||
81 | if ($title == '') { |
||
82 | $title = basename($hpresults[$i]['exe_name']); |
||
83 | } |
||
84 | if (empty($user_id)) { |
||
85 | $return[$i]['email'] = $hpresults[$i]['email']; |
||
86 | $return[$i]['first_name'] = $hpresults[$i]['userpart1']; |
||
87 | $return[$i]['last_name'] = $hpresults[$i]['userpart2']; |
||
88 | } |
||
89 | $return[$i]['title'] = $title; |
||
90 | $return[$i]['exe_date'] = $hpresults[$i]['exe_date']; |
||
91 | |||
92 | $return[$i]['result'] = $hpresults[$i]['exe_result']; |
||
93 | $return[$i]['max'] = $hpresults[$i]['exe_weighting']; |
||
94 | } |
||
95 | } |
||
96 | $this->results = $return; |
||
97 | |||
98 | return true; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Exports the complete report as a CSV file. |
||
103 | * |
||
104 | * @param string $document_path Document path inside the document tool |
||
105 | * @param string $hotpotato_name |
||
106 | * |
||
107 | * @return bool False on error |
||
108 | */ |
||
109 | public function exportCompleteReportCSV($document_path = '', $hotpotato_name = '') |
||
110 | { |
||
111 | global $charset; |
||
112 | $this->getExercisesReporting($document_path, $hotpotato_name); |
||
113 | $filename = 'exercise_results_'.date('YmdGis').'.csv'; |
||
114 | if (!empty($user_id)) { |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
115 | $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.csv'; |
||
116 | } |
||
117 | $data = ''; |
||
118 | |||
119 | if (api_is_western_name_order()) { |
||
120 | if (!empty($this->results[0]['first_name'])) { |
||
121 | $data .= get_lang('FirstName').';'; |
||
122 | } |
||
123 | if (!empty($this->results[0]['last_name'])) { |
||
124 | $data .= get_lang('LastName').';'; |
||
125 | } |
||
126 | } else { |
||
127 | if (!empty($this->results[0]['last_name'])) { |
||
128 | $data .= get_lang('LastName').';'; |
||
129 | } |
||
130 | if (!empty($this->results[0]['first_name'])) { |
||
131 | $data .= get_lang('FirstName').';'; |
||
132 | } |
||
133 | } |
||
134 | $data .= get_lang('Email').';'; |
||
135 | $data .= get_lang('Title').';'; |
||
136 | $data .= get_lang('StartDate').';'; |
||
137 | $data .= get_lang('Score').';'; |
||
138 | $data .= get_lang('Total').';'; |
||
139 | $data .= "\n"; |
||
140 | |||
141 | // Results |
||
142 | foreach ($this->results as $row) { |
||
143 | if (api_is_western_name_order()) { |
||
144 | $data .= str_replace("\r\n", ' ', api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';'; |
||
145 | $data .= str_replace("\r\n", ' ', api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';'; |
||
146 | } else { |
||
147 | $data .= str_replace("\r\n", ' ', api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';'; |
||
148 | $data .= str_replace("\r\n", ' ', api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';'; |
||
149 | } |
||
150 | |||
151 | $data .= str_replace("\r\n", ' ', api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset)).';'; |
||
152 | $data .= str_replace("\r\n", ' ', api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)).';'; |
||
153 | $data .= str_replace("\r\n", ' ', $row['exe_date']).';'; |
||
154 | $data .= str_replace("\r\n", ' ', $row['result']).';'; |
||
155 | $data .= str_replace("\r\n", ' ', $row['max']).';'; |
||
156 | $data .= "\n"; |
||
157 | } |
||
158 | |||
159 | //output the results |
||
160 | $len = strlen($data); |
||
161 | header('Content-type: application/octet-stream'); |
||
162 | header('Content-Type: application/force-download'); |
||
163 | header('Content-length: '.$len); |
||
164 | if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) { |
||
165 | header('Content-Disposition: filename= '.$filename); |
||
166 | } else { |
||
167 | header('Content-Disposition: attachment; filename= '.$filename); |
||
168 | } |
||
169 | if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) { |
||
170 | header('Pragma: '); |
||
171 | header('Cache-Control: '); |
||
172 | header('Cache-Control: public'); // IE cannot download from sessions without a cache |
||
173 | } |
||
174 | header('Content-Description: '.$filename); |
||
175 | header('Content-transfer-encoding: binary'); |
||
176 | // @todo add this utf-8 header for all csv files |
||
177 | echo "\xEF\xBB\xBF"; // force utf-8 header of csv file |
||
178 | echo $data; |
||
179 | |||
180 | return true; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Exports the complete report as an XLS file. |
||
185 | * |
||
186 | * @param string $document_path |
||
187 | * @param null $user_id |
||
0 ignored issues
–
show
|
|||
188 | * @param bool $export_user_fields |
||
189 | * @param int $export_filter |
||
190 | * @param int $exercise_id |
||
191 | * @param null $hotpotato_name |
||
0 ignored issues
–
show
|
|||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function exportCompleteReportXLS( |
||
196 | $document_path = '', |
||
197 | $user_id = null, |
||
198 | $export_user_fields = false, |
||
199 | $export_filter = 0, |
||
200 | $exercise_id = 0, |
||
201 | $hotpotato_name = null |
||
202 | ) { |
||
203 | global $charset; |
||
204 | $this->getExercisesReporting( |
||
205 | $document_path, |
||
206 | $user_id, |
||
207 | $export_filter, |
||
208 | $exercise_id, |
||
209 | $hotpotato_name |
||
210 | ); |
||
211 | $filename = 'exercise_results_'.api_get_local_time().'.xls'; |
||
212 | if (!empty($user_id)) { |
||
213 | $filename = 'exercise_results_user_'.$user_id.'_'.api_get_local_time().'.xls'; |
||
214 | } |
||
215 | |||
216 | $spreadsheet = new PHPExcel(); |
||
217 | $spreadsheet->setActiveSheetIndex(0); |
||
218 | $worksheet = $spreadsheet->getActiveSheet(); |
||
219 | |||
220 | $line = 0; |
||
221 | $column = 0; //skip the first column (row titles) |
||
222 | |||
223 | // check if exists column 'user' |
||
224 | $with_column_user = false; |
||
225 | foreach ($this->results as $result) { |
||
226 | if (!empty($result['last_name']) && !empty($result['first_name'])) { |
||
227 | $with_column_user = true; |
||
228 | break; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | if ($with_column_user) { |
||
233 | $worksheet->setCellValueByColumnAndRow( |
||
234 | $column, |
||
235 | $line, |
||
236 | get_lang('Email') |
||
237 | ); |
||
238 | $column++; |
||
239 | if (api_is_western_name_order()) { |
||
240 | $worksheet->setCellValueByColumnAndRow( |
||
241 | $column, |
||
242 | $line, |
||
243 | get_lang('FirstName') |
||
244 | ); |
||
245 | $column++; |
||
246 | $worksheet->setCellValueByColumnAndRow( |
||
247 | $column, |
||
248 | $line, |
||
249 | get_lang('LastName') |
||
250 | ); |
||
251 | $column++; |
||
252 | } else { |
||
253 | $worksheet->setCellValueByColumnAndRow( |
||
254 | $column, |
||
255 | $line, |
||
256 | get_lang('LastName') |
||
257 | ); |
||
258 | $column++; |
||
259 | $worksheet->setCellValueByColumnAndRow( |
||
260 | $column, |
||
261 | $line, |
||
262 | get_lang('FirstName') |
||
263 | ); |
||
264 | $column++; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | if ($export_user_fields) { |
||
269 | //show user fields section with a big th colspan that spans over all fields |
||
270 | $extra_user_fields = UserManager::get_extra_fields( |
||
271 | 0, |
||
272 | 1000, |
||
273 | 5, |
||
274 | 'ASC', |
||
275 | false, |
||
276 | 1 |
||
277 | ); |
||
278 | |||
279 | //show the fields names for user fields |
||
280 | foreach ($extra_user_fields as $field) { |
||
281 | $worksheet->setCellValueByColumnAndRow( |
||
282 | $column, |
||
283 | $line, |
||
284 | api_html_entity_decode( |
||
285 | strip_tags($field[3]), |
||
286 | ENT_QUOTES, |
||
287 | $charset |
||
288 | ) |
||
289 | ); |
||
290 | $column++; |
||
291 | } |
||
292 | } |
||
293 | |||
294 | $worksheet->setCellValueByColumnAndRow( |
||
295 | $column, |
||
296 | $line, |
||
297 | get_lang('Title') |
||
298 | ); |
||
299 | $column++; |
||
300 | $worksheet->setCellValueByColumnAndRow( |
||
301 | $column, |
||
302 | $line, |
||
303 | get_lang('StartDate') |
||
304 | ); |
||
305 | $column++; |
||
306 | $worksheet->setCellValueByColumnAndRow( |
||
307 | $column, |
||
308 | $line, |
||
309 | get_lang('EndDate') |
||
310 | ); |
||
311 | $column++; |
||
312 | $worksheet->setCellValueByColumnAndRow( |
||
313 | $column, |
||
314 | $line, |
||
315 | get_lang('Duration').' ('.get_lang('MinMinutes').')' |
||
316 | ); |
||
317 | $column++; |
||
318 | $worksheet->setCellValueByColumnAndRow( |
||
319 | $column, |
||
320 | $line, |
||
321 | get_lang('Score') |
||
322 | ); |
||
323 | $column++; |
||
324 | $worksheet->setCellValueByColumnAndRow( |
||
325 | $column, |
||
326 | $line, |
||
327 | get_lang('Total') |
||
328 | ); |
||
329 | $column++; |
||
330 | $worksheet->setCellValueByColumnAndRow( |
||
331 | $column, |
||
332 | $line, |
||
333 | get_lang('Status') |
||
334 | ); |
||
335 | $line++; |
||
336 | |||
337 | foreach ($this->results as $row) { |
||
338 | $column = 0; |
||
339 | |||
340 | if ($with_column_user) { |
||
341 | $worksheet->setCellValueByColumnAndRow( |
||
342 | $column, |
||
343 | $line, |
||
344 | api_html_entity_decode( |
||
345 | strip_tags($row['email']), |
||
346 | ENT_QUOTES, |
||
347 | $charset |
||
348 | ) |
||
349 | ); |
||
350 | $column++; |
||
351 | |||
352 | if (api_is_western_name_order()) { |
||
353 | $worksheet->setCellValueByColumnAndRow( |
||
354 | $column, |
||
355 | $line, |
||
356 | api_html_entity_decode( |
||
357 | strip_tags($row['first_name']), |
||
358 | ENT_QUOTES, |
||
359 | $charset |
||
360 | ) |
||
361 | ); |
||
362 | $column++; |
||
363 | $worksheet->setCellValueByColumnAndRow( |
||
364 | $column, |
||
365 | $line, |
||
366 | api_html_entity_decode( |
||
367 | strip_tags($row['last_name']), |
||
368 | ENT_QUOTES, |
||
369 | $charset |
||
370 | ) |
||
371 | ); |
||
372 | $column++; |
||
373 | } else { |
||
374 | $worksheet->setCellValueByColumnAndRow( |
||
375 | $column, |
||
376 | $line, |
||
377 | api_html_entity_decode( |
||
378 | strip_tags($row['last_name']), |
||
379 | ENT_QUOTES, |
||
380 | $charset |
||
381 | ) |
||
382 | ); |
||
383 | $column++; |
||
384 | $worksheet->setCellValueByColumnAndRow( |
||
385 | $column, |
||
386 | $line, |
||
387 | api_html_entity_decode( |
||
388 | strip_tags($row['first_name']), |
||
389 | ENT_QUOTES, |
||
390 | $charset |
||
391 | ) |
||
392 | ); |
||
393 | $column++; |
||
394 | } |
||
395 | } |
||
396 | |||
397 | if ($export_user_fields) { |
||
398 | //show user fields data, if any, for this user |
||
399 | $user_fields_values = UserManager::get_extra_user_data( |
||
400 | $row['user_id'], |
||
401 | false, |
||
402 | false, |
||
403 | false, |
||
404 | true |
||
405 | ); |
||
406 | foreach ($user_fields_values as $value) { |
||
407 | $worksheet->setCellValueByColumnAndRow($column, $line, api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)); |
||
408 | $column++; |
||
409 | } |
||
410 | } |
||
411 | |||
412 | $worksheet->setCellValueByColumnAndRow($column, $line, api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)); |
||
413 | $column++; |
||
414 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['start_date']); |
||
415 | $column++; |
||
416 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['end_date']); |
||
417 | $column++; |
||
418 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['duration']); |
||
419 | $column++; |
||
420 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['result']); |
||
421 | $column++; |
||
422 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['max']); |
||
423 | $column++; |
||
424 | $worksheet->setCellValueByColumnAndRow($column, $line, $row['status']); |
||
425 | $line++; |
||
426 | } |
||
427 | |||
428 | $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename); |
||
429 | $writer = new PHPExcel_Writer_Excel2007($spreadsheet); |
||
430 | $writer->save($file); |
||
431 | DocumentManager::file_send_for_download($file, true, $filename); |
||
432 | |||
433 | return true; |
||
434 | } |
||
435 | } |
||
436 |