gradebook_result.class.php 4.6 KB

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