gradebook_result.class.php 9.3 KB

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