Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
1090 lines (933 loc) · 41.4 KB
/
Copy pathscript.js
File metadata and controls
1090 lines (933 loc) · 41.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @ts-nocheck
class QuizApp {
constructor() {
this.questions = [];
this.allQuestions = [];
this.currentQuestionIndex = 0;
this.answers = new Map();
this.stats = {
correct: 0,
wrong: 0,
total: 0
};
this.questionTypes = ['言语理解', '资料分析', '图形推理'];
this.questionsPerType = 10;
this.timePerQuestion = 60; // 单位:秒
this.currentQuestionTimer = null;
this.remainingTime = this.timePerQuestion;
this.isPracticeMode = true;
this.isRandomOrder = false;
this.serverPort = 3000; // 默认端口
this.markedQuestions = new Set();
this.shortcuts = {
'm': () => this.toggleMarkQuestion()
};
this.initializeElements();
this.initializeEventListeners();
this.updateUI();
// 检测服务器端口并初始化
this.detectServerPort().then(() => {
// 只加载test.md文件
this.loadQuestions('test.md');
this.watchQuestions('test.md');
}).catch(error => {
console.error("无法连接到服务器:", error);
this.questionElement.innerHTML = `
<div class="error-message">
<h3>无法连接到服务器</h3>
<p>请确保服务器正在运行,并刷新页面重试。</p>
<p>错误信息: ${error.message}</p>
</div>
`;
});
}
async detectServerPort() {
try {
const response = await fetch(`http://localhost:${this.serverPort}/server-info`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
const data = await response.json();
this.serverPort = data.port || this.serverPort;
console.log(`已连接到服务器端口: ${this.serverPort}`);
return;
}
} catch (error) {
console.log(`默认端口 ${this.serverPort} 连接失败,尝试检测其他端口...`);
}
const maxPort = this.serverPort + 10;
for (let port = this.serverPort + 1; port <= maxPort; port++) {
try {
const response = await fetch(`http://localhost:${port}/server-info`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
const data = await response.json();
this.serverPort = data.port || port;
console.log(`已连接到服务器端口: ${this.serverPort}`);
return;
}
} catch (error) {
console.log(`端口 ${port} 连接失败`);
}
}
throw new Error(`无法找到可用的服务器端口 (${this.serverPort}-${maxPort})`);
}
initializeElements() {
this.questionElement = document.getElementById('question');
this.optionsElement = document.getElementById('options');
this.answerElement = document.getElementById('answer');
this.correctAnswerElement = document.getElementById('correctAnswer');
this.explanationElement = document.getElementById('explanation');
this.questionNumberElement = document.getElementById('questionNumber');
this.navToggleBtn = document.getElementById('navToggleBtn');
this.questionNav = document.querySelector('.question-nav');
this.questionNavList = document.getElementById('questionNav');
this.settingsBtn = document.getElementById('settingsBtn');
this.settingsPanel = document.getElementById('settingsPanel');
this.questionOrderSelect = document.getElementById('questionOrderSelect');
this.modeToggleBtn = document.getElementById('modeToggleBtn');
this.checkAnswerBtn = document.getElementById('checkAnswer');
this.submitTestBtn = document.getElementById('submitTest');
this.timerDisplay = document.getElementById('timerDisplay');
this.timeSpentElement = document.getElementById('timeSpent');
this.progressElement = document.getElementById('progressFill');
this.correctCountElement = document.getElementById('correctCount');
this.wrongCountElement = document.getElementById('wrongCount');
this.accuracyElement = document.getElementById('accuracy');
this.completionElement = document.getElementById('completion');
this.questionTypeElement = document.getElementById('questionType');
this.resultsPanel = document.getElementById('resultsPanel');
this.totalQuestionsElement = document.getElementById('totalQuestions');
this.totalCorrectElement = document.getElementById('totalCorrect');
this.totalWrongElement = document.getElementById('totalWrong');
this.finalAccuracyElement = document.getElementById('finalAccuracy');
this.totalTimeElement = document.getElementById('totalTime');
this.wrongQuestionsListElement = document.getElementById('wrongQuestionsList');
this.correctQuestionsListElement = document.getElementById('correctQuestionsList');
this.markedCountElement = document.getElementById('markedCount');
this.markQuestionBtn = document.getElementById('markQuestion');
this.helpBtn = document.getElementById('helpBtn');
this.helpPanel = document.getElementById('helpPanel');
this.updateModeDisplay();
this.settingsPanel.classList.add('hidden');
this.resultsPanel.querySelector('.close-btn').addEventListener('click', () => {
this.resultsPanel.classList.remove('visible');
this.resultsPanel.classList.add('hidden');
});
}
initializeEventListeners() {
document.getElementById('prevBtn').addEventListener('click', () => this.previousQuestion());
document.getElementById('nextBtn').addEventListener('click', () => this.nextQuestion());
this.checkAnswerBtn.addEventListener('click', () => this.toggleAnswer());
this.submitTestBtn.addEventListener('click', () => this.submitTest());
this.optionsElement.addEventListener('click', (e) => this.handleOptionClick(e));
this.modeToggleBtn.addEventListener('click', () => this.toggleMode());
this.settingsBtn.addEventListener('click', () => this.toggleSettingsPanel());
this.settingsPanel.querySelector('.close-btn').addEventListener('click', () => {
this.settingsPanel.classList.remove('visible');
this.settingsPanel.classList.add('hidden');
});
this.questionOrderSelect.addEventListener('change', () => {
this.isRandomOrder = this.questionOrderSelect.value === 'random';
this.resetQuiz();
});
this.navToggleBtn.addEventListener('click', () => this.toggleQuestionNav());
this.questionNav.querySelector('.close-btn').addEventListener('click', () => {
this.questionNav.classList.remove('visible');
this.questionNav.classList.add('hidden');
});
document.getElementById('typeFilter').addEventListener('change', (e) => {
const selectedType = e.target.value;
const categories = document.querySelectorAll('.question-category');
categories.forEach(category => {
if (selectedType === 'all' || category.querySelector('h4').textContent === selectedType) {
category.classList.remove('hidden');
} else {
category.classList.add('hidden');
}
});
});
this.markQuestionBtn.addEventListener('click', () => this.toggleMarkQuestion());
// 添加帮助按钮点击事件
this.helpBtn.addEventListener('click', () => {
this.helpPanel.classList.remove('hidden');
this.helpPanel.classList.add('visible');
});
this.helpPanel.querySelector('.close-btn').addEventListener('click', () => {
this.helpPanel.classList.remove('visible');
this.helpPanel.classList.add('hidden');
});
// 添加键盘快捷键监听
document.addEventListener('keydown', (e) => {
// 只在非输入框状态下响应快捷键
if (e.target.tagName !== 'INPUT' && e.target.tagName !== 'TEXTAREA') {
const handler = this.shortcuts[e.key.toLowerCase()];
if (handler) {
e.preventDefault();
handler();
}
}
});
}
toggleQuestionNav() {
const isHidden = this.questionNav.classList.contains('hidden');
if (isHidden) {
this.questionNav.classList.remove('hidden');
this.questionNav.classList.add('visible');
} else {
this.questionNav.classList.remove('visible');
this.questionNav.classList.add('hidden');
}
}
updateQuestionNav() {
if (!this.isPracticeMode) {
this.questionNav.classList.add('hidden');
this.navToggleBtn.classList.add('hidden');
return;
}
this.navToggleBtn.classList.remove('hidden');
const questionsByType = {};
this.questions.forEach((q, index) => {
const type = this.detectQuestionType(q);
if (!questionsByType[type]) {
questionsByType[type] = [];
}
questionsByType[type].push({ question: q, index });
});
this.questionNavList.innerHTML = Object.entries(questionsByType)
.map(([type, questions]) => {
const questionLinks = questions.map(({ question, index }) => {
const isAnswered = this.answers.has(index);
const isCorrect = isAnswered && this.answers.get(index) === question.answer;
const isCurrent = index === this.currentQuestionIndex;
const isMarked = this.markedQuestions.has(index);
return `
<div class="nav-item ${isCurrent ? 'active' : ''}
${isAnswered ? (isCorrect ? 'answered' : 'wrong') : ''}
${isMarked ? 'marked' : ''}"
data-index="${index}">
<span class="nav-number">${index + 1}</span>
${isMarked ? '<i class="fas fa-bookmark"></i>' : ''}
${isAnswered ? `<i class="fas ${isCorrect ? 'fa-check text-success' : 'fa-times text-danger'}"></i>` : ''}
</div>
`;
}).join('');
return `
<div class="question-category">
<div class="category-header">
<h4>${type}</h4>
<span class="badge">${questions.length}题</span>
</div>
<div class="category-questions">
${questionLinks}
</div>
</div>
`;
}).join('');
this.questionNavList.addEventListener('click', (e) => {
const item = e.target.closest('.nav-item');
if (!item) return;
const index = parseInt(item.dataset.index);
if (!isNaN(index)) {
this.currentQuestionIndex = index;
this.showQuestion();
}
});
}
toggleSettingsPanel() {
const isHidden = this.settingsPanel.classList.contains('hidden');
if (isHidden) {
this.settingsPanel.classList.remove('hidden');
this.settingsPanel.classList.add('visible');
} else {
this.settingsPanel.classList.remove('visible');
this.settingsPanel.classList.add('hidden');
}
this.questionOrderSelect.value = this.isRandomOrder ? 'random' : 'sequential';
}
shuffleQuestions() {
for (let i = this.questions.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.questions[i], this.questions[j]] = [this.questions[j], this.questions[i]];
}
}
toggleMode() {
// Clear any existing timer
if (this.currentQuestionTimer) {
clearInterval(this.currentQuestionTimer);
this.currentQuestionTimer = null;
}
this.isPracticeMode = !this.isPracticeMode;
if (!this.isPracticeMode) {
this.prepareAssessmentQuestions();
this.remainingTime = this.timePerQuestion;
this.timerDisplay.classList.remove('warning');
this.timerDisplay.textContent = '1:00';
}
this.updateModeDisplay();
this.resetQuiz();
}
prepareAssessmentQuestions() {
// 按题型分组所有题目
const questionsByType = {};
this.allQuestions.forEach(q => {
const type = q.type || '未知题型';
if (!questionsByType[type]) {
questionsByType[type] = [];
}
questionsByType[type].push(q);
});
// 清空当前题目
this.questions = [];
// 只处理指定的题型顺序
this.questionTypes.forEach(type => {
const typeQuestions = questionsByType[type] || [];
if (typeQuestions.length > 0) {
// 为当前题型选择10题
const shuffled = [...typeQuestions];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
// 选择并标记题目
const selectedQuestions = shuffled.slice(0, this.questionsPerType);
selectedQuestions.forEach(q => {
q.type = type;
q.typeIndex = selectedQuestions.indexOf(q); // 添加类型内的索引
});
// 添加到题目列表
this.questions = this.questions.concat(selectedQuestions);
} else {
console.warn(`题型 "${type}" 没有足够的题目`);
}
});
// 检查所有必需题型是否都有题目
if (this.questions.length < this.questionTypes.length * this.questionsPerType) {
console.warn(`总题目数不足 ${this.questionTypes.length * this.questionsPerType} 题`);
}
}
startQuestionTimer() {
if (this.currentQuestionTimer) {
clearInterval(this.currentQuestionTimer);
}
this.remainingTime = this.timePerQuestion;
// 更新计时器显示
// In assessment mode, we use the countdown timer element
let timerDisplay = !this.isPracticeMode ? this.timerDisplay : document.getElementById('timeSpent');
let updateTimer = () => {
if (this.remainingTime > 0) {
this.remainingTime--;
let minutes = Math.floor(this.remainingTime / 60);
let seconds = this.remainingTime % 60;
timerDisplay.textContent = `${minutes}:${seconds.toString().padStart(2, '0')}`;
// Add warning class when time is running low (less than 10 seconds)
if (this.remainingTime <= 10) {
timerDisplay.classList.add('warning');
} else {
timerDisplay.classList.remove('warning');
}
} else {
clearInterval(this.currentQuestionTimer);
this.handleTimeUp();
}
};
this.currentQuestionTimer = setInterval(updateTimer, 1000);
updateTimer(); // Initial display update
}
handleTimeUp() {
// Record current selection as answer
const selectedOption = document.querySelector('input[name="option"]:checked');
const answer = selectedOption ? selectedOption.value : null;
this.answers.set(this.currentQuestionIndex, answer);
// Auto advance to next question or submit test
if (this.currentQuestionIndex < this.questions.length - 1) {
setTimeout(() => this.nextQuestion(), 100); // Small delay for better UX
} else {
this.submitTest();
}
}
updateModeDisplay() {
this.modeToggleBtn.innerHTML = `<i class="fas fa-${this.isPracticeMode ? 'edit' : 'clock'}"></i> ${this.isPracticeMode ? '练习模式' : '模拟测评'}`;
this.checkAnswerBtn.classList.toggle('hidden', !this.isPracticeMode);
this.submitTestBtn.classList.toggle('hidden', this.isPracticeMode);
this.timerDisplay.classList.toggle('hidden', this.isPracticeMode);
// Hide navigation elements in assessment mode
document.getElementById('prevBtn').classList.toggle('hidden', !this.isPracticeMode);
// Hide type progress containers in assessment mode
this.questionTypes.forEach(type => {
const typeSection = document.getElementById(type + 'Section');
if (typeSection) {
typeSection.classList.toggle('hidden', !this.isPracticeMode);
}
});
if (this.isPracticeMode) {
this.navToggleBtn.classList.remove('hidden');
} else {
this.navToggleBtn.classList.add('hidden');
this.questionNav.classList.remove('visible');
this.questionNav.classList.add('hidden');
}
if (!this.isPracticeMode) {
this.startTimer();
} else {
this.stopTimer();
}
this.updateQuestionNav();
}
resetQuiz() {
this.answers.clear();
this.currentQuestionIndex = 0;
if (!this.isPracticeMode) {
this.prepareAssessmentQuestions();
} else {
this.questions = [...this.allQuestions];
if (this.isRandomOrder) {
this.shuffleQuestions();
}
}
this.updateStats();
this.showQuestion();
this.answerElement.classList.add('hidden');
this.updateQuestionNav();
}
startTimer() {
this.startTime = new Date();
if (this.timerInterval) {
clearInterval(this.timerInterval);
}
this.timerInterval = setInterval(() => {
const now = new Date();
const diff = now - this.startTime;
const hours = Math.floor(diff / 3600000);
const minutes = Math.floor((diff % 3600000) / 60000);
const seconds = Math.floor((diff % 60000) / 1000);
this.timeSpentElement.textContent =
`${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}, 1000);
}
stopTimer() {
if (this.timerInterval) {
clearInterval(this.timerInterval);
this.timerInterval = null;
}
}
async loadQuestions(filename) {
try {
this.questionElement.innerHTML = `正在加载题库...`;
this.optionsElement.innerHTML = '';
const response = await fetch(`http://localhost:${this.serverPort}/questions?file=${filename}`);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to load questions');
}
const data = await response.json();
this.parseQuestions(data.content);
this.watchQuestions(filename);
console.log(`Successfully loaded ${filename}`);
} catch (error) {
console.error('Error loading questions:', error);
this.questionElement.innerHTML = `加载题库失败: ${error.message}<br>请刷新页面重试`;
}
}
watchQuestions(type = 'default') {
if (this.updateChecker) {
clearInterval(this.updateChecker);
}
let lastModified = 0;
this.updateChecker = setInterval(async () => {
try {
const response = await fetch(`http://localhost:${this.serverPort}/check-updates?type=${type}`);
if (!response.ok) {
const errorData = await response.json();
console.warn(`File watch warning: ${errorData.error}`);
return;
}
const data = await response.json();
if (lastModified === 0) {
lastModified = data.lastModified;
console.log(`Started watching ${type} with timestamp ${lastModified}`);
} else if (data.lastModified > lastModified) {
lastModified = data.lastModified;
await this.loadQuestions(type);
console.log(`Questions updated from file changes in ${type}`);
}
} catch (error) {
console.error('Error checking for updates:', error);
}
}, 2000);
}
parseQuestions(markdown) {
const sections = markdown.split(/# .+? #/).filter(section => section.trim());
const questions = [];
let currentType = '';
const typeMatches = markdown.match(/# (.+?) #/g) || [];
const types = typeMatches.map(match => match.replace(/^# (.+?) #$/, '$1'));
sections.forEach((section, index) => {
currentType = types[index] || '';
const blocks = section.split(/# \d+/).filter(block => block.trim());
blocks.forEach(block => {
const question = this.parseQuestionBlock(block, currentType);
if (question) {
questions.push(question);
}
});
});
if (questions.length > 0) {
this.allQuestions = questions;
this.questions = [...questions];
if (this.isRandomOrder) {
this.shuffleQuestions();
}
this.currentQuestionIndex = 0;
this.showQuestion();
this.updateQuestionNav();
} else {
this.showFormatError();
}
}
parseQuestionBlock(block, type) {
const lines = block.split('\n').filter(line => line.trim());
const questionText = [];
const options = [];
let answer = '';
let explanation = '';
let currentPart = 'question';
let content = '';
for (const line of lines) {
const trimmedLine = line.trim();
if (!trimmedLine) continue;
if (trimmedLine.startsWith('![')) {
questionText.push(trimmedLine);
continue;
}
if (/^[A-Z]:/.test(trimmedLine)) {
if (currentPart === 'question') {
content = questionText.join('\n').trim();
}
options.push(trimmedLine);
currentPart = 'options';
} else if (trimmedLine.startsWith('答案:')) {
answer = trimmedLine.split(':')[1].trim();
currentPart = 'answer';
} else if (trimmedLine.startsWith('解析:')) {
explanation = trimmedLine.substring(3).trim();
currentPart = 'explanation';
} else {
if (currentPart === 'question') {
questionText.push(trimmedLine);
} else if (currentPart === 'explanation') {
explanation += ' ' + trimmedLine;
}
}
}
if (questionText.length && options.length && answer) {
return {
content: content || questionText.join('\n'),
question: questionText.join('\n'),
options,
answer,
explanation,
type
};
}
return null;
}
handleOptionClick(event) {
const option = event.target.closest('.option');
if (!option) return;
const currentQuestion = this.questions[this.currentQuestionIndex];
if (!currentQuestion) return;
const options = this.optionsElement.querySelectorAll('.option');
options.forEach(opt => opt.classList.remove('selected', 'correct', 'wrong'));
option.classList.add('selected');
const selectedAnswer = option.textContent.trim()[0];
const isCorrect = selectedAnswer === currentQuestion.answer;
this.answers.set(this.currentQuestionIndex, selectedAnswer);
if (this.isPracticeMode) {
option.classList.add(isCorrect ? 'correct' : 'wrong');
this.answerElement.classList.remove('hidden');
}
this.updateStats();
this.updateProgress();
this.updateQuestionNav();
}
submitTest() {
if (!this.isPracticeMode) {
// 在测评模式下,清除当前题目的计时器
if (this.currentQuestionTimer) {
clearInterval(this.currentQuestionTimer);
}
}
if (this.answers.size < this.questions.length) {
const confirmed = confirm('还有题目未完成,确定要提交吗?');
if (!confirmed) return;
}
this.stopTimer();
this.showAllAnswers();
this.submitTestBtn.disabled = true;
this.showTestResults();
}
showTestResults() {
const total = this.questions.length;
let correct = 0;
const correctQuestions = [];
const wrongQuestions = [];
this.questions.forEach((question, index) => {
const userAnswer = this.answers.get(index);
if (!userAnswer) return;
if (userAnswer === question.answer) {
correct++;
correctQuestions.push({ index, question, userAnswer });
} else {
wrongQuestions.push({ index, question, userAnswer });
}
});
this.totalQuestionsElement.textContent = total;
this.totalCorrectElement.textContent = correct;
this.totalWrongElement.textContent = this.answers.size - correct;
this.finalAccuracyElement.textContent = `${Math.round((correct / this.answers.size) * 100)}%`;
const timeSpent = new Date() - this.startTime;
const hours = Math.floor(timeSpent / 3600000);
const minutes = Math.floor((timeSpent % 3600000) / 60000);
const seconds = Math.floor((timeSpent % 60000) / 1000);
this.totalTimeElement.textContent =
`${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
this.wrongQuestionsListElement.innerHTML = wrongQuestions.map(item => `
<div class="result-question-item wrong" data-index="${item.index}">
<span class="question-number">第 ${item.index + 1} 题</span>
<span class="question-result">
<span class="user-answer">选择:${item.userAnswer}</span>
<span class="correct-answer">正确:${item.question.answer}</span>
</span>
</div>
`).join('');
this.correctQuestionsListElement.innerHTML = correctQuestions.map(item => `
<div class="result-question-item correct" data-index="${item.index}">
<span class="question-number">第 ${item.index + 1} 题</span>
<span class="question-result">
<span class="answer">答案:${item.question.answer}</span>
</span>
</div>
`).join('');
this.wrongQuestionsListElement.addEventListener('click', (e) => this.handleResultItemClick(e));
this.correctQuestionsListElement.addEventListener('click', (e) => this.handleResultItemClick(e));
this.resultsPanel.classList.remove('hidden');
this.resultsPanel.classList.add('visible');
}
handleResultItemClick(event) {
const item = event.target.closest('.result-question-item');
if (!item) return;
const index = parseInt(item.dataset.index);
if (!isNaN(index)) {
this.currentQuestionIndex = index;
this.showQuestion();
}
}
showAllAnswers() {
const options = this.optionsElement.querySelectorAll('.option');
const userAnswer = this.answers.get(this.currentQuestionIndex);
const currentQuestion = this.questions[this.currentQuestionIndex];
if (userAnswer) {
options.forEach(option => {
const optionLetter = option.textContent.trim()[0];
if (optionLetter === userAnswer) {
option.classList.add(userAnswer === currentQuestion.answer ? 'correct' : 'wrong');
}
});
}
this.correctAnswerElement.textContent = currentQuestion.answer;
this.explanationElement.innerHTML = marked.parse(currentQuestion.explanation);
this.answerElement.classList.remove('hidden');
this.updateStats();
}
showQuestion() {
// Clear any existing timer
if (this.currentQuestionTimer) {
clearInterval(this.currentQuestionTimer);
this.currentQuestionTimer = null;
}
// Start new timer in assessment mode
if (!this.isPracticeMode) {
this.remainingTime = this.timePerQuestion;
this.startQuestionTimer();
}
const question = this.questions[this.currentQuestionIndex];
if (!question) {
this.updateUI();
return;
}
this.questionElement.innerHTML = marked.parse(question.question);
const images = this.questionElement.getElementsByTagName('img');
for (const img of images) {
if (!img.src.includes('images/')) {
img.src = `images/${img.getAttribute('src')}`;
}
}
// 在模拟测评模式下,显示当前题型的进度
if (!this.isPracticeMode) {
const currentType = question.type;
const typeQuestions = this.questions.filter(q => q.type === currentType);
const typeIndex = typeQuestions.indexOf(question);
const questionNumber = typeIndex + 1;
this.questionNumberElement.textContent = `${currentType}第${questionNumber}/10题`;
this.questionTypeElement.textContent = `共${this.questionTypes.length}个题型`;
} else {
this.questionNumberElement.textContent = `题目 ${this.currentQuestionIndex + 1}/${this.questions.length}`;
this.questionTypeElement.textContent = question.type || '未知题型';
}
this.updateProgress();
const isMarked = this.markedQuestions.has(this.currentQuestionIndex);
this.markQuestionBtn.classList.toggle('active', isMarked);
this.markQuestionBtn.innerHTML = `<i class="fas fa-bookmark"></i> ${isMarked ? '取消标记' : '标记题目'}`;
this.optionsElement.innerHTML = question.options
.map(option => `<div class="option">${marked.parse(option)}</div>`)
.join('');
const previousAnswer = this.answers.get(this.currentQuestionIndex);
if (previousAnswer) {
const options = this.optionsElement.querySelectorAll('.option');
options.forEach(option => {
if (option.textContent.trim()[0] === previousAnswer) {
option.classList.add('selected');
if (this.isPracticeMode || this.submitTestBtn.disabled) {
option.classList.add(previousAnswer === question.answer ? 'correct' : 'wrong');
}
}
});
}
this.answerElement.classList.add('hidden');
if (this.isPracticeMode || this.submitTestBtn.disabled) {
this.correctAnswerElement.textContent = question.answer;
this.explanationElement.innerHTML = marked.parse(question.explanation);
}
this.fixImagePaths();
const questionType = this.detectQuestionType(question);
this.questionTypeElement.textContent = questionType;
}
detectQuestionType(question) {
return question.type || '未知题型';
}
updateStats() {
const total = this.answers.size;
let correct = 0;
// 按题型统计正确率
const statsByType = {};
this.questionTypes.forEach(type => {
statsByType[type] = {
total: 0,
correct: 0,
answered: 0
};
});
this.answers.forEach((answer, index) => {
const question = this.questions[index];
const type = question.type;
if (statsByType[type]) {
statsByType[type].answered++;
if (answer === question.answer) {
correct++;
statsByType[type].correct++;
}
}
});
// 更新总体统计
if (!this.isPracticeMode) {
if (!this.submitTestBtn.disabled) {
// 在模拟测评未提交前隐藏所有统计信息,只显示完成数量
this.correctCountElement.textContent = '--';
this.wrongCountElement.textContent = '--';
this.accuracyElement.textContent = '--';
this.completionElement.textContent = `${total}/${this.questions.length}题`;
} else {
// 测评提交后显示完整统计
this.correctCountElement.textContent = correct;
this.wrongCountElement.textContent = total - correct;
this.accuracyElement.textContent = `${Math.round((correct / total) * 100)}%`;
this.completionElement.textContent = `${total}/${this.questions.length}题`;
}
} else {
// 练习模式显示实时统计
if (total > 0) {
this.correctCountElement.textContent = correct;
this.wrongCountElement.textContent = total - correct;
this.accuracyElement.textContent = `${Math.round((correct / total) * 100)}%`;
this.completionElement.textContent = `${total}/${this.questions.length}题`;
} else {
this.correctCountElement.textContent = '0';
this.wrongCountElement.textContent = '0';
this.accuracyElement.textContent = '0%';
this.completionElement.textContent = '0/0题';
}
}
this.markedCountElement.textContent = this.markedQuestions.size;
// 仅在练习模式或测评已提交时显示题型统计
if (this.isPracticeMode || this.submitTestBtn.disabled) {
let statsContainer = document.querySelector('.type-stats-container');
if (!statsContainer) {
statsContainer = document.createElement('div');
statsContainer.className = 'type-stats-container';
const statusBar = document.querySelector('.status-bar');
statusBar.appendChild(statsContainer);
}
// 生成各题型的统计信息
const statsHTML = Object.entries(statsByType)
.map(([type, data]) => {
const accuracy = data.answered > 0 ? Math.round((data.correct / data.answered) * 100) : 0;
const accuracyDisplay = !this.isPracticeMode && !this.submitTestBtn.disabled ? '--' : `${accuracy}%`;
return `
<div class="type-stat">
<span class="type-name">${type}</span>
<span class="type-accuracy">正确率: ${accuracyDisplay}</span>
</div>
`;
}).join('');
statsContainer.innerHTML = statsHTML;
} else {
// 在练习模式下移除题型统计显示
const statsContainer = document.querySelector('.type-stats-container');
if (statsContainer) {
statsContainer.remove();
}
}
}
updateProgress() {
if (this.questions.length === 0) return;
// 更新总体进度
const progress = (this.answers.size / this.questions.length) * 100;
this.progressElement.style.width = `${progress}%`;
// 只在练习模式下显示题型进度
if (this.isPracticeMode) {
const questionsByType = {};
this.questionTypes.forEach(type => {
questionsByType[type] = {
total: 0,
answered: 0
};
});
this.questions.forEach((q, index) => {
const type = q.type;
if (questionsByType[type]) {
questionsByType[type].total++;
if (this.answers.has(index)) {
questionsByType[type].answered++;
}
}
});
// 更新或创建题型进度容器
let progressContainer = document.querySelector('.type-progress-container');
if (!progressContainer) {
progressContainer = document.createElement('div');
progressContainer.className = 'type-progress-container';
const questionContainer = document.getElementById('questionContainer');
questionContainer.insertBefore(progressContainer, this.questionElement);
}
// 生成进度HTML
const progressHTML = Object.entries(questionsByType)
.map(([type, data]) => {
const typeProgress = (data.answered / data.total) * 100;
return `
<div class="type-progress">
<span class="type-label">${type}</span>
<div class="progress-bar">
<div class="progress-fill" style="width: ${typeProgress}%"></div>
</div>
<span class="progress-text">${data.answered}/${data.total}</span>
</div>
`;
}).join('');
progressContainer.innerHTML = progressHTML;
} else {
// 在测评模式下移除题型进度显示
const progressContainer = document.querySelector('.type-progress-container');
if (progressContainer) {
progressContainer.remove();
}
}
}
updateUI() {
if (this.questions.length === 0) {
this.questionElement.innerHTML = '正在加载题库...';
this.optionsElement.innerHTML = '';
this.questionNumberElement.textContent = '';
this.answerElement.classList.add('hidden');
this.questionTypeElement.textContent = '';
}
}