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