MDL-67116 course: Strict empty string check required

In PHP the following are true:

    assert('' == '');
    assert(0 == '');
    assert(null == '');

The options in the course completion settings are now:

    ''    => none selected
    [int] => itemnumber of activity course completion item.

In the case where the itemnumber was 0  this was being incorrectly
evaluated as an empty string due to the implicit cast to int of the
empty string:

    (int) '' === 0

As a result, the non-strict comparison means that the following are
identical:

    assert(0 == '');
    assert(0 == (int) '');
    assert(0 == '0');

Changing the comparison to a strict comparison is correct because it
ensures the type consitency between the empty string value, and the int
of itemnumber 0. It is safe because the value always comes from the
form, where an int is always returned, and not the DB, where a string is
returned.
This commit is contained in:
Andrew Nicols 2019-11-14 08:38:19 +08:00
parent c035141971
commit 0a5a2ca89b

View file

@ -72,7 +72,7 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) {
$completion = new completion_info($course);
if ($completion->is_enabled()) {
$newcm->completion = $moduleinfo->completion;
if ($moduleinfo->completiongradeitemnumber == '') {
if ($moduleinfo->completiongradeitemnumber === '') {
$newcm->completiongradeitemnumber = null;
} else {
$newcm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
@ -527,8 +527,7 @@ function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
// Completion settings that would affect users who have already completed
// the activity may be locked; if so, these should not be updated.
if (!empty($moduleinfo->completionunlocked)) {
$cm->completion = $moduleinfo->completion;
if ($moduleinfo->completiongradeitemnumber == '') {
if ($moduleinfo->completiongradeitemnumber === '') {
$cm->completiongradeitemnumber = null;
} else {
$cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;