Skip to content

Commit 6a94b26

Browse files
authored
add option to skip checking correctness of model answer on import
1 parent ff4b834 commit 6a94b26

File tree

6 files changed

+283
-9
lines changed

6 files changed

+283
-9
lines changed

lang/en/qtype_formulas.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113
$string['error_bitwxor_integer'] = 'Bitwise XOR should only be used with integers.';
114114
$string['error_cannotusealgebraic'] = 'Algebraic variable \'{$a}\' cannot be used in this context.';
115115
$string['error_criterion_empty'] = 'The grading criterion must not be empty.';
116-
$string['error_damaged_question'] = 'Invalid data. The Formulas question might have been damaged, e. g. during a failed import or restore process.';
117116
$string['error_db_delete'] = 'Could not delete record from the database, table {$a}.';
118117
$string['error_db_missing_options'] = 'Formulas question ID {$a} was missing an options record. Using default.';
119118
$string['error_db_read'] = 'Could not read from to the database, table {$a}.';
@@ -334,6 +333,8 @@
334333
say, km, m, cm and mm. This option has no effect if no unit has been used.';
335334
$string['settingallowdecimalcomma'] = 'Localised decimal separator';
336335
$string['settingallowdecimalcomma_desc'] = 'Allow all students to use the comma as decimal separator in answers.<br>If activated, numbers will be displayed according to the locale settings.';
336+
$string['settinglenientimport'] = 'Lenient check on import';
337+
$string['settinglenientimport_desc'] = 'When importing a question, do not check whether the provided model answers would receive full marks. <br>Note: You should only activate this setting temporarily.';
337338
$string['settings_heading_general'] = 'General preferences';
338339
$string['settings_heading_general_desc'] = '';
339340
$string['settings_heading_width'] = 'Default widths';
@@ -394,6 +395,7 @@ functions and operators is given in the documentation.
394395
$string['error_algebraic_var'] = 'Syntax error of defining algebraic variable.';
395396
$string['error_answertype_mistmatch'] = 'Answer type mismatch: Numerical answer type requires number and algebraic answer type requires string';
396397
$string['error_criterion'] = 'The grading criterion must be evaluated to a single number.';
398+
$string['error_damaged_question'] = 'Invalid data. The Formulas question might have been damaged, e. g. during a failed import or restore process.';
397399
$string['error_eval_numerical'] = 'Some expressions cannot be evaluated numerically.';
398400
$string['error_fixed_range'] = 'Syntax error of a fixed range.';
399401
$string['error_forbid_char'] = 'Formula or expression contains forbidden characters or operators.';

