*/ class TreeItemLookup extends ConfigurableService implements ItemLookup { use PermissionLookupTrait; public const SERVICE_ID = 'taoQtiTest/CreatorItems/tree'; public function getCategoryService(): CategoryService { /** @noinspection PhpIncompatibleReturnTypeInspection */ return $this->getServiceLocator()->get(CategoryService::SERVICE_ID); } public function getTreeResourceLookupService(): ResourceLookup { /** @noinspection PhpIncompatibleReturnTypeInspection */ return $this->getServiceLocator()->get(TreeResourceLookup::SERVICE_ID); } /** * Retrieve QTI Items in their hierarchy, for the given parameters as format them as tree. * * @param core_kernel_classes_Class $itemClass the item class * @param array $propertyFilters propUri/propValue to search items * @param int $offset for paging * @param int $limit for paging * * @return array the items * * @throws common_exception_Error */ public function getItems( core_kernel_classes_Class $itemClass, array $propertyFilters = [], $offset = 0, $limit = 30 ): array { $data = $this->getTreeResourceLookupService()->getResources($itemClass, [], $propertyFilters, $offset, $limit); $nodes = $this->formatTreeData($data); $nodes = $this->fillPermissions($nodes); return $nodes; } /** * Reformat the the tree : state and count * Add the item's categories * * @param array $treeData * * @return array the formatted data * * @throws common_exception_Error */ private function formatTreeData(array $treeData): array { foreach ($treeData as &$item) { if ($item['type'] === 'instance') { $item['categories'] = $this->getCategoryService()->getItemCategories( new core_kernel_classes_Resource($item['uri']) ); } elseif (isset($item['children'])) { $item['children'] = $this->formatTreeData($item['children']); } } return $treeData; } }