-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.php
318 lines (222 loc) · 9.22 KB
/
stats.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
311
312
313
314
315
316
317
318
<?php
// Check if logged in
include 'login-check.php';
?>
<html>
<head>
<?php include 'user-style.php'; ?>
<title>Statistics</title>
</head>
<body>
<?php
include 'dbconnect.php';
include 'student-header.php';
// Find user id
$usrResult = $db->prepare("SELECT id FROM user WHERE `login`=:user");
$usrResult->bindValue(":user", $_SESSION['username'], PDO::PARAM_STR);
$usrResult->execute();
$firstResult = $usrResult->fetch();
$usrID = $firstResult['id'];
// Find user's group id
$grpID = $_SESSION['groupnum'];
// If the user is part of a group
if ($grpID != 0){
// Find game date
$simData = $db->prepare("SELECT `game_date` FROM `sim`");
$simData->execute();
$simResult = $simData->fetch();
$gameDate = $simResult['game_date'];
?>
<div class='content'>
<h2 class="title">User Activity</h2>
<?php
// Search for all of the voting activity of members of this group
$activitySearch = $db->prepare("SELECT `user`.`display_name`, COUNT(`vote`.`id`) AS `votenum` , `user`.`group` FROM `user` LEFT JOIN `vote` ON `user`.`id` = `vote`.`user` WHERE `user`.`group` = :grpID GROUP BY `user`.`id` ORDER BY `votenum` DESC");
$activitySearch->bindValue(":grpID", $grpID, PDO::PARAM_INT);
$activitySearch->execute();
// If user details found - and they should be
if ( $activitySearch->rowCount() ){
echo "<table>";
echo "<tr><th>Member</th><th>Number of Votes</th></tr>";
$oddRow = true;
while ( $activityDetails = $activitySearch->fetch() ){
if ($oddRow) {
echo "<tr class='oddRow'>";
} else {
echo "<tr class='evenRow'>";
}
$oddRow = !$oddRow;
echo "<td>".$activityDetails["display_name"]."</td>";
echo "<td>".$activityDetails["votenum"]."</td>";
echo "</tr>";
}
echo "</table>";
} else echo "<p> - No users (somehow)</p>";
?>
</div>
<div class='content'>
<h2 class="title">Consensus</h2>
<?php
//$tskSearch = $db->prepare("SELECT `task`.`id` FROM `task` WHERE `task`.`group` = :grpID");
$tskSearch = $db->prepare("SELECT `task`.`id` FROM `task` INNER JOIN `task_group` ON `task`.`taskgroup`=`task_group`.`id` INNER JOIN `task_info` ON `task_group`.`task`=`task_info`.`id` WHERE (`task`.`group`=:grpID AND `task_group`.`startdate` <= CURDATE())");
$tskSearch->bindValue(":grpID", $grpID, PDO::PARAM_INT);
$tskSearch->execute();
$noVotes = 0;
$undecided = 0;
$consensus = 0;
$openTasks = 0;
if ( $tskSearch->rowCount() ){
// Loop through all tasks
while ($task = $tskSearch->fetch() ){
// Find votes for this tasks
$voteSearch = $db->prepare("SELECT *, COUNT(`option`) AS votes FROM `vote` WHERE `task` = :tskID GROUP BY `option` ORDER BY `votes` DESC");
$voteSearch->bindValue(":tskID", $task['id'], PDO::PARAM_INT);
$voteSearch->execute();
// If only votes for one option (has to be at least 1 vote?)
if ( $voteSearch->rowCount()==1 ){
$voteDetails = $voteSearch->fetch();
if ( $voteDetails["votes"] > 1 ) $consensus++; // Everyone voted same
else if ($voteDetails["user"] > 0) $consensus++; // Only one person voted
else $noVotes++; // Only system voted
} else {
// Multiple items voted for
if ( $voteSearch->rowCount()>0 ) $undecided++; // Votes on multiple
else $openTasks++; // Unprocessed no vote tasks
}
}
} else echo "<p> - No tasks found</p>";
echo "<table>";
echo "<tr class='oddRow'><td>Number of tasks:</td><td>".$tskSearch->rowCount()."</td></tr>";
echo "<tr class='evenRow'><td>Tasks with consensus (everyone voted for the same option):</td><td>$consensus</td></tr>";
echo "<tr class='oddRow'><td>Tasks without consensus (people voted for different options):</td><td>$undecided</td></tr>";
echo "<tr class='evenRow'><td>Tasks with no votes:</td><td>$noVotes</td></tr>";
// Find tasks still open
$openSearch = $db->prepare("SELECT `task`.`id` FROM `task` INNER JOIN `task_group` ON `task`.`taskgroup`=`task_group`.`id` INNER JOIN `task_info` ON `task_group`.`task`=`task_info`.`id` WHERE (`task`.`group`=:grpID AND `task_group`.`startdate` <= CURDATE() AND `task_group`.`enddate` > CURDATE())");
$openSearch->bindValue(":grpID", $grpID, PDO::PARAM_INT);
$openSearch->execute();
echo "<tr class='oddRow'><td>Tasks still open:</td><td>".$openSearch->rowCount()."</td></tr>";
echo "</table>";
?>
</div>
<div class='content'>
<h2 class="title">Download Data</h2>
<a href="csv-stats.php">Right-click here</a> and choose to save as a file with a .csv extension. You can then open the file in Excel.
</div>
<div class='content'>
<h2 class="title">Overview Graph (not compatible with IE8)</h2>
<br />
<?php
// Get dates for stored hitorical params
$historySearch = $db->prepare("SELECT `date` FROM `group_history` WHERE `group`=:grpID GROUP BY `date` ORDER BY `date` DESC");
$historySearch->bindValue(":grpID", $grpID, PDO::PARAM_INT);
$historySearch->execute();
$pastItems = $historySearch->rowCount();
// Find parameters for this group
$paramSearch = $db->prepare("SELECT `type_params`.`id`, `type_params`.`parameter`, `type_params`.`type`, `group_param`.`value` FROM `group_param` INNER JOIN `type_params` ON `group_param`.`type` = `type_params`.`id` WHERE `group_param`.`group` = :grpID AND `type_params`.`type`!='Text'");
$paramSearch->bindValue(":grpID", $grpID, PDO::PARAM_INT);
$paramSearch->execute();
$entries = $paramSearch->rowCount();
//$result = mysql_query("SELECT `type_params`.`id`, `type_params`.`parameter`, `type_params`.`type`, `group_param`.`value` FROM `group_param` INNER JOIN `type_params` ON `group_param`.`type` = `type_params`.`id` WHERE `group_param`.`group` = $grpID", $db);
//if ($result) $entries = mysql_num_rows($result);
//else $entries = 0;
echo "<form action='stats.php#graph' method='get'>";
if ( isset($_GET['p']) ) $pid=$_GET['p']; else $pid=1;
// Loop and provide drop-down box
echo "Choose the parameter to display: <select name='p'>";
while ($param = $paramSearch->fetch() ){
echo "<option value='".$param["id"]."'";
if ($param["id"]==$pid) echo " selected='selected'";
echo ">".$param["parameter"]."</option>";
}
echo "</select>";
echo "<input type='submit' value='View' />";
echo "</form>";
?>
<p id="graph">
<canvas id="graphArea" width="800" height="400"></canvas>
</p>
</div>
<?php
} else { // Not in a group
?>
<div class="content">
<p>You are not currently part of any group. Your tutor will be in touch once you have been added to one.</p>
</div>
<?php
}
?>
</body>
<?php
// Trigger the graph drawing
if ( isset($_GET['p']) ){
$valueList = "";
$dataSearch = $db->prepare("SELECT `group_history`.`value` FROM `group_history` INNER JOIN `type_params` ON `group_history`.`type` = `type_params`.`id` WHERE `group_history`.`group`=:grpID AND `type_params`.`id`=:pid ORDER BY `date` DESC");
$dataSearch->bindValue(":grpID", $grpID, PDO::PARAM_INT);
$dataSearch->bindValue(":pid", $pid, PDO::PARAM_INT);
$dataSearch->execute();
$paramSet = $dataSearch->fetchAll();
for ($hlp=count($paramSet)-1; $hlp>=0; $hlp--) {
$valueList = $valueList.$paramSet[$hlp]["value"].",";
}
// Add current value
$currentSearch = $db->prepare("SELECT `value` FROM `group_param` WHERE `type`=:type AND `group`=:group");
$currentSearch->bindValue(":type", $pid, PDO::PARAM_INT);
$currentSearch->bindValue(":group", $grpID, PDO::PARAM_INT);
$currentSearch->execute();
// Must be a current value
// - but if a ?, then just re-use last value.
$value = $currentSearch->fetch();
if ($value["value"] != "?") $valueList = $valueList.$value["value"];
else $valueList = $valueList.$paramSet[0]["value"];
} else $valueList = "";
?>
<script>
// Graphics context
var c = document.getElementById("graphArea");
var ctx = c.getContext("2d");
ctx.fillStyle="#EEEEEE";
ctx.fillRect(0,0,800,400);
// Draw Axis
ctx.lineWidth = 2;
ctx.beginPath(); // X
ctx.moveTo(80, 380);
ctx.lineTo(780, 380);
ctx.stroke();
ctx.beginPath(); // Y
ctx.moveTo(80, 380);
ctx.lineTo(80, 20);
ctx.stroke();
var values = [<?php echo $valueList; ?>];
// Find min and max
var min = values[0];
var max = values[0];
for (var lp=1; lp<values.length; lp++){
if (values[lp]<min) min = values[lp];
if (values[lp]>max) max = values[lp];
}
// Config variables
var span = max - min;
if (span==0){
max += 10;
min -= 10;
span = 20;
}
var step = 700 / (values.length-1);
var unit = 360/span;
// Loop to display
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = '#0000DD';
ctx.moveTo( 80, 380-(unit*(values[0]-min)) );
for (var lp=1; lp<values.length; lp++){
ctx.lineTo( 80+(lp*step), 380-(unit*(values[lp]-min)) );
}
ctx.stroke();
// Label the axis
ctx.font = 'normal 12pt Arial';
ctx.fillStyle="#000000";
ctx.textAlign = 'right';
ctx.fillText(max.toString(), 70, 30);
ctx.fillText(min.toString(), 70, 380);
</script>
</html>