Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
57.69% |
45 / 78 |
|
33.33% |
3 / 9 |
CRAP | |
0.00% |
0 / 1 |
| IdentifiedElement | |
57.69% |
45 / 78 |
|
33.33% |
3 / 9 |
98.16 | |
0.00% |
0 / 1 |
| setAttribute | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| toArray | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getIdentifier | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| getAttributeValue | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| getAttributeValues | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| setIdentifier | |
41.18% |
7 / 17 |
|
0.00% |
0 / 1 |
16.97 | |||
| validateCurrentIdentifier | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| isIdentifierAvailable | |
72.73% |
8 / 11 |
|
0.00% |
0 / 1 |
6.73 | |||
| generateIdentifier | |
60.00% |
15 / 25 |
|
0.00% |
0 / 1 |
6.60 | |||
| 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\exception\QtiModelException; |
| 26 | use common_Logger; |
| 27 | |
| 28 | /** |
| 29 | * The QTI_Element class represent the abstract model for all the QTI objects. |
| 30 | * It contains all the attributes of the different kind of QTI objects. |
| 31 | * It manages the identifiers and serial creation. |
| 32 | * It provides the serialisation and persistence methods. |
| 33 | * And give the interface for the rendering. |
| 34 | * |
| 35 | * @abstract |
| 36 | * @access public |
| 37 | * @author Sam, <sam@taotesting.com> |
| 38 | * @package taoQTI |
| 39 | |
| 40 | */ |
| 41 | abstract class IdentifiedElement extends Element |
| 42 | { |
| 43 | /** |
| 44 | * It represents the QTI identifier. |
| 45 | * It must be unique string within an item. |
| 46 | * It will generated if it hasn't been set. |
| 47 | * |
| 48 | * @access protected |
| 49 | * @see http://www.imsglobal.org/question/qti_v2p0/imsqti_infov2p0.html#element10541 |
| 50 | * @var string |
| 51 | */ |
| 52 | protected $identifier = ''; |
| 53 | |
| 54 | public function setAttribute($name, $value) |
| 55 | { |
| 56 | if ($name == 'identifier') { |
| 57 | $this->setIdentifier($value); //manage identifier separately, as it is too complicate an attribute |
| 58 | } else { |
| 59 | parent::setAttribute($name, $value); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public function toArray($filterVariableContent = false, &$filtered = []) |
| 64 | { |
| 65 | $data = []; |
| 66 | $data['identifier'] = $this->getIdentifier(); |
| 67 | $data = array_merge($data, parent::toArray($filterVariableContent, $filtered)); |
| 68 | |
| 69 | return $data; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * get the identifier |
| 74 | * |
| 75 | * @access public |
| 76 | * @author Sam, <sam@taotesting.com> |
| 77 | * @return string |
| 78 | */ |
| 79 | public function getIdentifier($generate = true) |
| 80 | { |
| 81 | if (empty($this->identifier) && $generate) { |
| 82 | //try generating an identifier |
| 83 | $relatedItem = $this->getRelatedItem(); |
| 84 | if (!is_null($relatedItem)) { |
| 85 | $this->identifier = $this->generateIdentifier(); |
| 86 | } |
| 87 | } |
| 88 | return (string) $this->identifier; |
| 89 | } |
| 90 | |
| 91 | public function getAttributeValue($name) |
| 92 | { |
| 93 | |
| 94 | $returnValue = null; |
| 95 | |
| 96 | if ($name == 'identifier') { |
| 97 | $returnValue = $this->getIdentifier(); |
| 98 | } else { |
| 99 | $returnValue = parent::getAttributeValue($name); |
| 100 | } |
| 101 | |
| 102 | return $returnValue; |
| 103 | } |
| 104 | |
| 105 | public function getAttributeValues($filterNull = true) |
| 106 | { |
| 107 | |
| 108 | $returnValue = ['identifier' => $this->getIdentifier()]; |
| 109 | $returnValue = array_merge($returnValue, parent::getAttributeValues($filterNull)); |
| 110 | |
| 111 | return $returnValue; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Set a unique identifier. |
| 116 | * If the identifier is already given to another qti element in the same item an InvalidArgumentException is thrown. |
| 117 | * The option collisionFree allows to ensure that a new identifier is generated if a collision happens |
| 118 | * |
| 119 | * @access public |
| 120 | * @author Sam, <sam@taotesting.com> |
| 121 | * @param string id |
| 122 | * @return boolean |
| 123 | */ |
| 124 | public function setIdentifier($identifier, $collisionFree = false) |
| 125 | { |
| 126 | |
| 127 | $returnValue = false; |
| 128 | if (empty($identifier) || is_null($identifier)) { |
| 129 | common_Logger::w('ss'); |
| 130 | throw new \InvalidArgumentException("Id must not be empty"); |
| 131 | } |
| 132 | |
| 133 | if ($this->isIdentifierAvailable($identifier)) { |
| 134 | $returnValue = true; |
| 135 | } else { |
| 136 | if ($collisionFree) { |
| 137 | $identifier = $this->generateIdentifier($identifier); |
| 138 | $returnValue = true; |
| 139 | } else { |
| 140 | $relatedItem = $this->getRelatedItem(); |
| 141 | if (!is_null($relatedItem)) { |
| 142 | $identifiedElements = $relatedItem->getIdentifiedElements(); |
| 143 | } |
| 144 | common_Logger::w("Tried to set non unique identifier " . $identifier, ['TAOITEMS', 'QTI']); |
| 145 | throw new \InvalidArgumentException("The identifier \"{$identifier}\" is already in use"); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if ($returnValue) { |
| 150 | $this->identifier = $identifier; |
| 151 | } |
| 152 | |
| 153 | return $returnValue; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Validate if the current identifier of the qti element does not collide with another one's |
| 158 | * CollisionFree option allows to ensure that no collision subsides after the fonction call. |
| 159 | * It indeed ensures that that the identifier becomes unique if it collides with another one's. |
| 160 | * |
| 161 | * @param boolean $collisionFree |
| 162 | * @return boolean |
| 163 | */ |
| 164 | public function validateCurrentIdentifier($collisionFree = false) |
| 165 | { |
| 166 | |
| 167 | $returnValue = false; |
| 168 | |
| 169 | if (empty($this->identifier)) { |
| 170 | //empty identifier, nothing to check |
| 171 | $returnValue = true; |
| 172 | } else { |
| 173 | $returnValue = $this->setIdentifier($this->identifier, $collisionFree); |
| 174 | } |
| 175 | |
| 176 | return $returnValue; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Check if the given new identifier is valid in the current state of the qti element |
| 181 | * |
| 182 | * @param string $newIdentifier |
| 183 | * @return boolean |
| 184 | * @throws InvalidArgumentException |
| 185 | */ |
| 186 | public function isIdentifierAvailable($newIdentifier) |
| 187 | { |
| 188 | |
| 189 | $returnValue = false; |
| 190 | |
| 191 | if (empty($newIdentifier) || is_null($newIdentifier)) { |
| 192 | throw new InvalidArgumentException("newIdentifier must be set"); |
| 193 | } |
| 194 | |
| 195 | if (!empty($this->identifier) && $newIdentifier == $this->identifier) { |
| 196 | $returnValue = true; |
| 197 | } else { |
| 198 | $relatedItem = $this->getRelatedItem(); |
| 199 | if (is_null($relatedItem)) { |
| 200 | $returnValue = true; //no restriction on identifier since not attached to any qti item |
| 201 | } else { |
| 202 | $idCollection = $relatedItem->getIdentifiedElements(); |
| 203 | $returnValue = !$idCollection->exists($newIdentifier); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return $returnValue; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Create a unique identifier, based on the class if the qti element. |
| 212 | * |
| 213 | * @access protected |
| 214 | * @author Sam, <sam@taotesting.com> |
| 215 | * |
| 216 | * @param string $prefix |
| 217 | * |
| 218 | * @return mixed |
| 219 | * @throws QtiModelException |
| 220 | */ |
| 221 | protected function generateIdentifier($prefix = '') |
| 222 | { |
| 223 | |
| 224 | $relatedItem = $this->getRelatedItem(); |
| 225 | if (is_null($relatedItem)) { |
| 226 | throw new QtiModelException( |
| 227 | 'cannot generate the identifier because the element does not belong to any item' |
| 228 | ); |
| 229 | } |
| 230 | $identifiedElementsCollection = $relatedItem->getIdentifiedElements(); |
| 231 | |
| 232 | $index = 1; |
| 233 | $suffix = ''; |
| 234 | |
| 235 | if (empty($prefix)) { |
| 236 | $clazz = get_class($this); |
| 237 | if (preg_match('/[A-Z]{1}[a-z]*$/', $clazz, $matches)) { |
| 238 | $prefix = $matches[0]; |
| 239 | } else { |
| 240 | $prefix = substr($clazz, strripos($clazz, '_') + 1); |
| 241 | } |
| 242 | $suffix = '_' . $index; |
| 243 | } else { |
| 244 | //detect incremental id of type choice_12, response_3, etc. |
| 245 | $prefix = preg_replace('/_[0-9]+$/', '_', $prefix); |
| 246 | $prefix = preg_replace('/[^a-zA-Z0-9_]/', '_', $prefix); |
| 247 | $prefix = preg_replace('/(_)+/', '_', $prefix); |
| 248 | } |
| 249 | |
| 250 | do { |
| 251 | $exist = false; |
| 252 | $id = $prefix . $suffix; |
| 253 | if ($identifiedElementsCollection->exists($id)) { |
| 254 | $exist = true; |
| 255 | $suffix = '_' . $index; |
| 256 | $index++; |
| 257 | } |
| 258 | } while ($exist); |
| 259 | |
| 260 | return $id; |
| 261 | } |
| 262 | } |