removed nasty comments from Doxyfile
[iserv-mod-room-reservation.git] / inc / mod_roomReservationConfig.inc
1 <?php
2 /**
3 * @file mod_roomReservationConfig.inc
4 * Handling of the configuration file
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
6 * @date 10.01.2008
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("db.inc");
31 require_once("mod_room-reservation/functions.inc");
32 require_once("mod_room-reservation/mod_roomReservationTimeslice.inc");
33 require_once("mod_room-reservation/mod_roomReservationRoomsManager.inc");
34
35 /**
36 * Determines if a privilege has been assigned
37 * @param $sPriv (string) Privilege to test
38 * @return bool
39 */
40 function rrPrivilegeAssigned($sPriv) {
41 $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1;",
42 $sPriv);
43 return pg_num_rows($h) > 0;
44 }
45
46 /**
47 * Retrieve all groups that have a privilege assigned
48 * @param $strPriv (string) Privilege to test
49 * @return array
50 */
51 function rrPrivilegedGroups($strPriv) {
52 $aReturn = array();
53 $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1 ".
54 "ORDER BY act;", $strPriv);
55 if(pg_num_rows($h) > 0) {
56 while($a = pg_fetch_array($h)) {
57 $aReturn[] = $a["act"];
58 }
59 }
60 return $aReturn;
61 }
62
63 /**
64 * User-defined compare function to compare timeslices
65 * @param $oTs1 (mod_roomReservationTimeslice)
66 * @param $oTs2 (mod_roomReservationTimeslice)
67 * @return (int) <tt>-1</tt> if $oTs1 begins before $oTs2,
68 * <tt>0</tt> if the $oTs1 and $oTs2 have the same beginning,
69 * <tt>1</tt> if $oTs1 begins after $oTs2.
70 */
71 function rrConfigSortTimeslices(mod_roomReservationTimeslice $oTs1,
72 mod_roomReservationTimeslice $oTs2) {
73 if($oTs1->getBegin() == $oTs2->getBegin()) {
74 return 0;
75 } else {
76 return ($oTs1->getBegin() > $oTs2->getBegin()) ? 1 : -1;
77 }
78 }
79
80 define("MOD_ROOM_RESERVATION_CONFIGFILE_HEADER", "<?php
81 /**
82 * configuration file for package iserv-mod-room-reservation
83 * This file is written by the configuration script. Do not change it manually.
84 */\n");
85
86 /*****************************************************************************/
87 /**
88 * Handling of the configuration file
89 * @todo document
90 */
91 class mod_roomReservationConfig {
92
93 /** (array of rmTimeslice's) Timeslices */
94 protected $aoTimeslices;
95 /** (bool) Determine if the weekend is shown */
96 protected $bShowWeekend;
97 /** (bool) Determine if the strings "1. lesson", "2. lesson" etc are shown */
98 protected $bShowLessons;
99 /** (array of strings) error messages */
100 protected $asMessages;
101
102 /***************************************************************************/
103 /**
104 * @name Constructor
105 * @{
106 * Constructor.
107 * @return mod_roomReservationConfig
108 */
109 public function __construct() {
110 $this->flushTimeslices();
111 $this->setShowWeekend(false);
112 $this->setShowLessons(true);
113 $this->asMessages = array();
114
115 $this->readConfig();
116 }
117
118 /**
119 * **************************************************************************
120 * @}
121 * @name Access to attributes
122 * @{
123 */
124
125 /**
126 * Add a timeslice. A check is done that the timeslices do not overlap, and
127 * in this case, an Exception is thrown.
128 * @param $oTs (mod_roomReservationTimeslice)
129 * @throws Exception
130 * @return void
131 */
132 public function addTimeslice(mod_roomReservationTimeslice $oTs) {
133 // Check for overlapping timeslices
134 foreach($this->aoTimeslices as $oOldTs) {
135 if(($oOldTs->getBegin() < $oTs->getEnd() and
136 $oOldTs->getEnd() > $oTs->getBegin())) {
137 throw new Exception(
138 MOD_ROOM_RESERVATION_ERROR_CONFIG_OVERLAPPING_TIMESLICE);
139 }
140 }
141 $this->aoTimeslices[] = $oTs;
142 usort($this->aoTimeslices, "rrConfigSortTimeslices");
143 return;
144 }
145
146 /**
147 * Delete a timeslice
148 * @param $oTs (mod_roomReservationTimeslice) the timeslice to delete. If
149 * the timeslice is not found, an Exception is thrown.
150 * @throws Exception
151 * @return void
152 */
153 public function deleteTimeslice(mod_roomReservationTimeslice $oTs) {
154 for($i = 0; $i < count($this->aoTimeslices); $i++) {
155 if($this->aoTimeslices[$i]->getBegin() == $oTs->getBegin() and
156 $this->aoTimeslices[$i]->getEnd() == $oTs->getEnd()) {
157 // use array_splice because it renumbers the keys
158 array_splice($this->aoTimeslices, $i, 1);
159 return;
160 }
161 }
162 throw new Exception(MOD_ROOM_RESERVATION_ERROR_CONFIG_NO_SUCH_TIMESLICE);
163 }
164
165 /**
166 * Delete all timeslices.
167 * @return void
168 */
169 public function flushTimeslices() { $this->aoTimeslices = array(); }
170
171 /**
172 * Add a room to the list of rooms who can be booked. Throws an SQLException
173 * in case of an error.
174 * @param $sRoom (string) The name of the room
175 * @throws SQLException, Exception
176 * @return void
177 */
178 public function whitelistRoom($sRoom) {
179 if(!$this->isRoomWhitelisted($sRoom)) {
180 $r = db_store("mod_roomreservation_roomswhitelist",
181 array("rrr_name" => $sRoom));
182 if(!$r) {
183 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
184 } else {
185 log_insert(sprintf("Raum „%s“ für Buchungen zur Verfügung gestellt",
186 $sRoom));
187 }
188 }
189 }
190
191 /**
192 * Forbid bookings for a room. Throws an SQLException in case of an error.
193 * @param $sRoom The name of the room
194 * @throws SQLException
195 */
196 public function unWhitelistRoom($sRoom) {
197 $h = db_query("DELETE FROM mod_roomreservation_roomswhitelist WHERE ".
198 "rrr_name = $1;", $sRoom);
199 if(!$h) {
200 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
201 } else {
202 log_insert(sprintf("Raum „%s“ für Buchungen gesperrt", $sRoom));
203 }
204 }
205
206 /**
207 * Determine if a room is allowed for booking. Throws an SQLException
208 * in case of an error.
209 * @param $sRoom (string) The name of the room
210 * @return bool
211 * @throws SQLException
212 */
213 public function isRoomWhitelisted($sRoom) {
214 $h = db_query("SELECT * FROM mod_roomreservation_roomswhitelist WHERE ".
215 "rrr_name=$1;", $sRoom);
216 if(!$h) {
217 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
218 }
219 return (pg_num_rows($h) > 0);
220 }
221
222 /**
223 * Get all rooms that are allowed for booking. Throws an SQLException
224 * in case of an error.
225 * @throws SQLException
226 * @return array of mod_roomReservationRoomsManager objects
227 */
228 public function getWhitelistedRooms() {
229 $aor = mod_roomReservationRoomsManager::getRooms();
230 $ar = array();
231 foreach($aor as $key => $or) {
232 if($this->isRoomWhitelisted($or->getName())) {
233 $ar[] = $or;
234 }
235 }
236 return $ar;
237 }
238
239 /**
240 * Show or hide the weekend
241 * @param $b (bool)
242 */
243 public function setShowWeekend($b) { $this->bShowWeekend = ($b == true); }
244
245 /**
246 * Show or hide the lesson strings in the booking table
247 * @param $b (bool)
248 */
249 public function setShowLessons($b) { $this->bShowLessons = ($b == true); }
250
251 /**
252 * Add a message to the internal array of (error) messages
253 * @param $sMessage (string)
254 */
255 public function addMessage($sMessage) {
256 array_merge($this->asMessages, array($sMessage));
257 }
258
259 /**
260 * Get all timeslices in chronological order
261 * @return array of rmTimeslice
262 */
263 public function getTimeslices() { return $this->aoTimeslices; }
264
265 /**
266 * Return the starting times of every timeslice
267 * @param $bFormat (bool) <tt>true</tt>: Format the times according to the
268 * current locale
269 * <tt>false</tt>: return just the timestamps
270 * @return array
271 */
272 public function getTimesliceBeginnings($bFormat = false) {
273 $aot = $this->getTimeslices();
274 $aRet = array();
275 foreach($aot as $ao) {
276 $aRet[] = $bFormat ? gmstrftime(_("%#I:%M %p"), $ao->getBegin()) :
277 $ao->getBegin();
278 }
279 return $aRet;
280 }
281
282 /**
283 * Return the ending times of every timeslice
284 * @param $bFormat (bool) <tt>true</tt>: Format the times according to the
285 * current locale
286 * <tt>false</tt>: return just the timestamps
287 * @return array
288 */
289 public function getTimesliceEndings($bFormat = false) {
290 $aot = $this->getTimeslices();
291 $aRet = array();
292 foreach($aot as $ao) {
293 $aRet[] = $bFormat ? gmstrftime(_("%#I:%M %p"), $ao->getEnd()) :
294 $ao->getEnd();
295 }
296 return $aRet;
297 }
298
299 /**
300 * Get a timeslice
301 * @param $n (int) index of the timeslice in the array
302 * @return rmTimeslice
303 */
304 public function getTimeslice($n) { return $this->aoTimeslices[$n]; }
305
306 /**
307 * Determine if the weekend is shown
308 * @return bool
309 */
310 public function isShowWeekend() { return ($this->bShowWeekend == true); }
311
312 /**
313 * Determine if the lesson strings in the booking table are shown
314 * @return bool
315 */
316 public function isShowLessons() { return ($this->bShowLessons == true); }
317
318 /**
319 * Determine if the current user has admin rights. This function tests
320 * if the user is in a group which has the privilege of admin rights.
321 * @todo test
322 * @return bool
323 */
324 public function userIsAdmin() {
325 return secure_privilege("mod_roomreservation_admin");
326 }
327
328 /**
329 * Determine if the current user can book rooms. This function tests
330 * if the user is in a group which has the privilege to book rooms.
331 * If no group has this privilege, all users can book.
332 * @todo test
333 * @return bool
334 */
335 public function userCanBook() {
336 if(!rrPrivilegeAssigned("mod_roomreservation_book")) {
337 // If the privilege is not assigned to any group, all users can book
338 return true;
339 } else {
340 // If user is admin, it makes sense that he can book rooms ;-)
341 return secure_privilege("mod_roomreservation_book") ||
342 secure_privilege("mod_roomreservation_admin");
343 }
344 }
345
346 /**
347 * Determine if the current user can view bookings. This function tests
348 * if the user is in a group which has been configured as a group who
349 * can view bookings. If no groups are configured, any user can view the
350 * bookings table.
351 * @todo test
352 * @return bool
353 */
354 public function userCanView() {
355 if(!rrPrivilegeAssigned("mod_roomreservation_view")) {
356 // If the privilege is not assigned to any group, all users can view
357 return true;
358 } else {
359 // If user is admin or can book, it makes sense that he can view bookings
360 return secure_privilege("mod_roomreservation_admin") ||
361 secure_privilege("mod_roomreservation_book") ||
362 secure_privilege("mod_roomreservation_view");
363 }
364 }
365
366 /**
367 * Get the messages that have been produced
368 * @return string
369 */
370 public function getMessages() {
371 return join("\n", $this->asMessages);
372 }
373
374 /***************************************************************************/
375 /**
376 * @}
377 * @name Operations
378 * @{
379 */
380
381 /**
382 * Write the current state of this instance to the config file.
383 * @throws IOException
384 * @return bool
385 */
386 public function writeConfig() {
387 // Open config file
388 $hFile = fopen("mod_room-reservation/config.inc", "w", true);
389 if(!is_resource($hFile)) {
390 throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE);
391 return false;
392 }
393 // Try to lock file repeatedly
394 for($n = 0; !flock($hFile, LOCK_EX); $n++) {
395 if($n > 10) {
396 throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE);
397 return false; // Give up
398 } else {
399 sleep(0.2); // Retry after 100 ms
400 }
401 }
402
403 // Create text for config file
404 $strFile = MOD_ROOM_RESERVATION_CONFIGFILE_HEADER;
405
406 // Timeslices
407 $strFile .= "\$this->flushTimeslices();\n";
408 foreach($this->getTimeslices() as $oTs) {
409 $strFile .= sprintf("\$this->addTimeslice(new ".
410 "mod_roomReservationTimeslice(%d, %d));\n", $oTs->getBegin(),
411 $oTs->getEnd());
412 }
413
414 // Show weekend
415 $strFile .= sprintf("\$this->setShowWeekend(%s);\n",
416 $this->isShowWeekend() ? "true" : "false");
417
418 // Show lessons
419 $strFile .= sprintf("\$this->setShowLessons(%s);\n",
420 $this->isShowLessons() ? "true" : "false");
421
422 $strFile .= "?>";
423
424 // Write to config file and unlock it
425 if(fwrite($hFile, $strFile) == false) {
426 throw new IOException(MOD_ROOM_RESERVATION_ERROR_WRITE_FILE);
427 return false;
428 }
429 if(!flock($hFile, LOCK_UN)) {
430 throw new IOException(MOD_ROOM_RESERVATION_ERROR_UNLOCK_FILE);
431 return false;
432 }
433
434 rrInsertLog("Konfiguration verändert");
435 return true;
436 }
437
438 /**
439 * Read configuration from file. Returns <tt>false</tt> if an error occured,
440 * in this case getMessages() contains error messages.
441 * @return bool
442 */
443 public function readConfig() {
444 global $g_rmCfg;
445 try {
446 require("mod_room-reservation/config.inc");
447 } catch(Exception $e) {
448 $this->addMessage($e->getMessage());
449 }
450 }
451
452 /** @} */
453 }
454 ?>
This page took 0.064331 seconds and 5 git commands to generate.