gradebook_result.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }
  18. /**
  19. * Exports the complete report as a CSV file
  20. * @param string Document path inside the document tool
  21. * @param integer Optional user ID
  22. * @param boolean Whether to include user fields or not
  23. * @return boolean False on error
  24. */
  25. public function exportCompleteReportCSV($dato)
  26. {
  27. $filename = 'gradebook_results_'.gmdate('YmdGis').'.csv';
  28. if (!empty($user_id)) {
  29. $filename = 'gradebook_results_user_'.$user_id.'_'.gmdate('YmdGis').'.csv';
  30. }
  31. $data = '';
  32. //build the results
  33. //titles
  34. foreach ($dato[0] as $header_col) {
  35. if(!empty($header_col)) {
  36. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($header_col))).';';
  37. }
  38. }
  39. $data .="\r\n";
  40. $cant_students = count($dato[1]);
  41. //print_r($data); exit();
  42. for($i=0;$i<$cant_students;$i++) {
  43. $column = 0;
  44. foreach($dato[1][$i] as $col_name) {
  45. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($col_name))).';';
  46. }
  47. $data .="\r\n";
  48. }
  49. //output the results
  50. $len = strlen($data);
  51. header('Content-type: application/octet-stream');
  52. header('Content-Type: application/force-download');
  53. header('Content-length: '.$len);
  54. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  55. header('Content-Disposition: filename= '.$filename);
  56. } else {
  57. header('Content-Disposition: attachment; filename= '.$filename);
  58. } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  59. header('Pragma: ');
  60. header('Cache-Control: ');
  61. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  62. }
  63. header('Content-Description: '.$filename);
  64. header('Content-transfer-encoding: binary');
  65. echo $data;
  66. return true;
  67. }
  68. /**
  69. * Exports the complete report as an XLS file
  70. * @return boolean|null False on error
  71. */
  72. public function exportCompleteReportXLS($data)
  73. {
  74. $filename = 'gradebook-results-'.api_get_local_time().'.xlsx';
  75. $spreadsheet = new PHPExcel();
  76. $spreadsheet->setActiveSheetIndex(0);
  77. $worksheet = $spreadsheet->getActiveSheet();
  78. $line = 0;
  79. $column = 0; //skip the first column (row titles)
  80. //headers
  81. foreach ($data[0] as $header_col) {
  82. $worksheet->setCellValueByColumnAndRow($column, $line, html_entity_decode(strip_tags($header_col)));
  83. $column++;
  84. }
  85. $line++;
  86. $cant_students = count($data[1]);
  87. for ($i = 0; $i < $cant_students; $i++) {
  88. $column = 0;
  89. foreach ($data[1][$i] as $col_name) {
  90. $worksheet->setCellValueByColumnAndRow($column, $line, html_entity_decode(strip_tags($col_name)));
  91. $column++;
  92. }
  93. $line++;
  94. }
  95. $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename);
  96. $writer = new PHPExcel_Writer_Excel2007($spreadsheet);
  97. $writer->save($file);
  98. DocumentManager::file_send_for_download($file, true, $filename);
  99. exit;
  100. }
  101. /**
  102. * Exports the complete report as a DOCX file
  103. * @param $data The table data
  104. * @return bool
  105. */
  106. public function exportCompleteReportDOC($data)
  107. {
  108. $filename = 'gradebook_results_'.api_get_local_time() . '.docx';
  109. $doc = new \PhpOffice\PhpWord\PhpWord();
  110. $section = $doc->addSection(['orientation' => 'landscape']);
  111. $table = $section->addTable();
  112. $table->addRow();
  113. for ($i = 0; $i < count($data[0]); $i++) {
  114. $table->addCell(1750)->addText(strip_tags($data[0][$i]));
  115. }
  116. foreach ($data[1] as $dataLine) {
  117. $table->addRow();
  118. for ($i = 0; $i < count($dataLine); $i++) {
  119. $table->addCell(1750)->addText(strip_tags($dataLine[$i]));
  120. }
  121. }
  122. $file = api_get_path(SYS_ARCHIVE_PATH) . api_replace_dangerous_char($filename);
  123. $doc->save($file, 'Word2007');
  124. DocumentManager::file_send_for_download($file, true, $filename);
  125. return true;
  126. }
  127. }