-
Notifications
You must be signed in to change notification settings - Fork 4
/
past-workouts.php
273 lines (217 loc) · 8.99 KB
/
past-workouts.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
//connect to database
include 'dbconnect.php';
//check that user has a valid cookie, redirect if no valid cookie
include 'php_common/cookiecheck.php';
include 'php_common/phparray-to-htmltable.php';
include 'BoulderRouteGradingSystems.php';
$numworkoutsperpage = 60;
//extract all workouts by a user
//Special case if someone logged no climbs, then no entry in workout_segments is generated, in which case workout_segments.workout_id would be null. However, workouts.workout_id will always be a valid number. Also, make sure to use left outer join since no accompanying workout segments may exist.
$stmt4 = $db->prepare("SELECT *,workouts.workout_id as w_workout_id, workout_segments.workout_id as s_workout_id FROM workouts LEFT OUTER JOIN
workout_segments ON workouts.workout_id = workout_segments.workout_id INNER JOIN gyms on workouts.gymid = gyms.gymid
WHERE workouts.userid = :userid ORDER BY
date_workout DESC, workouts.workout_id DESC, climb_type ASC,
grade_index ASC");
$stmt4->execute(array(':userid'=>$userid));
include 'php_dataquery/getUserGradingPrefs.php';
$allworkouts = $stmt4->fetchAll(PDO::FETCH_ASSOC);
$prev_id = $allworkouts[0]['w_workout_id'];
$i = 1;
$table_html = "";
$len = count($allworkouts); //number of workout segments
$prev_row = 0;
foreach($allworkouts as $row) {
$curr_id = $row['w_workout_id'];
//convert workout_array to html if switching to different
//workout
if ($curr_id != $prev_id) {
//Convert climb tables to workout_array
$workout_array = convertClimbTabletoWorkoutArray($climbTable);
$table_html .= convertArrayToTable($workout_array);
$table_html .= "<tr><td colspan = 3 style='text-align:left'><b>Notes: </b>".$prev_row['boulder_notes']."</td>
<td colspan=3 style='text-align:left'><b>Notes: </b> ".$prev_row['tr_notes']."</td><td colspan=3 style='text-align:left'><b>Notes: </b>".
$prev_row['lead_notes']."</td></tr>
<tr><td colspan = 9 style='text-align:left'><b>Other Notes:</b> ".$prev_row['other_notes']."</td></tr>";
$table_html .= "</table></div></div>";
}
if ($i == 1 || $curr_id != $prev_id) {
//start a new panel since either first workout or new workout
$table_html .= "<div class='panel panel-default pastworkout-panel'>
<div class='panel-heading'>
".$row['date_workout'].": <b>".$row['gym_name']."</b>
<div id='workoutControlButtons-".$curr_id."' class='btn-toolbar pull-right'>
<a class='btn btn-primary btn-xs link-btn' href='workout-edit.php?wid=".$curr_id."'>Edit Workout</a>
<a class='btn btn-danger btn-xs link-btn delete-workout-button' data-workout-id='$curr_id' href='#confirmWorkoutDeleteModal' data-toggle='modal'>Delete Workout</a>
</div>
</div>
<div id='workoutDetails-".$curr_id."' class='panel-body'>
<table class='table table-striped table-bordered'>
<tr><th colspan=3 >Boulder</th><th colspan=3>Top-Rope</th>
<th colspan=3>Lead</th></tr>
<tr><th>Grade</th><th>Ascent Type</th><th>#</th>
<th>Grade</th><th>Ascent Type</th><th>#</th>
<th>Grade</th><th>Ascent Type</th><th>#</th></tr>";
//initialize the workout_array
$workout_array = array();
$b_ind = 0; //boulder index
$t_ind = 0; //top rope index
$l_ind = 0; //lead index
//create associative arrays to group workouts with same display grade and ascent type
$climbTable = array();
$trTable = array();
$leadTable = array();
}
//add the workout to the workout array
if ($row['ascent_type']=='project') {
$ascent_type_str = 'attempt';
}
else {
$ascent_type_str = $row['ascent_type'];
}
if ($row['climb_type']=='boulder') {
$grade_text = $boulderConversionTable[$boulderGradingID][$row['grade_index']];
$climbTable["boulder"."&".$grade_text."&".$ascent_type_str] += $row['reps'];
$colind = 0;
$rowind = $b_ind;
$b_ind++;
$gradeConversionTable = $boulderConversionTable[$boulderGradingID];
}
elseif ($row['climb_type']=='toprope') {
$grade_text = $routeConversionTable[$routeGradingID][$row['grade_index']];
$climbTable["toprope"."&".$grade_text."&".$ascent_type_str] += $row['reps'];
$colind = 3;
$rowind = $t_ind;
$t_ind++;
$gradeConversionTable = $routeConversionTable[$routeGradingID];
}
elseif ($row['climb_type']=='lead') {
$grade_text = $routeConversionTable[$routeGradingID][$row['grade_index']];
$climbTable["lead"."&".$grade_text."&".$ascent_type_str] += $row['reps'];
$colind = 6;
$rowind = $l_ind;
$l_ind++;
$gradeConversionTable = $routeConversionTable[$routeGradingID];
}
//convert workout_array to html table if last workoutid
if ($i==$len) {
//Convert climb tables to workout_array
$workout_array = convertClimbTabletoWorkoutArray($climbTable);
$table_html .= convertArrayToTable($workout_array);
//then add notes
$table_html .= "<tr><td colspan = 3 style='text-align:left'><b>Notes: </b>".$row['boulder_notes']."</td>
<td colspan=3 style=
'text-align:left'><b>Notes: </b>".$row['tr_notes']."</td><td colspan=3 style='text-align:left'><b>Notes: </b>".
$row['lead_notes']."</td></tr><tr><td colspan = 9 style='text-align:left'><b>Other Notes: </b>".$row['other_notes']."</td></tr>";
$table_html .= "</table></div></div>";
}
$i++;
$prev_id = $curr_id;
$prev_row = $row;
}
function convertClimbTabletoWorkoutArray($climbTable) {
$b_ind = 0;
$t_ind = 0;
$l_ind = 0;
$workout_array = array();
foreach ($climbTable as $key => $value) {
//split the key by the delimiter string (&) to get climbType, grade_text, and ascent_type, respectively.
$key_string = explode('&',$key);
$climbType = $key_string[0];
$grade_text = $key_string[1];
$ascent_type = $key_string[2];
if ($climbType=='boulder') {
$colind = 0;
$row_ind = $b_ind;
$b_ind++;
}
elseif ($climbType=='toprope') {
$colind = 3;
$row_ind = $t_ind;
$t_ind++;
}
elseif ($climbType=='lead') {
$colind = 6;
$row_ind = $l_ind;
$l_ind++;
}
$workout_array[$row_ind][$colind] = $grade_text;
$workout_array[$row_ind][$colind+1] = $ascent_type;
$workout_array[$row_ind][$colind+2] = $value;
}
return $workout_array;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Gym Climbing Tracker</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="style.php/mycss.scss">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/uservoice.js"></script>
<script src="js/sitePath.js"></script>
<link rel="stylesheet" type="text/css" href="style.php/mycss.scss">
<script>
window.onload = function() {
document.getElementById('workout-download-btn').onclick = function() {
var iframe = document.createElement('iframe');
iframe.setAttribute("id","download-iframe");
iframe.src = 'downloadworkouts.php';
document.body.appendChild(iframe);
};
$("#confirmWorkoutDeleteModal").on("shown.bs.modal", function(e) {
$(this).find('.btn-ok').data('workoutId', $(e.relatedTarget).data('workoutId'));
});
$('#confirmDeleteButton').click(function() {
var workoutId = $(this).data("workoutId");
$.ajax({
url: sitePath() + "/workout-delete.php?wid=" + workoutId,
success: function(data) {
$("#confirmWorkoutDeleteModal").modal("toggle");
$("#workoutControlButtons-" + workoutId).hide();
$("#workoutDetails-" + workoutId).html("<div class='alert alert-success text-center'><strong>Workout Deleted!</strong></div>");
},
error: function() {
alert('Error in AJAX call');
}
});
});
};
</script>
</head>
<body>
<div id="wrap">
<div id="main">
<?php include_once("php_common/analyticstracking.php") ?>
<?php require("navigation.php"); ?>
<div class="container">
<div class="page-header"><h1>Past Workouts</h1></div>
<button type="button" class="btn btn-primary" id="workout-download-btn">Download All Workouts (.xls)</button>
<div id="pastworkouts">
<?php echo $table_html; ?>
</div>
</div>
</div>
</div>
<?php require("php_common/footer.php"); ?>
</body>
<div class="modal fade" id="confirmWorkoutDeleteModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
Are you sure you would like to delete this workout?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<a id="confirmDeleteButton" class="btn btn-danger btn-ok">Delete Workout</a>
</div>
</div>
</div>
</div>
</html>