questiontype.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public function save_question_options($formdata) {
258258
// Validate the data from the edit form.
259259
$filtered = $this->validate($formdata);
260260
if (!empty($filtered->errors)) {
261-
return (object)['error' => get_string('error_damaged_question', 'qtype_formulas')];
261+
return (object)['error' => implode("\n", $filtered->errors)];
262262
}
263263

264264
// Order the parts according to how they appear in the question.
@@ -850,7 +850,7 @@ public function check_and_filter_parts(object $data): object {
850850

851851
// The grading criterion must not be empty. Also, if there is no grading criterion, it does
852852
// not make sense to continue the validation.
853-
if (empty(trim($data->correctness[$i]))) {
853+
if (empty(trim($data->correctness[$i])) && !is_numeric(trim($data->correctness[$i]))) {
854854
$errors["correctness[$i]"] = get_string('error_criterion_empty', 'qtype_formulas');
855855
continue;
856856
}
@@ -965,7 +965,7 @@ public function validate(object $data): object {
965965
// depending on those variables (model answers, correctness criterion) and unit
966966
// stuff. This check also allows us to calculate the number of answers for each part,
967967
// a value that we store as 'numbox'.
968-
$evaluationresult = $this->check_variables_and_expressions($data, $parts);
968+
$evaluationresult = $this->check_variables_and_expressions($data, $parts, $isfromimport);
969969
$errors += $evaluationresult->errors;
970970
$parts = $evaluationresult->parts;
971971

@@ -1027,9 +1027,10 @@ private function validate_global_unit_fields(object $data): array {
10271027
*
10281028
* @param object $data
10291029
* @param object[] $parts
1030+
* @param bool $fromimport whether the check is performed during an import process
10301031
* @return object stdClass with 'errors' and 'parts'
10311032
*/
1032-
public function check_variables_and_expressions(object $data, array $parts): object {
1033+
public function check_variables_and_expressions(object $data, array $parts, bool $fromimport = false): object {
10331034
// Collect all errors.
10341035
$errors = [];
10351036

@@ -1224,7 +1225,11 @@ public function check_variables_and_expressions(object $data, array $parts): obj
12241225
}
12251226

12261227
// We used the model answers, so the grading criterion should always evaluate to 1 (or more).
1227-
if ($grade < 0.999) {
1228+
// This check is omitted when importing questions, because it did not exist in legacy versions,
1229+
// so teachers might have questions with "wrong" model answers and their import will fail.
1230+
$lenientimport = get_config('qtype_formulas', 'lenientimport');
1231+
$checkthis = !$lenientimport || !$fromimport;
1232+
if ($checkthis && $grade < 0.999) {
12281233
$errors["correctness[$i]"] = get_string('error_grading_not_one', 'qtype_formulas', $grade);
12291234
}
12301235

settings.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
0
4141
));
4242

43+
// Whether we should omit the check the model answer's correctness during imports.
44+
$settings->add(new admin_setting_configcheckbox(
45+
'qtype_formulas/lenientimport',
46+
new lang_string('settinglenientimport', 'qtype_formulas'),
47+
new lang_string('settinglenientimport_desc', 'qtype_formulas'),
48+
0
49+
));
50+
4351
// Default answer type.
4452
$settings->add(new admin_setting_configselect(
4553
'qtype_formulas/defaultanswertype',
@@ -53,13 +61,15 @@
5361
1000 => new lang_string('algebraic_formula', 'qtype_formulas'),
5462
]
5563
));
64+
5665
// Default correctness.
5766
$settings->add(new admin_setting_configtext(
5867
'qtype_formulas/defaultcorrectness',
5968
new lang_string('defaultcorrectness', 'qtype_formulas'),
6069
new lang_string('defaultcorrectness_desc', 'qtype_formulas'),
6170
'_relerr < 0.01'
6271
));
72+
6373
// Default answermark.
6474
$settings->add(new admin_setting_configtext(
6575
'qtype_formulas/defaultanswermark',
@@ -69,6 +79,7 @@
6979
PARAM_FLOAT,
7080
4
7181
));
82+
7283
// Default unit penalty.
7384
$settings->add(new admin_setting_configtext(
7485
'qtype_formulas/defaultunitpenalty',
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<quiz>
3+
<!-- question: 410 -->
4+
<question type="formulas">
5+
<name>
6+
<text>Formulas question 001</text>
7+
</name>
8+
<questiontext format="html">
9+
<text><![CDATA[Question text]]></text>
10+
</questiontext>
11+
<generalfeedback format="html">
12+
<text></text>
13+
</generalfeedback>
14+
<defaultgrade>2.0000000</defaultgrade>
15+
<penalty>0.1000000</penalty>
16+
<hidden>0</hidden>
17+
<correctfeedback format="html">
18+
<text></text>
19+
</correctfeedback>
20+
<partiallycorrectfeedback format="html">
21+
<text></text>
22+
</partiallycorrectfeedback>
23+
<incorrectfeedback format="html">
24+
<text></text>
25+
</incorrectfeedback>
26+
<varsrandom><text></text>
27+
</varsrandom>
28+
<varsglobal><text></text>
29+
</varsglobal>
30+
<answers>
31+
<partindex>
32+
<text>0</text>
33+
</partindex>
34+
<placeholder>
35+
<text></text>
36+
</placeholder>
37+
<answermark>
38+
<text>2</text>
39+
</answermark>
40+
<answertype>
41+
<text>0</text>
42+
</answertype>
43+
<numbox>
44+
<text>1</text>
45+
</numbox>
46+
<vars1>
47+
<text></text>
48+
</vars1>
49+
<answer>
50+
<text>1</text>
51+
</answer>
52+
<vars2>
53+
<text></text>
54+
</vars2>
55+
<correctness>
56+
<text>0</text>
57+
</correctness>
58+
<unitpenalty>
59+
<text>1</text>
60+
</unitpenalty>
61+
<postunit>
62+
<text></text>
63+
</postunit>
64+
<ruleid>
65+
<text>1</text>
66+
</ruleid>
67+
<otherrule>
68+
<text></text>
69+
</otherrule>
70+
<subqtext format="html">
71+
<text></text>
72+
</subqtext>
73+
<feedback format="html">
74+
<text></text>
75+
</feedback>
76+
</answers>
77+
</question>
78+
79+
</quiz>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<quiz>
3+
<!-- question: 410 -->
4+
<question type="formulas">
5+
<name>
6+
<text>Formulas question 001</text>
7+
</name>
8+
<questiontext format="html">
9+
<text><![CDATA[Question text]]></text>
10+
</questiontext>
11+
<generalfeedback format="html">
12+
<text></text>
13+
</generalfeedback>
14+
<defaultgrade>2.0000000</defaultgrade>
15+
<penalty>0.1000000</penalty>
16+
<hidden>0</hidden>
17+
<correctfeedback format="html">
18+
<text></text>
19+
</correctfeedback>
20+
<partiallycorrectfeedback format="html">
21+
<text></text>
22+
</partiallycorrectfeedback>
23+
<incorrectfeedback format="html">
24+
<text></text>
25+
</incorrectfeedback>
26+
<varsrandom><text></text>
27+
</varsrandom>
28+
<varsglobal><text></text>
29+
</varsglobal>
30+
<answers>
31+
<partindex>
32+
<text>0</text>
33+
</partindex>
34+
<placeholder>
35+
<text></text>
36+
</placeholder>
37+
<answermark>
38+
<text>2</text>
39+
</answermark>
40+
<answertype>
41+
<text>0</text>
42+
</answertype>
43+
<numbox>
44+
<text>1</text>
45+
</numbox>
46+
<vars1>
47+
<text></text>
48+
</vars1>
49+
<answer>
50+
<text></text>
51+
</answer>
52+
<vars2>
53+
<text></text>
54+
</vars2>
55+
<correctness>
56+
<text><![CDATA[_relerr < 0.01]]></text>
57+
</correctness>
58+
<unitpenalty>
59+
<text>1</text>
60+
</unitpenalty>
61+
<postunit>
62+
<text></text>
63+
</postunit>
64+
<ruleid>
65+
<text>1</text>
66+
</ruleid>
67+
<otherrule>
68+
<text></text>
69+
</otherrule>
70+
<subqtext format="html">
71+
<text></text>
72+
</subqtext>
73+
<feedback format="html">
74+
<text></text>
75+
</feedback>
76+
</answers>
77+
</question>
78+
79+
</quiz>

0 commit comments

Comments
 (0)