提交考试时计算选择题,多选题,判断题得分

This commit is contained in:
Lqc 2025-08-29 15:59:54 +08:00
parent 61665d749d
commit 6cf1b91b85

View File

@ -20,12 +20,16 @@ import org.jeecg.modules.gen.paper.entity.Paper;
import org.jeecg.modules.gen.paper.service.IPaperService;
import org.jeecg.modules.gen.paperquestion.entity.PaperQuestion;
import org.jeecg.modules.gen.paperquestion.service.IPaperQuestionService;
import org.jeecg.modules.gen.question.controller.QuestionController;
import org.jeecg.modules.gen.question.entity.Question;
import org.jeecg.modules.gen.question.entity.QuestionRequest;
import org.jeecg.modules.gen.question.service.IQuestionService;
import org.jeecg.modules.gen.questionoption.entity.QuestionOption;
import org.jeecg.modules.gen.questionoption.service.IQuestionOptionService;
import org.jeecg.modules.gen.questionrepo.entity.QuestionRepo;
import org.jeecg.modules.gen.questionrepo.service.IQuestionRepoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@ -51,10 +55,13 @@ public class ExamBizController {
private IQuestionService questionService;
@Autowired
private IQuestionRepoService questionRepoService;
@Autowired
private IQuestionOptionService questionOptionService;
//获取考试试题
@RequestMapping("/getExamQuestions/{examId}")
@Operation(summary = "获取考试试题")
@Transactional
public Result<?> getExamQuestions(@PathVariable String examId, @RequestParam String studentId) {
Exam exam = examService.getById(examId);
if(exam.getPaperId().isEmpty()){
@ -170,6 +177,7 @@ public class ExamBizController {
@PostMapping("/submitExam")
@Operation(summary = "提交考试")
@Transactional
public Result<?> submitExam(@RequestBody ExamRecord examRecord,
HttpServletRequest req) {
examRecord.setIpAddress(getClientIp(req));
@ -188,6 +196,9 @@ public class ExamBizController {
if (examRecord.getDeviceInfo() != null) {
updateWrapper.set(ExamRecord::getDeviceInfo, examRecord.getDeviceInfo());
}
// 阅卷
List<ExamAnswer> gradedAnswers = gradeExam(examRecord.getExamId(), examRecord.getUserId());
examAnswerService.updateBatchById(gradedAnswers);
// 更新考试状态提交时间
updateWrapper.
set(ExamRecord::getStatus,1).
@ -282,6 +293,90 @@ public class ExamBizController {
return ip;
}
/**
* 批量阅卷
* @param examId 考试ID
* @param userId 用户ID
* @return 阅卷后的答题列表
*/
private List<ExamAnswer> gradeExam(String examId, String userId) {
// 获取学生的答题列表
List<ExamAnswer> examAnswerList = examAnswerService.list(new LambdaQueryWrapper<ExamAnswer>()
.eq(ExamAnswer::getExamId, examId)
.eq(ExamAnswer::getUserId, userId)
);
// 提取所有题目ID
List<String> questionIds = examAnswerList.stream()
.map(ExamAnswer::getQuestionId)
.collect(Collectors.toList());
// 查询题目
List<Question> questions = questionService.list(new LambdaQueryWrapper<Question>()
.in(Question::getId, questionIds)
.lt(Question::getType, 3)
);
// 创建题目ID到题目的映射
Map<String, Question> questionMap = questions.stream()
.collect(Collectors.toMap(Question::getId, question -> question));
// 获取 选择多选判断 题目ID列表
List<String> questionIdsFromQuestions = questions.stream()
.map(Question::getId)
.collect(Collectors.toList());
// 查询这些题目的正确选项
List<QuestionOption> questionOptions = questionOptionService.list(new LambdaQueryWrapper<QuestionOption>()
.in(QuestionOption::getQuestionId, questionIdsFromQuestions)
.eq(QuestionOption::getIzCorrent, 1)
);
// 将选项转换为Map结构为题目ID -> 正确答案选项ID列表
Map<String, List<String>> correctAnswerMap = questionOptions.stream()
.collect(Collectors.groupingBy(
QuestionOption::getQuestionId,
Collectors.mapping(QuestionOption::getId, Collectors.toList())
));
// 遍历学生的答案进行评分
for (ExamAnswer examAnswer : examAnswerList) {
String studentAnswer = examAnswer.getAnswer();
Question question = questionMap.get(examAnswer.getQuestionId());
List<String> correctAnswers = correctAnswerMap.get(examAnswer.getQuestionId());
// 比较答案并设置分数
if (studentAnswer != null && question != null && correctAnswers != null) {
// 将学生答案按逗号分割成列表
List<String> studentAnswers = Arrays.asList(studentAnswer.split(","));
double score = 0.0;
// 根据题目类型进行评分
if (question.getType() == 1 || question.getType() == 2) { // 单选题或判断题
if (studentAnswers.get(0).equals(correctAnswers.get(0))) {
score = question.getScore(); // 使用题目设定的分值
}
} else if (question.getType() == 3) { // 多选题
// 检查学生答案数量是否正确
if (studentAnswers.size() == correctAnswers.size()) {
// 检查每个答案是否都正确
boolean allCorrect = studentAnswers.stream()
.allMatch(correctAnswers::contains);
if (allCorrect) {
score = question.getScore(); // 使用题目设定的分值
}
}
}
examAnswer.setIzCorrect(score> 0.0 ? 1 : 0);
examAnswer.setScore(score);
} else {
examAnswer.setScore(0.0); // 答案为空或题目不存在
}
}
return examAnswerList;
}
//获取设备信息
public String getDeviceInfo(HttpServletRequest request) {
// 获取User-Agent