ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
class.ilBookingSchedule.php
Go to the documentation of this file.
1<?php
2
24{
25 public const SCHEDULE_TYPE_TIME_PERIOD = 'time_period';
26 public const SCHEDULE_TYPE_ALL_DAY = 'all_day';
27 public const ALL_DAY_SLOT = '00:00-24:00';
28
29 protected ilDBInterface $db;
30 protected int $id = 0;
31 protected string $title = "";
32 protected int $pool_id = 0;
33 protected int $raster = 0;
34 protected int $rent_min = 0;
35 protected int $rent_max = 0;
36 protected int $auto_break = 0;
37 protected int $deadline = 0;
38 protected array $definition;
39 protected ?ilDateTime $av_from = null;
40 protected ?ilDateTime $av_to = null;
42
43 public function __construct(
44 ?int $a_id = null
45 ) {
46 global $DIC;
47
48 $this->db = $DIC->database();
49 $this->id = (int) $a_id;
50 $this->read();
51 }
52
53 public function setTitle(
54 string $a_title
55 ): void {
56 $this->title = $a_title;
57 }
58
59 public function getTitle(): string
60 {
61 return $this->title;
62 }
63
64 public function setPoolId(
65 int $a_pool_id
66 ): void {
67 $this->pool_id = $a_pool_id;
68 }
69
70 public function getPoolId(): int
71 {
72 return $this->pool_id;
73 }
74
78 public function setRaster(
79 int $a_raster
80 ): void {
81 $this->raster = $a_raster;
82 }
83
84 public function getRaster(): int
85 {
86 return $this->raster;
87 }
88
92 public function setMinRental(
93 int $a_min
94 ): void {
95 $this->rent_min = $a_min;
96 }
97
98 public function getMinRental(): int
99 {
100 return $this->rent_min;
101 }
102
106 public function setMaxRental(
107 int $a_max
108 ): void {
109 $this->rent_max = $a_max;
110 }
111
112 public function getMaxRental(): int
113 {
114 return $this->rent_max;
115 }
116
117 // set break time
118 public function setAutoBreak(int $a_break): void
119 {
120 $this->auto_break = $a_break;
121 }
122
123 public function getAutoBreak(): int
124 {
125 return $this->auto_break;
126 }
127
131 public function setDeadline(int $a_deadline): void
132 {
133 $this->deadline = $a_deadline;
134 }
135
136 public function getDeadline(): int
137 {
138 return $this->deadline;
139 }
140
144 public function setDefinition(
145 array $a_definition
146 ): void {
147 $this->definition = $a_definition;
148 }
149
150 public function getDefinition(): array
151 {
152 return $this->definition;
153 }
154
155 public function setAvailabilityFrom(
156 ?ilDateTime $a_date = null
157 ): void {
158 $this->av_from = $a_date;
159 }
160
161 public function getAvailabilityFrom(): ?ilDateTime
162 {
163 return $this->av_from;
164 }
165
166 public function setAvailabilityTo(
167 ?ilDateTime $a_date = null
168 ): void {
169 $this->av_to = $a_date;
170 }
171
172 public function getAvailabilityTo(): ?ilDateTime
173 {
174 return $this->av_to;
175 }
176
177 public function getScheduleType(): string
178 {
179 return $this->schedule_type;
180 }
181
182 public function setScheduleType(string $a_schedule_type): void
183 {
184 $this->schedule_type = $a_schedule_type;
185 }
186
187 protected function read(): void
188 {
189 $ilDB = $this->db;
190
191 if ($this->id) {
192 $set = $ilDB->query('SELECT title,raster,rent_min,rent_max,auto_break,' .
193 'deadline,av_from,av_to,schedule_type' .
194 ' FROM booking_schedule' .
195 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
196 $row = $ilDB->fetchAssoc($set);
197 $this->setTitle($row['title'] ?? "");
198 $this->setDeadline($row['deadline'] ?? 0);
199 $this->setAvailabilityFrom($row['av_from'] ? new ilDateTime($row['av_from'], IL_CAL_UNIX) : null);
200 $this->setAvailabilityTo($row['av_to'] ? new ilDateTime($row['av_to'], IL_CAL_UNIX) : null);
201 if ($row['raster']) {
202 $this->setRaster($row['raster']);
203 $this->setMinRental($row['rent_min']);
204 $this->setMaxRental($row['rent_max']);
205 $this->setAutoBreak($row['auto_break']);
206 }
207 $this->setScheduleType($row['schedule_type'] ?? self::SCHEDULE_TYPE_TIME_PERIOD);
208
209 // load definition
210 $definition = array();
211 $set = $ilDB->query('SELECT day_id,slot_id,times' .
212 ' FROM booking_schedule_slot' .
213 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
214 while ($row = $ilDB->fetchAssoc($set)) {
215 $definition[$row["day_id"]][$row["slot_id"]] = $row["times"];
216 }
217 $this->setDefinition($definition);
218 }
219 }
220
221 public function save(): ?int
222 {
223 $ilDB = $this->db;
224
225 if ($this->id) {
226 return null;
227 }
228
229 $this->id = $ilDB->nextId('booking_schedule');
230
231 $av_from = ($this->getAvailabilityFrom() && !$this->getAvailabilityFrom()->isNull())
232 ? $this->getAvailabilityFrom()->get(IL_CAL_UNIX)
233 : null;
234 $av_to = ($this->getAvailabilityTo() && !$this->getAvailabilityTo()->isNull())
235 ? $this->getAvailabilityTo()->get(IL_CAL_UNIX)
236 : null;
237
238 $ilDB->manipulate('INSERT INTO booking_schedule' .
239 ' (booking_schedule_id,title,pool_id,raster,rent_min,rent_max,auto_break,' .
240 'deadline,av_from,av_to,schedule_type)' .
241 ' VALUES (' . $ilDB->quote($this->id, 'integer') . ',' . $ilDB->quote($this->getTitle(), 'text') .
242 ',' . $ilDB->quote($this->getPoolId(), 'integer') . ',' . $ilDB->quote($this->getRaster(), 'integer') .
243 ',' . $ilDB->quote($this->getMinRental(), 'integer') . ',' . $ilDB->quote($this->getMaxRental(), 'integer') .
244 ',' . $ilDB->quote($this->getAutoBreak(), 'integer') . ',' . $ilDB->quote($this->getDeadline(), 'integer') .
245 ',' . $ilDB->quote($av_from, 'integer') . ',' . $ilDB->quote($av_to, 'integer') .
246 ',' . $ilDB->quote($this->getScheduleType(), 'text') . ')');
247
248 $this->saveDefinition();
249
250 return $this->id;
251 }
252
253 public function update(): bool
254 {
255 $ilDB = $this->db;
256
257 if (!$this->id) {
258 return false;
259 }
260
261 $av_from = ($this->getAvailabilityFrom() && !$this->getAvailabilityFrom()->isNull())
262 ? $this->getAvailabilityFrom()->get(IL_CAL_UNIX)
263 : null;
264 $av_to = ($this->getAvailabilityTo() && !$this->getAvailabilityTo()->isNull())
265 ? $this->getAvailabilityTo()->get(IL_CAL_UNIX)
266 : null;
267
268 $ilDB->manipulate('UPDATE booking_schedule' .
269 ' SET title = ' . $ilDB->quote($this->getTitle(), 'text') .
270 ', pool_id = ' . $ilDB->quote($this->getPoolId(), 'integer') .
271 ', raster = ' . $ilDB->quote($this->getRaster(), 'integer') .
272 ', rent_min = ' . $ilDB->quote($this->getMinRental(), 'integer') .
273 ', rent_max = ' . $ilDB->quote($this->getMaxRental(), 'integer') .
274 ', auto_break = ' . $ilDB->quote($this->getAutoBreak(), 'integer') .
275 ', deadline = ' . $ilDB->quote($this->getDeadline(), 'integer') .
276 ', av_from = ' . $ilDB->quote($av_from, 'integer') .
277 ', av_to = ' . $ilDB->quote($av_to, 'integer') .
278 ', schedule_type = ' . $ilDB->quote($this->getScheduleType(), 'text') .
279 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
280
281 $this->saveDefinition();
282 return true;
283 }
284
285 public function doClone(int $a_pool_id): int
286 {
287 $new_obj = new self();
288 $new_obj->setPoolId($a_pool_id);
289 $new_obj->setTitle($this->getTitle());
290 $new_obj->setRaster($this->getRaster());
291 $new_obj->setMinRental($this->getMinRental());
292 $new_obj->setMaxRental($this->getMaxRental());
293 $new_obj->setAutoBreak($this->getAutoBreak());
294 $new_obj->setDeadline($this->getDeadline());
295 $new_obj->setDefinition($this->getDefinition());
296 $new_obj->setAvailabilityFrom($this->getAvailabilityFrom());
297 $new_obj->setAvailabilityTo($this->getAvailabilityTo());
298 $new_obj->setScheduleType($this->getScheduleType());
299 return $new_obj->save();
300 }
301
305 protected function saveDefinition(): bool
306 {
307 $ilDB = $this->db;
308
309 if (!$this->id) {
310 return false;
311 }
312
313 $ilDB->manipulate('DELETE FROM booking_schedule_slot' .
314 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
315
316 $definition = $this->getDefinition();
317 if ($definition) {
318 foreach ($definition as $day_id => $slots) {
319 foreach ($slots as $slot_id => $times) {
320 $fields = array(
321 "booking_schedule_id" => array('integer', $this->id),
322 "day_id" => array('text', $day_id),
323 "slot_id" => array('integer', $slot_id),
324 "times" => array('text', $times)
325 );
326 $ilDB->insert('booking_schedule_slot', $fields);
327 }
328 }
329 }
330 return true;
331 }
332
336 public static function hasExistingSchedules(int $a_pool_id): bool
337 {
338 global $DIC;
339
340 $ilDB = $DIC->database();
341
342 $set = $ilDB->query("SELECT booking_schedule_id" .
343 " FROM booking_schedule" .
344 " WHERE pool_id = " . $ilDB->quote($a_pool_id, 'integer'));
345 return (bool) $ilDB->numRows($set);
346 }
347
351 public static function getList(int $a_pool_id): array
352 {
353 global $DIC;
354
355 $ilDB = $DIC->database();
356
357 $set = $ilDB->query('SELECT s.booking_schedule_id,s.title,' .
358 'MAX(o.schedule_id) AS object_has_schedule' .
359 ' FROM booking_schedule s' .
360 ' LEFT JOIN booking_object o ON (s.booking_schedule_id = o.schedule_id)' .
361 ' WHERE s.pool_id = ' . $ilDB->quote($a_pool_id, 'integer') .
362 ' GROUP BY s.booking_schedule_id,s.title' .
363 ' ORDER BY s.title');
364 $res = array();
365 while ($row = $ilDB->fetchAssoc($set)) {
366 if (!$row['object_has_schedule']) {
367 $row['is_used'] = false;
368 } else {
369 $row['is_used'] = true;
370 }
371 $res[] = $row;
372 }
373 return $res;
374 }
375
376 public function delete(): int
377 {
378 $ilDB = $this->db;
379
380 if ($this->id) {
381 return $ilDB->manipulate('DELETE FROM booking_schedule' .
382 ' WHERE booking_schedule_id = ' . $ilDB->quote($this->id, 'integer'));
383 }
384 return 0;
385 }
386
390 public function getDefinitionBySlots(): array
391 {
392 $def = $this->getDefinition();
393 $slots = array();
394 foreach ($def as $day => $times) {
395 foreach ($times as $time) {
396 $slots[$time][] = $day;
397 }
398 }
399 foreach ($slots as $time => $days) {
400 $slots[$time] = array_unique($days);
401 }
402 ksort($slots);
403 return $slots;
404 }
405
406 public function setDefinitionBySlots(array $a_def): void
407 {
408 $slots = array();
409 foreach ($a_def as $time => $days) {
410 foreach ($days as $day) {
411 $slots[$day][] = $time;
412 }
413 }
414 $this->setDefinition($slots);
415 }
416}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const IL_CAL_UNIX
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setAvailabilityFrom(?ilDateTime $a_date=null)
static hasExistingSchedules(int $a_pool_id)
Check if given pool has any defined schedules.
setScheduleType(string $a_schedule_type)
setRaster(int $a_raster)
Set booking raster (in minutes)
setDefinitionBySlots(array $a_def)
setMinRental(int $a_min)
Set minimum rental time.
static getList(int $a_pool_id)
Get list of booking objects for given pool.
setMaxRental(int $a_max)
Set maximum rental time.
saveDefinition()
Save current definition (slots)
getDefinitionBySlots()
Return definition grouped by slots (not days)
setDeadline(int $a_deadline)
Set deadline.
setAvailabilityTo(?ilDateTime $a_date=null)
setDefinition(array $a_definition)
Set definition.
__construct(?int $a_id=null)
@classDescription Date and time handling
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
Interface ilDBInterface.
foreach($_POST as $key=> $value) $res
global $DIC
Definition: shib_login.php:26