hotpotatoes_exercise_result.class.php 13 KB

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