Repository navigation
Expand file tree
/
Copy pathprogress_api.php
More file actions
242 lines (203 loc) · 9.77 KB
/
Copy pathprogress_api.php
File metadata and controls
242 lines (203 loc) · 9.77 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
<?php
session_start();
header('Content-Type: application/json');
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Check if user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
http_response_code(401);
echo json_encode(['error' => 'Not logged in']);
exit();
}
// CHECK: Make sure user_id is in session
if (!isset($_SESSION['user_id'])) {
echo json_encode(['error' => 'User ID not found in session. Make sure your login system sets $_SESSION[\'user_id\']']);
exit();
}
// Database connection - UPDATE THESE WITH YOUR ACTUAL DATABASE CREDENTIALS
$servername = "localhost";
$username = "ulyag0urc2wft"; // CHANGE THIS
$password = "Userulya2025$"; // CHANGE THIS
$dbname = "dba23u1vgf2je4"; // CHANGE THIS
try {
$pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
error_log("Database connection failed: " . $e->getMessage());
http_response_code(500);
echo json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]);
exit();
}
$action = $_POST['action'] ?? $_GET['action'] ?? '';
$user_id = $_SESSION['user_id'];
// Debug logging
error_log("Progress API called - Action: $action, User ID: $user_id");
switch ($action) {
case 'get_progress':
$section_id = $_GET['section_id'] ?? 0;
try {
if ($section_id) {
// Get progress for specific section
$stmt = $pdo->prepare("SELECT * FROM user_progress WHERE user_id = ? AND section_id = ?");
$stmt->execute([$user_id, $section_id]);
$progress = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$progress) {
// Create new progress record
$stmt = $pdo->prepare("INSERT INTO user_progress (user_id, section_id, current_question, correct_answers, wrong_answers) VALUES (?, ?, 1, 0, 0)");
$stmt->execute([$user_id, $section_id]);
$progress = [
'user_id' => $user_id,
'section_id' => $section_id,
'current_question' => 1,
'correct_answers' => 0,
'wrong_answers' => 0,
'completed' => false
];
}
} else {
// Get progress for all sections
$stmt = $pdo->prepare("SELECT * FROM user_progress WHERE user_id = ?");
$stmt->execute([$user_id]);
$progress = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
echo json_encode(['success' => true, 'progress' => $progress]);
} catch (Exception $e) {
error_log("Error in get_progress: " . $e->getMessage());
echo json_encode(['error' => 'Failed to get progress: ' . $e->getMessage()]);
}
break;
case 'get_detailed_results':
$section_id = $_GET['section_id'] ?? 0;
if (!$section_id) {
echo json_encode(['error' => 'Section ID required']);
break;
}
try {
// Get detailed question results for the section
$stmt = $pdo->prepare("
SELECT question_number, user_answer, is_correct, attempt_time
FROM question_attempts
WHERE user_id = ? AND section_id = ?
ORDER BY question_number DESC, attempt_time DESC
");
$stmt->execute([$user_id, $section_id]);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Remove duplicates (keep latest attempt for each question)
$unique_results = [];
$seen_questions = [];
foreach ($results as $result) {
if (!in_array($result['question_number'], $seen_questions)) {
$unique_results[] = $result;
$seen_questions[] = $result['question_number'];
}
}
// Sort by question number
usort($unique_results, function($a, $b) {
return $a['question_number'] - $b['question_number'];
});
echo json_encode(['success' => true, 'results' => $unique_results]);
} catch (Exception $e) {
error_log("Error in get_detailed_results: " . $e->getMessage());
echo json_encode(['error' => 'Failed to get detailed results: ' . $e->getMessage()]);
}
break;
case 'update_progress':
$section_id = $_POST['section_id'] ?? 0;
$current_question = $_POST['current_question'] ?? 1;
$correct_answers = $_POST['correct_answers'] ?? 0;
$wrong_answers = $_POST['wrong_answers'] ?? 0;
$completed = $_POST['completed'] === 'true' ? 1 : 0;
if (!$section_id) {
echo json_encode(['error' => 'Section ID required']);
break;
}
try {
// Update or insert progress
$stmt = $pdo->prepare("
INSERT INTO user_progress (user_id, section_id, current_question, correct_answers, wrong_answers, completed)
VALUES (?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
current_question = VALUES(current_question),
correct_answers = VALUES(correct_answers),
wrong_answers = VALUES(wrong_answers),
completed = VALUES(completed),
last_updated = CURRENT_TIMESTAMP
");
$result = $stmt->execute([$user_id, $section_id, $current_question, $correct_answers, $wrong_answers, $completed]);
error_log("Progress updated - Section: $section_id, Question: $current_question, Correct: $correct_answers, Wrong: $wrong_answers, Completed: $completed");
echo json_encode(['success' => $result]);
} catch (Exception $e) {
error_log("Error in update_progress: " . $e->getMessage());
echo json_encode(['error' => 'Failed to update progress: ' . $e->getMessage()]);
}
break;
case 'save_answer':
$section_id = $_POST['section_id'] ?? 0;
$question_number = $_POST['question_number'] ?? 0;
$user_answer = $_POST['user_answer'] ?? '';
$is_correct = $_POST['is_correct'] === 'true' ? 1 : 0;
$question_text = $_POST['question_text'] ?? '';
$correct_answer = $_POST['correct_answer'] ?? '';
if (!$section_id || !$question_number) {
echo json_encode(['error' => 'Section ID and Question Number required']);
break;
}
try {
// Delete any previous attempts for this question
$stmt = $pdo->prepare("DELETE FROM question_attempts WHERE user_id = ? AND section_id = ? AND question_number = ?");
$stmt->execute([$user_id, $section_id, $question_number]);
// Save new attempt with question details
$stmt = $pdo->prepare("
INSERT INTO question_attempts (user_id, section_id, question_number, user_answer, is_correct, question_text, correct_answer)
VALUES (?, ?, ?, ?, ?, ?, ?)
");
$result = $stmt->execute([$user_id, $section_id, $question_number, $user_answer, $is_correct, $question_text, $correct_answer]);
echo json_encode(['success' => $result]);
} catch (Exception $e) {
error_log("Error in save_answer: " . $e->getMessage());
// Don't fail the whole process if answer logging fails
echo json_encode(['success' => true, 'warning' => 'Answer not logged: ' . $e->getMessage()]);
}
break;
case 'reset_section':
$section_id = $_POST['section_id'] ?? 0;
if (!$section_id) {
echo json_encode(['error' => 'Section ID required']);
break;
}
try {
// Reset progress for specific section
$stmt = $pdo->prepare("
UPDATE user_progress
SET current_question = 1, correct_answers = 0, wrong_answers = 0, completed = FALSE
WHERE user_id = ? AND section_id = ?
");
$stmt->execute([$user_id, $section_id]);
// Also delete question attempts for this section
$stmt = $pdo->prepare("DELETE FROM question_attempts WHERE user_id = ? AND section_id = ?");
$stmt->execute([$user_id, $section_id]);
error_log("Section reset - Section: $section_id");
echo json_encode(['success' => true]);
} catch (Exception $e) {
error_log("Error in reset_section: " . $e->getMessage());
echo json_encode(['error' => 'Failed to reset section: ' . $e->getMessage()]);
}
break;
case 'test_connection':
// Simple test endpoint
echo json_encode([
'success' => true,
'message' => 'API is working',
'user_id' => $user_id,
'session_data' => [
'logged_in' => $_SESSION['logged_in'] ?? 'not set',
'name' => $_SESSION['name'] ?? 'not set'
]
]);
break;
default:
echo json_encode(['error' => 'Invalid action: ' . $action]);
break;
}
?>