@@ -20,21 +20,21 @@ public function provide_import()
20
20
{
21
21
return true ;
22
22
23
- }
23
+ }//end provide_import()
24
24
25
25
26
26
public function provide_export ()
27
27
{
28
28
return true ;
29
29
30
- }
30
+ }//end provide_export()
31
31
32
32
33
33
public function mime_type ()
34
34
{
35
35
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ' ;
36
36
37
- }
37
+ }//end mime_type()
38
38
39
39
40
40
public function validate_file (stored_file $ file ): string
@@ -45,143 +45,175 @@ public function validate_file(stored_file $file): string
45
45
46
46
return '' ;
47
47
48
- }
48
+ }//end validate_file()
49
+
49
50
50
51
public function readquestions ($ data )
51
52
{
52
- $ allQuestions = [];
53
+ $ qa = [];
53
54
foreach ($ data ['sheets ' ] as $ sheetIndex => $ sheetData ) {
54
- $ sheet = $ sheetData ['rows ' ];
55
- $ images = $ sheetData ['images ' ] ?? [];
56
- $ name = $ this ->bottom_value ('_name ' , $ sheet );
57
- $ time = $ this ->bottom_value ('_time ' , $ sheet );
58
- $ attempts = $ this ->bottom_value ('_attempts ' , $ sheet );
59
- $ password = $ this ->bottom_value ('_password ' , $ sheet );
60
- $ fine = $ this ->bottom_value ('_fine ' , $ sheet );
61
- $ keys = $ this ->process_data ($ sheet );
62
- $ countQuestions = max (array_map (fn ($ item ) => count ($ item ), $ sheet ));
63
-
64
- for ($ i = 0 ; $ i < $ countQuestions ; $ i ++) {
65
- $ questiontext = $ this ->bottom_value ('_text ' , $ sheet );
55
+ $ sheet = $ sheetData ['rows ' ];
56
+ $ images = ($ sheetData ['images ' ] ?? []);
57
+ $ name = $ this ->bottom_value ('_name ' , $ sheet , 'Name not found ' );
58
+ $ fine = $ this ->abs_val ($ this ->bottom_value ('_fine ' , $ sheet , '0.1 ' ));
59
+ $ penalty = $ this ->abs_val ($ this ->bottom_value ('_penalty ' , $ sheet , '0.333 ' ));
60
+ $ fraction = $ this ->abs_val ($ this ->bottom_value ('_fraction ' , $ sheet , '1.0 ' ));
61
+ $ qtype = $ this ->bottom_value ('_qtype ' , $ sheet , 'numerical ' );
62
+ $ keys = $ this ->process_data ($ sheet );
63
+ $ countQuestions = max (array_map (fn ($ item ) => count ($ item ), $ keys ));
64
+
65
+ debugging ("Try import {$ countQuestions } questions " , DEBUG_DEVELOPER );
66
+
67
+ for ($ i = 0 ; $ i < $ countQuestions ; $ i ++) {
68
+ $ qtext = $ this ->bottom_value ('_text ' , $ sheet , '' );
66
69
67
70
foreach ($ keys as $ key => $ value ) {
68
- if ($ key === '_answer ' ) {
69
- debugging (" Skip _answer to be processed " , DEBUG_DEVELOPER );
71
+ if ($ key === '_answer ' ) {
72
+ debugging (' Skip _answer to be processed ' , DEBUG_DEVELOPER );
70
73
continue ;
71
74
}
72
- $ questiontext .= "<p><b> {$ key }</b>: {$ value [$ i ]}</p> " ;
75
+
76
+ $ qtext .= "<p><b> {$ key }</b>: {$ value [$ i ]}</p> " ;
73
77
}
74
78
75
79
foreach ($ images as $ image ) {
76
- $ questiontext .= "<img title= \"{$ image ['name ' ]}\" src= \"data: {$ image ['mimetype ' ]};base64, {$ image ['base64 ' ]}\"/> " ;
80
+ $ qtext .= "<img title= \"{$ image ['name ' ]}\" src= \"data: {$ image ['mimetype ' ]};base64, {$ image ['base64 ' ]}\"/> " ;
77
81
}
78
82
79
83
$ q = $ this ->defaultquestion ();
80
84
$ q ->id = $ i ;
81
85
$ q ->name = $ name ;
82
- $ q ->questiontext = $ questiontext ;
83
- $ q ->qtype = ' shortanswer ' ;
86
+ $ q ->questiontext = $ qtext ;
87
+ $ q ->qtype = $ qtype ;
84
88
$ q ->feedback = [
85
89
0 => [
86
90
'text ' => ' ' ,
87
91
'format ' => FORMAT_HTML ,
88
92
],
89
93
];
90
94
91
- $ q ->fraction = [1 ];
92
- $ q ->answer = [$ keys ['_answer ' ][$ i ]];
93
- $ allQuestions [] = $ q ;
94
- }
95
+ $ q ->fraction = [$ fraction ];
96
+ $ q ->answer = [$ keys ['_answer ' ][$ i ]];
97
+ $ q ->tolerance = [$ fine ];
98
+ $ q ->penalty = $ penalty ;
99
+ $ qa [] = $ q ;
100
+ }//end for
101
+ }//end foreach
102
+
103
+ return $ qa ;
104
+
105
+ }//end readquestions()
106
+
107
+
108
+ private function abs_val ($ str )
109
+ {
110
+ if (str_contains ($ str , '% ' )) {
111
+ return (floatval ($ str ) / 100 );
95
112
}
96
113
97
- return $ allQuestions ;
98
- }
114
+ return floatval ($ str );
115
+
116
+ }//end abs_val()
99
117
100
118
101
119
private function process_data ($ data )
102
120
{
103
- $ count = $ this ->get_counts ($ data );
121
+ $ count = $ this ->get_counts ($ data );
104
122
$ countColumns = $ count [0 ];
105
- $ countRows = $ count [1 ];
123
+ $ countRows = $ count [1 ];
106
124
107
125
$ keys = [];
108
- for ($ j = 0 ; $ j < $ countColumns ; $ j ++) {
126
+ for ($ j = 0 ; $ j < $ countColumns ; $ j ++) {
109
127
$ keys [$ data [0 ][$ j ]] = [];
110
128
}
111
- for ($ i = 1 ; $ i < $ countRows ; $ i ++) {
112
- for ($ j = 0 ; $ j < $ countColumns ; $ j ++) {
129
+
130
+ for ($ i = 1 ; $ i < $ countRows ; $ i ++) {
131
+ for ($ j = 0 ; $ j < $ countColumns ; $ j ++) {
113
132
$ keys [$ data [0 ][$ j ]][] = $ data [$ i ][$ j ];
114
133
}
115
134
}
135
+
116
136
return $ keys ;
117
- }
137
+
138
+ }//end process_data()
139
+
118
140
119
141
private function get_counts ($ data )
120
142
{
121
- $ countRows = 0 ;
143
+ $ countRows = 0 ;
122
144
$ countColumns = 0 ;
123
145
124
- foreach ($ data [0 ] as $ i => $ column ) {
125
- if (empty ($ column )) {
146
+ foreach ($ data [0 ] as $ i => $ column ) {
147
+ if (empty ($ column )) {
126
148
break ;
127
149
}
128
- $ countColumns = $ i + 1 ;
150
+
151
+ $ countColumns = ($ i + 1 );
129
152
}
130
153
131
- foreach ($ data as $ i => $ row ) {
154
+ foreach ($ data as $ i => $ row ) {
132
155
$ countEmpty = 0 ;
133
- for ($ j = 0 ; $ j < $ countColumns ; $ j ++) {
156
+ for ($ j = 0 ; $ j < $ countColumns ; $ j ++) {
134
157
$ cell = $ row [$ j ];
135
- if (empty ($ cell )) {
158
+ if (empty ($ cell )) {
136
159
$ countEmpty ++;
137
160
}
138
161
}
139
- if ($ countEmpty >= $ countColumns ) {
162
+
163
+ if ($ countEmpty >= $ countColumns ) {
140
164
debugging ("Found empty row $ countEmpty >= $ countColumns, at row $ i " , DEBUG_DEVELOPER );
141
165
break ;
142
166
}
143
- for ($ j = 0 ; $ j < $ countColumns ; $ j ++) {
167
+
168
+ for ($ j = 0 ; $ j < $ countColumns ; $ j ++) {
144
169
$ cell = $ row [$ j ];
145
- if (!empty ($ cell )) {
146
- $ countRows = $ i + 1 ;
170
+ if (!empty ($ cell )) {
171
+ $ countRows = ( $ i + 1 ) ;
147
172
}
148
173
}
149
- }
174
+ }//end foreach
150
175
151
- debugging ("Columns: " . $ countColumns , DEBUG_DEVELOPER );
152
- debugging ("Rows: " . $ countRows , DEBUG_DEVELOPER );
153
- return [$ countColumns , $ countRows ];
154
- }
176
+ debugging ('Columns: ' .$ countColumns , DEBUG_DEVELOPER );
177
+ debugging ('Rows: ' .$ countRows , DEBUG_DEVELOPER );
178
+ return [
179
+ $ countColumns ,
180
+ $ countRows ,
181
+ ];
155
182
183
+ }//end get_counts()
156
184
157
- private function bottom_value ($ identifier , $ data )
185
+
186
+ private function bottom_value ($ identifier , $ data , $ default = '' )
158
187
{
159
188
foreach ($ data as $ i => $ row ) {
160
189
foreach ($ row as $ j => $ value ) {
161
190
if ($ value === $ identifier ) {
162
- $ r = $ data [$ i + 1 ][$ j ];
191
+ $ r = $ data [( $ i + 1 ) ][$ j ];
163
192
debugging ("Found $ identifier, bottom value: {$ r }" , DEBUG_DEVELOPER );
164
193
return $ r ;
165
194
}
166
195
}
167
196
}
168
- debugging ("Not found bottom value " , DEBUG_DEVELOPER );
169
- return "" ;
170
- }
197
+
198
+ debugging ('Not found bottom value ' , DEBUG_DEVELOPER );
199
+ return $ default ;
200
+
201
+ }//end bottom_value()
202
+
171
203
172
204
public function export_file_extension ()
173
205
{
174
206
return '.xlsx ' ;
175
207
176
- }
208
+ }//end export_file_extension()
177
209
178
210
179
211
public function writequestion ($ question )
180
212
{
181
213
$ this ->lessonquestions [] = $ question ;
182
214
return true ;
183
215
184
- }
216
+ }//end writequestion()
185
217
186
218
187
219
public function presave_process ($ content )
@@ -190,6 +222,8 @@ public function presave_process($content)
190
222
throw new moodle_exception ('noquestions ' , 'qformat_xlsxtable ' );
191
223
}
192
224
225
+ debugging ('Questions: ' .print_r ($ this ->lessonquestions , true ), DEBUG_DEVELOPER );
226
+
193
227
$ workbook = new MoodleExcelWorkbook ($ this ->filename );
194
228
$ worksheet = $ workbook ->add_worksheet ('Questions ' );
195
229
foreach ($ this ->lessonquestions as $ rowIndex => $ question ) {
@@ -203,18 +237,20 @@ public function presave_process($content)
203
237
204
238
$ workbook ->close ();
205
239
return true ;
206
- }
240
+
241
+ }//end presave_process()
242
+
207
243
208
244
public function readdata ($ filename )
209
245
{
210
246
if ($ filename === null || !preg_match ('#\.xlsx$#i ' , $ filename )) {
211
247
return false ;
212
248
}
213
249
214
- $ reader = IOFactory::createReader ('Xlsx ' );
250
+ $ reader = IOFactory::createReader ('Xlsx ' );
215
251
$ spreadsheet = $ reader ->load ($ filename );
216
252
217
- $ data = [];
253
+ $ data = [];
218
254
$ sheetCount = $ spreadsheet ->getSheetCount ();
219
255
for ($ sheetIndex = 0 ; $ sheetIndex < $ sheetCount ; $ sheetIndex ++) {
220
256
$ worksheet = $ spreadsheet ->getSheet ($ sheetIndex );
@@ -224,7 +260,7 @@ public function readdata($filename)
224
260
$ cellIterator ->setIterateOnlyExistingCells (false );
225
261
$ rowData = [];
226
262
foreach ($ cellIterator as $ cell ) {
227
- $ value = $ cell ->getCalculatedValue ();
263
+ $ value = $ cell ->getCalculatedValue ();
228
264
$ rowData [] = $ value ;
229
265
}
230
266
@@ -239,18 +275,22 @@ public function readdata($filename)
239
275
$ imageData ['coordinates ' ] = $ drawing ->getCoordinates ();
240
276
$ imageData ['name ' ] = $ drawing ->getName ();
241
277
$ imageData ['base64 ' ] = base64_encode (file_get_contents ($ imagePath ));
242
- $ mimeType = 'image/png ' ;
278
+ $ mimeType = 'image/png ' ;
243
279
if (function_exists ('mime_content_type ' )) {
244
280
$ mimeType = mime_content_type ($ imagePath );
245
281
}
282
+
246
283
$ imageData ['mimetype ' ] = $ mimeType ;
247
284
248
285
$ sheetData ['images ' ][] = $ imageData ;
249
286
}
250
287
251
288
$ data ['sheets ' ][] = $ sheetData ;
252
- }
289
+ }//end for
253
290
254
291
return $ data ;
255
- }
256
- }
292
+
293
+ }//end readdata()
294
+
295
+
296
+ }//end class
0 commit comments