exercise_result.class.php 14 KB

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