gradebook_result.class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /*
  3. DOKEOS - elearning and course management software
  4. For a full list of contributors, see documentation/credits.html
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. See "documentation/licence.html" more details.
  10. Contact:
  11. Dokeos
  12. Rue du Corbeau, 108
  13. B-1030 Brussels - Belgium
  14. info@dokeos.com
  15. */
  16. /**
  17. * ExerciseResult class: This class allows to instantiate an object of type ExerciseResult
  18. * which allows you to export exercises results in multiple presentation forms
  19. * @package dokeos.exercise
  20. * @author Yannick Warnier
  21. * @version $Id: $
  22. */
  23. if(!class_exists('GradeBookResult')):
  24. class GradeBookResult
  25. {
  26. private $gradebook_list = array(); //stores the list of exercises
  27. private $results = array(); //stores the results
  28. /**
  29. * constructor of the class
  30. */
  31. public function GradeBookResult($get_questions=false,$get_answers=false) {
  32. //nothing to do
  33. /*
  34. $this->exercise_list = array();
  35. $this->readExercisesList();
  36. if($get_questions)
  37. {
  38. foreach($this->exercises_list as $exe)
  39. {
  40. $this->exercises_list['questions'] = $this->getExerciseQuestionList($exe['id']);
  41. }
  42. }
  43. */
  44. }
  45. /**
  46. * Reads exercises information (minimal) from the data base
  47. * @param boolean Whether to get only visible exercises (true) or all of them (false). Defaults to false.
  48. * @return array A list of exercises available
  49. */
  50. private function _readGradebookList($only_visible = false) {
  51. $return = array();
  52. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  53. $sql="SELECT id,title,type,random,active FROM $TBL_EXERCISES";
  54. if($only_visible) {
  55. $sql.= ' WHERE active=1';
  56. }
  57. $sql .= ' ORDER BY title';
  58. $result=Database::query($sql,__FILE__,__LINE__);
  59. // if the exercise has been found
  60. while($row=Database::fetch_array($result,'ASSOC')) {
  61. $return[] = $row;
  62. }
  63. // exercise not found
  64. return $return;
  65. }
  66. /**
  67. * Gets the questions related to one exercise
  68. * @param integer Exercise ID
  69. */
  70. private function _readGradeBookQuestionsList($e_id) {
  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, q.position, q.type, q.picture " .
  75. " FROM $TBL_EXERCISE_QUESTION eq, $TBL_QUESTIONS q " .
  76. " WHERE eq.question_id=q.id AND eq.exercice_id='$e_id' " .
  77. " ORDER BY q.position";
  78. $result=Database::query($sql,__FILE__,__LINE__);
  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. $return[] = $row;
  83. }
  84. return true;
  85. }
  86. /**
  87. * Gets the results of all students (or just one student if access is limited)
  88. * @param string The document path (for HotPotatoes retrieval)
  89. * @param integer User ID. Optional. If no user ID is provided, we take all the results. Defauts to null
  90. */
  91. function _getGradeBookReporting($document_path,$user_id=null) {
  92. $return = array();
  93. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  94. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  95. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  96. $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
  97. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  98. $TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  99. $TBL_TRACK_EXERCISES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  100. $TBL_TRACK_HOTPOTATOES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  101. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  102. $cid = api_get_course_id();
  103. if (empty($user_id)) {
  104. //get all results (ourself and the others) as an admin should see them
  105. //AND exe_user_id <> $_user['user_id'] clause has been removed
  106. $sql="SELECT ".(api_is_western_name_order() ? "CONCAT(firstname,' ',lastname)" : "CONCAT(lastname,' ',firstname)").", ce.title, te.exe_result ,
  107. te.exe_weighting, UNIX_TIMESTAMP(te.exe_date),te.exe_id, user.email, user.user_id
  108. FROM $TBL_EXERCISES ce , $TBL_TRACK_EXERCISES te, $TBL_USER user
  109. WHERE te.exe_exo_id = ce.id AND user_id=te.exe_user_id AND te.exe_cours_id='$cid'
  110. ORDER BY te.exe_cours_id ASC, ce.title ASC, te.exe_date ASC";
  111. $hpsql="SELECT ".(api_is_western_name_order() ? "CONCAT(tu.firstname,' ',tu.lastname)" : "CONCAT(tu.lastname,' ',tu.firstname)").", tth.exe_name,
  112. tth.exe_result , tth.exe_weighting, UNIX_TIMESTAMP(tth.exe_date), tu.email, tu.user_id
  113. FROM $TBL_TRACK_HOTPOTATOES tth, $TBL_USER tu
  114. WHERE tu.user_id=tth.exe_user_id AND tth.exe_cours_id = '".$cid."'
  115. ORDER BY tth.exe_cours_id ASC, tth.exe_date ASC";
  116. } else { // get only this user's results
  117. $sql="SELECT '',ce.title, te.exe_result , te.exe_weighting, " .
  118. "UNIX_TIMESTAMP(te.exe_date),te.exe_id
  119. FROM $TBL_EXERCISES ce , $TBL_TRACK_EXERCISES te
  120. WHERE te.exe_exo_id = ce.id AND te.exe_user_id='".$user_id."' AND te.exe_cours_id='$cid'
  121. ORDER BY te.exe_cours_id ASC, ce.title ASC, te.exe_date ASC";
  122. $hpsql="SELECT '',exe_name, exe_result , exe_weighting, UNIX_TIMESTAMP(exe_date)
  123. FROM $TBL_TRACK_HOTPOTATOES
  124. WHERE exe_user_id = '".$user_id."' AND exe_cours_id = '".$cid."'
  125. ORDER BY exe_cours_id ASC, exe_date ASC";
  126. }
  127. $results=getManyResultsXCol($sql,8);
  128. $hpresults=getManyResultsXCol($hpsql,7);
  129. $NoTestRes = 0;
  130. $NoHPTestRes = 0;
  131. $j=0;
  132. //Print the results of tests
  133. if (is_array($results)) {
  134. for ($i = 0; $i < sizeof($results); $i++) {
  135. $return[$i] = array();
  136. $id = $results[$i][5];
  137. $mailid = $results[$i][6];
  138. $user = $results[$i][0];
  139. $test = $results[$i][1];
  140. $dt = strftime(get_lang('dateTimeFormatLong'),$results[$i][4]);
  141. $res = $results[$i][2];
  142. if(empty($user_id)) {
  143. $user = $results[$i][0];
  144. $return[$i]['user'] = $user;
  145. $return[$i]['user_id'] = $results[$i][7];
  146. }
  147. $return[$i]['title'] = $test;
  148. $return[$i]['time'] = format_locale_date(get_lang('dateTimeFormatLong'),$results[$i][4]);
  149. $return[$i]['result'] = $res;
  150. $return[$i]['max'] = $results[$i][3];
  151. $j=$i;
  152. }
  153. }
  154. $j++;
  155. // Print the Result of Hotpotatoes Tests
  156. if (is_array($hpresults)) {
  157. for ($i = 0; $i < sizeof($hpresults); $i++) {
  158. $return[$j+$i] = array();
  159. $title = GetQuizName($hpresults[$i][1],$document_path);
  160. if ($title =='') {
  161. $title = basename($hpresults[$i][1]);
  162. }
  163. if (empty($user_id)) {
  164. $return[$j+$i]['user'] = $hpresults[$i][0];
  165. $return[$j+$i]['user_id'] = $results[$i][6];
  166. }
  167. $return[$j+$i]['title'] = $title;
  168. $return[$j+$i]['time'] = strftime(get_lang('dateTimeFormatLong'),$hpresults[$i][4]);
  169. $return[$j+$i]['result'] = $hpresults[$i][2];
  170. $return[$j+$i]['max'] = $hpresults[$i][3];
  171. }
  172. }
  173. $this->results = $return;
  174. return true;
  175. }
  176. /**
  177. * Exports the complete report as a CSV file
  178. * @param string Document path inside the document tool
  179. * @param integer Optional user ID
  180. * @param boolean Whether to include user fields or not
  181. * @return boolean False on error
  182. */
  183. public function exportCompleteReportCSV($dato) {
  184. //$this->_getGradeBookReporting($document_path,$user_id);
  185. $filename = 'gradebook_results_'.date('YmdGis').'.csv';
  186. if (!empty($user_id)) {
  187. $filename = 'gradebook_results_user_'.$user_id.'_'.date('YmdGis').'.csv';
  188. }
  189. $data = '';
  190. //build the results
  191. //titles
  192. foreach ($dato[0] as $header_col) {
  193. if(!empty($header_col)) {
  194. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($header_col))).';';
  195. }
  196. }
  197. $data .="\r\n";
  198. $cant_students = count($dato[1]);
  199. //print_r($data); exit();
  200. for($i=0;$i<$cant_students;$i++) {
  201. $column = 0;
  202. foreach($dato[1][$i] as $col_name) {
  203. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($col_name))).';';
  204. }
  205. $data .="\r\n";
  206. }
  207. //output the results
  208. $len = strlen($data);
  209. header('Content-type: application/octet-stream');
  210. header('Content-Type: application/force-download');
  211. header('Content-length: '.$len);
  212. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  213. header('Content-Disposition: filename= '.$filename);
  214. } else {
  215. header('Content-Disposition: attachment; filename= '.$filename);
  216. } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  217. header('Pragma: ');
  218. header('Cache-Control: ');
  219. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  220. }
  221. header('Content-Description: '.$filename);
  222. header('Content-transfer-encoding: binary');
  223. echo $data;
  224. return true;
  225. }
  226. /**
  227. * Exports the complete report as an XLS file
  228. * @return boolean False on error
  229. */
  230. public function exportCompleteReportXLS($data) {
  231. $filename = 'gradebook_results_user_'.date('YmdGis').'.xls';
  232. //build the results
  233. require_once(api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php');
  234. $workbook = new Spreadsheet_Excel_Writer();
  235. $workbook->send($filename);
  236. $worksheet =& $workbook->addWorksheet('Report '.date('YmdGis'));
  237. $line = 0;
  238. $column = 0; //skip the first column (row titles)
  239. //headers
  240. foreach ($data[0] as $header_col) {
  241. $worksheet->write($line,$column,$header_col);
  242. $column++;
  243. }
  244. //$worksheet->write($line,$column,get_lang('Total'));
  245. //$column++;
  246. $line++;
  247. $cant_students = count($data[1]);
  248. //print_r($data); exit();
  249. for ($i=0;$i<$cant_students;$i++) {
  250. $column = 0;
  251. foreach ($data[1][$i] as $col_name) {
  252. $worksheet->write($line,$column,strip_tags($col_name));
  253. $column++;
  254. }
  255. $line++;
  256. }
  257. //output the results
  258. $workbook->close();
  259. return true;
  260. }
  261. }
  262. endif;
  263. ?>