124068de3c4554e8751e74c550ba5a9d8b4da500
[iserv-mod-room-reservation.git] / inc / mod_roomReservationBookingsManager.inc
1 <?php
2 /**
3 * @file mod_roomReservationBookingsManager.inc
4 * Class to manage a set of bookings
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
6 * @date 23.11.2007
7 *
8 * Copyright © 2007 Roland Hieber
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * THE SOFTWARE.
27 */
28
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");
34
35 db_query("SET DATESTYLE = ISO;");
36
37 /**
38 * Management of a set of bookings
39 * @todo finish, document
40 */
41 class mod_roomReservationBookingsManager {
42
43 /** (mod_roomReservationConfig) Reference to the configuration object */
44 protected $oCfg;
45
46 /***************************************************************************/
47 /**
48 * @name Constructor
49 * @{
50 * Constructor.
51 * @param $oCfg (mod_roomReservationConfig) Reference to the configuration
52 * @return mod_roomReservationBookingsManager
53 */
54 function __construct(mod_roomReservationConfig &$oCfg) {
55 $this->oCfg = $oCfg;
56 }
57
58 /***************************************************************************/
59 /**
60 * @}
61 * @name Retrieving bookings
62 * @{
63 */
64
65 /**
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
69 */
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"]));
77 return $o;
78 }
79
80 /**
81 * Test if there is a booking which takes place on the specified position at
82 * the specified date.
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.
88 */
89 public static function getBookingByTimeslice($strRoom, $tsDate,
90 $nTimeslice) {
91 $a = mod_roomReservationBookingsManager::getOverlappingBookings(
92 new mod_roomReservationBooking($strRoom, $tsDate, $nTimeslice,
93 $nTimeslice, null, null));
94 return isset($a[0]) ? $a[0] : null;
95 }
96
97 /**
98 * Get all bookings in database which overlap with the given booking.
99 * @param $ob (mod_roomReservationBooking) New booking that should be tested
100 * if it overlaps
101 * @return array with elements of type mod_roomReservationBooking
102 */
103 public static function getOverlappingBookings(
104 mod_roomReservationBooking $ob) {
105 // TODO: Test for bookings that only take place every n.th week (modulo n)
106
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()));
115 $aoReturn = array();
116 while($aResult = pg_fetch_array($hQuery)) {
117 $aoReturn[] = mod_roomReservationBookingsManager::getBookingByUid(
118 $aResult["rrb_uid"]);
119 }
120 return $aoReturn;
121 }
122
123 /***************************************************************************/
124 /**
125 * @}
126 * @name Management of bookings
127 * @{
128 */
129
130 /**
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
138 * @todo document
139 */
140 function write(mod_roomReservationBooking $ob) {
141 // protect access
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);
146 }
147
148 // test if room is whitelisted
149 if(!$this->oCfg->isRoomWhitelisted($ob->getRoom())) {
150 throw new Exception(MOD_ROOM_RESERVATION_ERROR_ROOM_NOT_WHITELISTED);
151 }
152
153 $strWhere = null;
154 $strLog = "";
155
156 // check if everything is right and throw exceptions
157 if(trim($ob->getAct()) == "") {
158 $ob->setAct($SESSION["act"]);
159 } elseif(!isAct($ob->getAct())) {
160 throw new Exception(MOD_ROOM_RESERVATION_ERROR_NO_SUCH_ACCOUNT);
161 return false;
162 }
163 if($ob->getTsFirst() > $ob->getTsLast()) {
164 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_END_BEFORE_BEGIN);
165 return false;
166 }
167 if(trim($ob->getReason()) == "") {
168 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_NO_REASON);
169 return false;
170 }
171
172 // Test for overlapping bookings
173 if($this->getOverlappingBookings($ob) != array()) {
174 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_BOOKING_OVERLAPS);
175 return false;
176 }
177
178 // Show real times in log, but don't use the user's locale!
179 $oTsB = $this->oCfg->getTimesliceBeginnings(false);
180 $oTsE = $this->oCfg->getTimesliceEndings(false);
181
182 // Update or insert?
183 if($ob->getUid() == null) {
184 // No UID yet, insert new booking
185 // @todo write interval and user if interval > 0
186 $strLog = sprintf("Raum „%s“ am %s von %s bis %s gebucht ".
187 "(Begründung: %s)", $ob->getRoom(), date("d\.m\.Y", $ob->getDate()),
188 gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i",
189 $oTsE[$ob->getTsLast()]), $ob->getReason());
190 } else {
191 // Update an existing booking
192 // @todo write old and new times into log
193 $strWhere = "rs_uid = ".qdb(intval($ob->getUid()));
194 $strLog = sprintf("Buchung im Raum „%s“ auf %s von %s bis %s ".
195 "geändert (Begründung: „%s“)", $ob->getRoom(), date("d\.m\.Y",
196 $ob->getDate()), gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i",
197 $oTsE[$ob->getTsLast()]), $ob->getReason());
198 }
199 $aPut["rrb_room"] = $ob->getRoom();
200 $aPut["rrb_date"] = date("Y\-m\-d", $ob->getDate());
201 $aPut["rrb_tsfirst"] = intval($ob->getTsFirst());
202 $aPut["rrb_tslast"] = intval($ob->getTsLast());
203 $aPut["rrb_act"] = $ob->getAct();
204 $aPut["rrb_reason"] = $ob->getReason();
205 $aPut["rrb_interval"] = intval($ob->getInterval());
206
207 // @todo test if the foreign keys are being violated and throw an error
208 // message if neccessary
209 db_store("mod_roomreservation_bookings", $aPut, $strWhere);
210
211 $hQuery = db_query("SELECT currval('mod_roomreservation_bookings_rrb_uid_seq');");
212 $nNewUid = pg_fetch_result($hQuery, 0, "currval");
213
214 rrInsertLog($strLog);
215
216 // Return new UID
217 return $nNewUid;
218 }
219
220 /**
221 * Delete a booking from the database
222 * @param $nUid (int) Unique ID of the booking
223 * @return (bool) <tt>true</tt> on success, otherwise <tt>false</tt>.
224 * @todo test
225 */
226 public function delete($nUid) {
227 // Only administrators and owners are allowed to delete bookings
228 if(!($this->oCfg->userIsAdmin() or $this->userIsOwner($nUid))) {
229 throw new AccessException(MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED);
230 return false;
231 }
232
233 // Don't use the user's locale!
234 $oTsB = $this->oCfg->getTimesliceBeginnings(false);
235 $oTsE = $this->oCfg->getTimesliceEndings(false);
236 $ob = $this->getBookingByUid($nUid);
237 $strLog = sprintf("Buchung in Raum „%s“ am %s von %s bis %s ".
238 "gelöscht (Begründung war: %s)", $ob->getRoom(), date("d\.m\.Y",
239 $ob->getDate()), gmdate("G:i", $oTsB[$ob->getTsFirst()]), gmdate("G:i",
240 $oTsE[$ob->getTsLast()]), $ob->getReason());
241 // Delete it from the database
242 if(!db_query("DELETE FROM mod_roomreservation_bookings WHERE ".
243 "rrb_uid = $1;", $nUid)) {
244 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
245 return false;
246 } else {
247 rrInsertLog($strLog);
248 return true;
249 }
250 }
251
252 /**
253 * Determine if the current user is the owner of a specified error report.
254 * If this function fails, call getLastError() to get more information.
255 * @param $nID (int) Unique ID of the error report
256 * @throws SQLException
257 * @return bool
258 */
259 public static function userIsOwner($nID) {
260 if(!isset($_SESSION["act"])) {
261 return false; // user is not logged in
262 } else {
263 $hQuery = db_query("SELECT rrb_act FROM mod_roomreservation_bookings WHERE ".
264 "rrb_uid = $1;", intval($nID));
265 if(!is_resource($hQuery)) {
266 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
267 return false;
268 }
269 $arResult = pg_fetch_array($hQuery);
270 return ($arResult["rrb_act"] == $_SESSION["act"]);
271 }
272 }
273 }
274 ?>
This page took 0.057138 seconds and 3 git commands to generate.