gradebook_result.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. /**
  10. * Gradebook results class
  11. * @package chamilo.gradebook
  12. */
  13. class GradeBookResult
  14. {
  15. private $gradebook_list = array(); //stores the list of exercises
  16. private $results = array(); //stores the results
  17. /**
  18. * constructor of the class
  19. */
  20. public function GradeBookResult($get_questions=false,$get_answers=false) {
  21. //nothing to do
  22. /*
  23. $this->exercise_list = array();
  24. $this->readExercisesList();
  25. if($get_questions)
  26. {
  27. foreach($this->exercises_list as $exe)
  28. {
  29. $this->exercises_list['questions'] = $this->getExerciseQuestionList($exe['id']);
  30. }
  31. }
  32. */
  33. }
  34. /**
  35. * Reads exercises information (minimal) from the data base
  36. * @param boolean Whether to get only visible exercises (true) or all of them (false). Defaults to false.
  37. * @return array A list of exercises available
  38. */
  39. private function _readGradebookList($only_visible = false) {
  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. $sql.= ' WHERE active=1';
  45. }
  46. $sql .= ' ORDER BY title';
  47. $result=Database::query($sql);
  48. // if the exercise has been found
  49. while($row=Database::fetch_array($result,'ASSOC')) {
  50. $return[] = $row;
  51. }
  52. // exercise not found
  53. return $return;
  54. }
  55. /**
  56. * Gets the questions related to one exercise
  57. * @param integer Exercise ID
  58. */
  59. private function _readGradeBookQuestionsList($e_id) {
  60. $return = array();
  61. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  62. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  63. $course_id = api_get_course_int_id();
  64. $sql="SELECT q.id, q.question, q.ponderation, q.position, q.type, q.picture " .
  65. " FROM $TBL_EXERCISE_QUESTION eq, $TBL_QUESTIONS q " .
  66. " WHERE eq.c_di = $course_id AND
  67. q.c_di = $course_id AND
  68. eq.question_id=q.id AND
  69. eq.exercice_id='$e_id' " .
  70. " ORDER BY q.position";
  71. $result = Database::query($sql);
  72. // fills the array with the question ID for this exercise
  73. // the key of the array is the question position
  74. while($row=Database::fetch_array($result,'ASSOC')) {
  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 _getGradeBookReporting($document_path,$user_id=null) {
  85. $return = array();
  86. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  87. $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
  88. $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  89. $TBL_TRACK_HOTPOTATOES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  90. $cid = api_get_course_id();
  91. $course_id = api_get_course_int_id();
  92. if (empty($user_id)) {
  93. //get all results (ourself and the others) as an admin should see them
  94. //AND exe_user_id <> $_user['user_id'] clause has been removed
  95. $sql="SELECT ".(api_is_western_name_order() ? "CONCAT(firstname,' ',lastname)" : "CONCAT(lastname,' ',firstname)").", ce.title, te.exe_result ,
  96. te.exe_weighting, te.exe_date,te.exe_id, user.email, user.user_id
  97. FROM $TBL_EXERCISES ce , $TBL_TRACK_EXERCISES te, $TBL_USER user
  98. WHERE ce.c_id = $course_id AND
  99. te.exe_exo_id = ce.id AND
  100. user_id=te.exe_user_id AND te.c_id = '$course_id'
  101. ORDER BY te.c_id ASC, ce.title ASC, te.exe_date ASC";
  102. $hpsql="SELECT ".(api_is_western_name_order() ? "CONCAT(tu.firstname,' ',tu.lastname)" : "CONCAT(tu.lastname,' ',tu.firstname)").", tth.exe_name,
  103. tth.exe_result , tth.exe_weighting, tth.exe_date, tu.email, tu.user_id
  104. FROM $TBL_TRACK_HOTPOTATOES tth, $TBL_USER tu
  105. WHERE tu.user_id=tth.exe_user_id AND tth.c_id = '".$course_id."'
  106. ORDER BY tth.c_id ASC, tth.exe_date ASC";
  107. } else { // get only this user's results
  108. $sql = "SELECT '',ce.title, te.exe_result , te.exe_weighting, te.exe_date,te.exe_id
  109. FROM $TBL_EXERCISES ce , $TBL_TRACK_EXERCISES te
  110. WHERE ce.c_id = $course_id AND
  111. te.exe_exo_id = ce.id AND
  112. te.exe_user_id = '".$user_id."' AND
  113. te.c_id = '$course_id'
  114. ORDER BY te.c_id ASC, ce.title ASC, te.exe_date ASC";
  115. $hpsql="SELECT '',exe_name, exe_result , exe_weighting, exe_date
  116. FROM $TBL_TRACK_HOTPOTATOES
  117. WHERE exe_user_id = '".$user_id."' AND c_id = '".$course_id."'
  118. ORDER BY c_id ASC, exe_date ASC";
  119. }
  120. $results=getManyResultsXCol($sql,8);
  121. $hpresults=getManyResultsXCol($hpsql,7);
  122. $NoTestRes = 0;
  123. $NoHPTestRes = 0;
  124. $j=0;
  125. //Print the results of tests
  126. if (is_array($results)) {
  127. for ($i = 0; $i < sizeof($results); $i++) {
  128. $return[$i] = array();
  129. $id = $results[$i][5];
  130. $mailid = $results[$i][6];
  131. $user = $results[$i][0];
  132. $test = $results[$i][1];
  133. $res = $results[$i][2];
  134. if(empty($user_id)) {
  135. $user = $results[$i][0];
  136. $return[$i]['user'] = $user;
  137. $return[$i]['user_id'] = $results[$i][7];
  138. }
  139. $return[$i]['title'] = $test;
  140. $return[$i]['time'] = api_convert_and_format_date($results[$i][4], null, date_default_timezone_get());
  141. $return[$i]['result'] = $res;
  142. $return[$i]['max'] = $results[$i][3];
  143. $j=$i;
  144. }
  145. }
  146. $j++;
  147. // Print the Result of Hotpotatoes Tests
  148. if (is_array($hpresults)) {
  149. for ($i = 0; $i < sizeof($hpresults); $i++) {
  150. $return[$j+$i] = array();
  151. $title = GetQuizName($hpresults[$i][1],$document_path);
  152. if ($title =='') {
  153. $title = basename($hpresults[$i][1]);
  154. }
  155. if (empty($user_id)) {
  156. $return[$j+$i]['user'] = $hpresults[$i][0];
  157. $return[$j+$i]['user_id'] = $results[$i][6];
  158. }
  159. $return[$j+$i]['title'] = $title;
  160. $return[$j+$i]['time'] = api_convert_and_format_date($hpresults[$i][4], null, date_default_timezone_get());
  161. $return[$j+$i]['result'] = $hpresults[$i][2];
  162. $return[$j+$i]['max'] = $hpresults[$i][3];
  163. }
  164. }
  165. $this->results = $return;
  166. return true;
  167. }
  168. /**
  169. * Exports the complete report as a CSV file
  170. * @param string Document path inside the document tool
  171. * @param integer Optional user ID
  172. * @param boolean Whether to include user fields or not
  173. * @return boolean False on error
  174. */
  175. public function exportCompleteReportCSV($dato) {
  176. //$this->_getGradeBookReporting($document_path,$user_id);
  177. $filename = 'gradebook_results_'.gmdate('YmdGis').'.csv';
  178. if (!empty($user_id)) {
  179. $filename = 'gradebook_results_user_'.$user_id.'_'.gmdate('YmdGis').'.csv';
  180. }
  181. $data = '';
  182. //build the results
  183. //titles
  184. foreach ($dato[0] as $header_col) {
  185. if(!empty($header_col)) {
  186. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($header_col))).';';
  187. }
  188. }
  189. $data .="\r\n";
  190. $cant_students = count($dato[1]);
  191. //print_r($data); exit();
  192. for($i=0;$i<$cant_students;$i++) {
  193. $column = 0;
  194. foreach($dato[1][$i] as $col_name) {
  195. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($col_name))).';';
  196. }
  197. $data .="\r\n";
  198. }
  199. //output the results
  200. $len = strlen($data);
  201. header('Content-type: application/octet-stream');
  202. header('Content-Type: application/force-download');
  203. header('Content-length: '.$len);
  204. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  205. header('Content-Disposition: filename= '.$filename);
  206. } else {
  207. header('Content-Disposition: attachment; filename= '.$filename);
  208. } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  209. header('Pragma: ');
  210. header('Cache-Control: ');
  211. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  212. }
  213. header('Content-Description: '.$filename);
  214. header('Content-transfer-encoding: binary');
  215. echo $data;
  216. return true;
  217. }
  218. /**
  219. * Exports the complete report as an XLS file
  220. * @return boolean False on error
  221. */
  222. public function exportCompleteReportXLS($data) {
  223. $filename = 'gradebook-results-'.date('Y-m-d-h:i:s').'.xls';
  224. include api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php';
  225. $workbook = new Spreadsheet_Excel_Writer();
  226. $workbook->setVersion(8); // BIFF8
  227. $workbook->setTempDir(api_get_path(SYS_ARCHIVE_PATH));
  228. $workbook->send($filename);
  229. $worksheet =& $workbook->addWorksheet('Report');
  230. $worksheet->setInputEncoding(api_get_system_encoding());
  231. $line = 0;
  232. $column = 0; //skip the first column (row titles)
  233. //headers
  234. foreach ($data[0] as $header_col) {
  235. $worksheet->write($line, $column, $header_col);
  236. $column++;
  237. }
  238. $line++;
  239. $cant_students = count($data[1]);
  240. for ($i=0;$i<$cant_students;$i++) {
  241. $column = 0;
  242. foreach ($data[1][$i] as $col_name) {
  243. $worksheet->write($line,$column, html_entity_decode(strip_tags($col_name)));
  244. $column++;
  245. }
  246. $line++;
  247. }
  248. $workbook->close();
  249. exit;
  250. }
  251. /**
  252. * Exports the complete report as a DOCX file
  253. * @return boolean False on error
  254. */
  255. public function exportCompleteReportDOC($data) {
  256. $_course = api_get_course_info();
  257. $filename = 'gb_results_'.$_course['code'].'_'.gmdate('YmdGis');
  258. $filepath = api_get_path(SYS_ARCHIVE_PATH).$filename;
  259. //build the results
  260. $inc = api_get_path(LIBRARY_PATH).'phpdocx/classes/CreateDocx.inc';
  261. require_once api_get_path(LIBRARY_PATH).'phpdocx/classes/CreateDocx.inc';
  262. $docx = new CreateDocx();
  263. $paramsHeader = array(
  264. 'font' => 'Courrier',
  265. 'jc' => 'left',
  266. 'textWrap' => 5,
  267. );
  268. $docx->addHeader(get_lang('FlatView'), $paramsHeader);
  269. $params = array(
  270. 'font' => 'Courrier',
  271. 'border' => 'single',
  272. 'border_sz' => 20
  273. );
  274. $lines = 0;
  275. $values[] = implode("\t",$data[0]);
  276. foreach ($data[1] as $line) {
  277. $values[] = implode("\t",$line);
  278. $lines++;
  279. }
  280. //$data = array();
  281. //$docx->addTable($data, $params);
  282. $docx->addList($values, $params);
  283. //$docx->addFooter('', $paramsHeader);
  284. $paramsPage = array(
  285. // 'titlePage' => 1,
  286. 'orient' => 'landscape',
  287. // 'top' => 4000,
  288. // 'bottom' => 4000,
  289. // 'right' => 4000,
  290. // 'left' => 4000
  291. );
  292. $docx->createDocx($filepath,$paramsPage);
  293. //output the results
  294. $data = file_get_contents($filepath.'.docx');
  295. $len = strlen($data);
  296. //header("Content-type: application/vnd.ms-word");
  297. header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
  298. //header('Content-Type: application/force-download');
  299. header('Content-length: '.$len);
  300. header("Content-Disposition: attachment; filename=\"$filename.docx\"");
  301. header('Expires: 0');
  302. header('Cache-Control: must-revalidate, post-check=0,pre-check=0');
  303. header('Pragma: public');
  304. echo $data;
  305. return true;
  306. }
  307. }