gradebook_result.class.php 11 KB

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