exercise_result.class.php 16 KB

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