-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_teacher.php
205 lines (178 loc) · 8.81 KB
/
add_teacher.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
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
require('db.php');
// Collect data from the form
$teacher_username = $_POST['teacher_username'];
$department = $_POST['department'];
$is_head = isset($_POST['is_head']) ? 1 : 0;
$assigned_courses = $_POST['assigned_courses']; // Array of assigned course IDs
// Validate the data (add more validation as needed)
if (empty($teacher_username) || empty($department) || empty($assigned_courses)) {
echo "Teacher username, department, and assigned courses are required fields.";
exit();
}
// Fetch teacher details from the teachers table
$fetch_teacher_sql = "SELECT teacher_id, teacher_name FROM teachers WHERE username = ?";
$fetch_teacher_stmt = $conn->prepare($fetch_teacher_sql);
$fetch_teacher_stmt->bind_param("s", $teacher_username);
$fetch_teacher_stmt->execute();
$fetch_teacher_stmt->bind_result($teacher_id, $teacher_name);
$fetch_teacher_stmt->fetch();
$fetch_teacher_stmt->close();
if (empty($teacher_id)) {
echo "Teacher with the provided username does not exist.";
echo '<script type="text/javascript">
setTimeout(function(){
window.location = "create_teacher.php";
}, 4000);
</script>';
exit();
}
// Check if the assigned courses are already assigned to other instructors
$check_course_assignment_sql = "SELECT c.course_name FROM courses_instructors ci
JOIN courses c ON ci.course_id = c.course_id
WHERE ci.course_id = ?";
$check_course_assignment_stmt = $conn->prepare($check_course_assignment_sql);
foreach ($assigned_courses as $course_id) {
$check_course_assignment_stmt->bind_param("i", $course_id);
$check_course_assignment_stmt->execute();
$check_course_assignment_stmt->bind_result($course_name);
$check_course_assignment_stmt->fetch();
if (!empty($course_name)) {
echo "The course '$course_name' is already assigned to another instructor.";
echo '<script type="text/javascript">
setTimeout(function(){
window.location = "create_teacher.php";
}, 4000);
</script>';
exit();
}
}
$check_course_assignment_stmt->close();
// Check if the entered department matches the department of the selected courses
$course_department_check_sql = "SELECT department FROM courses WHERE course_id = ?";
$course_department_check_stmt = $conn->prepare($course_department_check_sql);
foreach ($assigned_courses as $course_id) {
$course_department_check_stmt->bind_param("i", $course_id);
$course_department_check_stmt->execute();
$course_department_check_stmt->bind_result($course_department);
$course_department_check_stmt->fetch();
if ($course_department !== $department) {
echo "The department entered does not match the department of the selected course.";
echo '<script type="text/javascript">
setTimeout(function(){
window.location = "create_teacher.php";
}, 4000);
</script>';
exit();
}
}
$course_department_check_stmt->close();
// Check if there is already a head for the specified department
if ($is_head) {
$check_head_sql = "SELECT teacher_id FROM instructors WHERE department = ? AND is_head = 1 LIMIT 1";
$check_head_stmt = $conn->prepare($check_head_sql);
$check_head_stmt->bind_param("s", $department);
$check_head_stmt->execute();
$check_head_stmt->store_result();
if ($check_head_stmt->num_rows > 0) {
echo "A head already exists for this department.";
echo '<script type="text/javascript">
setTimeout(function(){
window.location = "create_teacher.php";
}, 4000);
</script>';
exit();
}
}
// Insert the new teacher into the instructors table
// $insert_teacher_sql = "INSERT INTO instructors (teacher_id, teacher_name, department, is_head) VALUES (?, ?, ?, ?)";
// $insert_teacher_stmt = $conn->prepare($insert_teacher_sql);
// $insert_teacher_stmt->bind_param("issi", $teacher_id, $teacher_name, $department, $is_head);
// if ($insert_teacher_stmt->execute()) {
// // Assign courses to the teacher by inserting into the courses_instructors table
// if (!empty($assigned_courses)) {
// $assign_courses_sql = "INSERT INTO courses_instructors (teacher_id, course_id) VALUES (?, ?)";
// $assign_courses_stmt = $conn->prepare($assign_courses_sql);
// foreach ($assigned_courses as $course_id) {
// // Bind the instructor_id and course_id for the assignment
// $assign_courses_stmt->bind_param("ii", $teacher_id, $course_id);
// $assign_courses_stmt->execute();
// }
// $assign_courses_stmt->close();
// }
// echo "Teacher and assigned courses added successfully!";
// echo '<script type="text/javascript">
// setTimeout(function(){
// window.location = "create_teacher.php";
// }, 3000);
// </script>';
// } else {
// echo "Error: " . $insert_teacher_stmt->error;
// }
// Check if the teacher is already in the instructors table
$check_teacher_sql = "SELECT teacher_id FROM instructors WHERE teacher_id = ?";
$check_teacher_stmt = $conn->prepare($check_teacher_sql);
$check_teacher_stmt->bind_param("i", $teacher_id);
$check_teacher_stmt->execute();
$check_teacher_stmt->store_result();
if ($check_teacher_stmt->num_rows === 0) {
// Teacher is not in the instructors table, so add them
$insert_teacher_sql = "INSERT INTO instructors (teacher_id, teacher_name, department, is_head) VALUES (?, ?, ?, ?)";
$insert_teacher_stmt = $conn->prepare($insert_teacher_sql);
$insert_teacher_stmt->bind_param("issi", $teacher_id, $teacher_name, $department, $is_head);
$insert_teacher_stmt->execute();
$insert_teacher_stmt->close();
} else {
// Teacher is already in the instructors table, so check if the assigned department matches
$check_assigned_department_sql = "SELECT department FROM instructors WHERE teacher_id = ?";
$check_assigned_department_stmt = $conn->prepare($check_assigned_department_sql);
$check_assigned_department_stmt->bind_param("i", $teacher_id);
$check_assigned_department_stmt->execute();
$check_assigned_department_stmt->bind_result($assigned_department);
$check_assigned_department_stmt->fetch();
$check_assigned_department_stmt->close();
if ($assigned_department === $department) {
// Department matches, so allow course assignment
// You can insert the assigned courses into courses_instructors here
} else {
echo "The department assigned to the teacher previously does not match the current department.";
echo '<script type="text/javascript">
setTimeout(function(){
window.location = "create_teacher.php";
}, 4000);
</script>';
exit();
}
}
$check_teacher_stmt->close();
// Assign courses to the teacher by inserting into the courses_instructors table
if (!empty($assigned_courses)) {
$assign_courses_sql = "INSERT INTO courses_instructors (teacher_id, course_id) VALUES (?, ?)";
$assign_courses_stmt = $conn->prepare($assign_courses_sql);
foreach ($assigned_courses as $course_id) {
// Check if the department of the assigned course matches the department of the teacher
$course_department_check_sql = "SELECT department FROM courses WHERE course_id = ?";
$course_department_check_stmt = $conn->prepare($course_department_check_sql);
$course_department_check_stmt->bind_param("i", $course_id);
$course_department_check_stmt->execute();
$course_department_check_stmt->bind_result($course_department);
$course_department_check_stmt->fetch();
$course_department_check_stmt->close();
if ($course_department === $department) {
// Bind the teacher_id and course_id for the assignment
$assign_courses_stmt->bind_param("ii", $teacher_id, $course_id);
$assign_courses_stmt->execute();
}
}
$assign_courses_stmt->close();
}
echo "Teacher and assigned courses added successfully!";
echo '<script type="text/javascript">
setTimeout(function(){
window.location = "create_teacher.php";
}, 3000);
</script>';
$insert_teacher_stmt->close();
$conn->close();
}