52 $this->db = $DIC->database();
53 $this->
id = (int) $a_id;
56 $this->repo =
$f->getRepo();
76 $this->object_id = $a_object_id;
94 $this->user_id = (int) $a_user_id;
112 $this->assigner_id = (int) $a_assigner_id;
130 $this->from = (int) $a_from;
148 $this->to = (int) $a_to;
166 if ($a_status === null) {
167 $this->status = null;
170 $this->status = (int) $a_status;
190 if (in_array($a_status, array(self::STATUS_IN_USE, self::STATUS_CANCELLED))) {
202 $this->group_id = $a_group_id;
221 $this->context_obj_id = $a_val;
241 $row = $this->repo->getForId($this->
id);
245 $this->
setFrom($row[
'date_from']);
246 $this->
setTo($row[
'date_to']);
263 $this->
id = $this->repo->create(
273 return ($this->
id > 0);
302 public function delete()
304 $this->repo->delete($this->
id);
316 public static function getAvailableObject(array $a_ids, $a_from, $a_to, $a_return_single =
true, $a_return_counter =
false)
323 $blocked = $counter = array();
324 foreach (
$repo->getNumberOfReservations($a_ids, $a_from, $a_to) as $row) {
325 if ($row[
'cnt'] >= $nr_map[$row[
'object_id']]) {
326 $blocked[] = $row[
'object_id'];
327 } elseif ($a_return_counter) {
328 $counter[$row[
'object_id']] = (int) $nr_map[$row[
'object_id']] - (
int) $row[
'cnt'];
333 foreach ($a_ids as $obj_id) {
335 if ($bobj->getScheduleId()) {
338 $av_from = ($schedule->getAvailabilityFrom() && !$schedule->getAvailabilityFrom()->isNull())
341 $av_to = ($schedule->getAvailabilityTo() && !$schedule->getAvailabilityTo()->isNull())
342 ? strtotime($schedule->getAvailabilityTo()->get(
IL_CAL_DATE) .
" 23:59:59")
345 if (($av_from && $a_from < $av_from) ||
346 ($av_to && $a_to > $av_to)) {
347 $blocked[] = $obj_id;
348 unset($counter[$obj_id]);
353 $available = array_diff($a_ids, $blocked);
354 if (
sizeof($available)) {
355 if ($a_return_counter) {
356 foreach ($a_ids as
$id) {
357 if (!isset($counter[$id])) {
358 $counter[
$id] = (int) $nr_map[$id];
362 } elseif ($a_return_single) {
363 return array_shift($available);
374 $ilDB = $DIC->database();
380 $a_to = strtotime(
"+1year", $a_from);
383 if ($a_from > $a_to) {
390 $res =
$repo->getNumberOfReservations([$a_obj_id], $a_from, $a_to,
true);
391 $booked_in_period = (int)
$res[$a_obj_id][
"cnt"];
394 $per_slot = $per_slot[$a_obj_id];
397 $schedule_slots = array();
399 $map = array_flip(array(
"su",
"mo",
"tu",
"we",
"th",
"fr",
"sa"));
400 foreach ($definition as $day => $day_slots) {
401 $schedule_slots[$map[$day]] = $day_slots;
412 $available_in_period = 0;
414 while ($a_from < $a_to &&
417 $day_slots = $schedule_slots[date(
"w", $a_from)];
419 foreach ($day_slots as $slot) {
421 $slot = explode(
"-", $slot);
422 $slot_from = strtotime(date(
"Y-m-d", $a_from) .
" " . $slot[0]);
423 $slot_to = strtotime(date(
"Y-m-d", $a_from) .
" " . $slot[1]);
425 if ($slot_to > time() &&
426 $slot_from >= $av_from &&
427 ($slot_to <= $av_to || is_null($av_to))) {
428 $available_in_period += $per_slot;
433 $a_from += (60 * 60 * 24);
435 if ($available_in_period - $booked_in_period > 0) {
446 $ilDB = $DIC->database();
450 $query =
"SELECT count(user_id) total" .
451 " FROM booking_reservation" .
452 " WHERE " .
$ilDB->in(
'object_id', $booking_pool_objects,
false,
'integer') .
453 " AND user_id = " . $a_user_id .
458 return (
int) $row[
'total'];
464 $ilDB = $DIC->database();
469 $query =
'SELECT DISTINCT bm.user_id user_id' .
470 ' FROM booking_member bm' .
471 ' WHERE bm.booking_pool_id = ' .
$ilDB->quote($pool_id,
'integer') .
472 ' AND bm.user_id NOT IN (' .
474 ' FROM booking_reservation' .
475 ' WHERE object_id = ' .
$ilDB->quote($a_object_id,
'integer') .
480 while ($row =
$ilDB->fetchAssoc($set)) {
481 $res[] = $row[
'user_id'];
489 $available = self::getNumAvailablesNoSchedule($a_obj_id);
490 return (
bool) $available;
494 $available = self::getNumAvailablesNoSchedule($a_obj_id);
495 return (
int) $available;
502 $ilDB = $DIC->database();
505 $all = (int) $all[$a_obj_id];
507 $set =
$ilDB->query(
'SELECT COUNT(*) cnt' .
508 ' FROM booking_reservation r' .
509 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)' .
510 ' WHERE (status IS NULL OR status <> ' .
$ilDB->quote(self::STATUS_CANCELLED,
'integer') .
')' .
511 ' AND r.object_id = ' .
$ilDB->quote($a_obj_id,
'integer'));
512 $cnt =
$ilDB->fetchAssoc($set);
513 $cnt = (int) $cnt[
'cnt'];
515 return (
int) $all - $cnt;
527 $ilDB = $DIC->database();
529 $now =
$ilDB->quote(time(),
'integer');
532 $set =
$ilDB->query(
'SELECT user_id, status, date_from, date_to' .
533 ' FROM booking_reservation' .
534 ' WHERE ((date_from <= ' . $now .
' AND date_to >= ' . $now .
')' .
535 ' OR date_from > ' . $now .
')' .
536 ' AND (status <> ' .
$ilDB->quote(self::STATUS_CANCELLED,
'integer') .
537 ' OR STATUS IS NULL) AND object_id = ' .
$ilDB->quote($a_object_id,
'integer') .
538 ' ORDER BY date_from');
539 $row =
$ilDB->fetchAssoc($set);
547 $ilDB = $DIC->database();
549 $set =
$ilDB->query(
'SELECT booking_reservation_id FROM booking_reservation' .
550 ' WHERE user_id = ' .
$ilDB->quote($a_user_id,
'integer') .
551 ' AND object_id = ' .
$ilDB->quote($a_object_id,
'integer') .
552 ' AND (status <> ' .
$ilDB->quote(self::STATUS_CANCELLED,
'integer') .
553 ' OR STATUS IS NULL)');
555 $row =
$ilDB->fetchAssoc($set);
556 return $row[
'booking_reservation_id'];
559 while ($row =
$ilDB->fetchAssoc($set)) {
560 $res[] = $row[
'booking_reservation_id'];
574 public static function getList($a_object_ids, $a_limit = 10, $a_offset = 0, array $filter = [])
578 $ilDB = $DIC->database();
580 $sql =
'SELECT r.*,o.title' .
581 ' FROM booking_reservation r' .
582 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
584 $count_sql =
'SELECT COUNT(*) AS counter' .
585 ' FROM booking_reservation r' .
586 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
588 $where = array(
$ilDB->in(
'r.object_id', $a_object_ids,
'',
'integer'));
589 if ($filter[
'status']) {
590 if ($filter[
'status'] > 0) {
591 $where[] =
'status = ' .
$ilDB->quote($filter[
'status'],
'integer');
593 $where[] =
'(status != ' .
$ilDB->quote(-$filter[
'status'],
'integer') .
594 ' OR status IS NULL)';
597 if ($filter[
'from']) {
598 $where[] =
'date_from >= ' .
$ilDB->quote($filter[
'from'],
'integer');
601 $where[] =
'date_to <= ' .
$ilDB->quote($filter[
'to'],
'integer');
603 if (
sizeof($where)) {
604 $sql .=
' WHERE ' . implode(
' AND ', $where);
605 $count_sql .=
' WHERE ' . implode(
' AND ', $where);
608 $set =
$ilDB->query($count_sql);
609 $row =
$ilDB->fetchAssoc($set);
610 $counter = $row[
'counter'];
612 $sql .=
' ORDER BY date_from DESC, booking_reservation_id DESC';
614 $ilDB->setLimit($a_limit, $a_offset);
615 $set =
$ilDB->query($sql);
617 while ($row =
$ilDB->fetchAssoc($set)) {
621 return array(
'data' =>
$res,
'counter' => $counter);
635 $ilDB = $DIC->database();
639 $sql =
"SELECT ud.usr_id,ud.lastname,ud.firstname,ud.login" .
640 " FROM usr_data ud " .
641 " LEFT JOIN booking_reservation r ON (r.user_id = ud.usr_id)" .
642 " WHERE ud.usr_id <> " .
$ilDB->quote(ANONYMOUS_USER_ID,
"integer") .
643 " AND " .
$ilDB->in(
"r.object_id", $a_object_ids,
"",
"integer") .
644 " ORDER BY ud.lastname,ud.firstname";
645 $set =
$ilDB->query($sql);
646 while ($row =
$ilDB->fetchAssoc($set)) {
647 $res[$row[
"usr_id"]] = $row[
"lastname"] .
", " . $row[
"firstname"] .
648 " (" . $row[
"login"] .
")";
665 $ilDB = $DIC->database();
667 if (self::isValidStatus($a_status)) {
668 return $ilDB->manipulate(
'UPDATE booking_reservation' .
669 ' SET status = ' .
$ilDB->quote($a_status,
'integer') .
670 ' WHERE ' .
$ilDB->in(
'booking_reservation_id', $a_ids,
'',
'integer'));
679 $set =
$ilDB->query(
"SELECT ce.cal_id FROM cal_entries ce" .
680 " JOIN cal_cat_assignments cca ON ce.cal_id = cca.cal_id" .
681 " JOIN cal_categories cc ON cca.cat_id = cc.cat_id" .
682 " JOIN booking_reservation br ON ce.context_id = br.booking_reservation_id" .
683 " WHERE cc.obj_id = " .
$ilDB->quote($this->getUserId(),
'integer') .
684 " AND br.user_id = " .
$ilDB->quote($this->getUserId(),
'integer') .
686 " AND ce.context_id = " .
$ilDB->quote($this->
getId(),
'integer'));
687 $row =
$ilDB->fetchAssoc($set);
688 return $row[
"cal_id"];
704 $ilDB = $DIC->database();
708 $sql =
"SELECT booking_reservation_id" .
709 " FROM booking_reservation" .
710 " WHERE object_id = " .
$ilDB->quote($a_obj_id,
"integer") .
711 " AND user_id = " .
$ilDB->quote($a_user_id,
"integer") .
712 " AND date_from = " .
$ilDB->quote($a_from,
"integer") .
713 " AND date_to = " .
$ilDB->quote($a_to,
"integer") .
714 " AND (status IS NULL" .
715 " OR status <> " .
$ilDB->quote(self::STATUS_CANCELLED,
"integer") .
")";
716 $set =
$ilDB->query($sql);
717 while ($row =
$ilDB->fetchAssoc($set)) {
718 $res[] = $row[
"booking_reservation_id"];
getAssignerId()
Get assigner user id.
setStatus($a_status)
Set booking status.
getGroupId()
Get group id.
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)
getAvailabilityFrom()
Get availability start.
setFrom($a_from)
Set booking from date.
schedule for booking ressource
getAvailabilityTo()
Get availability end.
getStatus()
Get booking status.
setAssignerId($a_assigner_id)
Set assigner user id.
static getObjectsForPool(int $a_pool_id)
Get all booking pool object ids from an specific booking pool.
getFrom()
Get booking from date.
getContextObjId()
Get context object id.
static getNrOfItemsForObjects(array $a_obj_ids)
Get nr of available items.
static isValidStatus($a_status)
Check if given status is valid.
setContextObjId($a_val)
Set context object id.
static getObjectReservationForUser($a_object_id, $a_user_id, $a_multi=false)
foreach($_POST as $key=> $value) $res
static getMembersWithoutReservation(int $a_object_id)
update()
Update entry in db.
getDefinition()
Get definition.
static getList($a_object_ids, $a_limit=10, $a_offset=0, array $filter=[])
List all reservations.
setObjectId($a_object_id)
Set object id.
static getCurrentOrUpcomingReservation($a_object_id)
Get details about object reservation.
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 lookupPoolId($object_id)
Lookup pool id.
static isBookingPoolLimitReachedByUser(int $a_user_id, int $a_pool_id)
__construct($a_id=null)
Constructor.
read()
Get dataset from db.
Factory for reservation repo.
static isObjectAvailableInPeriod($a_obj_id, ilBookingSchedule $a_schedule, $a_from, $a_to)
setUserId($a_user_id)
Set booking user id.
static numAvailableFromObjectNoSchedule($a_obj_id)
getObjectId()
Get object 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.
static changeStatus(array $a_ids, $a_status)
Batch update reservation status.
getUserId()
Get booking user id.
getTo()
Get booking to date.
setTo($a_to)
Set booking to date.
static getNumAvailablesNoSchedule($a_obj_id)