ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.BookingProcessManager.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
19 
23 
28 {
32 
33  public function __construct(
34  InternalDataService $data,
35  InternalRepoService $repo,
36  InternalDomainService $domain
37  ) {
38  $this->data = $data;
39  $this->repo = $repo;
40  $this->domain = $domain;
41  }
42 
48  int $obj_id,
49  int $slot_from,
50  int $slot_to,
51  int $recurrence_weeks,
52  int $requested_nr,
53  \ilDate $until
54  ): array {
55  $end = $until->get(IL_CAL_UNIX);
56  $cut = 0;
57  $cycle = $recurrence_weeks * 7;
58  $booked_out_slots = [];
59  $check_slot_from = $slot_from;
60  while ($cut < 1000 && $check_slot_from <= $end) {
61  $check_slot_from = $this->addDaysStamp($slot_from, $cycle * $cut);
62  $check_slot_to = $this->addDaysStamp($slot_to, $cycle * $cut);
63  $available = \ilBookingReservation::getAvailableObject(array($obj_id), $check_slot_from, $check_slot_to, false, true);
64  $available = $available[$obj_id] ?? 0;
65  if ($available < $requested_nr) {
66  $booked_out_slots[] = [
67  "from" => $check_slot_from,
68  "to" => $check_slot_to,
69  "missing" => $requested_nr - $available
70  ];
71  }
72  $cut++;
73  }
74  return $booked_out_slots;
75  }
76 
77  protected function addDaysDate(
78  string $a_date,
79  int $a_days
80  ): string {
81  $date = date_parse($a_date);
82  $stamp = mktime(0, 0, 1, $date["month"], $date["day"] + $a_days, $date["year"]);
83  return date("Y-m-d", $stamp);
84  }
85 
86  protected function addDaysStamp(
87  int $a_stamp,
88  int $a_days
89  ): int {
90  $date = getdate($a_stamp);
91  return mktime(
92  $date["hours"],
93  $date["minutes"],
94  $date["seconds"],
95  $date["mon"],
96  $date["mday"] + $a_days,
97  $date["year"]
98  );
99  }
100 
101  public function bookAvailableObjects(
102  int $obj_id,
103  int $user_to_book,
104  int $assigner_id,
105  int $context_obj_id,
106  int $from,
107  int $to,
108  int $recurrence,
109  int $nr,
110  ?\ilDateTime $until,
111  string $message = ""
112  ): array {
113  $reservation_repo = $this->repo->reservation();
114 
115  $rsv_ids = [];
116 
117  if (is_null($until)) {
118  $end = $from;
119  $cut = 999; // only one iteration
120  $cycle = 0; // no recurrence
121  } else {
122  $end = $until->get(IL_CAL_UNIX);
123  $cut = 0;
124  $cycle = $recurrence * 7;
125  }
126  $booked_out_slots = [];
127  $check_slot_from = $from;
128  $group_id = $reservation_repo->getNewGroupId();
129  while ($cut < 1000 && $check_slot_from <= $end) {
130  $check_slot_from = $this->addDaysStamp($from, $cycle * $cut);
131  $check_slot_to = $this->addDaysStamp($to, $cycle * $cut);
132  $available = \ilBookingReservation::getAvailableObject(array($obj_id), $check_slot_from, $check_slot_to, false, true);
133  $available = $available[$obj_id] ?? 0;
134  $book_nr = min($nr, $available);
135  for ($loop = 0; $loop < $book_nr; $loop++) {
136  $rsv_ids[] = $this->bookSingle(
137  $obj_id,
138  $user_to_book,
139  $assigner_id,
140  $context_obj_id,
141  $check_slot_from,
142  $check_slot_to,
143  $group_id,
144  $message
145  );
146  $success = $obj_id;
147  }
148  $cut++;
149  }
150  return $rsv_ids;
151  }
152 
158  public function bookSingle(
159  int $object_id,
160  int $user_to_book,
161  int $assigner_id = 0,
162  int $context_obj_id = 0,
163  int $a_from = null,
164  int $a_to = null,
165  int $a_group_id = null,
166  string $message = ""
167  ): int {
168  $lng = $this->domain->lng();
169 
170  // add user to participants, if not existing
171  $pool_id = \ilBookingObject::lookupPoolId($object_id);
172  $this->domain->participants()->createIfNotExisting($user_to_book, $pool_id);
173 
174  // create new reservation
175  $reservation = new \ilBookingReservation();
176  $reservation->setObjectId($object_id);
177  $reservation->setUserId($user_to_book);
178  $reservation->setAssignerId($assigner_id);
179  $reservation->setFrom((int) $a_from);
180  $reservation->setTo((int) $a_to);
181  $reservation->setGroupId((int) $a_group_id);
182  $reservation->setContextObjId($context_obj_id);
183  $reservation->setMessage($message);
184  $reservation->save();
185 
186  // create calendar entry
187  if ($a_from) {
188  $lng->loadLanguageModule('dateplaner');
191  $user_to_book,
192  $lng->txt('cal_ch_personal_book'),
193  true
194  );
195 
196  $object = new \ilBookingObject($object_id);
197 
198  $entry = new \ilCalendarEntry();
199  $entry->setStart(new \ilDateTime($a_from, IL_CAL_UNIX));
200  $entry->setEnd(new \ilDateTime($a_to, IL_CAL_UNIX));
201  $entry->setTitle($lng->txt('book_cal_entry') . ' ' . $object->getTitle());
202  $entry->setContextId($reservation->getId());
203  $entry->save();
204 
205  $assignment = new \ilCalendarCategoryAssignments($entry->getEntryId());
206  if ($def_cat !== null) {
207  $assignment->addAssignment($def_cat->getCategoryID());
208  }
209  }
210 
211  return $reservation->getId();
212  }
213 }
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
get(int $a_format, string $a_format_str='', string $a_tz='')
static getAvailableObject(array $a_ids, int $a_from, int $a_to, bool $a_return_single=true, bool $a_return_counter=false)
Check if any of given objects are bookable.
const IL_CAL_UNIX
__construct(InternalDataService $data, InternalRepoService $repo, InternalDomainService $domain)
Author: Alexander Killing killing@leifos.de
static initDefaultCalendarByType(int $a_type_id, int $a_usr_id, string $a_title, bool $a_create=false)
Init the default calendar for given type and user.
getRecurrenceMissingAvailability(int $obj_id, int $slot_from, int $slot_to, int $recurrence_weeks, int $requested_nr, \ilDate $until)
Get missing availability of an object for a given weekly recurrence, object id, starting slot and req...
bookAvailableObjects(int $obj_id, int $user_to_book, int $assigner_id, int $context_obj_id, int $from, int $to, int $recurrence, int $nr, ?\ilDateTime $until, string $message="")
global $lng
Definition: privfeed.php:32
static lookupPoolId(int $object_id)
bookSingle(int $object_id, int $user_to_book, int $assigner_id=0, int $context_obj_id=0, int $a_from=null, int $a_to=null, int $a_group_id=null, string $message="")
Book object for date.