ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilBTPopOverGUI.php
Go to the documentation of this file.
1 <?php
2 
28 
36 {
37  use StateTranslator;
38  protected \ILIAS\DI\Container $dic;
39 
40 
41  public function __construct(\ILIAS\DI\Container $dic)
42  {
43  $this->dic = $dic;
44  }
45 
46 
50  public function getNotificationItem(int $nr_buckets): ILIAS\UI\Component\Item\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): ILIAS\UI\Component\Item\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) => "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): Legacy
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('')];
162  }
164  $userInteraction = $observer->getCurrentTask();
165  $options = $userInteraction->getOptions($userInteraction->getInput());
166 
167  return array_map(
168  function (UserInteraction\Option $option) use ($ctrl, $factory, $observer, $persistence, $redirect_uri, $language): \ILIAS\UI\Component\Button\Shy {
169  $ctrl->setParameterByClass(
170  ilBTControllerGUI::class,
172  ilBTControllerGUI::hash("//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}")
173  );
174  $ctrl->setParameterByClass(
175  ilBTControllerGUI::class,
177  $option->getValue()
178  );
179  $ctrl->setParameterByClass(
180  ilBTControllerGUI::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([ilBTControllerGUI::class], ilBTControllerGUI::CMD_USER_INTERACTION)
190  );
191  },
192  $options
193  );
194  }
195 
196 
197  private function getProgressbar(Bucket $observer): Legacy
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(" <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(UserInteraction\Option $option, string $redirect_uri, Bucket $observer): string
226  {
227  $ctrl = $this->dic->ctrl();
228  $persistence = $this->dic->backgroundTasks()->persistence();
229  $ctrl->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::OBSERVER_ID, $persistence->getBucketContainerId($observer));
230  $this->addFromUrlToNextRequest($redirect_uri);
231  $ctrl->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::IS_ASYNC, "true");
232 
233  switch ($option->getValue()) {
234  case AbstractTask::MAIN_ABORT:
235  $action = $ctrl->getLinkTargetByClass([ilBTControllerGUI::class], ilBTControllerGUI::CMD_ABORT);
236  break;
237  case AbstractTask::MAIN_REMOVE:
238  $action = $ctrl->getLinkTargetByClass([ilBTControllerGUI::class], ilBTControllerGUI::CMD_REMOVE);
239  break;
240  default:
241  $ctrl->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::SELECTED_OPTION, $option->getValue());
242  $action = $ctrl->getLinkTargetByClass([ilBTControllerGUI::class], ilBTControllerGUI::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(ilBTControllerGUI::class, ilBTControllerGUI::OBSERVER_ID, $persistence->getBucketContainerId($observer));
255 
256  return $ctrl->getLinkTargetByClass([ilBTControllerGUI::class], ilBTControllerGUI::CMD_GET_REPLACEMENT_ITEM);
257  }
258 
259 
260  private function addFromUrlToNextRequest(string $redirect_uri): void
261  {
262  $this->dic->ctrl()->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::FROM_URL, ilBTControllerGUI::hash($redirect_uri));
263  }
264 
265 
266  private function txt(string $id): string
267  {
268  return $this->dic->language()->txt($id);
269  }
270 }
ILIAS DI Container $dic
__construct(\ILIAS\DI\Container $dic)
$redirect_uri
Definition: ltiauth.php:69
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getItemForObserver(Bucket $observer)
getDefaultCardContent(Bucket $observer)
Class HTTPServicesTest.
getLastHeartbeat()
When was the last time that something happened on this bucket?
$url
Definition: ltiregstart.php:35
getProgressbar(Bucket $observer)
getCloseButtonAction(UserInteraction\Option $option, string $redirect_uri, Bucket $observer)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Option.php:19
getRefreshUrl(Bucket $observer)
addFromUrlToNextRequest(string $redirect_uri)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$message
Definition: xapiexit.php:32
getNotificationItem(int $nr_buckets)
Get the Notification Items.