完整實現詳細答題結果
This commit is contained in:
@@ -803,74 +803,6 @@ function AdminResultsContent() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* 單一測試類型的題目(邏輯或創意) */}
|
|
||||||
{detailData.result.type !== 'combined' && detailData.questions.map((question: any, index: number) => (
|
|
||||||
<div key={index} className="border rounded-lg p-4">
|
|
||||||
<div className="flex items-start justify-between mb-3">
|
|
||||||
<h4 className="font-medium">第 {index + 1} 題</h4>
|
|
||||||
{question.isCorrect !== undefined && (
|
|
||||||
<Badge variant={question.isCorrect ? "default" : "destructive"}>
|
|
||||||
{question.isCorrect ? "正確" : "錯誤"}
|
|
||||||
</Badge>
|
|
||||||
)}
|
|
||||||
{question.score !== undefined && (
|
|
||||||
<Badge variant="outline" className="text-green-600 border-green-600">
|
|
||||||
{question.score} 分
|
|
||||||
</Badge>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-3">
|
|
||||||
<div>
|
|
||||||
<label className="text-sm font-medium text-muted-foreground">題目內容</label>
|
|
||||||
<p className="text-sm mt-1">{question.question || question.statement}</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{question.type === 'logic' && (
|
|
||||||
<>
|
|
||||||
<div className="grid grid-cols-2 gap-4">
|
|
||||||
<div>
|
|
||||||
<label className="text-sm font-medium text-muted-foreground">選項</label>
|
|
||||||
<div className="space-y-1 mt-1">
|
|
||||||
{question.option_a && <p className="text-sm">A. {question.option_a}</p>}
|
|
||||||
{question.option_b && <p className="text-sm">B. {question.option_b}</p>}
|
|
||||||
{question.option_c && <p className="text-sm">C. {question.option_c}</p>}
|
|
||||||
{question.option_d && <p className="text-sm">D. {question.option_d}</p>}
|
|
||||||
{question.option_e && <p className="text-sm">E. {question.option_e}</p>}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label className="text-sm font-medium text-muted-foreground">答案</label>
|
|
||||||
<div className="space-y-1 mt-1">
|
|
||||||
<p className="text-sm">用戶答案: <span className="font-bold">{question.userAnswer}</span></p>
|
|
||||||
<p className="text-sm">正確答案: <span className="font-bold text-green-600">{question.correctAnswer}</span></p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{question.explanation && (
|
|
||||||
<div>
|
|
||||||
<label className="text-sm font-medium text-muted-foreground">解釋</label>
|
|
||||||
<p className="text-sm mt-1">{question.explanation}</p>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{question.type === 'creative' && (
|
|
||||||
<div className="grid grid-cols-2 gap-4">
|
|
||||||
<div>
|
|
||||||
<label className="text-sm font-medium text-muted-foreground">用戶答案</label>
|
|
||||||
<p className="text-sm mt-1">{question.userAnswer}</p>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label className="text-sm font-medium text-muted-foreground">得分</label>
|
|
||||||
<p className="text-sm mt-1 font-bold">{question.score} 分</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
</div>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
@@ -111,41 +111,7 @@ export async function GET(request: NextRequest) {
|
|||||||
|
|
||||||
console.log('Debug: 總共找到題目數量:', questions.length)
|
console.log('Debug: 總共找到題目數量:', questions.length)
|
||||||
|
|
||||||
// 如果 breakdown 中沒有詳細答案,嘗試從單獨的答案表獲取
|
// 綜合測試只從 breakdown 中獲取題目,不重複從答案表獲取
|
||||||
if (questions.length === 0) {
|
|
||||||
console.log('從 breakdown 中沒有找到答案,嘗試從答案表獲取...')
|
|
||||||
const logicAnswers = await getLogicTestAnswersByTestResultId(testResultId)
|
|
||||||
const creativeAnswers = await getCreativeTestAnswersByTestResultId(testResultId)
|
|
||||||
|
|
||||||
// 處理邏輯題答案
|
|
||||||
for (const answer of logicAnswers) {
|
|
||||||
const question = await findLogicQuestionById(answer.question_id)
|
|
||||||
if (question) {
|
|
||||||
questions.push({
|
|
||||||
...question,
|
|
||||||
type: 'logic',
|
|
||||||
userAnswer: answer.user_answer,
|
|
||||||
isCorrect: answer.is_correct,
|
|
||||||
correctAnswer: answer.correct_answer,
|
|
||||||
explanation: answer.explanation
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 處理創意題答案
|
|
||||||
for (const answer of creativeAnswers) {
|
|
||||||
const question = await findCreativeQuestionById(answer.question_id)
|
|
||||||
if (question) {
|
|
||||||
questions.push({
|
|
||||||
...question,
|
|
||||||
type: 'creative',
|
|
||||||
userAnswer: answer.user_answer,
|
|
||||||
score: answer.score,
|
|
||||||
isReverse: answer.is_reverse
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const testResult = await getTestResultById(testResultId)
|
const testResult = await getTestResultById(testResultId)
|
||||||
@@ -160,37 +126,109 @@ export async function GET(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 獲取詳細答案
|
// 獲取詳細答案
|
||||||
if (testType === "logic") {
|
try {
|
||||||
answers = await getLogicTestAnswersByTestResultId(testResultId)
|
if (testType === "logic") {
|
||||||
console.log('Debug: 邏輯測試答案數量:', answers.length)
|
console.log('Debug: 查詢邏輯測試答案,testResultId:', testResultId)
|
||||||
// 獲取對應的題目
|
|
||||||
for (const answer of answers) {
|
|
||||||
const question = await findLogicQuestionById(answer.question_id)
|
// 先嘗試從 logic_test_answers 表獲取
|
||||||
if (question) {
|
const logicAnswersQuery = `
|
||||||
questions.push({
|
SELECT lta.*, lq.question, lq.option_a, lq.option_b, lq.option_c, lq.option_d, lq.option_e,
|
||||||
...question,
|
lq.correct_answer, lq.explanation
|
||||||
userAnswer: answer.user_answer,
|
FROM logic_test_answers lta
|
||||||
isCorrect: answer.is_correct,
|
LEFT JOIN logic_questions lq ON lta.question_id = lq.id
|
||||||
correctAnswer: answer.correct_answer,
|
WHERE lta.test_result_id = ?
|
||||||
explanation: answer.explanation
|
ORDER BY lta.created_at ASC
|
||||||
})
|
`
|
||||||
}
|
const logicAnswers = await executeQuery(logicAnswersQuery, [testResultId])
|
||||||
}
|
console.log('Debug: 邏輯測試答案數量:', logicAnswers.length)
|
||||||
} else if (testType === "creative") {
|
|
||||||
answers = await getCreativeTestAnswersByTestResultId(testResultId)
|
if (logicAnswers.length > 0) {
|
||||||
console.log('Debug: 創意測試答案數量:', answers.length)
|
// 處理邏輯題答案
|
||||||
// 獲取對應的題目
|
for (const answer of logicAnswers) {
|
||||||
for (const answer of answers) {
|
if (answer.question) {
|
||||||
const question = await findCreativeQuestionById(answer.question_id)
|
questions.push({
|
||||||
if (question) {
|
id: answer.question_id,
|
||||||
questions.push({
|
question: answer.question,
|
||||||
...question,
|
option_a: answer.option_a,
|
||||||
userAnswer: answer.user_answer,
|
option_b: answer.option_b,
|
||||||
score: answer.score,
|
option_c: answer.option_c,
|
||||||
isReverse: answer.is_reverse
|
option_d: answer.option_d,
|
||||||
})
|
option_e: answer.option_e,
|
||||||
|
correct_answer: answer.correct_answer,
|
||||||
|
explanation: answer.explanation,
|
||||||
|
type: 'logic',
|
||||||
|
userAnswer: answer.user_answer,
|
||||||
|
isCorrect: answer.is_correct,
|
||||||
|
correctAnswer: answer.correct_answer
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 如果沒有找到答案,只顯示題目不顯示假答案
|
||||||
|
console.log('Debug: 沒有找到邏輯答案,只顯示題目')
|
||||||
|
const allLogicQuestions = await executeQuery('SELECT * FROM logic_questions ORDER BY id')
|
||||||
|
|
||||||
|
for (let i = 0; i < allLogicQuestions.length; i++) {
|
||||||
|
const question = allLogicQuestions[i]
|
||||||
|
questions.push({
|
||||||
|
...question,
|
||||||
|
type: 'logic',
|
||||||
|
userAnswer: null, // 不顯示假答案
|
||||||
|
isCorrect: null, // 不顯示假結果
|
||||||
|
correctAnswer: question.correct_answer
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (testType === "creative") {
|
||||||
|
console.log('Debug: 查詢創意測試答案,testResultId:', testResultId)
|
||||||
|
|
||||||
|
// 先嘗試從 creative_test_answers 表獲取
|
||||||
|
const creativeAnswersQuery = `
|
||||||
|
SELECT cta.*, cq.statement, cq.is_reverse
|
||||||
|
FROM creative_test_answers cta
|
||||||
|
LEFT JOIN creative_questions cq ON cta.question_id = cq.id
|
||||||
|
WHERE cta.test_result_id = ?
|
||||||
|
ORDER BY cta.created_at ASC
|
||||||
|
`
|
||||||
|
const creativeAnswers = await executeQuery(creativeAnswersQuery, [testResultId])
|
||||||
|
console.log('Debug: 創意測試答案數量:', creativeAnswers.length)
|
||||||
|
|
||||||
|
if (creativeAnswers.length > 0) {
|
||||||
|
// 處理創意題答案
|
||||||
|
for (const answer of creativeAnswers) {
|
||||||
|
if (answer.statement) {
|
||||||
|
questions.push({
|
||||||
|
id: answer.question_id,
|
||||||
|
statement: answer.statement,
|
||||||
|
is_reverse: answer.is_reverse,
|
||||||
|
type: 'creative',
|
||||||
|
userAnswer: answer.user_answer,
|
||||||
|
score: answer.score,
|
||||||
|
isReverse: answer.is_reverse
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 如果沒有找到答案,只顯示題目不顯示假答案
|
||||||
|
console.log('Debug: 沒有找到創意答案,只顯示題目')
|
||||||
|
const allCreativeQuestions = await executeQuery('SELECT * FROM creative_questions ORDER BY id')
|
||||||
|
|
||||||
|
for (let i = 0; i < allCreativeQuestions.length; i++) {
|
||||||
|
const question = allCreativeQuestions[i]
|
||||||
|
questions.push({
|
||||||
|
...question,
|
||||||
|
type: 'creative',
|
||||||
|
userAnswer: null, // 不顯示假答案
|
||||||
|
score: null, // 不顯示假分數
|
||||||
|
isReverse: question.is_reverse
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('獲取詳細答案失敗:', error)
|
||||||
|
// 如果獲取答案失敗,至少返回基本結果
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Debug: 單一測試類型題目數量:', questions.length)
|
console.log('Debug: 單一測試類型題目數量:', questions.length)
|
||||||
|
46
app/api/test-db-info/route.ts
Normal file
46
app/api/test-db-info/route.ts
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import { NextRequest, NextResponse } from "next/server"
|
||||||
|
import { executeQuery } from "@/lib/database/connection"
|
||||||
|
|
||||||
|
export async function GET(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
// 獲取資料庫信息
|
||||||
|
const dbInfo = await executeQuery('SELECT DATABASE() as current_db, USER() as current_user, VERSION() as version')
|
||||||
|
|
||||||
|
// 檢查所有表
|
||||||
|
const tables = await executeQuery('SHOW TABLES')
|
||||||
|
|
||||||
|
// 檢查 logic_test_answers 表
|
||||||
|
let logicAnswersCount = 0
|
||||||
|
let logicAnswersSample = []
|
||||||
|
|
||||||
|
try {
|
||||||
|
const countResult = await executeQuery('SELECT COUNT(*) as count FROM logic_test_answers')
|
||||||
|
logicAnswersCount = countResult[0].count
|
||||||
|
|
||||||
|
if (logicAnswersCount > 0) {
|
||||||
|
logicAnswersSample = await executeQuery('SELECT * FROM logic_test_answers LIMIT 3')
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('查詢 logic_test_answers 失敗:', error)
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
database: dbInfo[0],
|
||||||
|
tables: tables.map(t => Object.values(t)[0]),
|
||||||
|
logicTestAnswers: {
|
||||||
|
count: logicAnswersCount,
|
||||||
|
sample: logicAnswersSample
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('獲取資料庫信息失敗:', error)
|
||||||
|
return NextResponse.json(
|
||||||
|
{ success: false, message: "獲取資料庫信息失敗", error: error instanceof Error ? error.message : String(error) },
|
||||||
|
{ status: 500 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
38
scripts/check-logic-answers.js
Normal file
38
scripts/check-logic-answers.js
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
const { executeQuery } = require('../lib/database/connection');
|
||||||
|
|
||||||
|
async function checkLogicAnswers() {
|
||||||
|
try {
|
||||||
|
console.log('=== 檢查 logic_test_answers 表 ===');
|
||||||
|
const answers = await executeQuery('SELECT * FROM logic_test_answers');
|
||||||
|
console.log('logic_test_answers 資料總數:', answers.length);
|
||||||
|
if (answers.length > 0) {
|
||||||
|
console.log('前3筆資料:', answers.slice(0, 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('\n=== 檢查 test_results 表 ===');
|
||||||
|
const results = await executeQuery('SELECT * FROM test_results WHERE type = "logic" ORDER BY created_at DESC LIMIT 5');
|
||||||
|
console.log('logic test_results 資料總數:', results.length);
|
||||||
|
if (results.length > 0) {
|
||||||
|
console.log('前3筆資料:', results);
|
||||||
|
|
||||||
|
console.log('\n=== 檢查關聯資料 ===');
|
||||||
|
for (const result of results.slice(0, 2)) {
|
||||||
|
console.log(`\n檢查 test_result_id: ${result.id}`);
|
||||||
|
const relatedAnswers = await executeQuery('SELECT * FROM logic_test_answers WHERE test_result_id = ?', [result.id]);
|
||||||
|
console.log(`關聯的答案數量: ${relatedAnswers.length}`);
|
||||||
|
if (relatedAnswers.length > 0) {
|
||||||
|
console.log('答案資料:', relatedAnswers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('\n=== 檢查所有 test_results 類型 ===');
|
||||||
|
const allResults = await executeQuery('SELECT type, COUNT(*) as count FROM test_results GROUP BY type');
|
||||||
|
console.log('各類型測試結果數量:', allResults);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('錯誤:', error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checkLogicAnswers();
|
66
scripts/check-single-test-answers.js
Normal file
66
scripts/check-single-test-answers.js
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
const { executeQuery } = require('../lib/database/connection');
|
||||||
|
|
||||||
|
async function checkSingleTestAnswers() {
|
||||||
|
console.log('🔍 檢查單一測試類型的答案資料');
|
||||||
|
console.log('==============================');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 檢查 test_results 表
|
||||||
|
console.log('\n📋 Test Results:');
|
||||||
|
const testResults = await executeQuery('SELECT id, user_id, test_type, score, completed_at FROM test_results ORDER BY completed_at DESC LIMIT 5');
|
||||||
|
testResults.forEach((result, index) => {
|
||||||
|
console.log(`測試 ${index + 1}:`, {
|
||||||
|
id: result.id,
|
||||||
|
user_id: result.user_id,
|
||||||
|
test_type: result.test_type,
|
||||||
|
score: result.score,
|
||||||
|
completed_at: result.completed_at
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 檢查 logic_test_answers 表
|
||||||
|
console.log('\n📋 Logic Test Answers:');
|
||||||
|
const logicAnswers = await executeQuery('SELECT * FROM logic_test_answers ORDER BY created_at DESC LIMIT 5');
|
||||||
|
logicAnswers.forEach((answer, index) => {
|
||||||
|
console.log(`邏輯答案 ${index + 1}:`, {
|
||||||
|
id: answer.id,
|
||||||
|
test_result_id: answer.test_result_id,
|
||||||
|
question_id: answer.question_id,
|
||||||
|
user_answer: answer.user_answer,
|
||||||
|
is_correct: answer.is_correct
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 檢查 creative_test_answers 表
|
||||||
|
console.log('\n📋 Creative Test Answers:');
|
||||||
|
const creativeAnswers = await executeQuery('SELECT * FROM creative_test_answers ORDER BY created_at DESC LIMIT 5');
|
||||||
|
creativeAnswers.forEach((answer, index) => {
|
||||||
|
console.log(`創意答案 ${index + 1}:`, {
|
||||||
|
id: answer.id,
|
||||||
|
test_result_id: answer.test_result_id,
|
||||||
|
question_id: answer.question_id,
|
||||||
|
user_answer: answer.user_answer,
|
||||||
|
score: answer.score
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 檢查是否有匹配的答案
|
||||||
|
if (testResults.length > 0) {
|
||||||
|
const firstTest = testResults[0];
|
||||||
|
console.log(`\n🔍 檢查測試 ${firstTest.id} 的答案:`);
|
||||||
|
|
||||||
|
const matchingLogicAnswers = await executeQuery('SELECT * FROM logic_test_answers WHERE test_result_id = ?', [firstTest.id]);
|
||||||
|
console.log(`邏輯答案匹配數量: ${matchingLogicAnswers.length}`);
|
||||||
|
|
||||||
|
const matchingCreativeAnswers = await executeQuery('SELECT * FROM creative_test_answers WHERE test_result_id = ?', [firstTest.id]);
|
||||||
|
console.log(`創意答案匹配數量: ${matchingCreativeAnswers.length}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ 檢查失敗:', error.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('==============================\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
checkSingleTestAnswers();
|
53
scripts/test-db-connection.js
Normal file
53
scripts/test-db-connection.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
const { executeQuery } = require('../lib/database/connection');
|
||||||
|
|
||||||
|
async function testDbConnection() {
|
||||||
|
console.log('🔍 測試資料庫連接和表結構');
|
||||||
|
console.log('==============================');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 測試基本連接
|
||||||
|
console.log('1. 測試基本連接...');
|
||||||
|
const testQuery = await executeQuery('SELECT 1 as test');
|
||||||
|
console.log('✅ 資料庫連接成功:', testQuery);
|
||||||
|
|
||||||
|
// 檢查所有表
|
||||||
|
console.log('\n2. 檢查所有表...');
|
||||||
|
const tables = await executeQuery('SHOW TABLES');
|
||||||
|
console.log('📋 所有表:', tables.map(t => Object.values(t)[0]));
|
||||||
|
|
||||||
|
// 檢查 logic_test_answers 表是否存在
|
||||||
|
console.log('\n3. 檢查 logic_test_answers 表...');
|
||||||
|
const tableExists = await executeQuery(`
|
||||||
|
SELECT COUNT(*) as count
|
||||||
|
FROM information_schema.tables
|
||||||
|
WHERE table_schema = DATABASE()
|
||||||
|
AND table_name = 'logic_test_answers'
|
||||||
|
`);
|
||||||
|
console.log('logic_test_answers 表存在:', tableExists[0].count > 0);
|
||||||
|
|
||||||
|
// 檢查表結構
|
||||||
|
console.log('\n4. 檢查 logic_test_answers 表結構...');
|
||||||
|
const tableStructure = await executeQuery('DESCRIBE logic_test_answers');
|
||||||
|
console.log('📋 表結構:', tableStructure);
|
||||||
|
|
||||||
|
// 檢查資料數量
|
||||||
|
console.log('\n5. 檢查資料數量...');
|
||||||
|
const count = await executeQuery('SELECT COUNT(*) as count FROM logic_test_answers');
|
||||||
|
console.log('📊 logic_test_answers 資料數量:', count[0].count);
|
||||||
|
|
||||||
|
// 檢查前幾筆資料
|
||||||
|
if (count[0].count > 0) {
|
||||||
|
console.log('\n6. 檢查前 3 筆資料...');
|
||||||
|
const sampleData = await executeQuery('SELECT * FROM logic_test_answers LIMIT 3');
|
||||||
|
console.log('📋 範例資料:', sampleData);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ 測試失敗:', error.message);
|
||||||
|
console.error('錯誤詳情:', error);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('==============================\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
testDbConnection();
|
Reference in New Issue
Block a user