forked from rothkj1022/phppickem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry_form.php
273 lines (263 loc) · 11.4 KB
/
entry_form.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
<?php
require_once('includes/application_top.php');
require('includes/classes/team.php');
if ($_POST['action'] == 'Submit') {
$week = $_POST['week'];
$cutoffDateTime = getCutoffDateTime($week);
//update summary table
$sql = "delete from " . DB_PREFIX . "picksummary where weekNum = " . $_POST['week'] . " and userID = " . $user->userID . ";";
$mysqli->query($sql) or die('Error updating picks summary: ' . $mysqli->error);
$sql = "insert into " . DB_PREFIX . "picksummary (weekNum, userID, showPicks) values (" . $_POST['week'] . ", " . $user->userID . ", " . (int)$_POST['showPicks'] . ");";
$mysqli->query($sql) or die('Error updating picks summary: ' . $mysqli->error);
//loop through non-expire weeks and update picks
$sql = "select * from " . DB_PREFIX . "schedule where weekNum = " . $_POST['week'] . " and (DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) < gameTimeEastern and DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) < '" . $cutoffDateTime . "');";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
while ($row = $query->fetch_assoc()) {
$sql = "delete from " . DB_PREFIX . "picks where userID = " . $user->userID . " and gameID = " . $row['gameID'];
$mysqli->query($sql) or die('Error deleting picks: ' . $mysqli->error);
if (!empty($_POST['game' . $row['gameID']])) {
$sql = "insert into " . DB_PREFIX . "picks (userID, gameID, pickID) values (" . $user->userID . ", " . $row['gameID'] . ", '" . $_POST['game' . $row['gameID']] . "')";
$mysqli->query($sql) or die('Error inserting picks: ' . $mysqli->error);
}
}
}
$query->free;
header('Location: results.php?week=' . $_POST['week']);
exit;
} else {
$week = (int)$_GET['week'];
if (empty($week)) {
//get current week
$week = (int)getCurrentWeek();
}
$cutoffDateTime = getCutoffDateTime($week);
$firstGameTime = getFirstGameTime($week);
}
include('includes/header.php');
?>
<script type="text/javascript">
function checkform() {
//make sure all picks have a checked value
var f = document.entryForm;
var allChecked = true;
var allR = document.getElementsByTagName('input');
for (var i=0; i < allR.length; i++) {
if(allR[i].type == 'radio') {
if (!radioIsChecked(allR[i].name)) {
allChecked = false;
}
}
}
if (!allChecked) {
return confirm('One or more picks are missing for the current week. Do you wish to submit anyway?');
}
return true;
}
function radioIsChecked(elmName) {
var elements = document.getElementsByName(elmName);
for (var i = 0; i < elements.length; i++) {
if (elements[i].checked) {
return true;
}
}
return false;
}
function checkRadios() {
$('input[type=radio]').each(function(){
//alert($(this).attr('checked'));
var targetLabel = $('label[for="'+$(this).attr('id')+'"]');
console.log($(this).attr('id')+': '+$(this).is(':checked'));
if ($(this).is(':checked')) {
//console.log(targetLabel);
targetLabel.addClass('highlight');
} else {
targetLabel.removeClass('highlight');
}
});
}
$(function() {
checkRadios();
$('input[type=radio]').click(function(){
checkRadios();
});
$('label').click(function(){
checkRadios();
});
});
</script>
<?php
//display week nav
$sql = "select distinct weekNum from " . DB_PREFIX . "schedule order by weekNum;";
$query = $mysqli->query($sql);
$weekNav = '<div id="weekNav" class="row">';
$weekNav .= ' <div class="navbar3 col-xs-12"><b>Go to week:</b> ';
$i = 0;
if ($query->num_rows > 0) {
while ($row = $query->fetch_assoc()) {
if ($i > 0) $weekNav .= ' | ';
if ($week !== (int)$row['weekNum']) {
$weekNav .= '<a href="entry_form.php?week=' . $row['weekNum'] . '">' . $row['weekNum'] . '</a>';
} else {
$weekNav .= $row['weekNum'];
}
$i++;
}
}
$query->free;
$weekNav .= ' </div>' . "\n";
$weekNav .= '</div>' . "\n";
echo $weekNav;
?>
<div class="row">
<div class="col-md-4 col-xs-12 col-right">
<?php
include('includes/column_right.php');
?>
</div>
<div id="content" class="col-md-8 col-xs-12">
<h2>Week <?php echo $week; ?> - Make Your Picks:</h2>
<p>Please make your picks below for each game.</p>
<?php
//get existing picks
$picks = getUserPicks($week, $user->userID);
//get show picks status
$sql = "select * from " . DB_PREFIX . "picksummary where weekNum = " . $week . " and userID = " . $user->userID . ";";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
$row = $query->fetch_assoc();
$showPicks = (int)$row['showPicks'];
} else {
$showPicks = 1;
}
$query->free;
//display schedule for week
$sql = "select s.*, (DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > gameTimeEastern or DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > '" . $cutoffDateTime . "') as expired ";
$sql .= "from " . DB_PREFIX . "schedule s ";
$sql .= "inner join " . DB_PREFIX . "teams ht on s.homeID = ht.teamID ";
$sql .= "inner join " . DB_PREFIX . "teams vt on s.visitorID = vt.teamID ";
$sql .= "where s.weekNum = " . $week . " ";
$sql .= "order by s.gameTimeEastern, s.gameID";
//echo $sql;
$query = $mysqli->query($sql) or die($mysqli->error);
if ($query->num_rows > 0) {
echo '<form name="entryForm" action="entry_form.php" method="post" onsubmit="return checkform();">' . "\n";
echo '<input type="hidden" name="week" value="' . $week . '" />' . "\n";
//echo '<table cellpadding="4" cellspacing="0" class="table1">' . "\n";
//echo ' <tr><th>Home</th><th>Visitor</th><th align="left">Game</th><th>Time / Result</th><th>Your Pick</th></tr>' . "\n";
echo ' <div class="row">'."\n";
echo ' <div class="col-xs-12">'."\n";
$i = 0;
while ($row = $query->fetch_assoc()) {
$scoreEntered = false;
$homeTeam = new team($row['homeID']);
$visitorTeam = new team($row['visitorID']);
$homeScore = (int)$row['homeScore'];
$visitorScore = (int)$row['visitorScore'];
$rowclass = (($i % 2 == 0) ? ' class="altrow"' : '');
echo ' <div class="matchup">' . "\n";
echo ' <div class="row bg-row1">'."\n";
if (!empty($homeScore) || !empty($visitorScore)) {
//if score is entered, show score
$scoreEntered = true;
if ($homeScore > $visitorScore) {
$winnerID = $row['homeID'];
} else if ($visitorScore > $homeScore) {
$winnerID = $row['visitorID'];
};
//$winnerID will be null if tie, which is ok
echo ' <div class="col-xs-12 center"><b>Final: ' . $row['visitorScore'] . ' - ' . $row['homeScore'] . '</b></div>' . "\n";
} else {
//else show time of game
echo ' <div class="col-xs-12 center">' . date('D n/j g:i a', strtotime($row['gameTimeEastern'])) . ' ET</div>' . "\n";
}
echo ' </div>'."\n";
echo ' <div class="row versus">' . "\n";
echo ' <div class="col-xs-1"></div>' . "\n";
echo ' <div class="col-xs-4">'."\n";
echo ' <label for="' . $row['gameID'] . $visitorTeam->teamID . '" class="label-for-check"><div class="team-logo"><img src="images/logos/'.$visitorTeam->teamID.'.svg" onclick="document.entryForm.game'.$row['gameID'].'[0].checked=true;" /></div></label>' . "\n";
echo ' </div>'."\n";
echo ' <div class="col-xs-2">@</div>' . "\n";
echo ' <div class="col-xs-4">'."\n";
echo ' <label for="' . $row['gameID'] . $homeTeam->teamID . '" class="label-for-check"><div class="team-logo"><img src="images/logos/'.$homeTeam->teamID.'.svg" onclick="document.entryForm.game' . $row['gameID'] . '[1].checked=true;" /></div></label>'."\n";
echo ' </div>' . "\n";
echo ' <div class="col-xs-1"></div>' . "\n";
echo ' </div>' . "\n";
if (!$row['expired']) {
echo ' <div class="row bg-row2">'."\n";
echo ' <div class="col-xs-1"></div>' . "\n";
echo ' <div class="col-xs-4 center">'."\n";
echo ' <input type="radio" class="check-with-label" name="game' . $row['gameID'] . '" value="' . $visitorTeam->teamID . '" id="' . $row['gameID'] . $visitorTeam->teamID . '"' . (($picks[$row['gameID']]['pickID'] == $visitorTeam->teamID) ? ' checked' : '') . ' />'."\n";
echo ' </div>'."\n";
//echo ' <div class="col-xs-2 center" style="font-size: 0.8em;">◀ Choose ▶</div>' . "\n";
echo ' <div class="col-xs-2"></div>' . "\n";
echo ' <div class="col-xs-4 center">'."\n";
echo ' <input type="radio" class="check-with-label" name="game' . $row['gameID'] . '" value="' . $homeTeam->teamID . '" id="' . $row['gameID'] . $homeTeam->teamID . '"' . (($picks[$row['gameID']]['pickID'] == $homeTeam->teamID) ? ' checked' : '') . ' />' . "\n";
echo ' </div>' . "\n";
echo ' <div class="col-xs-1"></div>' . "\n";
echo ' </div>' . "\n";
}
echo ' <div class="row bg-row3">'."\n";
echo ' <div class="col-xs-6 center">'."\n";
echo ' <div class="team">' . $visitorTeam->city . ' ' . $visitorTeam->team . '</div>'."\n";
$teamRecord = trim(getTeamRecord($visitorTeam->teamID));
if (!empty($teamRecord)) {
echo ' <div class="record">Record: ' . $teamRecord . '</div>'."\n";
}
$teamStreak = trim(getTeamStreak($visitorTeam->teamID));
if (!empty($teamStreak)) {
echo ' <div class="streak">Streak: ' . $teamStreak . '</div>'."\n";
}
echo ' </div>'."\n";
echo ' <div class="col-xs-6 center">' . "\n";
echo ' <div class="team">' . $homeTeam->city . ' ' . $homeTeam->team . '</div>'."\n";
$teamRecord = trim(getTeamRecord($homeTeam->teamID));
if (!empty($teamRecord)) {
echo ' <div class="record">Record: ' . $teamRecord . '</div>'."\n";
}
$teamStreak = trim(getTeamStreak($homeTeam->teamID));
if (!empty($teamStreak)) {
echo ' <div class="streak">Streak: ' . $teamStreak . '</div>'."\n";
}
echo ' </div>' . "\n";
echo ' </div>'."\n";
if ($row['expired']) {
//else show locked pick
echo ' <div class="row bg-row4">'."\n";
$pickID = getPickID($row['gameID'], $user->userID);
if (!empty($pickID)) {
$statusImg = '';
$pickTeam = new team($pickID);
$pickLabel = $pickTeam->teamName;
} else {
$statusImg = '<img src="images/cross_16x16.png" width="16" height="16" alt="" />';
$pickLabel = 'None Selected';
}
if ($scoreEntered) {
//set status of pick (correct, incorrect)
if ($pickID == $winnerID) {
$statusImg = '<img src="images/check_16x16.png" width="16" height="16" alt="" />';
} else {
$statusImg = '<img src="images/cross_16x16.png" width="16" height="16" alt="" />';
}
}
echo ' <div class="col-xs-12 center your-pick"><b>Your Pick:</b></br />';
echo $statusImg . ' ' . $pickLabel;
echo '</div>' . "\n";
echo ' </div>' . "\n";
}
echo ' </div>'."\n";
$i++;
}
echo ' </div>' . "\n";
echo ' </div>' . "\n";
echo '<p class="noprint"><input type="checkbox" name="showPicks" id="showPicks" value="1"' . (($showPicks) ? ' checked="checked"' : '') . ' /> <label for="showPicks">Allow others to see my picks</label></p>' . "\n";
echo '<p class="noprint"><input type="submit" name="action" value="Submit" /></p>' . "\n";
echo '</form>' . "\n";
}
echo ' </div>'."\n"; // end col
echo ' </div>'."\n"; // end entry-form row
//echo '<div id="comments" class="row">';
include('includes/comments.php');
//echo '</div>';
include('includes/footer.php');