PHP8 issue: "Argument #1 must be of type int" on gradelib.php,

PHP8 issue: "Argument #1 must be of type int" on gradelib.php,

by Marcelo Carvalho -
Number of replies: 3

Hi,

Testing Moodle 3.11.3+ on PHP 8.0.12 and MySQL 8.0.27, when changing some conclusion settings for a task in a course I got this error:

Exception: round(): Argument #1 ($num) must be of type int|float, string given

After searching for the error I found references (related to other systems) that it could be caused by an incompatibility between Moodle and PHP 8 regarding types (strict types). 

So i changed the file gradelib.php, line 1599 - from:

    return round($number, 5);

to: (type casting)

    return round((int)$number, 5);

And the error ceased.

May it happen on other files? Is there some actual solution instead changing manually all files with type mismatch?

OBS: It happened after I was testing some combinations of requirements for task conclusion, including trying to keep "Require approval grade" set without keeping "Student must receive a grade to conclude the course" set. I'm not sure if one of these combinations triggered the error, but after it happened I was not able to set "Require approval grade" in this and any other questionnaires, always raising the error. (I was trying to minimize the number of labels in the course - these new indicators of completed tasks are too big invasive - I really prefer the small squares as before)

*Maybe some descriptions of mentioned Moodle options and elements in this text may be inaccurate because I'm freely translating from another language, I don't use Moodle in English to be able check the actual name of these elements.


Average of ratings: -
In reply to Marcelo Carvalho

Re: PHP8 issue: "Argument #1 must be of type int" on gradelib.php,

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hi Marcelo,

If you check the release notes for Moodle 3.11 ( https://docs.moodle.org/dev/Moodle_3.11_release_notes ) you will see that it specifically states that "PHP 8.0 support is being implemented and is not ready for production yet".

You should use Moodle with a compatible version of PHP.

As for the fix you suggested - no, that is not correct. By converting the string to an integer, you are throwing away everything after the decimal point, before rounding it to 5 decimal places (e.g. if the original value was "2.345678", then the correct result after rounding would be: 2.34568, but your code would produce: 2)
In reply to Davo Smith

Re: PHP8 issue: "Argument #1 must be of type int" on gradelib.php,

by Marcelo Carvalho -
Hello Davo,

Thanks for the reply and information.

Yes, type casting has some risks if we dont't know what the developers did or what data is comming, and may cause side effects. Maybe a (float) type cast could work in this case.

About the issues with PHP8 I did not realize that - if there are significant bugs yet they should state it clearly in the latest release page. I just downgraded to PHP 7.4.25 and the script worked as original.

Also, I've seen some minor bugs in this 3.11.3+ release, like the tips that were not implemented yet, or the course Completion Tracking options that are not working as expected.
In reply to Davo Smith

Re: PHP8 issue: "Argument #1 must be of type int" on gradelib.php,

by Séverin Terrier -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators
Hi,

MDL-70745 is the tracker item dedicated to Moodle support of PHP 8.0. You must be connected to see all detailed issues linked.

Most of the work is done, but there are still things to do.

I really think it would be very useful that Moodle 4.0 totally supports PHP 8.0 when launched (and Moodle 3.11.x at a moment also). Don't know if people have time to do so before it comes out in december.

If problems are discovered, related to PHP 8.0, for sure tracker items should be created, and linked to MDL-70745.

Séverin