|
@@ -8,6 +8,7 @@ use App\Models\Paper;
|
|
|
use App\Models\PaperQuestion;
|
|
use App\Models\PaperQuestion;
|
|
|
use App\Models\Student;
|
|
use App\Models\Student;
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Request;
|
|
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\File;
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
@@ -27,7 +28,8 @@ class ExamPdfExportService
|
|
|
private readonly LearningAnalyticsService $learningAnalyticsService,
|
|
private readonly LearningAnalyticsService $learningAnalyticsService,
|
|
|
private readonly QuestionBankService $questionBankService,
|
|
private readonly QuestionBankService $questionBankService,
|
|
|
private readonly QuestionServiceApi $questionServiceApi,
|
|
private readonly QuestionServiceApi $questionServiceApi,
|
|
|
- private readonly PdfStorageService $pdfStorageService
|
|
|
|
|
|
|
+ private readonly PdfStorageService $pdfStorageService,
|
|
|
|
|
+ private readonly MasteryCalculator $masteryCalculator
|
|
|
) {}
|
|
) {}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -72,18 +74,49 @@ class ExamPdfExportService
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
|
|
+ // 【调试】打印输入参数
|
|
|
|
|
+ Log::info('ExamPdfExportService: 开始生成学情分析PDF', [
|
|
|
|
|
+ 'paper_id' => $paperId,
|
|
|
|
|
+ 'student_id' => $studentId,
|
|
|
|
|
+ 'record_id' => $recordId,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
// 构建分析数据
|
|
// 构建分析数据
|
|
|
$analysisData = $this->buildAnalysisData($paperId, $studentId);
|
|
$analysisData = $this->buildAnalysisData($paperId, $studentId);
|
|
|
if (!$analysisData) {
|
|
if (!$analysisData) {
|
|
|
|
|
+ Log::warning('ExamPdfExportService: buildAnalysisData返回空数据', [
|
|
|
|
|
+ 'paper_id' => $paperId,
|
|
|
|
|
+ 'student_id' => $studentId,
|
|
|
|
|
+ ]);
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ Log::info('ExamPdfExportService: buildAnalysisData返回数据', [
|
|
|
|
|
+ 'paper_id' => $paperId,
|
|
|
|
|
+ 'student_id' => $studentId,
|
|
|
|
|
+ 'analysisData_keys' => array_keys($analysisData),
|
|
|
|
|
+ 'mastery_count' => count($analysisData['mastery']['items'] ?? []),
|
|
|
|
|
+ 'questions_count' => count($analysisData['questions'] ?? []),
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
// 创建DTO
|
|
// 创建DTO
|
|
|
$dto = ExamAnalysisDataDto::fromArray($analysisData);
|
|
$dto = ExamAnalysisDataDto::fromArray($analysisData);
|
|
|
$payloadDto = ReportPayloadDto::fromExamAnalysisDataDto($dto);
|
|
$payloadDto = ReportPayloadDto::fromExamAnalysisDataDto($dto);
|
|
|
|
|
|
|
|
|
|
+ // 【调试】打印传给模板的数据
|
|
|
|
|
+ $templateData = $payloadDto->toArray();
|
|
|
|
|
+ Log::info('ExamPdfExportService: 传给模板的数据', [
|
|
|
|
|
+ 'paper' => $templateData['paper'] ?? null,
|
|
|
|
|
+ 'student' => $templateData['student'] ?? null,
|
|
|
|
|
+ 'mastery' => $templateData['mastery'] ?? null,
|
|
|
|
|
+ 'parent_mastery_levels' => $templateData['parent_mastery_levels'] ?? null, // 新增:检查父节点掌握度
|
|
|
|
|
+ 'questions_count' => count($templateData['questions'] ?? []),
|
|
|
|
|
+ 'insights_count' => count($templateData['question_insights'] ?? []),
|
|
|
|
|
+ 'recommendations_count' => count($templateData['recommendations'] ?? []),
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
// 渲染HTML
|
|
// 渲染HTML
|
|
|
- $html = view('exam-analysis.pdf-report', $payloadDto->toArray())->render();
|
|
|
|
|
|
|
+ $html = view('exam-analysis.pdf-report', $templateData)->render();
|
|
|
if (!$html) {
|
|
if (!$html) {
|
|
|
Log::error('ExamPdfExportService: 渲染HTML为空', ['paper_id' => $paperId]);
|
|
Log::error('ExamPdfExportService: 渲染HTML为空', ['paper_id' => $paperId]);
|
|
|
return null;
|
|
return null;
|
|
@@ -262,6 +295,13 @@ class ExamPdfExportService
|
|
|
*/
|
|
*/
|
|
|
private function buildAnalysisData(string $paperId, string $studentId): ?array
|
|
private function buildAnalysisData(string $paperId, string $studentId): ?array
|
|
|
{
|
|
{
|
|
|
|
|
+ // 【关键调试】确认方法被调用
|
|
|
|
|
+ Log::warning('ExamPdfExportService: buildAnalysisData方法被调用了!', [
|
|
|
|
|
+ 'paper_id' => $paperId,
|
|
|
|
|
+ 'student_id' => $studentId,
|
|
|
|
|
+ 'timestamp' => now()->toISOString()
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
$paper = Paper::with(['questions' => function ($query) {
|
|
$paper = Paper::with(['questions' => function ($query) {
|
|
|
$query->orderBy('question_number')->orderBy('id');
|
|
$query->orderBy('question_number')->orderBy('id');
|
|
|
}])->find($paperId);
|
|
}])->find($paperId);
|
|
@@ -284,6 +324,8 @@ class ExamPdfExportService
|
|
|
|
|
|
|
|
// 【修改】直接从本地数据库获取分析数据(不再调用API)
|
|
// 【修改】直接从本地数据库获取分析数据(不再调用API)
|
|
|
$analysisData = [];
|
|
$analysisData = [];
|
|
|
|
|
+
|
|
|
|
|
+ // 首先尝试从paper->analysis_id获取
|
|
|
if (!empty($paper->analysis_id)) {
|
|
if (!empty($paper->analysis_id)) {
|
|
|
Log::info('ExamPdfExportService: 从本地数据库获取试卷分析数据', [
|
|
Log::info('ExamPdfExportService: 从本地数据库获取试卷分析数据', [
|
|
|
'paper_id' => $paperId,
|
|
'paper_id' => $paperId,
|
|
@@ -298,39 +340,221 @@ class ExamPdfExportService
|
|
|
|
|
|
|
|
if ($analysisRecord && !empty($analysisRecord->analysis_data)) {
|
|
if ($analysisRecord && !empty($analysisRecord->analysis_data)) {
|
|
|
$analysisData = json_decode($analysisRecord->analysis_data, true);
|
|
$analysisData = json_decode($analysisRecord->analysis_data, true);
|
|
|
- Log::info('ExamPdfExportService: 成功获取本地分析数据', [
|
|
|
|
|
|
|
+ Log::info('ExamPdfExportService: 成功获取本地分析数据(通过analysis_id)', [
|
|
|
'data_size' => strlen($analysisRecord->analysis_data)
|
|
'data_size' => strlen($analysisRecord->analysis_data)
|
|
|
]);
|
|
]);
|
|
|
} else {
|
|
} else {
|
|
|
- Log::warning('ExamPdfExportService: 未找到本地分析数据,将使用空数据', [
|
|
|
|
|
|
|
+ Log::warning('ExamPdfExportService: 未找到本地分析数据,将尝试其他方式', [
|
|
|
'paper_id' => $paperId,
|
|
'paper_id' => $paperId,
|
|
|
'student_id' => $studentId,
|
|
'student_id' => $studentId,
|
|
|
'analysis_id' => $paper->analysis_id
|
|
'analysis_id' => $paper->analysis_id
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
- } else {
|
|
|
|
|
- Log::warning('ExamPdfExportService: 试卷无analysis_id,将使用空分析数据', [
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果没有analysis_id或未找到数据,直接从exam_analysis_results表查询
|
|
|
|
|
+ if (empty($analysisData)) {
|
|
|
|
|
+ Log::info('ExamPdfExportService: 直接从exam_analysis_results表查询分析数据', [
|
|
|
'paper_id' => $paperId,
|
|
'paper_id' => $paperId,
|
|
|
'student_id' => $studentId
|
|
'student_id' => $studentId
|
|
|
]);
|
|
]);
|
|
|
|
|
+
|
|
|
|
|
+ $analysisRecord = \DB::table('exam_analysis_results')
|
|
|
|
|
+ ->where('paper_id', $paperId)
|
|
|
|
|
+ ->where('student_id', $studentId)
|
|
|
|
|
+ ->first();
|
|
|
|
|
+
|
|
|
|
|
+ if ($analysisRecord && !empty($analysisRecord->analysis_data)) {
|
|
|
|
|
+ $analysisData = json_decode($analysisRecord->analysis_data, true);
|
|
|
|
|
+ Log::info('ExamPdfExportService: 成功获取本地分析数据(直接查询)', [
|
|
|
|
|
+ 'data_size' => strlen($analysisRecord->analysis_data),
|
|
|
|
|
+ 'question_count' => count($analysisData['question_analysis'] ?? [])
|
|
|
|
|
+ ]);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Log::warning('ExamPdfExportService: 未找到任何分析数据,将使用空数据', [
|
|
|
|
|
+ 'paper_id' => $paperId,
|
|
|
|
|
+ 'student_id' => $studentId
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 【修改】使用本地方法获取掌握度概览(替代API调用)
|
|
|
|
|
|
|
+ // 【修复】优先使用analysisData中的knowledge_point_analysis数据
|
|
|
$masteryData = [];
|
|
$masteryData = [];
|
|
|
- try {
|
|
|
|
|
- Log::info('ExamPdfExportService: 获取学生掌握度概览', [
|
|
|
|
|
- 'student_id' => $studentId
|
|
|
|
|
- ]);
|
|
|
|
|
- $masteryOverview = $this->learningAnalyticsService->getStudentMasteryOverview($studentId);
|
|
|
|
|
- $masteryData = $masteryOverview['details'] ?? [];
|
|
|
|
|
- Log::info('ExamPdfExportService: 成功获取掌握度数据', [
|
|
|
|
|
- 'count' => count($masteryData)
|
|
|
|
|
|
|
+ $parentMasteryLevels = []; // 新增:父节点掌握度数据
|
|
|
|
|
+ Log::info('ExamPdfExportService: 开始处理掌握度数据', [
|
|
|
|
|
+ 'student_id' => $studentId,
|
|
|
|
|
+ 'analysisData_keys' => array_keys($analysisData),
|
|
|
|
|
+ 'has_knowledge_point_analysis' => !empty($analysisData['knowledge_point_analysis']),
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ if (!empty($analysisData['knowledge_point_analysis'])) {
|
|
|
|
|
+ // 将knowledge_point_analysis转换为buildMasterySummary期望的格式
|
|
|
|
|
+ foreach ($analysisData['knowledge_point_analysis'] as $kp) {
|
|
|
|
|
+ $masteryData[] = [
|
|
|
|
|
+ 'kp_code' => $kp['kp_id'] ?? null,
|
|
|
|
|
+ 'kp_name' => $kp['kp_id'] ?? '未知知识点',
|
|
|
|
|
+ 'mastery_level' => $kp['mastery_level'] ?? 0,
|
|
|
|
|
+ 'mastery_change' => $kp['change'] ?? null,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 【修复】基于所有兄弟节点历史数据计算父节点掌握度,并获取掌握度变化
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 获取本次考试涉及的知识点代码列表
|
|
|
|
|
+ $examKpCodes = array_column($masteryData, 'kp_code');
|
|
|
|
|
+ Log::info('ExamPdfExportService: 本次考试涉及的知识点', [
|
|
|
|
|
+ 'count' => count($examKpCodes),
|
|
|
|
|
+ 'kp_codes' => $examKpCodes
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ // 获取上一个快照的数据(用于计算变化)
|
|
|
|
|
+ // 如果没有其他试卷的记录,使用同一试卷的上一次快照
|
|
|
|
|
+ $lastSnapshot = DB::connection('mysql')
|
|
|
|
|
+ ->table('knowledge_point_mastery_snapshots')
|
|
|
|
|
+ ->where('student_id', $studentId)
|
|
|
|
|
+ ->where('paper_id', $paper->paper_id)
|
|
|
|
|
+ ->where('snapshot_id', '!=', "snap_{$paper->paper_id}_" . date('YmdHis'))
|
|
|
|
|
+ ->latest('snapshot_time')
|
|
|
|
|
+ ->first();
|
|
|
|
|
+
|
|
|
|
|
+ $previousMasteryData = [];
|
|
|
|
|
+ if ($lastSnapshot) {
|
|
|
|
|
+ $previousMasteryJson = json_decode($lastSnapshot->mastery_data, true);
|
|
|
|
|
+ foreach ($previousMasteryJson as $kpCode => $data) {
|
|
|
|
|
+ $previousMasteryData[$kpCode] = [
|
|
|
|
|
+ 'current_mastery' => $data['current_mastery'] ?? 0,
|
|
|
|
|
+ 'previous_mastery' => $data['previous_mastery'] ?? null,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ Log::info('ExamPdfExportService: 获取到上一次快照数据', [
|
|
|
|
|
+ 'snapshot_time' => $lastSnapshot->snapshot_time,
|
|
|
|
|
+ 'kp_count' => count($previousMasteryData)
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 为当前知识点添加变化数据
|
|
|
|
|
+ foreach ($masteryData as &$item) {
|
|
|
|
|
+ $kpCode = $item['kp_code'];
|
|
|
|
|
+ if (isset($previousMasteryData[$kpCode])) {
|
|
|
|
|
+ $previous = floatval($previousMasteryData[$kpCode]['previous_mastery'] ?? 0);
|
|
|
|
|
+ $current = floatval($item['mastery_level']);
|
|
|
|
|
+ $item['mastery_change'] = $current - $previous;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ unset($item); // 解除引用
|
|
|
|
|
+
|
|
|
|
|
+ // 获取所有父节点掌握度
|
|
|
|
|
+ $masteryOverview = $this->masteryCalculator->getStudentMasteryOverviewWithHierarchy($studentId);
|
|
|
|
|
+ $allParentMasteryLevels = $masteryOverview['parent_mastery_levels'] ?? [];
|
|
|
|
|
+
|
|
|
|
|
+ // 计算与本次考试相关的父节点掌握度(基于所有兄弟节点)
|
|
|
|
|
+ $parentMasteryLevels = [];
|
|
|
|
|
+ foreach ($allParentMasteryLevels as $parentKpCode => $parentMastery) {
|
|
|
|
|
+ // 检查这个父节点是否有子节点在本次考试中出现
|
|
|
|
|
+ $hasRelevantChild = false;
|
|
|
|
|
+ foreach ($examKpCodes as $childKpCode) {
|
|
|
|
|
+ if (str_starts_with($childKpCode, $parentKpCode)) {
|
|
|
|
|
+ $hasRelevantChild = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($hasRelevantChild) {
|
|
|
|
|
+ // 【修复】计算父节点变化:基于所有子节点的平均变化
|
|
|
|
|
+ $childChanges = [];
|
|
|
|
|
+ foreach ($examKpCodes as $childKpCode) {
|
|
|
|
|
+ if (str_starts_with($childKpCode, $parentKpCode)) {
|
|
|
|
|
+ $previousChild = $previousMasteryData[$childKpCode]['previous_mastery'] ?? null;
|
|
|
|
|
+ $currentChild = null;
|
|
|
|
|
+ foreach ($masteryData as $item) {
|
|
|
|
|
+ if ($item['kp_code'] === $childKpCode) {
|
|
|
|
|
+ $currentChild = $item['mastery_level'];
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($previousChild !== null && $currentChild !== null) {
|
|
|
|
|
+ $childChanges[] = floatval($currentChild) - floatval($previousChild);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $avgChange = !empty($childChanges) ? array_sum($childChanges) / count($childChanges) : null;
|
|
|
|
|
+
|
|
|
|
|
+ $parentMasteryLevels[$parentKpCode] = [
|
|
|
|
|
+ 'mastery_level' => $parentMastery,
|
|
|
|
|
+ 'mastery_change' => $avgChange,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Log::info('ExamPdfExportService: 过滤后的父节点掌握度', [
|
|
|
|
|
+ 'all_parent_count' => count($allParentMasteryLevels),
|
|
|
|
|
+ 'filtered_parent_count' => count($parentMasteryLevels),
|
|
|
|
|
+ 'filtered_codes' => array_keys($parentMasteryLevels)
|
|
|
|
|
+ ]);
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ Log::warning('ExamPdfExportService: 获取父节点掌握度失败', [
|
|
|
|
|
+ 'error' => $e->getMessage()
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Log::info('ExamPdfExportService: 使用analysisData中的掌握度数据', [
|
|
|
|
|
+ 'count' => count($masteryData),
|
|
|
|
|
+ 'masteryData_sample' => !empty($masteryData) ? array_slice($masteryData, 0, 2) : []
|
|
|
]);
|
|
]);
|
|
|
- } catch (\Exception $e) {
|
|
|
|
|
- Log::error('ExamPdfExportService: 获取掌握度数据失败', [
|
|
|
|
|
- 'student_id' => $studentId,
|
|
|
|
|
- 'error' => $e->getMessage()
|
|
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 如果没有knowledge_point_analysis,使用MasteryCalculator获取多层级掌握度概览
|
|
|
|
|
+ try {
|
|
|
|
|
+ Log::info('ExamPdfExportService: 获取学生多层级掌握度概览', [
|
|
|
|
|
+ 'student_id' => $studentId
|
|
|
|
|
+ ]);
|
|
|
|
|
+ $masteryOverview = $this->masteryCalculator->getStudentMasteryOverviewWithHierarchy($studentId);
|
|
|
|
|
+ $masteryData = $masteryOverview['details'] ?? [];
|
|
|
|
|
+ $parentMasteryLevels = $masteryOverview['parent_mastery_levels'] ?? []; // 获取父节点掌握度
|
|
|
|
|
+
|
|
|
|
|
+ // 【修复】将对象数组转换为关联数组(避免 stdClass 对象访问错误)
|
|
|
|
|
+ if (!empty($masteryData) && is_array($masteryData)) {
|
|
|
|
|
+ $masteryData = array_map(function($item) {
|
|
|
|
|
+ if (is_object($item)) {
|
|
|
|
|
+ return [
|
|
|
|
|
+ 'kp_code' => $item->kp_code ?? null,
|
|
|
|
|
+ 'kp_name' => $item->kp_name ?? null,
|
|
|
|
|
+ 'mastery_level' => floatval($item->mastery_level ?? 0),
|
|
|
|
|
+ 'mastery_change' => $item->mastery_change !== null ? floatval($item->mastery_change) : null,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ return $item;
|
|
|
|
|
+ }, $masteryData);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 【修复】获取快照数据以计算掌握度变化
|
|
|
|
|
+ $lastSnapshot = DB::connection('mysql')
|
|
|
|
|
+ ->table('knowledge_point_mastery_snapshots')
|
|
|
|
|
+ ->where('student_id', $studentId)
|
|
|
|
|
+ ->latest('snapshot_time')
|
|
|
|
|
+ ->first();
|
|
|
|
|
+
|
|
|
|
|
+ if ($lastSnapshot) {
|
|
|
|
|
+ $previousMasteryJson = json_decode($lastSnapshot->mastery_data, true);
|
|
|
|
|
+ foreach ($masteryData as &$item) {
|
|
|
|
|
+ $kpCode = $item['kp_code'];
|
|
|
|
|
+ if (isset($previousMasteryJson[$kpCode])) {
|
|
|
|
|
+ $previous = floatval($previousMasteryJson[$kpCode]['previous_mastery'] ?? 0);
|
|
|
|
|
+ $current = floatval($item['mastery_level']);
|
|
|
|
|
+ $item['mastery_change'] = $current - $previous;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ unset($item);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Log::info('ExamPdfExportService: 成功获取多层级掌握度数据', [
|
|
|
|
|
+ 'count' => count($masteryData),
|
|
|
|
|
+ 'parent_count' => count($parentMasteryLevels)
|
|
|
]);
|
|
]);
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ Log::error('ExamPdfExportService: 获取掌握度数据失败', [
|
|
|
|
|
+ 'student_id' => $studentId,
|
|
|
|
|
+ 'error' => $e->getMessage()
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 【修改】使用本地方法获取学习路径推荐(替代API调用)
|
|
// 【修改】使用本地方法获取学习路径推荐(替代API调用)
|
|
@@ -353,13 +577,37 @@ class ExamPdfExportService
|
|
|
|
|
|
|
|
// 获取知识点名称映射
|
|
// 获取知识点名称映射
|
|
|
$kpNameMap = $this->buildKnowledgePointNameMap();
|
|
$kpNameMap = $this->buildKnowledgePointNameMap();
|
|
|
|
|
+ Log::info('ExamPdfExportService: 获取知识点名称映射', [
|
|
|
|
|
+ 'kpNameMap_count' => count($kpNameMap),
|
|
|
|
|
+ 'kpNameMap_keys_sample' => !empty($kpNameMap) ? array_slice(array_keys($kpNameMap), 0, 5) : []
|
|
|
|
|
+ ]);
|
|
|
|
|
|
|
|
- // 获取题目详情
|
|
|
|
|
- $questionDetails = $this->getQuestionDetailsFromPaper($paper);
|
|
|
|
|
|
|
+ // 【修复】直接从MySQL数据库获取题目详情(不通过API)
|
|
|
|
|
+ $questionDetails = $this->getQuestionDetailsFromMySQL($paper);
|
|
|
|
|
|
|
|
// 处理题目数据
|
|
// 处理题目数据
|
|
|
$questions = $this->processQuestionsForReport($paper, $questionDetails, $kpNameMap);
|
|
$questions = $this->processQuestionsForReport($paper, $questionDetails, $kpNameMap);
|
|
|
|
|
|
|
|
|
|
+ // 【关键调试】查看buildMasterySummary的返回结果
|
|
|
|
|
+ $masterySummary = $this->buildMasterySummary($masteryData, $kpNameMap);
|
|
|
|
|
+ Log::info('ExamPdfExportService: buildMasterySummary返回结果', [
|
|
|
|
|
+ 'masteryData_count' => count($masteryData),
|
|
|
|
|
+ 'kpNameMap_count' => count($kpNameMap),
|
|
|
|
|
+ 'masterySummary_keys' => array_keys($masterySummary),
|
|
|
|
|
+ 'masterySummary_items_count' => count($masterySummary['items'] ?? []),
|
|
|
|
|
+ 'masterySummary_items_sample' => !empty($masterySummary['items']) ? array_slice($masterySummary['items'], 0, 2) : []
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ // 【修复】处理父节点掌握度数据:过滤零值、转换名称、构建层级关系
|
|
|
|
|
+ $examKpCodes = array_column($masteryData, 'kp_code'); // 本次考试涉及的知识点
|
|
|
|
|
+ $processedParentMastery = $this->processParentMasteryLevels($parentMasteryLevels, $kpNameMap, $examKpCodes);
|
|
|
|
|
+
|
|
|
|
|
+ Log::info('ExamPdfExportService: 处理后的父节点掌握度', [
|
|
|
|
|
+ 'raw_count' => count($parentMasteryLevels),
|
|
|
|
|
+ 'processed_count' => count($processedParentMastery),
|
|
|
|
|
+ 'processed_sample' => !empty($processedParentMastery) ? array_slice($processedParentMastery, 0, 3) : []
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
return [
|
|
return [
|
|
|
'paper' => [
|
|
'paper' => [
|
|
|
'id' => $paper->paper_id,
|
|
'id' => $paper->paper_id,
|
|
@@ -370,7 +618,8 @@ class ExamPdfExportService
|
|
|
],
|
|
],
|
|
|
'student' => $studentInfo,
|
|
'student' => $studentInfo,
|
|
|
'questions' => $questions,
|
|
'questions' => $questions,
|
|
|
- 'mastery' => $this->buildMasterySummary($masteryData, $kpNameMap),
|
|
|
|
|
|
|
+ 'mastery' => $masterySummary,
|
|
|
|
|
+ 'parent_mastery_levels' => $processedParentMastery, // 【修复】使用处理后的父节点数据
|
|
|
'insights' => $analysisData['question_analysis'] ?? [], // 使用question_analysis替代question_results
|
|
'insights' => $analysisData['question_analysis'] ?? [], // 使用question_analysis替代question_results
|
|
|
'recommendations' => $recommendations,
|
|
'recommendations' => $recommendations,
|
|
|
'analysis_data' => $analysisData,
|
|
'analysis_data' => $analysisData,
|
|
@@ -378,22 +627,39 @@ class ExamPdfExportService
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 获取题目详情
|
|
|
|
|
|
|
+ * 【修复】直接从PaperQuestion表获取题目详情(不通过API)
|
|
|
*/
|
|
*/
|
|
|
- private function getQuestionDetailsFromPaper(Paper $paper): array
|
|
|
|
|
|
|
+ private function getQuestionDetailsFromMySQL(Paper $paper): array
|
|
|
{
|
|
{
|
|
|
$details = [];
|
|
$details = [];
|
|
|
- $questionIds = $paper->questions->pluck('question_id')->filter()->unique()->values();
|
|
|
|
|
|
|
|
|
|
- foreach ($questionIds as $qid) {
|
|
|
|
|
|
|
+ Log::info('ExamPdfExportService: 从PaperQuestion表查询题目详情', [
|
|
|
|
|
+ 'paper_id' => $paper->paper_id,
|
|
|
|
|
+ 'question_count' => $paper->questions->count()
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($paper->questions as $pq) {
|
|
|
try {
|
|
try {
|
|
|
- $detail = $this->questionBankService->getQuestion((string) $qid);
|
|
|
|
|
- if (!empty($detail)) {
|
|
|
|
|
- $details[(string) $qid] = $detail;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 【关键修复】直接从PaperQuestion对象获取solution和correct_answer
|
|
|
|
|
+ $detail = [
|
|
|
|
|
+ 'id' => $pq->question_id,
|
|
|
|
|
+ 'content' => $pq->question_text,
|
|
|
|
|
+ 'question_type' => $pq->question_type,
|
|
|
|
|
+ 'answer' => $pq->correct_answer ?? null, // 【修复】从PaperQuestion获取正确答案
|
|
|
|
|
+ 'solution' => $pq->solution ?? null, // 【修复】从PaperQuestion获取解题思路
|
|
|
|
|
+ ];
|
|
|
|
|
+ $details[(string) ($pq->question_id ?? $pq->id)] = $detail;
|
|
|
|
|
+
|
|
|
|
|
+ Log::debug('ExamPdfExportService: 成功获取题目详情', [
|
|
|
|
|
+ 'paper_question_id' => $pq->id,
|
|
|
|
|
+ 'question_id' => $pq->question_id,
|
|
|
|
|
+ 'has_answer' => !empty($pq->correct_answer),
|
|
|
|
|
+ 'has_solution' => !empty($pq->solution),
|
|
|
|
|
+ 'answer_preview' => $pq->correct_answer ? substr($pq->correct_answer, 0, 50) : null
|
|
|
|
|
+ ]);
|
|
|
} catch (\Throwable $e) {
|
|
} catch (\Throwable $e) {
|
|
|
- Log::warning('ExamPdfExportService: 获取题目详情失败', [
|
|
|
|
|
- 'question_id' => $qid,
|
|
|
|
|
|
|
+ Log::error('ExamPdfExportService: 获取题目详情失败', [
|
|
|
|
|
+ 'paper_question_id' => $pq->id,
|
|
|
'error' => $e->getMessage(),
|
|
'error' => $e->getMessage(),
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
@@ -422,8 +688,12 @@ class ExamPdfExportService
|
|
|
foreach ($sortedQuestions as $idx => $question) {
|
|
foreach ($sortedQuestions as $idx => $question) {
|
|
|
$kpCode = $question->knowledge_point ?? '';
|
|
$kpCode = $question->knowledge_point ?? '';
|
|
|
$kpName = $kpNameMap[$kpCode] ?? $kpCode ?: '未标注';
|
|
$kpName = $kpNameMap[$kpCode] ?? $kpCode ?: '未标注';
|
|
|
- $detail = $questionDetails[(string) ($question->question_id ?? '')] ?? [];
|
|
|
|
|
- $solution = $detail['solution'] ?? $detail['解析'] ?? $detail['analysis'] ?? null;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 【修复】直接从PaperQuestion对象获取solution和correct_answer
|
|
|
|
|
+ $answer = $question->correct_answer ?? null; // 直接从PaperQuestion获取
|
|
|
|
|
+ $solution = $question->solution ?? null; // 直接从PaperQuestion获取
|
|
|
|
|
+
|
|
|
|
|
+ $detail = $questionDetails[(string) ($question->question_id ?? $question->id)] ?? [];
|
|
|
$typeRaw = $question->question_type ?? ($detail['question_type'] ?? $detail['type'] ?? '');
|
|
$typeRaw = $question->question_type ?? ($detail['question_type'] ?? $detail['type'] ?? '');
|
|
|
$normalizedType = $this->normalizeQuestionType($typeRaw);
|
|
$normalizedType = $this->normalizeQuestionType($typeRaw);
|
|
|
$number = $question->question_number ?? ($idx + 1);
|
|
$number = $question->question_number ?? ($idx + 1);
|
|
@@ -437,10 +707,24 @@ class ExamPdfExportService
|
|
|
'knowledge_point' => $kpCode,
|
|
'knowledge_point' => $kpCode,
|
|
|
'knowledge_point_name' => $kpName,
|
|
'knowledge_point_name' => $kpName,
|
|
|
'score' => $question->score,
|
|
'score' => $question->score,
|
|
|
- 'solution' => $solution,
|
|
|
|
|
|
|
+ 'answer' => $answer, // 正确答案
|
|
|
|
|
+ 'solution' => $solution, // 解题思路
|
|
|
|
|
+ 'student_answer' => $question->student_answer ?? null, // 【新增】学生答案
|
|
|
|
|
+ 'correct_answer' => $answer, // 【新增】正确答案
|
|
|
|
|
+ 'is_correct' => $question->is_correct ?? null, // 【新增】判分结果
|
|
|
|
|
+ 'score_obtained' => $question->score_obtained ?? null, // 【新增】得分
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
$grouped[$normalizedType][] = $payload;
|
|
$grouped[$normalizedType][] = $payload;
|
|
|
|
|
+
|
|
|
|
|
+ // 【调试】记录题目数据
|
|
|
|
|
+ Log::debug('ExamPdfExportService: 处理题目数据', [
|
|
|
|
|
+ 'paper_question_id' => $question->id,
|
|
|
|
|
+ 'question_id' => $question->question_id,
|
|
|
|
|
+ 'has_answer' => !empty($answer),
|
|
|
|
|
+ 'has_solution' => !empty($solution),
|
|
|
|
|
+ 'answer_preview' => $answer ? substr($answer, 0, 50) : null
|
|
|
|
|
+ ]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$ordered = array_merge($grouped['choice'], $grouped['fill'], $grouped['answer']);
|
|
$ordered = array_merge($grouped['choice'], $grouped['fill'], $grouped['answer']);
|
|
@@ -662,18 +946,20 @@ class ExamPdfExportService
|
|
|
*/
|
|
*/
|
|
|
private function buildMasterySummary(array $masteryData, array $kpNameMap): array
|
|
private function buildMasterySummary(array $masteryData, array $kpNameMap): array
|
|
|
{
|
|
{
|
|
|
|
|
+ Log::info('ExamPdfExportService: buildMasterySummary开始处理', [
|
|
|
|
|
+ 'masteryData_count' => count($masteryData),
|
|
|
|
|
+ 'kpNameMap_count' => count($kpNameMap)
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
$items = [];
|
|
$items = [];
|
|
|
$total = 0;
|
|
$total = 0;
|
|
|
$count = 0;
|
|
$count = 0;
|
|
|
- $hasMap = !empty($kpNameMap);
|
|
|
|
|
|
|
|
|
|
foreach ($masteryData as $row) {
|
|
foreach ($masteryData as $row) {
|
|
|
$code = $row['kp_code'] ?? null;
|
|
$code = $row['kp_code'] ?? null;
|
|
|
- if ($hasMap && $code && !isset($kpNameMap[$code])) {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- $name = $row['kp_name'] ?? ($code ? ($kpNameMap[$code] ?? $code) : '未知知识点');
|
|
|
|
|
- $level = (float) ($row['mastery_level'] ?? 0);
|
|
|
|
|
|
|
+ // 【修复】使用kpNameMap转换名称为友好显示名
|
|
|
|
|
+ $name = $kpNameMap[$code] ?? $row['kp_name'] ?? $code ?: '未知知识点';
|
|
|
|
|
+ $level = (float)($row['mastery_level'] ?? 0);
|
|
|
$delta = $row['mastery_change'] ?? null;
|
|
$delta = $row['mastery_change'] ?? null;
|
|
|
|
|
|
|
|
$items[] = [
|
|
$items[] = [
|
|
@@ -690,13 +976,22 @@ class ExamPdfExportService
|
|
|
$average = $count > 0 ? round($total / $count, 2) : null;
|
|
$average = $count > 0 ? round($total / $count, 2) : null;
|
|
|
|
|
|
|
|
// 按掌握度从低到高排序
|
|
// 按掌握度从低到高排序
|
|
|
- usort($items, fn($a, $b) => ($a['mastery_level'] <=> $b['mastery_level']));
|
|
|
|
|
|
|
+ if (!empty($items)) {
|
|
|
|
|
+ usort($items, fn($a, $b) => ($a['mastery_level'] <=> $b['mastery_level']));
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return [
|
|
|
|
|
|
|
+ $result = [
|
|
|
'items' => $items,
|
|
'items' => $items,
|
|
|
'average' => $average,
|
|
'average' => $average,
|
|
|
'weak_list' => array_slice($items, 0, 5),
|
|
'weak_list' => array_slice($items, 0, 5),
|
|
|
];
|
|
];
|
|
|
|
|
+
|
|
|
|
|
+ Log::info('ExamPdfExportService: buildMasterySummary完成', [
|
|
|
|
|
+ 'total_count' => $count,
|
|
|
|
|
+ 'items_count' => count($items)
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ return $result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -808,4 +1103,101 @@ class ExamPdfExportService
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 【修复】处理父节点掌握度数据
|
|
|
|
|
+ * 1. 过滤掉掌握度为0或null的父节点
|
|
|
|
|
+ * 2. 将kp_code转换为友好的kp_name
|
|
|
|
|
+ * 3. 构建父子层级关系(只显示本次考试相关的子节点)
|
|
|
|
|
+ */
|
|
|
|
|
+ private function processParentMasteryLevels(array $parentMasteryLevels, array $kpNameMap, array $examKpCodes = []): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $processed = [];
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($parentMasteryLevels as $kpCode => $masteryData) {
|
|
|
|
|
+ // 兼容不同数据结构:可能是数组或数字
|
|
|
|
|
+ $masteryLevel = is_array($masteryData) ? ($masteryData['mastery_level'] ?? 0) : $masteryData;
|
|
|
|
|
+ $masteryChange = is_array($masteryData) ? ($masteryData['mastery_change'] ?? null) : null;
|
|
|
|
|
+
|
|
|
|
|
+ // 过滤零值和空值
|
|
|
|
|
+ if ($masteryLevel === null || $masteryLevel === 0.0 || $masteryLevel <= 0.001) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取友好名称
|
|
|
|
|
+ $kpName = $kpNameMap[$kpCode] ?? $kpCode;
|
|
|
|
|
+
|
|
|
|
|
+ // 构建父节点数据,包含子节点信息(只显示本次考试相关的)
|
|
|
|
|
+ $processed[$kpCode] = [
|
|
|
|
|
+ 'kp_code' => $kpCode,
|
|
|
|
|
+ 'kp_name' => $kpName,
|
|
|
|
|
+ 'mastery_level' => round(floatval($masteryLevel), 4),
|
|
|
|
|
+ 'mastery_percentage' => round(floatval($masteryLevel) * 100, 2),
|
|
|
|
|
+ 'mastery_change' => $masteryChange !== null ? round(floatval($masteryChange), 4) : null,
|
|
|
|
|
+ // 【修复】只获取本次考试涉及的子节点
|
|
|
|
|
+ 'children' => $this->getChildKnowledgePoints($kpCode, $kpNameMap, $examKpCodes),
|
|
|
|
|
+ 'level' => $this->calculateKnowledgePointLevel($kpCode),
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 按掌握度降序排序
|
|
|
|
|
+ uasort($processed, function($a, $b) {
|
|
|
|
|
+ return $b['mastery_level'] <=> $a['mastery_level'];
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ return $processed;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 【修复】获取子知识点列表(只返回本次考试涉及的)
|
|
|
|
|
+ */
|
|
|
|
|
+ private function getChildKnowledgePoints(string $parentKpCode, array $kpNameMap, array $examKpCodes = []): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $children = [];
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ $childCodes = DB::connection('mysql')
|
|
|
|
|
+ ->table('knowledge_points')
|
|
|
|
|
+ ->where('parent_kp_code', $parentKpCode)
|
|
|
|
|
+ ->pluck('kp_code')
|
|
|
|
|
+ ->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($childCodes as $childCode) {
|
|
|
|
|
+ // 只包含本次考试涉及的知识点
|
|
|
|
|
+ if (in_array($childCode, $examKpCodes)) {
|
|
|
|
|
+ $children[] = [
|
|
|
|
|
+ 'kp_code' => $childCode,
|
|
|
|
|
+ 'kp_name' => $kpNameMap[$childCode] ?? $childCode,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ Log::warning('获取子知识点失败', [
|
|
|
|
|
+ 'parent_kp_code' => $parentKpCode,
|
|
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $children;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 计算知识点层级深度
|
|
|
|
|
+ */
|
|
|
|
|
+ private function calculateKnowledgePointLevel(string $kpCode): int
|
|
|
|
|
+ {
|
|
|
|
|
+ // 根据kp_code前缀判断层级深度
|
|
|
|
|
+ // 例如: M (1级) -> M01 (2级) -> M01A (3级)
|
|
|
|
|
+ if (preg_match('/^[A-Z]+$/', $kpCode)) {
|
|
|
|
|
+ return 1; // 一级分类,如 M, S, E, G
|
|
|
|
|
+ } elseif (preg_match('/^[A-Z]+\d+$/', $kpCode)) {
|
|
|
|
|
+ return 2; // 二级分类,如 M01, S02
|
|
|
|
|
+ } elseif (preg_match('/^[A-Z]+\d+[A-Z]+$/', $kpCode)) {
|
|
|
|
|
+ return 3; // 三级分类,如 M01A, S02B
|
|
|
|
|
+ } elseif (preg_match('/^[A-Z]+\d+[A-Z]+\d+$/', $kpCode)) {
|
|
|
|
|
+ return 4; // 四级分类,如 M01A1
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return 1; // 默认一级
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|