FormulasQuestion / moodle-qtype_formulas

Formulas question type for Moodle
17 stars 29 forks source link

Make question robust against fragile grading criterions #122

Closed PhilippImhof closed 8 months ago

PhilippImhof commented 8 months ago

The grading criterion is validated when the question is created or modified. It is therefore not possible to store an invalid grading criterion, i. e. one that contains undefined variables or one that would contain invalid operations like division by zero.

However, it is possible for the user to use values from the student answer in the grading criterion, e.g. they might write _0/_1 = a/b where a and b are valid variables. This criterion will validate, because during validation, the fields _0 and _1 will contain the values from the model answer.

Now, if a student enters 1 and 0 in the answer fields, the criterion will become 1/0 == a/b and division by zero is not defined. Currently this will cause the question to fail and can even lead to an invalid attempt state being stored in the DB.

This PR modifies the legacy code in order to make sure a ~badly-written~ risky grading criterion that is invalidated by a wrong student answer cannot break the question. A unit test is added to verify this works in the future.