debug_exam_flow.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. use App\Services\LearningAnalyticsService;
  3. use App\Services\QuestionBankService;
  4. use App\Models\Student;
  5. use Illuminate\Support\Facades\Log;
  6. require __DIR__ . '/vendor/autoload.php';
  7. $app = require_once __DIR__ . '/bootstrap/app.php';
  8. $kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
  9. $kernel->bootstrap();
  10. // Instantiate services
  11. $qbService = app(QuestionBankService::class);
  12. $laService = app(LearningAnalyticsService::class);
  13. echo "=== Starting Exam Generation Flow Debug ===\n";
  14. // 1. Test Student and Grade Standardization
  15. $studentId = 'stu_1762395159_4'; // Use the ID from the error
  16. echo "\n[1] Checking Student: $studentId\n";
  17. $student = Student::find($studentId);
  18. if (!$student) {
  19. echo "Error: Student not found! Using first available student.\n";
  20. $student = Student::first();
  21. if (!$student) {
  22. die("Error: No students found in database.\n");
  23. }
  24. $studentId = $student->student_id;
  25. }
  26. echo "Student Name: " . $student->name . "\n";
  27. echo "Original Grade: " . $student->grade . "\n";
  28. // Simulate Grade Standardization Logic
  29. $grade = $student->grade;
  30. $standardizedGrade = $grade;
  31. if ($grade === '初一') $standardizedGrade = '七年级';
  32. elseif ($grade === '初二') $standardizedGrade = '八年级';
  33. elseif ($grade === '初三') $standardizedGrade = '九年级';
  34. echo "Standardized Grade: $standardizedGrade\n";
  35. // 2. Test Knowledge Graph API Fetching
  36. echo "\n[2] Fetching Knowledge Points from KG API\n";
  37. $filters = [];
  38. if (str_contains($standardizedGrade, '初') || str_contains($standardizedGrade, '七年级') || str_contains($standardizedGrade, '八年级') || str_contains($standardizedGrade, '九年级')) {
  39. $filters['phase'] = '初中';
  40. } elseif (str_contains($standardizedGrade, '高')) {
  41. $filters['phase'] = '高中';
  42. }
  43. echo "Filters: " . json_encode($filters, JSON_UNESCAPED_UNICODE) . "\n";
  44. $kps = $laService->getKnowledgePoints($filters);
  45. echo "KPs Fetched: " . count($kps) . "\n";
  46. if (empty($kps)) {
  47. echo "Error: No KPs returned from KG API.\n";
  48. // Try without filters to see if API is working at all
  49. echo "Retrying without filters...\n";
  50. $allKps = $laService->getKnowledgePoints([]);
  51. echo "All KPs Fetched: " . count($allKps) . "\n";
  52. if (empty($allKps)) {
  53. die("Critical Error: KG API seems down or returning empty.\n");
  54. }
  55. } else {
  56. echo "Sample KP: " . json_encode($kps[0], JSON_UNESCAPED_UNICODE) . "\n";
  57. }
  58. // 3. Select Random KPs
  59. $kpCodes = [];
  60. if (!empty($kps)) {
  61. $kpKeys = array_column($kps, 'kp_code');
  62. if (empty($kpKeys)) $kpKeys = array_column($kps, 'code');
  63. if (!empty($kpKeys)) {
  64. $randomKeys = array_rand(array_flip($kpKeys), min(5, count($kpKeys)));
  65. $kpCodes = is_array($randomKeys) ? $randomKeys : [$randomKeys];
  66. }
  67. }
  68. echo "\n[3] Selected KP Codes: " . implode(', ', $kpCodes) . "\n";
  69. if (empty($kpCodes)) {
  70. die("Error: Could not select any KP codes.\n");
  71. }
  72. // 4. Test Question Bank API
  73. echo "\n[4] Querying Question Bank API\n";
  74. // Test 1: /questions/filter with kp_codes (Current implementation)
  75. echo "Test 1: /questions/filter with kp_codes=R01\n";
  76. $params1 = ['kp_codes' => 'R01', 'limit' => 10];
  77. $res1 = $qbService->filterQuestions($params1);
  78. echo "Result 1: " . (isset($res1['data']) ? count($res1['data']) : 'Error') . " questions.\n";
  79. // Test 2: /questions with kp_code (Legacy implementation)
  80. echo "Test 2: /questions with kp_code=R01\n";
  81. $params2 = ['kp_code' => 'R01', 'limit' => 10];
  82. $res2 = $qbService->listQuestions(1, 10, $params2);
  83. echo "Result 2: " . (isset($res2['data']) ? count($res2['data']) : 'Error') . " questions.\n";
  84. // Test 3: /questions with kp_codes (Maybe list supports plural?)
  85. echo "Test 3: /questions with kp_codes=R01\n";
  86. $params3 = ['kp_codes' => 'R01', 'limit' => 10];
  87. $res3 = $qbService->listQuestions(1, 10, $params3);
  88. echo "Result 3: " . (isset($res3['data']) ? count($res3['data']) : 'Error') . " questions.\n";
  89. echo "\n[5] Inspecting Question Bank Data\n";
  90. $allQuestions = $qbService->listQuestions(1, 10);
  91. if (isset($allQuestions['data']) && count($allQuestions['data']) > 0) {
  92. echo "Total Questions in Bank (approx): " . ($allQuestions['meta']['total'] ?? 'Unknown') . "\n";
  93. echo "Sample Question from Bank:\n";
  94. $sampleQ = $allQuestions['data'][0];
  95. echo "ID: " . ($sampleQ['id'] ?? 'N/A') . "\n";
  96. echo "KP Code: " . ($sampleQ['kp_code'] ?? $sampleQ['knowledge_point'] ?? 'N/A') . "\n";
  97. echo "Content: " . substr($sampleQ['content'] ?? '', 0, 50) . "...\n";
  98. } else {
  99. echo "Question Bank seems empty or listQuestions failed.\n";
  100. echo "Response: " . json_encode($allQuestions, JSON_UNESCAPED_UNICODE) . "\n";
  101. }
  102. echo "\n=== Debug Complete ===\n";