Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       0.00%  | 
       0 / 79  | 
               | 
       0.00%  | 
       0 / 4  | 
       CRAP |         | 
       0.00%  | 
       0 / 1  | 
      
| CommonExpression |         | 
       0.00%  | 
       0 / 79  | 
               | 
       0.00%  | 
       0 / 4  | 
       870 |         | 
       0.00%  | 
       0 / 1  | 
      
| getRule |         | 
       0.00%  | 
       0 / 40  | 
               | 
       0.00%  | 
       0 / 1  | 
       182 | |||
| __construct |         | 
       0.00%  | 
       0 / 2  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| setSubExpressions |         | 
       0.00%  | 
       0 / 1  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| setValue |         | 
       0.00%  | 
       0 / 36  | 
               | 
       0.00%  | 
       0 / 1  | 
       210 | |||
| 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\expression; | 
| 24 | |
| 25 | use oat\taoQtiItem\model\qti\expression\CommonExpression; | 
| 26 | use oat\taoQtiItem\model\qti\expression\Expression; | 
| 27 | use oat\taoQtiItem\model\qti\response\Rule; | 
| 28 | use common_Exception; | 
| 29 | use Exception; | 
| 30 | |
| 31 | /** | 
| 32 | * Short description of class | 
| 33 | * | 
| 34 | * @access public | 
| 35 | * @author Joel Bout, <joel.bout@tudor.lu> | 
| 36 | * @package taoQTI | 
| 37 | |
| 38 | */ | 
| 39 | class CommonExpression extends Expression implements Rule | 
| 40 | { | 
| 41 | // --- ASSOCIATIONS --- | 
| 42 | |
| 43 | |
| 44 | // --- ATTRIBUTES --- | 
| 45 | |
| 46 | /** | 
| 47 | * Short description of attribute subExpressions | 
| 48 | * | 
| 49 | * @access protected | 
| 50 | * @var array | 
| 51 | */ | 
| 52 | protected $subExpressions = []; | 
| 53 | |
| 54 | /** | 
| 55 | * Short description of attribute value | 
| 56 | * | 
| 57 | * @access protected | 
| 58 | */ | 
| 59 | protected $value = null; | 
| 60 | |
| 61 | /** | 
| 62 | * Short description of attribute name | 
| 63 | * | 
| 64 | * @access protected | 
| 65 | * @var string | 
| 66 | */ | 
| 67 | protected $name = ''; | 
| 68 | |
| 69 | /** | 
| 70 | * Short description of attribute attributes | 
| 71 | * | 
| 72 | * @access protected | 
| 73 | * @var array | 
| 74 | */ | 
| 75 | protected $attributes = []; | 
| 76 | |
| 77 | // --- OPERATIONS --- | 
| 78 | |
| 79 | /** | 
| 80 | * Short description of method getRule | 
| 81 | * | 
| 82 | * @access public | 
| 83 | * @author Joel Bout, <joel.bout@tudor.lu> | 
| 84 | * @return string | 
| 85 | */ | 
| 86 | public function getRule() | 
| 87 | { | 
| 88 | $returnValue = (string) ''; | 
| 89 | |
| 90 | |
| 91 | // Get subExpressions | 
| 92 | $subExpressionsRules = []; | 
| 93 | foreach ($this->subExpressions as $subExpression) { | 
| 94 | $subExpressionsRules[] = $subExpression->getRule(); | 
| 95 | } | 
| 96 | $subExpressionsJSON = implode(',', $subExpressionsRules); | 
| 97 | |
| 98 | // Format options | 
| 99 | $optionsJSON = count($this->attributes) ? '"' . addslashes(json_encode($this->attributes)) . '"' : 'null'; | 
| 100 | |
| 101 | // Format rule function of the expression operator | 
| 102 | switch ($this->name) { | 
| 103 | case 'correct': | 
| 104 | $returnValue = 'getCorrect("' . $this->attributes['identifier'] . '")'; | 
| 105 | break; | 
| 106 | case 'mapResponse': | 
| 107 | $identifier = $this->attributes['identifier']; | 
| 108 | $returnValue = 'mapResponse(' | 
| 109 | . $optionsJSON | 
| 110 | . ', getMap("' . $identifier . '"), getResponse("' . $identifier . '"))'; | 
| 111 | break; | 
| 112 | // Multiple is a Creation of List from parameters | 
| 113 | case 'multiple': | 
| 114 | $returnValue = 'createVariable("{\"type\":\"list\"}", ' . $subExpressionsJSON . ')'; | 
| 115 | break; | 
| 116 | // Null is a Creation of empty BaseTypeVariable | 
| 117 | case 'null': | 
| 118 | $returnValue = 'createVariable(null, null)'; | 
| 119 | break; | 
| 120 | // Ordered is a Creation of Tuple from parameters | 
| 121 | case 'ordered': | 
| 122 | $returnValue = 'createVariable("{\"type\":\"tuple\"}", ' . $subExpressionsJSON . ')'; | 
| 123 | break; | 
| 124 | case 'outcome': | 
| 125 | $returnValue = 'getOutcome("' . $this->attributes['identifier'] . '")'; | 
| 126 | break; | 
| 127 | case 'setOutcomeValue': | 
| 128 | //@todo remove this | 
| 129 | throw new common_Exception('setOutcomeValue is not a valid expression'); | 
| 130 | break; | 
| 131 | case 'variable': | 
| 132 | $returnValue = 'getVariable("' . $this->attributes['identifier'] . '")'; | 
| 133 | break; | 
| 134 | |
| 135 | default: | 
| 136 | $returnValue = | 
| 137 | $this->name . '(' | 
| 138 | . $optionsJSON | 
| 139 | . ($subExpressionsJSON != "" ? ', ' . $subExpressionsJSON : '') | 
| 140 | . ')'; | 
| 141 | } | 
| 142 | |
| 143 | |
| 144 | return (string) $returnValue; | 
| 145 | } | 
| 146 | |
| 147 | /** | 
| 148 | * Short description of method __construct | 
| 149 | * | 
| 150 | * @access public | 
| 151 | * @author Joel Bout, <joel.bout@tudor.lu> | 
| 152 | * @param string name | 
| 153 | * @param array attributes | 
| 154 | * @return mixed | 
| 155 | */ | 
| 156 | public function __construct($name, $attributes) | 
| 157 | { | 
| 158 | |
| 159 | $this->name = $name; | 
| 160 | $this->attributes = $attributes; | 
| 161 | } | 
| 162 | |
| 163 | /** | 
| 164 | * Short description of method setSubExpressions | 
| 165 | * | 
| 166 | * @access public | 
| 167 | * @author Joel Bout, <joel.bout@tudor.lu> | 
| 168 | * @param array expressions | 
| 169 | * @return mixed | 
| 170 | */ | 
| 171 | public function setSubExpressions($expressions) | 
| 172 | { | 
| 173 | |
| 174 | $this->subExpressions = $expressions; | 
| 175 | } | 
| 176 | |
| 177 | /** | 
| 178 | * Short description of method setValue | 
| 179 | * | 
| 180 | * @access public | 
| 181 | * @author Joel Bout, <joel.bout@tudor.lu> | 
| 182 | * @param value | 
| 183 | * @return mixed | 
| 184 | */ | 
| 185 | public function setValue($value) | 
| 186 | { | 
| 187 | |
| 188 | |
| 189 | // Set the value of the expression and cast it function of the (defined) base type of the variable | 
| 190 | if ($this->attributes['baseType']) { | 
| 191 | switch ($this->attributes['baseType']) { | 
| 192 | case 'boolean': | 
| 193 | if (is_string($value)) { | 
| 194 | $this->value = (bool)($value == 'true' || $value == '1' ? 1 : 0); | 
| 195 | } elseif (is_bool($value)) { | 
| 196 | $this->value = $value; | 
| 197 | } elseif ($value == null) { | 
| 198 | $this->value = null; | 
| 199 | } else { | 
| 200 | throw new Exception( | 
| 201 | 'taoQTI_models_classes_QTI_response_ExpressionOperator::setValue : an error ' | 
| 202 | . 'occured, the value [' . $value . '] is not a well formed boolean' | 
| 203 | ); | 
| 204 | } | 
| 205 | break; | 
| 206 | case 'float': | 
| 207 | $this->value = (float)$value; | 
| 208 | break; | 
| 209 | case 'integer': | 
| 210 | $this->value = (int)$value; | 
| 211 | break; | 
| 212 | case 'identifier': | 
| 213 | case 'string': | 
| 214 | $this->value = (string)$value; | 
| 215 | break; | 
| 216 | case 'pair': | 
| 217 | $this->value = taoQTI_models_classes_Matching_VariableFactory::createJSONValueFromQTIData( | 
| 218 | $value, | 
| 219 | 'pair' | 
| 220 | ); | 
| 221 | break; | 
| 222 | case 'directedPair': | 
| 223 | $this->value = taoQTI_models_classes_Matching_VariableFactory::createJSONValueFromQTIData( | 
| 224 | $value, | 
| 225 | 'directedPair' | 
| 226 | ); | 
| 227 | break; | 
| 228 | } | 
| 229 | } | 
| 230 | } | 
| 231 | } |