diff --git a/course/classes/category.php b/course/classes/category.php index 30cc36e0acf..04c7381dd4d 100644 --- a/course/classes/category.php +++ b/course/classes/category.php @@ -596,7 +596,11 @@ class core_course_category implements renderable, cacheable_object, IteratorAggr if (core_text::strlen($data->idnumber) > 100) { throw new moodle_exception('idnumbertoolong'); } - if (strval($data->idnumber) !== '' && $DB->record_exists('course_categories', array('idnumber' => $data->idnumber))) { + + // Ensure there are no other categories with the same idnumber. + if (strval($data->idnumber) !== '' && + $DB->record_exists_select('course_categories', 'idnumber = ? AND id != ?', [$data->idnumber, $this->id])) { + throw new moodle_exception('categoryidnumbertaken'); } $newcategory->idnumber = $data->idnumber;