ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilBookingProcessWithScheduleGUI.php
Go to the documentation of this file.
1 <?php
2 
20 
26 {
27  protected ilLogger $log;
28  protected \ILIAS\BookingManager\BookingProcess\ObjectSelectionManager $object_selection;
29  protected \ILIAS\BookingManager\Objects\ObjectsManager $object_manager;
30  protected \ILIAS\BookingManager\Reservations\ReservationManager $reservation;
31  protected \ILIAS\BookingManager\BookingProcess\ProcessUtilGUI $util_gui;
32  protected \ILIAS\BookingManager\InternalRepoService $repo;
33  protected \ILIAS\BookingManager\BookingProcess\BookingProcessManager $process;
34  protected \ILIAS\HTTP\Services $http;
35  protected \ILIAS\BookingManager\InternalGUIService $gui;
36  protected array $raw_post_data;
37  protected \ILIAS\BookingManager\StandardGUIRequest $book_request;
39  protected int $booking_object_id;
40  protected int $user_id_to_book;
41  protected int $user_id_assigner;
42  protected string $seed;
44  protected int $context_obj_id;
45  protected ilCtrl $ctrl;
47  protected ilLanguage $lng;
49  protected ilTabsGUI $tabs_gui;
50  protected ilObjUser $user;
51  protected int $book_obj_id;
52  protected array $rsv_ids = [];
53 
54  public function __construct(
55  ilObjBookingPool $pool,
56  int $booking_object_id,
57  string $seed = "",
58  int $context_obj_id = 0
59  ) {
60  global $DIC;
61 
62  $this->ctrl = $DIC->ctrl();
63  $this->tpl = $DIC["tpl"];
64  $this->lng = $DIC->language();
65  $this->access = $DIC->access();
66  $this->tabs_gui = $DIC->tabs();
67  $this->user = $DIC->user();
68  $this->http = $DIC->http();
69 
70  $this->context_obj_id = $context_obj_id;
71 
72  $this->book_obj_id = $booking_object_id;
73 
74  $this->pool = $pool;
75 
76  $this->seed = $seed;
77  $internal_service = $DIC->bookingManager()->internal();
78  $this->gui = $internal_service->gui();
79  $domain = $internal_service->domain();
80  $this->book_request = $this->gui->standardRequest();
81  $this->help = $this->gui->bookingHelp($pool);
82  $this->log = $domain->log();
83 
84  $this->repo = $internal_service->repo();
85 
86  $this->object_manager = $domain->objects($pool->getId());
87  $this->object_selection = $domain->objectSelection($pool->getId());
88 
89  $this->rsv_ids = $this->book_request->getReservationIdsFromString();
90 
91  $this->raw_post_data = $DIC->http()->request()->getParsedBody();
92 
93  $this->user_id_assigner = $this->user->getId();
94  if ($this->book_request->getBookedUser() > 0) {
95  $this->user_id_to_book = $this->book_request->getBookedUser();
96  } else {
97  $this->user_id_to_book = $this->user_id_assigner; // by default user books his own booking objects.
98  }
99  $this->ctrl->saveParameter($this, ["bkusr", "returnCmd"]);
100  $this->ctrl->setParameter($this, "seed", $this->seed);
101 
102  $this->process = $DIC->bookingManager()->internal()->domain()->process();
103  $this->reservation = $DIC->bookingManager()->internal()->domain()->reservations();
104  $this->util_gui = $DIC->bookingManager()->internal()->gui()->process()->ProcessUtilGUI(
105  $this->pool,
106  $this
107  );
108  }
109 
110  public function executeCommand(): void
111  {
112  $ctrl = $this->ctrl;
113 
114  $next_class = $ctrl->getNextClass($this);
115  $cmd = $ctrl->getCmd("show");
116  switch ($next_class) {
117  default:
118  if (in_array($cmd, array("book", "back", "week",
119  "assignParticipants",
120  "bookMultipleParticipants",
121  "saveMultipleBookings",
122  "showNumberForm",
123  "processNumberForm",
124  "checkAvailability",
125  "displayPostInfo",
126  "bookAvailableItems",
127  "deliverPostFile",
128  "selectObjects",
129  "redirectToParticipantsList"
130  ))) {
131  $this->$cmd();
132  }
133  }
134  }
135 
136 
137  //
138  // Step 0 / week view
139  //
140 
144  public function week(): void // ok
145  {
146  $tpl = $this->tpl;
147  $this->log->debug("Step 0, week");
148 
149  //$this->tabs_gui->clearTargets();
150  //$this->tabs_gui->setBackTarget($this->lng->txt('book_back_to_list'), $this->ctrl->getLinkTarget($this, 'back'));
151 
152  $this->util_gui->setHelpId("week");
153  $this->ctrl->setParameter($this, 'returnCmd', "week");
154 
155  if ($this->user_id_to_book !== $this->user_id_assigner) {
156  $this->ctrl->setParameter($this, 'bkusr', $this->user_id_to_book);
157  }
158  $user_settings = ilCalendarUserSettings::_getInstanceByUserId($this->user->getId());
159 
160  $week_gui = new \ILIAS\BookingManager\BookingProcess\WeekGUI(
161  $this,
162  "week",
163  $this->object_selection->getSelectedObjects(),
164  $this->pool->getId(),
165  $this->seed,
166  $user_settings->getWeekStart()
167  );
168  $tpl->setContent($week_gui->getHTML());
169 
170  $bar = $this->gui->toolbar();
171  $list_link = $this->ctrl->getLinkTargetByClass("ilObjBookingPoolGUI", "render");
172  $week_link = $this->ctrl->getLinkTargetByClass("ilBookingProcessWithScheduleGUI", "week");
173  $mode_control = $this->gui->ui()->factory()->viewControl()->mode([
174  $this->lng->txt("book_list") => $list_link,
175  $this->lng->txt("book_week") => $week_link
176  ], $this->lng->txt("book_view"))->withActive($this->lng->txt("book_week"));
177  $bar->addComponent($mode_control);
178 
179  $list_gui = new \ILIAS\BookingManager\BookingProcess\ObjectSelectionListGUI(
180  $this->pool->getId(),
181  $this->ctrl->getFormAction($this, "selectObjects")
182  );
183  $tpl->setRightContent($list_gui->render());
184  }
185 
186  protected function selectObjects(): void
187  {
188  $this->log->debug("selectObjects");
189  $obj_ids = $this->book_request->getObjectIds();
190  $this->object_selection->setSelectedObjects($obj_ids);
191  $this->ctrl->redirect($this, "week");
192  }
193 
194 
195  //
196  // Step 1
197  //
198 
199 
205  public function book(): void // ok
206  {
207  $this->log->debug("Step 1, book");
208  $tpl = $this->tpl;
209 
210  $this->tabs_gui->clearTargets();
211  $this->tabs_gui->setBackTarget($this->lng->txt('book_back_to_list'), $this->ctrl->getLinkTarget($this, 'back'));
212 
213  $this->util_gui->setHelpId("book");
214 
215  $obj = new ilBookingObject($this->book_obj_id);
216 
217  $this->lng->loadLanguageModule("dateplaner");
218  $this->ctrl->setParameter($this, 'object_id', $obj->getId());
219  //$this->ctrl->setParameter($this, 'returnCmd', "book");
220 
221  if ($this->user_id_to_book !== $this->user_id_assigner) {
222  $this->ctrl->setParameter($this, 'bkusr', $this->user_id_to_book);
223  }
224 
225  $user_settings = ilCalendarUserSettings::_getInstanceByUserId($this->user->getId());
226 
227  $week_gui = new \ILIAS\BookingManager\BookingProcess\WeekGUI(
228  $this,
229  "book",
230  [$obj->getId()],
231  $this->pool->getId(),
232  $this->seed,
233  $user_settings->getWeekStart()
234  );
235  $tpl->setContent($week_gui->getHTML());
236  }
237 
238  // Table to assign participants to an object.
239  public function assignParticipants(): void
240  {
241  $this->util_gui->assignParticipants($this->book_obj_id);
242  }
243 
244  public function showNumberForm(): void
245  {
246  $this->log->debug("showNumberForm");
247  $object_id = $this->book_obj_id;
248  $from = $this->book_request->getSlotFrom();
249  $to = $this->book_request->getSlotTo() - 1;
250  $this->tabs_gui->clearTargets();
251  $this->tabs_gui->setBackTarget(
252  $this->lng->txt('book_back_to_list'),
253  $this->ctrl->getLinkTarget($this, 'back')
254  );
255  $form = $this->getNumberForm($from, $to);
256  $this->gui->modal($this->getBookgingObjectTitle())
257  ->form($form)
258  ->send();
259  }
260 
261  protected function getBookgingObjectTitle(): string
262  {
263  return (new ilBookingObject($this->book_obj_id))->getTitle();
264  }
265 
266 
271  protected function getNumberForm(
272  int $from,
273  int $to
274  ): \ILIAS\Repository\Form\FormAdapterGUI {
275  $counter = $this->reservation->getAvailableNr($this->book_request->getObjectId(), $from, $to);
277  new ilDateTime($from, IL_CAL_UNIX),
278  new ilDateTime($to, IL_CAL_UNIX)
279  );
280  $this->ctrl->setParameter($this, "slot", $from . "_" . $to);
281  $form = $this->gui->form([self::class], "processNumberForm")
282  ->asyncModal()
283  ->section(
284  "props",
285  $this->lng->txt("book_confirm_booking_schedule_number_of_objects"),
286  $this->lng->txt("book_confirm_booking_schedule_number_of_objects_info")
287  )
288  ->number("nr", $period, "", 1, 1, $counter)
289  ->radio("recurrence", $this->lng->txt("book_recurrence"), "", "0")
290  ->radioOption("0", $this->lng->txt("book_no_recurrence"))
291  ->radioOption("1", $this->lng->txt("book_book_recurrence"));
292  if ($this->pool->usesMessages()) {
293  $form = $form->textarea(
294  "message",
295  $this->lng->txt("book_message"),
296  $this->lng->txt("book_message_info")
297  );
298  }
299  return $form;
300  }
301 
302  public function processNumberForm(): void
303  {
304  $this->log->debug("processNumberForm");
305  //get the user who will get the booking.
306  if ($this->book_request->getBookedUser() > 0) {
307  $this->user_id_to_book = $this->book_request->getBookedUser();
308  }
309  $slot = $this->book_request->getSlot();
310  $from = $this->book_request->getSlotFrom();
311  $to = $this->book_request->getSlotTo();
312  $obj_id = $this->book_request->getObjectId();
313 
314  if ($this->user_id_assigner !== $this->user_id_to_book) {
315  $this->ctrl->setParameterByClass(self::class, "bkusr", $this->user_id_to_book);
316  }
317  $this->ctrl->setParameterByClass(self::class, "slot", $slot);
318 
319  // form not valid -> show again
320  $form = $this->getNumberForm($from, $to);
321  if (!$form->isValid()) {
322  $this->gui->modal($this->getBookgingObjectTitle())
323  ->form($form)
324  ->send();
325  }
326 
327  $message = $this->pool->usesMessages()
328  ? $form->getData("message")
329  : "";
330 
331  // recurrence? -> show recurrence form
332  $recurrence = $form->getData("recurrence");
333  if ($recurrence === "1") {
334  if ($this->pool->usesMessages()) {
335  $this->ctrl->setParameterByClass(
336  self::class,
337  "message",
338  rawurlencode($message)
339  );
340  }
341  $this->ctrl->setParameterByClass(self::class, "object_id", $this->book_request->getObjectId());
342  $this->ctrl->setParameterByClass(self::class, "nr", (int) $form->getData("nr"));
343  $form = $this->getRecurrenceForm();
344  $this->gui->modal($this->getBookgingObjectTitle())
345  ->form($form)
346  ->send();
347  }
348  $this->checkAvailability(
349  false,
350  $form->getData("nr"),
351  $message
352  );
353  }
354 
355 
356  protected function getRecurrenceForm(): \ILIAS\Repository\Form\FormAdapterGUI
357  {
358  $this->lng->loadLanguageModule("dateplaner");
359  $today = new ilDate(time(), IL_CAL_UNIX);
360  $form = $this->gui->form([self::class], "checkAvailability")
361  ->section(
362  "props",
363  $this->lng->txt("book_confirm_booking_schedule_number_of_objects"),
364  $this->lng->txt("book_confirm_booking_schedule_number_of_objects_info")
365  )
366  ->switch("recurrence", $this->lng->txt("cal_recurrences"), "", "1")
367  ->group("1", $this->lng->txt("cal_weekly"))
368  ->date("until1", $this->lng->txt("cal_repeat_until"), "", $today)
369  ->group("2", $this->lng->txt("r_14"))
370  ->date("until2", $this->lng->txt("cal_repeat_until"), "", $today)
371  ->group("4", $this->lng->txt("r_4_weeks"))
372  ->date("until4", $this->lng->txt("cal_repeat_until"), "", $today)
373  ->end();
374  return $form;
375  }
376 
377  public function checkAvailability(
378  bool $incl_recurrence = true,
379  int $nr = 0,
380  string $message = ""
381  ): void {
382  $this->log->debug("checkAvailability");
383  $obj_id = $this->book_request->getObjectId();
384  $from = $this->book_request->getSlotFrom();
385  $to = $this->book_request->getSlotTo();
386  if ($nr === 0) {
387  $nr = $this->book_request->getNr();
388  }
389  if ($message === "" && $this->pool->usesMessages()) {
390  $message = $this->book_request->getMessage();
391  }
392  $recurrence = 0;
393  $until_ts = 0;
394  if ($incl_recurrence) {
395  $form = $this->getRecurrenceForm();
396  // recurrence form not valid -> show again
397  if (!$form->isValid()) {
398  $this->gui->modal($this->getBookgingObjectTitle())
399  ->form($form)
400  ->send();
401  }
402 
403  $recurrence = (int) $form->getData("recurrence"); // 1, 2 or 4
404  $until = $form->getData("until" . $recurrence);
405  $until_ts = $until->get(IL_CAL_UNIX);
406  }
407 
408  $this->ctrl->saveParameter($this, ["object_id", "slot", "nr"]);
409  if ($this->pool->usesMessages()) {
410  $this->ctrl->setParameter(
411  $this,
412  "message",
413  rawurlencode($message)
414  );
415  }
416  $this->ctrl->setParameter($this, "recurrence", $recurrence);
417  $this->ctrl->setParameter($this, "until", $until_ts);
418  $book_available_target = $this->getBookAvailableTarget(
419  $obj_id,
420  $this->book_request->getSlot(),
421  $recurrence,
422  $nr,
423  $until_ts
424  );
425 
426  if ($incl_recurrence) {
427 
428  $missing = $this->process->getRecurrenceMissingAvailability(
429  $obj_id,
430  $from,
431  $to,
432  $recurrence,
433  $nr,
434  $until
435  );
436 
437  // anything missing? -> send missing message
438  if (count($missing) > 0) {
439  $html = $this->getMissingAvailabilityMessage($missing);
440  $this->gui->modal($this->getBookgingObjectTitle())
441  ->legacy($html)
442  ->button(
443  $this->lng->txt("book_book_available"),
444  $book_available_target,
445  false
446  )
447  ->send();
448  }
449  }
450  $this->gui->send("<script>window.location.href = '" . $book_available_target . "';</script>");
451  }
452 
453  protected function getMissingAvailabilityMessage(array $missing): string
454  {
455  $f = $this->gui->ui()->factory();
456  $box = $f->messageBox()->failure($this->lng->txt("book_missing_availability"));
457  $items = array_map(function ($i) {
458  $from = ilDatePresentation::formatDate(new ilDateTime($i["from"], IL_CAL_UNIX));
460  return $from . " - " . $to . " : " . str_replace("$1", $i["missing"], $this->lng->txt("book_missing_items"));
461  }, $missing);
462 
463  $list = $f->listing()->unordered($items);
464  return $this->gui->ui()->renderer()->render([$box, $list]);
465  }
466 
467  protected function bookAvailableItems(?int $recurrence = null, ?ilDateTime $until = null): void
468  {
469  $this->log->debug("bookAvailableItems");
470  $obj_id = $this->book_request->getObjectId();
471  $from = $this->book_request->getSlotFrom();
472  $to = $this->book_request->getSlotTo();
473  $nr = $this->book_request->getNr();
474  $message = $this->pool->usesMessages()
475  ? $this->book_request->getMessage()
476  : "";
477  if (is_null($recurrence)) {
478  $recurrence = (int) $this->book_request->getRecurrence();
479  }
480  if (is_null($until)) {
481  if ($this->book_request->getUntil() > 0) {
482  $until = new ilDateTime($this->book_request->getUntil(), IL_CAL_UNIX);
483  }
484  }
485 
486  $booked = $this->process->bookAvailableObjects(
487  $obj_id,
488  $this->user_id_to_book,
489  $this->user_id_assigner,
490  $this->context_obj_id,
491  $from,
492  $to,
493  $recurrence,
494  $nr,
495  $until,
496  $message
497  );
498  if (count($booked) > 0) {
499  $this->util_gui->handleBookingSuccess($obj_id, "displayPostInfo", $booked);
500  } else {
501  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('book_reservation_failed'), true);
502  $this->util_gui->back();
503  }
504  }
505 
506  protected function getBookAvailableTarget(
507  int $obj_id,
508  string $slot,
509  int $recurrence,
510  int $nr,
511  int $until
512  ): string {
513  $this->ctrl->setParameter($this, "obj_id", $obj_id);
514  $this->ctrl->setParameter($this, "slot", $slot);
515  $this->ctrl->setParameter($this, "recurrence", $recurrence);
516  $this->ctrl->setParameter($this, "nr", $nr);
517  $this->ctrl->setParameter($this, "until", $until);
518  return $this->ctrl->getLinkTarget($this, "bookAvailableItems");
519  }
520 
521  public function displayPostInfo(): void
522  {
523  $this->util_gui->displayPostInfo(
524  $this->book_obj_id,
525  $this->user_id_assigner,
526  "deliverPostFile"
527  );
528  }
529 
530  public function deliverPostFile(): void
531  {
532  $this->util_gui->deliverPostFile(
533  $this->book_obj_id,
534  $this->user_id_assigner
535  );
536  }
537 
538  public function back(): void
539  {
540  $this->util_gui->back();
541  }
542 }
getBookAvailableTarget(int $obj_id, string $slot, int $recurrence, int $nr, int $until)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilObjBookingPool $pool, int $booking_object_id, string $seed="", int $context_obj_id=0)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getCmd(string $fallback_command=null)
ILIAS BookingManager InternalRepoService $repo
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider .
bookAvailableItems(?int $recurrence=null, ?ilDateTime $until=null)
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
checkAvailability(bool $incl_recurrence=true, int $nr=0, string $message="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_CAL_UNIX
static _getInstanceByUserId(int $a_user_id)
global $DIC
Definition: feed.php:28
book()
Triggered from object list week view for booking a single object / confirmation for.
ILIAS BookingManager BookingProcess BookingProcessManager $process
getNextClass($a_gui_class=null)
static http()
Fetches the global http state from ILIAS.
setContent(string $a_html)
Sets content for standard template.
ILIAS BookingManager StandardGUIRequest $book_request
ILIAS BookingManager BookingProcess ProcessUtilGUI $util_gui
ILIAS BookingManager Objects ObjectsManager $object_manager
ILIAS BookingManager Reservations ReservationManager $reservation
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
ILIAS BookingManager InternalGUIService $gui
setRightContent(string $a_html)
Sets content of right column.
$message
Definition: xapiexit.php:32
ILIAS BookingManager BookingProcess ObjectSelectionManager $object_selection
catch(ilCmiXapiException $e) send($response)
Definition: xapitoken.php:100