* @package taoOutcomeUi */ class VariableDataProvider implements tao_models_classes_table_DataProvider { /** * Short description of attribute cache * * @access public * @var array */ public $cache = []; /** * Short description of attribute singleton * * @access public * @var VariableDataProvider */ public static $singleton = null; /** * Short description of method prepare * * @access public * @author Joel Bout, * @param array resources results * @param array columns variables * @return mixed * @throws \common_Exception */ public function prepare($resources, $columns) { $this->cache = []; $resultsService = ResultsService::singleton(); $undefinedStr = __('unknown'); //some data may have not been submitted /** * @var DeliveryExecution $result */ foreach ($resources as $result) { $itemresults = $resultsService->getVariables($result, false); foreach ($itemresults as $itemResultUri => $vars) { // cache the item information pertaining to a given itemResult (stable over time) if ($this->getCache()->has('itemResultItemCache' . $itemResultUri)) { $object = json_decode($this->getCache()->get('itemResultItemCache' . $itemResultUri), true); } else { $object = $resultsService->getItemFromItemResult($itemResultUri); if (is_null($object)) { $object = $resultsService->getVariableFromTest($itemResultUri); } if (! is_null($object)) { $this->getCache()->put(json_encode($object), 'itemResultItemCache' . $itemResultUri); } } if ($object) { if ($object instanceof core_kernel_classes_Resource) { $contextIdentifier = $object->getUri(); } else { $contextIdentifier = $object['uriResource']; } } else { $contextIdentifier = $undefinedStr; } foreach ($vars as $var) { $var = $var[0]; // cache the variable data /** * @var \taoResultServer_models_classes_Variable $varData */ $varData = $var->variable; if ($varData->getBaseType() === 'file') { $decodedFile = Datatypes::decodeFile($varData->getValue()); $varData->setValue($decodedFile['name']); } $variableIdentifier = (string) $varData->getIdentifier(); foreach ($columns as $column) { if ($variableIdentifier == $column->getIdentifier() and $contextIdentifier == $column->getContextIdentifier()) { $epoch = $varData->getEpoch(); $readableTime = ""; if ($epoch != "") { $readableTime = "@" . tao_helpers_Date::displayeDate(tao_helpers_Date::getTimeStamp($epoch), tao_helpers_Date::FORMAT_VERBOSE); } $value = $varData->getValue(); // display the duration in seconds with microseconds if ($column->getIdentifier() === 'duration') { $qtiDuration = new QtiDuration($value); $value = $qtiDuration->getSeconds(true) . '.' . $qtiDuration->getMicroseconds(); } $data = [ $value, $readableTime ]; $this->cache[get_class($varData)][$result][$column->getContextIdentifier() . $variableIdentifier][(string) $epoch] = $data; if ($varData instanceof \taoResultServer_models_classes_ResponseVariable && $varData->getCorrectResponse() !== null) { $this->cache[get_class($varData)][$result][$column->getContextIdentifier() . $variableIdentifier.'_is_correct'][(string) $epoch] = [ (int)$varData->getCorrectResponse(), $readableTime ]; } } } } } } } /** * (non-PHPdoc) * @see tao_models_classes_table_DataProvider::getValue() */ public function getValue(core_kernel_classes_Resource $resource, tao_models_classes_table_Column $column) { $returnValue = []; if (! $column instanceof VariableColumn) { throw new \common_exception_InconsistentData('Unexpected colum type ' . get_class($column) . ' for ' . __CLASS__); } $vcUri = $column->getVariableType(); if (isset($this->cache[$vcUri][$resource->getUri()][$column->getContextIdentifier() . $column->getIdentifier()])) { $returnValue = $this->cache[$vcUri][$resource->getUri()][$column->getContextIdentifier() . $column->getIdentifier()]; } else { common_Logger::d('no data for resource: ' . $resource->getUri() . ' column: ' . $column->getIdentifier()); } return $returnValue; } /** * Get the cache service * * @return \common_cache_Cache */ protected function getCache() { return ServiceManager::getServiceManager()->get(\common_cache_Cache::SERVICE_ID); } }