Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TestTakerGenerisTree | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
| setValues | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| setReverseValues | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
56 | |||
| getEventManager | |
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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoTestTaker\actions; |
| 24 | |
| 25 | use oat\oatbox\event\EventManager; |
| 26 | use oat\taoTestTaker\models\events\TestTakerUpdatedEvent; |
| 27 | use tao_actions_GenerisTree; |
| 28 | use tao_helpers_form_GenerisTreeForm; |
| 29 | |
| 30 | /** |
| 31 | * Class TestTakerGenerisTree |
| 32 | * @author Ilya Yarkavets <ilya.yarkavets@1pt.com> |
| 33 | * @package oat\taoTestTaker\actions |
| 34 | */ |
| 35 | class TestTakerGenerisTree extends tao_actions_GenerisTree |
| 36 | { |
| 37 | /** |
| 38 | * @see tao_actions_GenerisTree::setValues() |
| 39 | */ |
| 40 | public function setValues() |
| 41 | { |
| 42 | $parentResult = parent::setValues(); |
| 43 | |
| 44 | $this->getEventManager()->trigger(new TestTakerUpdatedEvent($this->getRequestParameter('resourceUri'), [])); |
| 45 | |
| 46 | return $parentResult; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @see tao_actions_GenerisTree::setReverseValues() |
| 51 | */ |
| 52 | public function setReverseValues() |
| 53 | { |
| 54 | if (!$this->isXmlHttpRequest()) { |
| 55 | throw new common_exception_IsAjaxAction(__FUNCTION__); |
| 56 | } |
| 57 | |
| 58 | $values = tao_helpers_form_GenerisTreeForm::getSelectedInstancesFromPost(); |
| 59 | |
| 60 | $resource = $this->getResource($this->getRequestParameter('resourceUri')); |
| 61 | $property = $this->getProperty($this->getRequestParameter('propertyUri')); |
| 62 | |
| 63 | $ttClass = $this->getClass('http://www.tao.lu/Ontologies/TAOSubject.rdf#Subject'); |
| 64 | $instances = $ttClass->searchInstances([ |
| 65 | $property->getUri() => $resource |
| 66 | ], ['recursive' => true, 'like' => false]); |
| 67 | |
| 68 | $currentValues = array_merge([], array_keys($instances)); |
| 69 | |
| 70 | $toAdd = array_diff($values, $currentValues); |
| 71 | $toRemove = array_diff($currentValues, $values); |
| 72 | |
| 73 | $success = true; |
| 74 | foreach ($toAdd as $uri) { |
| 75 | $subject = $this->getResource($uri); |
| 76 | $success = $success && $subject->setPropertyValue($property, $resource); |
| 77 | } |
| 78 | |
| 79 | foreach ($toRemove as $uri) { |
| 80 | $subject = $this->getResource($uri); |
| 81 | $success = $success && $subject->removePropertyValue($property, $resource); |
| 82 | } |
| 83 | |
| 84 | $touchedValues = array_merge_recursive($toRemove, $values); |
| 85 | foreach ($touchedValues as $uri) { |
| 86 | $this->getEventManager()->trigger(new TestTakerUpdatedEvent($uri, [])); |
| 87 | } |
| 88 | |
| 89 | return $this->returnJson(['saved' => $success]); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @return EventManager |
| 94 | */ |
| 95 | private function getEventManager(): EventManager |
| 96 | { |
| 97 | return $this->getServiceLocator()->get(EventManager::SERVICE_ID); |
| 98 | } |
| 99 | } |