Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TestStateChannel
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 process
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
132
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) 2016 (original work) Open Assessment Technologies SA ;
19 */
20
21/**
22 * @author Jean-Sébastien Conan <jean-sebastien.conan@vesperiagroup.com>
23 */
24
25namespace oat\taoQtiTest\models\runner\communicator;
26
27use oat\taoQtiTest\models\ExtendedStateService;
28use oat\taoQtiTest\models\runner\QtiRunnerMessageService;
29use oat\taoQtiTest\models\runner\QtiRunnerServiceContext;
30use qtism\runtime\tests\AssessmentTestSessionState;
31use Zend\ServiceManager\ServiceLocatorAwareInterface;
32use Zend\ServiceManager\ServiceLocatorAwareTrait;
33
34/**
35 * Class TestStateChannel
36 *
37 * Describes the test session state
38 *
39 * @package oat\taoQtiTest\models\runner\communicator
40 */
41class TestStateChannel implements ServiceLocatorAwareInterface, CommunicationChannel
42{
43    use ServiceLocatorAwareTrait;
44
45    public const CHANNEL_NAME = 'teststate';
46
47    /**
48     * Get name of channel
49     * @return string
50     */
51    public function getName()
52    {
53        return self::CHANNEL_NAME;
54    }
55
56    /**
57     * Generate output message based on test session state
58     * @param QtiRunnerServiceContext $context Current runner context
59     * @param array $data
60     * @return array
61     */
62    public function process(QtiRunnerServiceContext $context, array $data = [])
63    {
64        $result = null;
65        $session = $context->getTestSession();
66        $state = $session->getState();
67        $sessionId = $session->getSessionId();
68
69        $messageService = $this->getServiceLocator()->get(QtiRunnerMessageService::SERVICE_ID);
70        $stateService = $this->getServiceLocator()->get(ExtendedStateService::SERVICE_ID);
71        $events = $stateService->getEvents($sessionId);
72        $ids = [];
73        foreach ($events as $event) {
74            if ($event['type'] == self::CHANNEL_NAME) {
75                $ids[] = $event['id'];
76                if (isset($event['data']['state'])) {
77                    $state = $event['data']['state'];
78                } else {
79                    \common_Logger::w('The state is missing from the ' . self::CHANNEL_NAME . ' event context');
80                }
81
82                if (isset($event['data']['message'])) {
83                    //translate the message to the current user language.
84                    $message = __($event['data']['message']);
85                } else {
86                    $message = $messageService->getStateMessage($context->getTestSession());
87                    \common_Logger::w('The message is missing from the ' . self::CHANNEL_NAME . ' event context');
88                }
89
90                if (!$result || $state == AssessmentTestSessionState::CLOSED) {
91                    if ($state == AssessmentTestSessionState::CLOSED) {
92                        $type = 'close';
93                    } elseif ($state == AssessmentTestSessionState::SUSPENDED) {
94                        $type = 'pause';
95                    } else {
96                        $type = null;
97                    }
98
99                    if (is_null($type)) {
100                        \common_Logger::w('Inconsistent ' . self::CHANNEL_NAME . ' event');
101                    } else {
102                        $result = [
103                            'type' => $type,
104                            'code' => $state,
105                            'message' => $message,
106                        ];
107                    }
108                }
109            }
110        }
111
112        if (count($ids)) {
113            $stateService->removeEvents($sessionId, $ids);
114        }
115
116        return $result;
117    }
118}