Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
AssessmentResultsService
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 9
420
0.00% covered (danger)
0.00%
0 / 1
 getTestTakerData
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getTestData
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getResultsData
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 getDeliveryData
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getPrintableRubric
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
42
 getScoreReportUrlParts
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getResultService
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 propertiesToArray
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 variablesToArray
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
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) 2015 (original work) Open Assessment Technologies SA;
19 *
20 *
21 */
22
23namespace oat\taoProctoring\model;
24
25use oat\generis\model\OntologyRdfs;
26use oat\oatbox\service\ConfigurableService;
27use oat\oatbox\service\ServiceManager;
28use oat\taoDelivery\model\execution\DeliveryExecution as DeliveryExecutionInterface;
29use oat\taoDeliveryRdf\model\DeliveryAssemblyService;
30use oat\taoOutcomeUi\model\ResultsService;
31use oat\taoOutcomeUi\model\Wrapper\ResultServiceWrapper;
32use oat\taoProctoring\model\implementation\TestSessionService;
33use qtism\data\View;
34use taoQtiTest_models_classes_QtiTestService;
35
36/**
37 * Class AssessmentResultsService
38 *
39 * Service to retrieve session data such as results, test taker data, execution time and so on.
40 *
41 * @package oat\taoProctoring\model
42 * @author Aleh Hutnikau <hutnikau@1pt.com>
43 */
44class AssessmentResultsService extends ConfigurableService
45{
46    /** @deprecated */
47    public const CONFIG_ID = 'taoProctoring/AssessmentResults';
48
49    public const SERVICE_ID = 'taoProctoring/AssessmentResults';
50
51    public const OPTION_PRINTABLE_RUBRIC_TAG = 'printable_rubric_tag';
52    public const OPTION_PRINT_REPORT_BUTTON = 'print_report_button';
53
54    public const OPTION_SCORE_URL = 'score_report_url';
55
56    /**
57     * Get test taker data as associative array
58     * @param DeliveryExecutionInterface $deliveryExecution
59     * @return array
60     */
61    public function getTestTakerData(DeliveryExecutionInterface $deliveryExecution)
62    {
63        $data = $this
64            ->getResultService($deliveryExecution->getDelivery())
65            ->getTestTakerData($deliveryExecution->getIdentifier());
66        $result = $this->propertiesToArray($data);
67        return $result;
68    }
69
70    /**
71     * Get test data as associative array
72     * @param DeliveryExecutionInterface $deliveryExecution
73     * @return array
74     * @throws \common_Exception
75     * @throws \common_exception_InvalidArgumentType
76     * @throws \common_exception_NotFound
77     * @throws \core_kernel_classes_EmptyProperty
78     */
79    public function getTestData(DeliveryExecutionInterface $deliveryExecution)
80    {
81        $resultService = $this->getResultService($deliveryExecution->getDelivery());
82        $testResource = DeliveryAssemblyService::singleton()->getOrigin($deliveryExecution->getDelivery());
83        $propValues = $testResource->getPropertiesValues(array(
84            OntologyRdfs::RDFS_LABEL,
85        ));
86        $result = $this->propertiesToArray($propValues);
87
88        $deliveryVariables = $resultService->getVariableDataFromDeliveryResult($deliveryExecution->getIdentifier());
89        $result = array_merge($result, $this->variablesToArray($deliveryVariables));
90
91        return $result;
92    }
93
94    /**
95     * Get session results
96     * @param DeliveryExecutionInterface $deliveryExecution
97     * @return array
98     */
99    public function getResultsData(DeliveryExecutionInterface $deliveryExecution)
100    {
101        $result = [];
102        $resultService = $this->getResultService($deliveryExecution->getDelivery());
103
104        $itemsData = $resultService->getItemVariableDataFromDeliveryResult(
105            $deliveryExecution->getIdentifier(),
106            'lastSubmitted'
107        );
108
109        foreach ($itemsData as $itemData) {
110            $rawResult = [];
111            $rawResult['label'] = $itemData['label'];
112            foreach ($itemData['sortedVars'] as $variables) {
113                $variableValues = array_map(function ($variable) {
114                    $variable = current($variable);
115                    return $variable['var']->getValue();
116                }, $variables);
117                $rawResult = array_merge($rawResult, $variableValues);
118            };
119            $result[] = $rawResult;
120        }
121        return $result;
122    }
123
124    /**
125     * Get delivery execution data
126     * @param DeliveryExecutionInterface $deliveryExecution
127     * @return array
128     */
129    public function getDeliveryData(DeliveryExecutionInterface $deliveryExecution)
130    {
131        $result = [
132            'start' => $deliveryExecution->getStartTime(),
133            'end' => $deliveryExecution->getFinishTime(),
134            'label' => $deliveryExecution->getLabel()
135        ];
136        return $result;
137    }
138
139
140    /**
141     * Get rubric to be printed
142     * Rubric is considered printed if it included to the section which has an item tagged by specified tag
143     * (@see )
144     *
145     * @param DeliveryExecutionInterface $deliveryExecution
146     * @return string
147     */
148    public function getPrintableRubric(DeliveryExecutionInterface $deliveryExecution)
149    {
150        $testSessionService = ServiceManager::getServiceManager()->get(TestSessionService::SERVICE_ID);
151        $session = $testSessionService->getTestSession($deliveryExecution);
152
153        $inputParameters = $testSessionService->getRuntimeInputParameters($deliveryExecution);
154        $testDefinition = \taoQtiTest_helpers_Utils::getTestDefinition($inputParameters['QtiTestCompilation']);
155        $sections = $testDefinition->getComponentsByClassName('assessmentSection');
156
157        $tag = $this->getOption(self::OPTION_PRINTABLE_RUBRIC_TAG);
158
159        $directoryIds = explode('|', $inputParameters['QtiTestCompilation']);
160        $compilationDirs = array(
161            'private' => \tao_models_classes_service_FileStorage::singleton()->getDirectoryById($directoryIds[0]),
162            'public' => \tao_models_classes_service_FileStorage::singleton()->getDirectoryById($directoryIds[1]),
163        );
164
165        $rubrics = [];
166
167        // -- variables used in the included rubric block templates.
168        // base path (base URI to be used for resource inclusion).
169        $basePathVarName = taoQtiTest_models_classes_QtiTestService::TEST_BASE_PATH_NAME;
170        $$basePathVarName = $compilationDirs['public']->getPublicAccessUrl();
171
172        // state name (the variable to access to get the state of the assessmentTestSession).
173        $stateName = taoQtiTest_models_classes_QtiTestService::TEST_RENDERING_STATE_NAME;
174        $$stateName = $session;
175
176        // views name (the variable to be accessed for the visibility of rubric blocks).
177        $viewsName = taoQtiTest_models_classes_QtiTestService::TEST_VIEWS_NAME;
178        $$viewsName = array(View::CANDIDATE);
179
180        foreach ($sections as $section) {
181            $assessmentItemsRef = $section->getComponentsByClassName('assessmentItemRef');
182            foreach ($assessmentItemsRef as $item) {
183                foreach ($item->getCategories() as $category) {
184                    if ($category === $tag) {
185                        foreach ($section->getRubricBlockRefs() as $rubric) {
186                            ob_start();
187                            include $compilationDirs['private']->getPath() . '/' . ltrim($rubric->getHref(), './\\');
188                            $rubrics[] = ob_get_clean();
189                        }
190                    }
191                };
192            };
193        }
194
195        return $rubrics;
196    }
197
198    /**
199     * Get the configurable url parts or default url to proctoring
200     */
201    public function getScoreReportUrlParts()
202    {
203        return (array) $this->getOption(self::OPTION_SCORE_URL);
204    }
205
206    /**
207     * Get result service instance.
208     * @param \core_kernel_classes_Resource $delivery
209     * @return ResultsService
210     */
211    protected function getResultService(\core_kernel_classes_Resource $delivery)
212    {
213        /**
214         * @var $ResultServiceWrapper ResultServiceWrapper
215         */
216        $ResultServiceWrapper = $this->getServiceManager()->get(ResultServiceWrapper::SERVICE_ID);
217
218        $resultsService = $ResultServiceWrapper->getService();
219        $implementation = $resultsService->getReadableImplementation($delivery);
220        $resultsService->setImplementation($implementation);
221        return $resultsService;
222    }
223
224    /**
225     * @param array $properties
226     * @return array
227     */
228    protected function propertiesToArray($properties)
229    {
230        $result = [];
231        foreach ($properties as $uri => $item) {
232            $resource = new \core_kernel_classes_Resource($uri);
233            if ($resource->exists() && isset($item[0])) {
234                $result[$resource->getLabel()] = (string) $item[0];
235            }
236        }
237        return $result;
238    }
239
240    /**
241     * @param array $variables list of variables
242     * @return array
243     */
244    protected function variablesToArray($variables)
245    {
246        $result = [];
247        foreach ($variables as $variable) {
248            $result[$variable->getIdentifier()] = $variable->getValue();
249        }
250        return $result;
251    }
252}