Skip to content

Commit

Permalink
GH-767 Prohibit deletion of default context
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszkiba committed Dec 20, 2024
1 parent 11fdd0a commit d5a116a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
12 changes: 9 additions & 3 deletions classes/catcontext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
}
}
}
15 changes: 8 additions & 7 deletions classes/table/catcontext_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lang/de/local_catquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down
1 change: 1 addition & 0 deletions lang/en/local_catquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down

0 comments on commit d5a116a

Please sign in to comment.