Details

Type: Bug

Status: Closed

Priority: Minor

Resolution: Fixed

Affects Version/s: 2.3.2, 2.4

Component/s: Quiz

Labels:

Testing Instructions:

Affected Branches:MOODLE_23_STABLE, MOODLE_24_STABLE

Fixed Branches:MOODLE_22_STABLE, MOODLE_23_STABLE, MOODLE_24_STABLE

Pull from Repository:

Pull Master Branch:
MDL36025negativezero 
Pull Master Diff URL:
Description
The processing of "0" as a dataset value is incorrect. I'm not sure the circumstances that create "0", but moodle must be creating it as we have lots on our site. It's also possible to enter "0" manually as a dataset value.
The bug is in evaluating a formula with the "0". In qtype_calculate  >substitute_variables(), it won't use parentheses around "0" since "0" >= 0. For example. 
{A}becomes in 0. This results in a PHP error being printed.
For format "Significant figures", this actually results in an infinite loop since there is no check that $answer is numeric before executing:
qtype_calculated_calculate_answer():
$p10 = 0;

while ($answer < 1) {

$p10;

$answer *= 10;

}

Suggested fixes:
1. In qtype_calculate>substitute_variables(), use parens for $val <= 0
2. In qtype_calculate>substitute_variables_and_eval(), use a different variable than $str in the eval(). Otherwise, if the eval() fails, the function will return the original formula. This triggers the infinite loop above.
3. Check for and reject "" (and "++") in qtype_calculated_find_formula_errors().
Also
4. Add a check that $answer is numeric before running the above loop. Eval() seems like something of a wild card to me, and I wonder if we can really guarantee it'll never return anything weird? An infinite loop makes for a bad user experience