-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.php
352 lines (295 loc) · 12.6 KB
/
result.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
require_once './includes/db.php';
require_once './includes/version.php';
// Check if it's a POST request
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$userId = $_POST['user_id'];
$answers = $_POST;
// Calculate score
$score = calculateScore($answers);
// Store results
if (!is_numeric($score)) {
error_log("Score is not numeric: " . print_r($score, true));
// Handle error, e.g., redirect to an error page
exit;
}
// Generate invitation code
$invitationCode = generateInvitationCode($score);
if (!isset($invitationCode) || empty($invitationCode)) {
$invitationCode = '错误:空值'; // Provide a default value
}
// Store results
storeResults($userId, $score, $invitationCode);
// Record start and end times
recordTimes($userId);
}
function calculateScore($answers)
{
$totalQuestions = 0;
$totalScore = 0; // Initialize total score
try {
// Establish database connection
$conn = connectToDatabase(); // 调用 connectToDatabase 函数
// Retrieve questions and their answers from the database once
$stmt = $conn->prepare("SELECT * FROM questions");
$stmt->execute();
$result = $stmt->get_result();
$questions = $result->fetch_all(MYSQLI_ASSOC);
foreach ($questions as $question) {
$totalQuestions++;
// Extract submitted answers for this question
$submittedAnswers = [];
foreach ($answers as $key => $value) {
if (strpos($key, "answer_{$question['id']}") === 0) {
if (is_array($value)) { // 处理多选题的数组形式答案
$submittedAnswers = array_merge($submittedAnswers, $value);
} else {
$submittedAnswers[] = $value;
}
}
}
// Check if the question was answered
if (empty($submittedAnswers)) {
// If no answer was provided, score is 0 for this question
continue;
}
// Normalize the correct answer to match the expected format
$correctAnswer = str_replace(['(', ')'], '', strtolower($question['answer']));
// Convert all elements to strings before applying strtolower
$submittedAnswerStr = implode(
',', array_map(
function ($item) {
return is_string($item) ? strtolower($item) : strtolower((string)$item);
}, $submittedAnswers
)
);
// Ensure $submittedAnswerStr is not an empty string
$submittedAnswerStr = $submittedAnswerStr ?: '';
// Check if the question is a single-choice or multiple-choice
if (stripos($question['answer'], ',') !== false) {
// Multiple-choice question
$scoreForQuestion = 0;
$correctAnswerParts = explode(',', $correctAnswer);
$submittedAnswerParts = explode(',', $submittedAnswerStr);
// Calculate partial score based on the number of correct answers
$numCorrect = count(array_intersect($correctAnswerParts, $submittedAnswerParts));
$numIncorrect = count(array_diff($submittedAnswerParts, $correctAnswerParts));
// Check if the submitted answers include any incorrect choices
if ($numIncorrect > 0) {
$scoreForQuestion = 0; // Score is 0 if any incorrect answer is chosen
} elseif ($numCorrect > 0) {
$scoreForQuestion = SCORE_PARTIAL_MULTIPLE_QUESTION; // Partial score if some correct answers are chosen
}
$totalScore += $scoreForQuestion;
} else {
// Single-choice question
if (stripos($correctAnswer, $submittedAnswerStr) !== false) {
$totalScore += SCORE_CORRECT_QUESTION; // Full score for single-choice
}
}
}
// Close the database connection
closeDatabaseConnection($conn); // 调用 closeDatabaseConnection 函数
} catch (Exception $e) {
// Handle exceptions here
error_log("Error in calculateScore: " . $e->getMessage());
}
// Ensure $totalScore is numeric
$totalScore = (int)$totalScore;
return $totalScore;
}
function storeResults($userId, $score, $invitationCode)
{
// Establish database connection
$conn = connectToDatabase();
if ($invitationCode === null) {
$invitationCode = '无(错误)'; // Provide a default value
}
// Store the result in the database using prepared statement
$stmt = $conn->prepare("INSERT INTO results (user_id, score, invitation_code) VALUES (?, ?, ?)");
$stmt->bind_param("iss", $userId, $score, $invitationCode);
$stmt->execute();
// Close the database connection
closeDatabaseConnection($conn);
}
function recordTimes($userId)
{
// Establish database connection
$conn = connectToDatabase();
// Check if the user_id already exists in the results table
$stmt = $conn->prepare("SELECT COUNT(*) AS count FROM results WHERE user_id = ?");
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$exists = $row['count'] > 0;
// Get the start time from the users table
$stmt = $conn->prepare("SELECT start_time FROM users WHERE id = ?");
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
// Get the start time
$startTime = $user['start_time'];
// Calculate expected end time
$expectedEndTime = date('Y-m-d H:i:s', strtotime($startTime . '+' . EXAM_REMAIN_TIME . ' minutes'));
// Get current server time
$currentServerTime = date('Y-m-d H:i:s');
// Check if user violated time limit
if ($currentServerTime > $expectedEndTime) {
// Remove result record
$stmt = $conn->prepare("DELETE FROM results WHERE user_id = ?");
$stmt->bind_param("i", $userId);
$stmt->execute();
// Remove user record
$stmt = $conn->prepare("DELETE FROM users WHERE id = ?");
$stmt->bind_param("i", $userId);
$stmt->execute();
// User violated time limit
$timeCheatDetected = true;
} else {
if ($exists) {
// Update the end time for existing user
$stmt = $conn->prepare("UPDATE results SET end_time = NOW() WHERE user_id = ?");
$stmt->bind_param("i", $userId);
$stmt->execute();
} else {
// Insert a new record for new user
$stmt = $conn->prepare("INSERT INTO results (user_id, end_time) VALUES (?, NOW())");
$stmt->bind_param("i", $userId);
$stmt->execute();
}
}
// Close the database connection
closeDatabaseConnection($conn);
}
// Extracts the key from the JSON response
function extractKey($data)
{
try {
$dataDict = json_decode($data, true);
$keyValue = $dataDict['data']['attributes']['key'];
return $keyValue;
} catch (Exception $e) {
error_log("Error extracting key: " . $e->getMessage());
return null;
}
}
// Generates a random string of specified length containing letters and digits
function generateRandomString($length = 8)
{
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$charsArray = [];
for ($i = 0; $i < $length; $i++) {
$charsArray[] = $characters[random_int(0, strlen($characters) - 1)];
}
$randomString = implode('', $charsArray);
$generatedString = CODE_TYPE . "@" . $randomString;
return $generatedString;
}
// Sends a request to generate a door key
function getDoorKey($key)
{
$url = 'https://' . API_SITE . '/api/fof/doorkeys';
$headers = [
'Content-Type: application/json; charset=UTF-8',
'Authorization: Token ' . API_X_CSRF_TOKEN,
'User-Agent: b18-exam/' . VERSION . ' b18-codeget-php/1.0.0',
];
$payload = [
"data" => [
"type" => "doorkeys",
"attributes" => [
"key" => $key,
"groupId" => GROUP_ID,
"maxUses" => MAX_USES,
"activates" => ACTIVATES
]
]
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true); // 使用 true 而不是 1 为了增加可读性
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$error = curl_error($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($error) {
error_log("cURL 错误:" . $error);
return null;
}
if ($statusCode !== 201) {
error_log("API 返回异常的 HTTP 状态码:$statusCode");
return null;
}
$responseData = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
error_log("收到了未知的 JSON 回应:" . $response);
return null;
}
if (!isset($responseData['data']['attributes']['key'])) {
error_log("Unexpected response structure: " . print_r($responseData, true));
return null;
}
return $responseData['data']['attributes']['key'];
}
// Main function to generate key
function generateKey()
{
$key = [];
$randomNum = generateRandomString();
$result = getDoorKey($randomNum);
if ($result !== null) {
$key[] = $result;
}
return $key;
}
function generateInvitationCode($score)
{
// Set the score threshold
$threshold = SCORE_THRESHOLD;
// Generate an invitation code only if the score is above the threshold
if ($score >= $threshold) {
$code = generateKey();
// Ensure the generated code is a string
$code = is_array($code) ? implode('', $code) : $code;
} else {
$code = '无(未达到条件)';
}
return $code;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>结果 - 十八桥社区论坛入站测试系统</title>
<link rel="stylesheet" href="./vendor/twbs/bootstrap/dist/css/bootstrap.min.css">
</head>
<?php require './views/nav.php'; ?>
<div class="card">
<div class="card-header">
测试结果
</div>
<div class="card-body">
<?php if (isset($timeCheatDetected) && $timeCheatDetected) : ?>
<div class="alert alert-danger" role="alert">作弊检测:你违反了测试的时间限制。你的信息已被删除,请重新开始测试。</div>
<h5 class="card-title">测试失败。</h5>
<p class="card-subtitle">系统检测到你在测试过程中有作弊行为。</p>
<p class="card-text">如果对此结果有任何问题,请截屏此页面然后向<a
href="mailto:[email protected]">管理邮箱</a>发送电子邮件。</p>
<?php else: ?>
<h5 class="card-title">测试已完成。</h5>
<p class="card-subtitle">你的分数是:<strong><?php echo htmlspecialchars($score); ?></strong></p>
<p class="card-subtitle">你的邀请码是:<strong><?php echo htmlspecialchars(is_string($invitationCode) ? $invitationCode : '错误:返回内容的类型不是字符串。这有可能是邀请码 API 出现了错误,请截屏并联系管理邮箱获取邀请码。', ENT_QUOTES, 'UTF-8'); ?></strong></p>
<p class="card-text">如果对此结果有任何问题,请截屏此页面然后向<a
href="mailto:[email protected]">管理邮箱</a>发送电子邮件(被测试者 ID:<strong><?php echo htmlspecialchars(isset($userId) ? $userId : '', ENT_QUOTES, 'UTF-8'); ?></strong>)。</p>
<a href="https://www.bridge18.us.kg/" class="btn btn-primary" data-toggle="tooltip"
data-placement="top" title="不要忘记写下(或安全地保存)邀请码,它只会出现一次!">去注册</a>
<?php endif; ?>
</div>
</div>
<?php require './views/footer.php'; ?>