Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
77.55% |
38 / 49 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
ResultsWatcher | |
77.55% |
38 / 49 |
|
57.14% |
4 / 7 |
14.91 | |
0.00% |
0 / 1 |
catchCreatedDeliveryExecutionEvent | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
catchTestChangedEvent | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
addIndex | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
3 | |||
isResultSearchEnabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
transformDateTime | |
50.00% |
5 / 10 |
|
0.00% |
0 / 1 |
4.12 | |||
getAdvancedSearchChecker | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUserService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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) 2018-2020 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | namespace oat\taoOutcomeUi\model\search; |
22 | |
23 | use DateTimeImmutable; |
24 | use oat\generis\model\OntologyAwareTrait; |
25 | use oat\oatbox\service\ConfigurableService; |
26 | use oat\oatbox\user\UserService; |
27 | use oat\tao\helpers\UserHelper; |
28 | use oat\tao\model\AdvancedSearch\AdvancedSearchChecker; |
29 | use oat\tao\model\search\Search; |
30 | use oat\tao\model\search\tasks\AddSearchIndexFromArray; |
31 | use oat\tao\model\taskQueue\QueueDispatcherInterface; |
32 | use oat\taoDelivery\model\execution\DeliveryExecutionInterface; |
33 | use oat\taoDelivery\model\execution\ServiceProxy; |
34 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionCreated; |
35 | use oat\taoResultServer\models\classes\ResultService; |
36 | use oat\taoTests\models\event\TestChangedEvent; |
37 | |
38 | /** |
39 | * Class ResultsWatcher |
40 | * @package oat\taoOutcomeUi\model\search |
41 | */ |
42 | class ResultsWatcher extends ConfigurableService |
43 | { |
44 | use OntologyAwareTrait; |
45 | |
46 | public const SERVICE_ID = 'taoOutcomeUi/ResultsWatcher'; |
47 | public const OPTION_RESULT_SEARCH_FIELD_VISIBILITY = 'option_result_search_field_visibility'; |
48 | |
49 | public const INDEX_DELIVERY = 'delivery'; |
50 | public const INDEX_TEST_TAKER = 'test_taker'; |
51 | public const INDEX_TEST_TAKER_NAME = 'test_taker_name'; |
52 | public const INDEX_TEST_TAKER_LABEL = 'test_taker_label'; |
53 | public const INDEX_DELIVERY_EXECUTION = 'delivery_execution'; |
54 | public const INDEX_DELIVERY_EXECUTION_START_TIME = 'delivery_execution_start_time'; |
55 | public const INDEX_TEST_TAKER_LAST_NAME = 'test_taker_last_name'; |
56 | public const INDEX_TEST_TAKER_FIRST_NAME = 'test_taker_first_name'; |
57 | |
58 | /** |
59 | * @param DeliveryExecutionCreated $event |
60 | * @return \common_report_Report |
61 | * @throws \common_exception_NotFound |
62 | */ |
63 | public function catchCreatedDeliveryExecutionEvent(DeliveryExecutionCreated $event) |
64 | { |
65 | /** @var DeliveryExecutionInterface $resource */ |
66 | $deliveryExecution = $event->getDeliveryExecution(); |
67 | return $this->addIndex($deliveryExecution); |
68 | } |
69 | |
70 | /** |
71 | * @param TestChangedEvent $event |
72 | * @return \common_report_Report |
73 | * @throws \common_exception_NotFound |
74 | */ |
75 | public function catchTestChangedEvent(TestChangedEvent $event) |
76 | { |
77 | $sessionMemento = $event->getSessionMemento(); |
78 | $session = $event->getSession(); |
79 | if ($sessionMemento && $session->getState() !== $sessionMemento->getState()) { |
80 | $deliveryExecution = ServiceProxy::singleton()->getDeliveryExecution($event->getServiceCallId()); |
81 | return $this->addIndex($deliveryExecution); |
82 | } |
83 | } |
84 | |
85 | /** |
86 | * @param DeliveryExecutionInterface $deliveryExecution |
87 | * @return \common_report_Report |
88 | * @throws \common_exception_NotFound |
89 | */ |
90 | protected function addIndex(DeliveryExecutionInterface $deliveryExecution) |
91 | { |
92 | /** @var Search $searchService */ |
93 | $report = \common_report_Report::createSuccess(); |
94 | $searchService = $this->getServiceLocator()->get(Search::SERVICE_ID); |
95 | if ($searchService->supportCustomIndex() && $this->getAdvancedSearchChecker()->isEnabled()) { |
96 | $deliveryExecutionId = $deliveryExecution->getIdentifier(); |
97 | $user = $this->getUserService()->getUser($deliveryExecution->getUserIdentifier()); |
98 | $customFieldService = $this->getServiceLocator()->get(ResultCustomFieldsService::SERVICE_ID); |
99 | $customBody = $customFieldService->getCustomFields($deliveryExecution); |
100 | |
101 | $body = [ |
102 | 'label' => $deliveryExecution->getLabel(), |
103 | self::INDEX_DELIVERY => $deliveryExecution->getDelivery()->getUri(), |
104 | 'type' => ResultService::DELIVERY_RESULT_CLASS_URI, |
105 | self::INDEX_TEST_TAKER => $user->getIdentifier(), |
106 | self::INDEX_TEST_TAKER_FIRST_NAME => UserHelper::getUserFirstName($user, true), |
107 | self::INDEX_TEST_TAKER_LAST_NAME => UserHelper::getUserLastName($user, true), |
108 | self::INDEX_TEST_TAKER_NAME => UserHelper::getUserName($user, true), |
109 | self::INDEX_TEST_TAKER_LABEL => UserHelper::getUserLabel($user), |
110 | self::INDEX_DELIVERY_EXECUTION => $deliveryExecutionId, |
111 | self::INDEX_DELIVERY_EXECUTION_START_TIME => $this->transformDateTime( |
112 | $deliveryExecution->getStartTime() |
113 | ) |
114 | ]; |
115 | $body = array_merge($body, $customBody); |
116 | $queueDispatcher = $this->getServiceLocator()->get(QueueDispatcherInterface::SERVICE_ID); |
117 | |
118 | $queueDispatcher->createTask( |
119 | new AddSearchIndexFromArray(), |
120 | [$deliveryExecutionId, $body], |
121 | __('Adding/Updating search index for %s', $deliveryExecution->getLabel()) |
122 | ); |
123 | } |
124 | return $report; |
125 | } |
126 | |
127 | /** |
128 | * Control filtering visibility |
129 | * @return boolean |
130 | */ |
131 | public function isResultSearchEnabled() |
132 | { |
133 | return (bool)$this->getOption(self::OPTION_RESULT_SEARCH_FIELD_VISIBILITY); |
134 | } |
135 | |
136 | private function transformDateTime(string $getStartTime): string |
137 | { |
138 | $timeArray = explode(" ", $getStartTime); |
139 | $date = DateTimeImmutable::createFromFormat('U', $timeArray[1]); |
140 | |
141 | if ($date === false) { |
142 | $date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $getStartTime); |
143 | } |
144 | |
145 | if (!$date instanceof DateTimeImmutable) { |
146 | $this->logCritical( |
147 | sprintf('We were not able to transform string: "%s" delivery-execution start time!', $getStartTime) |
148 | ); |
149 | return ''; |
150 | } |
151 | |
152 | return $date->format('m/d/Y H:i:s'); |
153 | } |
154 | |
155 | private function getAdvancedSearchChecker(): AdvancedSearchChecker |
156 | { |
157 | return $this->getServiceLocator()->get(AdvancedSearchChecker::class); |
158 | } |
159 | |
160 | private function getUserService(): UserService |
161 | { |
162 | return $this->getServiceLocator()->get(UserService::SERVICE_ID); |
163 | } |
164 | } |