-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.php
298 lines (223 loc) · 8.66 KB
/
format.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
defined('MOODLE_INTERNAL') || die();
require_once "$CFG->libdir/xmlize.php";
require_once "$CFG->dirroot/lib/uploadlib.php";
require_once "$CFG->dirroot/question/format/xml/format.php";
require_once "$CFG->dirroot/lib/excellib.class.php";
use moodle_exception;
require_once "$CFG->libdir/phpspreadsheet/vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\IOFactory;
class qformat_xlsxtable extends qformat_default
{
private $lessonquestions = [];
public function provide_import()
{
return true;
}//end provide_import()
public function provide_export()
{
return true;
}//end provide_export()
public function mime_type()
{
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
}//end mime_type()
public function validate_file(stored_file $file): string
{
if (!preg_match('#\.xlsx$#i', $file->get_filename())) {
return get_string('errorfilenamemustbexlsx', 'qformat_xlsxtable');
}
return '';
}//end validate_file()
public function readquestions($data)
{
$qa = [];
foreach ($data['sheets'] as $sheetIndex => $sheetData) {
$sheet = $sheetData['rows'];
$images = ($sheetData['images'] ?? []);
$name = $this->bottom_value('_name', $sheet, 'Name not found');
$fine = $this->abs_val($this->bottom_value('_fine', $sheet, '0.1'));
$penalty = $this->abs_val($this->bottom_value('_penalty', $sheet, '0.333'));
$fraction = $this->abs_val($this->bottom_value('_fraction', $sheet, '1.0'));
$qtype = $this->bottom_value('_qtype', $sheet, 'numerical');
$keys = $this->process_data($sheet);
$countQuestions = max(array_map(fn ($item) => count($item), $keys));
debugging("Try import {$countQuestions} questions", DEBUG_DEVELOPER);
for ($i = 0; $i < $countQuestions; $i++) {
$qtext = $this->bottom_value('_text', $sheet, '');
foreach ($keys as $key => $value) {
if ($key === '_answer') {
debugging('Skip _answer to be processed', DEBUG_DEVELOPER);
continue;
}
$qtext .= "<p><b>{$key}</b>: {$value[$i]}</p>";
}
foreach ($images as $image) {
$qtext .= "<img title=\"{$image['name']}\" src=\"data:{$image['mimetype']};base64,{$image['base64']}\"/>";
}
$q = $this->defaultquestion();
$q->id = $i;
$q->name = $name;
$q->tags = [
'xlsxtable',
$sheetData['name'],
];
$q->questiontext = $qtext;
$q->qtype = $qtype;
$q->feedback = [
0 => [
'text' => ' ',
'format' => FORMAT_HTML,
],
];
$q->fraction = [$fraction];
$q->answer = [$keys['_answer'][$i]];
$q->tolerance = [$fine];
$q->penalty = $penalty;
$qa[] = $q;
}//end for
}//end foreach
return $qa;
}//end readquestions()
private function abs_val($str)
{
if (str_contains($str, '%')) {
return (floatval($str) / 100);
}
return floatval($str);
}//end abs_val()
private function process_data($data)
{
$count = $this->get_counts($data);
$countColumns = $count[0];
$countRows = $count[1];
$keys = [];
for ($j = 0; $j < $countColumns; $j++) {
$keys[$data[0][$j]] = [];
}
for ($i = 1; $i < $countRows; $i++) {
for ($j = 0; $j < $countColumns; $j++) {
$keys[$data[0][$j]][] = $data[$i][$j];
}
}
return $keys;
}//end process_data()
private function get_counts($data)
{
$countRows = 0;
$countColumns = 0;
foreach ($data[0] as $i => $column) {
if (empty($column)) {
break;
}
$countColumns = ($i + 1);
}
foreach ($data as $i => $row) {
$countEmpty = 0;
for ($j = 0; $j < $countColumns; $j++) {
$cell = $row[$j];
if (empty($cell)) {
$countEmpty++;
}
}
if ($countEmpty >= $countColumns) {
debugging("Found empty row $countEmpty >= $countColumns, at row $i", DEBUG_DEVELOPER);
break;
}
for ($j = 0; $j < $countColumns; $j++) {
$cell = $row[$j];
if (!empty($cell)) {
$countRows = ($i + 1);
}
}
}//end foreach
debugging('Columns: '.$countColumns, DEBUG_DEVELOPER);
debugging('Rows: '.$countRows, DEBUG_DEVELOPER);
return [
$countColumns,
$countRows,
];
}//end get_counts()
private function bottom_value($identifier, $data, $default = '')
{
foreach ($data as $i => $row) {
foreach ($row as $j => $value) {
if ($value === $identifier) {
$r = $data[($i + 1)][$j];
debugging("Found $identifier, bottom value: {$r}", DEBUG_DEVELOPER);
return $r;
}
}
}
debugging('Not found bottom value', DEBUG_DEVELOPER);
return $default;
}//end bottom_value()
public function export_file_extension()
{
return '.xlsx';
}//end export_file_extension()
public function writequestion($question)
{
$this->lessonquestions[] = $question;
return true;
}//end writequestion()
public function presave_process($content)
{
if (count($this->lessonquestions) == 0) {
throw new moodle_exception('noquestions', 'qformat_xlsxtable');
}
$workbook = new MoodleExcelWorkbook($this->filename);
$worksheet = $workbook->add_worksheet('Questions');
foreach ($this->lessonquestions as $rowIndex => $question) {
$worksheet->write($rowIndex, 0, $question->name);
$worksheet->write($rowIndex, 1, $question->questiontext);
$answers = $question->options->answers;
foreach ($answers as $a) {
$worksheet->write($rowIndex, 2, $a->answer);
}
}
$workbook->close();
return true;
}//end presave_process()
public function readdata($filename)
{
if ($filename === null || !preg_match('#\.xlsx$#i', $filename)) {
return false;
}
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($filename);
$data = [];
$sheetCount = $spreadsheet->getSheetCount();
for ($sheetIndex = 0; $sheetIndex < $sheetCount; $sheetIndex++) {
$worksheet = $spreadsheet->getSheet($sheetIndex);
$sheetData = ['name' => $worksheet->getTitle()];
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$rowData = [];
foreach ($cellIterator as $cell) {
$value = $cell->getCalculatedValue();
$rowData[] = $value;
}
$sheetData['rows'][] = $rowData;
}
$drawings = $worksheet->getDrawingCollection();
foreach ($drawings as $drawing) {
$imageData = [];
$imagePath = $drawing->getPath();
$imageData['path'] = $imagePath;
$imageData['coordinates'] = $drawing->getCoordinates();
$imageData['name'] = $drawing->getName();
$imageData['base64'] = base64_encode(file_get_contents($imagePath));
$mimeType = 'image/png';
if (function_exists('mime_content_type')) {
$mimeType = mime_content_type($imagePath);
}
$imageData['mimetype'] = $mimeType;
$sheetData['images'][] = $imageData;
}
$data['sheets'][] = $sheetData;
}//end for
return $data;
}//end readdata()
}//end class