hotpotatoes_exercise_result.class.php 15 KB

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