-
Notifications
You must be signed in to change notification settings - Fork 2
/
updatePick.php
executable file
·310 lines (273 loc) · 13.6 KB
/
updatePick.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<html>
<head>
<?php
echo "<link rel=\"stylesheet\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\">";
echo "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>";
echo "<script src=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js\"></script>";
$link = mysqli_connect("localhost", "jklamer", "jackpw","domerdoors")
or die ("Connection failed: " .mysqli_connect_error());
echo "</head>";
//variables
$blankstring="";
$mates= array();
$numInd=0;
$querylist = array();
$undoMates =array();
$failed = false;
//get needed info
$netID = (ISSET($_REQUEST['netID']) ? "'".$_REQUEST['netID']."'" : null); array_push($mates,$netID);
$password = (ISSET($_REQUEST['password']) ? "'".$_REQUEST['password']."'" : null);
$roomNum = (!empty($_REQUEST['RoomNum']) ? "'".$_REQUEST['RoomNum']."'" : null);
$mate1 = (!empty($_REQUEST['mate1']) ? "'".$_REQUEST['mate1']."'" : null); if($mate1!=null ) {$numInd= $numInd +1; array_push($mates,$mate1);}
$mate2 = (!empty($_REQUEST['mate2']) ? "'".$_REQUEST['mate2']."'" : null); if($mate2!=null ) {$numInd= $numInd +1; array_push($mates,$mate2);}
$mate3 = (!empty($_REQUEST['mate3']) ? "'".$_REQUEST['mate3']."'" : null); if($mate3!=null ) {$numInd= $numInd +1;array_push($mates,$mate3);}
$mate4 = (!empty($_REQUEST['mate4']) ? "'".$_REQUEST['mate4']."'" : null); if($mate4!=null ) {$numInd= $numInd +1;array_push($mates,$mate4);}
$mate5 = (!empty($_REQUEST['mate5']) ? "'".$_REQUEST['mate5']."'" : null); if($mate5!=null ) {$numInd= $numInd +1;array_push($mates,$mate5);}
?>
<body>
<!---Navigation Bar at top-->
<nav class="navbar navbar-inverse navbar-static-top">
<div class="container">
<!--<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">-->
<ul class="nav navbar-nav">
<li><a href="browseFloor.php?netID=<?php echo "$netID"?>&password=<?php echo "$password"?>">Browse Rooms</a></li>
<li><a href="userPrefs.php?netID=<?php echo "$netID"?>&password=<?php echo "$password"?>">User Preferences</a></li>
<li class="active"><a href="pick.php?netID=<?php echo "$netID"?>&password=<?php echo "$password"?>">Pick <span class="sr-only">(current)</span></a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="index.html">Logout</a></li>
</ul>
</div>
</div>
</nav>
<?php
echo "<div class=\"container\">";
//find dorm
$dormQ=mysqli_query($link,"select dorm from Resident where netID=$netID;");
if(mysqli_num_rows( $dormQ)==0){
echo "<div class=\"alert alert-danger\"><strong>ERROR: </strong>" ;
echo "Incorrect NetID</div>";
$failed=true;
}
$row=mysqli_fetch_assoc($dormQ);
$dorm=$row['dorm'];
//see if person submitting is current pick
$currPickQ=mysqli_query($link,"select netID from Pick where dorm='$dorm' and hasPicked=0 order by pickNum limit 1;");
if(mysqli_num_rows( $currPickQ)==0){
echo "<div class=\"alert alert-danger\"><strong>ERROR: </strong>" ;
echo "No One Left to pick</div>";
$failed=true;
}
$row=mysqli_fetch_assoc($currPickQ);
$currPick=$row['netID'];
if($netID != "'".$currPick."'")
{
echo "<div class=\"alert alert-danger\"><strong>ERROR: </strong>" ;
echo "It is not your turn to pick</div>";
$failed=true;
}
//find number of residents in dormRoom selected
$numResQ=mysqli_query($link,"Select numResidents as num from Room where roomNum=$roomNum and isOccupied=0 and dorm='$dorm';");
if(mysqli_num_rows( $numResQ)==0){
echo "<div class=\"alert alert-danger\"><strong>ERROR: </strong>" ;
echo "Room is occupied or does not exist in this dorm</div>";
$failed=true;
}
$row=mysqli_fetch_assoc($numResQ);
$numRes=$row['num'];
//make sure right number of roomates selected
if($numRes-1 != $numInd)
{
echo "<div class=\"alert alert-danger\"><strong>ERROR: </strong>" ;
echo "Incorrect number of roommates</div>";
$failed=true;
}
//upate resident and pick and undo if no real person
//HAPPENS ALL OR NONE BABY
foreach($mates as $mate)
{
$testQ=mysqli_query($link, "select * from Resident where netID=$mate and dorm='$dorm' and roomNum is null;");
if(mysqli_num_rows( $testQ) == 0)
{
foreach($undoMates as $umate)
{
array_push($querylist,"Update Resident set roomNum=NULL where netID=$umate;");
array_push($querylist,"Update Pick set hasPicked=0 where netID=$umate;");
}
echo "<div class=\"alert alert-danger\"><strong>ERROR: </strong>" ;
echo "Incorrect Roommate netID or Roomate already in Room</div>";
$failed=true;
}else
{
array_push($querylist,"Update Resident set roomNum=$roomNum where netID=$mate;");
array_push($querylist,"Update Pick set hasPicked=1 where netID=$mate;");
array_push($undoMates,$mate);
}
}
//if any error check failed
if($failed)
{
echo "<a class=\"btn btn-danger\" href=\"pick.php?netID=$netID&password=$password\">Return</a>";
echo "</div>\n";
mysqli_close($link);
exit();
} else {
//run the script to update everyone's recommended rooms
$residents = "select * from Resident where dorm='$dorm';";
$residents = mysqli_query($link, $residents);
while($preferences = mysqli_fetch_assoc($residents)){
$net = $preferences['netID'];
$roomArray = array();
$available = "select * from Available where dorm='$dorm';";
$availableResult = mysqli_query($link, $available);
while($row = mysqli_fetch_assoc($availableResult)){
$roomArray[$row['roomNum']] = 0;
}
//Check if their stairs preference is good
$stairs = "select roomNum, closeToStairs from Available where dorm='$dorm';";
$stairs = mysqli_query($link, $stairs);
while($row = mysqli_fetch_assoc($stairs)){
if($preferences['closeToStairs'] == $row['closeToStairs']){
$roomArray[$row['roomNum']]++;
}
}
//Check if their Elevator preference is good
$elevator = "select roomNum, closeToElevator from Available where dorm='$dorm';";
$elevator = mysqli_query($link, $elevator);
while($row = mysqli_fetch_assoc($elevator)){
if($preferences['closeToElevator'] == $row['closeToElevator']){
$roomArray[$row['roomNum']]++;
}
}
//Check if their view preference is good
$view = "select roomNum, hasGoodView from Available where dorm='$dorm';";
$view = mysqli_query($link, $view);
while($row = mysqli_fetch_assoc($view)){
if($preferences['goodView'] == $row['hasGoodView']){
$roomArray[$row['roomNum']]++;
}
}
//check if the room is in their preferred section
$section = "select roomNum, sectionID from Available where dorm='$dorm';";
$section = mysqli_query($link, $section);
while($row = mysqli_fetch_assoc($section)){
if($preferences['prefSection'] == $row['sectionID']){
$roomArray[$row['roomNum']]++;
}
}
//check if the room is in their preferred floor
$floor = "select roomNum, floor from Available where dorm='$dorm';";
$floor = mysqli_query($link, $floor);
while($row = mysqli_fetch_assoc($floor)){
if($preferences['prefFloor'] == $row['floor']){
$roomArray[$row['roomNum']]++;
}
}
//check if the room has their preferred amount of Roommates
$numResidents = "select roomNum, numResidents from Available where dorm='$dorm';";
$numResidents = mysqli_query($link, $numResidents);
while($row = mysqli_fetch_assoc($numResidents)){
if($preferences['prefRoomSize'] == $row['numResidents']){
$roomArray[$row['roomNum']]+=3; //add three points for the right room size
}
}
//Check if they are near staff
$staff = "select roomNum, floor from Room R where dorm='$dorm' and staffRoom=1;";
$staff = mysqli_query($link, $staff);
while($row = mysqli_fetch_assoc($staff)){
if($preferences['closeToStaff'] == 1){ //user wants to be close to staff
foreach($roomArray as $room => $score){
if(abs($room-$row['roomNum']) <= 6){
$roomArray[$room]++;
}
}
} else { //user wants to be far from staff
foreach($roomArray as $room => $score){
if(abs($room-$row['roomNum']) <= 6){
$roomArray[$room]--;
}
}
}
}
//check if the section is a party section
$partyYes = "select A.roomNum, A.dorm, A.sectionID from Available A, ( select ID, dorm, sectionName, count(case when party=1 then 1 end) as party, count(case when party=0 then 1 end) as noParty from (select R.name, R.dorm, R.roomNum, S.ID, S.name as sectionName, party from Resident R, ( select R.roomNum, S.ID, S.name, S.dorm from Room R, Section S where S.ID=R.sectionID) S where R.roomNum=S.roomNum and R.dorm=S.dorm ) S group by S.ID having party > noParty ) P where P.ID = A.sectionID and A.dorm = '$dorm';";
$partyNo = "select A.roomNum, A.dorm, A.sectionID from Available A, ( select ID, dorm, sectionName, count(case when party=1 then 1 end) as party, count(case when party=0 then 1 end) as noParty from (select R.name, R.dorm, R.roomNum, S.ID, S.name as sectionName, party from Resident R, ( select R.roomNum, S.ID, S.name, S.dorm from Room R, Section S where S.ID=R.sectionID) S where R.roomNum=S.roomNum and R.dorm=S.dorm ) S group by S.ID having party < noParty ) P where P.ID = A.sectionID and A.dorm = '$dorm';";
if($preferences['party'] == 1){
$partyYes = mysqli_query($link, $partyYes);
while($row = mysqli_fetch_assoc($partyYes)){
$roomArray[$row['roomNum']]+=2;
}
$partyNo = mysqli_query($link, $partyNo);
while($row = mysqli_fetch_assoc($partyNo)){
$roomArray[$row['roomNum']]--;
}
} else if($preferences['party'] == 0) {
$partyYes = mysqli_query($link, $partyYes);
while($row = mysqli_fetch_assoc($partyYes)){
$roomArray[$row['roomNum']]-=2;
}
$partyNo = mysqli_query($link, $partyNo);
while($row = mysqli_fetch_assoc($partyNo)){
$roomArray[$row['roomNum']]++;
}
}
//check if the section is a study section
$studyYes = "select A.roomNum, A.dorm, A.sectionID from Available A, ( select ID, dorm, sectionName, count(case when study=1 then 1 end) as study, count(case when study=0 then 1 end) as noStudy from (select R.name, R.dorm, R.roomNum, S.ID, S.name as sectionName, study from Resident R, ( select R.roomNum, S.ID, S.name, S.dorm from Room R, Section S where S.ID=R.sectionID) S where R.roomNum=S.roomNum and R.dorm=S.dorm ) S group by S.ID having study > noStudy ) P where P.ID = A.sectionID and A.dorm = '$dorm';";
$studyNo = "select A.roomNum, A.dorm, A.sectionID from Available A, ( select ID, dorm, sectionName, count(case when study=1 then 1 end) as study, count(case when study=0 then 1 end) as noStudy from (select R.name, R.dorm, R.roomNum, S.ID, S.name as sectionName, study from Resident R, ( select R.roomNum, S.ID, S.name, S.dorm from Room R, Section S where S.ID=R.sectionID) S where R.roomNum=S.roomNum and R.dorm=S.dorm ) S group by S.ID having study < noStudy ) P where P.ID = A.sectionID and A.dorm = '$dorm';";
if($preferences['study'] == 1){
$studyYes = mysqli_query($link, $studyYes);
while($row = mysqli_fetch_assoc($studyYes)){
$roomArray[$row['roomNum']]+=2;
}
$studyNo = mysqli_query($link, $studyNo);
while($row = mysqli_fetch_assoc($studyNo)){
$roomArray[$row['roomNum']]--;
}
} else if($preferences['study'] == 0) {
$studyYes = mysqli_query($link, $studyYes);
while($row = mysqli_fetch_assoc($studyYes)){
$roomArray[$row['roomNum']]-=2;
}
$studyNo = mysqli_query($link, $studyNo);
while($row = mysqli_fetch_assoc($studyNo)){
$roomArray[$row['roomNum']]++;
}
}
//check how many people from your grade are in your section
$sameYear = "select A.roomNum, A.dorm, A.sectionID from Available A, ( select ID, dorm, sectionName, count(case when year=".$preferences['year']." then 1 end) as sameGrade, count(case when year<>".$preferences['year']." then 1 end) as diffGrade from (select R.name, R.dorm, R.roomNum, S.ID, S.name as sectionName, year from Resident R, ( select R.roomNum, S.ID, S.name, S.dorm from Room R, Section S where S.ID=R.sectionID) S where R.roomNum=S.roomNum and R.dorm=S.dorm ) S group by S.ID having sameGrade > diffGrade ) P where P.ID = A.sectionID and A.dorm = '$dorm';";
$sameYear = mysqli_query($link, $sameYear);
while($row = mysqli_fetch_assoc($studyYes)){
$roomArray[$row['roomNum']]++;
}
//Enter the array into the database
$query = "delete from Recommended where netID='$net';";
mysqli_query($link, $query);
foreach($roomArray as $room => $score){
$query = "insert into Recommended (netID, roomNum, dorm, score, sqareFootage) select '$net', '$room', '$dorm', '$score', sqareFootage from Room where roomNum='$room' and dorm='$dorm';";
if(mysqli_query($link, $query)){
} else {
echo $query;
}
}
}
}
//update Room
array_push($querylist,"Update Room set isOccupied=1 where dorm='$dorm' and roomNum=$roomNum;");
//update each roomate
foreach($querylist as $query)
{
$result = mysqli_query($link,$query);
if(!$result)
{
echo "Query Failed: ".$query ;
}
}
?>
</body>
<?php
mysqli_close($link);
//go to pick page
$url = 'pick.php';
$params = 'netID='.$netID.'&password='.$password;
header('Location: '.$url.'?'.$params);
?>