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
2 changes: 1 addition & 1 deletion amd/build/instantiation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/instantiation.min.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions amd/src/instantiation.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,9 @@ const instantiate = async() => {
}
}])[0];
if (response.status == 'error') {
let str = await String.get_string('previewerror', 'qtype_formulas');
document.getElementById('qtextpreview_display').innerHTML = `${str}<br>${response.message}`;
document.getElementById('qtextpreview_display').innerHTML = await String.get_string(
'previewerror', 'qtype_formulas', response.message
);
} else {
document.getElementById('qtextpreview_display').innerHTML = '';
prepareTableColumns(response.data);
Expand Down
13 changes: 9 additions & 4 deletions classes/external/instantiation.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,13 @@ protected static function fetch_one_instance($context, $parsedglobalvars, $parse
$partevaluator->evaluate($parsedlocalvars[$i]);
}
$localvars[$i] = self::variable_context_to_array($partevaluator->export_variable_context());
// Finally, evaluate the answer(s).
$answers[$i] = $partevaluator->evaluate($parsedanswers[$i])[0];
// Finally, evaluate the answer(s). If the model answer was empty, we cannot move on. As we
// are in a try-catch, we simply throw an exception with the appropriate error message.
$evaluationresult = $partevaluator->evaluate($parsedanswers[$i]);
if (empty($evaluationresult)) {
throw new Exception(get_string('error_answer_missing_in_part', 'qtype_formulas', $i + 1));
}
$answers[$i] = reset($evaluationresult);
}
} catch (Exception $e) {
return $e->getMessage();
Expand Down Expand Up @@ -512,7 +517,7 @@ public static function render_question_text($questiontext, $parttexts, $globalva
$renderedquestiontext = $evaluator->substitute_variables_in_text($params['questiontext']);
} catch (Exception $e) {
return [
'question' => get_string('previewerror', 'qtype_formulas') . ' ' . $e->getMessage(),
'question' => get_string('previewerror', 'qtype_formulas', $e->getMessage()),
'parts' => [],
];
}
Expand All @@ -529,7 +534,7 @@ public static function render_question_text($questiontext, $parttexts, $globalva
$renderedparttexts[$i] = $partevaluator->substitute_variables_in_text($params['parttexts'][$i]);
} catch (Exception $e) {
return [
'question' => get_string('previewerror', 'qtype_formulas') . ' ' . $e->getMessage(),
'question' => get_string('previewerror', 'qtype_formulas', $e->getMessage()),
'parts' => [],
];
}
Expand Down
3 changes: 2 additions & 1 deletion lang/en/qtype_formulas.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
$string['error_algebraic_relerr'] = 'Relative error (_relerr) cannot be used with answer type algebraic formula.';
$string['error_algvar_numbers'] = 'Algebraic variables can only be initialized with a list of numbers.';
$string['error_answer_missing'] = 'No answer has been defined.';
$string['error_answer_missing_in_part'] = 'No answer has been defined for part {$a}.';
$string['error_answerbox_duplicate'] = 'Answer box placeholders must be unique, found second instance of {$a}.';
$string['error_bitshift_integer'] = 'Bit shift operator should only be used with integers.';
$string['error_bitshift_negative'] = 'Bit shift by negative number {$a} is not allowed.';
Expand Down Expand Up @@ -288,7 +289,7 @@

Students are required to use the same input format. Examples:
<pre class="prettyprint">1 m<br>0.1 m^2<br>20 m s^(-1)<br>400 kg m/s<br>100 kW</pre>';
$string['previewerror'] = 'No preview available. Check your definition of random variables, global variables, parts\' local variables and answers. Original error message:';
$string['previewerror'] = 'No preview available. Check your definition of random variables, global variables, parts\' local variables and answers. Original error message: {$a}';
$string['privacy:metadata'] = 'The Formulas question type plugin does not store any personal data.';
$string['qtextpreview'] = 'Preview';
$string['questiontext'] = 'Question text';
Expand Down
18 changes: 18 additions & 0 deletions tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,24 @@ public function test_render_question_text($expected, $input): void {
*/
public static function provide_instantiation_data(): array {
return [
[
['status' => 'error', 'message' => "No answer has been defined for part 1."],
[
'n' => 1, 'randomvars' => '', 'globalvars' => '',
'localvars' => [''], 'answers' => [''],
],
],
[
['status' => 'ok', 'data' => [[
'randomvars' => [],
'globalvars' => [],
'parts' => [[['name' => '_0', 'value' => '1']]],
]]],
[
'n' => 1, 'randomvars' => '', 'globalvars' => '',
'localvars' => [''], 'answers' => ['1'],
],
],
[
['status' => 'ok', 'data' => [[
'randomvars' => [],
Expand Down
Loading