Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
12.77% |
6 / 47 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| WidgetRegistry | |
12.77% |
6 / 47 |
|
0.00% |
0 / 4 |
144.11 | |
0.00% |
0 / 1 |
| getWidgetDefinitionById | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| getWidgetDefinition | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getWidgetDefinitions | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
4.12 | |||
| getWidgetsFromOntology | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
42 | |||
| 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) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\helpers\form; |
| 24 | |
| 25 | use common_cache_FileCache; |
| 26 | use common_cache_NotFoundException; |
| 27 | use common_Logger; |
| 28 | use core_kernel_classes_Class; |
| 29 | use core_kernel_classes_Property; |
| 30 | use oat\generis\model\WidgetRdf; |
| 31 | |
| 32 | /** |
| 33 | * The FormFactory enable you to create ready-to-use instances of the Form |
| 34 | * It helps you to get the commonly used instances for the default rendering |
| 35 | * etc. |
| 36 | * |
| 37 | * @access public |
| 38 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 39 | * @package tao |
| 40 | |
| 41 | */ |
| 42 | class WidgetRegistry |
| 43 | { |
| 44 | public const CACHE_KEY = 'tao_widget_data'; |
| 45 | |
| 46 | /** |
| 47 | * Cache of widget informations |
| 48 | * @var array |
| 49 | */ |
| 50 | private static $widgetCache = null; |
| 51 | |
| 52 | public static function getWidgetDefinitionById($id) |
| 53 | { |
| 54 | $widgets = self::getWidgetDefinitions(); |
| 55 | foreach ($widgets as $widgetDefinition) { |
| 56 | if ($widgetDefinition['id'] == strtolower($id)) { |
| 57 | return $widgetDefinition; |
| 58 | } |
| 59 | } |
| 60 | // Many widgets don't have a proper definition, don't throw any error |
| 61 | // common_Logger::w('Widget with id "'.strtolower($id).'" not found'); |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | public static function getWidgetDefinition(\core_kernel_classes_Resource $widget) |
| 66 | { |
| 67 | $widgets = self::getWidgetDefinitions(); |
| 68 | if (isset($widgets[$widget->getUri()])) { |
| 69 | return $widgets[$widget->getUri()]; |
| 70 | } else { |
| 71 | common_Logger::w('Widget "' . $widget->getUri() . '" not found'); |
| 72 | return null; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | protected static function getWidgetDefinitions() |
| 77 | { |
| 78 | if (is_null(self::$widgetCache)) { |
| 79 | try { |
| 80 | self::$widgetCache = common_cache_FileCache::singleton()->get(self::CACHE_KEY); |
| 81 | } catch (common_cache_NotFoundException $e) { |
| 82 | // not in cache need to load |
| 83 | self::$widgetCache = self::getWidgetsFromOntology(); |
| 84 | common_cache_FileCache::singleton()->put(self::$widgetCache, self::CACHE_KEY); |
| 85 | } |
| 86 | } |
| 87 | return self::$widgetCache; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @return array |
| 92 | */ |
| 93 | private static function getWidgetsFromOntology() |
| 94 | { |
| 95 | $class = new core_kernel_classes_Class(WidgetRdf::CLASS_URI_WIDGET); |
| 96 | $rendererClass = new core_kernel_classes_Class(WidgetRdf::CLASS_URI_WIDGET_RENDERER); |
| 97 | $widgets = []; |
| 98 | foreach ($class->getInstances(true) as $widgetResource) { |
| 99 | $id = $widgetResource->getOnePropertyValue(new core_kernel_classes_Property(WidgetRdf::PROPERTY_WIDGET_ID)); |
| 100 | if (!is_null($id)) { |
| 101 | $renderers = $rendererClass->searchInstances([ |
| 102 | WidgetRdf::PROPERTY_WIDGET_RENDERER_WIDGET => $widgetResource->getUri() |
| 103 | ], ['like' => false]); |
| 104 | $rendererClasses = []; |
| 105 | foreach ($renderers as $renderer) { |
| 106 | $props = $renderer->getPropertiesValues([ |
| 107 | WidgetRdf::PROPERTY_WIDGET_RENDERER_MODE, WidgetRdf::PROPERTY_WIDGET_RENDERER_IMPLEMENTATION |
| 108 | ]); |
| 109 | if ( |
| 110 | count($props[WidgetRdf::PROPERTY_WIDGET_RENDERER_MODE]) == 1 |
| 111 | && count($props[WidgetRdf::PROPERTY_WIDGET_RENDERER_IMPLEMENTATION]) |
| 112 | ) { |
| 113 | $mode = (string)reset($props[WidgetRdf::PROPERTY_WIDGET_RENDERER_MODE]); |
| 114 | $class = (string)reset($props[WidgetRdf::PROPERTY_WIDGET_RENDERER_IMPLEMENTATION]); |
| 115 | $rendererClasses[$mode] = $class; |
| 116 | } else { |
| 117 | common_Logger::w('Definition of $widget renderer.' . $renderer->getUri() . ') invalid'); |
| 118 | } |
| 119 | } |
| 120 | $widgets[$widgetResource->getUri()] = [ |
| 121 | 'id' => strtolower($id), |
| 122 | 'renderers' => $rendererClasses |
| 123 | ]; |
| 124 | } else { |
| 125 | // deprecated widget |
| 126 | $id = substr($widgetResource->getUri(), strpos($widgetResource->getUri(), '#') + 1); |
| 127 | $className = "tao_helpers_form_elements_xhtml_" . ucfirst(strtolower($id)); |
| 128 | $widgets[$widgetResource->getUri()] = [ |
| 129 | 'id' => strtolower($id), |
| 130 | 'renderers' => ['xhtml' => $className] |
| 131 | ]; |
| 132 | } |
| 133 | } |
| 134 | return $widgets; |
| 135 | } |
| 136 | } |