From a8e2b5cea1bf9bc7b2fd3bb22d9d206cb8a82315 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Thu, 30 Jan 2020 17:24:23 +0000 Subject: [PATCH] MDL-67842 questions: Cannot remove the idnumber from a question --- question/type/questiontypebase.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/question/type/questiontypebase.php b/question/type/questiontypebase.php index 7af20957460..d5b86496639 100644 --- a/question/type/questiontypebase.php +++ b/question/type/questiontypebase.php @@ -350,16 +350,22 @@ class question_type { $question->defaultmark = $form->defaultmark; } - if (isset($form->idnumber) && ((string) $form->idnumber !== '')) { - // While this check already exists in the form validation, this is a backstop preventing unnecessary errors. - if (strpos($form->category, ',') !== false) { - list($category, $categorycontextid) = explode(',', $form->category); + if (isset($form->idnumber)) { + if ((string) $form->idnumber === '') { + $question->idnumber = null; } else { - $category = $form->category; - } - if (!$DB->record_exists('question', - ['idnumber' => $form->idnumber, 'category' => $category])) { - $question->idnumber = $form->idnumber; + // While this check already exists in the form validation, + // this is a backstop preventing unnecessary errors. + // Only set the idnumber if it has changed and will not cause a unique index violation. + if (strpos($form->category, ',') !== false) { + list($category, $categorycontextid) = explode(',', $form->category); + } else { + $category = $form->category; + } + if (!$DB->record_exists('question', + ['idnumber' => $form->idnumber, 'category' => $category])) { + $question->idnumber = $form->idnumber; + } } }