Fixing https://tracker.moodle.org/browse/CONTRIB-4322, thanks to Elizabeth Dalton
Fixing CONTRIB-3204
Merge remote-tracking branch 'originHTTPS/MOODLE_21_STABLE' into reviewed21