實作所有測驗資料的答題詳細內容
This commit is contained in:
@@ -6,6 +6,7 @@ import { getCombinedTestResultById } from "@/lib/database/models/combined_test_r
|
||||
import { findUserById } from "@/lib/database/models/user"
|
||||
import { findLogicQuestionById } from "@/lib/database/models/logic_question"
|
||||
import { findCreativeQuestionById } from "@/lib/database/models/creative_question"
|
||||
import { executeQuery } from "@/lib/database/connection"
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
@@ -45,44 +46,106 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
// 獲取綜合測試的詳細答題資料
|
||||
// 從 logic_breakdown 中獲取邏輯題答案
|
||||
// 綜合測試的詳細答案存儲在 logic_breakdown 和 creativity_breakdown 中
|
||||
console.log('Debug: combinedResult.logic_breakdown:', combinedResult.logic_breakdown)
|
||||
console.log('Debug: combinedResult.creativity_breakdown:', combinedResult.creativity_breakdown)
|
||||
|
||||
if (combinedResult.logic_breakdown && typeof combinedResult.logic_breakdown === 'object') {
|
||||
const logicBreakdown = combinedResult.logic_breakdown as any
|
||||
if (logicBreakdown.answers && Array.isArray(logicBreakdown.answers)) {
|
||||
for (const answer of logicBreakdown.answers) {
|
||||
const question = await findLogicQuestionById(answer.questionId)
|
||||
console.log('Debug: logicBreakdown.answers:', logicBreakdown.answers)
|
||||
if (logicBreakdown.answers && typeof logicBreakdown.answers === 'object') {
|
||||
// 處理物件格式的答案
|
||||
const answerEntries = Object.entries(logicBreakdown.answers)
|
||||
console.log('Debug: 邏輯題答案條目:', answerEntries)
|
||||
|
||||
// 獲取所有邏輯題目
|
||||
const allLogicQuestions = await executeQuery('SELECT * FROM logic_questions ORDER BY id')
|
||||
console.log('Debug: 所有邏輯題目數量:', allLogicQuestions.length)
|
||||
|
||||
for (let i = 0; i < answerEntries.length; i++) {
|
||||
const [questionIndex, userAnswer] = answerEntries[i]
|
||||
const question = allLogicQuestions[parseInt(questionIndex)] // 使用索引獲取題目
|
||||
if (question) {
|
||||
// 判斷是否正確
|
||||
const isCorrect = userAnswer === question.correct_answer
|
||||
questions.push({
|
||||
...question,
|
||||
type: 'logic',
|
||||
userAnswer: answer.userAnswer,
|
||||
isCorrect: answer.isCorrect,
|
||||
correctAnswer: answer.correctAnswer,
|
||||
explanation: answer.explanation
|
||||
userAnswer: userAnswer as string,
|
||||
isCorrect: isCorrect,
|
||||
correctAnswer: question.correct_answer,
|
||||
explanation: question.explanation
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 從 creativity_breakdown 中獲取創意題答案
|
||||
if (combinedResult.creativity_breakdown && typeof combinedResult.creativity_breakdown === 'object') {
|
||||
const creativityBreakdown = combinedResult.creativity_breakdown as any
|
||||
if (creativityBreakdown.answers && Array.isArray(creativityBreakdown.answers)) {
|
||||
for (const answer of creativityBreakdown.answers) {
|
||||
const question = await findCreativeQuestionById(answer.questionId)
|
||||
console.log('Debug: creativityBreakdown.answers:', creativityBreakdown.answers)
|
||||
if (creativityBreakdown.answers && typeof creativityBreakdown.answers === 'object') {
|
||||
// 處理物件格式的答案
|
||||
const answerEntries = Object.entries(creativityBreakdown.answers)
|
||||
console.log('Debug: 創意題答案條目:', answerEntries)
|
||||
|
||||
// 獲取所有創意題目
|
||||
const allCreativeQuestions = await executeQuery('SELECT * FROM creative_questions ORDER BY id')
|
||||
console.log('Debug: 所有創意題目數量:', allCreativeQuestions.length)
|
||||
|
||||
for (let i = 0; i < answerEntries.length; i++) {
|
||||
const [questionIndex, score] = answerEntries[i]
|
||||
const question = allCreativeQuestions[parseInt(questionIndex)] // 使用索引獲取題目
|
||||
if (question) {
|
||||
questions.push({
|
||||
...question,
|
||||
type: 'creative',
|
||||
userAnswer: answer.userAnswer,
|
||||
score: answer.score,
|
||||
isReverse: answer.isReverse
|
||||
userAnswer: score.toString(), // 創意題的答案就是分數
|
||||
score: score as number,
|
||||
isReverse: question.is_reverse
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Debug: 總共找到題目數量:', questions.length)
|
||||
|
||||
// 如果 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 {
|
||||
const testResult = await getTestResultById(testResultId)
|
||||
@@ -99,6 +162,7 @@ export async function GET(request: NextRequest) {
|
||||
// 獲取詳細答案
|
||||
if (testType === "logic") {
|
||||
answers = await getLogicTestAnswersByTestResultId(testResultId)
|
||||
console.log('Debug: 邏輯測試答案數量:', answers.length)
|
||||
// 獲取對應的題目
|
||||
for (const answer of answers) {
|
||||
const question = await findLogicQuestionById(answer.question_id)
|
||||
@@ -114,6 +178,7 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
} else if (testType === "creative") {
|
||||
answers = await getCreativeTestAnswersByTestResultId(testResultId)
|
||||
console.log('Debug: 創意測試答案數量:', answers.length)
|
||||
// 獲取對應的題目
|
||||
for (const answer of answers) {
|
||||
const question = await findCreativeQuestionById(answer.question_id)
|
||||
@@ -127,6 +192,8 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Debug: 單一測試類型題目數量:', questions.length)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,4 +221,4 @@ export async function GET(request: NextRequest) {
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user