Update Saqel Platform: 2026-08-28 16:06:52

This commit is contained in:
Hamza-Ayed
2026-08-28 16:06:53 +03:00
parent e6fd319ff4
commit 39c1e14232
8 changed files with 365 additions and 37 deletions
@@ -10,7 +10,7 @@ use App\Services\CurriculumExtractorService;
class CurriculumController
{
/**
* Upload and Deep-Extract Real Ministry PDF Document
* Upload and Queue Real Ministry PDF Document for Deep Extraction
*/
public function uploadPdf(Request $request, Response $response): void
{
@@ -25,52 +25,63 @@ class CurriculumController
$file = $_FILES['pdf_file'];
$origName = $file['name'];
$tmpPath = $file['tmp_name'];
$taskId = uniqid('task_');
// Save original uploaded PDF in storage/uploads/curriculum
$uploadsDir = __DIR__ . '/../../storage/uploads/curriculum';
if (!is_dir($uploadsDir)) {
mkdir($uploadsDir, 0777, true);
// Setup processing directory
$processingDir = __DIR__ . '/../../storage/curriculum/processing';
if (!is_dir($processingDir)) {
mkdir($processingDir, 0777, true);
}
$savedPdfPath = $uploadsDir . '/' . time() . '_' . preg_replace('/[^A-Za-z0-9_\-\.]/u', '_', $origName);
$savedPdfPath = $processingDir . '/' . $taskId . '.pdf';
move_uploaded_file($tmpPath, $savedPdfPath);
// Perform Deep Multi-Engine Extraction & Structuring
$parsedStructure = CurriculumExtractorService::extractAndStructure($origName, $savedPdfPath);
// Initialize Task State
$taskState = [
'task_id' => $taskId,
'status' => 'queued',
'progress' => 0,
'message' => 'تم استلام الملف وجاري تحويله للمعالجة المعمقة...',
'orig_name' => $origName
];
file_put_contents($processingDir . '/' . $taskId . '.json', json_encode($taskState, JSON_UNESCAPED_UNICODE));
// Merge into live tree and save to disk
$updatedTree = CurriculumService::mergeExtractedCurriculum($parsedStructure);
$firstLessonFile = '';
$firstLessonMd = '';
$firstLessonTitle = '';
$firstLessonOutcomes = [];
$breadcrumb = '';
if (!empty($parsedStructure['units'][0]['lessons'][0])) {
$firstLes = $parsedStructure['units'][0]['lessons'][0];
$firstLessonFile = "{$parsedStructure['grade_key']}/{$parsedStructure['subject_key']}/{$parsedStructure['semester_key']}/{$parsedStructure['units'][0]['unit_key']}/{$firstLes['lesson_id']}.md";
$firstLessonMd = CurriculumService::getLessonMarkdown($firstLessonFile);
$firstLessonTitle = $firstLes['title'];
$firstLessonOutcomes = $firstLes['outcomes'] ?? [];
$breadcrumb = "{$parsedStructure['grade_name']} ⟵ {$parsedStructure['subject_name']} ⟵ {$parsedStructure['units'][0]['unit_name']}";
}
$serverStorageDir = realpath(__DIR__ . '/../../storage/curriculum');
// Launch Background Worker
$scriptPath = realpath(__DIR__ . '/../../scripts/curriculum_worker.php');
$logPath = $processingDir . '/' . $taskId . '.log';
$cmd = "php " . escapeshellarg($scriptPath) . " " . escapeshellarg($taskId) . " > " . escapeshellarg($logPath) . " 2>&1 &";
exec($cmd);
$response->json([
'status' => 'success',
'message' => "تم تفريغ وفهرسة المنهاج [{$origName}] بنجاح في قاعدة المعرفة!",
'extracted_data' => $parsedStructure,
'tree' => $updatedTree,
'active_file' => $firstLessonFile,
'active_md' => $firstLessonMd,
'active_title' => $firstLessonTitle,
'active_outcomes' => $firstLessonOutcomes,
'active_breadcrumb' => $breadcrumb,
'server_storage_dir' => $serverStorageDir
'status' => 'processing',
'task_id' => $taskId,
'message' => 'تم استلام الكتاب، وجاري المعالجة المعمقة بواسطة الذكاء الاصطناعي. يرجى الانتظار...'
]);
}
/**
* Poll Upload Status
*/
public function getUploadStatus(Request $request, Response $response): void
{
$taskId = $request->getQueryParams()['task_id'] ?? '';
if (empty($taskId)) {
$response->status(400)->json(['status' => 'error', 'message' => 'رقم المهمة مفقود']);
return;
}
$processingDir = __DIR__ . '/../../storage/curriculum/processing';
$stateFile = $processingDir . '/' . $taskId . '.json';
if (!file_exists($stateFile)) {
$response->status(404)->json(['status' => 'error', 'message' => 'المهمة غير موجودة']);
return;
}
$state = json_decode(file_get_contents($stateFile), true);
$response->json($state);
}
/**
* Get Complete Live Tree
*/