gradebook_result.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Gradebook results class
  5. * @author Yannick Warnier
  6. * @package chamilo.gradebook
  7. */
  8. class GradeBookResult
  9. {
  10. private $gradebook_list = array(); //stores the list of exercises
  11. private $results = array(); //stores the results
  12. /**
  13. * constructor of the class
  14. */
  15. public function __construct($get_questions=false,$get_answers=false)
  16. {
  17. //nothing to do
  18. /*
  19. $this->exercise_list = array();
  20. $this->readExercisesList();
  21. if($get_questions)
  22. {
  23. foreach($this->exercises_list as $exe)
  24. {
  25. $this->exercises_list['questions'] = $this->getExerciseQuestionList($exe['id']);
  26. }
  27. }
  28. */
  29. }
  30. /**
  31. * Reads exercises information (minimal) from the data base
  32. * @param boolean Whether to get only visible exercises (true) or all of them (false). Defaults to false.
  33. * @return array A list of exercises available
  34. */
  35. private function _readGradebookList($only_visible = false)
  36. {
  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. {
  58. $return = array();
  59. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  60. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  61. $course_id = api_get_course_int_id();
  62. $sql="SELECT q.id, q.question, q.ponderation, q.position, q.type, q.picture " .
  63. " FROM $TBL_EXERCISE_QUESTION eq, $TBL_QUESTIONS q " .
  64. " WHERE eq.c_di = $course_id AND
  65. q.c_di = $course_id AND
  66. eq.question_id=q.id AND
  67. eq.exercice_id='$e_id' " .
  68. " ORDER BY q.position";
  69. $result = Database::query($sql);
  70. // fills the array with the question ID for this exercise
  71. // the key of the array is the question position
  72. while($row=Database::fetch_array($result,'ASSOC')) {
  73. $return[] = $row;
  74. }
  75. return true;
  76. }
  77. /**
  78. * Gets the results of all students (or just one student if access is limited)
  79. * @param string The document path (for HotPotatoes retrieval)
  80. * @param integer User ID. Optional. If no user ID is provided,
  81. * we take all the results. Defauts to null
  82. */
  83. public function _getGradeBookReporting($document_path, $user_id = null)
  84. {
  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_EXERCISES);
  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 = ce.c_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 = ce.c_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 = StatsUtils::getManyResultsXCol($sql, 8);
  121. $hpresults = StatsUtils::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. {
  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. {
  225. $filename = 'gradebook-results-'.api_get_local_time().'.xls';
  226. $spreadsheet = new PHPExcel();
  227. $spreadsheet->setActiveSheetIndex(0);
  228. $worksheet = $spreadsheet->getActiveSheet();
  229. $line = 0;
  230. $column = 0; //skip the first column (row titles)
  231. //headers
  232. foreach ($data[0] as $header_col) {
  233. $worksheet->SetCellValueByColumnAndRow($line, $column, $header_col);
  234. $column++;
  235. }
  236. $line++;
  237. $cant_students = count($data[1]);
  238. for ($i = 0; $i < $cant_students; $i++) {
  239. $column = 0;
  240. foreach ($data[1][$i] as $col_name) {
  241. $worksheet->SetCellValueByColumnAndRow($line,$column, html_entity_decode(strip_tags($col_name)));
  242. $column++;
  243. }
  244. $line++;
  245. }
  246. $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename);
  247. $writer = new PHPExcel_Writer_Excel2007($spreadsheet);
  248. $writer->save($file);
  249. DocumentManager::file_send_for_download($file, true, $filename);
  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. {
  258. $_course = api_get_course_info();
  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. }