hotpotatoes_exercise_result.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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_statistic_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.exe_cours_id = '" . Database :: escape_string($cid) . "' AND
  43. tth.exe_name = '$hotpotato_name'
  44. ORDER BY tth.exe_cours_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. exe_cours_id = '" . Database :: escape_string($cid) . "' AND
  53. tth.exe_name = '$hotpotato_name'
  54. ORDER BY exe_cours_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_'.date('YmdGis').'.xls';
  205. if (!empty($user_id)) {
  206. $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.xls';
  207. }
  208. $workbook = new Spreadsheet_Excel_Writer();
  209. $workbook->setTempDir(api_get_path(SYS_ARCHIVE_PATH));
  210. $workbook->setVersion(8); // BIFF8
  211. $workbook->send($filename);
  212. $worksheet =& $workbook->addWorksheet('Report '.date('YmdGis'));
  213. $worksheet->setInputEncoding(api_get_system_encoding());
  214. $line = 0;
  215. $column = 0; //skip the first column (row titles)
  216. // check if exists column 'user'
  217. $with_column_user = false;
  218. foreach ($this->results as $result) {
  219. if (!empty($result['last_name']) && !empty($result['first_name'])) {
  220. $with_column_user = true;
  221. break;
  222. }
  223. }
  224. if ($with_column_user) {
  225. $worksheet->write($line,$column,get_lang('Email'));
  226. $column++;
  227. if (api_is_western_name_order()) {
  228. $worksheet->write($line,$column,get_lang('FirstName'));
  229. $column++;
  230. $worksheet->write($line,$column,get_lang('LastName'));
  231. $column++;
  232. } else {
  233. $worksheet->write($line,$column,get_lang('LastName'));
  234. $column++;
  235. $worksheet->write($line,$column,get_lang('FirstName'));
  236. $column++;
  237. }
  238. }
  239. if ($export_user_fields) {
  240. //show user fields section with a big th colspan that spans over all fields
  241. $extra_user_fields = UserManager::get_extra_fields(0,1000,5,'ASC',false, 1);
  242. //show the fields names for user fields
  243. foreach ($extra_user_fields as $field) {
  244. $worksheet->write($line, $column, api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset));
  245. $column++;
  246. }
  247. }
  248. $worksheet->write($line,$column, get_lang('Title'));
  249. $column++;
  250. $worksheet->write($line,$column, get_lang('StartDate'));
  251. $column++;
  252. $worksheet->write($line,$column, get_lang('EndDate'));
  253. $column++;
  254. $worksheet->write($line,$column, get_lang('Duration').' ('.get_lang('MinMinutes').')');
  255. $column++;
  256. $worksheet->write($line,$column, get_lang('Score'));
  257. $column++;
  258. $worksheet->write($line,$column, get_lang('Total'));
  259. $column++;
  260. $worksheet->write($line,$column, get_lang('Status'));
  261. $line++;
  262. foreach ($this->results as $row) {
  263. $column = 0;
  264. if ($with_column_user) {
  265. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset));
  266. $column++;
  267. if (api_is_western_name_order()) {
  268. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset));
  269. $column++;
  270. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset));
  271. $column++;
  272. } else {
  273. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset));
  274. $column++;
  275. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset));
  276. $column++;
  277. }
  278. }
  279. if ($export_user_fields) {
  280. //show user fields data, if any, for this user
  281. $user_fields_values = UserManager::get_extra_user_data($row['user_id'],false,false, false, true);
  282. foreach($user_fields_values as $value) {
  283. $worksheet->write($line,$column, api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset));
  284. $column++;
  285. }
  286. }
  287. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset));
  288. $column++;
  289. $worksheet->write($line,$column,$row['start_date']);
  290. $column++;
  291. $worksheet->write($line,$column,$row['end_date']);
  292. $column++;
  293. $worksheet->write($line,$column,$row['duration']);
  294. $column++;
  295. $worksheet->write($line,$column,$row['result']);
  296. $column++;
  297. $worksheet->write($line,$column,$row['max']);
  298. $column++;
  299. $worksheet->write($line,$column,$row['status']);
  300. $line++;
  301. }
  302. $workbook->close();
  303. return true;
  304. }
  305. }