/**
  * Add a variable to be transformed into its IMS PCI JSON Representation
  * for a later output.
  * 
  * @param Variable $variable
  */
 public function addVariable(Variable $variable)
 {
     $output =& $this->getOutput();
     $varName = $variable->getIdentifier();
     $marshaller = new taoQtiCommon_helpers_PciJsonMarshaller();
     $output[$varName] = $marshaller->marshall($variable->getValue(), taoQtiCommon_helpers_PciJsonMarshaller::MARSHALL_ARRAY);
 }
 /**
  * Get the intrinsic values of a given QTI $variable.
  * 
  * @param Variable $variable
  * @return array
  */
 public static function getVariableValues(Variable $variable)
 {
     $returnValue = array();
     $baseType = $variable->getBaseType();
     $cardinalityType = $variable->getCardinality();
     $value = $variable->getValue();
     // This only works if the variable has a value ;)
     if ($value !== null) {
         if ($baseType === BaseType::IDENTIFIER) {
             if ($cardinalityType === Cardinality::SINGLE) {
                 $returnValue[] = $value->getValue();
             } else {
                 if ($cardinalityType === Cardinality::MULTIPLE) {
                     foreach ($variable->getValue() as $value) {
                         $returnValue[] = $value->getValue();
                     }
                 }
             }
         }
     }
     return $returnValue;
 }
 /**
  * Processes all values of a record container and merge them into
  * a single string.
  * 
  * @param RecordContainer $variable The record to process.
  * @return string All the key/values delimited by printedVariable->delimiter. Indicator between keys and values is defined by printedVariable->mappingIndicator.
  */
 private function processRecord(Variable $variable)
 {
     $processedValues = array();
     $baseType = $variable->getBaseType();
     $mappingIndicator = $this->getComponent()->getMappingIndicator();
     foreach ($variable->getValue() as $k => $v) {
         $processedValues[] = "{$k}{$mappingIndicator}" . $this->processValue(Utils::inferBaseType($v), $v);
     }
     return implode($this->getComponent()->getDelimiter(), $processedValues);
 }
 /**
  * Whether or not a given QTI Variable contains a QTI File value.
  * 
  * @param Variable $variable
  * @param boolean $considerNull Whether or not to consider File variables containing NULL variables.
  * @return boolean
  */
 public static function isQtiFile(Variable $variable, $considerNull = true)
 {
     $correctBaseType = $variable->getBaseType() === BaseType::FILE;
     $correctCardinality = $variable->getCardinality() === Cardinality::SINGLE;
     $nullConsideration = $considerNull === true ? true : $variable->getValue() !== null;
     return $correctBaseType === true && $correctCardinality === true && $nullConsideration === true;
 }
 /**
  * Write the value of $variable in the current binary stream.
  * 
  * @param Variable $variable A QTI Runtime Variable object.
  * @throws QtiBinaryStreamAccessException
  */
 public function writeVariableValue(Variable $variable)
 {
     try {
         $value = $variable->getValue();
         $cardinality = $variable->getCardinality();
         $baseType = $variable->getBaseType();
         if (is_null($value) === true) {
             $this->writeBoolean(true);
             return;
         }
         // Non-null value.
         $this->writeBoolean(false);
         if ($cardinality === Cardinality::RECORD) {
             // is-scalar
             $this->writeBoolean(false);
             // count
             $this->writeShort(count($value));
             // content
             foreach ($value as $k => $v) {
                 $this->writeRecordField(array($k, $v), is_null($v));
             }
         } else {
             $toCall = 'write' . ucfirst(BaseType::getNameByConstant($baseType));
             if ($cardinality === Cardinality::SINGLE) {
                 // is-scalar
                 $this->writeBoolean(true);
                 // content
                 $this->{$toCall}($value instanceof Scalar ? $value->getValue() : $value);
             } else {
                 // is-scalar
                 $this->writeBoolean(false);
                 // count
                 $this->writeShort(count($value));
                 // MULTIPLE or ORDERED
                 foreach ($value as $v) {
                     if (is_null($v) === false) {
                         $this->writeBoolean(false);
                         $this->{$toCall}($v instanceof Scalar ? $v->getValue() : $v);
                     } else {
                         $this->writeBoolean(true);
                     }
                 }
             }
         }
     } catch (BinaryStreamAccessException $e) {
         $msg = "An error occured while writing a Variable value.";
         throw new QtiBinaryStreamAccessException($msg, $this, QtiBinaryStreamAccessException::VARIABLE, $e);
     }
 }
 /**
  * Add a variable to the State that has to be built for the client-side. The final output
  * array is available through the tao_helpers_StateOutput::getOutput() method.
  * 
  * @param Variable $variable A QTI Runtime Variable
  */
 public function addVariable(Variable $variable)
 {
     $output =& $this->getOutput();
     $baseType = $variable->getBaseType();
     $cardinality = $variable->getCardinality();
     $varName = $variable->getIdentifier();
     if ($cardinality === Cardinality::SINGLE) {
         $singleValue = $variable->getValue();
         if (is_null($singleValue) === true) {
             $output[$varName] = array('');
             return;
         }
         $values = array($variable->getValue());
     } else {
         $containerValue = $variable->getValue();
         if (is_null($containerValue) === true || $containerValue->isNull() === true) {
             $output[$varName] = array();
             return;
         }
         $values = $containerValue->getArrayCopy(true);
     }
     $output[$varName] = array();
     foreach ($values as $val) {
         if (is_null($val) === true) {
             $output[$varName][] = '';
         } else {
             if (gettype($val) === 'boolean') {
                 $output[$varName][] = $val === true ? 'true' : 'false';
             } else {
                 if ($val instanceof QtiPoint) {
                     $output[$varName][] = array('' . $val->getX(), '' . $val->getY());
                 } else {
                     if ($val instanceof QtiPair) {
                         $output[$varName][] = array($val->getFirst(), $val->getSecond());
                     } else {
                         if (gettype($val) === 'object') {
                             $output[$varName][] = $val->__toString();
                         } else {
                             $output[$varName][] = '' . $val;
                         }
                     }
                 }
             }
         }
     }
 }