Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
60 lines (53 loc) · 2.23 KB
/
Copy pathindex.php
File metadata and controls
60 lines (53 loc) · 2.23 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
<?php
/**
* PHP教学应用 - 主入口文件
* 启动方式:php -S localhost:8080
*/
require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/core/functions.php';
require_once __DIR__ . '/data/courses.php';
require_once __DIR__ . '/data/quizzes.php';
$page = getCurrentPage();
$sessionId = getSessionId();
// 处理AJAX请求(API)
if (isset($_GET['api'])) {
header('Content-Type: application/json; charset=utf-8');
$api = $_GET['api'];
if ($api === 'progress' && $_SERVER['REQUEST_METHOD'] === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);
updateProgress($sessionId, (int)($input['chapter'] ?? 0), (int)($input['lesson'] ?? 0));
echo json_encode(['ok' => true]);
exit;
}
if ($api === 'quiz' && $_SERVER['REQUEST_METHOD'] === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);
$answers = $input['answers'] ?? [];
$result = gradeQuiz($input['quiz'] ?? 1, $answers);
saveQuizResult($sessionId, (int)($input['quiz'] ?? 1), $result['score'], $result['total'], $answers);
echo json_encode($result);
exit;
}
if ($api === 'run' && $_SERVER['REQUEST_METHOD'] === 'POST') {
require_once __DIR__ . '/core/runner.php';
$input = json_decode(file_get_contents('php://input'), true);
$result = runStudentCode((string)($input['code'] ?? ''));
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
echo json_encode(['ok' => false, 'error' => 'unknown api']);
exit;
}
// 页面渲染
include __DIR__ . '/templates/header.php';
echo '<main class="container">';
switch ($page) {
case 'home': include __DIR__ . '/templates/home.php'; break;
case 'course': include __DIR__ . '/templates/course.php'; break;
case 'lesson': include __DIR__ . '/templates/lesson.php'; break;
case 'exercise': include __DIR__ . '/templates/exercise.php'; break;
case 'quiz': include __DIR__ . '/templates/quiz.php'; break;
case 'progress': include __DIR__ . '/templates/progress.php'; break;
default: include __DIR__ . '/templates/home.php';
}
echo '</main>';
include __DIR__ . '/templates/footer.php';