Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| core_kernel_api_ModelFactory | |
0.00% |
0 / 31 |
|
0.00% |
0 / 2 |
90 | |
0.00% |
0 / 1 |
| createModel | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| addStatement | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
20 | |||
| 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) 2013-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * @author "Lionel Lecaque, <lionel@taotesting.com>" |
| 21 | * @license GPLv2 |
| 22 | * @package generis |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | use core_kernel_persistence_smoothsql_SmoothModel as SmoothModel; |
| 27 | use EasyRdf\Format; |
| 28 | use EasyRdf\Graph; |
| 29 | |
| 30 | class core_kernel_api_ModelFactory |
| 31 | { |
| 32 | /** |
| 33 | * @param string $namespace |
| 34 | * @param string $data xml content |
| 35 | * |
| 36 | * @throws \EasyRdf\Exception |
| 37 | */ |
| 38 | public function createModel($namespace, $data) |
| 39 | { |
| 40 | $modelId = SmoothModel::DEFAULT_READ_ONLY_MODEL; |
| 41 | |
| 42 | $modelDefinition = new Graph($namespace); |
| 43 | |
| 44 | if (is_file($data)) { |
| 45 | $modelDefinition->parseFile($data); |
| 46 | } else { |
| 47 | $modelDefinition->parse($data); |
| 48 | } |
| 49 | |
| 50 | $data = $modelDefinition->serialise(Format::getFormat('php')); |
| 51 | |
| 52 | foreach ($data as $subjectUri => $propertiesValues) { |
| 53 | foreach ($propertiesValues as $prop => $values) { |
| 54 | foreach ($values as $k => $v) { |
| 55 | $this->addStatement($modelId, $subjectUri, $prop, $v['value'], $v['lang'] ?? null); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Adds a statement to the ontology if it does not exist yet |
| 65 | * |
| 66 | * @author "Joel Bout, <joel@taotesting.com>" |
| 67 | * @param int $modelId |
| 68 | * @param string $subject |
| 69 | * @param string $predicate |
| 70 | * @param string $object |
| 71 | * @param string $lang |
| 72 | */ |
| 73 | private function addStatement($modelId, $subject, $predicate, $object, $lang = null) |
| 74 | { |
| 75 | $result = core_kernel_classes_DbWrapper::singleton()->query( |
| 76 | 'SELECT count(*) FROM statements WHERE modelid = ? AND subject = ? AND predicate = ? ' |
| 77 | . 'AND object = ? AND l_language = ?', |
| 78 | [$modelId, $subject, $predicate, $object, (is_null($lang)) ? '' : $lang] |
| 79 | ); |
| 80 | |
| 81 | if (intval($result->fetchColumn()) === 0) { |
| 82 | $dbWrapper = core_kernel_classes_DbWrapper::singleton(); |
| 83 | $date = $dbWrapper->getPlatForm()->getNowExpression(); |
| 84 | |
| 85 | $dbWrapper->insert( |
| 86 | 'statements', |
| 87 | [ |
| 88 | 'modelid' => $modelId, |
| 89 | 'subject' => $subject, |
| 90 | 'predicate' => $predicate, |
| 91 | 'object' => $object, |
| 92 | 'l_language' => is_null($lang) ? '' : $lang, |
| 93 | 'author' => 'http://www.tao.lu/Ontologies/TAO.rdf#installator', |
| 94 | 'epoch' => $date |
| 95 | ] |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 | } |