exercise_result.class.php 14 KB

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