-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rooms.class.php
125 lines (109 loc) · 4.04 KB
/
Rooms.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
class Rooms{
private $dbFile;
private $rooms;
private $roomsFeatures;
function Rooms($roomsFeatures){
$this->dbFile = '';
$this->rooms = '';
$this->roomsFeatures = $roomsFeatures;
}
public function readRoomsDatabase($datePicked)
{
$formatDate = explode("/", $datePicked);
$this->dbFile = $formatDate[2]."-".$formatDate[1]."-".$formatDate[0]."_rooms_db.txt";
if(!file_exists($this->dbFile)){
$fp = fopen($this->dbFile, "w") or die("Unable to open file for writing!");
foreach ($this->roomsFeatures as $roomId => $roomCapability) {
fwrite($fp, $roomId.','.$roomCapability."\n");
}
fclose($fp);
}
clearstatcache();
$fp = fopen($this->dbFile, 'r') or die("Unable to open file for reading!");
$this->rooms = explode("\n", fread($fp, filesize($this->dbFile)));
fclose($fp);
}
public function writeRoomsDatabase($startTime,$endTime,$roomId)
{
$lines = file($this->dbFile);
$new = '';
if (is_array($lines)) {
foreach($lines as $line) {
$data1room = explode(",", $line);
$line = str_replace("\n","",$line);
if($data1room[0]==$roomId){
$new .= $line . ',' . $startTime . ',' . $endTime . "\n";
} else{
$new .= $line . "\n";
}
}
}
file_put_contents($this->dbFile, $new);
}
public function printRoomsDatabase($datePicked)
{
$this->readRoomsDatabase($datePicked);
foreach ($this->rooms as $room) {
if(!empty($room)){
$data1room = explode(",", $room);
echo '<span class="badge bg-primary">Room ID: '.$data1room[0].'</span> ';
echo '<span class="badge bg-secondary">Capacity: '.$data1room[1].'</span> ';
for ($i=2;$i<sizeof($data1room);$i++){
if ($i%2==0)
echo '<span class="badge rounded-pill bg-danger">'.$data1room[$i].'-'.$data1room[$i+1].'</span>';
}
echo "<br>";
}
}
}
public function getOptimizedCapacityRoom($rooms, $numCapacity)
{
$bestTempRoomId=0;
$bestTempCapacity=999;
foreach ($rooms as $room) {
if(!empty($room)){
$data1room = explode(",", $room);
if($this->checkRoomCapacity($room, $numCapacity)==true && $bestTempCapacity>$data1room[1]){
$bestTempRoomId=$data1room[0];
$bestTempCapacity=$data1room[1];
}
}
}
return $bestTempRoomId;
}
private function checkRoomCapacity($room, $numCapacity)
{
$okCapacity=false;
$data1room = explode(",", $room);
if($data1room[1]>=$numCapacity){
$okCapacity=true;
}
return $okCapacity;
}
private function compareHours($val, $min, $max) {
return ($val > $min && $val < $max);
}
public function getAvailableRooms($startTime,$endTime){
$discard = false;
$roomsOk = array();
foreach ($this->rooms as $room) {
$data1room = explode(",", $room);
for ($i=0;$i<((sizeof($data1room)-2)/2);$i++)
{
if($this->compareHours(strtotime($startTime), strtotime($data1room[2+(2*$i)]), strtotime($data1room[3+(2*$i)]))
|| $this->compareHours(strtotime($endTime), strtotime($data1room[2+(2*$i)]), strtotime($data1room[3+(2*$i)])+1)){
$discard=true;
}
}
if($discard==false)
array_push($roomsOk, $room);
else if( ((sizeof($data1room))-2)/2==0){ // all time frames availables
array_push($roomsOk, $room);
}
$discard = false;
}
return $roomsOk;
}
}
?>