gradebook_result.class.php 11 KB

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