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