34 $this->
id = (int)$a_id;
53 $this->object_id = $a_object_id;
71 $this->user_id = (int)$a_user_id;
89 $this->from = (int)$a_from;
107 $this->to = (int)$a_to;
125 if($a_status === NULL)
127 $this->status = NULL;
131 $this->status = (int)$a_status;
151 if(in_array($a_status, array(self::STATUS_IN_USE, self::STATUS_CANCELLED)))
164 $this->group_id = $a_group_id;
185 $set = $ilDB->query(
'SELECT *'.
186 ' FROM booking_reservation'.
187 ' WHERE booking_reservation_id = '.$ilDB->quote($this->id,
'integer'));
188 $row = $ilDB->fetchAssoc($set);
211 $this->
id = $ilDB->nextId(
'booking_reservation');
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').
218 ','.$ilDB->quote($this->
getFrom(),
'integer').
219 ','.$ilDB->quote($this->
getTo(),
'integer').
220 ','.$ilDB->quote($this->
getStatus(),
'integer').
221 ','.$ilDB->quote($this->
getGroupId(),
'integer').
')');
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'));
267 return $ilDB->manipulate(
'DELETE FROM booking_reservation'.
268 ' WHERE booking_reservation_id = '.$ilDB->quote($this->id,
'integer'));
280 return $ilDB->nextId(
'booking_reservation_group');
291 static function getAvailableObject(array $a_ids, $a_from, $a_to, $a_return_single =
true, $a_return_counter =
false)
297 $from = $ilDB->quote($a_from,
'integer');
298 $to = $ilDB->quote($a_to,
'integer');
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))
311 if(
$row[
'cnt'] >= $nr_map[
$row[
'object_id']])
313 $blocked[] = $row[
'object_id'];
315 else if($a_return_counter)
317 $counter[$row[
'object_id']] = (int)$nr_map[$row[
'object_id']]-(
int)$row[
'cnt'];
320 $available = array_diff($a_ids, $blocked);
321 if(
sizeof($available))
323 if($a_return_counter)
325 foreach($a_ids as
$id)
327 if(!isset($counter[$id]))
329 $counter[
$id] = (int)$nr_map[$id];
334 else if($a_return_single)
336 return array_shift($available);
350 $all = (int)$all[$a_obj_id];
352 $set = $ilDB->query(
'SELECT COUNT(*) cnt'.
353 ' FROM booking_reservation r'.
354 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)'.
355 ' WHERE (status IS NULL OR status <> '.$ilDB->quote(self::STATUS_CANCELLED,
'integer').
')'.
356 ' AND r.object_id = '.$ilDB->quote($a_obj_id,
'integer'));
357 $cnt = $ilDB->fetchAssoc($set);
358 $cnt = (int)$cnt[
'cnt'];
360 return (
bool)($all-$cnt);
372 $now = $ilDB->quote(time(),
'integer');
375 $set = $ilDB->query(
'SELECT user_id, status, date_from, date_to'.
376 ' FROM booking_reservation'.
377 ' WHERE ((date_from <= '.$now.
' AND date_to >= '.$now.
')'.
378 ' OR date_from > '.$now.
')'.
379 ' AND (status <> '.$ilDB->quote(self::STATUS_CANCELLED,
'integer').
380 ' OR STATUS IS NULL) AND object_id = '.$ilDB->quote($a_object_id,
'integer').
381 ' ORDER BY date_from');
382 $row = $ilDB->fetchAssoc($set);
390 $set = $ilDB->query(
'SELECT booking_reservation_id FROM booking_reservation'.
391 ' WHERE user_id = '.$ilDB->quote($a_user_id,
'integer').
392 ' AND object_id = '.$ilDB->quote($a_object_id,
'integer').
393 ' AND (status <> '.$ilDB->quote(self::STATUS_CANCELLED,
'integer').
394 ' OR STATUS IS NULL)');
395 $row = $ilDB->fetchAssoc($set);
396 return $row[
'booking_reservation_id'];
407 static function getList($a_object_ids, $a_limit = 10, $a_offset = 0, array $filter)
411 $sql =
'SELECT r.*,o.title'.
412 ' FROM booking_reservation r'.
413 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
415 $count_sql =
'SELECT COUNT(*) AS counter'.
416 ' FROM booking_reservation r'.
417 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
419 $where = array($ilDB->in(
'r.object_id', $a_object_ids,
'',
'integer'));
420 if($filter[
'status'])
422 if($filter[
'status'] > 0)
424 $where[] =
'status = '.$ilDB->quote($filter[
'status'],
'integer');
428 $where[] =
'(status != '.$ilDB->quote(-$filter[
'status'],
'integer').
429 ' OR status IS NULL)';
434 $where[] =
'date_from >= '.$ilDB->quote($filter[
'from'],
'integer');
438 $where[] =
'date_to <= '.$ilDB->quote($filter[
'to'],
'integer');
442 $sql .=
' WHERE '.implode(
' AND ', $where);
443 $count_sql .=
' WHERE '.implode(
' AND ', $where);
446 $set = $ilDB->query($count_sql);
447 $row = $ilDB->fetchAssoc($set);
448 $counter =
$row[
'counter'];
450 $sql .=
' ORDER BY date_from DESC, booking_reservation_id DESC';
452 $ilDB->setLimit($a_limit, $a_offset);
453 $set = $ilDB->query($sql);
455 while(
$row = $ilDB->fetchAssoc($set))
460 return array(
'data'=>
$res,
'counter'=>$counter);
472 static function getGroupedList($a_object_ids, $a_limit = 10, $a_offset = 0, array $filter = null, $a_group_id = null)
476 $sql =
'SELECT r.*,o.title'.
477 ' FROM booking_reservation r'.
478 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
480 $where = array($ilDB->in(
'r.object_id', $a_object_ids,
'',
'integer'));
481 if($filter[
'status'])
483 if($filter[
'status'] > 0)
485 $where[] =
'status = '.$ilDB->quote($filter[
'status'],
'integer');
489 $where[] =
'(status != '.$ilDB->quote(-$filter[
'status'],
'integer').
490 ' OR status IS NULL)';
495 $where[] =
'date_from >= '.$ilDB->quote($filter[
'from'],
'integer');
499 $where[] =
'date_to <= '.$ilDB->quote($filter[
'to'],
'integer');
503 $where[] =
'group_id = '.$ilDB->quote(substr($a_group_id, 1),
'integer');
507 $sql .=
' WHERE '.implode(
' AND ', $where);
510 $sql .=
' ORDER BY date_from DESC, booking_reservation_id DESC';
512 $set = $ilDB->query($sql);
513 $res = $grps = array();
515 while(
$row = $ilDB->fetchAssoc($set))
517 if(
$row[
"group_id"] && !$a_group_id)
519 if(!isset($grps[
$row[
"group_id"]]))
521 $grps[$row[
"group_id"]] = 1;
526 $grps[$row[
"group_id"]]++;
534 if($a_group_id || ($counter > $a_offset &&
sizeof(
$res) < $a_limit))
536 if(
$row[
"group_id"] && !$a_group_id)
544 unset(
$row[
"group_id"]);
550 foreach(
$res as $idx => $item)
552 if(isset($item[
"details"]))
554 $res[$idx][
"date_from"] = null;
555 $res[$idx][
"date_to"] = null;
557 foreach($item[
"details"] as $detail)
560 $res[$idx][
"user_id"] = $detail[
"user_id"];
561 $res[$idx][
"object_id"] = $detail[
"object_id"];
562 $res[$idx][
"title"] = $detail[
"title"];
563 $res[$idx][
"booking_reservation_id"] = $detail[
"booking_reservation_id"];
566 if(!
$res[$idx][
"date_from"] || $detail[
"date_from"] <
$res[$idx][
"date_from"])
568 $res[$idx][
"date_from"] = $detail[
"date_from"];
570 if(!
$res[$idx][
"date_to"] || $detail[
"date_to"] >
$res[$idx][
"date_to"])
572 $res[$idx][
"date_to"] = $detail[
"date_to"];
576 if(
sizeof($item[
"details"]) > 1)
578 $res[$idx][
"booking_reservation_id"] = $idx;
579 $res[$idx][
"title"] .=
" (".sizeof($item[
"details"]).
")";
583 $res[$idx][
"status"] = $detail[
"status"];
584 unset(
$res[$idx][
"group_id"]);
589 return array(
'data'=>
$res,
'counter'=>$counter);
602 if(self::isValidStatus($a_status))
604 return $ilDB->manipulate(
'UPDATE booking_reservation'.
605 ' SET status = '.$ilDB->quote($a_status,
'integer').
606 ' WHERE '.$ilDB->in(
'booking_reservation_id', $a_ids,
'',
'integer'));
615 include_once
'Services/Calendar/classes/class.ilCalendarCategory.php';
617 $set = $ilDB->query(
"SELECT ce.cal_id FROM cal_entries ce".
618 " JOIN cal_cat_assignments cca ON ce.cal_id = cca.cal_id".
619 " JOIN cal_categories cc ON cca.cat_id = cc.cat_id".
620 " JOIN booking_reservation br ON ce.context_id = br.booking_reservation_id".
621 " WHERE cc.obj_id = ".$ilDB->quote($this->getUserId(),
'integer').
622 " AND br.user_id = ".$ilDB->quote($this->getUserId(),
'integer').
624 " AND ce.context_id = ".$ilDB->quote($this->
getId(),
'integer'));
625 $row = $ilDB->fetchAssoc($set);
626 return $row[
"cal_id"];