-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.php
120 lines (96 loc) · 3.82 KB
/
export.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/lib.php');
require_once("$CFG->libdir/phpspreadsheet/vendor/autoload.php");
$templateid = required_param('templateid', PARAM_INT);
$courseid = required_param('courseid', PARAM_INT);
$returnurl = required_param('returnurl', PARAM_URL);
$course = get_course($courseid);
$context = context_course::instance($course->id);
require_login($course);
if (!has_capability('block/course_rating:download', $context)) {
redirect($returnurl, 'Вы не зачислены как студент на данный курс!', 5, \core\output\notification::NOTIFY_ERROR);
}
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$spreadsheet->getDefaultStyle()->getAlignment()->setWrapText(true);
$styleHeader = [
'font' => [
'bold' => true,
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT,
'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
'wrapText' => true,
],
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
],
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'color' => [
'argb' => 'FF808080',
],
],
];
$headers = [
'Дата',
'Время',
'ФИО',
];
$width = ['14', '7', '40'];
foreach (block_course_rating_get_questions($templateid) as $question) {
$headers[] = $question;
$width[] = '30';
}
$headers[] = 'Отзыв';
$width[] = '30';
for ($i = 0, $l = sizeof($headers); $i < $l; $i++) {
$sheet->setCellValueByColumnAndRow($i + 1, 1, $headers[$i]);
}
$sheet->getStyleByColumnAndRow(1, 1, sizeof($headers), 1)->applyFromArray($styleHeader);
for ($i = 0, $l = sizeof($width); $i < $l; $i++) {
$sheet->getColumnDimensionByColumn($i + 1)->setWidth($width[$i]);
}
$data = block_course_rating_get_answers($courseid, $templateid);
for ($i = 0, $l = sizeof($data); $i < $l; $i++) { // row $i
$j = 0;
foreach ($data[$i] as $k => $v) { // column $j
$sheet->setCellValueByColumnAndRow($j + 1, ($i + 1 + 1), $v);
$j++;
}
}
$styleData = [
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT,
'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
'wrapText' => true,
],
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
],
],
];
$sheet->getStyleByColumnAndRow(1, 1 + 1, sizeof($headers), sizeof($data) + 1)->applyFromArray($styleData);
$sheet->getStyleByColumnAndRow(1, 1); // поставим курсор на A1
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . urlencode(get_string('pluginname', 'block_course_rating') . ' ' . $course->fullname . '-' . time()) . '.xlsx"');
$writer->save('php://output');