feat: 实现考试分数智能分配
All checks were successful
continuous-integration/drone/push Build is passing

解决题目数量无法整除总分时出现无限小数的问题

后端:
- 新增 ScoreDistributor 分数分配工具类
- 支持整数分配和小数分配两种模式
- 更新 exam_service.py 使用智能分配
- 考试总分固定100分,按题目数量智能分配

前端:
- 新增 scoreFormatter.ts 分数格式化工具
- 提供分数显示、等级判断等辅助函数

示例:100分/6题 = [17,17,17,17,16,16] 总和=100
This commit is contained in:
yuliang_guo
2026-01-30 14:50:41 +08:00
parent 27b5766694
commit e3b7bdcfd8
5 changed files with 411 additions and 12 deletions

View File

@@ -11,6 +11,7 @@ from app.models.exam import Exam, Question, ExamResult
from app.models.exam_mistake import ExamMistake
from app.models.course import Course, KnowledgePoint
from app.core.exceptions import BusinessException, ErrorCode
from app.utils.score_distributor import ScoreDistributor
class ExamService:
@@ -54,27 +55,38 @@ class ExamService:
all_questions, min(question_count, len(all_questions))
)
# 构建题目数据
# 使用智能分数分配器,确保总分为整数且分配合理
total_score = 100.0 # 固定总分为100分
actual_question_count = len(selected_questions)
# 创建分数分配器
distributor = ScoreDistributor(total_score, actual_question_count)
distributed_scores = distributor.distribute(mode="integer")
# 构建题目数据,使用分配后的分数
questions_data = []
for q in selected_questions:
for i, q in enumerate(selected_questions):
question_data = {
"id": str(q.id),
"type": q.question_type,
"title": q.title,
"content": q.content,
"options": q.options,
"score": q.score,
"score": distributed_scores[i], # 使用智能分配的分数
}
questions_data.append(question_data)
# 计算及格分数(向上取整,确保公平)
pass_score = ScoreDistributor.calculate_pass_score(total_score, 0.6)
# 创建考试记录
exam = Exam(
user_id=user_id,
course_id=course_id,
exam_name=f"{course.name} - 随机测试",
question_count=len(selected_questions),
total_score=sum(q.score for q in selected_questions),
pass_score=sum(q.score for q in selected_questions) * 0.6,
question_count=actual_question_count,
total_score=total_score,
pass_score=pass_score,
duration_minutes=60,
status="started",
questions={"questions": questions_data},
@@ -140,6 +152,9 @@ class ExamService:
for question_data in exam.questions["questions"]:
question_id = question_data["id"]
question = questions_map.get(question_id)
# 使用考试创建时分配的分数(智能分配后的整数分数)
allocated_score = question_data.get("score", 10.0)
if not question:
continue
@@ -148,7 +163,7 @@ class ExamService:
is_correct = user_answer == question.correct_answer
if is_correct:
total_score += question.score
total_score += allocated_score # 使用分配的分数
correct_count += 1
# 创建答题结果记录
@@ -157,7 +172,7 @@ class ExamService:
question_id=int(question_id),
user_answer=user_answer,
is_correct=is_correct,
score=question.score if is_correct else 0.0,
score=allocated_score if is_correct else 0.0, # 使用分配的分数
)
db.add(exam_result)