gradebook_result.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. if (!empty($user_id)) {
  31. $filename = 'gradebook_results_user_'.$user_id.'_'.gmdate('YmdGis').'.csv';
  32. }
  33. $data = '';
  34. //build the results
  35. //titles
  36. foreach ($dato[0] as $header_col) {
  37. if (!empty($header_col)) {
  38. $data .= str_replace("\r\n", ' ', api_html_entity_decode(strip_tags($header_col))).';';
  39. }
  40. }
  41. $data .= "\r\n";
  42. $cant_students = count($dato[1]);
  43. for ($i = 0; $i < $cant_students; $i++) {
  44. $column = 0;
  45. foreach ($dato[1][$i] as $col_name) {
  46. $data .= str_replace("\r\n", ' ', api_html_entity_decode(strip_tags($col_name))).';';
  47. }
  48. $data .= "\r\n";
  49. }
  50. //output the results
  51. $len = strlen($data);
  52. header('Content-type: application/octet-stream');
  53. header('Content-Type: application/force-download');
  54. header('Content-length: '.$len);
  55. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  56. header('Content-Disposition: filename= '.$filename);
  57. } else {
  58. header('Content-Disposition: attachment; filename= '.$filename);
  59. }
  60. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  61. header('Pragma: ');
  62. header('Cache-Control: ');
  63. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  64. }
  65. header('Content-Description: '.$filename);
  66. header('Content-transfer-encoding: binary');
  67. echo $data;
  68. return true;
  69. }
  70. /**
  71. * Exports the complete report as an XLS file.
  72. *
  73. * @param array $data
  74. *
  75. * @throws PHPExcel_Exception
  76. * @throws PHPExcel_Writer_Exception
  77. */
  78. public function exportCompleteReportXLS($data)
  79. {
  80. $filename = 'gradebook-results-'.api_get_local_time().'.xlsx';
  81. $spreadsheet = new PHPExcel();
  82. $spreadsheet->setActiveSheetIndex(0);
  83. $worksheet = $spreadsheet->getActiveSheet();
  84. $line = 1;
  85. $column = 0;
  86. //headers
  87. foreach ($data[0] as $header_col) {
  88. $worksheet->SetCellValueByColumnAndRow(
  89. $column,
  90. $line,
  91. html_entity_decode(strip_tags($header_col))
  92. );
  93. $column++;
  94. }
  95. $line++;
  96. $cant_students = count($data[1]);
  97. for ($i = 0; $i < $cant_students; $i++) {
  98. $column = 0;
  99. foreach ($data[1][$i] as $col_name) {
  100. $worksheet->SetCellValueByColumnAndRow(
  101. $column,
  102. $line,
  103. html_entity_decode(strip_tags($col_name))
  104. );
  105. $column++;
  106. }
  107. $line++;
  108. }
  109. $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename);
  110. $writer = new PHPExcel_Writer_Excel2007($spreadsheet);
  111. $writer->save($file);
  112. DocumentManager::file_send_for_download($file, true, $filename);
  113. exit;
  114. }
  115. /**
  116. * Exports the complete report as a DOCX file.
  117. *
  118. * @param array $data The table data
  119. *
  120. * @return bool
  121. */
  122. public function exportCompleteReportDOC($data)
  123. {
  124. $filename = 'gradebook_results_'.api_get_local_time().'.docx';
  125. $doc = new \PhpOffice\PhpWord\PhpWord();
  126. $section = $doc->addSection(['orientation' => 'landscape']);
  127. $table = $section->addTable();
  128. $table->addRow();
  129. for ($i = 0; $i < count($data[0]); $i++) {
  130. $table->addCell(1750)->addText(strip_tags($data[0][$i]));
  131. }
  132. foreach ($data[1] as $dataLine) {
  133. $table->addRow();
  134. for ($i = 0; $i < count($dataLine); $i++) {
  135. $table->addCell(1750)->addText(strip_tags($dataLine[$i]));
  136. }
  137. }
  138. $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename);
  139. $doc->save($file, 'Word2007');
  140. DocumentManager::file_send_for_download($file, true, $filename);
  141. return true;
  142. }
  143. }