Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.15% |
68 / 73 |
|
75.00% |
15 / 20 |
CRAP | |
0.00% |
0 / 1 |
| taoResultServer_models_classes_Variable | |
93.15% |
68 / 73 |
|
75.00% |
15 / 20 |
29.27 | |
0.00% |
0 / 1 |
| getType | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| setIdentifier | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getIdentifier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setCardinality | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
2.00 | |||
| getCardinality | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setBaseType | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getBaseType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setEpoch | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getEpoch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCreationTime | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| isSetEpoch | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isMultiple | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getValue | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| setValue | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| toJson | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| jsonSerialize | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| fromData | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
6 | |||
| validateKeys | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| fromOutcomeVariableData | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| fromResponseVariableData | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| fromTraceVariableData | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setExternallyGraded | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getExternallyGraded | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; under version 2 |
| 9 | * of the License (non-upgradable). |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | * |
| 20 | * Copyright (c) 2013 (original work) Open Assessment Technologies S.A. |
| 21 | * |
| 22 | * @author "Patrick Plichart, <patrick@taotesting.com>" |
| 23 | * |
| 24 | * An Assessment Result is used to report the results of a candidate's interaction |
| 25 | * with a test and/or one or more items attempted. Information about the test is optional, |
| 26 | * in some systems it may be possible to interact with items that are not organized into a |
| 27 | * test at all. For example, items that are organized with learning resources and presented |
| 28 | * individually in a formative context. |
| 29 | */ |
| 30 | |
| 31 | // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace,Squiz.Classes.ValidClassName.NotCamelCaps |
| 32 | abstract class taoResultServer_models_classes_Variable implements JsonSerializable |
| 33 | { |
| 34 | public const CARDINALITY_SINGLE = 'single'; |
| 35 | public const CARDINALITY_MULTIPLE = 'multiple'; |
| 36 | public const CARDINALITY_ORDERED = 'ordered'; |
| 37 | public const CARDINALITY_RECORD = 'record'; |
| 38 | |
| 39 | public const TYPE_VARIABLE_INTEGER = 'integer'; |
| 40 | public const TYPE_VARIABLE_BOOLEAN = 'boolean'; |
| 41 | public const TYPE_VARIABLE_IDENTIFIER = 'identifier'; |
| 42 | public const TYPE_VARIABLE_DURATION = 'duration'; |
| 43 | public const TYPE_VARIABLE_FLOAT = 'float'; |
| 44 | |
| 45 | /** |
| 46 | * The purpose of an itemVariable is to report the value of the item variable with the given identifier. |
| 47 | * |
| 48 | * @var string|null |
| 49 | */ |
| 50 | protected $identifier; |
| 51 | |
| 52 | /** |
| 53 | * The cardinality of the variable, taken from the corresponding declaration or definition. |
| 54 | * |
| 55 | * @var string|null {single, multiple, ordered, record} |
| 56 | */ |
| 57 | protected $cardinality; |
| 58 | |
| 59 | /** |
| 60 | * The base type of the variable, taken from the corresponding declaration of definition. |
| 61 | * This value is omitted only for variables with record cardinality. |
| 62 | * |
| 63 | * @var string|null should move to an enumeration |
| 64 | */ |
| 65 | protected $baseType; |
| 66 | |
| 67 | /** |
| 68 | * The epoch when the variable has been last modified |
| 69 | * |
| 70 | * @var string|null |
| 71 | */ |
| 72 | protected $epoch; |
| 73 | |
| 74 | private bool $isExternallyGraded = false; |
| 75 | |
| 76 | abstract protected function getType(): string; |
| 77 | |
| 78 | public function setIdentifier(string $identifier): self |
| 79 | { |
| 80 | $this->identifier = $identifier; |
| 81 | |
| 82 | return $this; |
| 83 | } |
| 84 | |
| 85 | public function getIdentifier(): ?string |
| 86 | { |
| 87 | return $this->identifier; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @throws common_exception_InvalidArgumentType |
| 92 | */ |
| 93 | public function setCardinality($cardinality = self::CARDINALITY_SINGLE): self |
| 94 | { |
| 95 | if ( |
| 96 | !in_array( |
| 97 | $cardinality, |
| 98 | [ |
| 99 | self::CARDINALITY_SINGLE, |
| 100 | self::CARDINALITY_MULTIPLE, |
| 101 | self::CARDINALITY_ORDERED, |
| 102 | self::CARDINALITY_RECORD, |
| 103 | ], |
| 104 | true |
| 105 | ) |
| 106 | ) { |
| 107 | throw new common_exception_InvalidArgumentType('cardinality'); |
| 108 | } |
| 109 | |
| 110 | $this->cardinality = $cardinality; |
| 111 | |
| 112 | return $this; |
| 113 | } |
| 114 | |
| 115 | public function getCardinality(): ?string |
| 116 | { |
| 117 | return $this->cardinality; |
| 118 | } |
| 119 | |
| 120 | public function setBaseType(string $baseType): self |
| 121 | { |
| 122 | $this->baseType = $baseType; |
| 123 | |
| 124 | return $this; |
| 125 | } |
| 126 | |
| 127 | public function getBaseType(): ?string |
| 128 | { |
| 129 | return $this->baseType; |
| 130 | } |
| 131 | |
| 132 | public function setEpoch(string $epoch): self |
| 133 | { |
| 134 | $this->epoch = $epoch; |
| 135 | |
| 136 | return $this; |
| 137 | } |
| 138 | |
| 139 | public function getEpoch(): ?string |
| 140 | { |
| 141 | return $this->epoch; |
| 142 | } |
| 143 | |
| 144 | public function getCreationTime(): ?float |
| 145 | { |
| 146 | if (!isset($this->epoch)) { |
| 147 | return null; |
| 148 | } |
| 149 | [$usec, $sec] = explode(' ', $this->epoch); |
| 150 | |
| 151 | return ((float)$usec + (float)$sec); |
| 152 | } |
| 153 | |
| 154 | public function isSetEpoch(): bool |
| 155 | { |
| 156 | return (isset($this->epoch)); |
| 157 | } |
| 158 | |
| 159 | public function isMultiple(): bool |
| 160 | { |
| 161 | return in_array($this->cardinality, [self::CARDINALITY_MULTIPLE, self::CARDINALITY_ORDERED], true); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * get the value of the variable |
| 166 | * |
| 167 | * @return mixed |
| 168 | */ |
| 169 | abstract public function getValue(); |
| 170 | |
| 171 | /** |
| 172 | * Set the value of the variable |
| 173 | * |
| 174 | * @param $value mixed |
| 175 | */ |
| 176 | abstract public function setValue($value); |
| 177 | |
| 178 | /** |
| 179 | * @deprecated Use jsonSerialize method instead |
| 180 | */ |
| 181 | public function toJson(): string |
| 182 | { |
| 183 | return json_encode($this); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * {@inheritdoc} |
| 188 | */ |
| 189 | public function jsonSerialize() |
| 190 | { |
| 191 | return [ |
| 192 | 'identifier' => $this->identifier, |
| 193 | 'cardinality' => $this->cardinality, |
| 194 | 'baseType' => $this->baseType, |
| 195 | 'epoch' => $this->epoch, |
| 196 | 'type' => $this->getType(), |
| 197 | 'externallyGraded' => $this->getExternallyGraded(), |
| 198 | ]; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @return taoResultServer_models_classes_OutcomeVariable|taoResultServer_models_classes_ResponseVariable|taoResultServer_models_classes_TraceVariable |
| 203 | * @throws common_exception_InvalidArgumentType |
| 204 | * @throws LogicException |
| 205 | */ |
| 206 | public static function fromData(array $rawVariable) |
| 207 | { |
| 208 | self::validateKeys(['identifier', 'cardinality', 'baseType', 'epoch', 'type'], $rawVariable); |
| 209 | |
| 210 | switch ($rawVariable['type']) { |
| 211 | case taoResultServer_models_classes_OutcomeVariable::TYPE: |
| 212 | $variable = self::fromOutcomeVariableData($rawVariable); |
| 213 | break; |
| 214 | case taoResultServer_models_classes_ResponseVariable::TYPE: |
| 215 | $variable = self::fromResponseVariableData($rawVariable); |
| 216 | break; |
| 217 | case taoResultServer_models_classes_TraceVariable::TYPE: |
| 218 | $variable = self::fromTraceVariableData($rawVariable); |
| 219 | break; |
| 220 | default: |
| 221 | throw new \LogicException(sprintf('Unsupported variable type: %s', $rawVariable['type'])); |
| 222 | } |
| 223 | |
| 224 | if (isset($rawVariable['baseType'])) { |
| 225 | $variable->setBaseType($rawVariable['baseType']); |
| 226 | } |
| 227 | |
| 228 | return $variable |
| 229 | ->setIdentifier($rawVariable['identifier']) |
| 230 | ->setCardinality($rawVariable['cardinality']) |
| 231 | ->setEpoch($rawVariable['epoch']); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @throws LogicException |
| 236 | */ |
| 237 | protected static function validateKeys(array $keys, array $data): void |
| 238 | { |
| 239 | foreach ($keys as $key) { |
| 240 | if (!array_key_exists($key, $data)) { |
| 241 | throw new LogicException(sprintf('Key "%s" is not defined in variable data.', $key)); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | private static function fromOutcomeVariableData( |
| 247 | array $rawOutcomeVariable |
| 248 | ): taoResultServer_models_classes_OutcomeVariable { |
| 249 | self::validateKeys(['normalMinimum', 'normalMaximum', 'value'], $rawOutcomeVariable); |
| 250 | |
| 251 | return (new taoResultServer_models_classes_OutcomeVariable()) |
| 252 | ->setNormalMinimum($rawOutcomeVariable['normalMinimum']) |
| 253 | ->setNormalMaximum($rawOutcomeVariable['normalMaximum']) |
| 254 | ->setEncodedValue($rawOutcomeVariable['value']) |
| 255 | ->setExternallyGraded($rawOutcomeVariable['externallyGraded'] ?? false); |
| 256 | } |
| 257 | |
| 258 | private static function fromResponseVariableData( |
| 259 | array $rawResponseVariable |
| 260 | ): taoResultServer_models_classes_ResponseVariable { |
| 261 | self::validateKeys(['correctResponse', 'candidateResponse'], $rawResponseVariable); |
| 262 | |
| 263 | return (new taoResultServer_models_classes_ResponseVariable()) |
| 264 | ->setCorrectResponse($rawResponseVariable['correctResponse']) |
| 265 | ->setEncodedCandidateResponse($rawResponseVariable['candidateResponse']); |
| 266 | } |
| 267 | |
| 268 | private static function fromTraceVariableData(array $rawTraceVariable): taoResultServer_models_classes_TraceVariable |
| 269 | { |
| 270 | self::validateKeys(['trace'], $rawTraceVariable); |
| 271 | |
| 272 | return (new taoResultServer_models_classes_TraceVariable())->setTrace($rawTraceVariable['trace']); |
| 273 | } |
| 274 | |
| 275 | public function setExternallyGraded(bool $flag): self |
| 276 | { |
| 277 | $this->isExternallyGraded = $flag; |
| 278 | |
| 279 | return $this; |
| 280 | } |
| 281 | |
| 282 | public function getExternallyGraded(): bool |
| 283 | { |
| 284 | return $this->isExternallyGraded; |
| 285 | } |
| 286 | } |