Skip to content

Commit f3744ca

Browse files
committed
broken-save - Deal with input/PRT addition and deletion.
1 parent 8b2c5df commit f3744ca

4 files changed

Lines changed: 27 additions & 8 deletions

File tree

edit_stack_form.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ protected function definition() {
179179
$mform->addHelpButton('fixdollars', 'fixdollars', 'qtype_stack');
180180
$isbroken = $mform->createElement('checkbox', 'isbroken',
181181
stack_string('isbroken'), stack_string('isbrokenlabel'));
182-
$mform->setDefault('isbroken', $this->question->options->isbroken);
182+
$mform->setDefault('isbroken', (!empty($this->question->options->isbroken) ? 1 : 0));
183183
$mform->insertElementBefore($isbroken, 'buttonar');
184184
$mform->addHelpButton('isbroken', 'isbroken', 'qtype_stack');
185185
$mform->closeHeaderBefore('fixdollars');
@@ -507,10 +507,13 @@ protected function definition_input($inputname, MoodleQuickForm $mform, $counts)
507507
// Set a default for the new question.
508508
if ($inputname === self::DEFAULT_INPUT) {
509509
$mform->setDefault($inputname . 'modelans', self::DEFAULT_TEACHER_ANSWER);
510+
} else {
511+
// Default for all parts of an input required in order to save a broken question.
512+
$mform->setDefault($inputname . 'modelans', '');
510513
}
511514

512515
$mform->addElement('text', $inputname . 'boxsize', stack_string('boxsize'), ['size' => 3]);
513-
$mform->setDefault($inputname . 'boxsize', $this->stackconfig->inputboxsize);
516+
$mform->setDefault($inputname . 'boxsize', (int) $this->stackconfig->inputboxsize);
514517
$mform->setType($inputname . 'boxsize', PARAM_INT);
515518
$mform->addHelpButton($inputname . 'boxsize', 'boxsize', 'qtype_stack');
516519
$mform->hideIf($inputname . 'boxsize', $inputname . 'type', 'in',
@@ -585,6 +588,7 @@ protected function definition_input($inputname, MoodleQuickForm $mform, $counts)
585588
$mform->hideIf($inputname . 'showvalidation', $inputname . 'type', 'in', []);
586589

587590
$mform->addElement('text', $inputname . 'options', stack_string('inputextraoptions'), ['size' => 30]);
591+
$mform->setDefault($inputname . 'options', '');
588592
$mform->setType($inputname . 'options', PARAM_RAW);
589593
$mform->addHelpButton($inputname . 'options', 'inputextraoptions', 'qtype_stack');
590594
}
@@ -961,13 +965,20 @@ protected function prepare_text_field($field, $text, $format, $itemid, $filearea
961965
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
962966
public function validation($fromform, $files) {
963967
$errors = parent::validation($fromform, $files);
964-
// By-pass STACK-specific validation if question is marked as broken.
965-
// Moodle validation still performed.
968+
$qtype = new qtype_stack();
969+
$allerrors = $qtype->validate_fromform($fromform, $errors);
970+
// Ignore STACK-specific validation if question is marked as broken unless
971+
// a confirmation is required.
972+
// Moodle validation errors always returned.
966973
if (empty($fromform['isbroken'])) {
967-
$qtype = new qtype_stack();
968-
return $qtype->validate_fromform($fromform, $errors);
974+
return $allerrors;
969975
} else {
970-
return $errors;
976+
if (array_search(stack_string('youmustconfirm'), $allerrors)) {
977+
$allerrors['versioninfo'] = stack_string('notsaved') . $allerrors['versioninfo'];
978+
return $allerrors;
979+
} else {
980+
return $errors;
981+
}
971982
}
972983
}
973984

lang/en/qtype_stack.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@
404404
$string['variantsselectionseed_help'] = 'Normally you can leave this box blank. If, however, you want two different questions in a quiz to use the same random seed, then type the same string in this box for the two questions (and deploy the same set of random seeds, if you are using deployed variants) and the random seeds for the two questions will be synchronised.';
405405
$string['verifyquestionandupdate'] = 'Verify the question text and update the form';
406406
$string['youmustconfirm'] = 'You must confirm here.';
407+
$string['notsaved'] = '** QUESTION WAS NOT SAVED ** You have changes to confirm.';
407408

408409
// Strings used by input elements.
409410
$string['studentinputtoolong'] = 'Your input is longer than permitted by STACK.';

questiontype.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ public function save_question_options($fromform) {
235235
$prts = $DB->get_records('qtype_stack_prts',
236236
['questionid' => $fromform->id], '', 'name, id, questionid');
237237
foreach ($prtnames as $prtname) {
238+
if (!isset($fromform->{$prtname . 'feedbackvariables'})) {
239+
// Skip the PRT if it's not been set up. This should only occur when
240+
// saving a broken question.
241+
continue;
242+
}
243+
238244
if (array_key_exists($prtname, $prts)) {
239245
$prt = $prts[$prtname];
240246
unset($prts[$prtname]);
@@ -271,7 +277,7 @@ public function save_question_options($fromform) {
271277
}
272278
$graph->layout();
273279
$roots = $graph->get_roots();
274-
if (count($roots) != 1 || $graph->get_broken_cycles()) {
280+
if (empty($fromform->isbroken) && (count($roots) != 1 || $graph->get_broken_cycles())) {
275281
throw new coding_exception('The PRT ' . $prtname . ' is malformed.');
276282
}
277283
reset($roots);

tests/questiontype_test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ public function test_xml_export(): void {
251251
<inversetrig>cos-1</inversetrig>
252252
<logicsymbol>lang</logicsymbol>
253253
<matrixparens>[</matrixparens>
254+
<isbroken>0</isbroken>
254255
<variantsselectionseed></variantsselectionseed>
255256
<input>
256257
<name>ans1</name>

0 commit comments

Comments
 (0)