Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lang/en/qtype_stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,9 @@
$string['noroots'] = 'The graph of this PRT has no roots. Does it have nodes?';
$string['structuralproblem'] = 'The PRT structure is malformed.';
$string['missingnextnode'] = 'The PRT structure is malformed. {$a->type} next node for PRT {$a->prt} node {$a->node} is invalid. It has been set to stop.';
$string['multipleinputs'] = 'Multiple inputs have the same name: {$a}.';
$string['multipleprts'] = 'Multiple prts have the same name: {$a}.';
$string['multiplenodes'] = 'Multiple nodes have the name: {$a->node} in PRT: {$a->prt}.';

// Equiv input specific string.
$string['equivnocomments'] = 'You are not permitted to use comments in this input type. Please just work line by line.';
Expand Down
58 changes: 50 additions & 8 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ public function save_question_options($fromform) {
break;
}

if (!empty($fromform->structuralerror)) {
if ($throwexceptions) {
throw new stack_exception($fromform->validationerrors);
}
$result->error = html_writer::tag('h6', $fromform->name);
$result->error .= $fromform->validationerrors;
return $result;
}

$context = $fromform->context;

parent::save_question_options($fromform);
Expand Down Expand Up @@ -1786,6 +1795,8 @@ public function import_from_xml($xml, $fromform, qformat_xml $format, $notused =
return false;
}

$loaderrors = [];

$fromform = $format->import_headers($xml);
$fromform->qtype = $this->name();

Expand Down Expand Up @@ -1871,8 +1882,15 @@ public function import_from_xml($xml, $fromform, qformat_xml $format, $notused =

$structurerepairs = '';
if (isset($xml['#']['input']) && count($xml['#']['input'])) {
$loadedinputs = [];
foreach ($xml['#']['input'] as $inputxml) {
$this->import_xml_input($inputxml, $fromform, $format);
$loadedinput = $this->import_xml_input($inputxml, $fromform, $format);
if (in_array($loadedinput, $loadedinputs)) {
$loaderrors[$loadedinput . 'input'] = stack_string('multipleinputs', $loadedinput);
$fromform->structuralerror = true;
} else {
$loadedinputs[] = $loadedinput;
}
}
} else {
if ($fromform->defaultmark) {
Expand All @@ -1883,8 +1901,17 @@ public function import_from_xml($xml, $fromform, qformat_xml $format, $notused =
}

if (isset($xml['#']['prt']) && count($xml['#']['prt'])) {
$loadedprts = [];
foreach ($xml['#']['prt'] as $prtxml) {
$structurerepairs .= $this->import_xml_prt($prtxml, $fromform, $format);
[$currentrepairs, $loadedprt, $nodeerrors] = $this->import_xml_prt($prtxml, $fromform, $format);
$loaderrors = array_merge($loaderrors, $nodeerrors);
$structurerepairs .= $currentrepairs;
if (in_array($loadedprt, $loadedprts)) {
$loaderrors[$loadedprt . 'prt'] = stack_string('multipleprts', $loadedprt);
$fromform->structuralerror = true;
} else {
$loadedprts[] = $loadedprt;
}
}
} else {
if ($fromform->defaultmark) {
Expand Down Expand Up @@ -1930,6 +1957,7 @@ public function import_from_xml($xml, $fromform, qformat_xml $format, $notused =
$this->prtgraph = [];

$errors = $this->validate_fromform($formarray, []);
$errors = array_merge($loaderrors, $this->validate_fromform($formarray, []));
if ($structurerepairs) {
$errors['structurerepairs'] = $structurerepairs;
}
Expand All @@ -1939,11 +1967,9 @@ public function import_from_xml($xml, $fromform, qformat_xml $format, $notused =
foreach ($errors as $key => $error) {
$errortext .= $key . ': ' . $error . ' <br />';
}
if (isset($errors['structuralerror'])) {
if (isset($errors['structuralerror']) || !empty($fromform->structuralerror)) {
// Graph creation failed. If we import this question
// we won't be able to open it in the edit form.
// TO-DO Once we have a text-based editor we could allow saving
// of even really broken questions.
$errortext .= stack_string('importwillfail');
} else {
$errortext .= stack_string('markedasbroken');
Expand Down Expand Up @@ -1984,6 +2010,8 @@ protected function import_xml_text($xml, $field, qformat_xml $format, $defaultfo
* @param array $xml the bit of the XML representing one input.
* @param object $fromform the data structure we are building from the XML.
* @param qformat_xml $format the importer/exporter object.
*
* @return string|null name of the input
*/
protected function import_xml_input($xml, $fromform, qformat_xml $format) {
$name = $format->getpath($xml, ['#', 'name', 0, '#'], null, false, 'Missing input name in the XML.');
Expand Down Expand Up @@ -2011,6 +2039,8 @@ protected function import_xml_input($xml, $fromform, qformat_xml $format) {
$fromform->{$name . 'showvalidation'}
= $format->getpath($xml, ['#', 'showvalidation', 0, '#'], get_config('qtype_stack', 'inputshowvalidation'));
$fromform->{$name . 'options'} = $format->getpath($xml, ['#', 'options', 0, '#'], '');

return $name;
}

/**
Expand All @@ -2019,7 +2049,7 @@ protected function import_xml_input($xml, $fromform, qformat_xml $format) {
* @param object $fromform the data structure we are building from the XML.
* @param qformat_xml $format the importer/exporter object.
*
* @return string errors
* @return array [errors string, prtname string, loaderrors string]
*/
protected function import_xml_prt($xml, $fromform, qformat_xml $format) {
$errors = [];
Expand All @@ -2043,9 +2073,17 @@ protected function import_xml_prt($xml, $fromform, qformat_xml $format) {
$fromform->{$name . $field} = [];
}

$loaderrors = [];
if (isset($xml['#']['node'])) {
$loadednodes = [];
foreach ($xml['#']['node'] as $nodexml) {
$this->import_xml_prt_node($nodexml, $name, $fromform, $format);
$loadednode = $this->import_xml_prt_node($nodexml, $name, $fromform, $format);
if (in_array($loadednode, $loadednodes)) {
$loaderrors[$loadednode . 'node'] = stack_string('multiplenodes', ['prt' => $name, 'node' => $loadednode]);
$fromform->structuralerror = true;
} else {
$loadednodes[] = $loadednode;
}
}
}

Expand All @@ -2070,7 +2108,7 @@ protected function import_xml_prt($xml, $fromform, qformat_xml $format) {
}
}
}
return implode(' ', $errors);
return [implode(' ', $errors), $name, $loaderrors];
;
}

Expand All @@ -2080,6 +2118,8 @@ protected function import_xml_prt($xml, $fromform, qformat_xml $format) {
* @param string $prtname the name of the PRT this node belongs to.
* @param object $fromform the data structure we are building from the XML.
* @param qformat_xml $format the importer/exporter object.
*
* @return string node name
*/
protected function import_xml_prt_node($xml, $prtname, $fromform, qformat_xml $format) {
$name = $format->getpath($xml, ['#', 'name', 0, '#'], null, false, 'Missing PRT node name in the XML.');
Expand Down Expand Up @@ -2116,6 +2156,8 @@ protected function import_xml_prt_node($xml, $prtname, $fromform, qformat_xml $f
$format,
FORMAT_HTML
);

return $name;
}

/**
Expand Down
Loading
Loading