Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
19.15% |
9 / 47 |
|
14.29% |
1 / 7 |
CRAP | |
0.00% |
0 / 1 |
| IdentifierCollection | |
19.15% |
9 / 47 |
|
14.29% |
1 / 7 |
383.28 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| add | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| exists | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| get | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getUnique | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
90 | |||
| addMultiple | |
25.00% |
2 / 8 |
|
0.00% |
0 / 1 |
15.55 | |||
| merge | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 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 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\qti; |
| 24 | |
| 25 | use oat\taoQtiItem\model\qti\IdentifierCollection; |
| 26 | use oat\taoQtiItem\model\qti\IdentifiedElement; |
| 27 | use oat\taoQtiItem\model\qti\exception\QtiModelException; |
| 28 | |
| 29 | /** |
| 30 | * The QTI_Item object represent the assessmentItem. |
| 31 | * It's the main QTI object, it contains all the other objects and is the main |
| 32 | * point |
| 33 | * to render a complete item. |
| 34 | * |
| 35 | * @access public |
| 36 | * @author Sam, <sam@taotesting.com> |
| 37 | * @package taoQTI |
| 38 | * @see http://www.imsglobal.org/question/qti_v2p0/imsqti_infov2p0.html#section10042 |
| 39 | |
| 40 | */ |
| 41 | class IdentifierCollection |
| 42 | { |
| 43 | /** |
| 44 | * The multi dimension array containing identified elements |
| 45 | * array(identifier => serial => oat\taoQtiItem\model\qti\IdentifiedElement) |
| 46 | * |
| 47 | * |
| 48 | */ |
| 49 | protected $elements = []; |
| 50 | |
| 51 | /** |
| 52 | * Short description of method __construct |
| 53 | * |
| 54 | * @access public |
| 55 | * @author Sam, <sam@taotesting.com> |
| 56 | * @param string identifier |
| 57 | * @param array options |
| 58 | * @return mixed |
| 59 | */ |
| 60 | public function __construct($identifiedElements = []) |
| 61 | { |
| 62 | $this->elements = []; |
| 63 | if (is_array($identifiedElements)) { |
| 64 | $this->addMultiple($identifiedElements); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public function add(IdentifiedElement $element) |
| 69 | { |
| 70 | $identifier = $element->getIdentifier(false); |
| 71 | if (!empty($identifier)) { |
| 72 | if (!isset($this->elements[$identifier])) { |
| 73 | $this->elements[$identifier] = []; |
| 74 | } |
| 75 | $this->elements[$identifier][$element->getSerial()] = $element; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public function exists($identifier) |
| 80 | { |
| 81 | |
| 82 | $returnValue = false; |
| 83 | |
| 84 | if (is_string($identifier)) { |
| 85 | $returnValue = isset($this->elements[$identifier]); |
| 86 | } else { |
| 87 | throw new InvalidArgumentException('the identifier must be a string'); |
| 88 | } |
| 89 | |
| 90 | return $returnValue; |
| 91 | } |
| 92 | |
| 93 | public function get($identifier = '') |
| 94 | { |
| 95 | |
| 96 | $returnValue = []; |
| 97 | |
| 98 | if (empty($identifier)) { |
| 99 | $returnValue = $this->elements; |
| 100 | } elseif ($this->exists($identifier)) { |
| 101 | $returnValue = $this->elements[$identifier]; |
| 102 | } |
| 103 | |
| 104 | return $returnValue; |
| 105 | } |
| 106 | |
| 107 | public function getUnique($identifier, $elementClass = '') |
| 108 | { |
| 109 | |
| 110 | $returnValue = null; |
| 111 | |
| 112 | if ($this->exists($identifier)) { |
| 113 | if (empty($elementClass)) { |
| 114 | if (count($this->elements[$identifier]) > 1) { |
| 115 | throw new QtiModelException( |
| 116 | 'More than one identifier found, please try specifying the class of the element' |
| 117 | ); |
| 118 | } elseif (!empty($this->elements[$identifier])) { |
| 119 | $returnValue = reset($this->elements[$identifier]); |
| 120 | } |
| 121 | } else { |
| 122 | $found = []; |
| 123 | foreach ($this->elements[$identifier] as $elt) { |
| 124 | if ($elt instanceof $elementClass) { |
| 125 | $found[] = $elt; |
| 126 | } |
| 127 | } |
| 128 | if (count($found) > 1) { |
| 129 | throw new QtiModelException('More than one identifier found with the class: ' . $elementClass); |
| 130 | } elseif (count($found) == 1) { |
| 131 | $returnValue = reset($found); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return $returnValue; |
| 137 | } |
| 138 | |
| 139 | public function addMultiple($identifiedElements) |
| 140 | { |
| 141 | |
| 142 | if (!is_array($identifiedElements)) { |
| 143 | throw new InvalidArgumentException('the argument "identifiedElements" must be an array'); |
| 144 | } |
| 145 | |
| 146 | foreach ($identifiedElements as $identifiedElement) { |
| 147 | if ($identifiedElement instanceof IdentifiedElement) { |
| 148 | $this->add($identifiedElement); |
| 149 | } elseif (is_array($identifiedElement)) { |
| 150 | $this->addMultiple($identifiedElement); |
| 151 | } else { |
| 152 | throw new InvalidArgumentException('must be either an identifier or an array'); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | public function merge(IdentifierCollection $identifierCollection) |
| 158 | { |
| 159 | |
| 160 | foreach ($identifierCollection->get() as $elements) { |
| 161 | $this->addMultiple($elements); |
| 162 | } |
| 163 | } |
| 164 | } |