diff --git a/course/moodleform_mod.php b/course/moodleform_mod.php index cad2bf879e5..0d47d96b604 100644 --- a/course/moodleform_mod.php +++ b/course/moodleform_mod.php @@ -492,8 +492,16 @@ abstract class moodleform_mod extends moodleform { $validategradepass = true; } - // Confirm gradepass is a valid non-zero value. - if ($validategradepass && (!isset($data[$gradepassfieldname]) || grade_floatval($data[$gradepassfieldname]) == 0)) { + // We need to make all the validations related with $gradepassfieldname + // with them being correct floats, keeping the originals unmodified for + // later validations / showing the form back... + // TODO: Note that once MDL-73994 is fixed we'll have to re-visit this and + // adapt the code below to the new values arriving here, without forgetting + // the special case of empties and nulls. + $gradepass = isset($data[$gradepassfieldname]) ? unformat_float($data[$gradepassfieldname]) : null; + + // Confirm gradepass is a valid non-empty (null or zero) value. + if ($validategradepass && (is_null($gradepass) || $gradepass == 0)) { $errors['completionpassgrade'] = get_string( 'activitygradetopassnotset', 'completion' @@ -959,10 +967,9 @@ abstract class moodleform_mod extends moodleform { } // Grade to pass. - $mform->addElement('text', $gradepassfieldname, get_string('gradepass', 'grades')); + $mform->addElement('float', $gradepassfieldname, get_string('gradepass', 'grades')); $mform->addHelpButton($gradepassfieldname, 'gradepass', 'grades'); $mform->setDefault($gradepassfieldname, ''); - $mform->setType($gradepassfieldname, PARAM_RAW); $mform->hideIf($gradepassfieldname, $assessedfieldname, 'eq', '0'); $mform->hideIf($gradepassfieldname, "{$scalefieldname}[modgrade_type]", 'eq', 'none'); } @@ -1136,10 +1143,9 @@ abstract class moodleform_mod extends moodleform { } // Grade to pass. - $mform->addElement('text', $gradepassfieldname, get_string($gradepassfieldname, 'grades')); + $mform->addElement('float', $gradepassfieldname, get_string($gradepassfieldname, 'grades')); $mform->addHelpButton($gradepassfieldname, $gradepassfieldname, 'grades'); $mform->setDefault($gradepassfieldname, ''); - $mform->setType($gradepassfieldname, PARAM_RAW); $mform->hideIf($gradepassfieldname, "{$gradefieldname}[modgrade_type]", 'eq', 'none'); } } diff --git a/lib/gradelib.php b/lib/gradelib.php index 71cdc236595..72d13e40564 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -1586,14 +1586,14 @@ function grade_course_reset($courseid) { } /** - * Convert a number to 5 decimal point float, an empty string or a null db compatible format + * Convert a number to 5 decimal point float, null db compatible format * (we need this to decide if db value changed) * * @param float|null $number The number to convert * @return float|null float or null */ function grade_floatval(?float $number) { - if (is_null($number) or $number === '') { + if (is_null($number)) { return null; } // we must round to 5 digits to get the same precision as in 10,5 db fields diff --git a/mod/quiz/tests/behat/completion_condition_passing_grade.feature b/mod/quiz/tests/behat/completion_condition_passing_grade.feature index b021ca18d50..ff9bbfae821 100644 --- a/mod/quiz/tests/behat/completion_condition_passing_grade.feature +++ b/mod/quiz/tests/behat/completion_condition_passing_grade.feature @@ -54,3 +54,36 @@ Feature: Set a quiz to be marked complete when the student passes And I navigate to "Reports" in current page administration And I click on "Activity completion" "link" And "Completed" "icon" should exist in the "Student 1" "table_row" + + Scenario Outline: Verify that gradepass, together with completionpassgrade are validated correctly + Given the following "language customisations" exist: + | component | stringid | value | + | core_langconfig | decsep | | + And the following "activity" exist: + | activity | name | course | idnumber | gradepass | completion | completionpassgrade | + | quiz | Oh, grades, passgrades and floats| C1 | ohgrades | | 2 | | + When I am on the "ohgrades" "quiz activity editing" page logged in as "teacher1" + And I expand all fieldsets + And I set the field "Grade to pass" to "" + And I set the field "completionusegrade" to "1" + And I set the field "completionpassgrade" to "" + And I press "Save and display" + Then I should see "" + And I should not see "" + + Examples: + | gradepass | completionpassgrade | decsep | seen | notseen | outcome | + | | 0 | . | method: Highest | Save and display | ok | + | | 1 | . | does not have a valid | method: Highest | completion-err | + | 0 | 0 | . | method: Highest | Save and display | ok | + | 0 | 1 | . | does not have a valid | method: Highest | completion-err | + | aaa | 0 | . | must enter a number | method: Highest | number-err | + | aaa | 1 | . | must enter a number | method: Highest | number-err | + | 200 | 0 | . | can not be greater | method: Highest | grade-big-err | + | 200 | 1 | . | can not be greater | method: Highest | grade-big-err | + | 5.55 | 0 | . | 5.55 out of 100 | Save and display | ok | + | 5.55 | 1 | . | 5.55 out of 100 | Save and display | ok | + | 5#55 | 0 | . | must enter a number | method: Highest | number-err | + | 5#55 | 1 | . | must enter a number | method: Highest | number-err | + | 5#55 | 0 | # | 5#55 out of 100 | Save and display | ok | + | 5#55 | 1 | # | 5#55 out of 100 | Save and display | ok |