diff --git a/src/Jiaowu.Api/Controllers/TeachingTasksController.cs b/src/Jiaowu.Api/Controllers/TeachingTasksController.cs
index fddfec2..8d39942 100644
--- a/src/Jiaowu.Api/Controllers/TeachingTasksController.cs
+++ b/src/Jiaowu.Api/Controllers/TeachingTasksController.cs
@@ -151,6 +151,34 @@ public sealed class TeachingTasksController(
return NoContent();
}
+ [HttpGet("moodle-course/batch-preview")]
+ public async Task PreviewMoodleCourseBatch(Guid academicTermId, Guid courseId, CancellationToken cancellationToken)
+ {
+ var course = await db.Courses.AsNoTracking().FirstOrDefaultAsync(x => x.Id == courseId, cancellationToken);
+ if (course is null) return NotFound();
+ if (!CanManage(course)) return Forbid();
+ var tasks = ScopedTasks().Where(x => x.AcademicTermId == academicTermId && x.CourseId == courseId && x.Status == TeachingTaskStatus.Published);
+ var total = await tasks.CountAsync(cancellationToken);
+ var eligible = await tasks.CountAsync(x => x.MoodleCourseUrl == null, cancellationToken);
+ return Ok(new { total, eligible, skipped = total - eligible });
+ }
+
+ [HttpPut("moodle-course/batch")]
+ public async Task SetMoodleCourseBatch(MoodleCourseBatchRequest request, CancellationToken cancellationToken)
+ {
+ var course = await db.Courses.AsNoTracking().FirstOrDefaultAsync(x => x.Id == request.CourseId, cancellationToken);
+ if (course is null) return NotFound();
+ if (!CanManage(course)) return Forbid();
+ var url = Normalize(request.Url);
+ if (url is null || url.Length > 2048 || !Uri.TryCreate(url, UriKind.Absolute, out var uri) ||
+ uri.Scheme != Uri.UriSchemeHttps || !string.IsNullOrEmpty(uri.UserInfo))
+ return ValidationProblem("请输入有效的 HTTPS Moodle 课程链接,且不能包含账号密码。");
+ var tasks = ScopedTasks().Where(x => x.AcademicTermId == request.AcademicTermId && x.CourseId == request.CourseId && x.Status == TeachingTaskStatus.Published);
+ var updated = await tasks.Where(x => x.MoodleCourseUrl == null)
+ .ExecuteUpdateAsync(setters => setters.SetProperty(x => x.MoodleCourseUrl, url), cancellationToken);
+ return Ok(new { updated });
+ }
+
[HttpGet("options")]
public async Task GetOptions(
Guid academicTermId,
@@ -856,6 +884,7 @@ public sealed class TeachingTasksController(
}
public sealed record MoodleCourseLinkRequest(string? Url);
+public sealed record MoodleCourseBatchRequest(Guid AcademicTermId, Guid CourseId, string? Url);
public sealed record TeachingTaskRequest(
[Required, MaxLength(40)] string TaskNumber,
diff --git a/tests/Jiaowu.Api.Tests/TeachingTasksControllerTests.cs b/tests/Jiaowu.Api.Tests/TeachingTasksControllerTests.cs
index 916d8d9..f91b0bc 100644
--- a/tests/Jiaowu.Api.Tests/TeachingTasksControllerTests.cs
+++ b/tests/Jiaowu.Api.Tests/TeachingTasksControllerTests.cs
@@ -12,6 +12,41 @@ namespace Jiaowu.Api.Tests;
public sealed class TeachingTasksControllerTests
{
+ [Fact]
+ public async Task MoodleBatchLink_UpdatesOnlyUnlinkedPublishedTasksInCourseAndTerm()
+ {
+ await using var database = await TestDatabase.CreateAsync();
+ var first = await database.AddTaskAsync("BATCH-1", TeachingTaskStatus.Published);
+ var second = await database.AddTaskAsync("BATCH-2", TeachingTaskStatus.Published);
+ var linked = await database.AddTaskAsync("BATCH-LINKED", TeachingTaskStatus.Published);
+ linked.MoodleCourseUrl = "https://moodle.example.edu/existing";
+ var draft = await database.AddTaskAsync("BATCH-DRAFT", TeachingTaskStatus.Draft);
+ await database.Db.SaveChangesAsync();
+ var preview = Assert.IsType(await database.Controller.PreviewMoodleCourseBatch(database.TermId, first.CourseId, CancellationToken.None));
+ Assert.Equal(2, preview.Value!.GetType().GetProperty("eligible")!.GetValue(preview.Value));
+ Assert.IsType(await database.Controller.SetMoodleCourseBatch(new(database.TermId, first.CourseId, "http://invalid"), CancellationToken.None));
+ var result = Assert.IsType(await database.Controller.SetMoodleCourseBatch(new(database.TermId, first.CourseId, " https://moodle.example.edu/course/view.php?id=1 "), CancellationToken.None));
+ Assert.Equal(2, result.Value!.GetType().GetProperty("updated")!.GetValue(result.Value));
+ foreach (var task in new[] { first, second, linked, draft }) await database.Db.Entry(task).ReloadAsync();
+ Assert.Equal("https://moodle.example.edu/course/view.php?id=1", first.MoodleCourseUrl);
+ Assert.Equal(first.MoodleCourseUrl, second.MoodleCourseUrl);
+ Assert.Equal("https://moodle.example.edu/existing", linked.MoodleCourseUrl);
+ Assert.Null(draft.MoodleCourseUrl);
+ var repeated = Assert.IsType(await database.Controller.SetMoodleCourseBatch(new(database.TermId, first.CourseId, "https://moodle.example.edu/other"), CancellationToken.None));
+ Assert.Equal(0, repeated.Value!.GetType().GetProperty("updated")!.GetValue(repeated.Value));
+ }
+
+ [Fact]
+ public async Task MoodleBatchLink_RejectsCourseOutsideCollegeScope()
+ {
+ await using var database = await TestDatabase.CreateAsync(collegeScoped: true);
+ var course = new Course { Code = "OTHER-BATCH", Name = "其他课程", College = new College { Code = "OTHER-BATCH", Name = "其他学院" }, Nature = CourseNature.MajorRequired, TotalHours = 32, LectureHours = 32 };
+ database.Db.Add(course);
+ await database.Db.SaveChangesAsync();
+ Assert.IsType(await database.Controller.PreviewMoodleCourseBatch(database.TermId, course.Id, CancellationToken.None));
+ Assert.IsType(await database.Controller.SetMoodleCourseBatch(new(database.TermId, course.Id, "https://moodle.example.edu/course"), CancellationToken.None));
+ }
+
[Fact]
public async Task MoodleLink_CollegeAdminCannotChangeAnotherCollegesTask()
{
diff --git a/web/src/i18n/messages/en-US/core.ts b/web/src/i18n/messages/en-US/core.ts
index b68e6bc..0961645 100644
--- a/web/src/i18n/messages/en-US/core.ts
+++ b/web/src/i18n/messages/en-US/core.ts
@@ -1,5 +1,6 @@
export default {
moodle: {
+ batchLink: "Batch link Moodle course", batchLinkHint: "Link all published teaching classes for one course and term. Existing links are skipped. This does not enrol users or create groups.", batchLinkTerm: "Academic term", batchLinkCourse: "Course", batchLinkUrl: "Moodle course URL", batchLinkRequired: "Select a term and course and enter a URL.", batchLinkReview: "Preview and confirm", batchLinkNone: "There are {total} published classes; {skipped} already have links. No classes can be linked.", batchLinkConfirm: "Term: {term}\nCourse: {course}\nURL: {url}\nPublished classes: {total}; link {count}, skip {skipped} already linked.\nThis does not enrol users or create groups. Continue?", batchLinkDone: "Linked {count} teaching classes.",
categoryMapping: "Moodle category mappings", categoryMappingHint: "Map the course's owning college and term to a Moodle subcategory ID. Public courses use their owning college. Existing linked courses are not moved.",
categoryCollege: "Course college", categoryTerm: "Term", categoryId: "Moodle category ID", categoryRequired: "Select a college and term and enter a category ID", categoryDeleteConfirm: "Removing this mapping prevents new Moodle courses from being created or copied for this college and term. Continue?",
myCourses: "My Moodle courses",
diff --git a/web/src/i18n/messages/zh-CN/core.ts b/web/src/i18n/messages/zh-CN/core.ts
index 0de62cc..68d189f 100644
--- a/web/src/i18n/messages/zh-CN/core.ts
+++ b/web/src/i18n/messages/zh-CN/core.ts
@@ -7,6 +7,7 @@ export default {
placeholder: "https://moodle.example.edu/course/view.php?id=123", save: "保存", cancel: "取消", saved: "Moodle 课程关联已保存", open: "进入 Moodle 课程", linked: "已关联 Moodle",
create: "匹配或创建课程", createConfirm: "将按教学班编号匹配 Moodle 课程;不存在时创建隐藏的空课程。教师和学生尚不会自动加入,确认继续?", created: "Moodle 课程已关联",
batchCreate: "批量匹配或创建 Moodle 课程", batchConfirm: "将为选中的 {count} 个未关联教学任务按编号匹配课程,未找到时创建隐藏的空课程;不会自动加入教师或学生。确认继续?", batchResult: "批量处理结果", batchSuccess: "已关联 {count} 门课程。", unavailable: "请先配置 Moodle 服务。",
+ batchLink: "批量绑定 Moodle 链接", batchLinkHint: "按学期和课程处理所有已发布教学班,已有链接的教学班会跳过。此操作不会同步师生或创建分组。", batchLinkTerm: "开课学期", batchLinkCourse: "课程", batchLinkUrl: "Moodle 课程链接", batchLinkRequired: "请选择学期、课程并填写链接。", batchLinkReview: "预览并确认", batchLinkNone: "共有 {total} 个已发布教学班,其中 {skipped} 个已关联,没有可绑定的教学班。", batchLinkConfirm: "学期:{term}\n课程:{course}\n链接:{url}\n已发布教学班:{total} 个;将绑定 {count} 个,跳过已关联的 {skipped} 个。\n不会同步师生或创建分组,确认继续?", batchLinkDone: "已绑定 {count} 个教学班。",
syncTeachers: "同步授课教师", syncConfirm: "按工号匹配 Moodle 用户名并加入授课教师;不会移除 Moodle 中已有的教师。确认继续?", syncSuccess: "授课教师已同步至 Moodle",
students: "导入 Moodle 学生", studentsConfirm: "按当前教学班名单和学号匹配 Moodle 用户名并加入课程;已有学生不会被移除。确认继续?", studentsDone: "已处理 {count} 名学生",
studentsBatch: "批量导入 Moodle 学生", studentsBatchConfirm: "为选中的 {count} 个已关联教学班导入当前学生名单;不会移除已有学生。确认继续?", studentsBatchDone: "已处理 {count} 个教学班。",
diff --git a/web/src/views/TeachingTasksView.vue b/web/src/views/TeachingTasksView.vue
index 826767f..c1fcffc 100644
--- a/web/src/views/TeachingTasksView.vue
+++ b/web/src/views/TeachingTasksView.vue
@@ -36,6 +36,31 @@ const moodleCategoryDialog = ref(false);
const moodleCategoryMappings = ref([]);
const moodleCategoryForm = reactive<{ collegeId: string; academicTermId: string; moodleCategoryId: number | undefined }>({ collegeId: '', academicTermId: '', moodleCategoryId: undefined });
const moodleCategorySaving = ref(false);
+const moodleLinkBatchDialog = ref(false);
+const moodleLinkBatchForm = reactive({ academicTermId: '', courseId: '', url: '' });
+const moodleLinkBatchSaving = ref(false);
+
+function openMoodleLinkBatch() {
+ Object.assign(moodleLinkBatchForm, { academicTermId: query.academicTermId ?? '', courseId: '', url: '' });
+ moodleLinkBatchDialog.value = true;
+}
+async function bindMoodleCourseBatch() {
+ const { academicTermId, courseId, url } = moodleLinkBatchForm;
+ if (!academicTermId || !courseId || !url.trim()) { ElMessage.warning(t('moodle.batchLinkRequired')); return; }
+ moodleLinkBatchSaving.value = true;
+ try {
+ const { data } = await http.get('/teaching-tasks/moodle-course/batch-preview', { params: { academicTermId, courseId } });
+ if (!data.eligible) { ElMessage.info(t('moodle.batchLinkNone', { total: data.total, skipped: data.skipped })); return; }
+ const course = manageableCourses.value.find(item => item.id === courseId);
+ const term = terms.value.find(item => item.id === academicTermId);
+ await ElMessageBox.confirm(t('moodle.batchLinkConfirm', { course: course?.name ?? '', term: term ? academicTermLabel(term) : '', url: url.trim(), total: data.total, count: data.eligible, skipped: data.skipped }), t('moodle.batchLink'), { type: 'warning' });
+ const result = await http.put('/teaching-tasks/moodle-course/batch', { academicTermId, courseId, url });
+ ElMessage.success(t('moodle.batchLinkDone', { count: result.data.updated }));
+ moodleLinkBatchDialog.value = false;
+ await load();
+ } catch (error) { if (error !== 'cancel' && error !== 'close') ElMessage.error(apiErrorMessage(error)); }
+ finally { moodleLinkBatchSaving.value = false; }
+}
async function loadMoodleCategories() {
try { moodleCategoryMappings.value = (await http.get('/moodle-courses/category-mappings')).data; }
@@ -982,6 +1007,7 @@ onMounted(async () => {
+ {{ t('moodle.batchLink') }}
{{ t('moodle.categoryMapping') }}
{{
t("teachingTaskTemplate.generateMergedPublicClasses")
@@ -992,6 +1018,16 @@ onMounted(async () => {
+
+ {{ t('moodle.batchLinkHint') }}
+
+
+
+
+
+ {{ t('moodle.cancel') }}{{ t('moodle.batchLinkReview') }}
+
+
{{ t("personnel.currentResults") }}