-
Notifications
You must be signed in to change notification settings - Fork 3
/
statistik.php
170 lines (163 loc) · 7.35 KB
/
statistik.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
<?php
require_once("todo-core.php");
$prefix = "";
require_once("queries/db.php");
function printPeriodicStat($periodName, $maxHeight, $valCount,
$checkedDate='completionDate', $groupName=null)
{
// $start = microtime(true);
global $db;
if ($groupName == null) {
$groupName = $periodName;
}
$ChartWidth = 800;
if ($groupName === 'YEAR')
{
$sql = "SELECT COALESCE(YEAR(t.".$checkedDate."), 'not finished'), ".
"COALESCE(YEAR(t.".$checkedDate."), 'not finished'), ".
"COUNT(t.id) FROM `todo` t ".
"WHERE deleted = 0 ".
"GROUP BY YEAR(t.".$checkedDate.") ".
"ORDER BY YEAR(t.".$checkedDate.")";
}
else if ($groupName === 'MONTH')
{
$sql = "SELECT YEAR(a.Date), MONTH(a.Date), COUNT(t.id) ".
"FROM (select curdate() - INTERVAL (DAY(curdate())-15+30*a) DAY as Date ".
"FROM (select 0 as a union all select 1 union all select 2 union all select 3 ".
"union all select 4 union all select 5 union all select 6 union all select 7 ".
"union all select 8 union all select 9 union all select 10 union all select 11 ".
"union all select 12 union all select 13 union all select 14 union all select 15 ".
"union all select 16 union all select 17 union all select 18 union all select 19 ".
"union all select 20 union all select 21 union all select 22 union all select 23 ".
"union all select 24) as x) as a ".
"left join `todo` t ON MONTH(a.Date) = MONTH(DATE(".$checkedDate.")) ".
"AND deleted = 0 ".
"and YEAR(a.Date) = YEAR(DATE(".$checkedDate.")) ".
"GROUP BY YEAR(a.Date), MONTH(a.Date) ".
"ORDER BY YEAR(a.Date), MONTH(a.Date)";
}
else
{
$groupNameExpr = "$groupName(a.Date".
((strcmp($groupName, "WEEK") == 0) ? ", 3" : "")
.")";
// sql script to generate all dates in range taken from
// http://stackoverflow.com/questions/2157282/generate-days-from-date-range
$sql = "SELECT YEAR(a.Date), $groupNameExpr, COUNT(t.id) ".
"FROM (SELECT curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date ".
"FROM (select 0 as a union all select 1 union all select 2 union all select 3 union all ".
"select 4 union all select 5 union all select 6 union all select 7 union all ".
"select 8 union all select 9) as a ".
"CROSS JOIN (SELECT 0 as a union all select 1 union all select 2 union all select 3 union all ".
"select 4 union all select 5 union all select 6 union all select 7 union all ".
"select 8 union all select 9) as b ".
"CROSS JOIN (SELECT 0 as a union all select 1 union all select 2 union all select 3 union all ".
"select 4 union all select 5 union all select 6 union all select 7 union all ".
"select 8 union all select 9) as c ".
") a".
" LEFT JOIN `todo` t ON a.Date = DATE($checkedDate) AND t.deleted=0 ".
"WHERE a.Date BETWEEN (UTC_TIMESTAMP() - INTERVAL $valCount $periodName) ".
"AND (UTC_TIMESTAMP() + INTERVAL 1 DAY) ".
"GROUP BY YEAR(a.Date), $groupNameExpr ".
"ORDER BY YEAR(a.Date), $groupNameExpr";
}
$qResult = dbQueryOrDie($db, $sql);
// $end = microtime(true);
// echo("Query $sql took ".($end-$start)." seconds<br/>");
$item = array();
$bar_row = '';
$value_row = '';
$maxVal = 0;
$minVal = 999999;
$sum = 0;
while ($stuff = $qResult->fetch_array())
{
$item[] = $stuff;
$curValue = (int)$stuff[2];
$maxVal = max($curValue, $maxVal);
$minVal = min($curValue, $minVal);
$sum += $curValue;
}
if (count($item) < $valCount) {
$valCount = count($item);
}
$avg = ((float)$sum) / $valCount;
// if too many items retrieved, delete oldest
if (count($item) > $valCount) {
$item = array_splice($item, count($item)-$valCount, $valCount);
}
$val_scale = (float)$maxHeight/$maxVal;
$width = (($ChartWidth-($valCount*4)) / ($valCount));
foreach($item as $stuff)
{
$height = (int)( $val_scale * $stuff[2] );
$bar_row .= '<td class="stat_row_bar"><div class="'.
(($stuff[2] == 0) ? ' emptystatbar': 'statbar').'" '.
'style="width: '. $width .'px;'.
'height:'.max($height, 12).'px;" >'.$stuff[2].'</div></td>';
$value_row .= '<td class="stat_row_value">'.
'<div class="statvalue" '.
'style="width: '.$width.'px;">'.$stuff[1].'</div></td>';
}
$bar_row .= '<td class="stat_row_characteristics" rowspan="2">'
.TodoLang::_("MAXIMUM").': '.$maxVal.'<br/>'
.TodoLang::_("AVERAGE").': '.sprintf("%.2f", $avg).'<br/>'
.TodoLang::_("MINIMUM").': '.$minVal.'</td>';
?>
<table>
<tr>
<td class="stat_row_header"><?php echo(TodoLang::_("STAT_FINISHED"));?></td>
<?php echo($bar_row); ?>
</tr>
<tr>
<td class="stat_row_header"><?php echo(TodoLang::_("STAT_".$periodName));?></td>
<?php echo($value_row); ?>
</tr>
</table>
<?php
}
?><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo TodoConstants::AppName; ?> - <?php echo TodoLang::_("STATISTICS"); ?></title>
<!-- JQuery & JQuery UI -->
<!--
<script src="jquery/jquery-1.6.2.min.js"></script>
<link href="jquery/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css"/>
<script src="jquery/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="todo.js"></script>
-->
<link rel="stylesheet" type="text/css" href="todo.css" />
</head>
<body>
<? require("../common/navigation.php"); ?>
<div id="todo_content">
<div class="linkblock"><a href="index.php"><?php echo(TodoLang::_("TO_TODO"));?></a></div>
<h1><?php echo(TodoLang::_("STATISTICS")); ?></h1>
<div id="main_content">
<?php
$dates = array("completion", "creation");
foreach ($dates as $dateName)
{
?>
<h2><?php echo(TodoLang::_("STATS_PERIODIC_".strtoupper($dateName))); ?></h2>
<h3><?php echo(TodoLang::_("STATS_DAILY")); ?></h3>
<?php printPeriodicStat('DAY', 100, 30, $dateName.'Date', 'DATE'); ?>
<h3><?php echo(TodoLang::_("STATS_WEEKLY")); ?></h3>
<?php printPeriodicStat('WEEK', 100, 26, $dateName.'Date'); ?>
<h3><?php echo(TodoLang::_("STATS_MONTHLY")); ?></h3>
<?php printPeriodicStat('MONTH', 100, 24, $dateName.'Date'); ?>
<h3><?php echo(TodoLang::_("STATS_YEARLY")); ?></h3>
<?php printPeriodicStat('YEAR', 100, 14, $dateName.'Date'); ?>
<?php
}
?>
<!-- to implement... <h2><?php echo(TodoLang::_("STATS_DUE")); ?></h2> -->
</div>
</div>
</body>
</html>
<?php
$db->close();