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 // Show real times in log, but don't use the user's locale!
174 $oTsB = $this->oCfg->getTimesliceBeginnings(false);
175 $oTsE = $this->oCfg->getTimesliceEndings(false);
178 if($ob->getUid() == null) {
179 // No UID yet, insert new booking
180 $strLog = sprintf("Raum „%s“ am %s von %s bis %s gebucht ".
181 "(Begründung: %s)", $ob->getRoom(), date("d\.m\.Y", $ob->getDate()),
182 gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i",
183 $oTsE[$ob->getTsLast()]), $ob->getReason());
185 // Update an existing booking
186 // @todo write old and new times into log
187 $strWhere = "rs_uid = ".qdb(intval($ob->getUid()));
188 $strLog = sprintf("Buchung im Raum „%s“ auf %s von %s bis %s ".
189 "geändert (Begründung: „%s“)", $ob->getRoom(), date("d\.m\.Y",
190 $ob->getDate()), gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i",
191 $oTsE[$ob->getTsLast()]), $ob->getReason());
193 $aPut["rrb_room"] = $ob->getRoom();
194 $aPut["rrb_date"] = date("Y\-m\-d", $ob->getDate());
195 $aPut["rrb_tsfirst"] = intval($ob->getTsFirst());
196 $aPut["rrb_tslast"] = intval($ob->getTsLast());
197 $aPut["rrb_act"] = $ob->getAct();
198 $aPut["rrb_reason"] = $ob->getReason();
199 $aPut["rrb_interval"] = intval($ob->getInterval());
201 // @todo test if the foreign keys are being violated and throw an error
202 // message if neccessary
203 db_store("mod_roomreservation_bookings", $aPut, $strWhere);
205 $hQuery = db_query("SELECT currval('mod_roomreservation_bookings_rrb_uid_seq');");
206 $nNewUid = pg_fetch_result($hQuery, 0, "currval");
209 echo "inserted new UID: $nNewUid<br />";
212 rrInsertLog($strLog);
219 * Delete a booking from the database
220 * @param $nUid (int) Unique ID of the booking
221 * @return (bool) <tt>true</tt> on success, otherwise <tt>false</tt>.
224 public function delete($nUid) {
225 // Only administrators and owners are allowed to delete bookings
226 if(!($this->oCfg->userIsAdmin() or $this->userIsOwner($nUid))) {
227 throw new AccessException(MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED);
231 // Don't use the user's locale!
232 $oTsB = $this->oCfg->getTimesliceBeginnings(false);
233 $oTsE = $this->oCfg->getTimesliceEndings(false);
234 $ob = $this->getBookingByUid($nUid);
235 $strLog = sprintf("Buchung in Raum „%s“ am %s von %s bis %s ".
236 "gelöscht (Begründung war: %s)", $ob->getRoom(), date("d\.m\.Y",
237 $ob->getDate()), gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i",
238 $oTsE[$ob->getTsLast()]), $ob->getReason());
239 // Delete it from the database
240 if(!db_query("DELETE FROM mod_roomreservation_bookings WHERE ".
241 "rrb_uid = $1;", $nUid)) {
242 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
245 rrInsertLog($strLog);
251 * Determine if the current user is the owner of a specified error report.
252 * If this function fails, call getLastError() to get more information.
253 * @param $nID (int) Unique ID of the error report
254 * @throws SQLException
257 public static function userIsOwner($nID) {
258 if(!isset($_SESSION["act"])) {
259 return false; // user is not logged in
261 $hQuery = db_query("SELECT rrb_act FROM mod_roomreservation_bookings WHERE ".
262 "rrb_uid = $1;", intval($nID));
263 if(!is_resource($hQuery)) {
264 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
267 $arResult = pg_fetch_array($hQuery);
268 return ($arResult["rrb_act"] == $_SESSION["act"]);