3 * @file mod_roomReservationBookingsManager.inc
4 * Class to manage a set of bookings
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
8 * Copyright © 2007 Roland Hieber
10 * Permission is hereby granted, free of charge, to any person obtaining
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 require_once("sec/secure.inc");
30 require_once("functions.inc");
31 require_once("mod_room-reservation/mod_roomReservationConfig.inc");
32 require_once("mod_room-reservation/mod_roomReservationBooking.inc");
33 require_once("format.inc");
35 db_query("SET DATESTYLE = ISO;");
38 * Management of a set of bookings
39 * @todo finish, document
41 class mod_roomReservationBookingsManager {
43 /** (mod_roomReservationConfig) Reference to the configuration object */
46 /***************************************************************************/
51 * @param $oCfg (mod_roomReservationConfig) Reference to the configuration
52 * @return mod_roomReservationBookingsManager
54 function __construct(mod_roomReservationConfig &$oCfg) {
58 /***************************************************************************/
61 * @name Retrieving bookings
66 * Fetch a booking with the given unique ID from the SQL table
67 * @param $nUid (int) Unique ID of the booking
68 * @return mod_roomReservationBooking
70 public static function getBookingByUid($nUid) {
71 $h = db_query("SELECT * FROM mod_roomreservation_bookings WHERE rrb_uid = $1;", $nUid);
72 $a = pg_fetch_array($h);
73 $o = new mod_roomReservationBooking($a["rrb_room"], strtotime($a["rrb_date"]),
74 intval($a["rrb_tsfirst"]), intval($a["rrb_tslast"]), $a["rrb_act"],
75 $a["rrb_reason"], intval($a["rrb_interval"]));
76 $o->setUid(intval($a["rrb_uid"]));
81 * Test if there is a booking which takes place on the specified position at
83 * @param $strRoom (string) Name of the room
84 * @param $tsDate (timestamp) The date
85 * @param $nTimeslice (int) The number of the timeslice
86 * @return mod_roomReservationBooking The booking which takes place on the
87 * specified time or <tt>null</tt> if no booking could be found.
89 public static function getBookingByTimeslice($strRoom, $tsDate,
91 $a = mod_roomReservationBookingsManager::getOverlappingBookings(
92 new mod_roomReservationBooking($strRoom, $tsDate, $nTimeslice,
93 $nTimeslice, null, null));
94 return isset($a[0]) ? $a[0] : null;
98 * Get all bookings in database which overlap with the given booking.
99 * @param $ob (mod_roomReservationBooking) New booking that should be tested
101 * @return array with elements of type mod_roomReservationBooking
103 public static function getOverlappingBookings(
104 mod_roomReservationBooking $ob) {
105 // TODO: Test for bookings that only take place every n.th week (modulo n)
107 // Two bookings overlap, when they are on the same day and if
108 // old beginning < new ending AND old ending > new beginning
109 $hQuery = db_query("SELECT * FROM mod_roomreservation_bookings WHERE ".
110 "rrb_room = $1 AND ((rrb_interval > 0 AND EXTRACT(DOW FROM rrb_date) ".
111 "= $2) OR (rrb_interval = 0 AND rrb_date = $3)) AND rrb_tsfirst <= ".
112 "$4 AND rrb_tslast >= $5 ORDER BY rrb_tsfirst;", $ob->getRoom(),
113 date("w", $ob->getDate()), date("Y-m-d", $ob->getDate()),
114 intval($ob->getTsLast()), intval($ob->getTsFirst()));
116 while($aResult = pg_fetch_array($hQuery)) {
117 $aoReturn[] = mod_roomReservationBookingsManager::getBookingByUid(
118 $aResult["rrb_uid"]);
123 /***************************************************************************/
126 * @name Management of bookings
131 * Insert or update a booking in the database.
132 * The function throws an AccessException if the user was not allowed to
133 * write the booking, or an SQLException if there was an error while trying
134 * to insert or update the booking into the database.
135 * @param $ob (mod_roomReservationBooking) Booking to write to the database
136 * @return (int) The UID of the written booking
137 * @throws SQLException, AccessException
140 function write(mod_roomReservationBooking $ob) {
142 if(($ob->getUid() != null and !$this->oCfg->userIsAdmin() and
143 !$this->userIsOwner($ob->nUid)) or
144 ($ob->getUid() == null and !$this->oCfg->userCanBook())) {
145 throw new AccessException(MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED);
151 // check if everything is right and throw exceptions
152 if(trim($ob->getAct()) == "") {
153 $ob->setAct($SESSION["act"]);
154 } elseif(!isAct($ob->getAct())) {
155 throw new Exception(MOD_ROOM_RESERVATION_ERROR_NO_SUCH_ACCOUNT);
158 if($ob->getTsFirst() > $ob->getTsLast()) {
159 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_END_BEFORE_BEGIN);
162 if(trim($ob->getReason()) == "") {
163 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_NO_REASON);
167 // Test for overlapping bookings
168 if($this->getOverlappingBookings($ob) != array()) {
169 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_BOOKING_OVERLAPS);
173 // @todo Show real times in log, take it from config
176 if($ob->getUid() == null) {
177 // No UID yet, insert new booking
178 $strLog = sprintf("Raum „%s“ am %s von %s bis %s gebucht",
179 $ob->getRoom(), date("d\.m\.Y", $ob->getDate()),
180 intval($ob->getTsFirst()), intval($ob->getTsLast()));
182 // Update an existing booking
183 // @todo write old and new times into log
184 $strWhere = "rs_uid = ".qdb(intval($ob->getUid()));
185 $strLog = sprintf("Buchung im Raum „%s“ auf %s von %s bis %s ".
186 "geändert (Begründung: „%s“)", $ob->getRoom(), date("d\.m\.Y",
187 $ob->getDate()), intval($ob->getTsFirst()),
188 intval($ob->getTsLast()), $ob->getReason());
190 $aPut["rrb_room"] = $ob->getRoom();
191 $aPut["rrb_date"] = date("Y\-m\-d", $ob->getDate());
192 $aPut["rrb_tsfirst"] = intval($ob->getTsFirst());
193 $aPut["rrb_tslast"] = intval($ob->getTsLast());
194 $aPut["rrb_act"] = $ob->getAct();
195 $aPut["rrb_reason"] = $ob->getReason();
196 $aPut["rrb_interval"] = intval($ob->getInterval());
198 // @todo test if the foreign keys are being violated and throw an error
199 // message if neccessary
200 db_store("mod_roomreservation_bookings", $aPut, $strWhere);
202 $hQuery = db_query("SELECT currval('mod_roomreservation_bookings_rrb_uid_seq') ".
203 "FROM mod_roomreservation_bookings;");
204 $nNewUid = pg_fetch_result($hQuery, 0, "currval");
206 rrInsertLog($strLog);
213 * Delete a booking from the database
214 * @param $nUid (int) Unique ID of the booking
215 * @return (bool) <tt>true</tt> on success, otherwise <tt>false</tt>.
218 public function delete($nUid) {
219 // Only administrators and owners are allowed to delete bookings
220 if(!($this->oCfg->userIsAdmin() or $this->userIsOwner($nUid))) {
221 throw new AccessException(MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED);
225 // @todo: Show real times in log, take it from config
226 $ob = $this->getBookingByUid($nUid);
227 $strLog = sprintf("Buchung in Raum „%s“ am %s von %s bis %s ".
228 "gelöscht", $ob->getRoom(), date("d\.m\.Y", $ob->getDate()),
229 $ob->getTsFirst(), $ob->getTsLast());
230 // Delete it from the database
231 if(!db_query("DELETE FROM mod_roomreservation_bookings WHERE ".
232 "rrb_uid = $1;", $nUid)) {
233 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
236 rrInsertLog($strLog);
242 * Determine if the current user is the owner of a specified error report.
243 * If this function fails, call getLastError() to get more information.
244 * @param $nID (int) Unique ID of the error report
245 * @throws SQLException
248 public static function userIsOwner($nID) {
249 if(!isset($_SESSION["act"])) {
250 return false; // user is not logged in
252 $hQuery = db_query("SELECT rrb_act FROM mod_roomreservation_bookings WHERE ".
253 "rrb_uid = $1;", intval($nID));
254 if(!is_resource($hQuery)) {
255 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
258 $arResult = pg_fetch_array($hQuery);
259 return ($arResult["rrb_act"] == $_SESSION["act"]);