-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.php
More file actions
184 lines (163 loc) · 5.71 KB
/
Copy pathanalytics.php
File metadata and controls
184 lines (163 loc) · 5.71 KB
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
<?php
require_once 'config.php';
require_login();
$user_id = $_SESSION['user_id'];
// Get basic stats
$stmt = $pdo->prepare("SELECT COUNT(*) as total_habits FROM habits WHERE user_id = ?");
$stmt->execute([$user_id]);
$total_habits = $stmt->fetch()['total_habits'];
$today = date('Y-m-d');
$stmt = $pdo->prepare("
SELECT COUNT(*) as completed_count
FROM habit_progress hp
JOIN habits h ON hp.habit_id = h.id
WHERE h.user_id = ? AND hp.completed = 1 AND hp.progress_date = ?
");
$stmt->execute([$user_id, $today]);
$today_completed = $stmt->fetch()['completed_count'];
// Calculate success rate (last 30 days)
$thirty_days_ago = date('Y-m-d', strtotime('-30 days'));
$stmt = $pdo->prepare("
SELECT
COUNT(*) as total_days,
SUM(CASE WHEN hp.completed = 1 THEN 1 ELSE 0 END) as completed_days
FROM habits h
LEFT JOIN habit_progress hp ON h.id = hp.habit_id AND hp.progress_date BETWEEN ? AND ?
WHERE h.user_id = ?
");
$stmt->execute([$thirty_days_ago, $today, $user_id]);
$progress_data = $stmt->fetch();
$total_days = $progress_data['total_days'];
$completed_days = $progress_data['completed_days'];
$success_rate = $total_days > 0 ? round(($completed_days / $total_days) * 100) : 0;
// Get weekly progress data (last 7 days)
$weekly_data = [];
$week_days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
for ($i = 6; $i >= 0; $i--) {
$date = date('Y-m-d', strtotime("-$i days"));
$day_name = date('D', strtotime($date));
$stmt = $pdo->prepare("
SELECT COUNT(*) as completed_count
FROM habit_progress hp
JOIN habits h ON hp.habit_id = h.id
WHERE h.user_id = ? AND hp.completed = 1 AND hp.progress_date = ?
");
$stmt->execute([$user_id, $date]);
$completed = $stmt->fetch()['completed_count'];
$weekly_data[] = $completed;
}
// Get habit distribution by category
$stmt = $pdo->prepare("
SELECT category, COUNT(*) as count
FROM habits
WHERE user_id = ?
GROUP BY category
");
$stmt->execute([$user_id]);
$category_data = $stmt->fetchAll();
// Prepare data for charts
$category_counts = [
'health' => 0,
'productivity' => 0,
'learning' => 0,
'fitness' => 0,
'others' => 0
];
$category_labels = [];
$category_values = [];
$category_colors = ['#3b82f6', '#10b981', '#8b5cf6', '#f59e0b', '#6b7280'];
foreach ($category_data as $category) {
$category_counts[$category['category']] = $category['count'];
}
foreach ($category_counts as $category => $count) {
$category_labels[] = ucfirst($category);
$category_values[] = $count;
}
require_once 'header.php';
?>
<div class="max-w-6xl mx-auto">
<h1 class="text-3xl font-bold text-gray-800 mb-8">Analytics</h1>
<!-- Stats Grid -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
<div class="bg-white p-6 rounded-lg shadow text-center">
<div class="text-3xl font-bold text-blue-500 mb-2"><?php echo $total_habits; ?></div>
<div class="text-gray-600">Total Habits</div>
</div>
<div class="bg-white p-6 rounded-lg shadow text-center">
<div class="text-3xl font-bold text-green-500 mb-2"><?php echo $today_completed; ?></div>
<div class="text-gray-600">Completed Today</div>
</div>
<div class="bg-white p-6 rounded-lg shadow text-center">
<div class="text-3xl font-bold text-purple-500 mb-2"><?php echo $success_rate; ?>%</div>
<div class="text-gray-600">Success Rate (30 days)</div>
</div>
</div>
<!-- Charts -->
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
<div class="bg-white p-6 rounded-lg shadow">
<h2 class="text-xl font-semibold mb-4">Weekly Progress</h2>
<div class="h-64"> <!-- Fixed height container -->
<canvas id="weeklyChart" height="256"></canvas>
</div>
</div>
<div class="bg-white p-6 rounded-lg shadow">
<h2 class="text-xl font-semibold mb-4">Habit Distribution</h2>
<div class="h-64"> <!-- Fixed height container -->
<canvas id="habitChart" height="256"></canvas>
</div>
</div>
</div>
</div>
<script>
// Weekly progress chart
const weeklyCtx = document.getElementById('weeklyChart').getContext('2d');
new Chart(weeklyCtx, {
type: 'bar',
data: {
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
datasets: [{
label: 'Completed Habits',
data: <?php echo json_encode($weekly_data); ?>,
backgroundColor: '#3b82f6'
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
ticks: {
stepSize: 1
}
}
}
}
});
// Habit distribution chart
const habitCtx = document.getElementById('habitChart').getContext('2d');
new Chart(habitCtx, {
type: 'doughnut',
data: {
labels: <?php echo json_encode($category_labels); ?>,
datasets: [{
data: <?php echo json_encode($category_values); ?>,
backgroundColor: ['#3b82f6', '#10b981', '#8b5cf6', '#f59e0b', '#6b7280']
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'bottom',
labels: {
boxWidth: 12,
padding: 15
}
}
}
}
});
</script>
<?php require_once 'footer.php'; ?>