ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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{
14 protected $id; // int
15 protected $object_id; // int
16 protected $user_id; // int
17 protected $from; // timestamp
18 protected $to; // timestamp
19 protected $status; // status
20 protected $group_id; // int
21
22 const STATUS_IN_USE = 2;
24
32 function __construct($a_id = NULL)
33 {
34 $this->id = (int)$a_id;
35 $this->read();
36 }
37
42 function getId()
43 {
44 return $this->id;
45 }
46
51 function setObjectId($a_object_id)
52 {
53 $this->object_id = $a_object_id;
54 }
55
60 function getObjectId()
61 {
62 return $this->object_id;
63 }
64
69 function setUserId($a_user_id)
70 {
71 $this->user_id = (int)$a_user_id;
72 }
73
78 function getUserId()
79 {
80 return $this->user_id;
81 }
82
87 function setFrom($a_from)
88 {
89 $this->from = (int)$a_from;
90 }
91
96 function getFrom()
97 {
98 return $this->from;
99 }
100
105 function setTo($a_to)
106 {
107 $this->to = (int)$a_to;
108 }
109
114 function getTo()
115 {
116 return $this->to;
117 }
118
123 function setStatus($a_status)
124 {
125 if($a_status === NULL)
126 {
127 $this->status = NULL;
128 }
129 if($this->isValidStatus((int)$a_status))
130 {
131 $this->status = (int)$a_status;
132 }
133 }
134
139 function getStatus()
140 {
141 return $this->status;
142 }
143
149 static function isValidStatus($a_status)
150 {
151 if(in_array($a_status, array(self::STATUS_IN_USE, self::STATUS_CANCELLED)))
152 {
153 return true;
154 }
155 return false;
156 }
157
162 function setGroupId($a_group_id)
163 {
164 $this->group_id = $a_group_id;
165 }
166
171 function getGroupId()
172 {
173 return $this->group_id;
174 }
175
179 protected function read()
180 {
181 global $ilDB;
182
183 if($this->id)
184 {
185 $set = $ilDB->query('SELECT *'.
186 ' FROM booking_reservation'.
187 ' WHERE booking_reservation_id = '.$ilDB->quote($this->id, 'integer'));
188 $row = $ilDB->fetchAssoc($set);
189 $this->setUserId($row['user_id']);
190 $this->setObjectId($row['object_id']);
191 $this->setFrom($row['date_from']);
192 $this->setTo($row['date_to']);
193 $this->setStatus($row['status']);
194 $this->setGroupId($row['group_id']);
195 }
196 }
197
202 function save()
203 {
204 global $ilDB;
205
206 if($this->id)
207 {
208 return false;
209 }
210
211 $this->id = $ilDB->nextId('booking_reservation');
212
213 return $ilDB->manipulate('INSERT INTO booking_reservation'.
214 ' (booking_reservation_id,user_id,object_id,date_from,date_to,status,group_id)'.
215 ' VALUES ('.$ilDB->quote($this->id, 'integer').
216 ','.$ilDB->quote($this->getUserId(), 'integer').
217 ','.$ilDB->quote($this->getObjectId(), 'integer').
218 ','.$ilDB->quote($this->getFrom(), 'integer').
219 ','.$ilDB->quote($this->getTo(), 'integer').
220 ','.$ilDB->quote($this->getStatus(), 'integer').
221 ','.$ilDB->quote($this->getGroupId(), 'integer').')');
222 }
223
228 function update()
229 {
230 global $ilDB;
231
232 if(!$this->id)
233 {
234 return false;
235 }
236
237 /* there can only be 1
238 if($this->getStatus() == self::STATUS_IN_USE)
239 {
240 $ilDB->manipulate('UPDATE booking_reservation'.
241 ' SET status = '.$ilDB->quote(NULL, 'integer').
242 ' WHERE object_id = '.$ilDB->quote($this->getObjectId(), 'integer').
243 ' AND status = '.$ilDB->quote(self::STATUS_IN_USE, 'integer'));
244 }
245 */
246
247 return $ilDB->manipulate('UPDATE booking_reservation'.
248 ' SET object_id = '.$ilDB->quote($this->getObjectId(), 'text').
249 ', user_id = '.$ilDB->quote($this->getUserId(), 'integer').
250 ', date_from = '.$ilDB->quote($this->getFrom(), 'integer').
251 ', date_to = '.$ilDB->quote($this->getTo(), 'integer').
252 ', status = '.$ilDB->quote($this->getStatus(), 'integer').
253 ', group_id = '.$ilDB->quote($this->getGroupId(), 'integer').
254 ' WHERE booking_reservation_id = '.$ilDB->quote($this->id, 'integer'));
255 }
256
261 function delete()
262 {
263 global $ilDB;
264
265 if($this->id)
266 {
267 return $ilDB->manipulate('DELETE FROM booking_reservation'.
268 ' WHERE booking_reservation_id = '.$ilDB->quote($this->id, 'integer'));
269 }
270 }
271
276 public static function getNewGroupId()
277 {
278 global $ilDB;
279
280 return $ilDB->nextId('booking_reservation_group');
281 }
282
291 static function getAvailableObject(array $a_ids, $a_from, $a_to, $a_return_single = true, $a_return_counter = false)
292 {
293 global $ilDB;
294
296
297 $from = $ilDB->quote($a_from, 'integer');
298 $to = $ilDB->quote($a_to, 'integer');
299
300 $set = $ilDB->query('SELECT count(*) cnt, object_id'.
301 ' FROM booking_reservation'.
302 ' WHERE '.$ilDB->in('object_id', $a_ids, '', 'integer').
303 ' AND (status IS NULL OR status <> '.$ilDB->quote(self::STATUS_CANCELLED, 'integer').')'.
304 ' AND ((date_from <= '.$from.' AND date_to >= '.$from.')'.
305 ' OR (date_from <= '.$to.' AND date_to >= '.$to.')'.
306 ' OR (date_from >= '.$from.' AND date_to <= '.$to.'))'.
307 ' GROUP BY object_id');
308 $blocked = $counter = array();
309 while($row = $ilDB->fetchAssoc($set))
310 {
311 if($row['cnt'] >= $nr_map[$row['object_id']])
312 {
313 $blocked[] = $row['object_id'];
314 }
315 else if($a_return_counter)
316 {
317 $counter[$row['object_id']] = (int)$nr_map[$row['object_id']]-(int)$row['cnt'];
318 }
319 }
320
321 // #17868 - validate against schedule availability
322 foreach($a_ids as $obj_id)
323 {
324 $bobj = new ilBookingObject($obj_id);
325 if($bobj->getScheduleId())
326 {
327 include_once "Modules/BookingManager/classes/class.ilBookingSchedule.php";
328 $schedule = new ilBookingSchedule($bobj->getScheduleId());
329
330 $av_from = ($schedule->getAvailabilityFrom() && !$schedule->getAvailabilityFrom()->isNull())
331 ? $schedule->getAvailabilityFrom()->get(IL_CAL_UNIX)
332 : null;
333 $av_to = ($schedule->getAvailabilityTo() && !$schedule->getAvailabilityTo()->isNull())
334 ? strtotime($schedule->getAvailabilityTo()->get(IL_CAL_DATE)." 23:59:59")
335 : null;
336
337 if(($av_from && $a_from < $av_from) ||
338 ($av_to && $a_to > $av_to))
339 {
340 $blocked[] = $obj_id;
341 unset($counter[$obj_id]);
342 }
343 }
344 }
345
346 $available = array_diff($a_ids, $blocked);
347 if(sizeof($available))
348 {
349 if($a_return_counter)
350 {
351 foreach($a_ids as $id)
352 {
353 if(!isset($counter[$id]))
354 {
355 $counter[$id] = (int)$nr_map[$id];
356 }
357 }
358 return $counter;
359 }
360 else if($a_return_single)
361 {
362 return array_shift($available);
363 }
364 else
365 {
366 return $available;
367 }
368 }
369 }
370
371 static function isObjectAvailableInPeriod($a_obj_id, ilBookingSchedule $a_schedule, $a_from, $a_to)
372 {
373 global $ilDB;
374
375 if(!$a_from)
376 {
377 $a_from = time();
378 }
379 if(!$a_to)
380 {
381 $a_to = strtotime("+1year", $a_from);
382 }
383
384 if($a_from > $a_to)
385 {
386 return;
387 }
388
389 $from = $ilDB->quote($a_from, 'integer');
390 $to = $ilDB->quote($a_to, 'integer');
391
392 // all bookings in period
393 $set = $ilDB->query('SELECT count(*) cnt'.
394 ' FROM booking_reservation'.
395 ' WHERE object_id = '.$ilDB->quote($a_obj_id, 'integer').
396 ' AND (status IS NULL OR status <> '.$ilDB->quote(self::STATUS_CANCELLED, 'integer').')'.
397 ' AND ((date_from <= '.$from.' AND date_to >= '.$from.')'.
398 ' OR (date_from <= '.$to.' AND date_to >= '.$to.')'.
399 ' OR (date_from >= '.$from.' AND date_to <= '.$to.'))');
400 $row = $ilDB->fetchAssoc($set);
401 $booked_in_period = $row["cnt"];
402
403 $per_slot = ilBookingObject::getNrOfItemsForObjects(array($a_obj_id));
404 $per_slot = $per_slot[$a_obj_id];
405
406 // max available nr of items per (week)day
407 $schedule_slots = array();
408 $definition = $a_schedule->getDefinition();
409 $map = array_flip(array("su", "mo", "tu", "we", "th", "fr", "sa"));
410 foreach($definition as $day => $day_slots)
411 {
412 $schedule_slots[$map[$day]] = $day_slots;
413 }
414
415 $av_from = ($a_schedule->getAvailabilityFrom() && !$a_schedule->getAvailabilityFrom()->isNull())
416 ? $a_schedule->getAvailabilityFrom()->get(IL_CAL_UNIX)
417 : null;
418 $av_to = ($a_schedule->getAvailabilityTo() && !$a_schedule->getAvailabilityTo()->isNull())
419 ? strtotime($a_schedule->getAvailabilityTo()->get(IL_CAL_DATE)." 23:59:59")
420 : null;
421
422 // sum up max available items in period per (week)day
423 $available_in_period = 0;
424 $loop = 0;
425 while($a_from < $a_to &&
426 ++$loop < 1000)
427 {
428 // any slots for current weekday?
429 $day_slots = $schedule_slots[date("w", $a_from)];
430 if($day_slots)
431 {
432 foreach($day_slots as $slot)
433 {
434 // convert slot to current datetime
435 $slot = explode("-", $slot);
436 $slot_from = strtotime(date("Y-m-d", $a_from)." ".$slot[0]);
437 $slot_to = strtotime(date("Y-m-d", $a_from)." ".$slot[1]);
438
439 // slot has to be in the future and part of schedule availability
440 if($slot_to > time() &&
441 $slot_from >= $av_from &&
442 $slot_to <= $av_to)
443 {
444 $available_in_period += $per_slot;
445 }
446 }
447 }
448
449 $a_from += (60*60*24);
450 }
451
452 return (bool)($available_in_period-$booked_in_period);
453 }
454
455 static function isObjectAvailableNoSchedule($a_obj_id)
456 {
457 global $ilDB;
458
459 $all = ilBookingObject::getNrOfItemsForObjects(array($a_obj_id));
460 $all = (int)$all[$a_obj_id];
461
462 $set = $ilDB->query('SELECT COUNT(*) cnt'.
463 ' FROM booking_reservation r'.
464 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)'.
465 ' WHERE (status IS NULL OR status <> '.$ilDB->quote(self::STATUS_CANCELLED, 'integer').')'.
466 ' AND r.object_id = '.$ilDB->quote($a_obj_id, 'integer'));
467 $cnt = $ilDB->fetchAssoc($set);
468 $cnt = (int)$cnt['cnt'];
469
470 return (bool)($all-$cnt); // #11864
471 }
472
478 static function getCurrentOrUpcomingReservation($a_object_id)
479 {
480 global $ilDB;
481
482 $now = $ilDB->quote(time(), 'integer');
483
484 $ilDB->setLimit(1);
485 $set = $ilDB->query('SELECT user_id, status, date_from, date_to'.
486 ' FROM booking_reservation'.
487 ' WHERE ((date_from <= '.$now.' AND date_to >= '.$now.')'.
488 ' OR date_from > '.$now.')'.
489 ' AND (status <> '.$ilDB->quote(self::STATUS_CANCELLED, 'integer').
490 ' OR STATUS IS NULL) AND object_id = '.$ilDB->quote($a_object_id, 'integer').
491 ' ORDER BY date_from');
492 $row = $ilDB->fetchAssoc($set);
493 return $row;
494 }
495
496 static function getObjectReservationForUser($a_object_id, $a_user_id, $a_multi = false)
497 {
498 global $ilDB;
499
500 $set = $ilDB->query('SELECT booking_reservation_id FROM booking_reservation'.
501 ' WHERE user_id = '.$ilDB->quote($a_user_id, 'integer').
502 ' AND object_id = '.$ilDB->quote($a_object_id, 'integer').
503 ' AND (status <> '.$ilDB->quote(self::STATUS_CANCELLED, 'integer').
504 ' OR STATUS IS NULL)');
505 if(!$a_multi)
506 {
507 $row = $ilDB->fetchAssoc($set);
508 return $row['booking_reservation_id'];
509 }
510 else
511 {
512 $res = array();
513 while($row = $ilDB->fetchAssoc($set))
514 {
515 $res[] = $row['booking_reservation_id'];
516 }
517 return $res;
518 }
519 }
520
529 static function getList($a_object_ids, $a_limit = 10, $a_offset = 0, array $filter)
530 {
531 global $ilDB;
532
533 $sql = 'SELECT r.*,o.title'.
534 ' FROM booking_reservation r'.
535 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
536
537 $count_sql = 'SELECT COUNT(*) AS counter'.
538 ' FROM booking_reservation r'.
539 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
540
541 $where = array($ilDB->in('r.object_id', $a_object_ids, '', 'integer'));
542 if($filter['status'])
543 {
544 if($filter['status'] > 0)
545 {
546 $where[] = 'status = '.$ilDB->quote($filter['status'], 'integer');
547 }
548 else
549 {
550 $where[] = '(status != '.$ilDB->quote(-$filter['status'], 'integer').
551 ' OR status IS NULL)';
552 }
553 }
554 if($filter['from'])
555 {
556 $where[] = 'date_from >= '.$ilDB->quote($filter['from'], 'integer');
557 }
558 if($filter['to'])
559 {
560 $where[] = 'date_to <= '.$ilDB->quote($filter['to'], 'integer');
561 }
562 if(sizeof($where))
563 {
564 $sql .= ' WHERE '.implode(' AND ', $where);
565 $count_sql .= ' WHERE '.implode(' AND ', $where);
566 }
567
568 $set = $ilDB->query($count_sql);
569 $row = $ilDB->fetchAssoc($set);
570 $counter = $row['counter'];
571
572 $sql .= ' ORDER BY date_from DESC, booking_reservation_id DESC';
573
574 $ilDB->setLimit($a_limit, $a_offset);
575 $set = $ilDB->query($sql);
576 $res = array();
577 while($row = $ilDB->fetchAssoc($set))
578 {
579 $res[] = $row;
580 }
581
582 return array('data'=>$res, 'counter'=>$counter);
583 }
584
596 static function getListByDate($a_has_schedule, array $a_object_ids, array $filter = null)
597 {
598 global $ilDB;
599
600 $res = array();
601
602 $sql = 'SELECT r.*, o.title'.
603 ' FROM booking_reservation r'.
604 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
605
606 $where = array($ilDB->in('object_id', $a_object_ids, '', 'integer'));
607 if($filter['status'])
608 {
609 if($filter['status'] > 0)
610 {
611 $where[] = 'status = '.$ilDB->quote($filter['status'], 'integer');
612 }
613 else
614 {
615 $where[] = '(status != '.$ilDB->quote(-$filter['status'], 'integer').
616 ' OR status IS NULL)';
617 }
618 }
619 if($filter['title'])
620 {
621 $where[] = '('.$ilDB->like('title', 'text', '%'.$filter['title'].'%').
622 ' OR '.$ilDB->like('description', 'text', '%'.$filter['title'].'%').')';
623 }
624 if($a_has_schedule)
625 {
626 if($filter['from'])
627 {
628 $where[] = 'date_from >= '.$ilDB->quote($filter['from'], 'integer');
629 }
630 if($filter['to'])
631 {
632 $where[] = 'date_to <= '.$ilDB->quote($filter['to'], 'integer');
633 }
634 }
635 if($filter['user_id']) // #16584
636 {
637 $where[] = 'user_id = '.$ilDB->quote($filter['user_id'], 'integer');
638 }
639 /*
640 if($a_group_id)
641 {
642 $where[] = 'group_id = '.$ilDB->quote(substr($a_group_id, 1), 'integer');
643 }
644 */
645 if(sizeof($where))
646 {
647 $sql .= ' WHERE '.implode(' AND ', $where);
648 }
649
650 if($a_has_schedule)
651 {
652 $sql .= ' ORDER BY date_from DESC';
653 }
654 else
655 {
656 // #16155 - could be cancelled and re-booked
657 $sql .= ' ORDER BY status';
658 }
659
660 $set = $ilDB->query($sql);
661 while($row = $ilDB->fetchAssoc($set))
662 {
663 $obj_id = $row["object_id"];
664 $user_id = $row["user_id"];
665
666 if($a_has_schedule)
667 {
668 $slot = $row["date_from"]."_".$row["date_to"];
669 $idx = $obj_id."_".$user_id."_".$slot;
670 }
671 else
672 {
673 $idx = $obj_id."_".$user_id;
674 }
675
676 if($a_has_schedule && $filter["slot"])
677 {
678 $slot_idx = date("w", $row["date_from"])."_".date("H:i", $row["date_from"]).
679 "-".date("H:i", $row["date_to"]+1);
680 if($filter["slot"] != $slot_idx)
681 {
682 continue;
683 }
684 }
685
686 if(!isset($res[$idx]))
687 {
689
690 $res[$idx] = array(
691 "object_id" => $obj_id
692 ,"title" => $row["title"]
693 ,"user_id" => $user_id
694 ,"counter" => 1
695 ,"user_name" => $uname["lastname"].", ".$uname["firstname"] // #17862
696 );
697
698 if($a_has_schedule)
699 {
700 $res[$idx]["booking_reservation_id"] = $idx;
701 $res[$idx]["date"] = date("Y-m-d", $row["date_from"]);
702 $res[$idx]["slot"] = date("H:i", $row["date_from"])." - ".
703 date("H:i", $row["date_to"]+1);
704 $res[$idx]["week"] = date("W", $row["date_from"]);
705 $res[$idx]["weekday"] = date("w", $row["date_from"]);
706 $res[$idx]["can_be_cancelled"] = ($row["status"] != self::STATUS_CANCELLED &&
707 $row["date_from"] > time());
708 $res[$idx]["_sortdate"] = $row["date_from"];
709 }
710 else
711 {
712 $res[$idx]["booking_reservation_id"] = $row["booking_reservation_id"];
713 $res[$idx]["status"] = $row["status"];
714 $res[$idx]["can_be_cancelled"] = ($row["status"] != self::STATUS_CANCELLED);
715 }
716 }
717 else
718 {
719 $res[$idx]["counter"]++;
720 }
721 }
722
723 return $res;
724 }
725
732 public static function getUserFilter(array $a_object_ids)
733 {
734 global $ilDB;
735
736 $res = array();
737
738 $sql = "SELECT ud.usr_id,ud.lastname,ud.firstname,ud.login".
739 " FROM usr_data ud ".
740 " LEFT JOIN booking_reservation r ON (r.user_id = ud.usr_id)".
741 " WHERE ud.usr_id <> ".$ilDB->quote(ANONYMOUS_USER_ID, "integer").
742 " AND ".$ilDB->in("r.object_id", $a_object_ids, "", "integer").
743 " ORDER BY ud.lastname,ud.firstname";
744 $set = $ilDB->query($sql);
745 while($row = $ilDB->fetchAssoc($set))
746 {
747 $res[$row["usr_id"]] = $row["lastname"].", ".$row["firstname"].
748 " (".$row["login"].")";
749 }
750
751 return $res;
752 }
753
763 /*
764 static function getGroupedList($a_object_ids, $a_limit = 10, $a_offset = 0, array $filter = null, $a_group_id = null)
765 {
766 global $ilDB;
767
768 // CURRENTLY UNUSED!!!
769 return;
770
771 // find matching groups / reservations
772
773 $sql = 'SELECT booking_reservation_id, group_id'.
774 ' FROM booking_reservation';
775
776 $where = array($ilDB->in('object_id', $a_object_ids, '', 'integer'));
777 if($filter['status'])
778 {
779 if($filter['status'] > 0)
780 {
781 $where[] = 'status = '.$ilDB->quote($filter['status'], 'integer');
782 }
783 else
784 {
785 $where[] = '(status != '.$ilDB->quote(-$filter['status'], 'integer').
786 ' OR status IS NULL)';
787 }
788 }
789 if($filter['from'])
790 {
791 $where[] = 'date_from >= '.$ilDB->quote($filter['from'], 'integer');
792 }
793 if($filter['to'])
794 {
795 $where[] = 'date_to <= '.$ilDB->quote($filter['to'], 'integer');
796 }
797 if($filter['user_id'])
798 {
799 $where[] = 'user_id = '.$ilDB->quote($filter['user_id'], 'integer');
800 }
801 if($a_group_id)
802 {
803 $where[] = 'group_id = '.$ilDB->quote(substr($a_group_id, 1), 'integer');
804 }
805 if(sizeof($where))
806 {
807 $sql .= ' WHERE '.implode(' AND ', $where);
808 }
809
810 $grp_ids = $rsv_ids = array();
811 $set = $ilDB->query($sql);
812 while($row = $ilDB->fetchAssoc($set))
813 {
814 if($row["group_id"])
815 {
816 $grp_ids[] = $row["group_id"];
817 }
818 else
819 {
820 $rsv_ids[] = $row["booking_reservation_id"];
821 }
822 }
823
824 $res = array();
825
826 // get complete groups (and/or reservations)
827
828 if($grp_ids || $rsv_ids)
829 {
830 $grp_ids = array_unique($grp_ids);
831
832 // if result is on last page, reduce limit to entries on last page
833 $max_page = sizeof($grp_ids)+sizeof($rsv_ids);
834 $max_page = min($a_limit, $max_page-$a_offset);
835
836 $sql = 'SELECT r.*,o.title'.
837 ' FROM booking_reservation r'.
838 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
839
840 $where = array();
841 if($grp_ids)
842 {
843 $where[] = $ilDB->in('group_id', $grp_ids, '', 'integer');
844 }
845 if($rsv_ids)
846 {
847 $where[] = $ilDB->in('booking_reservation_id', $rsv_ids, '', 'integer');
848 }
849
850 $sql .= ' WHERE ('.implode(' OR ', $where).')'.
851 ' ORDER BY date_from DESC, booking_reservation_id DESC';
852
853 $set = $ilDB->query($sql);
854 $grps = array();
855 $counter = 0;
856 while($row = $ilDB->fetchAssoc($set))
857 {
858 if($row["group_id"] && !$a_group_id)
859 {
860 if(!isset($grps[$row["group_id"]]))
861 {
862 $grps[$row["group_id"]] = 1;
863 $counter++;
864 }
865 else
866 {
867 $grps[$row["group_id"]]++;
868 }
869 }
870 else
871 {
872 $counter++;
873 }
874
875 if($a_group_id ||
876 ($counter > $a_offset &&
877 (sizeof($res) < $max_page ||
878 // if group is current page we have to get all group entries, regardless of booking period
879 ($row["group_id"] && isset($res["g".$row["group_id"]])))))
880 {
881 if($row["group_id"] && !$a_group_id)
882 {
883 $group_id = "g".$row["group_id"];
884 $res[$group_id]["group_id"] = $group_id;
885 $res[$group_id]["details"][] = $row;
886 }
887 else
888 {
889 unset($row["group_id"]);
890 $res[] = $row;
891 }
892 }
893 }
894 }
895
896 include_once('./Services/Calendar/classes/class.ilCalendarUtil.php');
897
898 foreach($res as $idx => $item)
899 {
900 if(isset($item["details"]))
901 {
902 $res[$idx]["date_from"] = null;
903 $res[$idx]["date_to"] = null;
904
905 $weekdays = $week_counter = array();
906 $recur = $last = 0;
907
908 foreach($item["details"] as $detail)
909 {
910 // same for each item
911 $res[$idx]["user_id"] = $detail["user_id"];
912 $res[$idx]["object_id"] = $detail["object_id"];
913 $res[$idx]["title"] = $detail["title"];
914 $res[$idx]["booking_reservation_id"] = $detail["booking_reservation_id"];
915
916 // recurrence/weekdays
917 $sortkey = date("wHi", $detail["date_from"])."_".date("wHi", $detail["date_to"]);
918 $weekdays[$sortkey] = ilCalendarUtil::_numericDayToString(date("w", $detail["date_from"]), false).
919 ", ".date("H:i", $detail["date_from"]).
920 " - ".date("H:i", $detail["date_to"]);
921
922 if($detail["status"] != self::STATUS_CANCELLED)
923 {
924 $week_counter[$sortkey][date("WHi", $detail["date_from"])."_".date("WHi", $detail["date_to"])]++;
925 }
926 else if(!isset($week_counter[$sortkey][date("WHi", $detail["date_from"])."_".date("WHi", $detail["date_to"])]))
927 {
928 $week_counter[$sortkey][date("WHi", $detail["date_from"])."_".date("WHi", $detail["date_to"])] = 0;
929 }
930
931 if($last && $last-$detail["date_to"] > $recur)
932 {
933 $recur = $last-$detail["date_to"];
934 }
935
936 // min/max period
937 if(!$res[$idx]["date_from"] || $detail["date_from"] < $res[$idx]["date_from"])
938 {
939 $res[$idx]["date_from"] = $detail["date_from"];
940 }
941 if(!$res[$idx]["date_to"] || $detail["date_to"] > $res[$idx]["date_to"])
942 {
943 $res[$idx]["date_to"] = $detail["date_to"];
944 }
945
946 $last = $detail["date_to"];
947 }
948
949 if(sizeof($item["details"]) > 1)
950 {
951 $weekdays = array_unique($weekdays);
952 ksort($weekdays);
953
954 foreach($weekdays as $week_id => $weekday)
955 {
956 $min = min($week_counter[$week_id]);
957 $max = max($week_counter[$week_id]);
958 if($min == $max)
959 {
960 $weekdays[$week_id] .= " (".$min.")";
961 }
962 else
963 {
964 $weekdays[$week_id] .= " (".$min."-".$max.")";
965 }
966 }
967
968
969 $res[$idx]["weekdays"] = array_values($weekdays);
970 if($recur)
971 {
972 if(date("YW", $res[$idx]["date_to"]) != date("YW", $res[$idx]["date_from"]))
973 {
974 $recur = ceil(($recur/(60*60*24))/7);
975 }
976 else
977 {
978 $recur = 0;
979 }
980 }
981 $res[$idx]["recurrence"] = (int)$recur;
982
983 $res[$idx]["booking_reservation_id"] = $idx;
984 $res[$idx]["title"] .= " (".sizeof($item["details"]).")";
985
986 }
987 else
988 {
989 // undo grouping
990 $res[$idx] = array_pop($item["details"]);
991 unset($res[$idx]["group_id"]);
992 }
993 }
994 }
995
996 $res = ilUtil::sortArray($res, "date_from", "desc", true);
997
998 return array('data'=>$res, 'counter'=>$counter);
999 }
1000 */
1001
1008 static function changeStatus(array $a_ids, $a_status)
1009 {
1010 global $ilDB;
1011
1012 if(self::isValidStatus($a_status))
1013 {
1014 return $ilDB->manipulate('UPDATE booking_reservation'.
1015 ' SET status = '.$ilDB->quote($a_status, 'integer').
1016 ' WHERE '.$ilDB->in('booking_reservation_id', $a_ids, '', 'integer'));
1017
1018 }
1019 }
1020
1022 {
1023 global $ilDB;
1024
1025 include_once 'Services/Calendar/classes/class.ilCalendarCategory.php';
1026
1027 $set = $ilDB->query("SELECT ce.cal_id FROM cal_entries ce".
1028 " JOIN cal_cat_assignments cca ON ce.cal_id = cca.cal_id".
1029 " JOIN cal_categories cc ON cca.cat_id = cc.cat_id".
1030 " JOIN booking_reservation br ON ce.context_id = br.booking_reservation_id".
1031 " WHERE cc.obj_id = ".$ilDB->quote($this->getUserId(),'integer').
1032 " AND br.user_id = ".$ilDB->quote($this->getUserId(),'integer').
1033 " AND cc.type = ".$ilDB->quote(ilCalendarCategory::TYPE_BOOK,'integer').
1034 " AND ce.context_id = ".$ilDB->quote($this->getId(), 'integer'));
1035 $row = $ilDB->fetchAssoc($set);
1036 return $row["cal_id"];
1037 }
1038
1048 public static function getCancelDetails($a_obj_id, $a_user_id, $a_from, $a_to)
1049 {
1050 global $ilDB;
1051
1052 $res = array();
1053
1054 $sql = "SELECT booking_reservation_id".
1055 " FROM booking_reservation".
1056 " WHERE object_id = ".$ilDB->quote($a_obj_id, "integer").
1057 " AND user_id = ".$ilDB->quote($a_user_id, "integer").
1058 " AND date_from = ".$ilDB->quote($a_from, "integer").
1059 " AND date_to = ".$ilDB->quote($a_to, "integer").
1060 " AND (status IS NULL".
1061 " OR status <> ".$ilDB->quote(self::STATUS_CANCELLED, "integer").")";
1062 $set = $ilDB->query($sql);
1063 while($row = $ilDB->fetchAssoc($set))
1064 {
1065 $res[] = $row["booking_reservation_id"];
1066 }
1067
1068 return $res;
1069 }
1070}
1071
1072?>
const IL_CAL_DATE
const IL_CAL_UNIX
a bookable ressource
static getNrOfItemsForObjects(array $a_obj_ids)
Get nr of available items
getFrom()
Get booking from date.
__construct($a_id=NULL)
Constructor.
static getObjectReservationForUser($a_object_id, $a_user_id, $a_multi=false)
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.
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 getListByDate($a_has_schedule, array $a_object_ids, array $filter=null)
List all reservations by date.
setStatus($a_status)
Set booking status.
static isObjectAvailableInPeriod($a_obj_id, ilBookingSchedule $a_schedule, $a_from, $a_to)
static isValidStatus($a_status)
Check if given status is valid.
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 isObjectAvailableNoSchedule($a_obj_id)
schedule for booking ressource
getAvailabilityTo()
Get availability end.
getAvailabilityFrom()
Get availability start.
getDefinition()
Get definition.
static _lookupName($a_user_id)
lookup user name
global $ilDB