hotpotatoes_exercise_result.class.php 14 KB

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