Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ClassActionRegistry | |
0.00% |
0 / 32 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
| getExtension | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getConfigId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getClassActions | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| registerAction | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| unregisterAction | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getRelevantClasses | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| 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-2023 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoBackOffice\model\menuStructure; |
| 22 | |
| 23 | use core_kernel_classes_Class; |
| 24 | use oat\generis\model\GenerisRdf; |
| 25 | use oat\generis\model\OntologyRdfs; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\oatbox\AbstractRegistry; |
| 28 | use oat\tao\model\menu\MenuService; |
| 29 | use oat\tao\model\TaoOntology; |
| 30 | use tao_models_classes_GenerisService; |
| 31 | |
| 32 | /** |
| 33 | * Class TreeService |
| 34 | */ |
| 35 | class ClassActionRegistry extends AbstractRegistry |
| 36 | { |
| 37 | public const CLASS_PREFIX = 'class_'; |
| 38 | |
| 39 | /** |
| 40 | * (non-PHPdoc) |
| 41 | * @see \oat\oatbox\AbstractRegistry::getExtension() |
| 42 | */ |
| 43 | protected function getExtension() |
| 44 | { |
| 45 | return \common_ext_ExtensionsManager::singleton()->getExtensionById('taoBackOffice'); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * (non-PHPdoc) |
| 50 | * @see \oat\oatbox\AbstractRegistry::getConfigId() |
| 51 | */ |
| 52 | protected function getConfigId() |
| 53 | { |
| 54 | return 'classActionRegistry'; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns all the actions associated with this class and its parents |
| 59 | * |
| 60 | * @param core_kernel_classes_Class $class |
| 61 | * @return array an array of Action |
| 62 | */ |
| 63 | public function getClassActions(core_kernel_classes_Class $class) |
| 64 | { |
| 65 | $actions = []; |
| 66 | foreach ($this->getRelevantClasses($class) as $rClass) { |
| 67 | if ($this->isRegistered($rClass->getUri())) { |
| 68 | $actions = array_merge($actions, $this->get($rClass->getUri())); |
| 69 | } |
| 70 | } |
| 71 | return $actions; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Register an action with a class |
| 76 | * |
| 77 | * @param core_kernel_classes_Class $class |
| 78 | * @param Action $action |
| 79 | */ |
| 80 | public function registerAction(core_kernel_classes_Class $class, Action $action) |
| 81 | { |
| 82 | $actions = $this->isRegistered($class->getUri()) |
| 83 | ? $this->get($class->getUri()) |
| 84 | : []; |
| 85 | $actions[$action->getId()] = $action; |
| 86 | $this->set($class->getUri(), $actions); |
| 87 | MenuService::flushCache(); |
| 88 | } |
| 89 | |
| 90 | public function unregisterAction(core_kernel_classes_Class $class, Action $action) |
| 91 | { |
| 92 | $actions = $this->isRegistered($class->getUri()) |
| 93 | ? $this->get($class->getUri()) |
| 94 | : []; |
| 95 | unset($actions[$action->getId()]); |
| 96 | $this->set($class->getUri(), $actions); |
| 97 | MenuService::flushCache(); |
| 98 | } |
| 99 | |
| 100 | private function getRelevantClasses(core_kernel_classes_Class $class) |
| 101 | { |
| 102 | $toDo = [$class->getUri() => $class]; |
| 103 | $classes = []; |
| 104 | while (!empty($toDo)) { |
| 105 | $current = array_pop($toDo); |
| 106 | $classes[$current->getUri()] = $current; |
| 107 | if ( |
| 108 | !in_array( |
| 109 | $current->getUri(), |
| 110 | [TaoOntology::OBJECT_CLASS_URI, GenerisRdf::CLASS_GENERIS_RESOURCE, OntologyRdfs::RDFS_CLASS ] |
| 111 | ) |
| 112 | ) { |
| 113 | foreach ($current->getParentClasses(false) as $parent) { |
| 114 | if (!in_array($parent->getUri(), array_keys($classes))) { |
| 115 | $toDo[$parent->getUri()] = $parent; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | return $classes; |
| 121 | } |
| 122 | } |