exercise_result.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  83. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  84. $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
  85. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  86. $TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  87. $TBL_TRACK_EXERCISES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  88. $TBL_TRACK_HOTPOTATOES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  89. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  90. $TBL_TRACK_ATTEMPT_RECORDING= Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  91. $TBL_COURSE_REL_USER = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  92. $cid = api_get_course_id();
  93. $course_id = api_get_course_int_id();
  94. $user_id = intval($user_id);
  95. $session_id_and = ' AND te.session_id = ' . api_get_session_id() . ' ';
  96. $exercise_id = intval($exercise_id);
  97. $hotpotato_name = Database::escape_string($hotpotato_name);
  98. if (!empty($exercise_id)) {
  99. $session_id_and .= " AND exe_exo_id = $exercise_id ";
  100. }
  101. if (empty($user_id)) {
  102. $sql="SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").", ce.title as extitle, te.exe_result as exresult , te.exe_weighting as exweight,
  103. te.exe_date as exdate, te.exe_id as exid, email as exemail, te.start_date as exstart, steps_counter as exstep, exe_user_id as excruid, te.exe_duration as exduration
  104. FROM $TBL_EXERCISES AS ce INNER JOIN $TBL_TRACK_EXERCISES AS te ON (te.exe_exo_id = ce.id) INNER JOIN
  105. $TBL_USER AS user ON (user.user_id = exe_user_id)
  106. WHERE ce.c_id = $course_id AND
  107. te.status != 'incomplete' AND
  108. te.exe_cours_id='" . Database :: escape_string($cid) . "' $user_id_and $session_id_and AND
  109. ce.active <>-1 AND
  110. orig_lp_id = 0 AND
  111. orig_lp_item_id = 0";
  112. $hpsql="SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").", email, tth.exe_name, tth.exe_result , tth.exe_weighting, tth.exe_date
  113. FROM $TBL_TRACK_HOTPOTATOES tth, $TBL_USER tu
  114. WHERE tu.user_id=tth.exe_user_id AND
  115. tth.exe_cours_id = '" . Database :: escape_string($cid) . "' AND
  116. tth.exe_name = '$hotpotato_name'
  117. ORDER BY tth.exe_cours_id ASC, tth.exe_date DESC";
  118. } else {
  119. $user_id_and = ' AND te.exe_user_id = ' . api_get_user_id() . ' ';
  120. // get only this user's results
  121. $sql="SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").", ce.title as extitle, te.exe_result as exresult, " .
  122. "te.exe_weighting as exweight, te.exe_date as exdate, te.exe_id as exid, email as exemail, " .
  123. "te.start_date as exstart, steps_counter as exstep, exe_user_id as excruid, te.exe_duration as exduration, ce.results_disabled as exdisabled
  124. 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)
  125. WHERE ce.c_id = $course_id AND
  126. te.status != 'incomplete' AND
  127. te.exe_cours_id='" . Database :: escape_string($cid) . "' $user_id_and $session_id_and AND
  128. ce.active <>-1 AND
  129. orig_lp_id = 0 AND
  130. orig_lp_item_id = 0
  131. ORDER BY userpart2, te.exe_cours_id ASC, ce.title ASC, te.exe_date DESC";
  132. $hpsql = "SELECT '', exe_name, exe_result , exe_weighting, exe_date
  133. FROM $TBL_TRACK_HOTPOTATOES
  134. WHERE exe_user_id = '" . $user_id . "' AND
  135. exe_cours_id = '" . Database :: escape_string($cid) . "' AND
  136. tth.exe_name = '$hotpotato_name'
  137. ORDER BY exe_cours_id ASC, exe_date DESC";
  138. }
  139. $results = array();
  140. $resx = Database::query($sql);
  141. while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  142. $results[] = $rowx;
  143. }
  144. $hpresults = array();
  145. $resx = Database::query($hpsql);
  146. while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  147. $hpresults[] = $rowx;
  148. }
  149. if ($filter) {
  150. switch ($filter) {
  151. case 1 :
  152. $filter_by_not_revised = true;
  153. break;
  154. case 2 :
  155. $filter_by_revised = true;
  156. break;
  157. default :
  158. null;
  159. }
  160. }
  161. //Print the results of tests
  162. if (is_array($results) && empty($hotpotato_name)) {
  163. for ($i = 0; $i < sizeof($results); $i++) {
  164. $revised = false;
  165. //revised or not
  166. $sql_exe = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT_RECORDING
  167. WHERE author != '' AND exe_id = ".Database :: escape_string($results[$i]['exid'])." LIMIT 1";
  168. $query = Database::query($sql_exe);
  169. if (Database :: num_rows($query) > 0)
  170. $revised = true;
  171. if ($filter_by_not_revised && $revised) continue;
  172. if ($filter_by_revised && !$revised) continue;
  173. $return[$i] = array();
  174. if (empty($user_id)) {
  175. $return[$i]['first_name'] = $results[$i]['userpart1'];
  176. $return[$i]['last_name'] = $results[$i]['userpart2'];
  177. $return[$i]['user_id'] = $results[$i]['excruid'];
  178. $return[$i]['email'] = $results[$i]['exemail'];
  179. }
  180. $return[$i]['title'] = $results[$i]['extitle'];
  181. $return[$i]['time'] = api_convert_and_format_date($results[$i]['exdate']);
  182. $return[$i]['result'] = $results[$i]['exresult'];
  183. $return[$i]['max'] = $results[$i]['exweight'];
  184. $return[$i]['status'] = $revised ? get_lang('Validated') : get_lang('NotValidated');
  185. }
  186. }
  187. // Print the Result of Hotpotatoes Tests
  188. if (is_array($hpresults)) {
  189. for($i = 0; $i < sizeof($hpresults); $i++) {
  190. $return[$i] = array();
  191. $title = GetQuizName($hpresults[$i]['exe_name'], $document_path);
  192. if ($title =='') {
  193. $title = basename($hpresults[$i]['exe_name']);
  194. }
  195. if(empty($user_id)) {
  196. $return[$i]['email'] = $hpresults[$i]['email'];
  197. $return[$i]['first_name'] = $hpresults[$i]['userpart1'];
  198. $return[$i]['last_name'] = $hpresults[$i]['userpart2'];
  199. }
  200. $return[$i]['title'] = $title;
  201. $return[$i]['time'] = api_convert_and_format_date($hpresults[$i]['exdate']);
  202. $return[$i]['result'] = $hpresults[$i]['exe_result'];
  203. $return[$i]['max'] = $hpresults[$i]['exe_weighting'];
  204. }
  205. }
  206. $this->results = $return;
  207. return true;
  208. }
  209. /**
  210. * Exports the complete report as a CSV file
  211. * @param string Document path inside the document tool
  212. * @param integer Optional user ID
  213. * @param boolean Whether to include user fields or not
  214. * @return boolean False on error
  215. */
  216. public function exportCompleteReportCSV($document_path='',$user_id=null, $export_user_fields = false, $export_filter = 0, $exercise_id = 0, $hotpotato_name = null) {
  217. global $charset;
  218. $this->_getExercisesReporting($document_path,$user_id, $export_filter, $exercise_id, $hotpotato_name);
  219. $filename = 'exercise_results_'.date('YmdGis').'.csv';
  220. if(!empty($user_id)) {
  221. $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.csv';
  222. }
  223. $data = '';
  224. if (api_is_western_name_order()) {
  225. if(!empty($this->results[0]['first_name'])) {
  226. $data .= get_lang('FirstName').';';
  227. }
  228. if(!empty($this->results[0]['last_name'])) {
  229. $data .= get_lang('LastName').';';
  230. }
  231. } else {
  232. if(!empty($this->results[0]['last_name'])) {
  233. $data .= get_lang('LastName').';';
  234. }
  235. if(!empty($this->results[0]['first_name'])) {
  236. $data .= get_lang('FirstName').';';
  237. }
  238. }
  239. $data .= get_lang('Email').';';
  240. if ($export_user_fields) {
  241. //show user fields section with a big th colspan that spans over all fields
  242. $extra_user_fields = UserManager::get_extra_fields(0,1000,5,'ASC',false, 1);
  243. $num = count($extra_user_fields);
  244. foreach($extra_user_fields as $field) {
  245. $data .= '"'.str_replace("\r\n",' ',api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset)).'";';
  246. }
  247. }
  248. $data .= get_lang('Title').';';
  249. $data .= get_lang('StartDate').';';
  250. $data .= get_lang('Score').';';
  251. $data .= get_lang('Total').';';
  252. $data .= get_lang('Status').';';
  253. $data .= "\n";
  254. //results
  255. foreach($this->results as $row) {
  256. if (api_is_western_name_order()) {
  257. if(!empty($row['first_name'])) {
  258. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
  259. }
  260. if(!empty($row['last_name'])) {
  261. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
  262. }
  263. } else {
  264. if(!empty($row['last_name'])) {
  265. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
  266. }
  267. if(!empty($row['first_name'])) {
  268. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
  269. }
  270. }
  271. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset)).';';
  272. if ($export_user_fields) {
  273. //show user fields data, if any, for this user
  274. $user_fields_values = UserManager::get_extra_user_data($row['user_id'],false,false, false, true);
  275. foreach($user_fields_values as $value) {
  276. $data .= '"'.str_replace('"','""',api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)).'";';
  277. }
  278. }
  279. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)).';';
  280. $data .= str_replace("\r\n",' ',$row['time']).';';
  281. $data .= str_replace("\r\n",' ',$row['result']).';';
  282. $data .= str_replace("\r\n",' ',$row['max']).';';
  283. $data .= str_replace("\r\n",' ',$row['status']).';';
  284. $data .= "\n";
  285. }
  286. //output the results
  287. $len = strlen($data);
  288. header('Content-type: application/octet-stream');
  289. header('Content-Type: application/force-download');
  290. header('Content-length: '.$len);
  291. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  292. header('Content-Disposition: filename= '.$filename);
  293. } else {
  294. header('Content-Disposition: attachment; filename= '.$filename);
  295. }
  296. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  297. header('Pragma: ');
  298. header('Cache-Control: ');
  299. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  300. }
  301. header('Content-Description: '.$filename);
  302. header('Content-transfer-encoding: binary');
  303. echo $data;
  304. return true;
  305. }
  306. /**
  307. * Exports the complete report as an XLS file
  308. * @return boolean False on error
  309. */
  310. public function exportCompleteReportXLS($document_path='',$user_id = null, $export_user_fields= false, $export_filter = 0, $exercise_id=0, $hotpotato_name = null) {
  311. global $charset;
  312. $this->_getExercisesReporting($document_path, $user_id, $export_filter, $exercise_id, $hotpotato_name);
  313. $filename = 'exercise_results_'.date('YmdGis').'.xls';
  314. if(!empty($user_id)) {
  315. $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.xls';
  316. }
  317. require_once api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php';
  318. $workbook = new Spreadsheet_Excel_Writer();
  319. $workbook->setTempDir(api_get_path(SYS_ARCHIVE_PATH));
  320. $workbook->setVersion(8); // BIFF8
  321. $workbook->send($filename);
  322. $worksheet =& $workbook->addWorksheet('Report '.date('YmdGis'));
  323. $worksheet->setInputEncoding(api_get_system_encoding());
  324. $line = 0;
  325. $column = 0; //skip the first column (row titles)
  326. // check if exists column 'user'
  327. $with_column_user = false;
  328. foreach ($this->results as $result) {
  329. if (!empty($result['last_name']) && !empty($result['first_name'])) {
  330. $with_column_user = true;
  331. break;
  332. }
  333. }
  334. if ($with_column_user) {
  335. $worksheet->write($line,$column,get_lang('Email'));
  336. $column++;
  337. if (api_is_western_name_order()) {
  338. $worksheet->write($line,$column,get_lang('FirstName'));
  339. $column++;
  340. $worksheet->write($line,$column,get_lang('LastName'));
  341. $column++;
  342. } else {
  343. $worksheet->write($line,$column,get_lang('LastName'));
  344. $column++;
  345. $worksheet->write($line,$column,get_lang('FirstName'));
  346. $column++;
  347. }
  348. }
  349. if ($export_user_fields) {
  350. //show user fields section with a big th colspan that spans over all fields
  351. $extra_user_fields = UserManager::get_extra_fields(0,1000,5,'ASC',false, 1);
  352. //show the fields names for user fields
  353. foreach($extra_user_fields as $field) {
  354. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset));
  355. $column++;
  356. }
  357. }
  358. $worksheet->write($line,$column,get_lang('Title'));
  359. $column++;
  360. $worksheet->write($line,$column,get_lang('StartDate'));
  361. $column++;
  362. $worksheet->write($line,$column,get_lang('Score'));
  363. $column++;
  364. $worksheet->write($line,$column,get_lang('Total'));
  365. $column++;
  366. $worksheet->write($line,$column,get_lang('Status'));
  367. $line++;
  368. foreach($this->results as $row) {
  369. $column = 0;
  370. if(!empty($row['last_name']) && !empty($row['first_name'])) {
  371. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset));
  372. $column++;
  373. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset));
  374. $column++;
  375. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset));
  376. $column++;
  377. }
  378. if ($export_user_fields) {
  379. //show user fields data, if any, for this user
  380. $user_fields_values = UserManager::get_extra_user_data($row['user_id'],false,false, false, true);
  381. foreach($user_fields_values as $value) {
  382. $worksheet->write($line,$column, api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset));
  383. $column++;
  384. }
  385. }
  386. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset));
  387. $column++;
  388. $worksheet->write($line,$column,$row['time']);
  389. $column++;
  390. $worksheet->write($line,$column,$row['result']);
  391. $column++;
  392. $worksheet->write($line,$column,$row['max']);
  393. $column++;
  394. $worksheet->write($line,$column,$row['status']);
  395. $line++;
  396. }
  397. //output the results
  398. $workbook->close();
  399. return true;
  400. }
  401. }
  402. endif;