From 7dc2c5739219cc1bf7e54460dfdecea70998a5d3 Mon Sep 17 00:00:00 2001 From: Stephan Kergomard Date: Sat, 7 Dec 2024 12:14:37 +0100 Subject: [PATCH] Test: Fix Applying Default Settings --- .../ILIASObject/classes/class.ilObjectGUI.php | 12 +++++-- .../ILIAS/Test/classes/class.ilObjTest.php | 10 +++--- .../ILIAS/Test/classes/class.ilObjTestGUI.php | 31 ++++++++++--------- .../ILIAS/Test/src/Scoring/Marks/Mark.php | 12 ------- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/components/ILIAS/ILIASObject/classes/class.ilObjectGUI.php b/components/ILIAS/ILIASObject/classes/class.ilObjectGUI.php index 348b87979b9d..84c93cbb9b1c 100755 --- a/components/ILIAS/ILIASObject/classes/class.ilObjectGUI.php +++ b/components/ILIAS/ILIASObject/classes/class.ilObjectGUI.php @@ -987,11 +987,19 @@ public function saveObject(): void */ public function getDidacticTemplateVar(string $type): int { - if (!$this->post_wrapper->has("didactic_type")) { + $create_form = $this->initCreateForm($this->type); + if ($create_form instanceof StandardForm) { + $data = $create_form->withRequest($this->request)->getData(); + return isset($data['didactic_templates']) + ? $this->parseDidacticTemplateVar($data['didactic_templates'], $type) + : 0; + } + + if (!$this->post_wrapper->has('didactic_type')) { return 0; } - $tpl = $this->post_wrapper->retrieve("didactic_type", $this->refinery->kindlyTo()->string()); + $tpl = $this->post_wrapper->retrieve('didactic_type', $this->refinery->kindlyTo()->string()); return $this->parseDidacticTemplateVar($tpl, $type); } diff --git a/components/ILIAS/Test/classes/class.ilObjTest.php b/components/ILIAS/Test/classes/class.ilObjTest.php index ab232261094c..ef9e288c4d1f 100755 --- a/components/ILIAS/Test/classes/class.ilObjTest.php +++ b/components/ILIAS/Test/classes/class.ilObjTest.php @@ -5951,11 +5951,11 @@ public function addDefaults($a_name) */ public function applyDefaults($test_defaults): bool { - $testsettings = unserialize($test_defaults['defaults']); - $unserialized_marks = unserialize($test_defaults['marks']); - - if ($unserialized_marks instanceof MarkSchema) { - $unserialized_marks = $unserialized_marks->getMarkSteps(); + $testsettings = unserialize($test_defaults['defaults'], ['allowed_classes' => [DateTimeImmutable::class]]); + try { + $unserialized_marks = unserialize($test_defaults['marks'], ['allowed_classes' => [Mark::class]]); + } catch (Exception $e) { + return false; } $this->mark_schema = $this->getMarkSchema()->withMarkSteps($unserialized_marks); diff --git a/components/ILIAS/Test/classes/class.ilObjTestGUI.php b/components/ILIAS/Test/classes/class.ilObjTestGUI.php index 7e8292d7dc38..ad15a8363527 100755 --- a/components/ILIAS/Test/classes/class.ilObjTestGUI.php +++ b/components/ILIAS/Test/classes/class.ilObjTestGUI.php @@ -1397,15 +1397,12 @@ public function afterSave(ilObject $new_object): void { $new_object->saveToDb(); - $test_def_id = $this->getDidacticTemplateVar("tstdef"); + $test_def_id = $this->getDidacticTemplateVar('tstdef'); if ($test_def_id !== 0) { $test_defaults = $new_object->getTestDefaults($test_def_id); - $new_object->applyDefaults($test_defaults); - } - - $template_id = $this->getDidacticTemplateVar("tsttpl"); - if ($template_id) { - $new_object->setTemplate($template_id); + if (!$new_object->applyDefaults($test_defaults)) { + $this->tpl->setOnScreenMessage('failure', $this->lng->txt('tst_defaults_apply_not_possible')); + } } $new_object->saveToDb(); @@ -1422,7 +1419,7 @@ public function afterSave(ilObject $new_object): void } // always send a message - $this->tpl->setOnScreenMessage('success', $this->lng->txt("object_added"), true); + $this->tpl->setOnScreenMessage('success', $this->lng->txt('object_added'), true); $this->ctrl->setParameter($this, 'ref_id', $new_object->getRefId()); $this->ctrl->redirectByClass(SettingsMainGUI::class); } @@ -1990,10 +1987,9 @@ public function confirmedApplyDefaultsObject() */ public function applyDefaultsObject($confirmed = false): void { - $defaults = $this->testrequest->retrieveArrayOfIntsFromPost('chb_defaults'); - if ($defaults !== []) { + $defaults_id = $this->testrequest->retrieveArrayOfIntsFromPost('chb_defaults'); + if ($defaults_id === []) { $this->tpl->setOnScreenMessage('info', $this->lng->txt('tst_defaults_apply_select_one')); - $this->defaultsObject(); return; } @@ -2005,7 +2001,11 @@ public function applyDefaultsObject($confirmed = false): void return; } - $default_settings = unserialize($defaults[0]['defaults'], ['allowed_classes' => false]); + $defaults = $this->getTestObject()->getTestDefaults($defaults_id[0]); + $default_settings = unserialize( + $defaults['defaults'], + ['allowed_classes' => [DateTimeImmutable::class]] + ); if (isset($default_settings['isRandomTest'])) { if ($default_settings['isRandomTest']) { @@ -2052,13 +2052,14 @@ public function applyDefaultsObject($confirmed = false): void if ($question_set_type_setting_switched && !$this->getTestObject()->getOfflineStatus()) { $this->getTestObject()->setOfflineStatus(true); - $info = $this->lng->txt('tst_set_offline_due_to_switched_question_set_type_setting'); - $this->tpl->setOnScreenMessage('info', $info, true); } - $this->getTestObject()->applyDefaults($defaults); + if (!$this->getTestObject()->applyDefaults($defaults)) { + $this->tpl->setOnScreenMessage('failure', $this->lng->txt('tst_defaults_apply_not_possible')); + $this->ctrl->redirect($this, 'defaults'); + } $this->tpl->setOnScreenMessage('success', $this->lng->txt('tst_defaults_applied'), true); diff --git a/components/ILIAS/Test/src/Scoring/Marks/Mark.php b/components/ILIAS/Test/src/Scoring/Marks/Mark.php index fe60fd83b8c3..6423e0fa2ba8 100755 --- a/components/ILIAS/Test/src/Scoring/Marks/Mark.php +++ b/components/ILIAS/Test/src/Scoring/Marks/Mark.php @@ -42,18 +42,6 @@ public function __construct( ) { } - /** - * Stephan Kergomard, 2023-11-08: We need an explicit __unserialize function - * here because of changes to the corresponding classes with ILIAS 8. - */ - public function __unserialize(array $data): void - { - $this->short_name = $data['short_name']; - $this->official_name = $data['short_name']; - $this->minimum_level = (float) $data['minimum_level']; - $this->passed = (int) $data['passed']; - } - public function getShortName(): string { return $this->short_name;