Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.65% |
22 / 23 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TestMapBranchRuleExtender | |
95.65% |
22 / 23 |
|
66.67% |
2 / 3 |
8 | |
0.00% |
0 / 1 |
| getTestMapWithBranchRules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addBranchRuleToTestMapRecursively | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
| addBranchRuleToTestMap | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| 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) 2019 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * @author Péter Halász <peter@taotesting.com> |
| 23 | */ |
| 24 | |
| 25 | namespace oat\taoQtiTest\models\runner\map; |
| 26 | |
| 27 | use taoQtiTest_models_classes_QtiTestService; |
| 28 | |
| 29 | class TestMapBranchRuleExtender |
| 30 | { |
| 31 | public const KEY_ATTRIBUTES = '@attributes'; |
| 32 | public const KEY_BRANCH_RULE = 'branchRule'; |
| 33 | public const KEY_IDENTIFIER = 'identifier'; |
| 34 | public const BRANCH_RULE_MAP_PAIRS = [ |
| 35 | [RunnerMap::MAP_ATTRIBUTE_PARTS, taoQtiTest_models_classes_QtiTestService::XML_TEST_PART], |
| 36 | [RunnerMap::MAP_ATTRIBUTE_SECTIONS, taoQtiTest_models_classes_QtiTestService::XML_ASSESSMENT_SECTION], |
| 37 | [RunnerMap::MAP_ATTRIBUTE_ITEMS, taoQtiTest_models_classes_QtiTestService::XML_ASSESSMENT_ITEM_REF], |
| 38 | ]; |
| 39 | |
| 40 | /** |
| 41 | * Returns the testMap, extended with the branch rules from the testDefinition |
| 42 | * |
| 43 | * @param array $testMap |
| 44 | * @param array $testDefinition |
| 45 | * @return array |
| 46 | */ |
| 47 | public function getTestMapWithBranchRules(array $testMap, array $testDefinition) |
| 48 | { |
| 49 | return $this->addBranchRuleToTestMapRecursively($testMap, $testDefinition, self::BRANCH_RULE_MAP_PAIRS); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Adds the branching rules from the given testDefinition into the testMap, going through recursively on the given |
| 54 | * mapping pairs. |
| 55 | * |
| 56 | * @param array $testMap |
| 57 | * @param array $testDefinition |
| 58 | * @param array $map |
| 59 | * @return array |
| 60 | */ |
| 61 | private function addBranchRuleToTestMapRecursively($testMap, $testDefinition, $map) |
| 62 | { |
| 63 | list($testMapIdentifier, $testDefinitionIdentifier) = array_shift($map); |
| 64 | |
| 65 | foreach ($testMap[$testMapIdentifier] as $id => $testMapSubObject) { |
| 66 | foreach ($testDefinition[$testDefinitionIdentifier] as $testDefinitionSubObject) { |
| 67 | if ($testDefinitionSubObject[self::KEY_ATTRIBUTES][self::KEY_IDENTIFIER] === $id) { |
| 68 | if (count($map) > 0) { |
| 69 | $testMap[$testMapIdentifier][$id] = $this->addBranchRuleToTestMapRecursively( |
| 70 | $testMapSubObject, |
| 71 | $testDefinitionSubObject, |
| 72 | $map |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | $testMap = $this->addBranchRuleToTestMap( |
| 77 | $testMap, |
| 78 | $testDefinitionSubObject, |
| 79 | $testMapIdentifier, |
| 80 | $id |
| 81 | ); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return $testMap; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Adds the branch rule to the testMap if there is branching rule for the given testDefinitionSubObject. |
| 91 | * If there is no branch rule, it adds an empty object instead. |
| 92 | * |
| 93 | * @param array $testMap |
| 94 | * @param array $testDefinitionSubObject |
| 95 | * @param string $testMapIdentifier |
| 96 | * @param string $id |
| 97 | * @return array |
| 98 | */ |
| 99 | private function addBranchRuleToTestMap($testMap, $testDefinitionSubObject, $testMapIdentifier, $id) |
| 100 | { |
| 101 | $testMap[$testMapIdentifier][$id][self::KEY_BRANCH_RULE] = |
| 102 | array_key_exists(self::KEY_BRANCH_RULE, $testDefinitionSubObject) |
| 103 | ? $testDefinitionSubObject[self::KEY_BRANCH_RULE] |
| 104 | : new \stdClass(); |
| 105 | |
| 106 | return $testMap; |
| 107 | } |
| 108 | } |