exercise_result.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * ExerciseResult class: This class allows to instantiate an object
  5. * of type ExerciseResult
  6. * which allows you to export exercises results in multiple presentation forms
  7. * @package chamilo.exercise
  8. * @author Yannick Warnier
  9. * @version $Id: $
  10. */
  11. /**
  12. * Code
  13. */
  14. if(!class_exists('ExerciseResult')):
  15. /**
  16. * Exercise results class
  17. * @package chamilo.exercise
  18. */
  19. class ExerciseResult
  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 questions related to one exercise
  54. * @param integer Exercise ID
  55. */
  56. private function _readExerciseQuestionsList($e_id)
  57. {
  58. $return = array();
  59. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  60. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  61. $sql="SELECT q.id, q.question, q.ponderation, eq.question_order, q.type, q.picture " .
  62. " FROM $TBL_EXERCISE_QUESTION eq, $TBL_QUESTIONS q " .
  63. " WHERE eq.question_id=q.id AND eq.exercice_id='".Database::escape_string($e_id)."' " .
  64. " ORDER BY eq.question_order";
  65. $result=Database::query($sql);
  66. // fills the array with the question ID for this exercise
  67. // the key of the array is the question position
  68. while($row=Database::fetch_array($result,'ASSOC'))
  69. {
  70. $return[] = $row;
  71. }
  72. return true;
  73. }
  74. /**
  75. * Gets the results of all students (or just one student if access is limited)
  76. * @param string The document path (for HotPotatoes retrieval)
  77. * @param integer User ID. Optional. If no user ID is provided, we take all the results. Defauts to null
  78. */
  79. function _getExercisesReporting($document_path, $user_id = null, $filter=0, $exercise_id = 0, $hotpotato_name = null) {
  80. $return = array();
  81. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  82. $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
  83. $TBL_TRACK_EXERCISES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  84. $TBL_TRACK_HOTPOTATOES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  85. $TBL_TRACK_ATTEMPT_RECORDING= Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  86. $cid = api_get_course_id();
  87. $course_id = api_get_course_int_id();
  88. $user_id = intval($user_id);
  89. $session_id_and = ' AND te.session_id = ' . api_get_session_id() . ' ';
  90. $exercise_id = intval($exercise_id);
  91. $hotpotato_name = Database::escape_string($hotpotato_name);
  92. if (!empty($exercise_id)) {
  93. $session_id_and .= " AND exe_exo_id = $exercise_id ";
  94. }
  95. if (empty($user_id)) {
  96. $sql = "SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").",
  97. ce.title as extitle,
  98. te.exe_result as exresult ,
  99. te.exe_weighting as exweight,
  100. te.exe_date as exdate,
  101. te.exe_id as exid,
  102. email as exemail,
  103. te.start_date as exstart,
  104. steps_counter as exstep,
  105. exe_user_id as excruid,
  106. te.exe_duration as duration
  107. FROM $TBL_EXERCISES AS ce INNER JOIN $TBL_TRACK_EXERCISES AS te ON (te.exe_exo_id = ce.id) INNER JOIN
  108. $TBL_USER AS user ON (user.user_id = exe_user_id)
  109. WHERE ce.c_id = $course_id AND
  110. te.status != 'incomplete' AND
  111. te.exe_cours_id='" . Database :: escape_string($cid) . "' $user_id_and $session_id_and AND
  112. ce.active <>-1 AND
  113. orig_lp_id = 0 AND
  114. orig_lp_item_id = 0";
  115. $hpsql="SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").",
  116. email,
  117. tth.exe_name,
  118. tth.exe_result,
  119. tth.exe_weighting,
  120. tth.exe_date
  121. FROM $TBL_TRACK_HOTPOTATOES tth, $TBL_USER tu
  122. WHERE tu.user_id=tth.exe_user_id AND
  123. tth.exe_cours_id = '" . Database :: escape_string($cid) . "' AND
  124. tth.exe_name = '$hotpotato_name'
  125. ORDER BY tth.exe_cours_id ASC, tth.exe_date DESC";
  126. } else {
  127. $user_id_and = ' AND te.exe_user_id = ' . api_get_user_id() . ' ';
  128. // get only this user's results
  129. $sql="SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").",
  130. ce.title as extitle,
  131. te.exe_result as exresult,
  132. te.exe_weighting as exweight,
  133. te.exe_date as exdate,
  134. te.exe_id as exid,
  135. email as exemail,
  136. te.start_date as exstart,
  137. steps_counter as exstep,
  138. exe_user_id as excruid,
  139. te.exe_duration as duration,
  140. ce.results_disabled as exdisabled
  141. FROM $TBL_EXERCISES AS ce INNER JOIN $TBL_TRACK_EXERCISES AS te ON (te.exe_exo_id = ce.id) INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id)
  142. WHERE ce.c_id = $course_id AND
  143. te.status != 'incomplete' AND
  144. te.exe_cours_id='" . Database :: escape_string($cid) . "' $user_id_and $session_id_and AND
  145. ce.active <>-1 AND
  146. orig_lp_id = 0 AND
  147. orig_lp_item_id = 0
  148. ORDER BY userpart2, te.exe_cours_id ASC, ce.title ASC, te.exe_date DESC";
  149. $hpsql = "SELECT '', exe_name, exe_result , exe_weighting, exe_date
  150. FROM $TBL_TRACK_HOTPOTATOES
  151. WHERE exe_user_id = '" . $user_id . "' AND
  152. exe_cours_id = '" . Database :: escape_string($cid) . "' AND
  153. tth.exe_name = '$hotpotato_name'
  154. ORDER BY exe_cours_id ASC, exe_date DESC";
  155. }
  156. $results = array();
  157. $resx = Database::query($sql);
  158. while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  159. $results[] = $rowx;
  160. }
  161. $hpresults = array();
  162. $resx = Database::query($hpsql);
  163. while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  164. $hpresults[] = $rowx;
  165. }
  166. if ($filter) {
  167. switch ($filter) {
  168. case 1 :
  169. $filter_by_not_revised = true;
  170. break;
  171. case 2 :
  172. $filter_by_revised = true;
  173. break;
  174. default :
  175. null;
  176. }
  177. }
  178. //Print the results of tests
  179. if (is_array($results) && empty($hotpotato_name)) {
  180. for ($i = 0; $i < sizeof($results); $i++) {
  181. $revised = false;
  182. //revised or not
  183. $sql_exe = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT_RECORDING
  184. WHERE author != '' AND exe_id = ".Database :: escape_string($results[$i]['exid'])." LIMIT 1";
  185. $query = Database::query($sql_exe);
  186. if (Database :: num_rows($query) > 0)
  187. $revised = true;
  188. if ($filter_by_not_revised && $revised) continue;
  189. if ($filter_by_revised && !$revised) continue;
  190. $return[$i] = array();
  191. if (empty($user_id)) {
  192. $return[$i]['first_name'] = $results[$i]['userpart1'];
  193. $return[$i]['last_name'] = $results[$i]['userpart2'];
  194. $return[$i]['user_id'] = $results[$i]['excruid'];
  195. $return[$i]['email'] = $results[$i]['exemail'];
  196. }
  197. $return[$i]['title'] = $results[$i]['extitle'];
  198. $return[$i]['start_date'] = api_get_local_time($results[$i]['exstart']);
  199. $return[$i]['end_date'] = api_get_local_time($results[$i]['exdate']);
  200. $return[$i]['duration'] = $results[$i]['duration'];
  201. $return[$i]['result'] = $results[$i]['exresult'];
  202. $return[$i]['max'] = $results[$i]['exweight'];
  203. $return[$i]['status'] = $revised ? get_lang('Validated') : get_lang('NotValidated');
  204. }
  205. }
  206. // Print the Result of Hotpotatoes Tests
  207. if (is_array($hpresults)) {
  208. for($i = 0; $i < sizeof($hpresults); $i++) {
  209. $return[$i] = array();
  210. $title = GetQuizName($hpresults[$i]['exe_name'], $document_path);
  211. if ($title =='') {
  212. $title = basename($hpresults[$i]['exe_name']);
  213. }
  214. if(empty($user_id)) {
  215. $return[$i]['email'] = $hpresults[$i]['email'];
  216. $return[$i]['first_name'] = $hpresults[$i]['userpart1'];
  217. $return[$i]['last_name'] = $hpresults[$i]['userpart2'];
  218. }
  219. $return[$i]['title'] = $title;
  220. $return[$i]['start_date'] = api_get_local_time($results[$i]['exstart']);
  221. $return[$i]['end_date'] = api_get_local_time($results[$i]['exdate']);
  222. $return[$i]['duration'] = $results[$i]['duration'];
  223. $return[$i]['result'] = $hpresults[$i]['exe_result'];
  224. $return[$i]['max'] = $hpresults[$i]['exe_weighting'];
  225. }
  226. }
  227. $this->results = $return;
  228. return true;
  229. }
  230. /**
  231. * Exports the complete report as a CSV file
  232. * @param string Document path inside the document tool
  233. * @param integer Optional user ID
  234. * @param boolean Whether to include user fields or not
  235. * @return boolean False on error
  236. */
  237. public function exportCompleteReportCSV($document_path='',$user_id=null, $export_user_fields = false, $export_filter = 0, $exercise_id = 0, $hotpotato_name = null) {
  238. global $charset;
  239. $this->_getExercisesReporting($document_path,$user_id, $export_filter, $exercise_id, $hotpotato_name);
  240. $filename = 'exercise_results_'.date('YmdGis').'.csv';
  241. if(!empty($user_id)) {
  242. $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.csv';
  243. }
  244. $data = '';
  245. if (api_is_western_name_order()) {
  246. if(!empty($this->results[0]['first_name'])) {
  247. $data .= get_lang('FirstName').';';
  248. }
  249. if(!empty($this->results[0]['last_name'])) {
  250. $data .= get_lang('LastName').';';
  251. }
  252. } else {
  253. if(!empty($this->results[0]['last_name'])) {
  254. $data .= get_lang('LastName').';';
  255. }
  256. if(!empty($this->results[0]['first_name'])) {
  257. $data .= get_lang('FirstName').';';
  258. }
  259. }
  260. $data .= get_lang('Email').';';
  261. $data .= get_lang('Groups').';';
  262. if ($export_user_fields) {
  263. //show user fields section with a big th colspan that spans over all fields
  264. $extra_user_fields = UserManager::get_extra_fields(0,1000,5,'ASC',false, 1);
  265. $num = count($extra_user_fields);
  266. foreach($extra_user_fields as $field) {
  267. $data .= '"'.str_replace("\r\n",' ',api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset)).'";';
  268. }
  269. }
  270. $data .= get_lang('Title').';';
  271. $data .= get_lang('StartDate').';';
  272. $data .= get_lang('EndDate').';';
  273. $data .= get_lang('Duration'). ' ('.get_lang('MinMinutes').') ;';
  274. $data .= get_lang('Score').';';
  275. $data .= get_lang('Total').';';
  276. $data .= get_lang('Status').';';
  277. $data .= "\n";
  278. //results
  279. foreach($this->results as $row) {
  280. if (api_is_western_name_order()) {
  281. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
  282. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
  283. } else {
  284. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
  285. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
  286. }
  287. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset)).';';
  288. $data .= str_replace("\r\n",' ',implode(", ", GroupManager :: get_user_group_name($row['user_id']))).';';
  289. if ($export_user_fields) {
  290. //show user fields data, if any, for this user
  291. $user_fields_values = UserManager::get_extra_user_data($row['user_id'],false,false, false, true);
  292. foreach($user_fields_values as $value) {
  293. $data .= '"'.str_replace('"','""',api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)).'";';
  294. }
  295. }
  296. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)).';';
  297. $data .= str_replace("\r\n",' ',$row['start_date']).';';
  298. $data .= str_replace("\r\n",' ',$row['end_date']).';';
  299. $data .= str_replace("\r\n",' ',$row['duration']).';';
  300. $data .= str_replace("\r\n",' ',$row['result']).';';
  301. $data .= str_replace("\r\n",' ',$row['max']).';';
  302. $data .= str_replace("\r\n",' ',$row['status']).';';
  303. $data .= "\n";
  304. }
  305. //output the results
  306. $len = strlen($data);
  307. header('Content-type: application/octet-stream');
  308. header('Content-Type: application/force-download');
  309. header('Content-length: '.$len);
  310. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  311. header('Content-Disposition: filename= '.$filename);
  312. } else {
  313. header('Content-Disposition: attachment; filename= '.$filename);
  314. }
  315. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  316. header('Pragma: ');
  317. header('Cache-Control: ');
  318. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  319. }
  320. header('Content-Description: '.$filename);
  321. header('Content-transfer-encoding: binary');
  322. echo $data;
  323. return true;
  324. }
  325. /**
  326. * Exports the complete report as an XLS file
  327. * @return boolean False on error
  328. */
  329. public function exportCompleteReportXLS($document_path='',$user_id = null, $export_user_fields= false, $export_filter = 0, $exercise_id=0, $hotpotato_name = null) {
  330. global $charset;
  331. $this->_getExercisesReporting($document_path, $user_id, $export_filter, $exercise_id, $hotpotato_name);
  332. $filename = 'exercise_results_'.date('YmdGis').'.xls';
  333. if (!empty($user_id)) {
  334. $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.xls';
  335. }
  336. $workbook = new Spreadsheet_Excel_Writer();
  337. $workbook->setTempDir(api_get_path(SYS_ARCHIVE_PATH));
  338. $workbook->setVersion(8); // BIFF8
  339. $workbook->send($filename);
  340. $worksheet =& $workbook->addWorksheet('Report '.date('YmdGis'));
  341. $worksheet->setInputEncoding(api_get_system_encoding());
  342. $line = 0;
  343. $column = 0; //skip the first column (row titles)
  344. // check if exists column 'user'
  345. $with_column_user = false;
  346. foreach ($this->results as $result) {
  347. if (!empty($result['last_name']) && !empty($result['first_name'])) {
  348. $with_column_user = true;
  349. break;
  350. }
  351. }
  352. if ($with_column_user) {
  353. if (api_is_western_name_order()) {
  354. $worksheet->write($line,$column,get_lang('FirstName'));
  355. $column++;
  356. $worksheet->write($line,$column,get_lang('LastName'));
  357. $column++;
  358. } else {
  359. $worksheet->write($line,$column,get_lang('LastName'));
  360. $column++;
  361. $worksheet->write($line,$column,get_lang('FirstName'));
  362. $column++;
  363. }
  364. $worksheet->write($line,$column,get_lang('Email'));
  365. $column++;
  366. }
  367. $worksheet->write($line,$column,get_lang('Groups'));
  368. $column++;
  369. if ($export_user_fields) {
  370. //show user fields section with a big th colspan that spans over all fields
  371. $extra_user_fields = UserManager::get_extra_fields(0,1000,5,'ASC',false, 1);
  372. //show the fields names for user fields
  373. foreach ($extra_user_fields as $field) {
  374. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset));
  375. $column++;
  376. }
  377. }
  378. $worksheet->write($line,$column, get_lang('Title'));
  379. $column++;
  380. $worksheet->write($line,$column, get_lang('StartDate'));
  381. $column++;
  382. $worksheet->write($line,$column, get_lang('EndDate'));
  383. $column++;
  384. $worksheet->write($line,$column, get_lang('Duration').' ('.get_lang('MinMinutes').')');
  385. $column++;
  386. $worksheet->write($line,$column, get_lang('Score'));
  387. $column++;
  388. $worksheet->write($line,$column, get_lang('Total'));
  389. $column++;
  390. $worksheet->write($line,$column, get_lang('Status'));
  391. $line++;
  392. foreach ($this->results as $row) {
  393. $column = 0;
  394. if ($with_column_user) {
  395. if (api_is_western_name_order()) {
  396. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset));
  397. $column++;
  398. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset));
  399. $column++;
  400. } else {
  401. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset));
  402. $column++;
  403. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset));
  404. $column++;
  405. }
  406. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset));
  407. $column++;
  408. }
  409. $worksheet->write($line,$column,api_html_entity_decode(strip_tags(implode(", ", GroupManager :: get_user_group_name($row['user_id']))), ENT_QUOTES, $charset));
  410. $column++;
  411. if ($export_user_fields) {
  412. //show user fields data, if any, for this user
  413. $user_fields_values = UserManager::get_extra_user_data($row['user_id'],false,false, false, true);
  414. foreach($user_fields_values as $value) {
  415. $worksheet->write($line,$column, api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset));
  416. $column++;
  417. }
  418. }
  419. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset));
  420. $column++;
  421. $worksheet->write($line,$column,$row['start_date']);
  422. $column++;
  423. $worksheet->write($line,$column,$row['end_date']);
  424. $column++;
  425. $worksheet->write($line,$column,$row['duration']);
  426. $column++;
  427. $worksheet->write($line,$column,$row['result']);
  428. $column++;
  429. $worksheet->write($line,$column,$row['max']);
  430. $column++;
  431. $worksheet->write($line,$column,$row['status']);
  432. $line++;
  433. }
  434. //output the results
  435. $workbook->close();
  436. return true;
  437. }
  438. }
  439. endif;