hotpotatoes_exercise_result.class.php 13 KB

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