Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
14.63% |
6 / 41 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ActivityMonitoringService | |
14.63% |
6 / 41 |
|
40.00% |
2 / 5 |
47.81 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getData | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
2 | |||
| getTimeKeys | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNumberOfAssessments | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getNumberOfActiveUsers | |
0.00% |
0 / 13 |
|
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) 2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoProctoring\model; |
| 24 | |
| 25 | use oat\generis\model\OntologyAwareTrait; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\tao\model\user\TaoRoles; |
| 28 | use oat\taoProctoring\model\execution\DeliveryExecution; |
| 29 | use oat\taoProctoring\model\monitorCache\DeliveryMonitoringService; |
| 30 | use oat\taoEventLog\model\userLastActivityLog\UserLastActivityLog; |
| 31 | |
| 32 | /** |
| 33 | * Service to manage and monitor assessment activity |
| 34 | * |
| 35 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 36 | */ |
| 37 | class ActivityMonitoringService extends ConfigurableService |
| 38 | { |
| 39 | use OntologyAwareTrait; |
| 40 | |
| 41 | public const SERVICE_ID = 'taoProctoring/ActivityMonitoringService'; |
| 42 | |
| 43 | /** Threshold in seconds */ |
| 44 | public const OPTION_ACTIVE_USER_THRESHOLD = 'active_user_threshold'; |
| 45 | |
| 46 | /** Interval of refreshing assessment activity graph in seconds. 0 - no auto refresh */ |
| 47 | public const OPTION_COMPLETED_ASSESSMENTS_AUTO_REFRESH = 'completed_assessments_auto_refresh'; |
| 48 | |
| 49 | /** Interval of refreshing assessment activity data in seconds. 0 - no auto refresh */ |
| 50 | public const OPTION_ASSESSMENT_ACTIVITY_AUTO_REFRESH = 'assessment_activity_auto_refresh'; |
| 51 | |
| 52 | /** Allow to specify custom activity widgets to be rendered */ |
| 53 | public const OPTION_USER_ACTIVITY_WIDGETS = 'userActivityWidgets'; |
| 54 | |
| 55 | /** State of awaiting assessment */ |
| 56 | public const STATE_AWAITING_ASSESSMENT = 'awaiting_assessments'; |
| 57 | |
| 58 | /** State of authorized assessment */ |
| 59 | public const STATE_AUTHORIZED_BUT_NOT_STARTED_ASSESSMENTS = 'authorized_but_not_started_assessments'; |
| 60 | |
| 61 | /** State of paused assessment */ |
| 62 | public const STATE_PAUSED_ASSESSMENTS = 'paused_assessments'; |
| 63 | |
| 64 | /** State of in progress assessment */ |
| 65 | public const STATE_IN_PROGRESS_ASSESSMENTS = 'in_progress_assessments'; |
| 66 | |
| 67 | /** Active proctors field */ |
| 68 | public const FIELD_ACTIVE_PROCTORS = 'active_proctors'; |
| 69 | |
| 70 | /** Active Test Takers field */ |
| 71 | public const FIELD_ACTIVE_TEST_TAKERS = 'active_test_takers'; |
| 72 | |
| 73 | /** Total assessments field */ |
| 74 | public const FIELD_TOTAL_ASSESSMENTS = 'total_assessments'; |
| 75 | |
| 76 | /** Deliveries statistics field*/ |
| 77 | public const FIELD_DELIVERIES_STATISTICS = 'deliveries_statistics'; |
| 78 | |
| 79 | /** Retired deliveries field*/ |
| 80 | public const FIELD_RETIRED_DELIVERIES = 'retired_deliveries'; |
| 81 | |
| 82 | /** Total current assessment field */ |
| 83 | public const FIELD_TOTAL_CURRENT_ASSESSMENTS = 'total_current_assessments'; |
| 84 | |
| 85 | public const LABEL_RETIRED_DELIVERIES = 'Retired Deliveries'; |
| 86 | |
| 87 | public const GROUPFIELD_USER_ACTIVITY = 'group_user_activity'; |
| 88 | |
| 89 | /** |
| 90 | * @var array list of all the statuses uris |
| 91 | */ |
| 92 | protected $deliveryStatuses = [ |
| 93 | DeliveryExecution::STATE_AWAITING, |
| 94 | DeliveryExecution::STATE_AUTHORIZED, |
| 95 | DeliveryExecution::STATE_PAUSED, |
| 96 | DeliveryExecution::STATE_ACTIVE, |
| 97 | DeliveryExecution::STATE_TERMINATED, |
| 98 | DeliveryExecution::STATE_CANCELED, |
| 99 | DeliveryExecution::STATE_FINISHED, |
| 100 | ]; |
| 101 | |
| 102 | /** |
| 103 | * ActivityMonitoringService constructor. |
| 104 | * @param array $options |
| 105 | */ |
| 106 | public function __construct(array $options = array()) |
| 107 | { |
| 108 | parent::__construct($options); |
| 109 | |
| 110 | $deliveryStatuses = []; |
| 111 | foreach ($this->deliveryStatuses as $deliveryStatus) { |
| 112 | $deliveryStatuses[] = new \core_kernel_classes_Resource($deliveryStatus); |
| 113 | } |
| 114 | $this->deliveryStatuses = $deliveryStatuses; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Return comprehensive activity monitoring data. |
| 119 | * @return array |
| 120 | */ |
| 121 | public function getData() |
| 122 | { |
| 123 | $awaiting = $this->getNumberOfAssessments(DeliveryExecution::STATE_AWAITING); |
| 124 | $authorized = $this->getNumberOfAssessments(DeliveryExecution::STATE_AUTHORIZED); |
| 125 | $paused = $this->getNumberOfAssessments(DeliveryExecution::STATE_PAUSED); |
| 126 | $active = $this->getNumberOfAssessments(DeliveryExecution::STATE_ACTIVE); |
| 127 | $current = $awaiting + $authorized + $paused + $active; |
| 128 | $assessments = [ |
| 129 | self::GROUPFIELD_USER_ACTIVITY => [ |
| 130 | self::FIELD_ACTIVE_PROCTORS => $this->getNumberOfActiveUsers(ProctorService::ROLE_PROCTOR), |
| 131 | self::FIELD_ACTIVE_TEST_TAKERS => $this->getNumberOfActiveUsers(TaoRoles::DELIVERY), |
| 132 | ], |
| 133 | self::FIELD_TOTAL_ASSESSMENTS => $this->getNumberOfAssessments(), |
| 134 | self::FIELD_TOTAL_CURRENT_ASSESSMENTS => $current, |
| 135 | self::STATE_AWAITING_ASSESSMENT => $awaiting, |
| 136 | self::STATE_AUTHORIZED_BUT_NOT_STARTED_ASSESSMENTS => $authorized, |
| 137 | self::STATE_PAUSED_ASSESSMENTS => $paused, |
| 138 | self::STATE_IN_PROGRESS_ASSESSMENTS => $active |
| 139 | ]; |
| 140 | |
| 141 | return $assessments; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @see \tao_helpers_Date::getTimeKeys() |
| 146 | * @deprecated |
| 147 | * @return array |
| 148 | */ |
| 149 | public function getTimeKeys(\DateInterval $interval, \DateTime $date = null, $amount = null) |
| 150 | { |
| 151 | return \tao_helpers_Date::getTimeKeys($interval, $date, $amount); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @param null|string $state |
| 156 | * @return int |
| 157 | */ |
| 158 | protected function getNumberOfAssessments($state = null) |
| 159 | { |
| 160 | $deliveryMonitoringService = $this->getServiceManager()->get(DeliveryMonitoringService::SERVICE_ID); |
| 161 | if ($state === null) { |
| 162 | return $deliveryMonitoringService->count(); |
| 163 | } else { |
| 164 | return $deliveryMonitoringService->count([DeliveryMonitoringService::STATUS => $state]); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @param null|string $role |
| 170 | * @return int |
| 171 | */ |
| 172 | protected function getNumberOfActiveUsers($role = null) |
| 173 | { |
| 174 | /** @var UserLastActivityLog $userActivityService */ |
| 175 | $userActivityService = $this->getServiceManager()->get(UserLastActivityLog::SERVICE_ID); |
| 176 | $now = microtime(true); |
| 177 | $filter = [ |
| 178 | [ |
| 179 | UserLastActivityLog::EVENT_TIME, |
| 180 | 'between', |
| 181 | $now - $this->getOption(self::OPTION_ACTIVE_USER_THRESHOLD), |
| 182 | $now, |
| 183 | ], |
| 184 | ]; |
| 185 | if ($role !== null) { |
| 186 | $filter[] = [UserLastActivityLog::USER_ROLES, 'like', '%,' . $role . ',%']; |
| 187 | } |
| 188 | return $userActivityService->count($filter, ['group' => UserLastActivityLog::USER_ID]); |
| 189 | } |
| 190 | } |