Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
74.42% |
32 / 43 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
| CategoryService | |
74.42% |
32 / 43 |
|
62.50% |
5 / 8 |
26.70 | |
0.00% |
0 / 1 |
| getItemsCategories | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getItemCategories | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
6 | |||
| sanitizeCategoryName | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getElligibleProperties | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| doesExposeCategory | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| exposeCategory | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getItemService | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| setItemService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2016-2020 (original work) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoItems\model; |
| 22 | |
| 23 | use core_kernel_classes_Class as RdfClass; |
| 24 | use core_kernel_classes_Property as RdfProperty; |
| 25 | use core_kernel_classes_Resource as RdfResource; |
| 26 | use oat\generis\model\GenerisRdf; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use taoItems_models_classes_ItemsService; |
| 29 | |
| 30 | /** |
| 31 | * Category management service. |
| 32 | * How to expose RDF properties to categorize them. |
| 33 | * |
| 34 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
| 35 | */ |
| 36 | class CategoryService extends ConfigurableService |
| 37 | { |
| 38 | public const SERVICE_ID = 'taoItems/Category'; |
| 39 | |
| 40 | public const ITEM_CLASS_URI = 'http://www.tao.lu/Ontologies/TAOItem.rdf#Item'; |
| 41 | public const EXPOSE_PROP_URI = 'http://www.tao.lu/Ontologies/TAOItem.rdf#ExposeCategory'; |
| 42 | |
| 43 | public static $supportedWidgetUris = [ |
| 44 | 'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#TextBox', |
| 45 | 'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#CheckBox', |
| 46 | 'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#RadioBox', |
| 47 | 'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#ComboBox', |
| 48 | 'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#TreeBox' |
| 49 | ]; |
| 50 | |
| 51 | public static $excludedPropUris = [ |
| 52 | 'http://www.tao.lu/Ontologies/TAOItem.rdf#ItemModel' |
| 53 | ]; |
| 54 | |
| 55 | /** |
| 56 | * @var taoItems_models_classes_ItemsService |
| 57 | */ |
| 58 | protected $itemService; |
| 59 | |
| 60 | /** |
| 61 | * Get the categories link to the list of items in parameter. |
| 62 | * Theses categories come from a configurable list of properties. |
| 63 | * The category label is also set in a configurable list |
| 64 | * |
| 65 | * @param RdfResource[] $items the list of items |
| 66 | * |
| 67 | * @return array of categories for specified items |
| 68 | * ['itemUri' => ['CATEGORY1', 'CATEGORY2']] |
| 69 | */ |
| 70 | public function getItemsCategories(array $items) |
| 71 | { |
| 72 | $categories = []; |
| 73 | |
| 74 | foreach ($items as $item) { |
| 75 | $itemCategories = $this->getItemCategories($item); |
| 76 | if (count($itemCategories) > 0) { |
| 77 | $categories[$item->getUri()] = $itemCategories; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return $categories; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the categories of an item |
| 86 | * |
| 87 | * @param RdfResource $item the item |
| 88 | * |
| 89 | * @return string[] the list of categories |
| 90 | */ |
| 91 | public function getItemCategories(RdfResource $item) |
| 92 | { |
| 93 | $categories = []; |
| 94 | |
| 95 | foreach ($item->getTypes() as $class) { |
| 96 | $eligibleProperties = array_filter($this->getElligibleProperties($class), [$this, 'doesExposeCategory']); |
| 97 | $propertiesValues = $item->getPropertiesValues(array_keys($eligibleProperties)); |
| 98 | |
| 99 | foreach ($propertiesValues as $propertyValues) { |
| 100 | foreach ($propertyValues as $value) { |
| 101 | if ($value instanceof RdfResource) { |
| 102 | $sanitizedIdentifier = self::sanitizeCategoryName($value->getLabel()); |
| 103 | } else { |
| 104 | $sanitizedIdentifier = self::sanitizeCategoryName((string)$value); |
| 105 | } |
| 106 | |
| 107 | if ($sanitizedIdentifier) { |
| 108 | $categories[] = $sanitizedIdentifier; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return $categories; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Sanitize the name of the category : |
| 119 | * Remove special chars, allowing unicode ones, replace spaces by dashes |
| 120 | * and trim the beginning if it's not a letter. |
| 121 | * |
| 122 | * @param string $value the input value |
| 123 | * |
| 124 | * @return string the sanitized value |
| 125 | */ |
| 126 | public static function sanitizeCategoryName($value) |
| 127 | { |
| 128 | $output = preg_replace('/\s+/', '-', trim($value)); |
| 129 | $output = preg_replace('/[^\p{L}0-9\-]/u', '', mb_strtolower($output)); |
| 130 | $output = preg_replace('/^[0-9\-_]+/', '', $output); |
| 131 | return mb_substr($output, 0, 32); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get the properties from a class that can be exposed |
| 136 | * |
| 137 | * @param RdfClass $class the $class |
| 138 | * |
| 139 | * @return RdfProperty[] the list of eligible properties |
| 140 | */ |
| 141 | public function getElligibleProperties(RdfClass $class) |
| 142 | { |
| 143 | $properties = $this->getItemService()->getClazzProperties($class, new RdfClass(self::ITEM_CLASS_URI)); |
| 144 | |
| 145 | return array_filter( |
| 146 | $properties, |
| 147 | static function (RdfProperty $property) { |
| 148 | if (in_array($property->getUri(), self::$excludedPropUris, true)) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | $widget = $property->getWidget(); |
| 153 | |
| 154 | return null !== $widget && in_array($widget->getUri(), self::$supportedWidgetUris, true); |
| 155 | } |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Check if a property is exposed |
| 161 | * |
| 162 | * @param RdfProperty $property the property to check |
| 163 | * |
| 164 | * @return bool true if exposed |
| 165 | */ |
| 166 | public function doesExposeCategory(RdfProperty $property) |
| 167 | { |
| 168 | $exposeProperty = new RdfProperty(self::EXPOSE_PROP_URI); |
| 169 | $expose = $property->getOnePropertyValue($exposeProperty); |
| 170 | |
| 171 | return !is_null($expose) && $expose->getUri() === GenerisRdf::GENERIS_TRUE; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Expose or not a property |
| 176 | * |
| 177 | * @param RdfProperty $property the property to check |
| 178 | * @param bool $value true if exposed |
| 179 | * |
| 180 | * @return void |
| 181 | */ |
| 182 | public function exposeCategory(RdfProperty $property, $value) |
| 183 | { |
| 184 | $exposeProperty = new RdfProperty(self::EXPOSE_PROP_URI); |
| 185 | |
| 186 | if ($value == true) { |
| 187 | $property->setPropertyValue($exposeProperty, GenerisRdf::GENERIS_TRUE); |
| 188 | } else { |
| 189 | $property->removePropertyValue($exposeProperty, GenerisRdf::GENERIS_TRUE); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Service getter and initializer. |
| 195 | * |
| 196 | * @return taoItems_models_classes_ItemsService the service |
| 197 | */ |
| 198 | public function getItemService() |
| 199 | { |
| 200 | if (is_null($this->itemService)) { |
| 201 | $this->itemService = taoItems_models_classes_ItemsService::singleton(); |
| 202 | } |
| 203 | return $this->itemService; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Service setter |
| 208 | * |
| 209 | * @param taoItems_models_classes_ItemsService $itemService the service |
| 210 | * |
| 211 | * @return void |
| 212 | */ |
| 213 | public function setItemService(taoItems_models_classes_ItemsService $itemService) |
| 214 | { |
| 215 | $this->itemService = $itemService; |
| 216 | } |
| 217 | } |