Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| CrudService | |
0.00% |
0 / 43 |
|
0.00% |
0 / 5 |
240 | |
0.00% |
0 / 1 |
| getClassService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| delete | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| createFromArray | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
42 | |||
| update | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| readLangProperty | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| 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-2014 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoTestTaker\models; |
| 23 | |
| 24 | use oat\generis\model\GenerisRdf; |
| 25 | use oat\generis\model\OntologyRdf; |
| 26 | use oat\generis\model\OntologyRdfs; |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * Crud services implements basic CRUD services, orginally intended for |
| 31 | * REST controllers/ HTTP exception handlers. |
| 32 | * Consequently the signatures and behaviors is closer to REST and throwing |
| 33 | * HTTP like exceptions. |
| 34 | * |
| 35 | * @author Patrick Plichart, patrick@taotesting.com |
| 36 | * |
| 37 | */ |
| 38 | class CrudService extends \tao_models_classes_CrudService |
| 39 | { |
| 40 | /** |
| 41 | * (non-PHPdoc) |
| 42 | * @see tao_models_classes_CrudService::getClassService() |
| 43 | */ |
| 44 | protected function getClassService() |
| 45 | { |
| 46 | return TestTakerService::singleton(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * (non-PHPdoc) |
| 51 | * @see tao_models_classes_CrudService::delete() |
| 52 | */ |
| 53 | public function delete($resource) |
| 54 | { |
| 55 | $this->getClassService()->deleteSubject(new \core_kernel_classes_Resource($resource)); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * |
| 61 | * @author Patrick Plichart, patrick@taotesting.com |
| 62 | * @param array $propertiesValues |
| 63 | * @return \core_kernel_classes_Resource |
| 64 | * @throws \common_exception_Error |
| 65 | * @throws \common_exception_MissingParameter |
| 66 | * @throws \common_exception_PreConditionFailure |
| 67 | * @throws \common_exception_ValidationFailed |
| 68 | * @throws \oat\generis\model\user\PasswordConstraintsException |
| 69 | */ |
| 70 | public function createFromArray($propertiesValues = []) |
| 71 | { |
| 72 | |
| 73 | // mandatory parameters |
| 74 | if (! isset($propertiesValues[GenerisRdf::PROPERTY_USER_LOGIN])) { |
| 75 | throw new \common_exception_MissingParameter("login"); |
| 76 | } |
| 77 | if (! isset($propertiesValues[GenerisRdf::PROPERTY_USER_PASSWORD])) { |
| 78 | throw new \common_exception_MissingParameter("password"); |
| 79 | } |
| 80 | |
| 81 | // validate parameters |
| 82 | if (empty($propertiesValues[GenerisRdf::PROPERTY_USER_LOGIN])) { |
| 83 | throw new \common_exception_ValidationFailed(GenerisRdf::PROPERTY_USER_LOGIN); |
| 84 | } |
| 85 | |
| 86 | // default values and validation |
| 87 | $propertiesValues[GenerisRdf::PROPERTY_USER_UILG] = |
| 88 | self::readLangProperty($propertiesValues, GenerisRdf::PROPERTY_USER_UILG); |
| 89 | |
| 90 | $propertiesValues[GenerisRdf::PROPERTY_USER_DEFLG] = |
| 91 | self::readLangProperty($propertiesValues, GenerisRdf::PROPERTY_USER_DEFLG); |
| 92 | |
| 93 | if (! isset($propertiesValues[OntologyRdfs::RDFS_LABEL])) { |
| 94 | $propertiesValues[OntologyRdfs::RDFS_LABEL] = ""; |
| 95 | } |
| 96 | // check if login already exists |
| 97 | $userService = \tao_models_classes_UserService::singleton(); |
| 98 | if ($userService->loginExists($propertiesValues[GenerisRdf::PROPERTY_USER_LOGIN])) { |
| 99 | throw new \common_exception_PreConditionFailure("login already exists"); |
| 100 | } |
| 101 | $propertiesValues[GenerisRdf::PROPERTY_USER_PASSWORD] = \core_kernel_users_Service::getPasswordHash() |
| 102 | ->encrypt($propertiesValues[GenerisRdf::PROPERTY_USER_PASSWORD]); |
| 103 | $type = $propertiesValues[OntologyRdf::RDF_TYPE] ?? $this->getRootClass(); |
| 104 | $label = $propertiesValues[OntologyRdfs::RDFS_LABEL]; |
| 105 | // hmmm |
| 106 | unset($propertiesValues[OntologyRdfs::RDFS_LABEL]); |
| 107 | unset($propertiesValues[OntologyRdf::RDF_TYPE]); |
| 108 | |
| 109 | $resource = parent::create($label, $type, $propertiesValues); |
| 110 | |
| 111 | $this->getClassService()->setTestTakerRole($resource); |
| 112 | |
| 113 | return $resource; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * (non-PHPdoc) |
| 118 | * @see tao_models_classes_CrudService::update() |
| 119 | */ |
| 120 | public function update($uri = null, $propertiesValues = []) |
| 121 | { |
| 122 | if (is_null($uri)) { |
| 123 | throw new \common_exception_MissingParameter("uri"); |
| 124 | } |
| 125 | if (isset($propertiesValues[GenerisRdf::PROPERTY_USER_LOGIN])) { |
| 126 | throw new \common_exception_PreConditionFailure("login update not allowed"); |
| 127 | } |
| 128 | if (isset($propertiesValues[GenerisRdf::PROPERTY_USER_PASSWORD])) { |
| 129 | $propertiesValues[GenerisRdf::PROPERTY_USER_PASSWORD] = \core_kernel_users_Service::getPasswordHash() |
| 130 | ->encrypt($propertiesValues[GenerisRdf::PROPERTY_USER_PASSWORD]); |
| 131 | } |
| 132 | parent::update($uri, $propertiesValues); |
| 133 | // throw new common_exception_NotImplemented(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param array $properties |
| 138 | * @param string $propKey |
| 139 | * @return string |
| 140 | * @throws \common_exception_Error |
| 141 | * @throws \common_exception_ValidationFailed |
| 142 | */ |
| 143 | protected static function readLangProperty(array &$properties, $propKey) |
| 144 | { |
| 145 | if (!isset($properties[$propKey])) { |
| 146 | return \tao_helpers_I18n::getLangResourceByCode(DEFAULT_LANG)->getUri(); |
| 147 | } |
| 148 | |
| 149 | $existingUri = \tao_models_classes_LanguageService::getExistingLanguageUri($properties[$propKey]); |
| 150 | if ($existingUri === null) { |
| 151 | throw new \common_exception_ValidationFailed($propKey); |
| 152 | } |
| 153 | |
| 154 | return $existingUri; |
| 155 | } |
| 156 | } |