Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_Search | |
0.00% |
0 / 46 |
|
0.00% |
0 / 4 |
90 | |
0.00% |
0 / 1 |
| searchParams | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
6 | |||
| search | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| getIndexes | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| getSearchProxy | |
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) 2014-2020 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | use oat\generis\model\OntologyAwareTrait; |
| 25 | use oat\tao\model\http\HttpJsonResponseTrait; |
| 26 | use oat\tao\model\Lists\Business\Domain\ClassMetadataSearchRequest; |
| 27 | use oat\tao\model\search\index\OntologyIndexService; |
| 28 | use oat\tao\model\search\SearchProxy; |
| 29 | |
| 30 | /** |
| 31 | * Controller for indexed searches |
| 32 | * |
| 33 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
| 34 | * @package tao |
| 35 | * phpcs:disable Squiz.Classes.ValidClassName |
| 36 | */ |
| 37 | class tao_actions_Search extends tao_actions_CommonModule |
| 38 | { |
| 39 | use OntologyAwareTrait; |
| 40 | use HttpJsonResponseTrait; |
| 41 | |
| 42 | /** |
| 43 | * Search parameters endpoints. |
| 44 | * The response provides parameters to create a datatable. |
| 45 | */ |
| 46 | public function searchParams(): void |
| 47 | { |
| 48 | $queryParams = $this->getPsrRequest()->getQueryParams(); |
| 49 | $parsedBody = $this->getPsrRequest()->getParsedBody(); |
| 50 | if ( |
| 51 | !isset( |
| 52 | $parsedBody['structure'], |
| 53 | $parsedBody['query'], |
| 54 | $queryParams['rootNode'], |
| 55 | $parsedBody['parentNode'] |
| 56 | ) |
| 57 | ) { |
| 58 | $this->setErrorJsonResponse('Request is missing required params'); |
| 59 | } |
| 60 | |
| 61 | $this->setSuccessJsonResponse([ |
| 62 | 'url' => _url('search'), |
| 63 | 'params' => [ |
| 64 | 'query' => $parsedBody['query'], |
| 65 | 'rootNode' => $queryParams['rootNode'], |
| 66 | 'parentNode' => $parsedBody['parentNode'], |
| 67 | 'structure' => $parsedBody['structure'], |
| 68 | ], |
| 69 | 'settings' => $this->getSearchProxy()->getSearchSettingsService()->getSettingsByClassMetadataSearchRequest( |
| 70 | (new ClassMetadataSearchRequest())->setStructure($parsedBody['structure']) |
| 71 | ->setClassUri($parsedBody['parentNode']) |
| 72 | ), |
| 73 | 'filter' => [], |
| 74 | 'result' => true |
| 75 | ]); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Search results |
| 80 | * The search is paginated and initiated by the datatable component. |
| 81 | */ |
| 82 | public function search(): void |
| 83 | { |
| 84 | try { |
| 85 | $this->returnJson( |
| 86 | $this->getSearchProxy()->search( |
| 87 | $this->getPsrRequest() |
| 88 | ) |
| 89 | ); |
| 90 | } catch (Exception $exception) { |
| 91 | $this->setErrorJsonResponse( |
| 92 | $exception->getMessage() |
| 93 | ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public function getIndexes(): void |
| 98 | { |
| 99 | if ($this->hasRequestParameter('rootNode') === true) { |
| 100 | $rootNodeUri = $this->getRequestParameter('rootNode'); |
| 101 | $indexes = OntologyIndexService::getIndexesByClass($this->getClass($rootNodeUri)); |
| 102 | $json = []; |
| 103 | |
| 104 | foreach ($indexes as $propertyUri => $index) { |
| 105 | foreach ($index as $i) { |
| 106 | $json[] = [ |
| 107 | 'identifier' => $i->getIdentifier(), |
| 108 | 'fuzzyMatching' => $i->isFuzzyMatching(), |
| 109 | 'propertyId' => $propertyUri, |
| 110 | ]; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | $this->setSuccessJsonResponse($json, 200); |
| 115 | } else { |
| 116 | $this->returnJson("The 'rootNode' parameter is missing.", 500); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private function getSearchProxy(): SearchProxy |
| 121 | { |
| 122 | return $this->getServiceLocator()->get(SearchProxy::SERVICE_ID); |
| 123 | } |
| 124 | } |