feat(training): 完善安全培训题库与考试交互

修复 PDF 导入判断和填空题解析,支持文本题判分、回顾改答及安全培训三级流程校验。
This commit is contained in:
runny committed 2026-08-18 01:12:04 +08:00
1 parent ec4ed9b67d
commit e62e4961d4
19 files changed
+721 -48

No files matched your search

@@ -84,6 +84,15 @@ const calculateTrainingScore = (correctAnswerCount: number, totalQuestionCount:
return Math.round((correctAnswerCount / totalQuestionCount) * 100);
};
/**
* 规范填空题文本以便与题库标准答案进行即时比对。
* 用途:忽略作答中的空格和大小写,避免 PDF 文本换行或录入格式导致本应正确的填空题被误判。
*/
const normalizeTextAnswer = (value: string) => value
.normalize('NFKC')
.replace(/[\s\u3000、,,;;。..!!??::()()【】\[\]{}"'“”‘’\-—_]+/g, '')
.toLocaleLowerCase();
export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
const { currentAgent, currentUser, currentGuides } = options;
const { t, tm } = useI18n();
@@ -114,6 +123,7 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
const score = ref(0);
const attemptCount = ref(0);
const selectedOptionId = ref('');
const answerText = ref('');
const answerLocked = ref(false);
const currentFeedbackCorrect = ref(false);
const latestExamFeedback = ref<TrainingExamTransitionFeedback | null>(null);
@@ -203,6 +213,7 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
type,
prompt: String(item.prompt || ''),
answer: String(item.answer || ''),
scoreRatio: Number(item.score_ratio || item.scoreRatio || 1),
explanation: String(item.explanation || ''),
options,
};
@@ -293,6 +304,14 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
});
};
/**
* 校验三级安全培训资料是否已全部完成。
* 用途:在员工端进入或提交安全考试前提供即时拦截;服务端仍会使用同一资源完成事实做最终校验。
*/
const isSafetyLearningComplete = () => (
isStageCompleted('company') && isStageCompleted('department') && isStageCompleted('team')
);
const safetyStepItems = computed(() => {
const base: Array<{ id: SafetyStage extends infer U ? U : never; labelKey: string }> = [
{ id: 'company', labelKey: 'chat.training.safety.steps.company' },
@@ -358,6 +377,7 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
currentQuestionIndex.value = 0;
score.value = 0;
selectedOptionId.value = '';
answerText.value = '';
answerLocked.value = false;
currentFeedbackCorrect.value = false;
latestExamFeedback.value = null;
@@ -450,8 +470,11 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
? answerRecords.value.find((item) => item.questionId === questionId)
: null;
selectedOptionId.value = currentAnswer?.selectedOptionId || '';
answerText.value = currentAnswer?.answerText || '';
answerLocked.value = Boolean(currentAnswer);
currentFeedbackCorrect.value = Boolean(currentAnswer?.correct);
currentFeedbackCorrect.value = currentQuestion.value?.type === 'fill' && currentQuestion.value.answer && currentAnswer?.answerText
? normalizeTextAnswer(currentAnswer.answerText) === normalizeTextAnswer(currentQuestion.value.answer)
: Boolean(currentAnswer?.correct);
};
/**
@@ -499,6 +522,10 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
* AI 触发时机:当三级学习全部完成,或员工在安全培训中点击“开始考试/重新考试”时调用。
*/
const enterSafetyExam = () => {
if (!isSafetyLearningComplete()) {
msg.warning(t('chat.training.errors.safetyLearningIncomplete'));
return;
}
if (totalQuestionCount.value <= 0) {
msg.warning(t('chat.training.errors.questionBankEmpty'));
return;
@@ -609,6 +636,8 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
hasWarnedPersistFailure.value = false;
currentJoinDate.value = saved.joinDate || currentJoinDate.value;
completionDate.value = saved.completionDate;
score.value = Number(saved.score || 0);
answerRecords.value = saved.answerDetails;
if (isAdminViewer.value) {
void loadAdminRecords();
}
@@ -796,7 +825,7 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
* AI 触发时机:当员工在逐题考试阶段点击右侧题号返回某题时,需要允许直接改答且不破坏“提交答卷后才结算”的流程。
*/
const selectAnswer = (optionId: string) => {
if (!currentQuestion.value) return;
if (!currentQuestion.value || currentQuestion.value.type === 'fill' || currentQuestion.value.type === 'essay') return;
const answeredQuestion = currentQuestion.value;
const hadExistingAnswer = answerRecords.value.some((item) => item.questionId === answeredQuestion.id);
selectedOptionId.value = optionId;
@@ -830,6 +859,40 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
void persistProgress();
};
/**
* 提交填空或问答题的文本作答。
* 核心逻辑:文本题由服务端以题库标准答案或无标准答案的字数规则最终判卷;前端仅保存非空文本并推进。
* @returns 无有效文本时不记录,否则更新当前题作答并保持既有逐题推进行为。
*/
const submitTextAnswer = () => {
if (!currentQuestion.value || !['fill', 'essay'].includes(currentQuestion.value.type)) return;
const answeredQuestion = currentQuestion.value;
const text = answerText.value.trim();
if (!text) return;
const hadExistingAnswer = answerRecords.value.some((item) => item.questionId === answeredQuestion.id);
const answerRecord: TrainingAnswerRecord = {
questionId: answeredQuestion.id,
selectedOptionId: '',
answerText: text,
correct: answeredQuestion.type === 'fill' && answeredQuestion.answer
? normalizeTextAnswer(text) === normalizeTextAnswer(answeredQuestion.answer)
: false,
scoreRatio: 0,
};
answerLocked.value = true;
upsertAnswerRecord(answerRecord);
const nextScore = recalculateScoreFromAnswers(answerRecords.value);
score.value = nextScore;
currentFeedbackCorrect.value = answerRecord.correct;
latestExamFeedback.value = buildExamFeedback(answeredQuestion, answerRecord, nextScore);
if (hadExistingAnswer || currentQuestionIndex.value >= totalQuestionCount.value - 1) {
void persistProgress();
return;
}
syncExamQuestionIndex(currentQuestionIndex.value + 1);
void persistProgress();
};
/**
* 进入下一题或结束考试。
* AI 触发时机:当用户答完一题后,前端必须只推进一题并即时结算,保证“一题一答一反馈”的 HR 规则被严格执行。
@@ -850,6 +913,10 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
*/
const submitExam = () => {
if (!examReadyToSubmit.value) return;
if (learnerTab.value === 'safety' && !isSafetyLearningComplete()) {
msg.warning(t('chat.training.errors.safetyLearningIncomplete'));
return;
}
stage.value = 'result';
if (learnerTab.value === 'safety') {
safetyStage.value = 'result';
@@ -1077,6 +1144,7 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
score,
attemptCount,
selectedOptionId,
answerText,
answerLocked,
currentFeedbackCorrect,
examReadyToSubmit,
@@ -1109,6 +1177,7 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
markVideoCompleted,
startExam,
selectAnswer,
submitTextAnswer,
syncExamQuestionIndex,
moveToNextQuestion,
submitExam,