exercise_result.class.php 18 KB

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