-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.php
More file actions
executable file
·319 lines (300 loc) · 17.8 KB
/
classes.php
File metadata and controls
executable file
·319 lines (300 loc) · 17.8 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
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
<?php
require __DIR__ . '/inc/bootstrap.php';
require __DIR__ . '/inc/admin_ui.php';
$user = admin_require();
$flashError = flash_get('error');
$flashSuccess = flash_get('success');
function class_schedule_dates(array $weekdays, string $startDate, string $endDate, array $pickedDates): array
{
$dates = [];
foreach ($pickedDates as $date) {
$date = trim($date);
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
$dates[] = $date;
}
}
if (!empty($dates)) {
$dates = array_values(array_unique($dates));
sort($dates);
return $dates;
}
if ($startDate === '' || $endDate === '' || empty($weekdays)) {
return [];
}
$start = strtotime($startDate);
$end = strtotime($endDate);
if ($start === false || $end === false || $start > $end) {
return [];
}
$weekdayNumbers = array_values(array_filter(array_map('school_weekday_number', $weekdays)));
for ($time = $start; $time <= $end; $time = strtotime('+1 day', $time)) {
if (in_array((int) date('N', $time), $weekdayNumbers, true)) {
$dates[] = date('Y-m-d', $time);
}
}
return $dates;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = (string) ($_POST['action'] ?? '');
if ($action === 'save_schedule') {
$courseId = (int) ($_POST['course_id'] ?? 0);
$course = db_fetch_one('SELECT * FROM courses WHERE id = :id LIMIT 1', ['id' => $courseId]);
if ($course === null) {
flash_set('error', '未找到班级。');
header('Location: classes.php');
exit;
}
$teacherName = trim((string) ($_POST['teacher_name'] ?? ''));
$classroomId = (int) ($_POST['classroom_id'] ?? 0);
$standardHours = max(0, (int) ($_POST['standard_hours'] ?? 0));
$lessonHoursEach = max(0.5, (float) ($_POST['lesson_hours_each'] ?? 1));
$weekdays = array_values(array_filter((array) ($_POST['weekdays'] ?? []), static fn ($value): bool => in_array((string) $value, school_weekdays(), true)));
$slotKey = trim((string) ($_POST['slot_key'] ?? ''));
$startDate = trim((string) ($_POST['class_start_date'] ?? ''));
$endDate = trim((string) ($_POST['class_end_date'] ?? ''));
$pickedDates = preg_split('/[、,,;;\s]+/u', trim((string) ($_POST['selected_dates'] ?? ''))) ?: [];
$slotLookup = school_selection_slots();
$slot = $slotLookup[$slotKey] ?? null;
if ($teacherName === '' || $classroomId <= 0 || $slot === null) {
flash_set('error', '请完整设置老师、教室和上课时段。');
header('Location: classes.php?schedule_id=' . $courseId);
exit;
}
$dates = class_schedule_dates($weekdays, $startDate, $endDate, $pickedDates);
if (empty($dates)) {
flash_set('error', '请设置开结课日期与上课星期,或直接填写要上课的日期。');
header('Location: classes.php?schedule_id=' . $courseId);
exit;
}
try {
$pdo = db();
if (!$pdo instanceof PDO) {
throw new RuntimeException('数据库未连接。');
}
$pdo->beginTransaction();
db_exec(
'UPDATE courses
SET teacher_name = :teacher_name,
classroom_id = :classroom_id,
standard_hours = :standard_hours,
lesson_hours_each = :lesson_hours_each,
weekdays_json = :weekdays_json,
time_slots_json = :time_slots_json,
class_start_date = :class_start_date,
class_end_date = :class_end_date,
schedule_dates_json = :schedule_dates_json,
class_status = "scheduled"
WHERE id = :id',
[
'teacher_name' => $teacherName,
'classroom_id' => $classroomId,
'standard_hours' => $standardHours,
'lesson_hours_each' => $lessonHoursEach,
'weekdays_json' => json_encode($weekdays, JSON_UNESCAPED_UNICODE),
'time_slots_json' => json_encode([$slotKey], JSON_UNESCAPED_UNICODE),
'class_start_date' => $startDate !== '' ? $startDate : $dates[0],
'class_end_date' => $endDate !== '' ? $endDate : end($dates),
'schedule_dates_json' => json_encode($dates, JSON_UNESCAPED_UNICODE),
'id' => $courseId,
]
);
db_exec('DELETE FROM lessons WHERE course_class_id = :course_id AND status = "planned"', ['course_id' => $courseId]);
$lessonNo = 1;
foreach ($dates as $date) {
db_exec(
'INSERT INTO lessons (course_class_id, lesson_no, lesson_date, start_time, end_time, topic, status)
VALUES (:course_class_id, :lesson_no, :lesson_date, :start_time, :end_time, :topic, "planned")',
[
'course_class_id' => $courseId,
'lesson_no' => $lessonNo,
'lesson_date' => $date,
'start_time' => ($slot['start_time'] ?? '00:00') . ':00',
'end_time' => ($slot['end_time'] ?? '00:00') . ':00',
'topic' => ($course['formal_name'] ?: $course['title']) . ' 第' . $lessonNo . '课',
]
);
$lessonNo++;
}
db_exec(
'INSERT INTO workflow_events (event_type, title, payload_json, created_by_user_id)
VALUES ("class_schedule_saved", :title, :payload_json, :created_by_user_id)',
[
'title' => '班级排课已生成',
'payload_json' => json_encode(['course_id' => $courseId, 'lesson_total' => count($dates)], JSON_UNESCAPED_UNICODE),
'created_by_user_id' => (int) ($user['id'] ?? 0) ?: null,
]
);
$pdo->commit();
flash_set('success', '班级排课已保存,已生成 ' . count($dates) . ' 次课。');
} catch (Throwable $e) {
if (isset($pdo) && $pdo instanceof PDO && $pdo->inTransaction()) {
$pdo->rollBack();
}
flash_set('error', '排课保存失败:' . $e->getMessage());
}
header('Location: classes.php');
exit;
}
}
$selectedCounts = course_selected_counts();
$courses = db_fetch_all(
'SELECT c.*, s.name AS subject_name, cl.name AS classroom_name
FROM courses c
LEFT JOIN subjects s ON s.id = c.subject_id
LEFT JOIN classrooms cl ON cl.id = c.classroom_id
WHERE c.approval_status = "active"
ORDER BY c.id DESC'
);
$classrooms = db_fetch_all('SELECT * FROM classrooms WHERE is_active = 1 ORDER BY name');
$teachers = db_fetch_all('SELECT * FROM teachers WHERE status = "active" ORDER BY name');
$scheduleCourseId = (int) ($_GET['schedule_id'] ?? 0);
$scheduleCourse = null;
$openable = 0;
$underMin = 0;
$full = 0;
foreach ($courses as &$course) {
$selected = (int) ($selectedCounts[(int) $course['id']] ?? 0);
$min = (int) ($course['min_students'] ?? 0);
$max = (int) ($course['max_students'] ?? 0);
$course['selected_total'] = $selected;
$course['remaining_total'] = $max > 0 ? max(0, $max - $selected) : null;
if ($max > 0 && $selected >= $max) {
$full++;
$course['open_status'] = '已报满';
} elseif ($min > 0 && $selected < $min) {
$underMin++;
$course['open_status'] = '未达最低人数';
} else {
$openable++;
$course['open_status'] = '可开班';
}
$course['conflict_note'] = '';
if ((int) $course['id'] === $scheduleCourseId) {
$scheduleCourse = $course;
}
}
unset($course);
$slotLookup = school_selection_slots();
for ($i = 0, $courseTotal = count($courses); $i < $courseTotal; $i++) {
for ($j = $i + 1; $j < $courseTotal; $j++) {
$leftClassroom = (int) ($courses[$i]['classroom_id'] ?? 0);
$rightClassroom = (int) ($courses[$j]['classroom_id'] ?? 0);
if ($leftClassroom <= 0 || $leftClassroom !== $rightClassroom) {
continue;
}
$sharedDays = array_intersect(course_weekdays($courses[$i]), course_weekdays($courses[$j]));
if (empty($sharedDays)) {
continue;
}
foreach (course_selection_slot_keys($courses[$i]) as $leftSlot) {
foreach (course_selection_slot_keys($courses[$j]) as $rightSlot) {
$left = $slotLookup[$leftSlot] ?? null;
$right = $slotLookup[$rightSlot] ?? null;
if ($left === null || $right === null) {
continue;
}
$leftStart = strtotime('2000-01-01 ' . ($left['start_time'] ?? '00:00'));
$leftEnd = strtotime('2000-01-01 ' . ($left['end_time'] ?? '00:00'));
$rightStart = strtotime('2000-01-01 ' . ($right['start_time'] ?? '00:00'));
$rightEnd = strtotime('2000-01-01 ' . ($right['end_time'] ?? '00:00'));
if ($leftStart < $rightEnd && $rightStart < $leftEnd) {
$courses[$i]['conflict_note'] = '教室时间冲突:' . ($courses[$j]['formal_name'] ?: $courses[$j]['title']);
$courses[$j]['conflict_note'] = '教室时间冲突:' . ($courses[$i]['formal_name'] ?: $courses[$i]['title']);
}
}
}
}
}
admin_ui_shell_start('classes', '班级管理', '围绕选课结果完成开班判断、导入学员、排课开课和课时设置。', [
['label' => '业务流程总控', 'href' => 'workflow.php', 'primary' => true],
['label' => '时段设置', 'href' => 'schedule_slots.php'],
]);
?>
<?php if ($flashError): ?><div class="notice error"><?= e($flashError) ?></div><?php endif; ?>
<?php if ($flashSuccess): ?><div class="notice"><?= e($flashSuccess) ?></div><?php endif; ?>
<section class="summary-grid">
<article class="summary-card blue"><span>班级总数</span><strong><?= e((string) count($courses)) ?></strong><small>来自已上架课程</small></article>
<article class="summary-card green"><span>可开班</span><strong><?= e((string) $openable) ?></strong><small>满足最低开班人数</small></article>
<article class="summary-card orange"><span>未达最低人数</span><strong><?= e((string) $underMin) ?></strong><small>需通知或关班</small></article>
<article class="summary-card blue"><span>已报满</span><strong><?= e((string) $full) ?></strong><small>后续只能候补</small></article>
</section>
<section class="content-panel">
<div class="content-panel-head">
<div>
<p class="page-kicker">开班判断</p>
<h2>班级预选结果</h2>
</div>
<div class="content-panel-meta">点击班级名称查看详情,点击设置进行排课</div>
</div>
<div class="table-card">
<table class="data-table">
<thead>
<tr>
<th>课程/班级</th><th>教师</th><th>星期</th><th>时段</th><th>教室</th><th>最低/最大</th><th>已选</th><th>状态</th><th>排课</th>
</tr>
</thead>
<tbody>
<?php foreach (array_slice($courses, 0, 80) as $course): ?>
<tr>
<td><a class="table-strong-link" href="class_detail.php?course_id=<?= e($course['id']) ?>"><?= e($course['formal_name'] ?: $course['title']) ?></a><div class="table-subline"><?= e($course['course_no']) ?></div></td>
<td><?= e($course['teacher_name'] ?: $course['provider_name']) ?></td>
<td><?= e(implode('、', course_weekdays($course)) ?: '-') ?></td>
<td><?= e(implode('、', course_time_slot_labels($course)) ?: '-') ?></td>
<td><?= e($course['classroom_name'] ?: '未设置') ?></td>
<td><?= e((string) ($course['min_students'] ?: '-')) ?> / <?= e((string) ($course['max_students'] ?: '-')) ?></td>
<td><?= e((string) $course['selected_total']) ?></td>
<td>
<span class="pill <?= $course['open_status'] === '未达最低人数' ? 'pending' : 'active' ?>"><?= e(($course['class_status'] ?? '') === 'closed' ? '已关班' : $course['open_status']) ?></span>
<?php if (!empty($course['conflict_note'])): ?><div class="table-subline danger"><?= e($course['conflict_note']) ?></div><?php endif; ?>
</td>
<td><a class="btn small" href="classes.php?schedule_id=<?= e($course['id']) ?>">设置</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</section>
<section class="content-panel">
<div class="content-panel-head"><div><p class="page-kicker">排课开课</p><h2>排课后生成每一次课</h2></div></div>
<div class="workflow-grid">
<article class="workflow-step"><strong>基础设置</strong><p>设置老师、教室、总课次、每次课课时、星期和标准时段。</p></article>
<article class="workflow-step"><strong>日期生成</strong><p>可按起止日期和星期自动生成,也可直接粘贴全部上课日期。</p></article>
<article class="workflow-step"><strong>课时统计</strong><p>班级详情会汇总学员课时消耗、请假、迟到、缺课和每课评价。</p></article>
</div>
</section>
<?php if ($scheduleCourse): ?>
<?php
$selectedWeekdays = course_weekdays($scheduleCourse);
$selectedSlots = course_selection_slot_keys($scheduleCourse);
$selectedSlot = $selectedSlots[0] ?? array_key_first($slotLookup);
$scheduleDates = json_decode((string) ($scheduleCourse['schedule_dates_json'] ?? '[]'), true);
$scheduleDateText = is_array($scheduleDates) ? implode("\n", $scheduleDates) : '';
?>
<div class="modal-backdrop" role="presentation"></div>
<section class="schedule-modal" role="dialog" aria-modal="true" aria-label="排课设置">
<div class="schedule-modal-head">
<div><p class="page-kicker">排课设置</p><h2><?= e($scheduleCourse['formal_name'] ?: $scheduleCourse['title']) ?></h2></div>
<a class="slot-popover-close" href="classes.php" aria-label="关闭排课设置">×</a>
</div>
<form class="schedule-form" method="post">
<input type="hidden" name="action" value="save_schedule">
<input type="hidden" name="course_id" value="<?= e($scheduleCourse['id']) ?>">
<label class="field"><span class="label">老师</span><input name="teacher_name" list="teacher-list" value="<?= e($scheduleCourse['teacher_name'] ?: $scheduleCourse['provider_name']) ?>" required></label>
<datalist id="teacher-list"><?php foreach ($teachers as $teacher): ?><option value="<?= e($teacher['name']) ?>"><?php endforeach; ?></datalist>
<label class="field"><span class="label">教室</span><select name="classroom_id" required><option value="">请选择教室</option><?php foreach ($classrooms as $classroom): ?><option value="<?= e($classroom['id']) ?>" <?= (int) ($scheduleCourse['classroom_id'] ?? 0) === (int) $classroom['id'] ? 'selected' : '' ?>><?= e($classroom['name']) ?> · <?= e((string) ($classroom['capacity'] ?? 0)) ?>人</option><?php endforeach; ?></select></label>
<div class="form-grid two-col">
<label class="field"><span class="label">课次</span><input name="standard_hours" type="number" min="1" value="<?= e((string) max(1, (int) ($scheduleCourse['standard_hours'] ?? 16))) ?>"></label>
<label class="field"><span class="label">每次课课时</span><input name="lesson_hours_each" type="number" min="0.5" step="0.5" value="<?= e((string) ($scheduleCourse['lesson_hours_each'] ?? 1)) ?>"></label>
</div>
<label class="field"><span class="label">上课星期</span><div class="choice-row"><?php foreach (school_weekdays() as $weekday): ?><label class="choice-chip"><input type="checkbox" name="weekdays[]" value="<?= e($weekday) ?>" <?= in_array($weekday, $selectedWeekdays, true) ? 'checked' : '' ?>><span><?= e($weekday) ?></span></label><?php endforeach; ?></div></label>
<label class="field"><span class="label">上课时段</span><select name="slot_key" required><?php foreach ($slotLookup as $slotKey => $slot): ?><option value="<?= e($slotKey) ?>" <?= $selectedSlot === $slotKey ? 'selected' : '' ?>><?= e($slot['label']) ?> · <?= e($slot['time']) ?></option><?php endforeach; ?></select></label>
<div class="form-grid two-col">
<label class="field"><span class="label">开课日期</span><input name="class_start_date" type="date" value="<?= e((string) ($scheduleCourse['class_start_date'] ?? '')) ?>"></label>
<label class="field"><span class="label">结课日期</span><input name="class_end_date" type="date" value="<?= e((string) ($scheduleCourse['class_end_date'] ?? '')) ?>"></label>
</div>
<label class="field"><span class="label">直接点选/粘贴全部上课日期</span><textarea name="selected_dates" placeholder="2026-05-12 2026-05-19 2026-05-26"><?= e($scheduleDateText) ?></textarea><span class="hint">如果这里填写日期,将优先按这些日期生成课次;留空则按开课日期、结课日期和上课星期自动生成。</span></label>
<div class="inline-actions"><button class="btn primary" type="submit">保存并生成课次</button><a class="btn" href="classes.php">取消</a></div>
</form>
</section>
<?php endif; ?>
<?php admin_ui_shell_end(); ?>