From ebaec57bb230820d50fa92558668ce7cbc2880ce Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 12 Dec 2022 14:20:10 +0000 Subject: [PATCH] MDL-76611 cohort: prevent duplicate idnumber in inplace editable. --- cohort/classes/output/cohortidnumber.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cohort/classes/output/cohortidnumber.php b/cohort/classes/output/cohortidnumber.php index 454fdde638b..482025542fa 100644 --- a/cohort/classes/output/cohortidnumber.php +++ b/cohort/classes/output/cohortidnumber.php @@ -63,9 +63,11 @@ class cohortidnumber extends \core\output\inplace_editable { $cohortcontext = \context::instance_by_id($cohort->contextid); \external_api::validate_context($cohortcontext); require_capability('moodle/cohort:manage', $cohortcontext); - $record = (object)array('id' => $cohort->id, 'idnumber' => $newvalue, 'contextid' => $cohort->contextid); - cohort_update_cohort($record); - $cohort->idnumber = $newvalue; + if ($newvalue == '' || !$DB->record_exists_select('cohort', 'idnumber = ? AND id != ?', [$newvalue, $cohort->id])) { + $record = (object) ['id' => $cohort->id, 'idnumber' => $newvalue, 'contextid' => $cohort->contextid]; + cohort_update_cohort($record); + $cohort->idnumber = $newvalue; + } return new static($cohort); } }