From 370863a0ca66293eeda3b2bce08e68ccc5b2614c Mon Sep 17 00:00:00 2001 From: Stephan Kergomard Date: Fri, 3 Jan 2025 15:35:54 +0200 Subject: [PATCH] Test: Warn on Old Mark Schema in Personal Settings See: https://mantis.ilias.de/view.php?id=43396 --- .../ILIAS/Test/classes/class.ilObjTest.php | 47 ++++++++++++------- .../ILIAS/Test/classes/class.ilObjTestGUI.php | 27 ++++++----- lang/ilias_de.lang | 1 + lang/ilias_en.lang | 1 + 4 files changed, 49 insertions(+), 27 deletions(-) diff --git a/components/ILIAS/Test/classes/class.ilObjTest.php b/components/ILIAS/Test/classes/class.ilObjTest.php index cc9362c0d811..3fe9b01a5d9b 100755 --- a/components/ILIAS/Test/classes/class.ilObjTest.php +++ b/components/ILIAS/Test/classes/class.ilObjTest.php @@ -5762,6 +5762,16 @@ public function addDefaults($a_name) 'HideInfoTab' => (int) $main_settings->getAdditionalSettings()->getHideInfoTab(), ]; + $marks = array_map( + fn(Mark $v): array => [ + 'short_name' => $v->getShortName(), + 'official_name' => $v->getOfficialName(), + 'minimum_level' => $v->getMinimumLevel(), + 'passed' => $v->getPassed() + ], + $this->getMarkSchema()->getMarkSteps() + ); + $next_id = $this->db->nextId('tst_test_defaults'); $this->db->insert( 'tst_test_defaults', @@ -5770,30 +5780,35 @@ public function addDefaults($a_name) 'name' => ['text', $a_name], 'user_fi' => ['integer', $this->user->getId()], 'defaults' => ['clob', serialize($testsettings)], - 'marks' => ['clob', serialize($this->getMarkSchema()->getMarkSteps())], + 'marks' => ['clob', json_encode($marks)], 'tstamp' => ['integer', time()] ] ); } - /** - * Applies given test defaults to this test - * - * @param array $test_default The test defaults database id. - * - * @return boolean TRUE if the application succeeds, FALSE otherwise - */ - public function applyDefaults(array $test_defaults): bool + public function applyDefaults(array $test_defaults): string { $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; + $unserialized_marks = json_decode($test_defaults['marks'], true); + + $info = ''; + if (is_array($unserialized_marks) + && is_array($unserialized_marks[0])) { + $this->mark_schema = $this->getMarkSchema()->withMarkSteps( + array_map( + fn(array $v): Mark => new Mark( + $v['short_name'], + $v['official_name'], + $v['minimum_level'], + $v['passed'] + ), + $unserialized_marks + ) + ); + } else { + $info = 'old_mark_default_not_applied'; } - $this->mark_schema = $this->getMarkSchema()->withMarkSteps($unserialized_marks); - $this->storeActivationSettings([ 'is_activation_limited' => $testsettings['activation_limited'], 'activation_starting_time' => $testsettings['activation_start_time'], @@ -5924,7 +5939,7 @@ public function applyDefaults(array $test_defaults): bool $this->getScoreSettingsRepository()->store($score_settings); $this->saveToDb(); - return true; + return $info; } private function convertTimeToDateTimeImmutableIfNecessary( diff --git a/components/ILIAS/Test/classes/class.ilObjTestGUI.php b/components/ILIAS/Test/classes/class.ilObjTestGUI.php index 3a210c62676c..89c510a0a005 100755 --- a/components/ILIAS/Test/classes/class.ilObjTestGUI.php +++ b/components/ILIAS/Test/classes/class.ilObjTestGUI.php @@ -1398,13 +1398,13 @@ public function retrieveAdditionalDidacticTemplateOptions(): array */ public function afterSave(ilObject $new_object): void { + $info = ''; $new_object->saveToDb(); $test_def_id = $this->getSelectedPersonalDefaultsSettingsFromForm(); if ($test_def_id !== null - && ($defaults = $new_object->getTestDefaults($test_def_id)) !== null - && !$new_object->applyDefaults($defaults)) { - $this->tpl->setOnScreenMessage('failure', $this->lng->txt('tst_defaults_apply_not_possible')); + && ($defaults = $new_object->getTestDefaults($test_def_id)) !== null) { + $info = $new_object->applyDefaults($defaults); } $new_object->saveToDb(); @@ -1420,8 +1420,11 @@ public function afterSave(ilObject $new_object): void ); } - // always send a message - $this->tpl->setOnScreenMessage('success', $this->lng->txt('object_added'), true); + if ($info === '') { + $this->tpl->setOnScreenMessage('success', $this->lng->txt('object_added'), true); + } else { + $this->tpl->setOnScreenMessage('info', $this->lng->txt($info), true); + } $this->ctrl->setParameter($this, 'ref_id', $new_object->getRefId()); $this->ctrl->redirectByClass(SettingsMainGUI::class); } @@ -2007,13 +2010,13 @@ public function applyDefaultsObject($confirmed = false): void } // do not apply if user datasets exist - if ($this->getTestObject()->evalTotalPersons() > 0) { + if ($this->getTestObject()->evalTotalPersons() > 0 + || ($defaults = $this->getTestObject()->getTestDefaults($defaults_id[0])) === null) { $this->tpl->setOnScreenMessage('info', $this->lng->txt('tst_defaults_apply_not_possible')); $this->defaultsObject(); return; } - $defaults = $this->getTestObject()->getTestDefaults($defaults_id[0]); $default_settings = unserialize( $defaults['defaults'], ['allowed_classes' => [DateTimeImmutable::class]] @@ -2068,12 +2071,14 @@ public function applyDefaultsObject($confirmed = false): void $this->tpl->setOnScreenMessage('info', $info, true); } - if (is_array($defaults) && !$this->getTestObject()->applyDefaults($defaults)) { - $this->tpl->setOnScreenMessage('failure', $this->lng->txt('tst_defaults_apply_not_possible')); - $this->ctrl->redirect($this, 'defaults'); + $info = $this->getTestObject()->applyDefaults($defaults); + if ($info === '') { + $this->tpl->setOnScreenMessage('success', $this->lng->txt('tst_defaults_applied'), true); + } else { + $this->tpl->setOnScreenMessage('info', $this->lng->txt($info), true); } - $this->tpl->setOnScreenMessage('success', $this->lng->txt('tst_defaults_applied'), true); + if ($question_set_type_setting_switched && $old_question_set_config->doesQuestionSetRelatedDataExist()) { $old_question_set_config->removeQuestionSetRelatedData(); diff --git a/lang/ilias_de.lang b/lang/ilias_de.lang index be62424365ad..4da9651b1dfd 100644 --- a/lang/ilias_de.lang +++ b/lang/ilias_de.lang @@ -958,6 +958,7 @@ assessment#:#not_yet_accessed#:#Noch kein Zugriff erfolgt assessment#:#nr_of_correct_answers#:#Anzahl der erwünschten Antworten assessment#:#number_of_answers#:#Anzahl der Antworten assessment#:#numeric_gap#:#Numerische Lücke +assessment#:#old_mark_default_not_applied#:#Das Notenschma aus Ihren persönlichen Einstellungen konnte nicht übernommen werden, da es ein altes Format verwendet. Alle anderen Einstellungen wurden angewendet. assessment#:#option_label#:#Optionsbezeichnungen assessment#:#option_label_adequate#:#adäquat assessment#:#option_label_adequate_or_not#:#adäquat / nicht adäquat diff --git a/lang/ilias_en.lang b/lang/ilias_en.lang index c6544666f904..1eeef2d26ad6 100755 --- a/lang/ilias_en.lang +++ b/lang/ilias_en.lang @@ -959,6 +959,7 @@ assessment#:#not_yet_accessed#:#Not yet accessed assessment#:#nr_of_correct_answers#:#Number of Requested Answers assessment#:#number_of_answers#:#Number of Answers assessment#:#numeric_gap#:#Numeric Gap +assessment#:#old_mark_default_not_applied#:#The marks from your personal settings could not be applied as they use an old format. All other settings have been updated. assessment#:#option_label#:#Option Labels assessment#:#option_label_adequate#:#adequate assessment#:#option_label_adequate_or_not#:#adequate / not adequate