diff --git a/backend/scripts/diagnose_tree.php b/backend/scripts/diagnose_tree.php new file mode 100644 index 0000000..cb5239d --- /dev/null +++ b/backend/scripts/diagnose_tree.php @@ -0,0 +1,98 @@ +getMessage() . "\n"; +} + +// 2. Storage manifest.json check +echo "\n2. Live manifest.json check:\n"; +$tree = CurriculumService::getCurriculumTree(); +if (empty($tree)) { + echo " ERROR: CurriculumService::getCurriculumTree() returned empty array!\n"; + echo " Check: backend/storage/curriculum/manifest.json\n"; +} else { + $firstLesson = $tree['grade_10']['subjects']['math_10']['semesters']['semester_1']['units']['unit_01']['lessons'][1] ?? null; + echo " Grade 10 math lesson 1 in manifest.json:\n"; + if ($firstLesson) { + echo " - id: " . ($firstLesson['id'] ?? '') . "\n"; + echo " - file: " . ($firstLesson['file'] ?? '') . "\n"; + } else { + echo " - Not found at expected key!\n"; + } +} + +// 3. Test Enrichment Logic directly +echo "\n3. Testing Tree Enrichment Logic:\n"; +try { + $approvedLessons = Database::select("SELECT id, uuid, source_manifest_path FROM curriculum_lessons WHERE source_status='approved'"); + $byPath = []; + foreach ($approvedLessons as $row) { + $p = (string)$row['source_manifest_path']; + $byPath[$p] = [ + 'curriculum_lesson_id' => (string)$row['uuid'], + 'has_video' => false, + ]; + } + echo " Mapped " . count($byPath) . " paths in \$byPath lookup table.\n"; + + $matchCount = 0; + foreach ($tree as &$grade) { + foreach (($grade['subjects'] ?? []) as &$subject) { + foreach (($subject['semesters'] ?? []) as &$semester) { + foreach (($semester['units'] ?? []) as &$unit) { + foreach (($unit['lessons'] ?? []) as &$lesson) { + $path = (string)($lesson['file'] ?? ''); + if (isset($byPath[$path])) { + $lesson = array_merge($lesson, $byPath[$path]); + $matchCount++; + } + } + } + } + } + } + unset($grade, $subject, $semester, $unit, $lesson); + + echo " Enriched matches in manifest.json tree: {$matchCount}\n"; + + // Check if first lesson got enriched + $sampleEnriched = $tree['grade_10']['subjects']['math_10']['semesters']['semester_1']['units']['unit_01']['lessons'][1] ?? null; + if (!empty($sampleEnriched['curriculum_lesson_id'])) { + echo "\n=== SUCCESS! Enrichment logic works perfectly in CLI! ===\n"; + echo "Sample enriched lesson:\n"; + print_r($sampleEnriched); + echo "\nNOTE: If curl returns empty while CLI works, PHP-FPM has cached the old bytecode in OPcache.\n"; + echo "Run: systemctl reload php8.3-fpm (or your server php-fpm service) to reload PHP-FPM.\n"; + } else { + echo "\n=== WARNING: Sample lesson did not get enriched. Checking sample path... ===\n"; + $samplePath = $sampleEnriched['file'] ?? 'none'; + echo "Sample path is: '{$samplePath}'\n"; + echo "Is set in \$byPath? " . (isset($byPath[$samplePath]) ? "YES" : "NO") . "\n"; + if (!isset($byPath[$samplePath])) { + echo "Top 5 paths in \$byPath:\n"; + print_r(array_slice(array_keys($byPath), 0, 5)); + } + } +} catch (Throwable $e) { + echo " EXCEPTION during enrichment: " . $e->getMessage() . "\n"; + echo $e->getTraceAsString() . "\n"; +}