ILIAS  release_8 Revision v8.24
class.ilBTPopOverGUI.php
Go to the documentation of this file.
1<?php
2
22use ILIAS\BackgroundTasks\Implementation\UI\StateTranslator;
27
35{
36 use StateTranslator;
37 protected \ILIAS\DI\Container $dic;
38
39
40 public function __construct(\ILIAS\DI\Container $dic)
41 {
42 $this->dic = $dic;
43 }
44
45
49 public function getNotificationItem(int $nr_buckets): ILIAS\UI\Component\Item\Notification
50 {
51 $ui_factory = $this->dic->ui()->factory();
52
53 $title = $ui_factory->link()->standard($this->txt('background_tasks'), '#');
54 $icon = $ui_factory->symbol()->icon()->standard('bgtk', $this->txt('background_tasks'));
55
56 return $this->dic->ui()->factory()
57 ->item()
58 ->notification($title, $icon)
59 ->withDescription("$nr_buckets {$this->txt('background_tasks')}")
60 ->withAggregateNotifications($this->getAggregateItems());
61 }
62
63
67 protected function getAggregateItems(): array
68 {
69 $persistence = $this->dic->backgroundTasks()->persistence();
70 $items = [];
71 $observer_ids = $persistence->getBucketIdsOfUser($this->dic->user()->getId(), 'id', 'DESC');
72 foreach ($persistence->loadBuckets($observer_ids) as $observer) {
73 $items[] = $this->getItemForObserver($observer);
74 }
75
76 return $items;
77 }
78
79
80 public function getItemForObserver(Bucket $observer): ILIAS\UI\Component\Item\Notification
81 {
82 $redirect_uri = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
83
84 $f = $this->dic->ui()->factory();
85
86 $state = $observer->getState();
87 $current_task = $observer->getCurrentTask();
88
89 $icon = $f->symbol()->icon()->standard("bgtk", $this->txt("bg_task"));
90 $title = $observer->getTitle() . ($state === State::SCHEDULED ? " ({$this->txt('scheduled')})" : "");
91
92 if ($state === State::USER_INTERACTION) {
93 $actions = $this->getUserInteractionContent($observer, $redirect_uri);
94 $primary_action = array_pop($actions);
95 if ($primary_action instanceof Button) {
96 $title = $primary_action->withLabel($title);
97 }
98 $item = $f->item()->notification($title, $icon);
99
100// $item = $item->withProperties([
101// $this->dic->language()->txt('nc_mail_prop_time') => \ilDatePresentation::formatDate(
102// new \ilDateTime(time(), IL_CAL_UNIX)
103// )
104// ]);
105
106 $item = $item->withActions($f->dropdown()->standard($actions));
107 $input = $current_task->getInput();
108 $message = $current_task->getMessage($input);
109
110 if (!empty($message) && $message != null) {
111 $item = $item->withDescription($message);
112 } else {
113 $item = $item->withAdditionalContent($this->getProgressbar($observer));
114 }
115
116 return $item->withCloseAction(
117 $this->getCloseButtonAction($current_task->getRemoveOption(), $redirect_uri, $observer)
118 );
119 }
120
121 $item = $f->item()->notification($title, $icon);
122
123 if ($state === State::RUNNING) {
124 $url = $this->getRefreshUrl($observer);
125 //Running Items probably need to refresh themselves, right?
126 $item = $item->withAdditionalOnLoadCode(fn ($id) => "var notification_item = il.UI.item.notification.getNotificationItemObject($('#$id'));
127 il.BGTask.refreshItem(notification_item,'$url');");
128
129 $expected = $current_task->getExpectedTimeOfTaskInSeconds();
130 $possibly_failed = ($observer->getLastHeartbeat() < (time() - $expected));
131 if ($possibly_failed) {
132 $item = $item->withDescription($this->txt('task_might_be_failed'));
133 $item = $item->withCloseAction(
134 $this->getCloseButtonAction($current_task->getAbortOption(), $redirect_uri, $observer)
135 );
136 }
137 }
138
139 return $item->withAdditionalContent($this->getDefaultCardContent($observer));
140 }
141
142
143 private function getDefaultCardContent(Bucket $observer): Legacy
144 {
145 return $this->getProgressbar($observer);
146 }
147
148
152 public function getUserInteractionContent(Bucket $observer, string $redirect_uri): array
153 {
154 $factory = $this->dic->ui()->factory();
155 $language = $this->dic->language();
156 $persistence = $this->dic->backgroundTasks()->persistence();
157 $ctrl = $this->dic->ctrl();
158
159 if (!$observer->getCurrentTask() instanceof UserInteraction) {
160 return [$factory->legacy('')];
161 }
163 $userInteraction = $observer->getCurrentTask();
164 $options = $userInteraction->getOptions($userInteraction->getInput());
165
166 return array_map(
167 function (UserInteraction\Option $option) use ($ctrl, $factory, $observer, $persistence, $redirect_uri, $language): \ILIAS\UI\Component\Button\Shy {
168 $ctrl->setParameterByClass(
169 ilBTControllerGUI::class,
171 ilBTControllerGUI::hash("//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}")
172 );
173 $ctrl->setParameterByClass(
174 ilBTControllerGUI::class,
176 $option->getValue()
177 );
178 $ctrl->setParameterByClass(
179 ilBTControllerGUI::class,
181 $persistence->getBucketContainerId($observer)
182 );
183 $this->addFromUrlToNextRequest($redirect_uri);
184
185 return $factory->button()
186 ->shy(
187 $language->txt($option->getLangVar()),
188 $ctrl->getLinkTargetByClass([ilBTControllerGUI::class], ilBTControllerGUI::CMD_USER_INTERACTION)
189 );
190 },
191 $options
192 );
193 }
194
195
196 private function getProgressbar(Bucket $observer): Legacy
197 {
198 $percentage = $observer->getOverallPercentage();
199
200 switch (true) {
201 case ($percentage === 100):
202 $running = "";
203 $content = $this->dic->language()->txt("completed");
204 break;
205 case ($observer->getState() === State::USER_INTERACTION):
206 $running = "";
207 $content = $this->dic->language()->txt("waiting");
208 break;
209 default:
210 $running = "active";
211 $content = "{$percentage}%";
212 break;
213 }
214
215 return $this->dic->ui()->factory()->legacy(" <div class='progress'>
216 <div class='progress-bar progress-bar-striped {$running}' role='progressbar' aria-valuenow='{$percentage}'
217 aria-valuemin='0' aria-valuemax='100' style='width:{$percentage}%'>
218 {$content}
219 </div>
220 </div> ");
221 }
222
223
224 private function getCloseButtonAction(UserInteraction\Option $option, string $redirect_uri, Bucket $observer): string
225 {
226 $ctrl = $this->dic->ctrl();
227 $persistence = $this->dic->backgroundTasks()->persistence();
228 $ctrl->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::OBSERVER_ID, $persistence->getBucketContainerId($observer));
229 $this->addFromUrlToNextRequest($redirect_uri);
230 $ctrl->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::IS_ASYNC, "true");
231
232 switch ($option->getValue()) {
233 case AbstractTask::MAIN_ABORT:
234 $action = $ctrl->getLinkTargetByClass([ilBTControllerGUI::class], ilBTControllerGUI::CMD_ABORT);
235 break;
236 case AbstractTask::MAIN_REMOVE:
237 $action = $ctrl->getLinkTargetByClass([ilBTControllerGUI::class], ilBTControllerGUI::CMD_REMOVE);
238 break;
239 default:
240 $ctrl->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::SELECTED_OPTION, $option->getValue());
241 $action = $ctrl->getLinkTargetByClass([ilBTControllerGUI::class], ilBTControllerGUI::CMD_USER_INTERACTION);
242 break;
243 }
244
245 return $action;
246 }
247
248
249 private function getRefreshUrl(Bucket $observer): string
250 {
251 $ctrl = $this->dic->ctrl();
252 $persistence = $this->dic->backgroundTasks()->persistence();
253 $ctrl->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::OBSERVER_ID, $persistence->getBucketContainerId($observer));
254
255 return $ctrl->getLinkTargetByClass([ilBTControllerGUI::class], ilBTControllerGUI::CMD_GET_REPLACEMENT_ITEM);
256 }
257
258
259 private function addFromUrlToNextRequest(string $redirect_uri): void
260 {
261 $this->dic->ctrl()->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::FROM_URL, ilBTControllerGUI::hash($redirect_uri));
262 }
263
264
265 private function txt(string $id): string
266 {
267 return $this->dic->language()->txt($id);
268 }
269}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
Class ilBTPopOverGUI.
__construct(\ILIAS\DI\Container $dic)
addFromUrlToNextRequest(string $redirect_uri)
getCloseButtonAction(UserInteraction\Option $option, string $redirect_uri, Bucket $observer)
getRefreshUrl(Bucket $observer)
getDefaultCardContent(Bucket $observer)
getNotificationItem(int $nr_buckets)
Get the Notification Items.
ILIAS DI Container $dic
getProgressbar(Bucket $observer)
getItemForObserver(Bucket $observer)
getLastHeartbeat()
When was the last time that something happened on this bucket?
This describes commonalities between standard and primary buttons.
Definition: Button.php:32
$redirect_uri
Definition: ltiauth.php:69
$factory
Definition: metadata.php:75
Class HTTPServicesTest.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Option.php:19
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Bulky.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider \MainMenu\Provider.
Class Factory.
$url
$message
Definition: xapiexit.php:32