Skip to content

Commit b957382

Browse files
committed
Fix to issue #1762 varmatrix with EMPTYANSWER is broken.
1 parent 95937a0 commit b957382

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

doc/en/Authoring/Inputs/Matrix_input.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ The matrix of variable size input is a textarea into which students type in thei
2424

2525
Students must separate their matrix elements by spaces, and newline characters.
2626

27-
Input box size is used to determine the starting width of the input.
27+
Input box size is used to determine the starting width of the input.
28+
29+
If you use the `allowempty` option then an empty answer is indicated by the `EMPTYANSWER` tag. This is a different _type_ than a matrix. (We could have chosen `matrix()` as the empty matrix, but `EMPTYANSWER` is more in keeping with other inputs.)

stack/input/varmatrix/varmatrix.class.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ protected function response_to_contents($response) {
185185
$contents = [];
186186
if (array_key_exists($this->name, $response)) {
187187
$sans = $response[$this->name];
188+
if (trim($sans) === '' && $this->get_extra_option('allowempty')) {
189+
return(['EMPTYANSWER']);
190+
}
188191
$rowsin = explode("\n", $sans);
189192
foreach ($rowsin as $key => $row) {
190193
$cleanrow = trim($row);
@@ -200,7 +203,6 @@ protected function response_to_contents($response) {
200203
$maxlen = max(count($entries), $maxlen);
201204
$contents[$key] = $entries;
202205
}
203-
204206
foreach ($contents as $key => $row) {
205207
// Pad out short rows.
206208
$padrow = [];
@@ -209,9 +211,6 @@ protected function response_to_contents($response) {
209211
}
210212
$contents[$key] = array_merge($row, $padrow);
211213
}
212-
if ($contents == [] && $this->get_extra_option('allowempty')) {
213-
$contents = ['EMPTYANSWER'];
214-
}
215214
return $contents;
216215
}
217216

@@ -241,7 +240,7 @@ protected function caslines_to_answer($caslines, $secrules = false) {
241240
*/
242241
public function contents_to_maxima($contents) {
243242
if ($contents == ['EMPTYANSWER']) {
244-
return 'matrix(EMPTYCHAR)';
243+
return 'EMPTYANSWER';
245244
}
246245
$matrix = [];
247246
foreach ($contents as $row) {

tests/input_varmatrix_test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public function test_validate_student_response_blank_allowempty(): void {
346346
);
347347
$this->assertEquals(stack_input::SCORE, $state->status);
348348
$this->assertEquals('', $state->note);
349-
$this->assertEquals('matrix(EMPTYCHAR)', $state->contentsmodified);
349+
$this->assertEquals('EMPTYANSWER', $state->contentsmodified);
350350
$this->assertEquals(
351351
'',
352352
$state->contentsdisplayed

0 commit comments

Comments
 (0)