Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
TestSessionMetaData
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 7
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 save
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
72
 getData
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getVariable
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getTestSession
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getItemUri
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getServiceManager
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) 2015 (original work) Open Assessment Technologies SA;
19 *
20 */
21
22namespace oat\taoQtiTest\models;
23
24use oat\oatbox\service\ServiceManager;
25use oat\taoDelivery\model\execution\DeliveryServerService;
26use qtism\data\AssessmentItemRef;
27use qtism\runtime\tests\AssessmentTestSession;
28use qtism\common\enums\Cardinality;
29use Context;
30use taoResultServer_models_classes_TraceVariable;
31use qtism\runtime\tests\RouteItem;
32
33/**
34 * Class manages test session metadata such as section or test exit codes and other.
35 *
36 * Data will be stored as trace variable {@link \taoResultServer_models_classes_TraceVariable}.
37 *
38 * Section level data stored as test variable
39 * {@link \taoResultServer_models_classes_ResultServerStateFull::storeTestVariable()}
40 * prefixed with session identifier e.g. <i>SECTION_EXIT_CODE</i> will be stored as
41 * <i>SECTION_EXIT_CODE_assessmentSection-1</i>
42 *
43 *
44 * Usage example:
45 * <pre>
46 * $sessionMetaData = new TestSessionMetaData($session);
47 * $metaData = array(
48 *   //Test level metadata
49 *   'TEST' => array(
50 *      'TEST_EXIT_CODE' => TEST_CODE_COMPLETE,
51 *   ),
52 *   //Section level metadata
53 *   'SECTION' => array(
54 *      'SECTION_EXIT_CODE' => SECTION_CODE_COMPLETED_NORMALLY,
55 *   ),
56 *   //Item level metadata
57 *   'ITEM' => array( //save item level metadata
58 *      'ITEM_META_DATA' => 'value',
59 *   ),
60 * )
61 * $sessionMetaData->save($metaData);
62 * </pre>
63 *
64 * @author Aleh Hutnikau <hutnikau@1pt.com>
65 *
66 */
67class TestSessionMetaData
68{
69    /**
70     * Test session instance
71     * @var AssessmentTestSession
72     */
73    private $session;
74
75    /**
76     * Constructor.
77     * @param \taoQtiTest_helpers_TestSession $session Test session instance.
78     */
79    public function __construct(\taoQtiTest_helpers_TestSession $session)
80    {
81        $this->session = $session;
82    }
83
84    /**
85     * Save session metadata.
86     *
87     * @param array $metaData Meta data array to be saved.
88     * @param RouteItem $routeItem item for which data will be saved
89     * @param string $assessmentSectionId section id for which data will be saved
90     * Example:
91     * array(
92     *   'TEST' => array('TEST_EXIT_CODE' => 'IC'),
93     *   'SECTION' => array('SECTION_EXIT_CODE' => 701),
94     * )
95     */
96    public function save(array $metaData, RouteItem $routeItem = null, $assessmentSectionId = null)
97    {
98        $testUri = $this->session->getTest()->getUri();
99        /** @var DeliveryServerService $deliveryServerService */
100        $deliveryServerService = $this->getServiceManager()->get(DeliveryServerService::SERVICE_ID);
101
102
103        foreach ($metaData as $type => $data) {
104            foreach ($data as $key => $value) {
105                $metaVariable = $this->getVariable($key, $value);
106                $sessionId = $this->session->getSessionId();
107
108                $resultStore = $deliveryServerService->getResultStoreWrapper($sessionId);
109
110                if (strcasecmp($type, 'ITEM') === 0) {
111                    if ($routeItem === null) {
112                        $itemRef = $this->session->getCurrentAssessmentItemRef();
113                        $occurence = $this->session->getCurrentAssessmentItemRefOccurence();
114                    } else {
115                        $itemRef = $routeItem->getAssessmentItemRef();
116                        $occurence = $routeItem->getOccurence();
117                    }
118
119                    $itemUri = $this->getItemUri($itemRef);
120
121                    $transmissionId = "${sessionId}.${itemRef}.${occurence}";
122                    $resultStore->storeItemVariable($testUri, $itemUri, $metaVariable, $transmissionId);
123                } elseif (strcasecmp($type, 'TEST') === 0) {
124                    $resultStore->storeTestVariable($testUri, $metaVariable, $sessionId);
125                } elseif (strcasecmp($type, 'SECTION') === 0) {
126                    //suffix section variables with _{SECTION_IDENTIFIER}
127                    if ($assessmentSectionId === null) {
128                        $assessmentSectionId = $this->session->getCurrentAssessmentSection()->getIdentifier();
129                    }
130                    $metaVariable->setIdentifier($key . '_' . $assessmentSectionId);
131                    $resultStore->storeTestVariable($testUri, $metaVariable, $sessionId);
132                }
133            }
134        }
135    }
136
137    /**
138     * Get current test session meta data array
139     *
140     * @return array test session meta data.
141     */
142    public function getData()
143    {
144        $request = Context::getInstance()->getRequest();
145        $data = $request->hasParameter('metaData') ? $request->getParameter('metaData') : [];
146
147        return $data;
148    }
149
150    /**
151     * Get trace variable instance to save.
152     *
153     * @param string $identifier
154     * @param string $value
155     * @return taoResultServer_models_classes_TraceVariable variable instance to save.
156     */
157    private function getVariable($identifier, $value)
158    {
159        $metaVariable = new taoResultServer_models_classes_TraceVariable();
160        $metaVariable->setIdentifier($identifier);
161        $metaVariable->setBaseType('string');
162        $metaVariable->setCardinality(Cardinality::getNameByConstant(Cardinality::SINGLE));
163        $metaVariable->setTrace($value);
164
165        return $metaVariable;
166    }
167
168    /**
169     * Get test session instance
170     * @return AssessmentTestSession|\taoQtiTest_helpers_TestSession
171     */
172    public function getTestSession()
173    {
174        return $this->session;
175    }
176
177    /**
178     * Get the URI referencing the Assessment Item (in the knowledge base)
179     *
180     * @param AssessmentItemRef $itemRef
181     * @return string A URI.
182     */
183    private function getItemUri(AssessmentItemRef $itemRef)
184    {
185        $href = $itemRef->getHref();
186        $parts = explode('|', $href);
187
188        return $parts[0];
189    }
190
191    private function getServiceManager()
192    {
193        return ServiceManager::getServiceManager();
194    }
195}