exercise_result.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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=api_sql_query($sql,__FILE__,__LINE__);
  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=api_sql_query($sql,__FILE__,__LINE__);
  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)
  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. $cid = api_get_course_id();
  111. if(empty($user_id))
  112. {
  113. //get all results (ourself and the others) as an admin should see them
  114. //AND exe_user_id <> $_user['user_id'] clause has been removed
  115. $sql="SELECT CONCAT(lastname,' ',firstname),ce.title, te.exe_result ,
  116. te.exe_weighting, UNIX_TIMESTAMP(te.exe_date),te.exe_id, user.email, user.user_id
  117. FROM $TBL_EXERCISES ce , $TBL_TRACK_EXERCISES te, $TBL_USER user
  118. WHERE te.exe_exo_id = ce.id AND user_id=te.exe_user_id AND te.exe_cours_id='$cid'
  119. ORDER BY te.exe_cours_id ASC, ce.title ASC, te.exe_date ASC";
  120. $hpsql="SELECT CONCAT(tu.lastname,' ',tu.firstname), tth.exe_name,
  121. tth.exe_result , tth.exe_weighting, UNIX_TIMESTAMP(tth.exe_date), tu.email, tu.user_id
  122. FROM $TBL_TRACK_HOTPOTATOES tth, $TBL_USER tu
  123. WHERE tu.user_id=tth.exe_user_id AND tth.exe_cours_id = '".$cid."'
  124. ORDER BY tth.exe_cours_id ASC, tth.exe_date ASC";
  125. }
  126. else
  127. { // get only this user's results
  128. $sql="SELECT '',ce.title, te.exe_result , te.exe_weighting, " .
  129. "UNIX_TIMESTAMP(te.exe_date),te.exe_id
  130. FROM $TBL_EXERCISES ce , $TBL_TRACK_EXERCISES te
  131. WHERE te.exe_exo_id = ce.id AND te.exe_user_id='".Database::escape_string($user_id)."' AND te.exe_cours_id='".Database::escape_string($cid)."'
  132. ORDER BY te.exe_cours_id ASC, ce.title ASC, te.exe_date ASC";
  133. $hpsql="SELECT '',exe_name, exe_result , exe_weighting, UNIX_TIMESTAMP(exe_date)
  134. FROM $TBL_TRACK_HOTPOTATOES
  135. WHERE exe_user_id = '".Database::escape_string($user_id)."' AND exe_cours_id = '".Database::escape_string($cid)."'
  136. ORDER BY exe_cours_id ASC, exe_date ASC";
  137. }
  138. $results=getManyResultsXCol($sql,8);
  139. $hpresults=getManyResultsXCol($hpsql,7);
  140. $NoTestRes = 0;
  141. $NoHPTestRes = 0;
  142. $j=0;
  143. //Print the results of tests
  144. if(is_array($results))
  145. {
  146. for($i = 0; $i < sizeof($results); $i++)
  147. {
  148. $return[$i] = array();
  149. $id = $results[$i][5];
  150. $mailid = $results[$i][6];
  151. $user = $results[$i][0];
  152. $test = $results[$i][1];
  153. $dt = strftime(get_lang('dateTimeFormatLong'),$results[$i][4]);
  154. $res = $results[$i][2];
  155. if(empty($user_id))
  156. {
  157. $user = $results[$i][0];
  158. $return[$i]['user'] = $user;
  159. $return[$i]['user_id'] = $results[$i][7];
  160. }
  161. $return[$i]['title'] = $test;
  162. $return[$i]['time'] = format_locale_date(get_lang('dateTimeFormatLong'),$results[$i][4]);
  163. $return[$i]['result'] = $res;
  164. $return[$i]['max'] = $results[$i][3];
  165. $j=$i;
  166. }
  167. }
  168. $j++;
  169. // Print the Result of Hotpotatoes Tests
  170. if(is_array($hpresults))
  171. {
  172. for($i = 0; $i < sizeof($hpresults); $i++)
  173. {
  174. $return[$j+$i] = array();
  175. $title = GetQuizName($hpresults[$i][1],$document_path);
  176. if ($title =='')
  177. {
  178. $title = GetFileName($hpresults[$i][1]);
  179. }
  180. if(empty($user_id))
  181. {
  182. $return[$j+$i]['user'] = $hpresults[$i][0];
  183. $return[$j+$i]['user_id'] = $results[$i][6];
  184. }
  185. $return[$j+$i]['title'] = $title;
  186. $return[$j+$i]['time'] = strftime(get_lang('dateTimeFormatLong'),$hpresults[$i][4]);
  187. $return[$j+$i]['result'] = $hpresults[$i][2];
  188. $return[$j+$i]['max'] = $hpresults[$i][3];
  189. }
  190. }
  191. $this->results = $return;
  192. return true;
  193. }
  194. /**
  195. * Exports the complete report as a CSV file
  196. * @param string Document path inside the document tool
  197. * @param integer Optional user ID
  198. * @param boolean Whether to include user fields or not
  199. * @return boolean False on error
  200. */
  201. public function exportCompleteReportCSV($document_path='',$user_id=null, $export_user_fields)
  202. {
  203. global $charset;
  204. $this->_getExercisesReporting($document_path,$user_id);
  205. $filename = 'exercise_results_'.date('YmdGis').'.csv';
  206. if(!empty($user_id))
  207. {
  208. $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.csv';
  209. }
  210. $data = '';
  211. //build the results
  212. //titles
  213. if(!empty($this->results[0]['user']))
  214. {
  215. $data .= get_lang('User').';';
  216. }
  217. if($export_user_fields)
  218. {
  219. //show user fields section with a big th colspan that spans over all fields
  220. $extra_user_fields = UserManager::get_extra_fields(0,0,5,'ASC',false);
  221. $num = count($extra_user_fields);
  222. foreach($extra_user_fields as $field)
  223. {
  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('Results').';';
  231. $data .= get_lang('Weighting').';';
  232. $data .= "\n";
  233. //results
  234. foreach($this->results as $row)
  235. {
  236. if(!empty($row['user']))
  237. {
  238. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['user']), ENT_QUOTES, $charset)).';';
  239. }
  240. if($export_user_fields)
  241. {
  242. //show user fields data, if any, for this user
  243. $user_fields_values = UserManager::get_extra_user_data(intval($row['user_id']),false,false);
  244. foreach($user_fields_values as $value)
  245. {
  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. {
  262. header('Content-Disposition: filename= '.$filename);
  263. }
  264. else
  265. {
  266. header('Content-Disposition: attachment; filename= '.$filename);
  267. }
  268. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
  269. {
  270. header('Pragma: ');
  271. header('Cache-Control: ');
  272. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  273. }
  274. header('Content-Description: '.$filename);
  275. header('Content-transfer-encoding: binary');
  276. echo $data;
  277. return true;
  278. }
  279. /**
  280. * Exports the complete report as an XLS file
  281. * @return boolean False on error
  282. */
  283. public function exportCompleteReportXLS($document_path='',$user_id=null, $export_user_fields)
  284. {
  285. global $charset;
  286. $this->_getExercisesReporting($document_path,$user_id);
  287. $filename = 'exercise_results_'.date('YmdGis').'.xls';
  288. if(!empty($user_id))
  289. {
  290. $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.xls';
  291. } //build the results
  292. require_once(api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php');
  293. $workbook = new Spreadsheet_Excel_Writer();
  294. $workbook->send($filename);
  295. $worksheet =& $workbook->addWorksheet('Report '.date('YmdGis'));
  296. $line = 0;
  297. $column = 0; //skip the first column (row titles)
  298. if(!empty($this->results[0]['user']))
  299. {
  300. $worksheet->write($line,$column,get_lang('User'));
  301. $column++;
  302. }
  303. if($export_user_fields)
  304. {
  305. //show user fields section with a big th colspan that spans over all fields
  306. $extra_user_fields = UserManager::get_extra_fields(0,0,5,'ASC',false);
  307. //show the fields names for user fields
  308. foreach($extra_user_fields as $field)
  309. {
  310. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset));
  311. $column++;
  312. }
  313. }
  314. $worksheet->write($line,$column,get_lang('Title'));
  315. $column++;
  316. $worksheet->write($line,$column,get_lang('Date'));
  317. $column++;
  318. $worksheet->write($line,$column,get_lang('Results'));
  319. $column++;
  320. $worksheet->write($line,$column,get_lang('Weighting'));
  321. $line++;
  322. foreach($this->results as $row)
  323. {
  324. $column = 0;
  325. if(!empty($row['user']))
  326. {
  327. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['user']), ENT_QUOTES, $charset));
  328. $column++;
  329. }
  330. //show user fields data, if any, for this user
  331. $user_fields_values = UserManager::get_extra_user_data(intval($row['user_id']),false,false);
  332. foreach($user_fields_values as $value)
  333. {
  334. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset));
  335. $column++;
  336. }
  337. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset));
  338. $column++;
  339. $worksheet->write($line,$column,$row['time']);
  340. $column++;
  341. $worksheet->write($line,$column,$row['result']);
  342. $column++;
  343. $worksheet->write($line,$column,$row['max']);
  344. $line++;
  345. }
  346. //output the results
  347. $workbook->close();
  348. return true;
  349. }
  350. }
  351. endif;
  352. ?>