Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemClassListService
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getList
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
6
 getListElementText
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getDynamicQueryParameters
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
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) 2024 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoItems\model\search;
24
25use core_kernel_classes_Resource;
26use oat\generis\model\data\Ontology;
27use oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService;
28use oat\generis\model\OntologyRdfs;
29use oat\tao\model\TaoOntology;
30
31class ItemClassListService
32{
33    private const CLASS_LIST_LIMIT = 10;
34    private ComplexSearchService $complexSearchService;
35    private Ontology $ontology;
36    public function __construct(ComplexSearchService $complexSearchService, Ontology $ontology)
37    {
38        $this->complexSearchService = $complexSearchService;
39        $this->ontology = $ontology;
40    }
41
42    public function getList(string $query, string $page): array
43    {
44        $page = (int) $page;
45        $root = $this->ontology->getClass(TaoOntology::CLASS_URI_ITEM);
46        $basicQueryParameters = [
47            'recursive' => true,
48            'like' => true,
49            'onlyClass' => true
50        ];
51
52        $query = [
53            OntologyRdfs::RDFS_LABEL => $query
54        ];
55
56        $searchResult = $root->searchInstances(
57            $query,
58            $this->getDynamicQueryParameters($page, $basicQueryParameters)
59        );
60
61        $result['total'] = $root->countInstances($query, $basicQueryParameters) ?? 0;
62        $result['items'] = [];
63
64        foreach ($searchResult as $row) {
65            $result['items'][] = [
66                'id' => $row->getUri(),
67                'uri' => $row->getUri(),
68                'text' => $row->getLabel(),
69                'path' => $this->getListElementText($row)
70            ];
71        }
72        return $result;
73    }
74
75    private function getListElementText(core_kernel_classes_Resource $row): string
76    {
77        $displayText = '';
78        foreach ($row->getParentClassesIds() as $parent) {
79            if ($parent !== TaoOntology::CLASS_URI_ITEM) {
80                $displayText .= $this->ontology->getResource($parent)->getLabel();
81                $displayText .= '/';
82            }
83        }
84
85        $displayText .= $row->getLabel();
86        return $displayText;
87    }
88
89    private function getDynamicQueryParameters(int $page, array $basicQueryParameters)
90    {
91        return array_merge(
92            $basicQueryParameters,
93            [
94                'limit' => self::CLASS_LIST_LIMIT,
95                'offset' => ($page - 1) * self::CLASS_LIST_LIMIT
96            ]
97        );
98    }
99}