File: /var/www/html/wp-content/themes/elearning-education/page-teacher-dashboard.php
<?php
/*
Template Name: Teacher Dashboard
*/
get_header();
if (!current_user_can('manage_options')) wp_die('无权限');
$current_user_id = get_current_user_id();
global $wpdb;
// 加载课程(creator或team)
$courses = $wpdb->get_results($wpdb->prepare("
SELECT c.*, tt.teacher_id IS NOT NULL as is_team
FROM {$wpdb->prefix}edu_courses c
LEFT JOIN {$wpdb->prefix}edu_team_teachers tt ON c.id = tt.course_id AND tt.teacher_id = %d
WHERE c.creator_id = %d OR tt.teacher_id = %d", $current_user_id, $current_user_id, $current_user_id));
// 加载班级(活跃)
$classes = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}edu_classes WHERE archived = 0");
// 游戏表格(按课程)
$games = $wpdb->get_results("SELECT g.*, c.name as course_name, u.display_name as creator_name, gt.name as type_name
FROM {$wpdb->prefix}edu_games g
JOIN {$wpdb->prefix}edu_courses c ON g.course_id = c.id
JOIN {$wpdb->prefix}edu_game_types gt ON g.type_id = gt.id
JOIN {$wpdb->prefix}users u ON g.creator_id = u.ID
ORDER BY g.created_at DESC");
// 处理POST:创建课程/游戏/发布/导入/归档(用AJAX调用上钩子)
?>
<style>
.dashboard { max-width: 1200px; margin: 20px auto; padding: 20px; background: white; border-radius: 8px; }
.section { margin-bottom: 30px; }
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
button, input, select { padding: 8px; margin: 5px; border: 1px solid #ddd; border-radius: 4px; }
.form-group { margin-bottom: 15px; }
</style>
<div class="dashboard">
<h1>教师仪表盘</h1>
<!-- 创建课程 -->
<div class="section">
<h2>创建课程</h2>
<form id="create-course-form">
<div class="form-group">
<label>课程名称</label>
<input type="text" name="name" required placeholder="e.g. 新能源汽车概论">
</div>
<button type="submit">创建</button>
</form>
</div>
<!-- 课程列表 & 添加团队教师 -->
<div class="section">
<h2>我的课程</h2>
<table>
<tr><th>ID</th><th>名称</th><th>创建时间</th><th>操作</th></tr>
<?php foreach ($courses as $course): ?>
<tr>
<td><?php echo $course->id; ?></td>
<td><?php echo esc_html($course->name); ?></td>
<td><?php echo $course->created_at; ?></td>
<td>
<button onclick="addTeamTeacher(<?php echo $course->id; ?>)">添加团队教师</button>
<!-- 弹窗选用户ID -->
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<!-- 创建游戏 -->
<div class="section">
<h2>创建游戏</h2>
<form id="create-game-form">
<div class="form-group">
<label>课程</label>
<select name="course_id" required><?php foreach ($courses as $c) echo "<option value='{$c->id}'>{$c->name}</option>"; ?></select>
</div>
<div class="form-group">
<label>游戏类型</label>
<select name="type_id" required>
<?php $types = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}edu_game_types"); foreach ($types as $t) echo "<option value='{$t->id}'>{$t->name}</option>"; ?>
</select>
</div>
<div class="form-group">
<label>游戏名称</label>
<input type="text" name="name" required>
</div>
<div class="form-group">
<label>设置 (JSON)</label>
<textarea name="settings" rows="5" placeholder='{"questions": [...]}'></textarea>
</div>
<button type="submit">创建</button>
</form>
</div>
<!-- 游戏列表 -->
<div class="section">
<h2>游戏列表</h2>
<table>
<tr><th>ID</th><th>课程</th><th>类型</th><th>名称</th><th>创建人</th><th>创建时间</th><th>操作</th></tr>
<?php foreach ($games as $game): ?>
<tr>
<td><?php echo $game->id; ?></td>
<td><?php echo esc_html($game->course_name); ?></td>
<td><?php echo esc_html($game->type_name); ?></td>
<td><?php echo esc_html($game->name); ?></td>
<td><?php echo esc_html($game->creator_name); ?></td>
<td><?php echo $game->created_at; ?></td>
<td>
<button onclick="publishGame(<?php echo $game->id; ?>)">发布</button>
<button onclick="editGame(<?php echo $game->id; ?>)">编辑</button>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<!-- 班级管理 & 学生导入 & 归档 -->
<div class="section">
<h2>班级管理</h2>
<form id="create-class-form">
<div class="form-group">
<label>班级名称</label>
<input type="text" name="name" required>
<select name="course_id"><option value="">无课程</option><?php foreach ($courses as $c) echo "<option value='{$c->id}'>{$c->name}</option>"; ?></select>
</div>
<button type="submit">创建班级</button>
</form>
<h3>导入学生 (下载模板: CSV - student_id,name,user_id,email)</h3>
<a href="<?php echo admin_url('admin-post.php?action=download_template'); ?>">下载模板</a>
<form id="import-students-form" enctype="multipart/form-data">
<div class="form-group">
<label>班级</label>
<select name="class_id" required><?php foreach ($classes as $cls) echo "<option value='{$cls->id}'>{$cls->name}</option>"; ?></select>
</div>
<input type="file" name="csv_file" accept=".csv" required>
<button type="submit">导入</button>
</form>
<table>
<tr><th>ID</th><th>名称</th><th>课程</th><th>状态</th><th>操作</th></tr>
<?php foreach ($classes as $cls): ?>
<tr>
<td><?php echo $cls->id; ?></td>
<td><?php echo esc_html($cls->name); ?></td>
<td><?php echo $cls->course_id ? $wpdb->get_var("SELECT name FROM {$wpdb->prefix}edu_courses WHERE id = {$cls->course_id}") : '通用'; ?></td>
<td><?php echo $cls->archived ? '归档' : '活跃'; ?></td>
<td><button onclick="archiveClass(<?php echo $cls->id; ?>)"><?php echo $cls->archived ? '恢复' : '归档'; ?></button></td>
</tr>
<?php endforeach; ?>
</table>
</div>
<!-- 发布弹窗逻辑 (JS) -->
</div>
<script>
const ajaxUrl = '<?php echo admin_url("admin-ajax.php"); ?>';
const nonce = '<?php echo wp_create_nonce("teacher_actions"); ?>';
// 示例:创建课程
document.getElementById('create-course-form').addEventListener('submit', (e) => {
e.preventDefault();
const formData = new FormData(e.target);
formData.append('action', 'create_course');
formData.append('nonce', nonce);
fetch(ajaxUrl, { method: 'POST', body: formData }).then(res => res.json()).then(data => {
if (data.success) alert('创建成功!ID: ' + data.data.course_id);
});
});
// 类似其他表单:create_game, publish_game (POST class_ids, start_time, end_time), import_students, archive_class
function addTeamTeacher(courseId) {
const teacherId = prompt('教师User ID:');
fetch(ajaxUrl, { method: 'POST', body: new URLSearchParams({ action: 'add_team_teacher', nonce, course_id: courseId, teacher_id: teacherId }) })
.then(res => res.json()).then(data => alert(data.success ? '添加成功' : data.data));
}
function publishGame(gameId) {
const classIds = prompt('班级IDs (逗号分隔):'); // e.g. 1,2
const start = prompt('开始时间 (YYYY-MM-DD HH:MM):');
const end = prompt('结束时间 (可选):');
fetch(ajaxUrl, { method: 'POST', body: new URLSearchParams({ action: 'publish_game', nonce, game_id: gameId, class_ids: classIds, start_time: start, end_time: end }) })
.then(res => res.json()).then(data => alert(data.success ? '发布成功' : data.data));
}
// editGame: 加载settings JSON编辑
// archiveClass: POST action=archive_class
</script>
<?php get_footer(); ?>