Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| MetaDataOntologyExtractor | |
0.00% |
0 / 26 |
|
0.00% |
0 / 8 |
182 | |
0.00% |
0 / 1 |
| setItem | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| addColumn | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| run | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| getData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __toPhpCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| chooseStrategy | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setMetaDataProperties | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getAvailableMetaDataProperties | |
0.00% |
0 / 4 |
|
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) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\Export\Extractor; |
| 24 | |
| 25 | use core_kernel_classes_Class; |
| 26 | use core_kernel_classes_Resource; |
| 27 | use oat\taoQtiItem\model\Export\Extractor\Strategy\Strategy; |
| 28 | use oat\taoQtiItem\model\Export\Extractor\Strategy\StrategyFactory; |
| 29 | use oat\taoQtiItem\model\flyExporter\extractor\Extractor; |
| 30 | use oat\taoQtiItem\model\flyExporter\extractor\ExtractorException; |
| 31 | use tao_helpers_form_GenerisFormFactory; |
| 32 | |
| 33 | class MetaDataOntologyExtractor implements Extractor |
| 34 | { |
| 35 | /** |
| 36 | * Item to export |
| 37 | * @var \core_kernel_classes_Resource |
| 38 | */ |
| 39 | protected $item; |
| 40 | |
| 41 | /** |
| 42 | * Request columns |
| 43 | * @var array |
| 44 | */ |
| 45 | protected $columns = []; |
| 46 | |
| 47 | /** |
| 48 | * Output of data |
| 49 | * @var array |
| 50 | */ |
| 51 | protected $data = []; |
| 52 | |
| 53 | /** |
| 54 | * Meta data properties available for export. |
| 55 | * |
| 56 | * @var array |
| 57 | */ |
| 58 | private $metaDataProperties = []; |
| 59 | |
| 60 | |
| 61 | public function setItem(\core_kernel_classes_Resource $item) |
| 62 | { |
| 63 | $this->item = $item; |
| 64 | |
| 65 | return $this; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add column to export |
| 70 | * Check if core_kernel_classes_Property exists into $config |
| 71 | * |
| 72 | * @param $column |
| 73 | * @param array $config |
| 74 | * @throws ExtractorException |
| 75 | * @return Extractor $this |
| 76 | */ |
| 77 | public function addColumn($column, array $config) |
| 78 | { |
| 79 | $this->columns[$column] = $config; |
| 80 | |
| 81 | return $this; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @inheritdoc |
| 86 | */ |
| 87 | public function run() |
| 88 | { |
| 89 | if (empty($this->item) || !($this->item instanceof \core_kernel_classes_Resource)) { |
| 90 | throw new ExtractorException('Export item not set.'); |
| 91 | } |
| 92 | |
| 93 | $this->data = []; |
| 94 | |
| 95 | foreach ($this->columns as $column => $config) { |
| 96 | $strategy = $this->chooseStrategy($config, $column); |
| 97 | |
| 98 | foreach ($this->metaDataProperties as $dataProperty) { |
| 99 | $strategy->addHashEntry(OntologyExtractorRunner::run($this->item, $dataProperty)); |
| 100 | } |
| 101 | |
| 102 | $this->data = array_merge($this->data, $strategy->toArray()); |
| 103 | } |
| 104 | |
| 105 | return $this; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Return output data |
| 110 | * |
| 111 | * @return array |
| 112 | */ |
| 113 | public function getData() |
| 114 | { |
| 115 | return $this->data; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @inheritdoc |
| 120 | */ |
| 121 | public function __toPhpCode() |
| 122 | { |
| 123 | return 'new ' . get_class($this) . '()'; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * @param array $config |
| 129 | * @param string $column |
| 130 | * @return Strategy |
| 131 | */ |
| 132 | protected function chooseStrategy(array $config, $column) |
| 133 | { |
| 134 | $this->setMetaDataProperties($config); |
| 135 | |
| 136 | return StrategyFactory::create($config, $column, $this->metaDataProperties); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /** |
| 141 | * Setting the meta data properties available for export. |
| 142 | * |
| 143 | * @param array $config |
| 144 | * @throws ExtractorException |
| 145 | */ |
| 146 | protected function setMetaDataProperties(array $config) |
| 147 | { |
| 148 | $itemTypes = $this->item->getTypes(); |
| 149 | $classType = reset($itemTypes); |
| 150 | |
| 151 | if (!$classType->isClass()) { |
| 152 | throw new ExtractorException('Item class type do not exists'); |
| 153 | } |
| 154 | |
| 155 | $this->metaDataProperties = $this->getAvailableMetaDataProperties($classType, $config['excludedProperties']); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @param core_kernel_classes_Class $classToExport |
| 160 | * |
| 161 | * @param array $excludedProperties |
| 162 | * @return array |
| 163 | */ |
| 164 | protected function getAvailableMetaDataProperties($classToExport, array $excludedProperties) |
| 165 | { |
| 166 | $props = tao_helpers_form_GenerisFormFactory::getClassProperties($classToExport); |
| 167 | |
| 168 | return array_filter($props, function ($prop) use ($excludedProperties) { |
| 169 | /**@var $prop core_kernel_classes_Resource */ |
| 170 | return !in_array($prop->getUri(), $excludedProperties); |
| 171 | }); |
| 172 | } |
| 173 | } |