imported sources
[iserv-mod-room-reservation.git] / includes / 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("mod_room-reservation/functions.inc");
31 require_once("mod_room-reservation/mod_roomReservationTimeslice.inc");
32
33 /**
34 * Determines if a privilege has been assigned
35 * @param $sPriv (string) Privilege to test
36 * @return bool
37 */
38 function rrPrivilegeAssigned($sPriv) {
39 $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1;",
40 $sPriv);
41 return pg_num_rows($h) > 0;
42 }
43
44 /**
45 * Retrieve all groups that have a privilege assigned
46 * @param $strPriv (string) Privilege to test
47 * @return array
48 */
49 function rrPrivilegedGroups($strPriv) {
50 $aReturn = array();
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"];
56 }
57 }
58 return $aReturn;
59 }
60
61 /**
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.
68 */
69 function rrConfigSortTimeslices(mod_roomReservationTimeslice $oTs1,
70 mod_roomReservationTimeslice $oTs2) {
71 if($oTs1->getBegin() == $oTs2->getBegin()) {
72 return 0;
73 } else {
74 return ($oTs1->getBegin() > $oTs2->getBegin()) ? 1 : -1;
75 }
76 }
77
78 define("MOD_ROOM_RESERVATION_CONFIGFILE_HEADER", "<?php
79 /**
80 * configuration file for package iserv-room-reservation
81 * This file is written by the configuration script. Do not change it manually.
82 */\n");
83
84 /*****************************************************************************/
85 /**
86 * Handling of the configuration file
87 * @todo document
88 */
89 class mod_roomReservationConfig {
90
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 // FIXME needed?
97 protected $bShowLessons;
98 /** (array of strings) error messages */
99 protected $asMessages;
100
101 /***************************************************************************/
102 /**
103 * @name Constructor
104 * @{
105 * Constructor.
106 * @return mod_roomReservationConfig
107 */
108 public function __construct() {
109 $this->flushTimeslices();
110 $this->setShowWeekend(false);
111 $this->setShowLessons(true);
112 $this->asMessages = array();
113
114 $this->readConfig();
115 }
116
117 /**
118 * **************************************************************************
119 * @}
120 * @name Access to attributes
121 * @{
122 */
123
124 /**
125 * Add a timeslice. A check is done that the timeslices do not overlap, and
126 * in this case, an Exception is thrown.
127 * @param $oTs (mod_roomReservationTimeslice)
128 * @throws Exception
129 * @return void
130 */
131 public function addTimeslice(mod_roomReservationTimeslice $oTs) {
132 // Check for overlapping timeslices
133 foreach($this->aoTimeslices as $oOldTs) {
134 if(($oOldTs->getBegin() < $oTs->getEnd() and
135 $oOldTs->getEnd() > $oTs->getBegin())) {
136 throw new Exception(
137 MOD_ROOM_RESERVATION_ERROR_CONFIG_OVERLAPPING_TIMESLICE);
138 }
139 }
140 $this->aoTimeslices[] = $oTs;
141 usort($this->aoTimeslices, "rrConfigSortTimeslices");
142 return;
143 }
144
145 /**
146 * Delete a timeslice
147 * @param $oTs (mod_roomReservationTimeslice) the timeslice to delete. If
148 * the timeslice is not found, an Exception is thrown.
149 * @throws Exception
150 * @return void
151 */
152 public function deleteTimeslice(mod_roomReservationTimeslice $oTs) {
153 for($i = 0; $i < count($this->aoTimeslices); $i++) {
154 if($this->aoTimeslices[$i]->getBegin() == $oTs->getBegin() and
155 $this->aoTimeslices[$i]->getEnd() == $oTs->getEnd()) {
156 // use array_splice because it renumbers the keys
157 array_splice($this->aoTimeslices, $i, 1);
158 return;
159 }
160 }
161 throw new Exception(MOD_ROOM_RESERVATION_ERROR_CONFIG_NO_SUCH_TIMESLICE);
162 }
163
164 /**
165 * Delete all timeslices.
166 * @return void
167 */
168 public function flushTimeslices() { $this->aoTimeslices = array(); }
169
170 /**
171 * Show or hide the weekend
172 * @param $b (bool)
173 */
174 public function setShowWeekend($b) { $this->bShowWeekend = ($b == true); }
175
176 /**
177 * Show or hide the lesson strings in the booking table
178 * @param $b (bool)
179 */
180 public function setShowLessons($b) { $this->bShowLessons = ($b == true); }
181
182 /**
183 * Add a message to the internal array of (error) messages
184 * @param $sMessage (string)
185 */
186 public function addMessage($sMessage) {
187 array_merge($this->asMessages, array($sMessage));
188 }
189
190 /**
191 * Get all timeslices in chronological order
192 * @return array of rmTimeslice
193 */
194 public function getTimeslices() { return $this->aoTimeslices; }
195
196 /**
197 * Return the starting times of every timeslice
198 * @param $bFormat (bool) <tt>true</tt>: Format the times according to the
199 * current locale
200 * <tt>false</tt>: return just the timestamps
201 * @return array
202 */
203 public function getTimesliceBeginnings($bFormat = false) {
204 $aot = $this->getTimeslices();
205 $aRet = array();
206 foreach($aot as $ao) {
207 $aRet[] = $bFormat ? _date("%#I:%M %p", $ao->getBegin()) :
208 $ao->getBegin();
209 }
210 return $aRet;
211 }
212
213 /**
214 * Return the ending times of every timeslice
215 * @param $bFormat (bool) <tt>true</tt>: Format the times according to the
216 * current locale
217 * <tt>false</tt>: return just the timestamps
218 * @return array
219 */
220 public function getTimesliceEndings($bFormat = false) {
221 $aot = $this->getTimeslices();
222 $aRet = array();
223 foreach($aot as $ao) {
224 $aRet[] = $bFormat ? _date("%#I:%M %p", $ao->getEnd()) : $ao->getEnd();
225 }
226 return $aRet;
227 }
228
229 /**
230 * Get a timeslice
231 * @param $n (int) index of the timeslice in the array
232 * @return rmTimeslice
233 */
234 public function getTimeslice($n) { return $this->aoTimeslices[$n]; }
235
236 /**
237 * Determine if the weekend is shown
238 * @return bool
239 */
240 public function isShowWeekend() { return ($this->bShowWeekend == true); }
241
242 /**
243 * Determine if the lesson strings in the booking table are shown
244 * @return bool
245 * FIXME needed?
246 */
247 public function isShowLessons() { return ($this->bShowLessons == true); }
248
249 /**
250 * Determine if the current user has admin rights. This function tests
251 * if the user is in a group which has the privilege of admin rights.
252 * @todo test
253 * @return bool
254 */
255 public function userIsAdmin() {
256 return secure_privilege("mod_roomreservation_admin");
257 }
258
259 /**
260 * Determine if the current user can book rooms. This function tests
261 * if the user is in a group which has the privilege to book rooms.
262 * If no group has this privilege, all users can book.
263 * @todo test
264 * @return bool
265 */
266 public function userCanBook() {
267 if(!rrPrivilegeAssigned("mod_roomreservation_book")) {
268 // If the privilege is not assigned to any group, all users can book
269 return true;
270 } else {
271 // If user is admin, it makes sense that he can book rooms ;-)
272 return secure_privilege("mod_roomreservation_book") ||
273 secure_privilege("mod_roomreservation_admin");
274 }
275 }
276
277 /**
278 * Determine if the current user can view bookings. This function tests
279 * if the user is in a group which has been configured as a group who
280 * can view bookings. If no groups are configured, any user can view the
281 * bookings table.
282 * @todo test
283 * @return bool
284 */
285 public function userCanView() {
286 if(!rrPrivilegeAssigned("mod_roomreservation_view")) {
287 // If the privilege is not assigned to any group, all users can view
288 return true;
289 } else {
290 // If user is admin or can book, it makes sense that he can view bookings
291 return secure_privilege("mod_roomreservation_admin") ||
292 secure_privilege("mod_roomreservation_book") ||
293 secure_privilege("mod_roomreservation_view");
294 }
295 }
296
297 /**
298 * Get the messages that have been produced
299 * @return string
300 */
301 public function getMessages() {
302 return join("\n", $this->asMessages);
303 }
304
305 /***************************************************************************/
306 /**
307 * @}
308 * @name Operations
309 * @{
310 */
311
312 /**
313 * Write the current state of this instance to the config file.
314 * @throws IOException
315 * @return bool
316 */
317 public function writeConfig() {
318 // Open config file
319 $hFile = fopen("mod_room-reservation/config.inc", "w", true);
320 if(!is_resource($hFile)) {
321 throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE);
322 return false;
323 }
324 // Try to lock file repeatedly
325 for($n = 0; !flock($hFile, LOCK_EX); $n++) {
326 if($n > 10) {
327 throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE);
328 return false; // Give up
329 } else {
330 sleep(0.2); // Retry after 100 ms
331 }
332 }
333
334 // Create text for config file
335 $strFile = MOD_ROOM_RESERVATION_CONFIGFILE_HEADER;
336
337 // Timeslices
338 $strFile .= "\$this->flushTimeslices();\n";
339 foreach($this->getTimeslices() as $oTs) {
340 $strFile .= sprintf("\$this->addTimeslice(new ".
341 "mod_roomReservationTimeslice(%d, %d));\n", $oTs->getBegin(),
342 $oTs->getEnd());
343 }
344
345 // Show weekend
346 $strFile .= sprintf("\$this->setShowWeekend(%s);\n",
347 $this->isShowWeekend() ? "true" : "false");
348
349 // Show lessons
350 $strFile .= sprintf("\$this->setShowLessons(%s);\n",
351 $this->isShowLessons() ? "true" : "false");
352
353 $strFile .= "?>";
354
355 // Write to config file and unlock it
356 if(fwrite($hFile, $strFile) == false) {
357 throw new IOException(MOD_ROOM_RESERVATION_ERROR_WRITE_FILE);
358 return false;
359 }
360 if(!flock($hFile, LOCK_UN)) {
361 throw new IOException(MOD_ROOM_RESERVATION_ERROR_UNLOCK_FILE);
362 return false;
363 }
364
365 rrInsertLog("Konfiguration verändert");
366 return true;
367 }
368
369 /**
370 * Read configuration from file. Returns <tt>false</tt> if an error occured,
371 * in this case getMessages() contains error messages.
372 * @return bool
373 */
374 public function readConfig() {
375 global $g_rmCfg;
376 try {
377 require("mod_room-reservation/config.inc");
378 } catch(Exception $e) {
379 $this->addMessage($e->getMessage());
380 }
381 }
382
383 /** @} */
384 }
385 ?>
This page took 0.06568 seconds and 5 git commands to generate.