A fix for https://tracker.moodle.org/browse/CONTRIB-4645

Hi,

I made a fix for the aforementioned bug.

I haven't checked what happens in all different cases, but it shouldn't break what it didn't set out to fix.

~Jacob