Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 95 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
| TestTaker | |
0.00% |
0 / 95 |
|
0.00% |
0 / 11 |
552 | |
0.00% |
0 / 1 |
| getEventManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getOntologyData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getClassService | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getSearchForm | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| editSubject | |
0.00% |
0 / 80 |
|
0.00% |
0 / 1 |
156 | |||
| moveAll | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| cloneInstance | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| moveInstance | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| moveClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDependsOnPropertyValidator | |
0.00% |
0 / 1 |
|
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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2002-2008 (update and modification) Public Research Centre Henri Tudor & University of Luxembourg |
| 21 | * (under the project TAO & TAO2); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * 2013-2014 (update and modification) Open Assessment Technologies SA |
| 25 | */ |
| 26 | |
| 27 | namespace oat\taoTestTaker\actions; |
| 28 | |
| 29 | use common_ext_ExtensionException; |
| 30 | use common_ext_ExtensionsManager; |
| 31 | use core_kernel_classes_Class; |
| 32 | use core_kernel_classes_Property; |
| 33 | use core_kernel_classes_Resource; |
| 34 | use core_kernel_users_Service; |
| 35 | use oat\oatbox\validator\ValidatorInterface; |
| 36 | use oat\generis\Helper\UserHashForEncryption; |
| 37 | use oat\generis\model\GenerisRdf; |
| 38 | use oat\generis\model\OntologyAwareTrait; |
| 39 | use oat\oatbox\event\EventManager; |
| 40 | use oat\tao\model\resources\ResourceWatcher; |
| 41 | use oat\tao\model\routing\AnnotationReader\security; |
| 42 | use oat\taoDelivery\model\AssignmentService; |
| 43 | use oat\taoDeliveryRdf\model\GroupAssignment; |
| 44 | use oat\taoTestTaker\actions\form\Search; |
| 45 | use oat\taoTestTaker\actions\form\TestTaker as TestTakerForm; |
| 46 | use oat\taoGroups\helpers\TestTakerForm as GroupForm; |
| 47 | use oat\taoTestTaker\models\events\TestTakerUpdatedEvent; |
| 48 | use oat\taoTestTaker\models\TestTakerService; |
| 49 | use tao_actions_SaSModule; |
| 50 | use tao_helpers_Uri; |
| 51 | use tao_models_classes_UserService; |
| 52 | use tao_helpers_form_FormContainer as FormContainer; |
| 53 | use oat\tao\model\Lists\Business\Validation\DependsOnPropertyValidator; |
| 54 | |
| 55 | /** |
| 56 | * Subjects Controller provide actions performed from url resolution |
| 57 | * |
| 58 | * @author Bertrand Chevrier, <taosupport@tudor.lu> |
| 59 | * @package taoTestTaker |
| 60 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
| 61 | */ |
| 62 | class TestTaker extends tao_actions_SaSModule |
| 63 | { |
| 64 | use OntologyAwareTrait; |
| 65 | |
| 66 | /** |
| 67 | * @return EventManager |
| 68 | */ |
| 69 | protected function getEventManager() |
| 70 | { |
| 71 | return $this->getServiceLocator()->get(EventManager::SERVICE_ID); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @throws common_ext_ExtensionException |
| 76 | * @security("hide") |
| 77 | */ |
| 78 | public function __construct() |
| 79 | { |
| 80 | parent::__construct(); |
| 81 | |
| 82 | $this->defaultData(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * overwrite the parent getOntologyData to add the requiresRight only in TestTakers |
| 87 | * @see tao_actions_TaoModule::getOntologyData() |
| 88 | * @requiresRight classUri READ |
| 89 | */ |
| 90 | public function getOntologyData() |
| 91 | { |
| 92 | return parent::getOntologyData(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * (non-PHPdoc) |
| 97 | * @see tao_actions_RdfController::getClassService() |
| 98 | * @return TestTakerService |
| 99 | */ |
| 100 | protected function getClassService() |
| 101 | { |
| 102 | if (is_null($this->service)) { |
| 103 | $this->service = TestTakerService::singleton(); |
| 104 | } |
| 105 | |
| 106 | return $this->service; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * |
| 111 | * @author Lionel Lecaque, lionel@taotesting.com |
| 112 | * @param core_kernel_classes_Class $clazz |
| 113 | * @return tao_actions_form_Search |
| 114 | */ |
| 115 | protected function getSearchForm($clazz) |
| 116 | { |
| 117 | return new Search($clazz, null, [ |
| 118 | 'recursive' => true |
| 119 | ]); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * edit an subject instance |
| 124 | * |
| 125 | * @requiresRight id READ |
| 126 | * |
| 127 | * @throws \InterruptedActionException |
| 128 | * @throws \common_exception_Error |
| 129 | * @throws \core_kernel_persistence_Exception |
| 130 | * @throws \oat\generis\model\user\PasswordConstraintsException |
| 131 | * @throws \oat\tao\model\security\SecurityException |
| 132 | * @throws \tao_models_classes_MissingRequestParameterException |
| 133 | * @throws \tao_models_classes_dataBinding_GenerisFormDataBindingException |
| 134 | */ |
| 135 | public function editSubject() |
| 136 | { |
| 137 | $clazz = $this->getCurrentClass(); |
| 138 | |
| 139 | // get the subject to edit |
| 140 | $subject = $this->getCurrentInstance(); |
| 141 | |
| 142 | $addMode = false; |
| 143 | $login = (string) $subject->getOnePropertyValue( |
| 144 | new core_kernel_classes_Property(GenerisRdf::PROPERTY_USER_LOGIN) |
| 145 | ); |
| 146 | if (empty($login)) { |
| 147 | $addMode = true; |
| 148 | $this->setData('loginUri', tao_helpers_Uri::encode(GenerisRdf::PROPERTY_USER_LOGIN)); |
| 149 | } |
| 150 | |
| 151 | if ($this->hasRequestParameter('reload')) { |
| 152 | $this->setData('reload', true); |
| 153 | } |
| 154 | |
| 155 | $myFormContainer = new TestTakerForm( |
| 156 | $clazz, |
| 157 | $subject, |
| 158 | $addMode, |
| 159 | [ |
| 160 | FormContainer::CSRF_PROTECTION_OPTION => true, |
| 161 | FormContainer::ATTRIBUTE_VALIDATORS => [ |
| 162 | 'data-depends-on-property' => [ |
| 163 | $this->getDependsOnPropertyValidator(), |
| 164 | ], |
| 165 | ], |
| 166 | ] |
| 167 | ); |
| 168 | $myForm = $myFormContainer->getForm(); |
| 169 | |
| 170 | $subjectUri = $subject->getUri(); |
| 171 | |
| 172 | if ($myForm->isSubmited() && $myForm->isValid()) { |
| 173 | $this->validateInstanceRoot($subjectUri); |
| 174 | $this->setData('reload', false); |
| 175 | |
| 176 | $values = $myForm->getValues(); |
| 177 | |
| 178 | if ($addMode) { |
| 179 | $plainPassword = $values['password1']; |
| 180 | $values[GenerisRdf::PROPERTY_USER_PASSWORD] = |
| 181 | core_kernel_users_Service::getPasswordHash()->encrypt($values['password1']); |
| 182 | unset($values['password1'], $values['password2']); |
| 183 | } else { |
| 184 | if (! empty($values['password2'])) { |
| 185 | $plainPassword = $values['password2']; |
| 186 | $values[GenerisRdf::PROPERTY_USER_PASSWORD] = |
| 187 | core_kernel_users_Service::getPasswordHash()->encrypt($values['password2']); |
| 188 | } |
| 189 | unset($values['password2'], $values['password3']); |
| 190 | } |
| 191 | |
| 192 | $binder = new \tao_models_classes_dataBinding_GenerisFormDataBinder($subject); |
| 193 | $subject = $binder->bind($values); |
| 194 | |
| 195 | $data = []; |
| 196 | if (isset($plainPassword)) { |
| 197 | $data = ['hashForKey' => UserHashForEncryption::hash($plainPassword)]; |
| 198 | } |
| 199 | |
| 200 | if ($addMode) { |
| 201 | // force default subject roles to be the Delivery Role: |
| 202 | $this->getClassService()->setTestTakerRole($subject); |
| 203 | } |
| 204 | |
| 205 | // force the data language to be the same as the gui language |
| 206 | $userService = tao_models_classes_UserService::singleton(); |
| 207 | $lang = new core_kernel_classes_Resource($values[GenerisRdf::PROPERTY_USER_UILG]); |
| 208 | $userService->bindProperties($subject, [ |
| 209 | GenerisRdf::PROPERTY_USER_DEFLG => $lang->getUri() |
| 210 | ]); |
| 211 | |
| 212 | $this->getEventManager()->trigger( |
| 213 | new TestTakerUpdatedEvent($subjectUri, array_merge($values, $data)) |
| 214 | ); |
| 215 | |
| 216 | $message = __('Test taker saved'); |
| 217 | |
| 218 | if ($addMode) { |
| 219 | $params = [ |
| 220 | 'id' => $subjectUri, |
| 221 | 'uri' => tao_helpers_Uri::encode($subjectUri), |
| 222 | 'classUri' => tao_helpers_Uri::encode($clazz->getUri()), |
| 223 | 'reload' => true, |
| 224 | 'message' => $message |
| 225 | ]; |
| 226 | $this->redirect(_url('editSubject', null, null, $params)); |
| 227 | } |
| 228 | |
| 229 | $this->setData('selectNode', tao_helpers_Uri::encode($subjectUri)); |
| 230 | $this->setData('message', $message); |
| 231 | $this->setData('reload', true); |
| 232 | } |
| 233 | |
| 234 | $assignmentService = $this->getServiceLocator()->get(AssignmentService::SERVICE_ID); |
| 235 | if ( |
| 236 | common_ext_ExtensionsManager::singleton()->isEnabled('taoGroups') && |
| 237 | get_class($assignmentService) == GroupAssignment::class |
| 238 | ) { |
| 239 | $groupForm = GroupForm::returnGroupTreeFormObject($subject); |
| 240 | |
| 241 | $groupForm->setData('saveUrl', _url('setValues', 'TestTakerGenerisTree', 'taoTestTaker')); |
| 242 | |
| 243 | $this->setData('groupForm', $groupForm->render()); |
| 244 | } |
| 245 | $updatedAt = $this->getServiceManager()->get(ResourceWatcher::SERVICE_ID)->getUpdatedAt($subject); |
| 246 | $this->setData('updatedAt', $updatedAt); |
| 247 | $this->setData('checkLogin', $addMode); |
| 248 | $this->setData('formTitle', __('Edit subject')); |
| 249 | $this->setData('myForm', $myForm->render()); |
| 250 | $this->setView('form_subjects.tpl'); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * overwrite the parent moveAllInstances to add the requiresRight only in TestTakers |
| 255 | * @see tao_actions_TaoModule::moveAll() |
| 256 | * @requiresRight ids WRITE |
| 257 | */ |
| 258 | public function moveAll() |
| 259 | { |
| 260 | return parent::moveAll(); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * overwrite the parent cloneInstance to add the requiresRight only in TestTakers |
| 265 | * @see tao_actions_TaoModule::cloneInstance() |
| 266 | * @requiresRight uri READ |
| 267 | * @requiresRight classUri WRITE |
| 268 | */ |
| 269 | public function cloneInstance() |
| 270 | { |
| 271 | return parent::cloneInstance(); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * overwrite the parent moveInstance to add the requiresRight only in TestTakers |
| 276 | * @see tao_actions_TaoModule::moveInstance() |
| 277 | * @requiresRight uri WRITE |
| 278 | * @requiresRight destinationClassUri WRITE |
| 279 | */ |
| 280 | public function moveInstance() |
| 281 | { |
| 282 | return parent::moveInstance(); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Move class to another location |
| 287 | * @requiresRight classUri WRITE |
| 288 | */ |
| 289 | public function moveClass() |
| 290 | { |
| 291 | return parent::moveResource(); |
| 292 | } |
| 293 | |
| 294 | private function getDependsOnPropertyValidator(): ValidatorInterface |
| 295 | { |
| 296 | return $this->getPsrContainer()->get(DependsOnPropertyValidator::class); |
| 297 | } |
| 298 | } |