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