gradebook_result.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Gradebook results class.
  5. *
  6. * @author Yannick Warnier
  7. *
  8. * @package chamilo.gradebook
  9. */
  10. class GradeBookResult
  11. {
  12. private $gradebook_list = []; //stores the list of exercises
  13. private $results = []; //stores the results
  14. /**
  15. * constructor of the class.
  16. */
  17. public function __construct($get_questions = false, $get_answers = false)
  18. {
  19. }
  20. /**
  21. * Exports the complete report as a CSV file.
  22. *
  23. * @param string $dato Document path inside the document tool
  24. *
  25. * @return bool False on error
  26. */
  27. public function exportCompleteReportCSV($dato)
  28. {
  29. $filename = 'gradebook_results_'.gmdate('YmdGis').'.csv';
  30. $data = '';
  31. //build the results
  32. //titles
  33. foreach ($dato[0] as $header_col) {
  34. if (!empty($header_col)) {
  35. if (is_array($header_col)) {
  36. if (isset($header_col['header'])) {
  37. $data .= str_replace("\r\n", ' ', api_html_entity_decode(strip_tags($header_col['header']))).';';
  38. }
  39. } else {
  40. $data .= str_replace("\r\n", ' ', api_html_entity_decode(strip_tags($header_col))).';';
  41. }
  42. }
  43. }
  44. $data .= "\r\n";
  45. $cant_students = count($dato[1]);
  46. for ($i = 0; $i < $cant_students; $i++) {
  47. $column = 0;
  48. foreach ($dato[1][$i] as $col_name) {
  49. $data .= str_replace("\r\n", ' ', api_html_entity_decode(strip_tags($col_name))).';';
  50. }
  51. $data .= "\r\n";
  52. }
  53. // output the results
  54. $len = strlen($data);
  55. header('Content-type: application/octet-stream');
  56. header('Content-Type: application/force-download');
  57. header('Content-length: '.$len);
  58. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  59. header('Content-Disposition: filename= '.$filename);
  60. } else {
  61. header('Content-Disposition: attachment; filename= '.$filename);
  62. }
  63. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  64. header('Pragma: ');
  65. header('Cache-Control: ');
  66. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  67. }
  68. header('Content-Description: '.$filename);
  69. header('Content-transfer-encoding: binary');
  70. echo $data;
  71. return true;
  72. }
  73. /**
  74. * Exports the complete report as an XLS file.
  75. *
  76. * @param array $data
  77. *
  78. * @throws PHPExcel_Exception
  79. * @throws PHPExcel_Writer_Exception
  80. */
  81. public function exportCompleteReportXLS($data)
  82. {
  83. $filename = 'gradebook-results-'.api_get_local_time().'.xlsx';
  84. $spreadsheet = new PHPExcel();
  85. $spreadsheet->setActiveSheetIndex(0);
  86. $worksheet = $spreadsheet->getActiveSheet();
  87. $line = 1;
  88. $column = 0;
  89. //headers
  90. foreach ($data[0] as $header_col) {
  91. $worksheet->SetCellValueByColumnAndRow(
  92. $column,
  93. $line,
  94. html_entity_decode(strip_tags($header_col))
  95. );
  96. $column++;
  97. }
  98. $line++;
  99. $cant_students = count($data[1]);
  100. for ($i = 0; $i < $cant_students; $i++) {
  101. $column = 0;
  102. foreach ($data[1][$i] as $col_name) {
  103. $worksheet->SetCellValueByColumnAndRow(
  104. $column,
  105. $line,
  106. html_entity_decode(strip_tags($col_name))
  107. );
  108. $column++;
  109. }
  110. $line++;
  111. }
  112. $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename);
  113. $writer = new PHPExcel_Writer_Excel2007($spreadsheet);
  114. $writer->save($file);
  115. DocumentManager::file_send_for_download($file, true, $filename);
  116. exit;
  117. }
  118. /**
  119. * Exports the complete report as a DOCX file.
  120. *
  121. * @param array $data The table data
  122. *
  123. * @return bool
  124. */
  125. public function exportCompleteReportDOC($data)
  126. {
  127. $filename = 'gradebook_results_'.api_get_local_time().'.docx';
  128. $doc = new \PhpOffice\PhpWord\PhpWord();
  129. $section = $doc->addSection(['orientation' => 'landscape']);
  130. $table = $section->addTable();
  131. $table->addRow();
  132. for ($i = 0; $i < count($data[0]); $i++) {
  133. $table->addCell(1750)->addText(strip_tags($data[0][$i]));
  134. }
  135. foreach ($data[1] as $dataLine) {
  136. $table->addRow();
  137. for ($i = 0; $i < count($dataLine); $i++) {
  138. $table->addCell(1750)->addText(strip_tags($dataLine[$i]));
  139. }
  140. }
  141. $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename);
  142. $doc->save($file, 'Word2007');
  143. DocumentManager::file_send_for_download($file, true, $filename);
  144. return true;
  145. }
  146. }