exercise_result.class.php 20 KB

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