Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.23% |
49 / 52 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
ItemClassListService | |
94.23% |
49 / 52 |
|
80.00% |
4 / 5 |
12.03 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getList | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
2 | |||
getListElementText | |
57.14% |
4 / 7 |
|
0.00% |
0 / 1 |
3.71 | |||
getDynamicQueryParameters | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
skipNotAccessible | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 |
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) 2024 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoItems\model\search; |
24 | |
25 | use core_kernel_classes_Resource; |
26 | use oat\generis\model\data\Ontology; |
27 | use oat\generis\model\data\permission\PermissionInterface; |
28 | use oat\generis\model\OntologyRdfs; |
29 | use oat\oatbox\session\SessionService; |
30 | use oat\tao\model\TaoOntology; |
31 | |
32 | class ItemClassListService |
33 | { |
34 | private const CLASS_LIST_LIMIT = 25; |
35 | private Ontology $ontology; |
36 | private PermissionInterface $permissionManager; |
37 | private SessionService $sessionService; |
38 | |
39 | public function __construct(Ontology $ontology, PermissionInterface $permissionManager, $sessionService) |
40 | { |
41 | $this->ontology = $ontology; |
42 | $this->permissionManager = $permissionManager; |
43 | $this->sessionService = $sessionService; |
44 | } |
45 | |
46 | public function getList(string $query, string $page): array |
47 | { |
48 | $page = (int) $page; |
49 | $root = $this->ontology->getClass(TaoOntology::CLASS_URI_ITEM); |
50 | $basicQueryParameters = [ |
51 | 'recursive' => true, |
52 | 'like' => true, |
53 | 'onlyClass' => true |
54 | ]; |
55 | |
56 | $query = [ |
57 | OntologyRdfs::RDFS_LABEL => $query |
58 | ]; |
59 | |
60 | $searchResult = $root->searchInstances( |
61 | $query, |
62 | $this->getDynamicQueryParameters($page, $basicQueryParameters) |
63 | ); |
64 | |
65 | $this->skipNotAccessible($searchResult); |
66 | |
67 | $result['total'] = $root->countInstances($query, $basicQueryParameters) ?? 0; |
68 | $result['items'] = []; |
69 | |
70 | foreach ($searchResult as $row) { |
71 | $result['items'][] = [ |
72 | 'id' => $row->getUri(), |
73 | 'uri' => $row->getUri(), |
74 | 'text' => $row->getLabel(), |
75 | 'path' => $this->getListElementText($row) |
76 | ]; |
77 | } |
78 | return $result; |
79 | } |
80 | |
81 | private function getListElementText(core_kernel_classes_Resource $row): string |
82 | { |
83 | $displayText = ''; |
84 | foreach ($row->getParentClassesIds() as $parent) { |
85 | if ($parent !== TaoOntology::CLASS_URI_ITEM) { |
86 | $displayText .= $this->ontology->getResource($parent)->getLabel(); |
87 | $displayText .= '/'; |
88 | } |
89 | } |
90 | |
91 | $displayText .= $row->getLabel(); |
92 | return $displayText; |
93 | } |
94 | |
95 | private function getDynamicQueryParameters(int $page, array $basicQueryParameters): array |
96 | { |
97 | return array_merge( |
98 | $basicQueryParameters, |
99 | [ |
100 | 'limit' => self::CLASS_LIST_LIMIT, |
101 | 'offset' => ($page - 1) * self::CLASS_LIST_LIMIT |
102 | ] |
103 | ); |
104 | } |
105 | |
106 | private function skipNotAccessible(array &$results): void |
107 | { |
108 | if (!count($this->permissionManager->getSupportedRights())) { |
109 | // if DAC is not enabled |
110 | return; |
111 | } |
112 | |
113 | $uris = array_map(function (core_kernel_classes_Resource $a): string { |
114 | return $a->getUri(); |
115 | }, $results); |
116 | |
117 | $permissions = $this->permissionManager->getPermissions($this->sessionService->getCurrentUser(), $uris); |
118 | |
119 | foreach ($results as $key => &$row) { |
120 | $uri = $row->getUri(); |
121 | if (isset($permissions[$uri]) && !in_array(PermissionInterface::RIGHT_WRITE, $permissions[$uri])) { |
122 | unset($results[$key]); |
123 | } |
124 | } |
125 | } |
126 | } |