From d5a116a95dee6a11a8c6dbb4afb1f67cdba874d4 Mon Sep 17 00:00:00 2001 From: David Szkiba Date: Fri, 20 Dec 2024 09:58:33 +0100 Subject: [PATCH] GH-767 Prohibit deletion of default context --- classes/catcontext.php | 12 +++++++++--- classes/table/catcontext_table.php | 15 ++++++++------- lang/de/local_catquiz.php | 1 + lang/en/local_catquiz.php | 1 + 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/classes/catcontext.php b/classes/catcontext.php index 3cccc6e11..7d3626e59 100644 --- a/classes/catcontext.php +++ b/classes/catcontext.php @@ -33,6 +33,7 @@ use local_catquiz\local\model\model_person_param_list; use local_catquiz\local\model\model_responses; use local_catquiz\local\model\model_strategy; +use moodle_exception; use stdClass; defined('MOODLE_INTERNAL') || die(); @@ -391,16 +392,21 @@ public function gettimecalculated(): int { * @param int $id * * @return bool - * + * @throws moodle_exception */ public static function delete(int $id) { global $DB; + // The default context should never be deleted. + $defaultcontextid = catquiz::get_default_context_id(); + if ($id == $defaultcontextid) { + throw new moodle_exception('cannotdeletedefaultcontext', 'local_catquiz'); + } + try { $DB->delete_records('local_catquiz_catcontext', ['id' => $id]); - return true; } catch (dml_exception $e) { - return false; + throw new moodle_exception('error'); } } } diff --git a/classes/table/catcontext_table.php b/classes/table/catcontext_table.php index 41a7cae28..b4ac31516 100644 --- a/classes/table/catcontext_table.php +++ b/classes/table/catcontext_table.php @@ -29,6 +29,7 @@ global $CFG; use cache_helper; +use Exception; use local_catquiz\catcontext; use local_catquiz\testenvironment; use local_wunderbyte_table\wunderbyte_table; @@ -192,16 +193,16 @@ public function col_action($values) { */ public function action_deleteitem(int $id, string $data): array { - if (catcontext::delete($id)) { - $success = 1; + try { + catcontext::delete($id); $message = get_string('success'); - } else { - $success = 0; - $message = get_string('error'); + $success = true; + cache_helper::purge_by_event('changesincatcontexts'); + } catch (Exception $e) { + $message = $e->getMessage(); + $success = false; } - cache_helper::purge_by_event('changesincatcontexts'); - return [ 'success' => $success, 'message' => $message, diff --git a/lang/de/local_catquiz.php b/lang/de/local_catquiz.php index 01ffbcb20..8a67571e4 100644 --- a/lang/de/local_catquiz.php +++ b/lang/de/local_catquiz.php @@ -98,6 +98,7 @@ $string['callbackfunctionnotdefined'] = 'Callback Funktion nicht definiert.'; $string['canbesetto0iflabelgiven'] = 'Kann 0 sein, wenn Abgleich über Label stattfindet.'; $string['cancelexpiredattempts'] = 'Abgelaufene Versuche schließen'; +$string['cannotdeletedefaultcontext'] = 'Der Default CAT Kontext kann nicht gelöscht werden'; $string['cannotdeletescalewithchildren'] = 'Skalen mit Unterskalen können nicht gelöscht werden.'; $string['catcatscaleprime'] = 'Inhaltsbereich (Globalskala)'; $string['catcatscaleprime_help'] = 'Wählen Sie den für Sie relevanten Inhaltsbereich aus. Inhaltsbereche werden als CAT-Skala durch eine*n CAT-Manager*in angelegt und verwaltet. Falls Sie eigene Inhalts- und Unterbereiche wünschen, wenden Sie sich bitte an den oder die CAT-Manager*in oder den bzw. die Adminstrator*in Ihrer Moodle-Instanz.'; diff --git a/lang/en/local_catquiz.php b/lang/en/local_catquiz.php index e04eb51a2..dcc0e4ac7 100644 --- a/lang/en/local_catquiz.php +++ b/lang/en/local_catquiz.php @@ -98,6 +98,7 @@ $string['callbackfunctionnotdefined'] = 'Callback function is not defined.'; $string['canbesetto0iflabelgiven'] = 'Can be 0 if matching of testitem is via label.'; $string['cancelexpiredattempts'] = 'Cancel attempts exceeding maximum time'; +$string['cannotdeletedefaultcontext'] = 'Cannot delete default CAT context'; $string['cannotdeletescalewithchildren'] = 'Cannot delete CAT scale with children'; $string['catcatscaleprime'] = 'Content/Scale'; $string['catcatscaleprime_help'] = 'Select the content area that is relevant to you. Content areas are created and managed as a so-called scale by a CAT manager. If you would like your own content and sub-areas, please contact the CAT manager or the administrator of your Moodle instance.';