hotpotatoes_exercise_result.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.score,
  40. tth.max_score,
  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, score, max_score, 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]['score'];
  83. $return[$i]['max'] = $hpresults[$i]['max_score'];
  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('First name').';';
  109. }
  110. if (!empty($this->results[0]['last_name'])) {
  111. $data .= get_lang('Last name').';';
  112. }
  113. } else {
  114. if (!empty($this->results[0]['last_name'])) {
  115. $data .= get_lang('Last name').';';
  116. }
  117. if (!empty($this->results[0]['first_name'])) {
  118. $data .= get_lang('First name').';';
  119. }
  120. }
  121. $data .= get_lang('e-mail').';';
  122. $data .= get_lang('Title').';';
  123. $data .= get_lang('Start Date').';';
  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();
  194. if (!empty($user_id)) {
  195. $filename = 'exercise_results_user_'.$user_id.'_'.api_get_local_time();
  196. }
  197. // check if exists column 'user'
  198. $withColumnUser = false;
  199. foreach ($this->results as $result) {
  200. if (!empty($result['last_name']) && !empty($result['first_name'])) {
  201. $withColumnUser = true;
  202. break;
  203. }
  204. }
  205. $list = [];
  206. if ($withColumnUser) {
  207. $list[0][] = get_lang('e-mail');
  208. if (api_is_western_name_order()) {
  209. $list[0][] = get_lang('First name');
  210. $list[0][] = get_lang('Last name');
  211. } else {
  212. $list[0][] = get_lang('Last name');
  213. $list[0][] = get_lang('First name');
  214. }
  215. }
  216. if ($export_user_fields) {
  217. //show user fields section with a big th colspan that spans over all fields
  218. $extra_user_fields = UserManager::get_extra_fields(
  219. 0,
  220. 1000,
  221. 5,
  222. 'ASC',
  223. false,
  224. 1
  225. );
  226. //show the fields names for user fields
  227. foreach ($extra_user_fields as $field) {
  228. $list[0][] = api_html_entity_decode(
  229. strip_tags($field[3]),
  230. ENT_QUOTES,
  231. $charset
  232. );
  233. }
  234. }
  235. $list[0][] = get_lang('Title');
  236. $list[0][] = get_lang('Start Date');
  237. $list[0][] = get_lang('End Date');
  238. $list[0][] = get_lang('Duration').' ('.get_lang('minutes').')';
  239. $list[0][] = get_lang('Score');
  240. $list[0][] = get_lang('Total');
  241. $list[0][] = get_lang('Status');
  242. $column = 1;
  243. foreach ($this->results as $row) {
  244. if ($withColumnUser) {
  245. $list[$column][] = api_html_entity_decode(
  246. strip_tags($row['email']),
  247. ENT_QUOTES,
  248. $charset
  249. );
  250. if (api_is_western_name_order()) {
  251. $list[$column][] = api_html_entity_decode(
  252. strip_tags($row['first_name']),
  253. ENT_QUOTES,
  254. $charset
  255. );
  256. $list[$column][] = api_html_entity_decode(
  257. strip_tags($row['last_name']),
  258. ENT_QUOTES,
  259. $charset
  260. );
  261. } else {
  262. $list[$column][] = api_html_entity_decode(
  263. strip_tags($row['last_name']),
  264. ENT_QUOTES,
  265. $charset
  266. );
  267. $list[$column][] = api_html_entity_decode(
  268. strip_tags($row['first_name']),
  269. ENT_QUOTES,
  270. $charset
  271. );
  272. }
  273. }
  274. if ($export_user_fields) {
  275. //show user fields data, if any, for this user
  276. $values = UserManager::get_extra_user_data(
  277. $row['user_id'],
  278. false,
  279. false,
  280. false,
  281. true
  282. );
  283. foreach ($values as $value) {
  284. $list[$column][] = api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset);
  285. }
  286. }
  287. $list[$column][] = api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset);
  288. $list[$column][] = $row['start_date'];
  289. $list[$column][] = $row['end_date'];
  290. $list[$column][] = $row['duration'];
  291. $list[$column][] = $row['result'];
  292. $list[$column][] = $row['max'];
  293. $list[$column][] = $row['status'];
  294. }
  295. Export::arrayToXls($list, $filename);
  296. return true;
  297. }
  298. }