-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.php
More file actions
131 lines (110 loc) · 5.9 KB
/
code.php
File metadata and controls
131 lines (110 loc) · 5.9 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
<?php
// error_reporting(E_ALL);
// ini_set('display_errors', 1);
session_start();
// 引入数据库连接
require_once 'db.php';
// 检查用户是否已登录
if (!isset($_SESSION['user_email'])) {
header("Location: regLog.html");
exit();
}
if (isset($_GET['code'])) {
$invite_code = $_GET['code'];
// 查询code是否存在且未使用
$query = "SELECT * FROM code WHERE code = :code AND is_used = 0";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':code', $invite_code, PDO::PARAM_STR);
$stmt->execute();
$code_data = $stmt->fetch(PDO::FETCH_ASSOC);
if ($code_data) {
$class_id = $code_data['class_id'];
// 获取当前用户ID
$user_id = $_SESSION['user_id'];
// 获取当前班级的class_teacher和class_headteacher字段
$query = "SELECT class_teacher, class_headteacher FROM classes WHERE class_id = :class_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':class_id', $class_id, PDO::PARAM_INT);
$stmt->execute();
$class_data = $stmt->fetch(PDO::FETCH_ASSOC);
// 处理可能为空的class_teacher字段
$class_teacher = json_decode($class_data['class_teacher'], true);
if (!is_array($class_teacher)) {
$class_teacher = []; // 如果class_teacher为空或无效,则初始化为空数组
}
$class_headteacher = $class_data['class_headteacher'];
// 判断用户是否是班主任
if ($user_id == $class_headteacher) {
echo "您是该班级的班主任,无需加入班级!";
echo '<script>setTimeout(function(){window.location.href="panel.php";}, 2000);</script>';
echo '<p><a href="regLog.html">没有自动返回点我</a></p>';
} else {
// 判断用户是否已经是该班级的教师
if (in_array($user_id, $class_teacher)) {
echo "您已经是该班级的教师!";
echo '<script>setTimeout(function(){window.location.href="regLog.html";}, 2000);</script>';
echo '<p><a href="panel.php">没有自动返回点我</a></p>';
} else {
// 如果用户没有加入班级,展示确认加入的表单
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['confirm_join'])) {
// 获取用户的班级列表
$query = "SELECT classes FROM users WHERE id = :user_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();
$user_data = $stmt->fetch(PDO::FETCH_ASSOC);
// 处理可能为空的classes字段
$user_classes = json_decode($user_data['classes'], true);
if (!is_array($user_classes)) {
$user_classes = []; // 如果classes为空或无效,则初始化为空数组
}
// 判断用户是否已经加入该班级
if (!in_array($class_id, $user_classes)) {
// 将班级ID添加到用户的班级列表
$user_classes[] = $class_id;
// 更新用户的classes字段
$query = "UPDATE users SET classes = :classes WHERE id = :user_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':classes', json_encode($user_classes), PDO::PARAM_STR);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();
// 将班级的教师ID添加到班级教师列表
$class_teacher[] = $user_id;
// 更新班级的class_teacher字段
$query = "UPDATE classes SET class_teacher = :class_teacher WHERE class_id = :class_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':class_teacher', json_encode($class_teacher), PDO::PARAM_STR);
$stmt->bindParam(':class_id', $class_id, PDO::PARAM_INT);
$stmt->execute();
// 更新链接为已使用
$query = "UPDATE code SET is_used = 1 WHERE code = :code";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':code', $invite_code, PDO::PARAM_STR);
$stmt->execute();
echo "您已成功加入该班级!";
echo '<script>setTimeout(function(){window.location.href="panel.php";}, 2000);</script>';
echo '<p><a href="regLog.html">没有自动返回点我</a></p>';
exit();
} else {
echo "您已是该班级的成员!";
echo '<script>setTimeout(function(){window.location.href="panel.php";}, 2000);</script>';
echo '<p><a href="regLog.html">没有自动返回点我</a></p>';
exit();
}
} else {
// 显示确认加入班级的表单
echo '<form method="POST">
<p>您确认要加入该班级吗?</p>
<button type="submit" name="confirm_join">确认加入</button>
</form>';
}
}
}
} else {
echo "无效或已使用的邀请链接!";
echo '<script>setTimeout(function(){window.location.href="panel.php";}, 2000);</script>';
echo '<p><a href="regLog.html">没有自动返回点我</a></p>';
exit();
}
}
?>