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