支持按课程批量绑定 Moodle 链接

This commit is contained in:
biss committed 2026-11-23 18:13:27 +08:00
1 parent 5493e7e5ff
commit ddbb88d474
5 files changed
+102

No files matched your search

@@ -151,6 +151,34 @@ public sealed class TeachingTasksController(
return NoContent();
}
[HttpGet("moodle-course/batch-preview")]
public async Task<ActionResult> 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<ActionResult> 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<ActionResult> 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,
@@ -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<OkObjectResult>(await database.Controller.PreviewMoodleCourseBatch(database.TermId, first.CourseId, CancellationToken.None));
Assert.Equal(2, preview.Value!.GetType().GetProperty("eligible")!.GetValue(preview.Value));
Assert.IsType<ObjectResult>(await database.Controller.SetMoodleCourseBatch(new(database.TermId, first.CourseId, "http://invalid"), CancellationToken.None));
var result = Assert.IsType<OkObjectResult>(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<OkObjectResult>(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<ForbidResult>(await database.Controller.PreviewMoodleCourseBatch(database.TermId, course.Id, CancellationToken.None));
Assert.IsType<ForbidResult>(await database.Controller.SetMoodleCourseBatch(new(database.TermId, course.Id, "https://moodle.example.edu/course"), CancellationToken.None));
}
[Fact]
public async Task MoodleLink_CollegeAdminCannotChangeAnotherCollegesTask()
{
+1
View File
@@ -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",
Loaded 3 of 5 files, more files were not shown because too many files have changed in this diff. Show more