MDL-50650 core_grades: Validate minimum grade when importing grades
Also add behat test for max and min grade validation.
This commit is contained in:
committed by
Ilya Tregubov
parent
e2f5104676
commit
79ea6a2d22
@@ -149,24 +149,31 @@ class gradeimport_csv_load_data {
|
||||
/**
|
||||
* Inserts a record into the grade_import_values table. This also adds common record information.
|
||||
*
|
||||
* @param object $record The grade record being inserted into the database.
|
||||
* @param stdClass $record The grade record being inserted into the database.
|
||||
* @param int $studentid The student ID.
|
||||
* @return bool|int true or insert id on success. Null if the grade value is too high.
|
||||
* @param grade_item $gradeitem Grade item.
|
||||
* @return mixed true or insert id on success. Null if the grade value is too high or too low or grade item not exist.
|
||||
*/
|
||||
protected function insert_grade_record($record, $studentid) {
|
||||
protected function insert_grade_record(stdClass $record, int $studentid, grade_item $gradeitem): mixed {
|
||||
global $DB, $USER, $CFG;
|
||||
$record->importcode = $this->importcode;
|
||||
$record->userid = $studentid;
|
||||
$record->importer = $USER->id;
|
||||
// By default the maximum grade is 100.
|
||||
// If the grade limit has been increased then use the gradepointmax setting.
|
||||
// Unlimitedgrades allows for scores over 100%.
|
||||
// If the record final grade is set then check that the grade value isn't too high.
|
||||
// Final grade will not be set if we are inserting feedback.
|
||||
if (!isset($record->finalgrade) || $record->finalgrade <= $CFG->gradepointmax || $CFG->unlimitedgrades) {
|
||||
$gradepointmaximum = $gradeitem->grademax;
|
||||
$gradepointminimum = $gradeitem->grademin;
|
||||
|
||||
$finalgradeinrange =
|
||||
isset($record->finalgrade) && $record->finalgrade <= $gradepointmaximum && $record->finalgrade >= $gradepointminimum;
|
||||
if (!isset($record->finalgrade) || $finalgradeinrange || $CFG->unlimitedgrades) {
|
||||
return $DB->insert_record('grade_import_values', $record);
|
||||
} else {
|
||||
$this->cleanup_import(get_string('gradevaluetoobig', 'grades', $CFG->gradepointmax));
|
||||
if ($record->finalgrade > $gradepointmaximum) {
|
||||
$this->cleanup_import(get_string('gradevaluetoobig', 'grades', format_float($gradepointmaximum)));
|
||||
} else {
|
||||
$this->cleanup_import(get_string('gradevaluetoosmall', 'grades', format_float($gradepointminimum)));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -572,7 +579,12 @@ class gradeimport_csv_load_data {
|
||||
}
|
||||
}
|
||||
}
|
||||
$insertid = self::insert_grade_record($newgrade, $this->studentid);
|
||||
if (isset($newgrade->itemid)) {
|
||||
$gradeitem = new grade_item(['id' => $newgrade->itemid]);
|
||||
} else if (isset($newgrade->newgradeitem)) {
|
||||
$gradeitem = new grade_item(['id' => $newgrade->newgradeitem]);
|
||||
}
|
||||
$insertid = isset($gradeitem) ? self::insert_grade_record($newgrade, $this->studentid, $gradeitem) : null;
|
||||
// Check to see if the insert was successful.
|
||||
if (empty($insertid)) {
|
||||
return null;
|
||||
@@ -594,7 +606,7 @@ class gradeimport_csv_load_data {
|
||||
} else {
|
||||
// The grade item for this is not updated.
|
||||
$newfeedback->importonlyfeedback = true;
|
||||
$insertid = self::insert_grade_record($newfeedback, $this->studentid);
|
||||
$insertid = self::insert_grade_record($newfeedback, $this->studentid, new grade_item(['id' => $newfeedback->itemid]));
|
||||
// Check to see if the insert was successful.
|
||||
if (empty($insertid)) {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user