Files
saqel/backend/scripts/diagnose_tree.php
T

102 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
require_once dirname(__DIR__) . '/app/bootstrap.php';
use App\Core\Database;
use App\Services\CurriculumService;
echo "=== DIAGNOSING CURRICULUM TREE ENRICHMENT ===\n\n";
// 1. Database check
try {
$approvedCount = Database::selectOne("SELECT COUNT(*) as cnt FROM curriculum_lessons WHERE source_status = 'approved'");
echo "1. Database Status:\n";
echo " Approved curriculum lessons in DB: " . ($approvedCount['cnt'] ?? 0) . "\n";
$samples = Database::select("SELECT id, uuid, source_manifest_path, source_status FROM curriculum_lessons WHERE source_status = 'approved' LIMIT 3");
foreach ($samples as $s) {
echo " - [{$s['source_status']}] uuid: {$s['uuid']} | path: {$s['source_manifest_path']}\n";
}
} catch (Throwable $e) {
echo " ERROR querying curriculum_lessons: " . $e->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 $gKey => $grade) {
if (!isset($tree[$gKey]['subjects']) || !is_array($tree[$gKey]['subjects'])) continue;
foreach ($tree[$gKey]['subjects'] as $sKey => $subject) {
if (!isset($tree[$gKey]['subjects'][$sKey]['semesters']) || !is_array($tree[$gKey]['subjects'][$sKey]['semesters'])) continue;
foreach ($tree[$gKey]['subjects'][$sKey]['semesters'] as $semKey => $semester) {
if (!isset($tree[$gKey]['subjects'][$sKey]['semesters'][$semKey]['units']) || !is_array($tree[$gKey]['subjects'][$sKey]['semesters'][$semKey]['units'])) continue;
foreach ($tree[$gKey]['subjects'][$sKey]['semesters'][$semKey]['units'] as $uKey => $unit) {
if (!isset($tree[$gKey]['subjects'][$sKey]['semesters'][$semKey]['units'][$uKey]['lessons']) || !is_array($tree[$gKey]['subjects'][$sKey]['semesters'][$semKey]['units'][$uKey]['lessons'])) continue;
foreach ($tree[$gKey]['subjects'][$sKey]['semesters'][$semKey]['units'][$uKey]['lessons'] as $lKey => $lesson) {
$path = (string)($lesson['file'] ?? '');
if (isset($byPath[$path])) {
$tree[$gKey]['subjects'][$sKey]['semesters'][$semKey]['units'][$uKey]['lessons'][$lKey] = array_merge($lesson, $byPath[$path]);
$matchCount++;
}
}
}
}
}
}
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";
}