hotpotatoes_exercise_result.class.php 14 KB

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