gradebook_result.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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();
  84. $list = [];
  85. //headers
  86. foreach ($data[0] as $header_col) {
  87. $list[0][] = html_entity_decode(strip_tags($header_col));
  88. }
  89. $cant_students = count($data[1]);
  90. $line = 0;
  91. for ($i = 0; $i < $cant_students; $i++) {
  92. $column = 0;
  93. foreach ($data[1][$i] as $col_name) {
  94. $list[$column][$line] = html_entity_decode(strip_tags($col_name));
  95. /*$worksheet->SetCellValueByColumnAndRow(
  96. $column,
  97. $line,
  98. html_entity_decode(strip_tags($col_name))
  99. );*/
  100. $column++;
  101. }
  102. $line++;
  103. }
  104. Export::arrayToXls($list, $filename);
  105. return true;
  106. }
  107. /**
  108. * Exports the complete report as a DOCX file.
  109. *
  110. * @param array $data The table data
  111. *
  112. * @return bool
  113. */
  114. public function exportCompleteReportDOC($data)
  115. {
  116. $filename = 'gradebook_results_'.api_get_local_time().'.docx';
  117. $doc = new \PhpOffice\PhpWord\PhpWord();
  118. $section = $doc->addSection(['orientation' => 'landscape']);
  119. $table = $section->addTable();
  120. $table->addRow();
  121. for ($i = 0; $i < count($data[0]); $i++) {
  122. $table->addCell(1750)->addText(strip_tags($data[0][$i]));
  123. }
  124. foreach ($data[1] as $dataLine) {
  125. $table->addRow();
  126. for ($i = 0; $i < count($dataLine); $i++) {
  127. $table->addCell(1750)->addText(strip_tags($dataLine[$i]));
  128. }
  129. }
  130. $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename);
  131. $doc->save($file, 'Word2007');
  132. DocumentManager::file_send_for_download($file, true, $filename);
  133. return true;
  134. }
  135. }