debug_kp_fallback.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. use App\Services\LearningAnalyticsService;
  3. use Illuminate\Support\Facades\Log;
  4. require __DIR__ . '/vendor/autoload.php';
  5. $app = require_once __DIR__ . '/bootstrap/app.php';
  6. $kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
  7. $kernel->bootstrap();
  8. $service = app(LearningAnalyticsService::class);
  9. echo "Fetching Knowledge Points from Learning Analytics Service...\n";
  10. $kps = $service->getKnowledgePoints();
  11. echo "Total KPs fetched from LA: " . count($kps) . "\n";
  12. if (empty($kps)) {
  13. echo "LA Service returned empty. Trying Knowledge Graph API directly...\n";
  14. $kgBaseUrl = config('services.knowledge_api.base_url', 'http://localhost:5011');
  15. echo "KG Base URL: $kgBaseUrl\n";
  16. try {
  17. echo "Testing KG API with phase=初中...\n";
  18. $response = \Illuminate\Support\Facades\Http::timeout(5)->get($kgBaseUrl . '/knowledge-points/', ['phase' => '初中']);
  19. if ($response->successful()) {
  20. $kps = $response->json()['data'] ?? [];
  21. echo "KPs with phase=初中: " . count($kps) . "\n";
  22. }
  23. echo "Testing KG API with grade=七年级...\n";
  24. $response = \Illuminate\Support\Facades\Http::timeout(5)->get($kgBaseUrl . '/knowledge-points/', ['grade' => '七年级']);
  25. if ($response->successful()) {
  26. $kps = $response->json()['data'] ?? [];
  27. echo "KPs with grade=七年级: " . count($kps) . "\n";
  28. }
  29. // Fallback to fetch all if filtering fails for now to continue script
  30. $response = \Illuminate\Support\Facades\Http::timeout(5)->get($kgBaseUrl . '/knowledge-points/');
  31. $kps = $response->json()['data'] ?? [];
  32. } catch (\Exception $e) {
  33. echo "KG API Exception: " . $e->getMessage() . "\n";
  34. }
  35. }
  36. if (!empty($kps)) {
  37. echo "First KP structure:\n";
  38. print_r($kps[0]);
  39. }
  40. $studentId = 'stu_1762395159_4';
  41. echo "\nSimulating filtering for student ID: $studentId\n";
  42. $student = \App\Models\Student::find($studentId);
  43. if ($student) {
  44. echo "Student found: " . $student->name . ", Grade: " . $student->grade . "\n";
  45. $grade = $student->grade;
  46. $filteredKps = array_filter($kps, function($kp) use ($grade) {
  47. $book = $kp['book'] ?? '';
  48. // Add Junior High logic
  49. if (str_contains($grade, '初一') || str_contains($grade, '七年级')) {
  50. return str_contains($book, '七年级');
  51. } elseif (str_contains($grade, '初二') || str_contains($grade, '八年级')) {
  52. return str_contains($book, '八年级');
  53. } elseif (str_contains($grade, '初三') || str_contains($grade, '九年级')) {
  54. return str_contains($book, '九年级');
  55. }
  56. if (str_contains($grade, '高一')) {
  57. return str_contains($book, '必修') || str_contains($book, '第一册') || str_contains($book, '第二册');
  58. } elseif (str_contains($grade, '高二')) {
  59. return str_contains($book, '选择性必修') || str_contains($book, '第三册');
  60. }
  61. return true;
  62. });
  63. echo "Filtered KPs count: " . count($filteredKps) . "\n";
  64. if (!empty($filteredKps)) {
  65. echo "Sample filtered KP book: " . $filteredKps[array_key_first($filteredKps)]['book'] . "\n";
  66. }
  67. } else {
  68. echo "Student not found in local DB.\n";
  69. // Try to find ANY student to test
  70. $anyStudent = \App\Models\Student::first();
  71. if ($anyStudent) {
  72. echo "Testing with existing student: " . $anyStudent->name . " (" . $anyStudent->grade . ")\n";
  73. $grade = $anyStudent->grade;
  74. $filteredKps = array_filter($kps, function($kp) use ($grade) {
  75. $book = $kp['book'] ?? '';
  76. if (str_contains($grade, '高一')) {
  77. return str_contains($book, '必修') || str_contains($book, '第一册') || str_contains($book, '第二册');
  78. } elseif (str_contains($grade, '高二')) {
  79. return str_contains($book, '选择性必修') || str_contains($book, '第三册');
  80. }
  81. return true;
  82. });
  83. echo "Filtered KPs count: " . count($filteredKps) . "\n";
  84. }
  85. }