Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_GenerisTree | |
0.00% |
0 / 31 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
| getData | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| setValues | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| setReverseValues | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
56 | |||
| 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 Genera 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 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * 2013-2018 (update and modification) Open Assessment Technologies SA; |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * GenerisTree is a generic implementation of the |
| 29 | * javascript generisTree getter and setter actions |
| 30 | * |
| 31 | * @author Joel bout, <joel@taotesting.com> |
| 32 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
| 33 | * @package tao |
| 34 | */ |
| 35 | use oat\tao\model\Tree\GetTreeRequest; |
| 36 | use oat\tao\model\Tree\GetTreeService; |
| 37 | use oat\generis\model\OntologyAwareTrait; |
| 38 | |
| 39 | class tao_actions_GenerisTree extends tao_actions_CommonModule |
| 40 | { |
| 41 | use OntologyAwareTrait; |
| 42 | |
| 43 | public const DEFAULT_LIMIT = 10; |
| 44 | |
| 45 | public function getData() |
| 46 | { |
| 47 | /** @var GetTreeService $service */ |
| 48 | $service = $this->getServiceLocator()->get(GetTreeService::SERVICE_ID); |
| 49 | |
| 50 | $response = $service->handle(GetTreeRequest::create($this->getRequest())); |
| 51 | |
| 52 | return $this->returnJson($response->getTreeArray()); |
| 53 | } |
| 54 | |
| 55 | public function setValues() |
| 56 | { |
| 57 | if (!$this->isXmlHttpRequest()) { |
| 58 | throw new common_exception_IsAjaxAction(__FUNCTION__); |
| 59 | } |
| 60 | |
| 61 | $values = tao_helpers_form_GenerisTreeForm::getSelectedInstancesFromPost(); |
| 62 | |
| 63 | $resource = $this->getResource($this->getRequestParameter('resourceUri')); |
| 64 | $property = $this->getProperty($this->getRequestParameter('propertyUri')); |
| 65 | $success = $resource->editPropertyValues($property, $values); |
| 66 | |
| 67 | return $this->returnJson(['saved' => $success ]); |
| 68 | } |
| 69 | |
| 70 | public function setReverseValues() |
| 71 | { |
| 72 | if (!$this->isXmlHttpRequest()) { |
| 73 | throw new common_exception_IsAjaxAction(__FUNCTION__); |
| 74 | } |
| 75 | |
| 76 | $values = tao_helpers_form_GenerisTreeForm::getSelectedInstancesFromPost(); |
| 77 | |
| 78 | $resource = $this->getResource($this->getRequestParameter('resourceUri')); |
| 79 | $property = $this->getProperty($this->getRequestParameter('propertyUri')); |
| 80 | |
| 81 | $currentValues = []; |
| 82 | foreach ($property->getDomain() as $domain) { |
| 83 | $instances = $domain->searchInstances([ |
| 84 | $property->getUri() => $resource |
| 85 | ], ['recursive' => true, 'like' => false]); |
| 86 | $currentValues = array_merge($currentValues, array_keys($instances)); |
| 87 | } |
| 88 | |
| 89 | $toAdd = array_diff($values, $currentValues); |
| 90 | $toRemove = array_diff($currentValues, $values); |
| 91 | |
| 92 | $success = true; |
| 93 | foreach ($toAdd as $uri) { |
| 94 | $subject = $this->getResource($uri); |
| 95 | $success = $success && $subject->setPropertyValue($property, $resource); |
| 96 | } |
| 97 | |
| 98 | foreach ($toRemove as $uri) { |
| 99 | $subject = $this->getResource($uri); |
| 100 | $success = $success && $subject->removePropertyValue($property, $resource); |
| 101 | } |
| 102 | |
| 103 | return $this->returnJson(['saved' => $success]); |
| 104 | } |
| 105 | } |