exercise_result.class.php 16 KB

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