ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilBTPopOverGUI.php
Go to the documentation of this file.
1 <?php
2 
12 
20 {
21  use DIC;
22  use StateTranslator;
26  protected $btPersistence;
27 
28 
37  public function __construct(Factory $uiFactory, Persistence $btPersistence, \ilLanguage $lng, ilCtrl $ctrl)
38  {
39  $this->btPersistence = $btPersistence;
40  $this->lng()->loadLanguageModule('background_tasks');
41  }
42 
43 
52  public function getPopOverContent($user_id, $redirect_uri, $replace_url = '')
53  {
54  $r = $this->ui()->renderer();
55  $f = $this->ui()->factory();
56  $persistence = $this->dic()->backgroundTasks()->persistence();
57 
58  $observer_ids = $this->btPersistence->getBucketIdsOfUser($user_id, 'id', 'DESC');
59  $observers = $this->btPersistence->loadBuckets($observer_ids);
60 
61  $metas = $persistence->getBucketMetaOfUser($this->user()->getId());
62  $user_inter = count(array_filter($metas, function (BucketMeta $meta) {
63  return $meta->getState() == State::USER_INTERACTION;
64  }));
65 
66  $po_content = new ilTemplate("tpl.popover_content.html", true, true, "Services/BackgroundTasks");
67  $po_content->setVariable("BACKGROUND_TASKS_TOTAL", count($metas));
68  $po_content->setVariable("BACKGROUND_TASKS_USER_INTERACTION", $user_inter);
69 
70  $bucket = new ilTemplate("tpl.bucket.html", true, true, "Services/BackgroundTasks");
71 
72  foreach ($observers as $observer) {
73  $state = (int) $observer->getState();
74  $current_task = $observer->getCurrentTask();
75 
76  switch ($state) {
77  case State::USER_INTERACTION:
78  $bucket->setVariable("CONTENT", $r->render($this->getProgressbar($observer)));
79  $bucket->setVariable("INTERACTIONS", $r->render([
80  $this->getUserInteractionContent($observer, $redirect_uri),
81  ]));
82  break;
83  case State::RUNNING:
84  $expected = (int) $current_task->getExpectedTimeOfTaskInSeconds();
85  $possibly_failed = (bool) ($observer->getLastHeartbeat() < (time() - $expected));
86 
87  if ($possibly_failed) {
88  $bucket->setCurrentBlock('failed');
89  $bucket->setVariable("ALERT", $this->lng()->txt('task_might_be_failed'));
90  $bucket->parseCurrentBlock();
91  $this->addButton($current_task->getAbortOption(), $redirect_uri, $bucket, $observer);
92  }
93  $bucket->setVariable("CONTENT", $r->render($this->getDefaultCardContent($observer)));
94  break;
95  default:
96  $bucket->setVariable("CONTENT", $r->render($this->getDefaultCardContent($observer)));
97  break;
98  }
99 
100  if ($state === State::USER_INTERACTION) {
101  $this->addButton($current_task->getRemoveOption(), $redirect_uri, $bucket, $observer);
102  }
103 
104  $bucket->setCurrentBlock("bucket");
105  $bucket_title = $observer->getTitle() . ($state
106  == State::SCHEDULED ? " ({$this->lng()->txt("scheduled")})" : "");
107  $bucket->setVariable("BUCKET_TITLE", $bucket_title);
108  if ($observer->getDescription()) {
109  $bucket->setVariable("BUCKET_DESCRIPTION", $observer->getDescription());
110  }
111  $bucket->parseCurrentBlock();
112  }
113  $po_content->setVariable("CONTENT", $bucket->get());
114  $uiElement = $f->legacy($po_content->get());
115 
116  return $uiElement;
117  }
118 
119 
125  public function getDefaultCardContent(Bucket $observer)
126  {
127  $progressbar = $this->getProgressbar($observer);
128 
129  return $progressbar;
130  }
131 
132 
139  public function getUserInteractionContent(Bucket $observer, $redirect_uri)
140  {
141  $factory = $this->ui()->factory();
142  $renderer = $this->ui()->renderer();
143  $language = $this->lng();
144  $persistence = $this->dic()->backgroundTasks()->persistence();
145  if (!$observer->getCurrentTask() instanceof UserInteraction) {
146  return $factory->legacy("");
147  }
149  $userInteraction = $observer->getCurrentTask();
150  $options = $userInteraction->getOptions($userInteraction->getInput());
151  $buttons = array_map(function (UserInteraction\Option $option) use ($factory, $renderer, $observer, $persistence, $redirect_uri, $language) {
152  $this->ctrl()
153  ->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::SELECTED_OPTION, $option->getValue());
154  $this->ctrl()
155  ->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::OBSERVER_ID, $persistence->getBucketContainerId($observer));
156  $this->addFromUrlToNextRequest($redirect_uri);
157 
158  return $renderer->render($factory->button()
159  ->standard($language->txt($option->getLangVar()), $this->ctrl()
160  ->getLinkTargetByClass([ ilBTControllerGUI::class ], ilBTControllerGUI::CMD_USER_INTERACTION)));
161  }, $options);
162 
163  $options = implode(" ", $buttons);
164 
165  return $factory->legacy($options);
166  }
167 
168 
174  protected function getProgressbar(Bucket $observer)
175  {
176  $percentage = $observer->getOverallPercentage();
177 
178  switch (true) {
179  case ((int) $percentage === 100):
180  $running = "";
181  $content = $this->lng()->txt("completed");
182  break;
183  case ((int) $observer->getState() === State::USER_INTERACTION):
184  $running = "";
185  $content = $this->lng()->txt("waiting");
186  break;
187  default:
188  $running = "active";
189  $content = "{$percentage}%";
190  break;
191  }
192 
193  return $this->ui()->factory()->legacy(" <div class='progress'>
194  <div class='progress-bar progress-bar-striped {$running}' role='progressbar' aria-valuenow='{$percentage}'
195  aria-valuemin='0' aria-valuemax='100' style='width:{$percentage}%'>
196  {$content}
197  </div>
198  </div> ");
199  }
200 
201 
202  protected function addButton(UserInteraction\Option $option, $redirect_uri, ilTemplate $bucket, Bucket $observer)
203  {
204  $r = $this->ui()->renderer();
205  $f = $this->ui()->factory();
206  $persistence = $this->dic()->backgroundTasks()->persistence();
207  // Close Action
208  $bucket->setCurrentBlock('close_button');
209 
210  $this->ctrl()
211  ->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::OBSERVER_ID, $persistence->getBucketContainerId($observer));
212  $this->addFromUrlToNextRequest($redirect_uri);
213 
214  switch ($option->getValue()) {
215  case AbstractTask::MAIN_ABORT:
216  $action = $this->ctrl()
217  ->getLinkTargetByClass([ ilBTControllerGUI::class ], ilBTControllerGUI::CMD_ABORT);
218  break;
219  case AbstractTask::MAIN_REMOVE:
220  $action = $this->ctrl()
221  ->getLinkTargetByClass([ ilBTControllerGUI::class ], ilBTControllerGUI::CMD_REMOVE);
222  break;
223  default:
224  $this->ctrl()
225  ->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::SELECTED_OPTION, $option->getValue());
226  $action = $this->ctrl()
227  ->getLinkTargetByClass([ ilBTControllerGUI::class ], ilBTControllerGUI::CMD_USER_INTERACTION);
228  break;
229  }
230 
231  $label = $this->lng()->txt($option->getLangVar());
232 
233  $remove = $r->render($f->button()->standard($label, $action));
234 
235  $bucket->setVariable("CLOSE_BUTTON", $remove);
236  $bucket->parseCurrentBlock();
237  }
238 
239 
243  protected function addFromUrlToNextRequest($redirect_uri)
244  {
245  $this->ctrl()
246  ->setParameterByClass(ilBTControllerGUI::class, ilBTControllerGUI::FROM_URL, ilBTControllerGUI::hash($redirect_uri));
247  }
248 }
This class provides processing control methods.
Class ilBTPopOverGUI.
$action
addButton(UserInteraction\Option $option, $redirect_uri, ilTemplate $bucket, Bucket $observer)
$factory
Definition: metadata.php:43
addFromUrlToNextRequest($redirect_uri)
user()
Definition: user.php:4
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
$r
Definition: example_031.php:79
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:613
getDefaultCardContent(Bucket $observer)
$lng
This is how the factory for UI elements looks.
Definition: Factory.php:15
special template class to simplify handling of ITX/PEAR
getPopOverContent($user_id, $redirect_uri, $replace_url='')
Get the content for the popover as ui element.
getProgressbar(Bucket $observer)
setCurrentBlock($part="DEFAULT")
Überladene Funktion, die sich hier lokal noch den aktuellen Block merkt.
__construct(Factory $uiFactory, Persistence $btPersistence, \ilLanguage $lng, ilCtrl $ctrl)
ilBTPopOverGUI constructor.
language handling
parseCurrentBlock($part="DEFAULT")
Überladene Funktion, die auf den aktuelle Block vorher noch ein replace ausführt public...