Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 97
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResultsMonitoringDatatable
0.00% covered (danger)
0.00%
0 / 97
0.00% covered (danger)
0.00%
0 / 5
992
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getPayload
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
272
 getResultsByDeliveries
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
132
 doPostProcessing
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 jsonSerialize
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 *
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) 2018 (original work) Public Research Centre Henri Tudor (under the project TAO-SUSTAIN & TAO-DEV);
19 *
20 *
21 */
22
23namespace oat\taoOutcomeUi\model\table;
24
25use oat\tao\helpers\UserHelper;
26use oat\tao\model\datatable\DatatablePayload;
27use oat\tao\model\search\index\IndexDocument;
28use oat\tao\model\search\Search;
29use oat\taoDelivery\model\execution\ServiceProxy;
30use oat\taoDeliveryRdf\model\DeliveryAssemblyService;
31use oat\taoDelivery\model\execution\DeliveryExecutionInterface;
32use oat\taoResultServer\models\classes\ResultManagement;
33use oat\taoResultServer\models\classes\ResultServerService;
34use oat\taoResultServer\models\classes\ResultService;
35use Zend\ServiceManager\ServiceLocatorAwareInterface;
36use Zend\ServiceManager\ServiceLocatorAwareTrait;
37use oat\tao\model\datatable\DatatableRequest;
38
39/**
40 * Class ResultsMonitoringDatatable
41 * @package oat\taoOutcomeUi\model\table
42 */
43class ResultsMonitoringDatatable implements DatatablePayload, ServiceLocatorAwareInterface
44{
45    use ServiceLocatorAwareTrait;
46
47    protected $request;
48
49    protected $results = [];
50
51    /**
52     * ResultsMonitoringDatatable constructor.
53     * @param DatatableRequest $request
54     */
55    public function __construct(DatatableRequest $request)
56    {
57        $this->request = $request;
58        $this->results = [];
59    }
60
61    /**
62     * @throws \common_Exception
63     * @throws \common_exception_NotFound
64     *
65     * @return array
66     */
67    public function getPayload()
68    {
69        $this->results = [
70            'data' => [],
71            'records' => 0,
72        ];
73
74        $page = $this->request->getPage();
75        $limit = $this->request->getRows();
76        $start = $limit * $page - $limit;
77
78        $options = [
79            'order' => $this->request->getSortBy(),
80            'orderdir' => strtoupper($this->request->getSortOrder()),
81            'offset' => $start,
82            'limit' => $limit,
83            'recursive' => true,
84        ];
85
86        $params = \Context::getInstance()->getRequest()->getParameters();
87        $criteria = isset($params['filterquery']) ? $params['filterquery'] : '';
88        $classUri = isset($params['classUri']) ? $params['classUri'] : '';
89
90        $deliveriesArray = [];
91
92        if ($criteria) {
93            /** @var Search $searchService */
94            $searchService = $this->getServiceLocator()->get(Search::SERVICE_ID);
95
96            if ($classUri) {
97                $criteria .= ' AND delivery:"' . $classUri . '"';
98            }
99
100            $resultsArray = $searchService->query($criteria, ResultService::DELIVERY_RESULT_CLASS_URI, $start, $limit);
101
102            /** @var IndexDocument $index */
103            foreach ($resultsArray as $index) {
104                /** @var DeliveryExecutionInterface $execution */
105                $execution = ServiceProxy::singleton()->getDeliveryExecution($index);
106
107                try {
108                    $delivery = $execution->getDelivery();
109
110                    if ($classUri && $delivery->getUri() !== $classUri) {
111                        break;
112                    }
113
114                    try {
115                        $startTime = \tao_helpers_Date::displayeDate($execution->getStartTime());
116                    } catch (\common_exception_NotFound $e) {
117                        \common_Logger::w($e->getMessage());
118                        $startTime = '';
119                    }
120
121                    $user = UserHelper::getUser($execution->getUserIdentifier());
122                    $userName = UserHelper::getUserName($user, true);
123
124                    $this->results['data'][] = [
125                        'id' => $execution->getIdentifier() . '|' . $delivery->getUri(),
126                        'delivery' => $execution->getLabel() ? $execution->getLabel() : $delivery->getLabel(),
127                        'userName' => !empty($userName) ? $userName : $execution->getUserIdentifier(),
128                        'testTakerIdentifier' => $execution->getUserIdentifier(),
129                        'deliveryResultIdentifier' => $execution->getIdentifier(),
130                        'start_time' => $startTime,
131                    ];
132                } catch (\common_exception_NotFound $e) {
133                    if ($classUri && $execution->getIdentifier() !== $classUri) {
134                        break;
135                    }
136
137                    $this->getResultsByDeliveries([$execution->getIdentifier()], $options);
138                }
139            }
140
141            $this->results['records'] = $resultsArray->getTotalCount();
142        } else {
143            /** @var \core_kernel_classes_Resource $delivery */
144            foreach (DeliveryAssemblyService::singleton()->getRootClass()->getInstances(true) as $delivery) {
145                $deliveriesArray[] = $delivery->getUri();
146            }
147
148            if ($deliveriesArray) {
149                $this->getResultsByDeliveries($deliveriesArray, $options);
150            }
151        }
152
153        return $this->doPostprocessing();
154    }
155
156    /**
157     * @param $deliveriesArray
158     * @param array $options
159     *
160     * @throws \common_Exception
161     * @throws \common_exception_Error
162     */
163    protected function getResultsByDeliveries($deliveriesArray, $options = [])
164    {
165        /** @var ResultServerService $resultService */
166        $resultService = $this->getServiceLocator()->get(ResultServerService::SERVICE_ID);
167        $resultsImplementation = $resultService->getResultStorage(null);
168
169        if ($resultsImplementation instanceof ResultManagement) {
170            foreach ($resultsImplementation->getResultByDelivery($deliveriesArray, $options) as $result) {
171                $id = isset($result['deliveryResultIdentifier']) ? $result['deliveryResultIdentifier'] : null;
172                if ($id) {
173                    $deliveryExecution = ServiceProxy::singleton()->getDeliveryExecution($id);
174                    try {
175                        $startTime = \tao_helpers_Date::displayeDate($deliveryExecution->getStartTime());
176                    } catch (\common_exception_NotFound $e) {
177                        \common_Logger::w($e->getMessage());
178                        $startTime = '';
179                    }
180                    $label = '';
181                    try {
182                        $label = $deliveryExecution->getLabel();
183                    } catch (\common_exception_NotFound $e) {
184                        \common_Logger::w($e->getMessage());
185                        if (isset($result['deliveryIdentifier'])) {
186                            $deliveryResource = new \core_kernel_classes_Resource($result['deliveryIdentifier']);
187                            if ($deliveryResource) {
188                                $label = $deliveryResource->getLabel();
189                            }
190                        }
191                    }
192
193                    $testTakerId = $result['testTakerIdentifier'] ? $result['testTakerIdentifier'] : 'TestTaker';
194                    $user = UserHelper::getUser($testTakerId);
195                    $userName = UserHelper::getUserName($user, true);
196
197                    $this->results['data'][] = [
198                        'id' => $id . '|' . $result['deliveryIdentifier'],
199                        'delivery' => $label,
200                        'userName' => !empty($userName) ? $userName : $testTakerId,
201                        'testTakerIdentifier' => $testTakerId,
202                        'deliveryResultIdentifier' => $id,
203                        'start_time' => $startTime,
204                    ];
205                }
206            }
207
208            $this->results['records'] = $resultsImplementation->countResultByDelivery($deliveriesArray);
209        } else {
210            \common_Logger::i('Attempt to read from non-manageable result storage');
211        }
212    }
213
214    /**
215     * @return array
216     */
217    protected function doPostProcessing()
218    {
219        $numberOfRecords = isset($this->results['records']['value'])
220            ? $this->results['records']['value']
221            : $this->results['records'];
222
223        return [
224            'data' => $this->results['data'],
225            'page' => (int) $this->request->getPage(),
226            'records' => (int) count($this->results['data']),
227            'total' => ceil($numberOfRecords / $this->request->getRows()),
228        ];
229    }
230    /**
231     * @return array
232     * @throws \common_Exception
233     * @throws \common_exception_Error
234     */
235    public function jsonSerialize()
236    {
237        return $this->getPayload();
238    }
239}