Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 100
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Tools
0.00% covered (danger)
0.00%
0 / 100
0.00% covered (danger)
0.00%
0 / 7
506
0.00% covered (danger)
0.00%
0 / 1
 assessmentActivity
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 assessmentActivityData
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 completedAssessmentsData
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
6
 deliveriesActivityData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 pauseActiveExecutions
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
12
 getInterval
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
56
 getTimeKeys
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
56
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) 2017 (original work) Open Assessment Technologies SA ;
19 *
20 */
21
22namespace oat\taoProctoring\controller;
23
24use oat\generis\model\OntologyAwareTrait;
25use oat\taoProctoring\helpers\DeliveryHelper;
26use oat\taoProctoring\model\monitorCache\DeliveryMonitoringService;
27use oat\taoProctoring\model\execution\DeliveryExecution;
28use oat\taoProctoring\model\ActivityMonitoringService;
29use oat\taoProctoring\model\datatable\DeliveriesActivityDatatable;
30use oat\taoProctoring\model\event\DeliveryExecutionFinished;
31
32/**
33 * Class Tools
34 *
35 * @package oat\taoProctoring\controller
36 * @author Aleh Hutnikau, <hutnikau@1pt.com>
37 */
38class Tools extends SimplePageModule
39{
40    use OntologyAwareTrait;
41
42    /**
43     * Show assessment activity dashboard
44     */
45    public function assessmentActivity()
46    {
47        $service = $this->getServiceLocator()->get(ActivityMonitoringService::SERVICE_ID);
48
49        // Reason categories
50        $this->setData('reason_categories', DeliveryHelper::getAllReasonsCategories());
51
52        // Config
53        $config = [ActivityMonitoringService::OPTION_USER_ACTIVITY_WIDGETS =>
54            $service->getOption(ActivityMonitoringService::OPTION_USER_ACTIVITY_WIDGETS),
55            ActivityMonitoringService::OPTION_ASSESSMENT_ACTIVITY_AUTO_REFRESH =>
56                $service->getOption(ActivityMonitoringService::OPTION_ASSESSMENT_ACTIVITY_AUTO_REFRESH),
57            ActivityMonitoringService::OPTION_COMPLETED_ASSESSMENTS_AUTO_REFRESH =>
58                $service->getOption(ActivityMonitoringService::OPTION_COMPLETED_ASSESSMENTS_AUTO_REFRESH),
59        ];
60        $this->setData('config', $config);
61
62        $this->setView('Tools/assessment_activity.tpl');
63    }
64
65    /**
66     * Show assessment activity data as json
67     */
68    public function assessmentActivityData()
69    {
70        $service = $this->getServiceLocator()->get(ActivityMonitoringService::SERVICE_ID);
71        $data = $service->getData();
72
73        $this->returnJson([
74            'success' => true,
75            'data' => $data
76        ]);
77    }
78
79    /**
80     * Get completed assessments data
81     */
82    public function completedAssessmentsData()
83    {
84        $timePeriod = $this->getRequestParameter('interval');
85
86        $eventLog = $this->getServiceLocator()->get(\oat\taoEventLog\model\eventLog\LoggerService::SERVICE_ID);
87
88        $tz = new \DateTimeZone(\common_session_SessionManager::getSession()->getTimeZone());
89        $timeKeys = $this->getTimeKeys($timePeriod);
90        $interval = $this->getInterval($timePeriod);
91
92        foreach ($timeKeys as $timeKey) {
93            $to = clone($timeKey);
94            $from = clone($to);
95            $from->sub($interval);
96            $countEvents = $eventLog->count([
97                ['occurred', 'between', $from->format('Y-m-d H:i:s'), $to->format('Y-m-d H:i:s')],
98                ['event_name', '=', DeliveryExecutionFinished::class],
99            ]);
100            $result['time'][] = $to->setTimezone($tz)->format('Y-m-d H:i:s');
101            $result['amount'][] = $countEvents;
102        }
103
104        $this->returnJson($result, 200);
105    }
106
107    /**
108     *
109     */
110    public function deliveriesActivityData()
111    {
112        $this->returnJson(new DeliveriesActivityDatatable());
113    }
114
115    /**
116     * Action pauses all the active delivery executions
117     */
118    public function pauseActiveExecutions()
119    {
120        if (!$this->isRequestPost()) {
121            throw new \common_exception_BadRequest('Invalid request. Only POST method allowed.');
122        }
123
124        $reason = $this->hasRequestParameter('reason') ? $this->getRequestParameter('reason') : [
125            'reasons' => ['category' => 'Technical', 'subCategory' => 'ACT'],
126            'comment' => __('Pause due to server maintenance'),
127        ];
128        /** @var DeliveryMonitoringService $monitoringService */
129        $monitoringService = $this->getServiceLocator()->get(DeliveryMonitoringService::SERVICE_ID);
130        $deliveryExecutions = $monitoringService->find(
131            [DeliveryMonitoringService::STATUS => DeliveryExecution::STATE_ACTIVE],
132            ['asArray' => true]
133        );
134        $ids = array_map(function ($deliveryExecution) {
135            return $deliveryExecution['delivery_execution_id'];
136        }, $deliveryExecutions);
137        $stats = DeliveryHelper::pauseExecutions($ids, $reason);
138        $paused = $stats['processed'];
139        $notPaused = $stats['unprocessed'];
140
141        $this->returnJson([
142            'success' => true,
143            'data' => [
144                'message' => count($paused) . ' ' . __('sessions paused'),
145                'processed' => $paused,
146                'unprocessed' => $notPaused
147            ]
148        ]);
149    }
150
151    /**
152     * @return \DateInterval
153     */
154    private function getInterval($timePeriod)
155    {
156        $interval = new \DateInterval('PT1H');
157
158        if ($timePeriod) {
159            switch ($timePeriod) {
160                case 'day':
161                    $interval = new \DateInterval('PT1H');
162                    break;
163                case 'week':
164                    $interval = new \DateInterval('P1D');
165                    break;
166                case 'month':
167                    $interval = new \DateInterval('P1D');
168                    break;
169                case 'prevmonth':
170                    $interval = new \DateInterval('P1D');
171                    break;
172                default:
173                    $interval = new \DateInterval('PT1H');
174                    break;
175            }
176        }
177
178        return $interval;
179    }
180
181    /**
182     * @return \DateTime[]
183     */
184    private function getTimeKeys($timePeriod)
185    {
186        /** @var ActivityMonitoringService $service */
187        $service = $this->getServiceLocator()->get(ActivityMonitoringService::SERVICE_ID);
188
189        $amount = null;
190        $startDate = null;
191
192        if ($timePeriod) {
193            switch ($timePeriod) {
194                case 'day':
195                    $startDate = new \DateTime('now', new \DateTimeZone('UTC'));
196                    break;
197                case 'week':
198                    $startDate = new \DateTime('now', new \DateTimeZone('UTC'));
199                    $amount = 7;
200                    break;
201                case 'month':
202                    $startDate = new \DateTime('now', new \DateTimeZone('UTC'));
203                    $amount = cal_days_in_month(CAL_GREGORIAN, $startDate->format('m'), $startDate->format('Y'));
204                    break;
205                case 'prevmonth':
206                    $startDate = new \DateTime('now', new \DateTimeZone('UTC'));
207                    $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $startDate->format('m'), $startDate->format('Y'));
208                    $startDate->sub(new \DateInterval('P' . $daysInMonth . 'D'));
209                    $amount = cal_days_in_month(CAL_GREGORIAN, $startDate->format('m'), $startDate->format('Y'));
210                    break;
211                default:
212                    $startDate = new \DateTime('now', new \DateTimeZone('UTC'));
213                    break;
214            }
215        }
216
217        return $service->getTimeKeys($this->getInterval($timePeriod), $startDate, $amount);
218    }
219}