Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| RestTestTakers | |
0.00% |
0 / 46 |
|
0.00% |
0 / 9 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| index | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getParametersAliases | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| getParametersRequirements | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| put | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| post | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
30 | |||
| delete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getParameters | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | /** @noinspection UnNecessaryDoubleQuotesInspection */ |
| 4 | |
| 5 | /** |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; under version 2 |
| 9 | * of the License (non-upgradable). |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | * |
| 20 | * Copyright (c) 2019 (original work) Open Assessment Technologies SA; |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | namespace oat\taoTestTaker\actions; |
| 25 | |
| 26 | use Exception; |
| 27 | use common_exception_RestApi; |
| 28 | use tao_models_classes_UserService; |
| 29 | use common_exception_ValidationFailed; |
| 30 | use oat\generis\model\OntologyRdf; |
| 31 | use oat\generis\Helper\UserHashForEncryption; |
| 32 | use oat\generis\model\user\PasswordConstraintsException; |
| 33 | use oat\generis\model\user\UserRdf; |
| 34 | use oat\tao\model\routing\AnnotationReader\security; |
| 35 | use oat\tao\model\TaoOntology; |
| 36 | use oat\taoTestTaker\models\CrudService; |
| 37 | |
| 38 | /** |
| 39 | * @OA\Info(title="TAO Test Taker API", version="2.0") |
| 40 | * @OA\Post( |
| 41 | * path="/taoTestTaker/api/testTakers", |
| 42 | * summary="Create new test taker", |
| 43 | * @OA\RequestBody( |
| 44 | * @OA\MediaType( |
| 45 | * mediaType="application/x-www-form-urlencoded", |
| 46 | * @OA\Schema(ref="#/components/schemas/taoTestTaker.TestTaker.New") |
| 47 | * ) |
| 48 | * ), |
| 49 | * @OA\Response( |
| 50 | * response="200", |
| 51 | * description="Test taker created", |
| 52 | * @OA\MediaType( |
| 53 | * mediaType="application/json", |
| 54 | * @OA\Schema( |
| 55 | * type="object", |
| 56 | * @OA\Property( |
| 57 | * property="success", |
| 58 | * type="boolean", |
| 59 | * description="`false` on failure, `true` on success", |
| 60 | * ), |
| 61 | * @OA\Property( |
| 62 | * property="uri", |
| 63 | * type="string", |
| 64 | * description="Created test taker URI", |
| 65 | * ), |
| 66 | * example={ |
| 67 | * "success": true, |
| 68 | * "uri": "http://sample/first.rdf#i1536680377163171" |
| 69 | * } |
| 70 | * ), |
| 71 | * ), |
| 72 | * ), |
| 73 | * @OA\Response( |
| 74 | * response="400", |
| 75 | * description="Invalid request data", |
| 76 | * @OA\JsonContent(ref="#/components/schemas/tao.RestTrait.FailureResponse") |
| 77 | * ) |
| 78 | * ) |
| 79 | * |
| 80 | */ |
| 81 | class RestTestTakers extends \tao_actions_CommonRestModule |
| 82 | { |
| 83 | /** @var array */ |
| 84 | protected $parameters; |
| 85 | |
| 86 | /** |
| 87 | * @OA\Schema( |
| 88 | * schema="taoTestTaker.TestTaker.New", |
| 89 | * type="object", |
| 90 | * allOf={ |
| 91 | * @OA\Schema(ref="#/components/schemas/tao.GenerisClass.Search"), |
| 92 | * @OA\Schema(ref="#/components/schemas/taoTestTaker.TestTaker.Update") |
| 93 | * }, |
| 94 | * @OA\Property( |
| 95 | * property="login", |
| 96 | * type="string", |
| 97 | * description="Login" |
| 98 | * ), |
| 99 | * required={"login", "password", "userLanguage"} |
| 100 | * ) |
| 101 | * @OA\Schema( |
| 102 | * schema="taoTestTaker.TestTaker.Update", |
| 103 | * type="object", |
| 104 | * @OA\Property( |
| 105 | * property="label", |
| 106 | * type="string", |
| 107 | * description="Label" |
| 108 | * ), |
| 109 | * @OA\Property( |
| 110 | * property="login", |
| 111 | * type="string", |
| 112 | * description="Login" |
| 113 | * ), |
| 114 | * @OA\Property( |
| 115 | * property="password", |
| 116 | * type="string", |
| 117 | * description="Password" |
| 118 | * ), |
| 119 | * @OA\Property( |
| 120 | * property="userLanguage", |
| 121 | * type="string", |
| 122 | * description="Interface language (uri or language code, 'fr-FR' |
| 123 | * or 'http://www.tao.lu/Ontologies/TAO.rdf#Langfr-FR' for example)" |
| 124 | * ), |
| 125 | * @OA\Property( |
| 126 | * property="defaultLanguage", |
| 127 | * type="string", |
| 128 | * description="Default language (uri or language code, 'fr-FR' |
| 129 | * or 'http://www.tao.lu/Ontologies/TAO.rdf#Langfr-FR' for example)" |
| 130 | * ), |
| 131 | * @OA\Property( |
| 132 | * property="firstName", |
| 133 | * type="string", |
| 134 | * description="First name" |
| 135 | * ), |
| 136 | * @OA\Property( |
| 137 | * property="lastName", |
| 138 | * type="string", |
| 139 | * description="Last name" |
| 140 | * ), |
| 141 | * @OA\Property( |
| 142 | * property="mail", |
| 143 | * type="string", |
| 144 | * description="Email" |
| 145 | * ) |
| 146 | * ) |
| 147 | */ |
| 148 | |
| 149 | public const ROOT_CLASS = TaoOntology::CLASS_URI_SUBJECT; |
| 150 | |
| 151 | /** |
| 152 | * RestTestTakers constructor. |
| 153 | * @security("hide"); |
| 154 | */ |
| 155 | public function __construct() |
| 156 | { |
| 157 | parent::__construct(); |
| 158 | $this->service = CrudService::singleton(); |
| 159 | } |
| 160 | |
| 161 | public function index() |
| 162 | { |
| 163 | $this->returnFailure(new common_exception_RestApi('Not implemented.')); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Optionally a specific rest controller may declare |
| 168 | * aliases for parameters used for the rest communication |
| 169 | */ |
| 170 | protected function getParametersAliases() |
| 171 | { |
| 172 | return array_merge(parent::getParametersAliases(), [ |
| 173 | 'login' => UserRdf::PROPERTY_LOGIN, |
| 174 | 'password' => UserRdf::PROPERTY_PASSWORD, |
| 175 | 'userLanguage' => UserRdf::PROPERTY_UILG, |
| 176 | 'defaultLanguage' => UserRdf::PROPERTY_DEFLG, |
| 177 | 'firstName' => UserRdf::PROPERTY_FIRSTNAME, |
| 178 | 'lastName' => UserRdf::PROPERTY_LASTNAME, |
| 179 | 'mail' => UserRdf::PROPERTY_MAIL |
| 180 | ]); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Optional Requirements for parameters to be sent on every service |
| 185 | */ |
| 186 | protected function getParametersRequirements() |
| 187 | { |
| 188 | return [ |
| 189 | 'post' => ["login", "password", "userLanguage"] |
| 190 | ]; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * @param null $uri |
| 195 | * @return mixed |
| 196 | * @throws \common_exception_NotImplemented |
| 197 | */ |
| 198 | public function get($uri = null) |
| 199 | { |
| 200 | $this->returnFailure(new common_exception_RestApi('Not implemented.')); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * @param string $uri |
| 205 | * @return mixed |
| 206 | * @throws \common_exception_NotImplemented |
| 207 | */ |
| 208 | public function put($uri) |
| 209 | { |
| 210 | $this->returnFailure(new common_exception_RestApi('Not implemented.')); |
| 211 | } |
| 212 | |
| 213 | public function post() |
| 214 | { |
| 215 | try { |
| 216 | /** @var \core_kernel_classes_Resource $testTakerResource */ |
| 217 | $testTakerResource = parent::post(); |
| 218 | $parameters = $this->getParameters(); |
| 219 | $hashForKey = array_key_exists(UserRdf::PROPERTY_PASSWORD, $parameters) |
| 220 | ? UserHashForEncryption::hash($parameters[UserRdf::PROPERTY_PASSWORD]) |
| 221 | : null; |
| 222 | |
| 223 | /** @var tao_models_classes_UserService $userService */ |
| 224 | $userService = $this->getServiceLocator()->get(tao_models_classes_UserService::SERVICE_ID); |
| 225 | $userService->triggerUpdatedEvent( |
| 226 | $testTakerResource, |
| 227 | [UserRdf::PROPERTY_PASSWORD => $testTakerResource->getProperty(UserRdf::PROPERTY_PASSWORD)], |
| 228 | $hashForKey |
| 229 | ); |
| 230 | |
| 231 | $this->returnSuccess([ |
| 232 | 'success' => true, |
| 233 | 'uri' => $testTakerResource->getUri(), |
| 234 | ], false); |
| 235 | } catch (PasswordConstraintsException $e) { |
| 236 | $this->returnFailure(new common_exception_RestApi($e->getMessage())); |
| 237 | } catch (common_exception_ValidationFailed $e) { |
| 238 | $alias = $this->reverseSearchAlias($e->getField()); |
| 239 | $this->returnFailure(new common_exception_ValidationFailed($alias, null, $e->getCode())); |
| 240 | } catch (Exception $e) { |
| 241 | $this->returnFailure($e); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * @param string $uri |
| 247 | * @return mixed |
| 248 | * @throws \common_exception_NotImplemented |
| 249 | */ |
| 250 | public function delete($uri = null) |
| 251 | { |
| 252 | $this->returnFailure(new common_exception_RestApi('Not implemented.')); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * @throws common_exception_RestApi |
| 257 | * |
| 258 | * @return array |
| 259 | */ |
| 260 | protected function getParameters() |
| 261 | { |
| 262 | if (!$this->parameters) { |
| 263 | $this->parameters = parent::getParameters(); |
| 264 | } |
| 265 | |
| 266 | if ( |
| 267 | $this->getRequestMethod() === 'POST' && |
| 268 | $classResource = $this->getClassFromRequest($this->getClass(self::ROOT_CLASS)) |
| 269 | ) { |
| 270 | $this->parameters[OntologyRdf::RDF_TYPE] = $classResource->getUri(); |
| 271 | } |
| 272 | |
| 273 | return $this->parameters; |
| 274 | } |
| 275 | } |