Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 83
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
VariableDataProvider
0.00% covered (danger)
0.00%
0 / 83
0.00% covered (danger)
0.00%
0 / 5
650
0.00% covered (danger)
0.00%
0 / 1
 prepare
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 1
380
 getValue
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
12
 getCache
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getColumnIdProvider
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getItemResultStrategy
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 * Copyright (c) 2009-2012 (original work) Public Research Centre Henri Tudor (under the project TAO-SUSTAIN & TAO-DEV);
16 *               2012-2022 Open Assessment Technologies SA;
17 *
18 */
19
20namespace oat\taoOutcomeUi\model\table;
21
22use common_Logger;
23use core_kernel_classes_Resource;
24use oat\oatbox\service\ServiceManager;
25use oat\taoDelivery\model\execution\DeliveryExecution;
26use oat\taoOutcomeUi\helper\Datatypes;
27use oat\taoOutcomeUi\model\ItemResultStrategy;
28use oat\taoOutcomeUi\model\ResultsService;
29use oat\taoOutcomeUi\model\table\ColumnDataProvider\ColumnId\VariableColumnIdProvider;
30use oat\taoOutcomeUi\model\table\ColumnDataProvider\ColumnIdProvider;
31use qtism\common\datatypes\QtiDuration;
32use tao_helpers_Date;
33use tao_models_classes_table_Column;
34use tao_models_classes_table_DataProvider;
35
36/**
37 * Short description of class
38 *
39 * @access public
40 * @author Joel Bout, <joel.bout@tudor.lu>
41 * @package taoOutcomeUi
42 */
43class VariableDataProvider implements tao_models_classes_table_DataProvider
44{
45    /**
46     * Short description of attribute cache
47     *
48     * @access public
49     * @var array
50     */
51    public $cache = [];
52
53    /**
54     * Short description of attribute singleton
55     *
56     * @access public
57     * @var VariableDataProvider
58     */
59    public static $singleton = null;
60
61    /**
62     * Short description of method prepare
63     *
64     * @access public
65     * @author Joel Bout, <joel.bout@tudor.lu>
66     * @param array resources results
67     * @param array columns variables
68     * @return mixed
69     * @throws \common_Exception
70     */
71    public function prepare($resources, $columns)
72    {
73        $this->cache = [];
74
75        $resultsService = ResultsService::singleton();
76        $undefinedStr = __('unknown'); //some data may have not been submitted
77        /**
78         * @var DeliveryExecution $result
79         */
80        foreach ($resources as $result) {
81            $itemresults = $resultsService->getVariables($result, false);
82
83            foreach ($itemresults as $itemResultUri => $vars) {
84                // cache the item information pertaining to a given itemResult (stable over time)
85                if ($this->getCache()->has('itemResultItemCache' . $itemResultUri)) {
86                    $object = json_decode($this->getCache()->get('itemResultItemCache' . $itemResultUri), true);
87                } else {
88                    $object = $resultsService->getItemFromItemResult($itemResultUri);
89                    if (is_null($object)) {
90                        $object = $resultsService->getVariableFromTest($itemResultUri);
91                    }
92                    if (! is_null($object)) {
93                        $this->getCache()->put(json_encode($object), 'itemResultItemCache' . $itemResultUri);
94                    }
95                }
96                if ($object) {
97                    if ($object instanceof core_kernel_classes_Resource) {
98                        $contextIdentifier = $object->getUri();
99                    } else {
100                        $contextIdentifier = $object['uriResource'];
101                    }
102                } else {
103                    $contextIdentifier = $undefinedStr;
104                }
105                foreach ($vars as $var) {
106                    $var = $var[0];
107                    // cache the variable data
108                    /**
109                     * @var \taoResultServer_models_classes_Variable $varData
110                     */
111                    $varData = $var->variable;
112
113                    if ($varData->getBaseType() === 'file') {
114                        $decodedFile = Datatypes::decodeFile($varData->getValue());
115                        $varData->setValue($decodedFile['name']);
116                    }
117                    $callIdItem = $var->callIdItem;
118                    $variableIdentifier = (string) $varData->getIdentifier();
119
120                    foreach ($columns as $column) {
121                        if (
122                            $variableIdentifier == $column->getIdentifier()
123                            && $contextIdentifier == $column->getContextIdentifier()
124                            && (
125                                $this->getItemResultStrategy()->isItemEntityBased()
126                                || strpos($callIdItem, $column->getRefId()) !== false
127                            )
128                        ) {
129                            $epoch = $varData->getEpoch();
130                            $readableTime = "";
131                            if ($epoch != "") {
132                                $readableTime = sprintf(
133                                    "@%s",
134                                    tao_helpers_Date::displayeDate(
135                                        tao_helpers_Date::getTimeStamp($epoch),
136                                        tao_helpers_Date::FORMAT_VERBOSE
137                                    )
138                                );
139                            }
140
141                            $value = $varData->getValue();
142
143                            // display the duration in seconds with microseconds
144                            if ($column->getIdentifier() === 'duration') {
145                                $qtiDuration = new QtiDuration($value);
146                                $value = $qtiDuration->getSeconds(true) . '.' . $qtiDuration->getMicroseconds();
147                            }
148
149                            $data = [
150                                $value,
151                                $readableTime
152                            ];
153                            $columnIdProvider = $this->getColumnIdProvider();
154                            $columnId = $columnIdProvider->provide($column);
155                            $this->cache[get_class($varData)][$result][$columnId][(string)$epoch] = $data;
156
157                            if (
158                                $varData instanceof \taoResultServer_models_classes_ResponseVariable
159                                && $varData->getCorrectResponse() !== null
160                            ) {
161                                $correctColumnId = $columnId . '_is_correct';
162                                $this->cache[get_class($varData)][$result][$correctColumnId][(string)$epoch] = [
163                                    (int)$varData->getCorrectResponse(),
164                                    $readableTime
165                                ];
166                            }
167                        }
168                    }
169                }
170            }
171        }
172    }
173
174    /**
175     * (non-PHPdoc)
176     * @see tao_models_classes_table_DataProvider::getValue()
177     */
178    public function getValue(core_kernel_classes_Resource $resource, tao_models_classes_table_Column $column)
179    {
180        $returnValue = [];
181
182        if (!$column instanceof VariableColumn) {
183            throw new \common_exception_InconsistentData(
184                sprintf(
185                    'Unexpected colum type %s for %s',
186                    get_class($column),
187                    __CLASS__
188                )
189            );
190        }
191
192        $vcUri = $column->getVariableType();
193        $columnIdProvider = $this->getColumnIdProvider();
194        if (isset($this->cache[$vcUri][$resource->getUri()][$columnIdProvider->provide($column)])) {
195            $returnValue = $this->cache[$vcUri][$resource->getUri()][$columnIdProvider->provide($column)];
196        } else {
197            common_Logger::d(
198                sprintf(
199                    'no data for resource: %s column: %s',
200                    $resource->getUri(),
201                    $column->getIdentifier()
202                )
203            );
204        }
205
206        return $returnValue;
207    }
208
209    /**
210     * Get the cache service
211     *
212     * @return \common_cache_Cache
213     */
214    protected function getCache()
215    {
216        return ServiceManager::getServiceManager()->get(\common_cache_Cache::SERVICE_ID);
217    }
218
219    public function getColumnIdProvider(): ColumnIdProvider
220    {
221        return ServiceManager::getServiceManager()->getContainer()->get(ColumnIdProvider::class);
222    }
223
224    public function getItemResultStrategy(): ItemResultStrategy
225    {
226        return ServiceManager::getServiceManager()->getContainer()->get(ItemResultStrategy::class);
227    }
228}