3 * @file mod_roomReservationConfig.inc
4 * Handling of the configuration file
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("mod_room-reservation/functions.inc");
31 require_once("mod_room-reservation/mod_roomReservationTimeslice.inc");
34 * Determines if a privilege has been assigned
35 * @param $sPriv (string) Privilege to test
38 function rrPrivilegeAssigned($sPriv) {
39 $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1;",
41 return pg_num_rows($h) > 0;
45 * Retrieve all groups that have a privilege assigned
46 * @param $strPriv (string) Privilege to test
49 function rrPrivilegedGroups($strPriv) {
51 $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1 ".
52 "ORDER BY act;", $strPriv);
53 if(pg_num_rows($h) > 0) {
54 while($a = pg_fetch_array($h)) {
55 $aReturn[] = $a["act"];
62 * User-defined compare function to compare timeslices
63 * @param $oTs1 (mod_roomReservationTimeslice)
64 * @param $oTs2 (mod_roomReservationTimeslice)
65 * @return (int) <tt>-1</tt> if $oTs1 begins before $oTs2,
66 * <tt>0</tt> if the $oTs1 and $oTs2 have the same beginning,
67 * <tt>1</tt> if $oTs1 begins after $oTs2.
69 function rrConfigSortTimeslices(mod_roomReservationTimeslice $oTs1,
70 mod_roomReservationTimeslice $oTs2) {
71 if($oTs1->getBegin() == $oTs2->getBegin()) {
74 return ($oTs1->getBegin() > $oTs2->getBegin()) ? 1 : -1;
78 define("MOD_ROOM_RESERVATION_CONFIGFILE_HEADER", "<?php
80 * configuration file for package iserv-room-reservation
81 * This file is written by the configuration script. Do not change it manually.
84 /*****************************************************************************/
86 * Handling of the configuration file
89 class mod_roomReservationConfig {
91 /** (array of rmTimeslice's) Timeslices */
92 protected $aoTimeslices;
93 /** (bool) Determine if the weekend is shown */
94 protected $bShowWeekend;
95 /** (bool) Determine if the strings "1. lesson", "2. lesson" etc are shown */
96 protected $bShowLessons;
97 /** (array of strings) error messages */
98 protected $asMessages;
100 /***************************************************************************/
105 * @return mod_roomReservationConfig
107 public function __construct() {
108 $this->flushTimeslices();
109 $this->setShowWeekend(false);
110 $this->setShowLessons(true);
111 $this->asMessages = array();
117 * **************************************************************************
119 * @name Access to attributes
124 * Add a timeslice. A check is done that the timeslices do not overlap, and
125 * in this case, an Exception is thrown.
126 * @param $oTs (mod_roomReservationTimeslice)
130 public function addTimeslice(mod_roomReservationTimeslice $oTs) {
131 // Check for overlapping timeslices
132 foreach($this->aoTimeslices as $oOldTs) {
133 if(($oOldTs->getBegin() < $oTs->getEnd() and
134 $oOldTs->getEnd() > $oTs->getBegin())) {
136 MOD_ROOM_RESERVATION_ERROR_CONFIG_OVERLAPPING_TIMESLICE);
139 $this->aoTimeslices[] = $oTs;
140 usort($this->aoTimeslices, "rrConfigSortTimeslices");
146 * @param $oTs (mod_roomReservationTimeslice) the timeslice to delete. If
147 * the timeslice is not found, an Exception is thrown.
151 public function deleteTimeslice(mod_roomReservationTimeslice $oTs) {
152 for($i = 0; $i < count($this->aoTimeslices); $i++) {
153 if($this->aoTimeslices[$i]->getBegin() == $oTs->getBegin() and
154 $this->aoTimeslices[$i]->getEnd() == $oTs->getEnd()) {
155 // use array_splice because it renumbers the keys
156 array_splice($this->aoTimeslices, $i, 1);
160 throw new Exception(MOD_ROOM_RESERVATION_ERROR_CONFIG_NO_SUCH_TIMESLICE);
164 * Delete all timeslices.
167 public function flushTimeslices() { $this->aoTimeslices = array(); }
170 * Show or hide the weekend
173 public function setShowWeekend($b) { $this->bShowWeekend = ($b == true); }
176 * Show or hide the lesson strings in the booking table
179 public function setShowLessons($b) { $this->bShowLessons = ($b == true); }
182 * Add a message to the internal array of (error) messages
183 * @param $sMessage (string)
185 public function addMessage($sMessage) {
186 array_merge($this->asMessages, array($sMessage));
190 * Get all timeslices in chronological order
191 * @return array of rmTimeslice
193 public function getTimeslices() { return $this->aoTimeslices; }
196 * Return the starting times of every timeslice
197 * @param $bFormat (bool) <tt>true</tt>: Format the times according to the
199 * <tt>false</tt>: return just the timestamps
202 public function getTimesliceBeginnings($bFormat = false) {
203 $aot = $this->getTimeslices();
205 foreach($aot as $ao) {
206 $aRet[] = $bFormat ? _date("%#I:%M %p", $ao->getBegin()) :
213 * Return the ending times of every timeslice
214 * @param $bFormat (bool) <tt>true</tt>: Format the times according to the
216 * <tt>false</tt>: return just the timestamps
219 public function getTimesliceEndings($bFormat = false) {
220 $aot = $this->getTimeslices();
222 foreach($aot as $ao) {
223 $aRet[] = $bFormat ? _date("%#I:%M %p", $ao->getEnd()) : $ao->getEnd();
230 * @param $n (int) index of the timeslice in the array
231 * @return rmTimeslice
233 public function getTimeslice($n) { return $this->aoTimeslices[$n]; }
236 * Determine if the weekend is shown
239 public function isShowWeekend() { return ($this->bShowWeekend == true); }
242 * Determine if the lesson strings in the booking table are shown
245 public function isShowLessons() { return ($this->bShowLessons == true); }
248 * Determine if the current user has admin rights. This function tests
249 * if the user is in a group which has the privilege of admin rights.
253 public function userIsAdmin() {
254 return secure_privilege("mod_roomreservation_admin");
258 * Determine if the current user can book rooms. This function tests
259 * if the user is in a group which has the privilege to book rooms.
260 * If no group has this privilege, all users can book.
264 public function userCanBook() {
265 if(!rrPrivilegeAssigned("mod_roomreservation_book")) {
266 // If the privilege is not assigned to any group, all users can book
269 // If user is admin, it makes sense that he can book rooms ;-)
270 return secure_privilege("mod_roomreservation_book") ||
271 secure_privilege("mod_roomreservation_admin");
276 * Determine if the current user can view bookings. This function tests
277 * if the user is in a group which has been configured as a group who
278 * can view bookings. If no groups are configured, any user can view the
283 public function userCanView() {
284 if(!rrPrivilegeAssigned("mod_roomreservation_view")) {
285 // If the privilege is not assigned to any group, all users can view
288 // If user is admin or can book, it makes sense that he can view bookings
289 return secure_privilege("mod_roomreservation_admin") ||
290 secure_privilege("mod_roomreservation_book") ||
291 secure_privilege("mod_roomreservation_view");
296 * Get the messages that have been produced
299 public function getMessages() {
300 return join("\n", $this->asMessages);
303 /***************************************************************************/
311 * Write the current state of this instance to the config file.
312 * @throws IOException
315 public function writeConfig() {
317 $hFile = fopen("mod_room-reservation/config.inc", "w", true);
318 if(!is_resource($hFile)) {
319 throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE);
322 // Try to lock file repeatedly
323 for($n = 0; !flock($hFile, LOCK_EX); $n++) {
325 throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE);
326 return false; // Give up
328 sleep(0.2); // Retry after 100 ms
332 // Create text for config file
333 $strFile = MOD_ROOM_RESERVATION_CONFIGFILE_HEADER;
336 $strFile .= "\$this->flushTimeslices();\n";
337 foreach($this->getTimeslices() as $oTs) {
338 $strFile .= sprintf("\$this->addTimeslice(new ".
339 "mod_roomReservationTimeslice(%d, %d));\n", $oTs->getBegin(),
344 $strFile .= sprintf("\$this->setShowWeekend(%s);\n",
345 $this->isShowWeekend() ? "true" : "false");
348 $strFile .= sprintf("\$this->setShowLessons(%s);\n",
349 $this->isShowLessons() ? "true" : "false");
353 // Write to config file and unlock it
354 if(fwrite($hFile, $strFile) == false) {
355 throw new IOException(MOD_ROOM_RESERVATION_ERROR_WRITE_FILE);
358 if(!flock($hFile, LOCK_UN)) {
359 throw new IOException(MOD_ROOM_RESERVATION_ERROR_UNLOCK_FILE);
363 rrInsertLog("Konfiguration verändert");
368 * Read configuration from file. Returns <tt>false</tt> if an error occured,
369 * in this case getMessages() contains error messages.
372 public function readConfig() {
375 require("mod_room-reservation/config.inc");
376 } catch(Exception $e) {
377 $this->addMessage($e->getMessage());