From ec1731ff31101944c88aa11185e5597537e7e656 Mon Sep 17 00:00:00 2001 From: siwane Date: Wed, 17 Jan 2024 14:51:04 +0100 Subject: [PATCH] feat: enforce default value for testtaker import --- models/CsvImporter.php | 49 +++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/models/CsvImporter.php b/models/CsvImporter.php index 6fcad36..63b8c13 100644 --- a/models/CsvImporter.php +++ b/models/CsvImporter.php @@ -22,12 +22,21 @@ namespace oat\taoTestTaker\models; +use common_ext_ExtensionException; +use common_ext_ExtensionsManager; +use core_kernel_classes_Property; use core_kernel_classes_Resource; +use core_kernel_persistence_Exception; +use core_kernel_users_Service; use oat\generis\Helper\UserHashForEncryption; +use oat\generis\model\user\UserRdf; use oat\oatbox\service\ServiceManager; use oat\tao\model\TaoOntology; use oat\generis\model\GenerisRdf; use oat\taoTestTaker\models\events\dispatcher\TestTakerImportEventDispatcher; +use tao_helpers_form_FormFactory; +use tao_helpers_I18n; +use tao_models_classes_import_CsvImporter; /** * A custom subject CSV importer @@ -37,7 +46,7 @@ * @package taoSubjects */ -class CsvImporter extends \tao_models_classes_import_CsvImporter +class CsvImporter extends tao_models_classes_import_CsvImporter { public function import($class, $form, $userId = null) { @@ -57,21 +66,21 @@ function ($resource) { public function getValidators() { return [ - GenerisRdf::PROPERTY_USER_LOGIN => [\tao_helpers_form_FormFactory::getValidator('Unique')], - GenerisRdf::PROPERTY_USER_UILG => [\tao_helpers_form_FormFactory::getValidator('NotEmpty')], + GenerisRdf::PROPERTY_USER_LOGIN => [tao_helpers_form_FormFactory::getValidator('Unique')], + GenerisRdf::PROPERTY_USER_UILG => [tao_helpers_form_FormFactory::getValidator('NotEmpty')], ]; } /** * @param core_kernel_classes_Resource $resource * @return array - * @throws \core_kernel_persistence_Exception - * @throws \common_ext_ExtensionException + * @throws core_kernel_persistence_Exception + * @throws common_ext_ExtensionException */ protected function getProperties($resource) { - /** @var \common_ext_ExtensionsManager $extManager */ - $extManager = ServiceManager::getServiceManager()->get(\common_ext_ExtensionsManager::SERVICE_ID); + /** @var common_ext_ExtensionsManager $extManager */ + $extManager = ServiceManager::getServiceManager()->get(common_ext_ExtensionsManager::SERVICE_ID); $taoTestTaker = $extManager->getExtensionById('taoTestTaker'); $config = $taoTestTaker->getConfig('csvImporterCallbacks'); @@ -79,7 +88,7 @@ protected function getProperties($resource) return [ 'hashForKey' => UserHashForEncryption::hash(TestTakerSavePasswordInMemory::getPassword()), GenerisRdf::PROPERTY_USER_PASSWORD => $resource->getOnePropertyValue( - new \core_kernel_classes_Property(GenerisRdf::PROPERTY_USER_PASSWORD) + new core_kernel_classes_Property(GenerisRdf::PROPERTY_USER_PASSWORD) )->literal ]; } @@ -110,7 +119,7 @@ protected function getExludedProperties() */ protected function getStaticData() { - $lang = \tao_helpers_I18n::getLangResourceByCode(DEFAULT_LANG)->getUri(); + $lang = tao_helpers_I18n::getLangResourceByCode(DEFAULT_LANG)->getUri(); return [ GenerisRdf::PROPERTY_USER_DEFLG => $lang, @@ -122,12 +131,12 @@ protected function getStaticData() /** * (non-PHPdoc) * @see tao_models_classes_import_CsvImporter::getAdditionAdapterOptions() - * @throws \common_ext_ExtensionException + * @throws common_ext_ExtensionException */ protected function getAdditionAdapterOptions() { - /** @var \common_ext_ExtensionsManager $extManager */ - $extManager = ServiceManager::getServiceManager()->get(\common_ext_ExtensionsManager::SERVICE_ID); + /** @var common_ext_ExtensionsManager $extManager */ + $extManager = ServiceManager::getServiceManager()->get(common_ext_ExtensionsManager::SERVICE_ID); $taoTestTaker = $extManager->getExtensionById('taoTestTaker'); $config = $taoTestTaker->getConfig('csvImporterCallbacks'); @@ -146,6 +155,10 @@ protected function getAdditionAdapterOptions() ]; } + $returnValue['onResourceImported'] = [ + $this->getResourceImportedCallback(), + ]; + return $returnValue; } @@ -157,7 +170,17 @@ protected function getAdditionAdapterOptions() */ public static function taoSubjectsPasswordEncode($value) { - return \core_kernel_users_Service::getPasswordHash()->encrypt($value); + return core_kernel_users_Service::getPasswordHash()->encrypt($value); + } + + private function getResourceImportedCallback(): callable + { + return function (core_kernel_classes_Resource $resource): void { + $resource->editPropertyValues( + new core_kernel_classes_Property(UserRdf::PROPERTY_DEFLG), + $resource->getOnePropertyValue(new core_kernel_classes_Property(UserRdf::PROPERTY_UILG))->getUri() + ); + }; } private function getTestTakerImportEventDispatcher(): TestTakerImportEventDispatcher