Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| QtiTestCompilerIndex | |
0.00% |
0 / 27 |
|
0.00% |
0 / 5 |
342 | |
0.00% |
0 / 1 |
| setItem | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getItem | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| getItemValue | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
30 | |||
| unserialize | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| serialize | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 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) 2016 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * @author Jean-Sébastien Conan <jean-sebastien@taotesting.com> |
| 23 | */ |
| 24 | |
| 25 | namespace oat\taoQtiTest\models; |
| 26 | |
| 27 | use oat\taoItems\model\ItemCompilerIndex; |
| 28 | |
| 29 | /** |
| 30 | * Class QtiTestCompilerIndex |
| 31 | * |
| 32 | * @package oat\taoQtiTest\models |
| 33 | */ |
| 34 | class QtiTestCompilerIndex implements ItemCompilerIndex |
| 35 | { |
| 36 | /** |
| 37 | * The index of compiled items, keys are the items identifiers |
| 38 | * @var array |
| 39 | */ |
| 40 | private $index = []; |
| 41 | |
| 42 | /** |
| 43 | * Stores context info of a compiled Item into the index |
| 44 | * @param string $id |
| 45 | * @param string $language |
| 46 | * @param mixed $data |
| 47 | * @return $this |
| 48 | */ |
| 49 | public function setItem($id, $language, $data) |
| 50 | { |
| 51 | $this->index[$language][$id] = $data; |
| 52 | return $this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Gets context info of a compiled Item. |
| 57 | * |
| 58 | * In case of no compiled item context is found with $language, the implementation |
| 59 | * will try to retrieve a context related to the default installation language. In case of |
| 60 | * no context can be retrieved for the default language, the method returns NULL. |
| 61 | * |
| 62 | * @param string $id |
| 63 | * @param string $language |
| 64 | * @return mixed |
| 65 | */ |
| 66 | public function getItem($id, $language) |
| 67 | { |
| 68 | if (isset($this->index[$language]) && isset($this->index[$language][$id])) { |
| 69 | return $this->index[$language][$id]; |
| 70 | } elseif (isset($this->index[DEFAULT_LANG]) && isset($this->index[DEFAULT_LANG][$id])) { |
| 71 | return $this->index[DEFAULT_LANG][$id]; |
| 72 | } |
| 73 | |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Gets a particular value from context info of a compiled Item. |
| 79 | * |
| 80 | * In case of no value can be found with $language for the given item $id, |
| 81 | * the implementation will try to retrieve a value for the default installation |
| 82 | * language. Otherwise, the method returns NULL. |
| 83 | * |
| 84 | * @param string $id |
| 85 | * @param string $language |
| 86 | * @param string $name |
| 87 | * @return mixed |
| 88 | */ |
| 89 | public function getItemValue($id, $language, $name) |
| 90 | { |
| 91 | $attributes = $this->getItem($id, $language); |
| 92 | if ($attributes && isset($attributes[$name])) { |
| 93 | return $attributes[$name]; |
| 94 | } |
| 95 | |
| 96 | // Try Default Language. |
| 97 | $attributes = $this->getItem($id, DEFAULT_LANG); |
| 98 | if ($attributes && isset($attributes[$name])) { |
| 99 | return $attributes[$name]; |
| 100 | } |
| 101 | |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Unpacks index from a string |
| 107 | * @param string $data |
| 108 | * @param string $language |
| 109 | * @throws \common_exception_InconsistentData |
| 110 | */ |
| 111 | public function unserialize($data, $language = null) |
| 112 | { |
| 113 | if (!is_string($data)) { |
| 114 | throw new \common_exception_InconsistentData('The encoded index data should be provided as a string'); |
| 115 | } |
| 116 | |
| 117 | $index = json_decode($data, true); |
| 118 | |
| 119 | if (!is_array($index)) { |
| 120 | throw new \common_exception_InconsistentData('The decoded index data should be an array'); |
| 121 | } |
| 122 | |
| 123 | if ($language) { |
| 124 | $this->index[$language] = $index; |
| 125 | } else { |
| 126 | $this->index = $index; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Packs the index into a string |
| 132 | * @param string $language |
| 133 | * @return string |
| 134 | */ |
| 135 | public function serialize($language = null) |
| 136 | { |
| 137 | if ($language) { |
| 138 | if (isset($this->index[$language])) { |
| 139 | return json_encode($this->index[$language]); |
| 140 | } else { |
| 141 | return json_encode([]); |
| 142 | } |
| 143 | } else { |
| 144 | return json_encode($this->index); |
| 145 | } |
| 146 | } |
| 147 | } |