-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxp_debug.php
More file actions
257 lines (227 loc) Β· 8.53 KB
/
Copy pathxp_debug.php
File metadata and controls
257 lines (227 loc) Β· 8.53 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
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
<?php
// Debug script for XP management system
session_start();
include 'config.php';
// Check if user is logged in and is admin/teacher
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
if (!in_array($_SESSION['role'], ['admin', 'teacher'])) {
header("Location: dashboard.php");
exit();
}
include 'header.php';
?>
<style>
.debug-container {
max-width: 1000px;
margin: 20px auto;
padding: 20px;
background: white;
border-radius: 10px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}
.debug-section {
margin-bottom: 30px;
padding: 20px;
border: 1px solid #e9ecef;
border-radius: 8px;
}
.debug-section h3 {
margin-top: 0;
color: #495057;
border-bottom: 2px solid #007bff;
padding-bottom: 10px;
}
.status-good {
color: #28a745;
font-weight: bold;
}
.status-bad {
color: #dc3545;
font-weight: bold;
}
.test-result {
padding: 10px;
margin: 10px 0;
border-radius: 4px;
font-family: monospace;
}
.test-success {
background: #d4edda;
border: 1px solid #c3e6cb;
color: #155724;
}
.test-error {
background: #f8d7da;
border: 1px solid #f5c6cb;
color: #721c24;
}
.test-btn {
background: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
margin: 5px;
}
.test-btn:hover {
background: #0056b3;
}
.table-debug {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
.table-debug th,
.table-debug td {
padding: 8px;
text-align: left;
border: 1px solid #ddd;
}
.table-debug th {
background: #f8f9fa;
font-weight: 600;
}
</style>
<main>
<div class="debug-container">
<h1>π§ XP Management Debug Tool</h1>
<p>Use this tool to diagnose issues with the XP spending system.</p>
<!-- Database Tables Check -->
<div class="debug-section">
<h3>π Database Tables Check</h3>
<?php
$tables = ['xp_spending_items', 'xp_spending_requests', 'notifications'];
foreach ($tables as $table) {
$result = $conn->query("SHOW TABLES LIKE '$table'");
$exists = $result && $result->num_rows > 0;
echo "<div class='test-result " . ($exists ? "test-success" : "test-error") . "'>";
echo "Table '$table': " . ($exists ? "EXISTS β" : "MISSING β");
echo "</div>";
}
?>
</div>
<!-- Session Check -->
<div class="debug-section">
<h3>π Session Check</h3>
<div class="test-result test-success">
User ID: <?php echo $_SESSION['user_id']; ?><br>
Username: <?php echo htmlspecialchars($_SESSION['username']); ?><br>
Role: <?php echo $_SESSION['role']; ?><br>
Session Status: ACTIVE β
</div>
</div>
<!-- XP Items Check -->
<div class="debug-section">
<h3>π XP Items Check</h3>
<?php
$result = $conn->query("SELECT COUNT(*) as count FROM xp_spending_items");
if ($result) {
$count = $result->fetch_assoc()['count'];
echo "<div class='test-result test-success'>Total XP items: $count</div>";
if ($count > 0) {
$items_result = $conn->query("SELECT id, title, xp_cost, category, is_active FROM xp_spending_items ORDER BY xp_cost ASC LIMIT 10");
echo "<table class='table-debug'>";
echo "<tr><th>ID</th><th>Title</th><th>XP Cost</th><th>Category</th><th>Active</th></tr>";
while ($item = $items_result->fetch_assoc()) {
echo "<tr>";
echo "<td>{$item['id']}</td>";
echo "<td>{$item['title']}</td>";
echo "<td>{$item['xp_cost']}</td>";
echo "<td>{$item['category']}</td>";
echo "<td>" . ($item['is_active'] ? 'Yes' : 'No') . "</td>";
echo "</tr>";
}
echo "</table>";
}
} else {
echo "<div class='test-result test-error'>Cannot query XP items table</div>";
}
?>
</div>
<!-- Pending Requests Check -->
<div class="debug-section">
<h3>π Pending Requests Check</h3>
<?php
$result = $conn->query("SELECT COUNT(*) as count FROM xp_spending_requests WHERE status = 'pending'");
if ($result) {
$count = $result->fetch_assoc()['count'];
echo "<div class='test-result test-success'>Pending requests: $count</div>";
if ($count > 0) {
$req_result = $conn->query("
SELECT r.id, u.first_name, u.last_name, i.title, r.xp_cost, r.requested_at
FROM xp_spending_requests r
JOIN users u ON r.student_id = u.id
JOIN xp_spending_items i ON r.item_id = i.id
WHERE r.status = 'pending'
ORDER BY r.requested_at DESC LIMIT 5
");
echo "<table class='table-debug'>";
echo "<tr><th>Student</th><th>Item</th><th>XP Cost</th><th>Requested</th></tr>";
while ($req = $req_result->fetch_assoc()) {
echo "<tr>";
echo "<td>{$req['first_name']} {$req['last_name']}</td>";
echo "<td>{$req['title']}</td>";
echo "<td>{$req['xp_cost']}</td>";
echo "<td>" . date('M j, Y g:i A', strtotime($req['requested_at'])) . "</td>";
echo "</tr>";
}
echo "</table>";
}
} else {
echo "<div class='test-result test-error'>Cannot query pending requests</div>";
}
?>
</div>
<!-- AJAX Test -->
<div class="debug-section">
<h3>π AJAX Test</h3>
<p>Test the AJAX endpoints used by the XP management system:</p>
<button class="test-btn" onclick="testAjax('get_pending_requests')">Test Pending Requests</button>
<button class="test-btn" onclick="testAjax('get_xp_items')">Test XP Items</button>
<button class="test-btn" onclick="testAjax('get_request_history')">Test Request History</button>
<div id="ajax-result" style="margin-top: 15px; padding: 10px; border: 1px solid #ddd; border-radius: 4px; background: #f8f9fa; display: none;"></div>
</div>
<!-- Setup Instructions -->
<div class="debug-section">
<h3>π Setup Instructions</h3>
<p>If you're seeing errors, make sure to run the XP setup script:</p>
<ol>
<li>Access your database admin panel (phpMyAdmin, etc.)</li>
<li>Run the queries from <code>xp_spending_setup.php</code></li>
<li>Or access <code>xp_spending_setup.php</code> directly in your browser</li>
<li>Check that the tables <code>xp_spending_items</code> and <code>xp_spending_requests</code> exist</li>
</ol>
</div>
</div>
</main>
<script>
function testAjax(action) {
const resultDiv = document.getElementById('ajax-result');
resultDiv.style.display = 'block';
resultDiv.innerHTML = 'Testing ' + action + '...';
fetch('xp_management_handler.php', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'action=' + action
})
.then(response => {
if (!response.ok) {
throw new Error('HTTP ' + response.status + ': ' + response.statusText);
}
return response.json();
})
.then(data => {
resultDiv.innerHTML = '<pre>' + JSON.stringify(data, null, 2) + '</pre>';
resultDiv.className = data.success ? 'test-result test-success' : 'test-result test-error';
})
.catch(error => {
resultDiv.innerHTML = 'AJAX Error: ' + error.message;
resultDiv.className = 'test-result test-error';
});
}
</script>
<?php include 'footer.php'; ?>