0;
}
/**
* Retrieve all groups that have a privilege assigned
* @param $strPriv (string) Privilege to test
* @return array
*/
function rrPrivilegedGroups($strPriv) {
$aReturn = array();
$h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1 ".
"ORDER BY act;", $strPriv);
if(pg_num_rows($h) > 0) {
while($a = pg_fetch_array($h)) {
$aReturn[] = $a["act"];
}
}
return $aReturn;
}
/**
* User-defined compare function to compare timeslices
* @param $oTs1 (mod_roomReservationTimeslice)
* @param $oTs2 (mod_roomReservationTimeslice)
* @return (int) -1 if $oTs1 begins before $oTs2,
* 0 if the $oTs1 and $oTs2 have the same beginning,
* 1 if $oTs1 begins after $oTs2.
*/
function rrConfigSortTimeslices(mod_roomReservationTimeslice $oTs1,
mod_roomReservationTimeslice $oTs2) {
if($oTs1->getBegin() == $oTs2->getBegin()) {
return 0;
} else {
return ($oTs1->getBegin() > $oTs2->getBegin()) ? 1 : -1;
}
}
define("MOD_ROOM_RESERVATION_CONFIGFILE_HEADER", "flushTimeslices();
$this->setShowWeekend(false);
$this->setShowLessons(true);
$this->asMessages = array();
$this->readConfig();
}
/**
* **************************************************************************
* @}
* @name Access to attributes
* @{
*/
/**
* Add a timeslice. A check is done that the timeslices do not overlap, and
* in this case, an Exception is thrown.
* @param $oTs (mod_roomReservationTimeslice)
* @throws Exception
* @return void
*/
public function addTimeslice(mod_roomReservationTimeslice $oTs) {
// Check for overlapping timeslices
foreach($this->aoTimeslices as $oOldTs) {
if(($oOldTs->getBegin() < $oTs->getEnd() and
$oOldTs->getEnd() > $oTs->getBegin())) {
throw new Exception(
MOD_ROOM_RESERVATION_ERROR_CONFIG_OVERLAPPING_TIMESLICE);
}
}
$this->aoTimeslices[] = $oTs;
usort($this->aoTimeslices, "rrConfigSortTimeslices");
return;
}
/**
* Delete a timeslice
* @param $oTs (mod_roomReservationTimeslice) the timeslice to delete. If
* the timeslice is not found, an Exception is thrown.
* @throws Exception
* @return void
*/
public function deleteTimeslice(mod_roomReservationTimeslice $oTs) {
for($i = 0; $i < count($this->aoTimeslices); $i++) {
if($this->aoTimeslices[$i]->getBegin() == $oTs->getBegin() and
$this->aoTimeslices[$i]->getEnd() == $oTs->getEnd()) {
// use array_splice because it renumbers the keys
array_splice($this->aoTimeslices, $i, 1);
return;
}
}
throw new Exception(MOD_ROOM_RESERVATION_ERROR_CONFIG_NO_SUCH_TIMESLICE);
}
/**
* Delete all timeslices.
* @return void
*/
public function flushTimeslices() { $this->aoTimeslices = array(); }
/**
* Add a room to the list of rooms who can be booked. Throws an SQLException
* in case of an error.
* @param $sRoom (string) The name of the room
* @throws SQLException, Exception
* @return void
*/
public function whitelistRoom($sRoom) {
if(!$this->isRoomWhitelisted($sRoom)) {
$r = db_store("mod_roomreservation_roomswhitelist",
array("rrr_name" => $sRoom));
if(!$r) {
throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
} else {
log_insert(sprintf("Raum „%s“ für Buchungen zur Verfügung gestellt",
$sRoom));
}
}
}
/**
* Forbid bookings for a room. Throws an SQLException in case of an error.
* @param $sRoom The name of the room
* @throws SQLException
*/
public function unWhitelistRoom($sRoom) {
$h = db_query("DELETE FROM mod_roomreservation_roomswhitelist WHERE ".
"rrr_name = $1;", $sRoom);
if(!$h) {
throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
} else {
log_insert(sprintf("Raum „%s“ für Buchungen gesperrt", $sRoom));
}
}
/**
* Determine if a room is allowed for booking. Throws an SQLException
* in case of an error.
* @param $sRoom (string) The name of the room
* @return bool
* @throws SQLException
*/
public function isRoomWhitelisted($sRoom) {
$h = db_query("SELECT * FROM mod_roomreservation_roomswhitelist WHERE ".
"rrr_name=$1;", $sRoom);
if(!$h) {
throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
}
return (pg_num_rows($h) > 0);
}
/**
* Get all rooms that are allowed for booking. Throws an SQLException
* in case of an error.
* @throws SQLException
* @return array of mod_roomReservationRoomsManager objects
*/
public function getWhitelistedRooms() {
$aor = mod_roomReservationRoomsManager::getRooms();
$ar = array();
foreach($aor as $key => $or) {
if($this->isRoomWhitelisted($or->getName())) {
$ar[] = $or;
}
}
return $ar;
}
/**
* Show or hide the weekend
* @param $b (bool)
*/
public function setShowWeekend($b) { $this->bShowWeekend = ($b == true); }
/**
* Show or hide the lesson strings in the booking table
* @param $b (bool)
*/
public function setShowLessons($b) { $this->bShowLessons = ($b == true); }
/**
* Add a message to the internal array of (error) messages
* @param $sMessage (string)
*/
public function addMessage($sMessage) {
array_merge($this->asMessages, array($sMessage));
}
/**
* Get all timeslices in chronological order
* @return array of rmTimeslice
*/
public function getTimeslices() { return $this->aoTimeslices; }
/**
* Return the starting times of every timeslice
* @param $bFormat (bool) true: Format the times according to the
* current locale
* false: return just the timestamps
* @return array
*/
public function getTimesliceBeginnings($bFormat = false) {
$aot = $this->getTimeslices();
$aRet = array();
foreach($aot as $ao) {
$aRet[] = $bFormat ? gmstrftime(_("%#I:%M %p"), $ao->getBegin()) :
$ao->getBegin();
}
return $aRet;
}
/**
* Return the ending times of every timeslice
* @param $bFormat (bool) true: Format the times according to the
* current locale
* false: return just the timestamps
* @return array
*/
public function getTimesliceEndings($bFormat = false) {
$aot = $this->getTimeslices();
$aRet = array();
foreach($aot as $ao) {
$aRet[] = $bFormat ? gmstrftime(_("%#I:%M %p"), $ao->getEnd()) :
$ao->getEnd();
}
return $aRet;
}
/**
* Get a timeslice
* @param $n (int) index of the timeslice in the array
* @return rmTimeslice
*/
public function getTimeslice($n) { return $this->aoTimeslices[$n]; }
/**
* Determine if the weekend is shown
* @return bool
*/
public function isShowWeekend() { return ($this->bShowWeekend == true); }
/**
* Determine if the lesson strings in the booking table are shown
* @return bool
*/
public function isShowLessons() { return ($this->bShowLessons == true); }
/**
* Determine if the current user has admin rights. This function tests
* if the user is in a group which has the privilege of admin rights.
* @todo test
* @return bool
*/
public function userIsAdmin() {
return secure_privilege("mod_roomreservation_admin");
}
/**
* Determine if the current user can book rooms. This function tests
* if the user is in a group which has the privilege to book rooms.
* If no group has this privilege, all users can book.
* @todo test
* @return bool
*/
public function userCanBook() {
if(!rrPrivilegeAssigned("mod_roomreservation_book")) {
// If the privilege is not assigned to any group, all users can book
return true;
} else {
// If user is admin, it makes sense that he can book rooms ;-)
return secure_privilege("mod_roomreservation_book") ||
secure_privilege("mod_roomreservation_admin");
}
}
/**
* Determine if the current user can view bookings. This function tests
* if the user is in a group which has been configured as a group who
* can view bookings. If no groups are configured, any user can view the
* bookings table.
* @todo test
* @return bool
*/
public function userCanView() {
if(!rrPrivilegeAssigned("mod_roomreservation_view")) {
// If the privilege is not assigned to any group, all users can view
return true;
} else {
// If user is admin or can book, it makes sense that he can view bookings
return secure_privilege("mod_roomreservation_admin") ||
secure_privilege("mod_roomreservation_book") ||
secure_privilege("mod_roomreservation_view");
}
}
/**
* Get the messages that have been produced
* @return string
*/
public function getMessages() {
return join("\n", $this->asMessages);
}
/***************************************************************************/
/**
* @}
* @name Operations
* @{
*/
/**
* Write the current state of this instance to the config file.
* @throws IOException
* @return bool
*/
public function writeConfig() {
// Open config file
$hFile = fopen("mod_room-reservation/config.inc", "w", true);
if(!is_resource($hFile)) {
throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE);
return false;
}
// Try to lock file repeatedly
for($n = 0; !flock($hFile, LOCK_EX); $n++) {
if($n > 10) {
throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE);
return false; // Give up
} else {
sleep(0.2); // Retry after 100 ms
}
}
// Create text for config file
$strFile = MOD_ROOM_RESERVATION_CONFIGFILE_HEADER;
// Timeslices
$strFile .= "\$this->flushTimeslices();\n";
foreach($this->getTimeslices() as $oTs) {
$strFile .= sprintf("\$this->addTimeslice(new ".
"mod_roomReservationTimeslice(%d, %d));\n", $oTs->getBegin(),
$oTs->getEnd());
}
// Show weekend
$strFile .= sprintf("\$this->setShowWeekend(%s);\n",
$this->isShowWeekend() ? "true" : "false");
// Show lessons
$strFile .= sprintf("\$this->setShowLessons(%s);\n",
$this->isShowLessons() ? "true" : "false");
$strFile .= "?>";
// Write to config file and unlock it
if(fwrite($hFile, $strFile) == false) {
throw new IOException(MOD_ROOM_RESERVATION_ERROR_WRITE_FILE);
return false;
}
if(!flock($hFile, LOCK_UN)) {
throw new IOException(MOD_ROOM_RESERVATION_ERROR_UNLOCK_FILE);
return false;
}
rrInsertLog("Konfiguration verändert");
return true;
}
/**
* Read configuration from file. Returns false if an error occured,
* in this case getMessages() contains error messages.
* @return bool
*/
public function readConfig() {
global $g_rmCfg;
try {
require("mod_room-reservation/config.inc");
} catch(Exception $e) {
$this->addMessage($e->getMessage());
}
}
/** @} */
}
?>