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