ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilBookingReservation.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
13{
17 protected $db;
18
19 protected $id; // int
20 protected $object_id; // int
21 protected $user_id; // int
22 protected $from; // timestamp
23 protected $to; // timestamp
24 protected $status; // status
25 protected $group_id; // int
26 protected $assigner_id; // int
27
28 const STATUS_IN_USE = 2;
30
38 public function __construct($a_id = null)
39 {
40 global $DIC;
41
42 $this->db = $DIC->database();
43 $this->id = (int) $a_id;
44 $this->read();
45 }
46
51 public function getId()
52 {
53 return $this->id;
54 }
55
60 public function setObjectId($a_object_id)
61 {
62 $this->object_id = $a_object_id;
63 }
64
69 public function getObjectId()
70 {
71 return $this->object_id;
72 }
73
78 public function setUserId($a_user_id)
79 {
80 $this->user_id = (int) $a_user_id;
81 }
82
87 public function getUserId()
88 {
89 return $this->user_id;
90 }
91
96 public function setAssignerId($a_assigner_id)
97 {
98 $this->assigner_id = (int) $a_assigner_id;
99 }
100
105 public function getAssignerId()
106 {
107 return $this->assigner_id;
108 }
109
114 public function setFrom($a_from)
115 {
116 $this->from = (int) $a_from;
117 }
118
123 public function getFrom()
124 {
125 return $this->from;
126 }
127
132 public function setTo($a_to)
133 {
134 $this->to = (int) $a_to;
135 }
136
141 public function getTo()
142 {
143 return $this->to;
144 }
145
150 public function setStatus($a_status)
151 {
152 if ($a_status === null) {
153 $this->status = null;
154 }
155 if ($this->isValidStatus((int) $a_status)) {
156 $this->status = (int) $a_status;
157 }
158 }
159
164 public function getStatus()
165 {
166 return $this->status;
167 }
168
174 public static function isValidStatus($a_status)
175 {
176 if (in_array($a_status, array(self::STATUS_IN_USE, self::STATUS_CANCELLED))) {
177 return true;
178 }
179 return false;
180 }
181
186 public function setGroupId($a_group_id)
187 {
188 $this->group_id = $a_group_id;
189 }
190
195 public function getGroupId()
196 {
197 return $this->group_id;
198 }
199
203 protected function read()
204 {
206
207 if ($this->id) {
208 $set = $ilDB->query('SELECT *' .
209 ' FROM booking_reservation' .
210 ' WHERE booking_reservation_id = ' . $ilDB->quote($this->id, 'integer'));
211 $row = $ilDB->fetchAssoc($set);
212 $this->setUserId($row['user_id']);
213 $this->setAssignerId($row['assigner_id']);
214 $this->setObjectId($row['object_id']);
215 $this->setFrom($row['date_from']);
216 $this->setTo($row['date_to']);
217 $this->setStatus($row['status']);
218 $this->setGroupId($row['group_id']);
219 }
220 }
221
226 public function save()
227 {
229
230 if ($this->id) {
231 return false;
232 }
233
234 $this->id = $ilDB->nextId('booking_reservation');
235
236 return $ilDB->manipulate('INSERT INTO booking_reservation' .
237 ' (booking_reservation_id,user_id,assigner_id,object_id,date_from,date_to,status,group_id)' .
238 ' VALUES (' . $ilDB->quote($this->id, 'integer') .
239 ',' . $ilDB->quote($this->getUserId(), 'integer') .
240 ',' . $ilDB->quote($this->getAssignerId(), 'integer') .
241 ',' . $ilDB->quote($this->getObjectId(), 'integer') .
242 ',' . $ilDB->quote($this->getFrom(), 'integer') .
243 ',' . $ilDB->quote($this->getTo(), 'integer') .
244 ',' . $ilDB->quote($this->getStatus(), 'integer') .
245 ',' . $ilDB->quote($this->getGroupId(), 'integer') . ')');
246 }
247
252 public function update()
253 {
255
256 if (!$this->id) {
257 return false;
258 }
259
260 /* there can only be 1
261 if($this->getStatus() == self::STATUS_IN_USE)
262 {
263 $ilDB->manipulate('UPDATE booking_reservation'.
264 ' SET status = '.$ilDB->quote(NULL, 'integer').
265 ' WHERE object_id = '.$ilDB->quote($this->getObjectId(), 'integer').
266 ' AND status = '.$ilDB->quote(self::STATUS_IN_USE, 'integer'));
267 }
268 */
269
270 return $ilDB->manipulate('UPDATE booking_reservation' .
271 ' SET object_id = ' . $ilDB->quote($this->getObjectId(), 'text') .
272 ', user_id = ' . $ilDB->quote($this->getUserId(), 'integer') .
273 ', assigner_id = ' . $ilDB->quote($this->getAssignerId(), 'integer') .
274 ', date_from = ' . $ilDB->quote($this->getFrom(), 'integer') .
275 ', date_to = ' . $ilDB->quote($this->getTo(), 'integer') .
276 ', status = ' . $ilDB->quote($this->getStatus(), 'integer') .
277 ', group_id = ' . $ilDB->quote($this->getGroupId(), 'integer') .
278 ' WHERE booking_reservation_id = ' . $ilDB->quote($this->id, 'integer'));
279 }
280
285 public function delete()
286 {
288
289 if ($this->id) {
290 return $ilDB->manipulate('DELETE FROM booking_reservation' .
291 ' WHERE booking_reservation_id = ' . $ilDB->quote($this->id, 'integer'));
292 }
293 }
294
299 public static function getNewGroupId()
300 {
301 global $DIC;
302
303 $ilDB = $DIC->database();
304
305 return $ilDB->nextId('booking_reservation_group');
306 }
307
316 public static function getAvailableObject(array $a_ids, $a_from, $a_to, $a_return_single = true, $a_return_counter = false)
317 {
318 global $DIC;
319
320 $ilDB = $DIC->database();
321
323
324 $from = $ilDB->quote($a_from, 'integer');
325 $to = $ilDB->quote($a_to, 'integer');
326
327 $set = $ilDB->query('SELECT count(*) cnt, object_id' .
328 ' FROM booking_reservation' .
329 ' WHERE ' . $ilDB->in('object_id', $a_ids, '', 'integer') .
330 ' AND (status IS NULL OR status <> ' . $ilDB->quote(self::STATUS_CANCELLED, 'integer') . ')' .
331 ' AND ((date_from <= ' . $from . ' AND date_to >= ' . $from . ')' .
332 ' OR (date_from <= ' . $to . ' AND date_to >= ' . $to . ')' .
333 ' OR (date_from >= ' . $from . ' AND date_to <= ' . $to . '))' .
334 ' GROUP BY object_id');
335 $blocked = $counter = array();
336 while ($row = $ilDB->fetchAssoc($set)) {
337 if ($row['cnt'] >= $nr_map[$row['object_id']]) {
338 $blocked[] = $row['object_id'];
339 } elseif ($a_return_counter) {
340 $counter[$row['object_id']] = (int) $nr_map[$row['object_id']] - (int) $row['cnt'];
341 }
342 }
343
344 // #17868 - validate against schedule availability
345 foreach ($a_ids as $obj_id) {
346 $bobj = new ilBookingObject($obj_id);
347 if ($bobj->getScheduleId()) {
348 include_once "Modules/BookingManager/classes/class.ilBookingSchedule.php";
349 $schedule = new ilBookingSchedule($bobj->getScheduleId());
350
351 $av_from = ($schedule->getAvailabilityFrom() && !$schedule->getAvailabilityFrom()->isNull())
352 ? $schedule->getAvailabilityFrom()->get(IL_CAL_UNIX)
353 : null;
354 $av_to = ($schedule->getAvailabilityTo() && !$schedule->getAvailabilityTo()->isNull())
355 ? strtotime($schedule->getAvailabilityTo()->get(IL_CAL_DATE) . " 23:59:59")
356 : null;
357
358 if (($av_from && $a_from < $av_from) ||
359 ($av_to && $a_to > $av_to)) {
360 $blocked[] = $obj_id;
361 unset($counter[$obj_id]);
362 }
363 }
364 }
365
366 $available = array_diff($a_ids, $blocked);
367 if (sizeof($available)) {
368 if ($a_return_counter) {
369 foreach ($a_ids as $id) {
370 if (!isset($counter[$id])) {
371 $counter[$id] = (int) $nr_map[$id];
372 }
373 }
374 return $counter;
375 } elseif ($a_return_single) {
376 return array_shift($available);
377 } else {
378 return $available;
379 }
380 }
381 }
382
383 public static function isObjectAvailableInPeriod($a_obj_id, ilBookingSchedule $a_schedule, $a_from, $a_to)
384 {
385 global $DIC;
386
387 $ilDB = $DIC->database();
388
389 if (!$a_from) {
390 $a_from = time();
391 }
392 if (!$a_to) {
393 $a_to = strtotime("+1year", $a_from);
394 }
395
396 if ($a_from > $a_to) {
397 return;
398 }
399
400 $from = $ilDB->quote($a_from, 'integer');
401 $to = $ilDB->quote($a_to, 'integer');
402
403 // all bookings in period
404 $now = time();
405 $set = $ilDB->query('SELECT count(*) cnt' .
406 ' FROM booking_reservation' .
407 ' WHERE object_id = ' . $ilDB->quote($a_obj_id, 'integer') .
408 ' AND (status IS NULL OR status <> ' . $ilDB->quote(self::STATUS_CANCELLED, 'integer') . ')' .
409 ' AND date_to > ' . $now .
410 ' AND ((date_from <= ' . $from . ' AND date_to >= ' . $from . ')' .
411 ' OR (date_from <= ' . $to . ' AND date_to >= ' . $to . ')' .
412 ' OR (date_from >= ' . $from . ' AND date_to <= ' . $to . '))');
413 $row = $ilDB->fetchAssoc($set);
414 $booked_in_period = $row["cnt"];
415
416 $per_slot = ilBookingObject::getNrOfItemsForObjects(array($a_obj_id));
417 $per_slot = $per_slot[$a_obj_id];
418
419 // max available nr of items per (week)day
420 $schedule_slots = array();
421 $definition = $a_schedule->getDefinition();
422 $map = array_flip(array("su", "mo", "tu", "we", "th", "fr", "sa"));
423 foreach ($definition as $day => $day_slots) {
424 $schedule_slots[$map[$day]] = $day_slots;
425 }
426
427 $av_from = ($a_schedule->getAvailabilityFrom() && !$a_schedule->getAvailabilityFrom()->isNull())
428 ? $a_schedule->getAvailabilityFrom()->get(IL_CAL_UNIX)
429 : null;
430 $av_to = ($a_schedule->getAvailabilityTo() && !$a_schedule->getAvailabilityTo()->isNull())
431 ? strtotime($a_schedule->getAvailabilityTo()->get(IL_CAL_DATE) . " 23:59:59")
432 : null;
433
434 // sum up max available items in period per (week)day
435 $available_in_period = 0;
436 $loop = 0;
437 while ($a_from < $a_to &&
438 ++$loop < 1000) {
439 // any slots for current weekday?
440 $day_slots = $schedule_slots[date("w", $a_from)];
441 if ($day_slots) {
442 foreach ($day_slots as $slot) {
443 // convert slot to current datetime
444 $slot = explode("-", $slot);
445 $slot_from = strtotime(date("Y-m-d", $a_from) . " " . $slot[0]);
446 $slot_to = strtotime(date("Y-m-d", $a_from) . " " . $slot[1]);
447
448 // slot has to be in the future and part of schedule availability
449 if ($slot_to > time() &&
450 $slot_from >= $av_from &&
451 $slot_to <= $av_to) {
452 $available_in_period += $per_slot;
453 }
454 }
455 }
456
457 $a_from += (60 * 60 * 24);
458 }
459
460 if ($available_in_period - $booked_in_period > 0) {
461 return true;
462 }
463
464 return false;
465 }
466
467 //check if the user reached the limit of bookings in this booking pool.
468 public static function isBookingPoolLimitReachedByUser(int $a_user_id, int $a_pool_id) : int
469 {
470 global $DIC;
471 $ilDB = $DIC->database();
472
473 $booking_pool_objects = ilBookingObject::getObjectsForPool($a_pool_id);
474
475 $query = "SELECT count(user_id) total" .
476 " FROM booking_reservation" .
477 " WHERE " . $ilDB->in('object_id', $booking_pool_objects, false, 'integer') .
478 " AND user_id = " . $a_user_id .
479 " AND (status IS NULL OR status <> " . ilBookingReservation::STATUS_CANCELLED . ')';
480 $res = $ilDB->query($query);
482
483 return (int) $row['total'];
484 }
485
486 public static function getMembersWithoutReservation(int $a_object_id) : array
487 {
488 global $DIC;
489 $ilDB = $DIC->database();
490
491 $pool_id = ilBookingObject::lookupPoolId($a_object_id);
492
493 $res = array();
494 $query = 'SELECT DISTINCT bm.user_id user_id' .
495 ' FROM booking_member bm' .
496 ' WHERE bm.booking_pool_id = ' . $ilDB->quote($pool_id, 'integer') .
497 ' AND bm.user_id NOT IN (' .
498 'SELECT user_id' .
499 ' FROM booking_reservation' .
500 ' WHERE object_id = ' . $ilDB->quote($a_object_id, 'integer') .
501 ' AND (status IS NULL OR status <> ' . ilBookingReservation::STATUS_CANCELLED . '))';
502
503 $set = $ilDB->query($query);
504
505 while ($row = $ilDB->fetchAssoc($set)) {
506 $res[] = $row['user_id'];
507 }
508
509 return $res;
510 }
511
512 public static function isObjectAvailableNoSchedule($a_obj_id)
513 {
514 $available = self::getNumAvailablesNoSchedule($a_obj_id);
515 return (bool) $available; // #11864
516 }
517 public static function numAvailableFromObjectNoSchedule($a_obj_id)
518 {
519 $available = self::getNumAvailablesNoSchedule($a_obj_id);
520 return (int) $available;
521 }
522
523 public static function getNumAvailablesNoSchedule($a_obj_id)
524 {
525 global $DIC;
526
527 $ilDB = $DIC->database();
528
529 $all = ilBookingObject::getNrOfItemsForObjects(array($a_obj_id));
530 $all = (int) $all[$a_obj_id];
531
532 $set = $ilDB->query('SELECT COUNT(*) cnt' .
533 ' FROM booking_reservation r' .
534 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)' .
535 ' WHERE (status IS NULL OR status <> ' . $ilDB->quote(self::STATUS_CANCELLED, 'integer') . ')' .
536 ' AND r.object_id = ' . $ilDB->quote($a_obj_id, 'integer'));
537 $cnt = $ilDB->fetchAssoc($set);
538 $cnt = (int) $cnt['cnt'];
539
540 return (int) $all - $cnt; // #11864
541 }
542
548 public static function getCurrentOrUpcomingReservation($a_object_id)
549 {
550 global $DIC;
551
552 $ilDB = $DIC->database();
553
554 $now = $ilDB->quote(time(), 'integer');
555
556 $ilDB->setLimit(1);
557 $set = $ilDB->query('SELECT user_id, status, date_from, date_to' .
558 ' FROM booking_reservation' .
559 ' WHERE ((date_from <= ' . $now . ' AND date_to >= ' . $now . ')' .
560 ' OR date_from > ' . $now . ')' .
561 ' AND (status <> ' . $ilDB->quote(self::STATUS_CANCELLED, 'integer') .
562 ' OR STATUS IS NULL) AND object_id = ' . $ilDB->quote($a_object_id, 'integer') .
563 ' ORDER BY date_from');
564 $row = $ilDB->fetchAssoc($set);
565 return $row;
566 }
567
568 public static function getObjectReservationForUser($a_object_id, $a_user_id, $a_multi = false)
569 {
570 global $DIC;
571
572 $ilDB = $DIC->database();
573
574 $set = $ilDB->query('SELECT booking_reservation_id FROM booking_reservation' .
575 ' WHERE user_id = ' . $ilDB->quote($a_user_id, 'integer') .
576 ' AND object_id = ' . $ilDB->quote($a_object_id, 'integer') .
577 ' AND (status <> ' . $ilDB->quote(self::STATUS_CANCELLED, 'integer') .
578 ' OR STATUS IS NULL)');
579 if (!$a_multi) {
580 $row = $ilDB->fetchAssoc($set);
581 return $row['booking_reservation_id'];
582 } else {
583 $res = array();
584 while ($row = $ilDB->fetchAssoc($set)) {
585 $res[] = $row['booking_reservation_id'];
586 }
587 return $res;
588 }
589 }
590
599 public static function getList($a_object_ids, $a_limit = 10, $a_offset = 0, array $filter)
600 {
601 global $DIC;
602
603 $ilDB = $DIC->database();
604
605 $sql = 'SELECT r.*,o.title' .
606 ' FROM booking_reservation r' .
607 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
608
609 $count_sql = 'SELECT COUNT(*) AS counter' .
610 ' FROM booking_reservation r' .
611 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
612
613 $where = array($ilDB->in('r.object_id', $a_object_ids, '', 'integer'));
614 if ($filter['status']) {
615 if ($filter['status'] > 0) {
616 $where[] = 'status = ' . $ilDB->quote($filter['status'], 'integer');
617 } else {
618 $where[] = '(status != ' . $ilDB->quote(-$filter['status'], 'integer') .
619 ' OR status IS NULL)';
620 }
621 }
622 if ($filter['from']) {
623 $where[] = 'date_from >= ' . $ilDB->quote($filter['from'], 'integer');
624 }
625 if ($filter['to']) {
626 $where[] = 'date_to <= ' . $ilDB->quote($filter['to'], 'integer');
627 }
628 if (sizeof($where)) {
629 $sql .= ' WHERE ' . implode(' AND ', $where);
630 $count_sql .= ' WHERE ' . implode(' AND ', $where);
631 }
632
633 $set = $ilDB->query($count_sql);
634 $row = $ilDB->fetchAssoc($set);
635 $counter = $row['counter'];
636
637 $sql .= ' ORDER BY date_from DESC, booking_reservation_id DESC';
638
639 $ilDB->setLimit($a_limit, $a_offset);
640 $set = $ilDB->query($sql);
641 $res = array();
642 while ($row = $ilDB->fetchAssoc($set)) {
643 $res[] = $row;
644 }
645
646 return array('data' => $res, 'counter' => $counter);
647 }
648
657 public static function getListByDate(
658 $a_has_schedule,
659 array $a_object_ids = null,
660 array $filter = null,
661 array $a_pool_ids = null
662 ) {
663 global $DIC;
664
665 $ilDB = $DIC->database();
666
667 $res = array();
668
669 $sql = 'SELECT r.*, o.title, o.pool_id' .
670 ' FROM booking_reservation r' .
671 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
672
673 if ($a_pool_ids !== null) {
674 $where = array($ilDB->in('pool_id', $a_pool_ids, '', 'integer'));
675 }
676
677 if ($a_object_ids !== null) {
678 $where = array($ilDB->in('object_id', $a_object_ids, '', 'integer'));
679 }
680
681 if ($filter['status']) {
682 if ($filter['status'] > 0) {
683 $where[] = 'status = ' . $ilDB->quote($filter['status'], 'integer');
684 } else {
685 $where[] = '(status != ' . $ilDB->quote(-$filter['status'], 'integer') .
686 ' OR status IS NULL)';
687 }
688 }
689 if ($filter['title']) {
690 $where[] = '(' . $ilDB->like('title', 'text', '%' . $filter['title'] . '%') .
691 ' OR ' . $ilDB->like('description', 'text', '%' . $filter['title'] . '%') . ')';
692 }
693 if ($a_has_schedule) {
694 if ($filter['from']) {
695 $where[] = 'date_from >= ' . $ilDB->quote($filter['from'], 'integer');
696 }
697 if ($filter['to']) {
698 $where[] = 'date_to <= ' . $ilDB->quote($filter['to'], 'integer');
699 }
700 if (!$filter['past']) {
701 $where[] = 'date_to > ' . $ilDB->quote(time(), 'integer');
702 }
703 }
704 if ($filter['user_id']) { // #16584
705 $where[] = 'user_id = ' . $ilDB->quote($filter['user_id'], 'integer');
706 }
707 /*
708 if($a_group_id)
709 {
710 $where[] = 'group_id = '.$ilDB->quote(substr($a_group_id, 1), 'integer');
711 }
712 */
713 if (sizeof($where)) {
714 $sql .= ' WHERE ' . implode(' AND ', $where);
715 }
716
717 if ($a_has_schedule) {
718 $sql .= ' ORDER BY date_from DESC';
719 } else {
720 // #16155 - could be cancelled and re-booked
721 $sql .= ' ORDER BY status';
722 }
723
724 $set = $ilDB->query($sql);
725 while ($row = $ilDB->fetchAssoc($set)) {
726 $obj_id = $row["object_id"];
727 $user_id = $row["user_id"];
728
729 if ($a_has_schedule) {
730 $slot = $row["date_from"] . "_" . $row["date_to"];
731 $idx = $obj_id . "_" . $user_id . "_" . $slot;
732 } else {
733 $idx = $obj_id . "_" . $user_id;
734 }
735
736 if ($a_has_schedule && $filter["slot"]) {
737 $slot_idx = date("w", $row["date_from"]) . "_" . date("H:i", $row["date_from"]) .
738 "-" . date("H:i", $row["date_to"] + 1);
739 if ($filter["slot"] != $slot_idx) {
740 continue;
741 }
742 }
743
744 if (!isset($res[$idx])) {
746
747 $res[$idx] = array(
748 "object_id" => $obj_id
749 ,"title" => $row["title"]
750 ,"pool_id" => $row["pool_id"]
751 ,"user_id" => $user_id
752 ,"counter" => 1
753 ,"user_name" => $uname["lastname"] . ", " . $uname["firstname"] // #17862
754 );
755
756 if ($a_has_schedule) {
757 $res[$idx]["booking_reservation_id"] = $idx;
758 $res[$idx]["date"] = date("Y-m-d", $row["date_from"]);
759 $res[$idx]["slot"] = date("H:i", $row["date_from"]) . " - " .
760 date("H:i", $row["date_to"] + 1);
761 $res[$idx]["week"] = date("W", $row["date_from"]);
762 $res[$idx]["weekday"] = date("w", $row["date_from"]);
763 $res[$idx]["can_be_cancelled"] = ($row["status"] != self::STATUS_CANCELLED &&
764 $row["date_from"] > time());
765 $res[$idx]["_sortdate"] = $row["date_from"];
766 } else {
767 $res[$idx]["booking_reservation_id"] = $row["booking_reservation_id"];
768 $res[$idx]["status"] = $row["status"];
769 $res[$idx]["can_be_cancelled"] = ($row["status"] != self::STATUS_CANCELLED);
770 }
771 } else {
772 $res[$idx]["counter"]++;
773 }
774 }
775
776 return $res;
777 }
778
785 public static function getUserFilter(array $a_object_ids)
786 {
787 global $DIC;
788
789 $ilDB = $DIC->database();
790
791 $res = array();
792
793 $sql = "SELECT ud.usr_id,ud.lastname,ud.firstname,ud.login" .
794 " FROM usr_data ud " .
795 " LEFT JOIN booking_reservation r ON (r.user_id = ud.usr_id)" .
796 " WHERE ud.usr_id <> " . $ilDB->quote(ANONYMOUS_USER_ID, "integer") .
797 " AND " . $ilDB->in("r.object_id", $a_object_ids, "", "integer") .
798 " ORDER BY ud.lastname,ud.firstname";
799 $set = $ilDB->query($sql);
800 while ($row = $ilDB->fetchAssoc($set)) {
801 $res[$row["usr_id"]] = $row["lastname"] . ", " . $row["firstname"] .
802 " (" . $row["login"] . ")";
803 }
804
805 return $res;
806 }
807
817 /*
818 static function getGroupedList($a_object_ids, $a_limit = 10, $a_offset = 0, array $filter = null, $a_group_id = null)
819 {
820 global $ilDB;
821
822 // CURRENTLY UNUSED!!!
823 return;
824
825 // find matching groups / reservations
826
827 $sql = 'SELECT booking_reservation_id, group_id'.
828 ' FROM booking_reservation';
829
830 $where = array($ilDB->in('object_id', $a_object_ids, '', 'integer'));
831 if($filter['status'])
832 {
833 if($filter['status'] > 0)
834 {
835 $where[] = 'status = '.$ilDB->quote($filter['status'], 'integer');
836 }
837 else
838 {
839 $where[] = '(status != '.$ilDB->quote(-$filter['status'], 'integer').
840 ' OR status IS NULL)';
841 }
842 }
843 if($filter['from'])
844 {
845 $where[] = 'date_from >= '.$ilDB->quote($filter['from'], 'integer');
846 }
847 if($filter['to'])
848 {
849 $where[] = 'date_to <= '.$ilDB->quote($filter['to'], 'integer');
850 }
851 if($filter['user_id'])
852 {
853 $where[] = 'user_id = '.$ilDB->quote($filter['user_id'], 'integer');
854 }
855 if($a_group_id)
856 {
857 $where[] = 'group_id = '.$ilDB->quote(substr($a_group_id, 1), 'integer');
858 }
859 if(sizeof($where))
860 {
861 $sql .= ' WHERE '.implode(' AND ', $where);
862 }
863
864 $grp_ids = $rsv_ids = array();
865 $set = $ilDB->query($sql);
866 while($row = $ilDB->fetchAssoc($set))
867 {
868 if($row["group_id"])
869 {
870 $grp_ids[] = $row["group_id"];
871 }
872 else
873 {
874 $rsv_ids[] = $row["booking_reservation_id"];
875 }
876 }
877
878 $res = array();
879
880 // get complete groups (and/or reservations)
881
882 if($grp_ids || $rsv_ids)
883 {
884 $grp_ids = array_unique($grp_ids);
885
886 // if result is on last page, reduce limit to entries on last page
887 $max_page = sizeof($grp_ids)+sizeof($rsv_ids);
888 $max_page = min($a_limit, $max_page-$a_offset);
889
890 $sql = 'SELECT r.*,o.title'.
891 ' FROM booking_reservation r'.
892 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
893
894 $where = array();
895 if($grp_ids)
896 {
897 $where[] = $ilDB->in('group_id', $grp_ids, '', 'integer');
898 }
899 if($rsv_ids)
900 {
901 $where[] = $ilDB->in('booking_reservation_id', $rsv_ids, '', 'integer');
902 }
903
904 $sql .= ' WHERE ('.implode(' OR ', $where).')'.
905 ' ORDER BY date_from DESC, booking_reservation_id DESC';
906
907 $set = $ilDB->query($sql);
908 $grps = array();
909 $counter = 0;
910 while($row = $ilDB->fetchAssoc($set))
911 {
912 if($row["group_id"] && !$a_group_id)
913 {
914 if(!isset($grps[$row["group_id"]]))
915 {
916 $grps[$row["group_id"]] = 1;
917 $counter++;
918 }
919 else
920 {
921 $grps[$row["group_id"]]++;
922 }
923 }
924 else
925 {
926 $counter++;
927 }
928
929 if($a_group_id ||
930 ($counter > $a_offset &&
931 (sizeof($res) < $max_page ||
932 // if group is current page we have to get all group entries, regardless of booking period
933 ($row["group_id"] && isset($res["g".$row["group_id"]])))))
934 {
935 if($row["group_id"] && !$a_group_id)
936 {
937 $group_id = "g".$row["group_id"];
938 $res[$group_id]["group_id"] = $group_id;
939 $res[$group_id]["details"][] = $row;
940 }
941 else
942 {
943 unset($row["group_id"]);
944 $res[] = $row;
945 }
946 }
947 }
948 }
949
950 include_once('./Services/Calendar/classes/class.ilCalendarUtil.php');
951
952 foreach($res as $idx => $item)
953 {
954 if(isset($item["details"]))
955 {
956 $res[$idx]["date_from"] = null;
957 $res[$idx]["date_to"] = null;
958
959 $weekdays = $week_counter = array();
960 $recur = $last = 0;
961
962 foreach($item["details"] as $detail)
963 {
964 // same for each item
965 $res[$idx]["user_id"] = $detail["user_id"];
966 $res[$idx]["object_id"] = $detail["object_id"];
967 $res[$idx]["title"] = $detail["title"];
968 $res[$idx]["booking_reservation_id"] = $detail["booking_reservation_id"];
969
970 // recurrence/weekdays
971 $sortkey = date("wHi", $detail["date_from"])."_".date("wHi", $detail["date_to"]);
972 $weekdays[$sortkey] = ilCalendarUtil::_numericDayToString(date("w", $detail["date_from"]), false).
973 ", ".date("H:i", $detail["date_from"]).
974 " - ".date("H:i", $detail["date_to"]);
975
976 if($detail["status"] != self::STATUS_CANCELLED)
977 {
978 $week_counter[$sortkey][date("WHi", $detail["date_from"])."_".date("WHi", $detail["date_to"])]++;
979 }
980 else if(!isset($week_counter[$sortkey][date("WHi", $detail["date_from"])."_".date("WHi", $detail["date_to"])]))
981 {
982 $week_counter[$sortkey][date("WHi", $detail["date_from"])."_".date("WHi", $detail["date_to"])] = 0;
983 }
984
985 if($last && $last-$detail["date_to"] > $recur)
986 {
987 $recur = $last-$detail["date_to"];
988 }
989
990 // min/max period
991 if(!$res[$idx]["date_from"] || $detail["date_from"] < $res[$idx]["date_from"])
992 {
993 $res[$idx]["date_from"] = $detail["date_from"];
994 }
995 if(!$res[$idx]["date_to"] || $detail["date_to"] > $res[$idx]["date_to"])
996 {
997 $res[$idx]["date_to"] = $detail["date_to"];
998 }
999
1000 $last = $detail["date_to"];
1001 }
1002
1003 if(sizeof($item["details"]) > 1)
1004 {
1005 $weekdays = array_unique($weekdays);
1006 ksort($weekdays);
1007
1008 foreach($weekdays as $week_id => $weekday)
1009 {
1010 $min = min($week_counter[$week_id]);
1011 $max = max($week_counter[$week_id]);
1012 if($min == $max)
1013 {
1014 $weekdays[$week_id] .= " (".$min.")";
1015 }
1016 else
1017 {
1018 $weekdays[$week_id] .= " (".$min."-".$max.")";
1019 }
1020 }
1021
1022
1023 $res[$idx]["weekdays"] = array_values($weekdays);
1024 if($recur)
1025 {
1026 if(date("YW", $res[$idx]["date_to"]) != date("YW", $res[$idx]["date_from"]))
1027 {
1028 $recur = ceil(($recur/(60*60*24))/7);
1029 }
1030 else
1031 {
1032 $recur = 0;
1033 }
1034 }
1035 $res[$idx]["recurrence"] = (int)$recur;
1036
1037 $res[$idx]["booking_reservation_id"] = $idx;
1038 $res[$idx]["title"] .= " (".sizeof($item["details"]).")";
1039
1040 }
1041 else
1042 {
1043 // undo grouping
1044 $res[$idx] = array_pop($item["details"]);
1045 unset($res[$idx]["group_id"]);
1046 }
1047 }
1048 }
1049
1050 $res = ilUtil::sortArray($res, "date_from", "desc", true);
1051
1052 return array('data'=>$res, 'counter'=>$counter);
1053 }
1054 */
1055
1062 public static function changeStatus(array $a_ids, $a_status)
1063 {
1064 global $DIC;
1065
1066 $ilDB = $DIC->database();
1067
1068 if (self::isValidStatus($a_status)) {
1069 return $ilDB->manipulate('UPDATE booking_reservation' .
1070 ' SET status = ' . $ilDB->quote($a_status, 'integer') .
1071 ' WHERE ' . $ilDB->in('booking_reservation_id', $a_ids, '', 'integer'));
1072 }
1073 }
1074
1075 public function getCalendarEntry()
1076 {
1077 $ilDB = $this->db;
1078
1079 include_once 'Services/Calendar/classes/class.ilCalendarCategory.php';
1080
1081 $set = $ilDB->query("SELECT ce.cal_id FROM cal_entries ce" .
1082 " JOIN cal_cat_assignments cca ON ce.cal_id = cca.cal_id" .
1083 " JOIN cal_categories cc ON cca.cat_id = cc.cat_id" .
1084 " JOIN booking_reservation br ON ce.context_id = br.booking_reservation_id" .
1085 " WHERE cc.obj_id = " . $ilDB->quote($this->getUserId(), 'integer') .
1086 " AND br.user_id = " . $ilDB->quote($this->getUserId(), 'integer') .
1087 " AND cc.type = " . $ilDB->quote(ilCalendarCategory::TYPE_BOOK, 'integer') .
1088 " AND ce.context_id = " . $ilDB->quote($this->getId(), 'integer'));
1089 $row = $ilDB->fetchAssoc($set);
1090 return $row["cal_id"];
1091 }
1092
1102 public static function getCancelDetails($a_obj_id, $a_user_id, $a_from, $a_to)
1103 {
1104 global $DIC;
1105
1106 $ilDB = $DIC->database();
1107
1108 $res = array();
1109
1110 $sql = "SELECT booking_reservation_id" .
1111 " FROM booking_reservation" .
1112 " WHERE object_id = " . $ilDB->quote($a_obj_id, "integer") .
1113 " AND user_id = " . $ilDB->quote($a_user_id, "integer") .
1114 " AND date_from = " . $ilDB->quote($a_from, "integer") .
1115 " AND date_to = " . $ilDB->quote($a_to, "integer") .
1116 " AND (status IS NULL" .
1117 " OR status <> " . $ilDB->quote(self::STATUS_CANCELLED, "integer") . ")";
1118 $set = $ilDB->query($sql);
1119 while ($row = $ilDB->fetchAssoc($set)) {
1120 $res[] = $row["booking_reservation_id"];
1121 }
1122
1123 return $res;
1124 }
1125}
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_DATE
const IL_CAL_UNIX
a bookable ressource
static lookupPoolId($object_id)
Lookup pool id.
static getNrOfItemsForObjects(array $a_obj_ids)
Get nr of available items.
static getObjectsForPool(int $a_pool_id)
Get all booking pool object ids from an specific booking pool.
getFrom()
Get booking from date.
static getObjectReservationForUser($a_object_id, $a_user_id, $a_multi=false)
static getListByDate( $a_has_schedule, array $a_object_ids=null, array $filter=null, array $a_pool_ids=null)
List all reservations by date.
static getCurrentOrUpcomingReservation($a_object_id)
Get details about object reservation.
getUserId()
Get booking user id.
setTo($a_to)
Set booking to date.
static changeStatus(array $a_ids, $a_status)
List all reservations.
setObjectId($a_object_id)
Set object id.
setAssignerId($a_assigner_id)
Set assigner user id.
setFrom($a_from)
Set booking from date.
setGroupId($a_group_id)
Set group id.
static getAvailableObject(array $a_ids, $a_from, $a_to, $a_return_single=true, $a_return_counter=false)
Check if any of given objects are bookable.
static isBookingPoolLimitReachedByUser(int $a_user_id, int $a_pool_id)
__construct($a_id=null)
Constructor.
setStatus($a_status)
Set booking status.
static getMembersWithoutReservation(int $a_object_id)
static isObjectAvailableInPeriod($a_obj_id, ilBookingSchedule $a_schedule, $a_from, $a_to)
static isValidStatus($a_status)
Check if given status is valid.
static getNumAvailablesNoSchedule($a_obj_id)
static getList($a_object_ids, $a_limit=10, $a_offset=0, array $filter)
List all reservations.
static getCancelDetails($a_obj_id, $a_user_id, $a_from, $a_to)
Get reservation ids from aggregated id for cancellation.
static getNewGroupId()
Get next group id.
save()
Create new entry in db.
static getUserFilter(array $a_object_ids)
Get all users who have reservations for object(s)
setUserId($a_user_id)
Set booking user id.
static numAvailableFromObjectNoSchedule($a_obj_id)
static isObjectAvailableNoSchedule($a_obj_id)
getAssignerId()
Get assigner user id.
schedule for booking ressource
getAvailabilityTo()
Get availability end.
getAvailabilityFrom()
Get availability start.
getDefinition()
Get definition.
static _lookupName($a_user_id)
lookup user name
$row
$query
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
global $ilDB