package nju.seec.helper.controller; import nju.seec.helper.sys.dataanalysis.constant.EventAction; import nju.seec.helper.sys.dataanalysis.constant.EventType; import nju.seec.helper.aspect.auth.Auth; import nju.seec.helper.aspect.auth.LoginUser; import nju.seec.helper.aspect.event.Event; import nju.seec.helper.aspect.record.ExerciseRecord; import nju.seec.helper.controller.response.PageResponse; import nju.seec.helper.dto.groups.Create; import nju.seec.helper.dto.groups.Modify; import nju.seec.helper.dto.quiz.QuizDTO; import nju.seec.helper.dto.question.StudentAnswerDTO; import nju.seec.helper.enums.QuizState; import nju.seec.helper.enums.UserRole; import nju.seec.helper.service.QuizService; import nju.seec.helper.service.QuizStudentAnswerService; import nju.seec.helper.vo.quiz.QuizStudentAnswerStatisticVO; import nju.seec.helper.vo.quiz.QuizStudentAnswerVO; import nju.seec.helper.vo.quiz.QuizVO; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; /** * 测试 * * @author xst *

* updated by cst */ @RestController @RequestMapping("/api/quiz") public class QuizController { private final QuizService quizService; private final QuizStudentAnswerService quizStudentAnswerService; public QuizController(QuizService quizService, QuizStudentAnswerService quizStudentAnswerService) { this.quizService = quizService; this.quizStudentAnswerService = quizStudentAnswerService; } /** * 基于课件获取测试列表 */ @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获得测试列表") @GetMapping("/slide/{slideId}") public PageResponse getQuizzesBySlide(LoginUser user, @PathVariable Long slideId, @RequestParam(required = false, defaultValue = "") String key, @PageableDefault(Integer.MAX_VALUE) Pageable pageable) { switch (user.getRole()) { case TEACHER: return PageResponse.of(quizService.teacherGetQuizzesBySlide(user, slideId, key, pageable)); case STUDENT: return PageResponse.of(quizService.studentGetQuizzesBySlide(user, slideId, key, pageable)); default: return PageResponse.empty(); } } /** * 基于状态获取测试列表 */ @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获取测试列表") @GetMapping("/state") public PageResponse getQuizzesByState(LoginUser user, QuizState state, @RequestParam(required = false, defaultValue = "") String key, @PageableDefault(Integer.MAX_VALUE) Pageable pageable) { switch (user.getRole()) { case TEACHER: return PageResponse.of(quizService.teacherGetQuizzesByState(user, state, key, pageable)); case STUDENT: return PageResponse.of(quizService.studentGetQuizzesByState(user, state, key, pageable)); default: return PageResponse.empty(); } } /** * 获得某一测试的详细内容 */ @Auth(roles = {UserRole.STUDENT, UserRole.TEACHER}, message = "获得测试") @GetMapping("/{quizId}") public QuizVO getOneQuiz(LoginUser user, @PathVariable("quizId") Long quizId) { return quizService.getOneQuiz(user, quizId); } /** * 创建测试 */ @Auth(roles = {UserRole.TEACHER}, message = "创建测试") @PostMapping public QuizVO createQuiz(LoginUser user, @Validated(Create.class) @RequestBody QuizDTO quizDTO) { return quizService.createQuiz(user, quizDTO); } /** * 修改测试 (仅限无人提交时可以修改) */ @Auth(roles = {UserRole.TEACHER}, message = "修改测试") @PutMapping("/{quizId}") public QuizVO updateQuiz(LoginUser user, @PathVariable Long quizId, @Validated(Modify.class) @RequestBody QuizDTO quizDTO) { return quizService.modifyQuiz(user, quizId, quizDTO); } /** * 删除测试 (伪删除) */ @Auth(roles = {UserRole.TEACHER}, message = "删除测试") @DeleteMapping("/{quizId}") public void deleteQuiz(LoginUser user, @PathVariable("quizId") String quizId) { quizService.deleteQuiz(user, Long.valueOf(quizId)); } /** * 提交作答 */ @ExerciseRecord @Event(type = EventType.QUIZ, action = EventAction.COMPLETE) @Auth(roles = {UserRole.STUDENT}, message = "提交作答") @PostMapping("/{quizId}/student-answer") public QuizStudentAnswerVO submitQuizStudentAnswer(LoginUser user, @PathVariable Long quizId, @Validated @RequestBody StudentAnswerDTO studentAnswerDTO) { return quizStudentAnswerService.submitQuizStudentAnswer(user, quizId, studentAnswerDTO); } /** * 获取测试作答列表 */ @Auth(roles = {UserRole.TEACHER}, message = "获取学生作答") @GetMapping("/{quizId}/student-answers") public PageResponse getQuizStudentAnswers(LoginUser user, @PathVariable Long quizId, @RequestParam(required = false, defaultValue = "") String key, @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) { return PageResponse.of(quizStudentAnswerService.getQuizStudentAnswers(user, quizId, key, pageable)); } /** * 学生获取某一测试自己的作答 */ @Auth(roles = {UserRole.STUDENT}, message = "获取作答") @GetMapping("/{quizId}/student-answer") public QuizStudentAnswerVO getOneQuizStudentAnswer(LoginUser user, @PathVariable Long quizId) { return quizStudentAnswerService.getQuizStudentAnswer(user, quizId); } /** * 获取测试作答提交的统计信息 */ @Auth(roles = {UserRole.TEACHER}, message = "获取测试统计消息") @GetMapping("/{quizId}/statistic") public QuizStudentAnswerStatisticVO getQuizStatistic(LoginUser user, @PathVariable Long quizId) { return quizStudentAnswerService.getQuizStudentAnswerStatistic(user, quizId); } }