hotpotatoes_exercise_result.class.php 13 KB

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