bug in FORMULAS question

bug in FORMULAS question

by Bernat Martinez -
Number of replies: 5

Hi all, 

First of all, congratulations to Jean Michel for the new version of FORMULAS question type, it works like a charm
Anyway it seems we found some bug, it happens when the algebraic formula ends with + , - , *  or /. Also if I introduce ++ , -- or // in the middle of algebraic a formulas and send the question, appears this error.
And we think that is due to php update to version 7.0 . 
We suppose that bug appears due to eval function , because that function in php 5 returns true or false, but now in php 7 it throws an error that stop the execution. So students can't acces later to the question, neither finish the attempt.

If you want to try, there's a link to a algebraic formulas question.( create an account or log in as teacher/teacher)


Average of ratings: Useful (1)
In reply to Bernat Martinez

Re: bug in FORMULAS question

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi Bernat,

I would also like to thank Jean-Michel for his excellent and indispensable work.

I get the same problem, for example: http://35.193.77.29/mod/quiz/view.php?id=631 (login: 'student', password: 'Moodle2018#'.

Until the bug is fixed, the red triangle warns that the answer will not be accepted and will actually make the question crash and make it inaccessible until reset by the teacher! (which means that I will have to reset my example if you try it smile)

In reply to Dominique Bauer

Re: bug in FORMULAS question

by Jean-Michel Védrine -

Hello,

Thanks to both of you for the report.

I missed these messages and only discover them today.

I tested the new version with php 7.1.9 and also Travis run the phpunit and Behat tests with php 7 without discovering the problem so there is surely something to improve in this area too.

See https://travis-ci.org/jmvedrine/moodle-qtype_formulas/jobs/337781982 ans scroll down to the end of the page to see that phpunit and Behat tests are OK.

When there is a bug it is better to create an issue in the CONTRIB tracker or in Github so that I receive a mail to warn me. That will be faster because I don't read this forum each day.

I will look at the problem.

Thanks also for providing informations and question samples to allow me to reproduce the problem.

It may takes a few days as I have some "family constraints" until Friday.

In reply to Jean-Michel Védrine

Re: bug in FORMULAS question

by Jean-Michel Védrine -

You are right the problem is in the eval function line 930 of the question/type/formulas/variables.php file.

According to a comment on the page http://php.net/manual/fr/function.eval.php this function has changed in php 7.x from just returning false to terminating the script if the evaluated code generate a fatal error

Most probably the solution is to include it in a try {  } catch block

Unfortunately I will not be able to commit the change after having fully tested it (and release a new version until Friday (sorry Family constraints !)

Maybe if you are brave enough you can test to replace this line with something like

            try {
                eval('$res = '.implode(' ',$splitted).';');
            } catch (Throwable $t) {
                throw new Exception(get_string('error_eval_numerical','qtype_formulas'));
            }


In reply to Jean-Michel Védrine

Re: bug in FORMULAS question

by Jean-Michel Védrine -

I was able to conduct very quick tests of the above fix and it seems to solve the problem.

It also solve the problem I had when I ported the php tests that were included in variables.php and that are now in question/type/formulas/tests/variables_test.php that 4 tests were not giving the expected result.

And this demonstrate that when you test something you have to consider all factors : when I saw that these 4 tests were falling, I quickly set up some files to verify in that in Hon Wai Lau original tests those 4 tests were also falling and I deducted that these failures were not harmful.

Of coure I was wrong because I tested the original tests with php 7 not with the php version that was used at that time !