3 * @file mod_roomReservationRoomsManager.inc
4 * Class for the management of rooms
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
8 * TODO: move into M Class Library
9 * Copyright © 2007 Roland Hieber
11 * Permission is hereby granted, free of charge, to any person obtaining
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 require_once("db.inc");
31 require_once("mod_room-reservation/functions.inc");
32 require_once("mod_room-reservation/mod_roomReservationRoom.inc");
33 require_once("mod_room-reservation/mod_roomReservationConfig.inc");
35 /** Simple class for creating, editing and deleting rooms */
36 class mod_roomReservationRoomsManager {
37 /** (mod_roomReservationConfig) Reference to the configuration */
42 * @param $oCfg (reference to mod_roomReservationConfig) Reference to the
43 * configuration object
44 * @return rmRoomsManager
46 public function __construct(mod_roomReservationConfig &$oCfg) {
51 * Insert or update a room in the database
52 * param $or (rsRoom) Room to store in the database
53 * return (int) The UID of the booking, or <tt>-1</tt> in case of a failure.
54 * Call getLastError() to get more information about the error.
56 /** public function write(rsRoom $or) {
57 // Only administrators are allowed to create and alter rooms
58 if(!$this->oCfg->userIsAdmin()) {
59 // TODO throw exception
60 setLastError(RS_ERROR_ACCESS_DENIED);
68 if($or->getUid() == null) {
69 // No UID yet, insert new room
70 $strLog = sprintf("Raum „%s“ angelegt", $or->getName());
72 $strWhere = "rsr_uid = ".qp(intval($or->getUid()));
73 $strLog = sprintf("Raum „%s“ geändert", $or->getName());
76 $aPut["rsr_name"] = $or->getName();
77 db_store("rooms", $aPut, $strWhere == "" ? null : $strWhere);
79 $hQuery = db_query("SELECT currval('roomschedule_rooms_rsr_uid_seq') ".
80 "FROM roomschedule_rooms;");
81 $nNewUid = pg_fetch_result($hQuery, 0, "currval");
90 * Delete a room from the database
91 * param $nUid (int) Unique ID of the room
92 * return (bool) <tt>true</tt> on success, otherwise <tt>false</tt>.
93 * Call getLastError() to get more information about the error.
95 /* public function delete($nUid) {
96 // Only administrators are allowed to delete rooms
97 if(!$this->oCfg->userIsAdmin()) {
98 // TODO throw exception
99 setLastError(RS_ERROR_ACCESS_DENIED);
102 // Delete it from the database
103 $strRoom = $this->getRoomName($nUid);
104 if(!db_query("DELETE FROM roomschedule_rooms WHERE rsr_uid = $1;",
106 // TODO throw exception
107 setLastError(RS_ERROR_SQL);
110 rsInsertLog(sprintf("Raum „%s“ gelöscht", $strRoom));
117 * Get all rooms from the database
118 * @return array of mod_roomReservationRoom
120 function getRooms() {
122 $hQuery = db_query("SELECT * FROM rooms ORDER BY name;");
123 while($arResult = pg_fetch_array($hQuery)) {
124 $aoReturn[] = new mod_roomReservationRoom($arResult["name"],
125 $arResult["room_no"], $arResult["floor"], $arResult["building"],
126 $arResult["location"]);