public function process()
    {
        $state = $this->getState();
        $variableIdentifier = $this->getExpression()->getIdentifier();
        $weightIdentifier = $this->getExpression()->getWeightIdentifier();

        $variable = $state->getVariable($variableIdentifier);
        if (empty($variable)) {
            return null;
        }

        $variableValue = $state[$variableIdentifier];

        if ($variable->isNull()) {
            return $variableValue; // Even if empty string, it is considered by QTI as null.
        }

        // We have a value for this variable, is it weighted?
        if ($state instanceof AssessmentTestSession) {

            try {
                $vIdentifier = new VariableIdentifier($variableIdentifier);
                if ($vIdentifier->hasPrefix() === true && empty($weightIdentifier) === false) {
                    $weight = $state->getWeight($vIdentifier->getPrefix() . '.' . $weightIdentifier);
                    $baseType = $variableValue->getBaseType();
                    $cardinality = $variableValue->getCardinality();

                    // From IMS QTI:
                    // Weights only apply to item variables with base types integer and float.
                    // If the item variable is of any other type the weight is ignored.
                    if (!empty($weight) && ($baseType === BaseType::INTEGER || $baseType === BaseType::FLOAT)) {

                        if ($cardinality === Cardinality::SINGLE) {
                            return new QtiFloat($variableValue->getValue() * $weight->getValue());
                        } elseif ($cardinality === Cardinality::MULTIPLE || $cardinality === Cardinality::ORDERED) {

                            // variableValue is an object, the weighting should not
                            // affect the content of the state so a new container is created.
                            $finalValue = ($cardinality === Cardinality::MULTIPLE) ? new MultipleContainer(BaseType::FLOAT) : new OrderedContainer(BaseType::FLOAT);
                            for ($i = 0; $i < count($variableValue); $i++) {
                                if ($variableValue[$i] !== null) {
                                    $finalValue[] = new QtiFloat($variableValue[$i]->getValue() * $weight->getValue()) ;
                                } else {
                                    $finalValue[] = null;
                                }
                            }

                            return $finalValue;
                        }
                    } else {
                        return $variableValue;
                    }
                } else {
                    return $variableValue;
                }
            } catch (InvalidArgumentException $e) {
                // Invalid $variableIdentifier.
                $msg = "Invalid identifier '${variableIdentifier}' given for variable identifier.";
                throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::NONEXISTENT_VARIABLE, $e);
            }

        } else {
            return $variableValue;
        }
    }
public function process()
    {
        $state = $this->getState();
        $variableIdentifier = $this->getExpression()->getIdentifier();
        $weightIdentifier = $this->getExpression()->getWeightIdentifier();

        $variable = $state->getVariable($variableIdentifier);
        if (empty($variable)) {
            return null;
        }

        $variableValue = $state[$variableIdentifier];

        if ($variable->isNull()) {
            return $variableValue; // Even if empty string, it is considered by QTI as null.
        }

        // We have a value for this variable, is it weighted?
        if ($state instanceof AssessmentTestSession) {

            try {
                $vIdentifier = new VariableIdentifier($variableIdentifier);
                if ($vIdentifier->hasPrefix() === true && empty($weightIdentifier) === false) {
                    $weight = $state->getWeight($vIdentifier->getPrefix() . '.' . $weightIdentifier);
                    $baseType = $variableValue->getBaseType();
                    $cardinality = $variableValue->getCardinality();

                    // From IMS QTI:
                    // Weights only apply to item variables with base types integer and float.
                    // If the item variable is of any other type the weight is ignored.
                    if (!empty($weight) && ($baseType === BaseType::INTEGER || $baseType === BaseType::FLOAT)) {

                        if ($cardinality === Cardinality::SINGLE) {
                            return new QtiFloat($variableValue->getValue() * $weight->getValue());
                        } elseif ($cardinality === Cardinality::MULTIPLE || $cardinality === Cardinality::ORDERED) {

                            // variableValue is an object, the weighting should not
                            // affect the content of the state so a new container is created.
                            $finalValue = ($cardinality === Cardinality::MULTIPLE) ? new MultipleContainer(BaseType::FLOAT) : new OrderedContainer(BaseType::FLOAT);
                            for ($i = 0; $i < count($variableValue); $i++) {
                                if ($variableValue[$i] !== null) {
                                    $finalValue[] = new QtiFloat($variableValue[$i]->getValue() * $weight->getValue()) ;
                                } else {
                                    $finalValue[] = null;
                                }
                            }

                            return $finalValue;
                        }
                    } else {
                        return $variableValue;
                    }
                } else {
                    return $variableValue;
                }
            } catch (InvalidArgumentException $e) {
                // Invalid $variableIdentifier.
                $msg = "Invalid identifier '${variableIdentifier}' given for variable identifier.";
                throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::NONEXISTENT_VARIABLE, $e);
            }

        } else {
            return $variableValue;
        }
    }
