protected static function getWeekAvailability($room_id_list, $activityId, $person, $timestamp) { $now = time(); // Get the week start $weekStart = ReservationPeer::getWeekStart($timestamp); // We get all the reservations for the current room list in the week containing the given timestamp. $c = ReservationPeer::getWeekCriteria($timestamp); $c->addJoin(ReservationPeer::ROOMPROFILE_ID, RoomprofilePeer::ID); $c->addAnd(RoomprofilePeer::ROOM_ID, $room_id_list, Criteria::IN); $c->addAscendingOrderByColumn(ReservationPeer::DATE); $reservations = ReservationPeer::doSelect($c); // We get all the day periods for the current room list. $c = new Criteria(); $c->addAnd(DayperiodPeer::ROOM_ID, $room_id_list, Criteria::IN); $c->addAscendingOrderByColumn(DayperiodPeer::DAY_OF_WEEK); $c->addAscendingOrderByColumn(DayperiodPeer::START); $dayPeriods = DayperiodPeer::doSelect($c); // We get all the close periods for the current room list. $c = CloseperiodPeer::getWeekCriteria($timestamp); $c->addAnd(CloseperiodPeer::ROOM_ID, $room_id_list, Criteria::IN); $c->addAscendingOrderByColumn(CloseperiodPeer::START); $c->addAscendingOrderByColumn(CloseperiodPeer::STOP); $closePeriods = CloseperiodPeer::doSelect($c); // We build the availability array $startIndex = 48; $stopIndex = 0; $result = array(); for ($i = 0; $i < 7; ++$i) { $result[$i] = array(); for ($j = 0; $j < 48; ++$j) { $tst = strtotime(date('Y-m-d H:i:s', $weekStart) . ' + ' . $i . ' day + ' . $j * 30 . ' minute'); $value = RoomPeer::COMPLETE; $roomsId = array(); $cnt = 0; foreach ($dayPeriods as $dayPeriod) { if ($dayPeriod->matchTimestamp($tst)) { if ($j < $startIndex) { $startIndex = $j; } if ($j >= $stopIndex) { $stopIndex = $j + 1; } ++$cnt; $roomsId[] = $dayPeriod->getRoomId(); } elseif ($dayPeriod->getDayOfWeek() > $i) { break; } } if ($cnt == count($room_id_list)) { $value = RoomPeer::FREE; } elseif ($cnt > 0) { $value = RoomPeer::OCCUPIED; } $cnt = 0; foreach ($closePeriods as $closePeriod) { if ($closePeriod->matchTimestamp($tst)) { ++$cnt; unset($roomsId[array_search($closePeriod->getRoomId(), $roomsId)]); } if (strtotime($closePeriod->getStart()) > $tst) { break; } } if ($cnt == count($room_id_list)) { $value = RoomPeer::COMPLETE; } else { if ($cnt > 0) { $value = RoomPeer::OCCUPIED; } } if ($value != RoomPeer::COMPLETE) { if ($tst <= $now) { $value = RoomPeer::PAST; } else { $cnt = 0; foreach ($room_id_list as $roomId) { $maximumTimestamp = $person->getMaximumDate($activityId, $roomId); if ($maximumTimestamp <= $tst || !$person->hasSubscription($activityId, $roomId, $tst)) { ++$cnt; unset($roomsId[array_search($roomId, $roomsId)]); } } if ($cnt == count($room_id_list)) { $value = RoomPeer::TOOFAR; } else { if ($cnt > 0) { $value = RoomPeer::OCCUPIED; } $cnt = 0; foreach ($reservations as $reservation) { if ($reservation->matchTimestamp($tst)) { ++$cnt; unset($roomsId[array_search($reservation->getRoomprofile()->getRoomId(), $roomsId)]); } elseif (strtotime($reservation->getDate()) > $tst) { break; } } if (count($roomsId) == 0) { $value = RoomPeer::COMPLETE; } else { if ($cnt > 0) { $value = RoomPeer::OCCUPIED; } } } } } $result[$i][$j]['value'] = $value; $result[$i][$j]['rooms'] = $roomsId; $result[$i][$j]['timestamp'] = $tst; } } $result['startIndex'] = $startIndex; $result['stopIndex'] = $stopIndex; return $result; }
public function countMinutesPerWeek($activityId, $roomId, $tst, $reservation_id = null) { $c = ReservationPeer::getWeekCriteria($tst); $c->addAnd(ReservationPeer::CARD_ID, $this->getId(), Criteria::EQUAL); $c->addAnd(ReservationPeer::ACTIVITY_ID, $activityId, Criteria::EQUAL); $zones = $this->getActiveSubscriptionsZones($activityId, $roomId); $rooms = ZonePeer::getRooms($zones); $rooms_ids = array(); foreach ($rooms as $room) { $rooms_ids[] = $room->getId(); } $c->addJoin(ReservationPeer::ROOMPROFILE_ID, RoomprofilePeer::ID); $c->addAnd(RoomprofilePeer::ROOM_ID, $rooms_ids, Criteria::IN); if (!is_null($reservation_id)) { $c->addand(ReservationPeer::ID, $reservation_id, Criteria::NOT_EQUAL); } $c->clearSelectColumns(); $c->addSelectColumn('SUM(' . ReservationPeer::DURATION . ')'); $c->setLimit(1); $stmt = ReservationPeer::doSelectStmt($c); if ($row = $stmt->fetch(PDO::FETCH_NUM)) { return $row[0]; } return 0; }