gradebook_result.class.php 4.7 KB

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