Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| OntologyExtractor | |
0.00% |
0 / 40 |
|
0.00% |
0 / 5 |
272 | |
0.00% |
0 / 1 |
| setItem | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addColumn | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| run | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
110 | |||
| getData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __toPhpCode | |
0.00% |
0 / 1 |
|
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) 2015 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\flyExporter\extractor; |
| 24 | |
| 25 | /** |
| 26 | * Extract given column of item ontology data |
| 27 | * |
| 28 | * Class OntologyExtractor |
| 29 | * @package oat\taoQtiItem\model\simpleExporter |
| 30 | */ |
| 31 | class OntologyExtractor implements Extractor |
| 32 | { |
| 33 | /** |
| 34 | * Item to export |
| 35 | * @var \core_kernel_classes_Resource |
| 36 | */ |
| 37 | protected $item; |
| 38 | |
| 39 | /** |
| 40 | * Request columns |
| 41 | * @var array |
| 42 | */ |
| 43 | protected $columns = []; |
| 44 | |
| 45 | /** |
| 46 | * Output of data |
| 47 | * @var array |
| 48 | */ |
| 49 | protected $data = []; |
| 50 | |
| 51 | /** |
| 52 | * Set item to export |
| 53 | * |
| 54 | * @param \core_kernel_classes_Resource $item |
| 55 | * @return Extractor $this |
| 56 | */ |
| 57 | public function setItem(\core_kernel_classes_Resource $item) |
| 58 | { |
| 59 | $this->item = $item; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Add column to export |
| 64 | * Check if core_kernel_classes_Property exists into $config |
| 65 | * |
| 66 | * @param $column |
| 67 | * @param array $config |
| 68 | * @throws ExtractorException |
| 69 | * @return Extractor $this |
| 70 | */ |
| 71 | public function addColumn($column, array $config) |
| 72 | { |
| 73 | if (!isset($config['property'])) { |
| 74 | throw new ExtractorException('Property config is missing.'); |
| 75 | } |
| 76 | |
| 77 | $property = new \core_kernel_classes_Property($config['property']); |
| 78 | if (!$property->exists()) { |
| 79 | throw new ExtractorException('Property config is not a valid property uri.'); |
| 80 | } |
| 81 | $config['property'] = $property; |
| 82 | |
| 83 | $this->columns[$column] = $config; |
| 84 | return $this; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get ontology values of requested item properties |
| 89 | * |
| 90 | * @return $this |
| 91 | * @throws \Exception |
| 92 | */ |
| 93 | public function run() |
| 94 | { |
| 95 | $this->data = []; |
| 96 | |
| 97 | if (empty($this->item) || !($this->item instanceof \core_kernel_classes_Resource)) { |
| 98 | throw new ExtractorException('Export item not set.'); |
| 99 | } |
| 100 | |
| 101 | $properties = []; |
| 102 | foreach ($this->columns as $config) { |
| 103 | $properties[] = $config['property']; |
| 104 | } |
| 105 | $values = $this->item->getPropertiesValues($properties); |
| 106 | |
| 107 | foreach ($this->columns as $column => $config) { |
| 108 | try { |
| 109 | $data = []; |
| 110 | foreach ($values[$config['property']->getUri()] as $itemValue) { |
| 111 | if (is_array($itemValue)) { |
| 112 | array_walk($itemValue, function (&$value) { |
| 113 | $resource = new \core_kernel_classes_Resource($value); |
| 114 | $value = $resource->getLabel(); |
| 115 | }); |
| 116 | |
| 117 | if (isset($config['delimiter'])) { |
| 118 | $delimiter = $config['delimiter']; |
| 119 | } else { |
| 120 | $delimiter = self::DEFAULT_PROPERTY_DELIMITER; |
| 121 | } |
| 122 | $data[] = explode($delimiter, $itemValue); |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | $data[] = ($itemValue instanceof \core_kernel_classes_Resource) |
| 127 | ? $itemValue->getLabel() |
| 128 | : str_replace('"', '""', (string) $itemValue); |
| 129 | } |
| 130 | } catch (\Exception $e) { |
| 131 | \common_Logger::e('ERROR on column ' . $column . ' : ' . $e->getMessage()); |
| 132 | $data = ['N/A']; |
| 133 | } |
| 134 | |
| 135 | $this->data[$column] = implode(self::DEFAULT_PROPERTY_DELIMITER, $data); |
| 136 | } |
| 137 | $this->columns = []; |
| 138 | return $this; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Return formatted output |
| 143 | * @return array |
| 144 | */ |
| 145 | public function getData() |
| 146 | { |
| 147 | return $this->data; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get human readable declaration class |
| 152 | * @return string |
| 153 | */ |
| 154 | public function __toPhpCode() |
| 155 | { |
| 156 | return 'new ' . get_class($this) . '()'; |
| 157 | } |
| 158 | } |