exercise_result.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. $cid = api_get_course_id();
  86. $course_id = api_get_course_int_id();
  87. $user_id = intval($user_id);
  88. $session_id_and = ' AND te.session_id = ' . api_get_session_id() . ' ';
  89. $exercise_id = intval($exercise_id);
  90. $hotpotato_name = Database::escape_string($hotpotato_name);
  91. if (!empty($exercise_id)) {
  92. $session_id_and .= " AND exe_exo_id = $exercise_id ";
  93. }
  94. if (empty($user_id)) {
  95. $user_id_and = null;
  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.iid) 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.c_id = '" . $course_id . "' $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.c_id = '" . $course_id . "' AND
  124. tth.exe_name = '$hotpotato_name'
  125. ORDER BY tth.c_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.iid)
  142. INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id)
  143. WHERE ce.c_id = $course_id AND
  144. te.status != 'incomplete' AND
  145. te.c_id ='" . $course_id . "' $user_id_and $session_id_and AND
  146. ce.active <>-1 AND
  147. orig_lp_id = 0 AND
  148. orig_lp_item_id = 0
  149. ORDER BY userpart2, te.c_id ASC, ce.title ASC, te.exe_date DESC";
  150. $hpsql = "SELECT '', exe_name, exe_result , exe_weighting, exe_date
  151. FROM $TBL_TRACK_HOTPOTATOES
  152. WHERE exe_user_id = '" . $user_id . "' AND
  153. c_id = '" . $course_id . "' AND
  154. tth.exe_name = '$hotpotato_name'
  155. ORDER BY c_id ASC, exe_date DESC";
  156. }
  157. $results = array();
  158. $resx = Database::query($sql);
  159. while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  160. $results[] = $rowx;
  161. }
  162. $hpresults = array();
  163. $resx = Database::query($hpsql);
  164. while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  165. $hpresults[] = $rowx;
  166. }
  167. $filter_by_not_revised = false;
  168. $filter_by_revised = false;
  169. if ($filter) {
  170. switch ($filter) {
  171. case 1 :
  172. $filter_by_not_revised = true;
  173. break;
  174. case 2 :
  175. $filter_by_revised = true;
  176. break;
  177. default :
  178. null;
  179. }
  180. }
  181. //Print the results of tests
  182. if (is_array($results) && empty($hotpotato_name)) {
  183. for ($i = 0; $i < sizeof($results); $i++) {
  184. $revised = false;
  185. //revised or not
  186. $sql_exe = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT_RECORDING
  187. WHERE author != '' AND exe_id = ".Database :: escape_string($results[$i]['exid'])." LIMIT 1";
  188. $query = Database::query($sql_exe);
  189. if (Database :: num_rows($query) > 0)
  190. $revised = true;
  191. if ($filter_by_not_revised && $revised) {
  192. continue;
  193. }
  194. if ($filter_by_revised && !$revised) {
  195. continue;
  196. }
  197. $return[$i] = array();
  198. if (empty($user_id)) {
  199. $return[$i]['first_name'] = $results[$i]['userpart1'];
  200. $return[$i]['last_name'] = $results[$i]['userpart2'];
  201. $return[$i]['user_id'] = $results[$i]['excruid'];
  202. $return[$i]['email'] = $results[$i]['exemail'];
  203. }
  204. $return[$i]['title'] = $results[$i]['extitle'];
  205. $return[$i]['start_date'] = api_get_local_time($results[$i]['exstart']);
  206. $return[$i]['end_date'] = api_get_local_time($results[$i]['exdate']);
  207. $return[$i]['duration'] = $results[$i]['duration'];
  208. $return[$i]['result'] = $results[$i]['exresult'];
  209. $return[$i]['max'] = $results[$i]['exweight'];
  210. $return[$i]['status'] = $revised ? get_lang('Validated') : get_lang('NotValidated');
  211. }
  212. }
  213. // Print the Result of Hotpotatoes Tests
  214. if (is_array($hpresults)) {
  215. for($i = 0; $i < sizeof($hpresults); $i++) {
  216. $return[$i] = array();
  217. $title = GetQuizName($hpresults[$i]['exe_name'], $document_path);
  218. if ($title =='') {
  219. $title = basename($hpresults[$i]['exe_name']);
  220. }
  221. if(empty($user_id)) {
  222. $return[$i]['email'] = $hpresults[$i]['email'];
  223. $return[$i]['first_name'] = $hpresults[$i]['userpart1'];
  224. $return[$i]['last_name'] = $hpresults[$i]['userpart2'];
  225. }
  226. $return[$i]['title'] = $title;
  227. $return[$i]['start_date'] = api_get_local_time($results[$i]['exstart']);
  228. $return[$i]['end_date'] = api_get_local_time($results[$i]['exdate']);
  229. $return[$i]['duration'] = $results[$i]['duration'];
  230. $return[$i]['result'] = $hpresults[$i]['exe_result'];
  231. $return[$i]['max'] = $hpresults[$i]['exe_weighting'];
  232. }
  233. }
  234. $this->results = $return;
  235. return true;
  236. }
  237. /**
  238. * Exports the complete report as a CSV file
  239. * @param string Document path inside the document tool
  240. * @param integer Optional user ID
  241. * @param boolean Whether to include user fields or not
  242. * @return boolean False on error
  243. */
  244. public function exportCompleteReportCSV($document_path='',$user_id=null, $export_user_fields = false, $export_filter = 0, $exercise_id = 0, $hotpotato_name = null) {
  245. global $charset;
  246. $this->_getExercisesReporting($document_path,$user_id, $export_filter, $exercise_id, $hotpotato_name);
  247. $filename = 'exercise_results_'.date('YmdGis').'.csv';
  248. if(!empty($user_id)) {
  249. $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.csv';
  250. }
  251. $data = '';
  252. if (api_is_western_name_order()) {
  253. if(!empty($this->results[0]['first_name'])) {
  254. $data .= get_lang('FirstName').';';
  255. }
  256. if(!empty($this->results[0]['last_name'])) {
  257. $data .= get_lang('LastName').';';
  258. }
  259. } else {
  260. if(!empty($this->results[0]['last_name'])) {
  261. $data .= get_lang('LastName').';';
  262. }
  263. if(!empty($this->results[0]['first_name'])) {
  264. $data .= get_lang('FirstName').';';
  265. }
  266. }
  267. $data .= get_lang('Email').';';
  268. $data .= get_lang('Groups').';';
  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. $num = count($extra_user_fields);
  273. foreach($extra_user_fields as $field) {
  274. $data .= '"'.str_replace("\r\n",' ',api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset)).'";';
  275. }
  276. }
  277. $data .= get_lang('Title').';';
  278. $data .= get_lang('StartDate').';';
  279. $data .= get_lang('EndDate').';';
  280. $data .= get_lang('Duration'). ' ('.get_lang('MinMinutes').') ;';
  281. $data .= get_lang('Score').';';
  282. $data .= get_lang('Total').';';
  283. $data .= get_lang('Status').';';
  284. $data .= "\n";
  285. //results
  286. foreach($this->results as $row) {
  287. if (api_is_western_name_order()) {
  288. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
  289. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
  290. } else {
  291. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
  292. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
  293. }
  294. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset)).';';
  295. $data .= str_replace("\r\n",' ',implode(", ", GroupManager :: get_user_group_name($row['user_id']))).';';
  296. if ($export_user_fields) {
  297. //show user fields data, if any, for this user
  298. $user_fields_values = UserManager::get_extra_user_data($row['user_id'],false,false, false, true);
  299. foreach($user_fields_values as $value) {
  300. $data .= '"'.str_replace('"','""',api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)).'";';
  301. }
  302. }
  303. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)).';';
  304. $data .= str_replace("\r\n",' ',$row['start_date']).';';
  305. $data .= str_replace("\r\n",' ',$row['end_date']).';';
  306. $data .= str_replace("\r\n",' ',$row['duration']).';';
  307. $data .= str_replace("\r\n",' ',$row['result']).';';
  308. $data .= str_replace("\r\n",' ',$row['max']).';';
  309. $data .= str_replace("\r\n",' ',$row['status']).';';
  310. $data .= "\n";
  311. }
  312. //output the results
  313. $len = strlen($data);
  314. header('Content-type: application/octet-stream');
  315. header('Content-Type: application/force-download');
  316. header('Content-length: '.$len);
  317. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  318. header('Content-Disposition: filename= '.$filename);
  319. } else {
  320. header('Content-Disposition: attachment; filename= '.$filename);
  321. }
  322. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  323. header('Pragma: ');
  324. header('Cache-Control: ');
  325. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  326. }
  327. header('Content-Description: '.$filename);
  328. header('Content-transfer-encoding: binary');
  329. echo $data;
  330. return true;
  331. }
  332. /**
  333. * Exports the complete report as an XLS file
  334. * @return boolean False on error
  335. */
  336. public function exportCompleteReportXLS($document_path='',$user_id = null, $export_user_fields= false, $export_filter = 0, $exercise_id=0, $hotpotato_name = null) {
  337. global $charset;
  338. $this->_getExercisesReporting($document_path, $user_id, $export_filter, $exercise_id, $hotpotato_name);
  339. $filename = 'exercise_results_'.date('YmdGis').'.xls';
  340. if (!empty($user_id)) {
  341. $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.xls';
  342. }
  343. $workbook = new Spreadsheet_Excel_Writer();
  344. $workbook->setTempDir(api_get_path(SYS_ARCHIVE_PATH));
  345. $workbook->setVersion(8); // BIFF8
  346. $workbook->send($filename);
  347. $worksheet =& $workbook->addWorksheet('Report '.date('YmdGis'));
  348. $worksheet->setInputEncoding(api_get_system_encoding());
  349. $line = 0;
  350. $column = 0; //skip the first column (row titles)
  351. // check if exists column 'user'
  352. $with_column_user = false;
  353. foreach ($this->results as $result) {
  354. if (!empty($result['last_name']) && !empty($result['first_name'])) {
  355. $with_column_user = true;
  356. break;
  357. }
  358. }
  359. if ($with_column_user) {
  360. if (api_is_western_name_order()) {
  361. $worksheet->write($line,$column,get_lang('FirstName'));
  362. $column++;
  363. $worksheet->write($line,$column,get_lang('LastName'));
  364. $column++;
  365. } else {
  366. $worksheet->write($line,$column,get_lang('LastName'));
  367. $column++;
  368. $worksheet->write($line,$column,get_lang('FirstName'));
  369. $column++;
  370. }
  371. $worksheet->write($line,$column,get_lang('Email'));
  372. $column++;
  373. }
  374. $worksheet->write($line,$column,get_lang('Groups'));
  375. $column++;
  376. if ($export_user_fields) {
  377. //show user fields section with a big th colspan that spans over all fields
  378. $extra_user_fields = UserManager::get_extra_fields(0,1000,5,'ASC',false, 1);
  379. //show the fields names for user fields
  380. foreach ($extra_user_fields as $field) {
  381. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset));
  382. $column++;
  383. }
  384. }
  385. $worksheet->write($line,$column, get_lang('Title'));
  386. $column++;
  387. $worksheet->write($line,$column, get_lang('StartDate'));
  388. $column++;
  389. $worksheet->write($line,$column, get_lang('EndDate'));
  390. $column++;
  391. $worksheet->write($line,$column, get_lang('Duration').' ('.get_lang('MinMinutes').')');
  392. $column++;
  393. $worksheet->write($line,$column, get_lang('Score'));
  394. $column++;
  395. $worksheet->write($line,$column, get_lang('Total'));
  396. $column++;
  397. $worksheet->write($line,$column, get_lang('Status'));
  398. $line++;
  399. foreach ($this->results as $row) {
  400. $column = 0;
  401. if ($with_column_user) {
  402. if (api_is_western_name_order()) {
  403. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset));
  404. $column++;
  405. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset));
  406. $column++;
  407. } else {
  408. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset));
  409. $column++;
  410. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset));
  411. $column++;
  412. }
  413. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset));
  414. $column++;
  415. }
  416. $worksheet->write($line,$column,api_html_entity_decode(strip_tags(implode(", ", GroupManager :: get_user_group_name($row['user_id']))), ENT_QUOTES, $charset));
  417. $column++;
  418. if ($export_user_fields) {
  419. //show user fields data, if any, for this user
  420. $user_fields_values = UserManager::get_extra_user_data($row['user_id'],false,false, false, true);
  421. foreach($user_fields_values as $value) {
  422. $worksheet->write($line,$column, api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset));
  423. $column++;
  424. }
  425. }
  426. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset));
  427. $column++;
  428. $worksheet->write($line,$column,$row['start_date']);
  429. $column++;
  430. $worksheet->write($line,$column,$row['end_date']);
  431. $column++;
  432. $worksheet->write($line,$column,$row['duration']);
  433. $column++;
  434. $worksheet->write($line,$column,$row['result']);
  435. $column++;
  436. $worksheet->write($line,$column,$row['max']);
  437. $column++;
  438. $worksheet->write($line,$column,$row['status']);
  439. $line++;
  440. }
  441. //output the results
  442. $workbook->close();
  443. return true;
  444. }
  445. }