Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
CsvImporter
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 9
132
0.00% covered (danger)
0.00%
0 / 1
 import
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getValidators
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getProperties
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 getExludedProperties
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getStaticData
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getAdditionAdapterOptions
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
6
 taoSubjectsPasswordEncode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getResourceImportedCallback
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getTestTakerImportEventDispatcher
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2013-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoTestTaker\models;
24
25use common_ext_ExtensionException;
26use common_ext_ExtensionsManager;
27use core_kernel_classes_Property;
28use core_kernel_classes_Resource;
29use core_kernel_persistence_Exception;
30use core_kernel_users_Service;
31use oat\generis\Helper\UserHashForEncryption;
32use oat\generis\model\user\UserRdf;
33use oat\oatbox\service\ServiceManager;
34use oat\tao\model\TaoOntology;
35use oat\generis\model\GenerisRdf;
36use oat\taoTestTaker\models\events\dispatcher\TestTakerImportEventDispatcher;
37use tao_helpers_form_FormFactory;
38use tao_helpers_I18n;
39use tao_models_classes_import_CsvImporter;
40
41/**
42 * A custom subject CSV importer
43 *
44 * @access public
45 * @author Joel Bout, <joel@taotesting.com>
46 * @package taoSubjects
47
48 */
49class CsvImporter extends tao_models_classes_import_CsvImporter
50{
51    public function import($class, $form, $userId = null)
52    {
53        $report = parent::import($class, $form);
54
55        $this->getTestTakerImportEventDispatcher()
56            ->dispatch(
57                $report,
58                function ($resource) {
59                    return $this->getProperties($resource);
60                }
61            );
62
63        return $report;
64    }
65
66    public function getValidators()
67    {
68        return [
69            GenerisRdf::PROPERTY_USER_LOGIN => [tao_helpers_form_FormFactory::getValidator('Unique')],
70            GenerisRdf::PROPERTY_USER_UILG => [tao_helpers_form_FormFactory::getValidator('NotEmpty')],
71        ];
72    }
73
74    /**
75     * @param core_kernel_classes_Resource $resource
76     * @return array
77     * @throws core_kernel_persistence_Exception
78     * @throws common_ext_ExtensionException
79     */
80    protected function getProperties($resource)
81    {
82        /** @var common_ext_ExtensionsManager $extManager */
83        $extManager =  ServiceManager::getServiceManager()->get(common_ext_ExtensionsManager::SERVICE_ID);
84        $taoTestTaker = $extManager->getExtensionById('taoTestTaker');
85        $config = $taoTestTaker->getConfig('csvImporterCallbacks');
86
87        if ((bool)$config['use_properties_for_event']) {
88            return [
89                'hashForKey' => UserHashForEncryption::hash(TestTakerSavePasswordInMemory::getPassword()),
90                GenerisRdf::PROPERTY_USER_PASSWORD => $resource->getOnePropertyValue(
91                    new core_kernel_classes_Property(GenerisRdf::PROPERTY_USER_PASSWORD)
92                )->literal
93            ];
94        }
95
96
97
98        return [];
99    }
100
101    /**
102     * (non-PHPdoc)
103     * @see tao_models_classes_import_CsvImporter::getExludedProperties()
104     */
105    protected function getExludedProperties()
106    {
107        return array_merge(parent::getExludedProperties(), [
108            GenerisRdf::PROPERTY_USER_DEFLG,
109            GenerisRdf::PROPERTY_USER_ROLES,
110            TaoOntology::PROPERTY_USER_LAST_EXTENSION,
111            TaoOntology::PROPERTY_USER_FIRST_TIME,
112            GenerisRdf::PROPERTY_USER_TIMEZONE
113        ]);
114    }
115
116    /**
117     * (non-PHPdoc)
118     * @see tao_models_classes_import_CsvImporter::getStaticData()
119     */
120    protected function getStaticData()
121    {
122        $lang = tao_helpers_I18n::getLangResourceByCode(DEFAULT_LANG)->getUri();
123
124        return [
125            GenerisRdf::PROPERTY_USER_DEFLG => $lang,
126            GenerisRdf::PROPERTY_USER_TIMEZONE => TIME_ZONE,
127            GenerisRdf::PROPERTY_USER_ROLES => TaoOntology::PROPERTY_INSTANCE_ROLE_DELIVERY,
128        ];
129    }
130
131    /**
132     * (non-PHPdoc)
133     * @see tao_models_classes_import_CsvImporter::getAdditionAdapterOptions()
134     * @throws common_ext_ExtensionException
135     */
136    protected function getAdditionAdapterOptions()
137    {
138        /** @var common_ext_ExtensionsManager $extManager */
139        $extManager = ServiceManager::getServiceManager()->get(common_ext_ExtensionsManager::SERVICE_ID);
140        $taoTestTaker = $extManager->getExtensionById('taoTestTaker');
141        $config = $taoTestTaker->getConfig('csvImporterCallbacks');
142
143        if (empty($config['callbacks'])) {
144            $returnValue = [
145                'callbacks' => [
146                    '*' => ['trim'],
147                    GenerisRdf::PROPERTY_USER_PASSWORD => [
148                        'oat\taoTestTaker\models\CsvImporter::taoSubjectsPasswordEncode',
149                    ],
150                ],
151            ];
152        } else {
153            $returnValue = [
154                'callbacks' => $config['callbacks']
155            ];
156        }
157
158        $returnValue['onResourceImported'] = [
159            $this->getResourceImportedCallback(),
160        ];
161
162        return $returnValue;
163    }
164
165    /**
166     * Wrapper for password hash
167     *
168     * @param  string $value
169     * @return string
170     */
171    public static function taoSubjectsPasswordEncode($value)
172    {
173        return core_kernel_users_Service::getPasswordHash()->encrypt($value);
174    }
175
176    private function getResourceImportedCallback(): callable
177    {
178        return function (core_kernel_classes_Resource $resource): void {
179            $resource->editPropertyValues(
180                new core_kernel_classes_Property(UserRdf::PROPERTY_DEFLG),
181                $resource->getOnePropertyValue(new core_kernel_classes_Property(UserRdf::PROPERTY_UILG))->getUri()
182            );
183        };
184    }
185
186    private function getTestTakerImportEventDispatcher(): TestTakerImportEventDispatcher
187    {
188        return $this->getServiceLocator()->get(TestTakerImportEventDispatcher::class);
189    }
190}