public function process ( ) { $ state = $ this -> getState ( ) ; $ variableIdentifier = $ this -> getExpression ( ) -> getIdentifier ( ) ; $ weightIdentifier = $ this -> getExpression ( ) -> getWeightIdentifier ( ) ; $ variable = $ state -> getVariable ( $ variableIdentifier ) ; if ( empty ( $ variable ) ) { return null ; } $ variableValue = $ state [ $ variableIdentifier ] ; if ( $ variable -> isNull ( ) ) { return $ variableValue ; // Even if empty string, it is considered by QTI as null. } // We have a value for this variable, is it weighted? if ( $ state instanceof AssessmentTestSession ) { try { $ vIdentifier = new VariableIdentifier ( $ variableIdentifier ) ; if ( $ vIdentifier -> hasPrefix ( ) === true && empty ( $ weightIdentifier ) === false ) { $ weight = $ state -> getWeight ( $ vIdentifier -> getPrefix ( ) . '.' . $ weightIdentifier ) ; $ baseType = $ variableValue -> getBaseType ( ) ; $ cardinality = $ variableValue -> getCardinality ( ) ; // From IMS QTI: // Weights only apply to item variables with base types integer and float. // If the item variable is of any other type the weight is ignored. if ( ! empty ( $ weight ) && ( $ baseType === BaseType :: INTEGER || $ baseType === BaseType :: FLOAT ) ) { if ( $ cardinality === Cardinality :: SINGLE ) { return new QtiFloat ( $ variableValue -> getValue ( ) * $ weight -> getValue ( ) ) ; } elseif ( $ cardinality === Cardinality :: MULTIPLE || $ cardinality === Cardinality :: ORDERED ) { // variableValue is an object, the weighting should not // affect the content of the state so a new container is created. $ finalValue = ( $ cardinality === Cardinality :: MULTIPLE ) ? new MultipleContainer ( BaseType :: FLOAT ) : new OrderedContainer ( BaseType :: FLOAT ) ; for ( $ i = 0 ; $ i < count ( $ variableValue ) ; $ i ++ ) { if ( $ variableValue [ $ i ] !== null ) { $ finalValue [ ] = new QtiFloat ( $ variableValue [ $ i ] -> getValue ( ) * $ weight -> getValue ( ) ) ; } else { $ finalValue [ ] = null ; } } return $ finalValue ; } } else { return $ variableValue ; } } else { return $ variableValue ; } } catch ( InvalidArgumentException $ e ) { // Invalid $variableIdentifier. $ msg = "Invalid identifier '${variableIdentifier}' given for variable identifier." ; throw new ExpressionProcessingException ( $ msg , $ this , ExpressionProcessingException :: NONEXISTENT_VARIABLE , $ e ) ; } } else { return $ variableValue ; } }
Process the Variable expression.

* If the requested variable does not exist, NULL is returned.
* In a test context, if the requested weight does not exist, the raw value of the variable is returned.

@returns null|mixed The value of the target variable or NULL if the variable does not exist.
@throws \qtism\runtime\expressions\ExpressionProcessingException
Process the Variable expression .