/
user.php
182 lines (134 loc) · 4.23 KB
/
user.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
<?php
include "db.php";
//date format
$df = "M d, Y";
if(isset($_COOKIE["funkyTrainUser"])){
$login = 1;
$username = $_COOKIE["funkyTrainUser"];
}
else{
$login = 0;
}
if ($login == 1) {
$con = mysql_connect(localhost, $uname, $password);
if (!$con){
die("Could not connect" . mysql_error());
}
mysql_select_db($database);
//get the training schedule for the user
$query = sprintf("SELECT currentTraining, start, uid FROM users WHERE name = '%s'", $username);
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
$tpid = $row['currentTraining'];
$start = $row['start'];
$sid = $row['uid'];
}
$today = getdate();
//let's see what the day of training the user is on
if ($start == "0000-00-00") {
//we're starting today
$day = 1;
$query = sprintf("UPDATE users SET start=\"%s-%s-%s\" WHERE name = '%s'", $today['year'], $today['mon'], $today['mday'], $username);
mysql_query($query);
} else {
// if we have already started we need to determine the day
//date_timezone_set("America/Toronto");
$start_day = strtotime($start);
$today = sprintf("%s-%s-%s", $today['year'], $today['mon'], $today['mday']);
//calculate day diffs
$s = strtotime($start);
$end = strtotime($today);
$day = ceil(abs($end - $s) / 86400) + 1;
$time = date('G:i');
}
$datetime = sprintf("<span>Today is %s.<br/>The time is %s.<br/>You started training on %s.<br/>This is day %s of training.<br/></span>", date($df, $end), $time, date($df, $start_day), $day);
//Set cookie to track the day
setcookie("funkyTrainDay",$day, time() + 7200);
//so we know what day it is, let's determine whether we are in pre, training, or post phase
$query = sprintf("SELECT pre, post, training FROM training WHERE tpid = %s", $tpid);
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$pre = $row['pre'];
$post = $row['post'];
$training = $row['training'];
}
//need to chop the value of training into a list
$training = explode(',', $training);
array_unshift($training, $pre);
$training[] = $post;
$one_day = 86400;
$pre_day = $start_day + ($one_day * ($pre-1));
$post_day = $start_day + ($one_day * ($post-1));
$schedule = "<span><h4>Your training calendar:</h4>";
$schedule .= "<table><tr><th>day</th><th>date</th><th>games completed</th></tr>";
foreach ($training as $tday) {
$the_day = $start_day + ($one_day * (intval($tday) - 1));
$query = sprintf("SELECT name, gid from games WHERE gid IN (SELECT gid FROM completed WHERE sid = %s AND day = %s);", $sid, $tday);
//echo $query;
$result = mysql_query($query);
$games = "";
$gids = "";
while($row = mysql_fetch_assoc($result)){
$games .= sprintf("%s, ", $row['name']);
$gids .= sprintf("%s,", $row['gid']);
}
$games = trim($games, ', ');
$gids = trim($gids, ',');
//if () {
//}
//echo $games;
$schedule.= sprintf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>", $tday, date($df, $the_day), $games);
}
$schedule .= "</table>";
if ($day == $pre) {
$phase = "preGames";
}
elseif ($day == $post) {
$phase = "postGames";
}
elseif (in_array($day, $training) == True) {
$phase = "trainingGames";
}
else {
$phase = Null;
}
if ($phase != Null) {
echo "<p>Here are the games you need to play today:</p>";
$query = sprintf("SELECT %s FROM training WHERE tpid = %s", $phase, $tpid);
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$games = $row[$phase];
}
$games = explode(",", $games);
foreach ($games as $game) {
$query = sprintf("SELECT day FROM completed WHERE gid=%s AND sid=%s", $game, $sid);
$result = mysql_query($query);
$completed = False;
while($row = mysql_fetch_assoc($result)) {
if ($day == $row['day']) {
$completed = True;
}
}
if ($completed != True) {
$query = sprintf("SELECT url, name FROM games WHERE gid = %s", $game);
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
$name = $row['name'];
$url = $row['url'];
$output .= sprintf("<a href='%s'>%s</a><br/>", $url, $name);
}
}
}
}
else {
$output .= "<p>You don't need to play any games today.</p>";
}
$output .= "<br/>";
$output .= $datetime;
$output .= $schedule;
}
else {
$output = "0";
}
echo $output;
?>