396 lines
16 KiB
PHP
396 lines
16 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Core\Database;
|
|
|
|
/**
|
|
* 100% Live Dynamic Curriculum Service (Zero Fake Data)
|
|
* Manages real uploaded textbook manifests and dynamic markdown files.
|
|
*/
|
|
class CurriculumService
|
|
{
|
|
/**
|
|
* Stub for backwards compatibility with controllers calling ensureSchema.
|
|
* Curriculum is now file-based.
|
|
*/
|
|
public static function ensureSchema(): void
|
|
{
|
|
// No-op (schema managed via database_schema.sql)
|
|
}
|
|
|
|
/**
|
|
* Runtime migration: add is_system_curriculum (and related columns) to `courses`
|
|
* if they are missing. Safe to call multiple times — uses IF NOT EXISTS / IGNORE.
|
|
*/
|
|
public static function ensureSystemCurriculumColumn(): void
|
|
{
|
|
static $ran = false;
|
|
if ($ran) return;
|
|
$ran = true;
|
|
|
|
try {
|
|
// Check whether the column exists to avoid noisy ALTER errors on every request
|
|
$row = Database::selectOne(
|
|
"SELECT COUNT(*) as cnt FROM information_schema.COLUMNS
|
|
WHERE TABLE_SCHEMA = DATABASE()
|
|
AND TABLE_NAME = 'courses'
|
|
AND COLUMN_NAME = 'is_system_curriculum'
|
|
LIMIT 1"
|
|
);
|
|
|
|
if (empty($row['cnt'])) {
|
|
Database::query(
|
|
"ALTER TABLE `courses`
|
|
ADD COLUMN `is_system_curriculum` TINYINT(1) NOT NULL DEFAULT 0
|
|
COMMENT 'الدورة الرئيسية للمنهاج الوزاري'
|
|
AFTER `is_school_exclusive`"
|
|
);
|
|
}
|
|
|
|
// Also ensure grade_level + stream columns exist (for student filtering)
|
|
$gradeRow = Database::selectOne(
|
|
"SELECT COUNT(*) as cnt FROM information_schema.COLUMNS
|
|
WHERE TABLE_SCHEMA = DATABASE()
|
|
AND TABLE_NAME = 'courses'
|
|
AND COLUMN_NAME = 'grade_level'
|
|
LIMIT 1"
|
|
);
|
|
if (empty($gradeRow['cnt'])) {
|
|
Database::query(
|
|
"ALTER TABLE `courses`
|
|
ADD COLUMN `grade_level` VARCHAR(50) DEFAULT NULL
|
|
COMMENT 'المرحلة الدراسية مثل: tawjihi_2008'
|
|
AFTER `is_system_curriculum`,
|
|
ADD COLUMN `stream` ENUM('scientific','literary','vocational','general') DEFAULT NULL
|
|
COMMENT 'الفرع الدراسي'
|
|
AFTER `grade_level`"
|
|
);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
error_log('[CurriculumService] ensureSystemCurriculumColumn error: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resolve or create the master system curriculum course.
|
|
*
|
|
* Returns a guaranteed valid `course_id` that satisfies the FK constraint on
|
|
* `lessons.course_id → courses.id`. Called by CurriculumController when
|
|
* uploading AI/ministry videos without an explicit teacher course.
|
|
*
|
|
* @return int Valid course_id
|
|
* @throws \RuntimeException if no subjects or teachers exist and creation fails
|
|
*/
|
|
public static function getOrCreateSystemCourse(): int
|
|
{
|
|
// Ensure the column exists before querying it (runtime migration)
|
|
self::ensureSystemCurriculumColumn();
|
|
|
|
// 1. Look for existing master system course
|
|
$existing = Database::selectOne(
|
|
"SELECT id FROM courses WHERE is_system_curriculum = 1 LIMIT 1"
|
|
);
|
|
if ($existing && !empty($existing['id'])) {
|
|
return (int)$existing['id'];
|
|
}
|
|
|
|
// 2. Also try by canonical title in case migration was run after records existed
|
|
$byTitle = Database::selectOne(
|
|
"SELECT id FROM courses WHERE title LIKE '%منهاج وزارة التربية%' LIMIT 1"
|
|
);
|
|
if ($byTitle && !empty($byTitle['id'])) {
|
|
// Stamp it so future lookups are fast
|
|
Database::query(
|
|
"UPDATE courses SET is_system_curriculum = 1 WHERE id = ?",
|
|
[(int)$byTitle['id']]
|
|
);
|
|
return (int)$byTitle['id'];
|
|
}
|
|
|
|
// 3. Create a new master course — resolve required FK dependencies first
|
|
// Resolve subject_id (use first available subject or create a placeholder)
|
|
$subject = Database::selectOne("SELECT id FROM subjects LIMIT 1");
|
|
if (!$subject) {
|
|
$subjectId = Database::insert(
|
|
"INSERT INTO subjects (name, description) VALUES ('المنهاج الوزاري الأردني', 'مقررات وزارة التربية والتعليم المعتمدة') ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)"
|
|
);
|
|
} else {
|
|
$subjectId = (int)$subject['id'];
|
|
}
|
|
|
|
// Resolve teacher_id (use system teacher with id=1 or first available)
|
|
$teacher = Database::selectOne("SELECT id FROM teachers WHERE id = 1 LIMIT 1");
|
|
if (!$teacher) {
|
|
$teacher = Database::selectOne("SELECT id FROM teachers LIMIT 1");
|
|
}
|
|
if (!$teacher) {
|
|
// Last resort: look up the super_admin user and create a teacher row for them
|
|
$adminUser = Database::selectOne(
|
|
"SELECT id FROM users WHERE role = 'super_admin' LIMIT 1"
|
|
);
|
|
$adminUserId = $adminUser ? (int)$adminUser['id'] : 1;
|
|
$teacherId = Database::insert(
|
|
"INSERT INTO teachers (user_id, specialization, verification_status)
|
|
VALUES (?, 'المنهاج الوزاري', 'verified')
|
|
ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)",
|
|
[$adminUserId]
|
|
);
|
|
} else {
|
|
$teacherId = (int)$teacher['id'];
|
|
}
|
|
|
|
// Generate stable UUID for the master course
|
|
$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
|
mt_rand(0, 0xffff),
|
|
mt_rand(0, 0x0fff) | 0x4000,
|
|
mt_rand(0, 0x3fff) | 0x8000,
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
|
);
|
|
|
|
$courseId = Database::insert(
|
|
"INSERT INTO courses
|
|
(uuid, subject_id, teacher_id, title, description, semester,
|
|
price_jod, is_published, is_system_curriculum, grade_level, stream)
|
|
VALUES
|
|
(?, ?, ?, 'منهاج وزارة التربية والتعليم الأساسي',
|
|
'الدورة الرئيسية للمنهاج الوزاري — تُستخدم لرفع وتنظيم فيديوهات الذكاء الاصطناعي التعليمية',
|
|
'full_year', 0.00, 1, 1, 'tawjihi_2008', 'scientific')",
|
|
[$uuid, $subjectId, $teacherId]
|
|
);
|
|
|
|
if (!$courseId) {
|
|
throw new \RuntimeException('فشل إنشاء الدورة الرئيسية للمنهاج — تحقق من وجود records في جداول subjects و teachers');
|
|
}
|
|
|
|
error_log("[CurriculumService] Created master system course id={$courseId}");
|
|
return (int)$courseId;
|
|
}
|
|
|
|
private static string $storagePath = __DIR__ . '/../../storage/curriculum';
|
|
private static string $manifestFile = __DIR__ . '/../../storage/curriculum/manifest.json';
|
|
|
|
public static function ensureStorage(): void
|
|
{
|
|
if (!is_dir(self::$storagePath)) {
|
|
mkdir(self::$storagePath, 0777, true);
|
|
}
|
|
if (!file_exists(self::$manifestFile)) {
|
|
file_put_contents(self::$manifestFile, json_encode(new \stdClass(), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get Complete Live Curriculum Tree from Storage Manifest
|
|
*/
|
|
public static function getCurriculumTree(): array
|
|
{
|
|
self::ensureStorage();
|
|
if (file_exists(self::$manifestFile)) {
|
|
$json = file_get_contents(self::$manifestFile);
|
|
$tree = json_decode($json, true);
|
|
if (is_array($tree)) {
|
|
return $tree;
|
|
}
|
|
}
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Save/Update Complete Curriculum Tree Manifest
|
|
*/
|
|
public static function saveCurriculumTree(array $tree): bool
|
|
{
|
|
self::ensureStorage();
|
|
return file_put_contents(self::$manifestFile, json_encode($tree, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)) !== false;
|
|
}
|
|
|
|
/**
|
|
* Merge Newly Extracted PDF Structure into Live Tree
|
|
*/
|
|
public static function mergeExtractedCurriculum(array $extracted): array
|
|
{
|
|
self::ensureStorage();
|
|
$tree = self::getCurriculumTree();
|
|
|
|
$gradeKey = $extracted['grade_key'] ?? 'general_grade';
|
|
$gradeName = $extracted['grade_name'] ?? 'الصف الدراسي';
|
|
|
|
$subKey = $extracted['subject_key'] ?? 'general_subject';
|
|
$subName = $extracted['subject_name'] ?? 'المبحث';
|
|
|
|
$semKey = $extracted['semester_key'] ?? 'semester_1';
|
|
$semName = $extracted['semester_name'] ?? 'الفصل الدراسي الأول';
|
|
|
|
if (!isset($tree[$gradeKey])) {
|
|
$tree[$gradeKey] = [
|
|
'name' => $gradeName,
|
|
'subjects' => []
|
|
];
|
|
}
|
|
|
|
if (!isset($tree[$gradeKey]['subjects'][$subKey])) {
|
|
$tree[$gradeKey]['subjects'][$subKey] = [
|
|
'name' => $subName,
|
|
'semesters' => []
|
|
];
|
|
}
|
|
|
|
if (!isset($tree[$gradeKey]['subjects'][$subKey]['semesters'][$semKey])) {
|
|
$tree[$gradeKey]['subjects'][$subKey]['semesters'][$semKey] = [
|
|
'name' => $semName,
|
|
'units' => []
|
|
];
|
|
}
|
|
|
|
// Add / Update Units & Lessons
|
|
foreach ($extracted['units'] ?? [] as $unit) {
|
|
$unitKey = $unit['unit_key'] ?? ('unit_' . mt_rand(100, 999));
|
|
$unitName = $unit['unit_name'] ?? 'الوحدة الدراسية';
|
|
|
|
$unitLessons = [];
|
|
foreach ($unit['lessons'] ?? [] as $les) {
|
|
$lesId = $les['lesson_id'] ?? ('les_' . mt_rand(1000, 9999));
|
|
$lesTitle = $les['title'] ?? 'الدرس';
|
|
$lesOutcomes = $les['outcomes'] ?? [];
|
|
$relFilePath = "{$gradeKey}/{$subKey}/{$semKey}/{$unitKey}/{$lesId}.md";
|
|
$mdContent = $les['markdown_content'] ?? "# {$lesTitle}\n\nنتاجات التعلم المستهدفة للمنهاج الرسمي.";
|
|
|
|
// Save MD file to disk
|
|
self::saveLessonMarkdown($relFilePath, $mdContent);
|
|
|
|
$unitLessons[] = [
|
|
'id' => $lesId,
|
|
'title' => $lesTitle,
|
|
'outcomes' => $lesOutcomes,
|
|
'file' => $relFilePath
|
|
];
|
|
}
|
|
|
|
$tree[$gradeKey]['subjects'][$subKey]['semesters'][$semKey]['units'][$unitKey] = [
|
|
'name' => $unitName,
|
|
'lessons' => $unitLessons
|
|
];
|
|
}
|
|
|
|
self::saveCurriculumTree($tree);
|
|
return $tree;
|
|
}
|
|
|
|
/**
|
|
* Save Single Lesson Markdown Content to Hierarchical File System
|
|
*/
|
|
public static function saveLessonMarkdown(string $relativePath, string $content): bool
|
|
{
|
|
self::ensureStorage();
|
|
$fullPath = self::$storagePath . '/' . ltrim($relativePath, '/');
|
|
$dir = dirname($fullPath);
|
|
if (!is_dir($dir)) {
|
|
mkdir($dir, 0777, true);
|
|
}
|
|
return file_put_contents($fullPath, $content) !== false;
|
|
}
|
|
|
|
/**
|
|
* Retrieve Lesson Markdown Content
|
|
*/
|
|
public static function getLessonMarkdown(string $relativePath): string
|
|
{
|
|
self::ensureStorage();
|
|
$fullPath = self::$storagePath . '/' . ltrim($relativePath, '/');
|
|
if (file_exists($fullPath)) {
|
|
return file_get_contents($fullPath);
|
|
}
|
|
return "# محتوى المنهاج\nالمحتوى المعتمد للمنهاج الرسمي.";
|
|
}
|
|
|
|
public static function saveLessonAiAssets(string $relativePath, array $assets): bool
|
|
{
|
|
self::ensureStorage();
|
|
$baseName = preg_replace('/\.md$/i', '', ltrim($relativePath, '/'));
|
|
$jsonPath = self::$storagePath . '/' . $baseName . '_ai_assets.json';
|
|
|
|
$dir = dirname($jsonPath);
|
|
if (!is_dir($dir)) {
|
|
mkdir($dir, 0777, true);
|
|
}
|
|
return file_put_contents($jsonPath, json_encode($assets, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)) !== false;
|
|
}
|
|
|
|
public static function getLessonAiAssets(string $relativePath): array
|
|
{
|
|
self::ensureStorage();
|
|
$baseName = preg_replace('/\.md$/i', '', ltrim($relativePath, '/'));
|
|
$jsonPath = self::$storagePath . '/' . $baseName . '_ai_assets.json';
|
|
|
|
if (file_exists($jsonPath)) {
|
|
return json_decode(file_get_contents($jsonPath), true) ?: [];
|
|
}
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Fast Keyword Search across All Markdown Files
|
|
*/
|
|
public static function searchCurriculum(string $query): array
|
|
{
|
|
$tree = self::getCurriculumTree();
|
|
$results = [];
|
|
$queryClean = mb_strtolower(trim($query));
|
|
if (empty($queryClean)) return [];
|
|
|
|
foreach ($tree as $gradeKey => $grade) {
|
|
foreach ($grade['subjects'] ?? [] as $subKey => $subject) {
|
|
foreach ($subject['semesters'] ?? [] as $semKey => $semester) {
|
|
foreach ($semester['units'] ?? [] as $unitKey => $unit) {
|
|
foreach ($unit['lessons'] ?? [] as $lesson) {
|
|
$matches = false;
|
|
if (str_contains(mb_strtolower($lesson['title']), $queryClean) ||
|
|
str_contains(mb_strtolower($unit['name']), $queryClean) ||
|
|
str_contains(mb_strtolower($subject['name']), $queryClean)) {
|
|
$matches = true;
|
|
}
|
|
if (!$matches && !empty($lesson['outcomes'])) {
|
|
foreach ($lesson['outcomes'] as $out) {
|
|
if (str_contains(mb_strtolower($out), $queryClean)) {
|
|
$matches = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if ($matches) {
|
|
$results[] = [
|
|
'grade' => $grade['name'],
|
|
'subject' => $subject['name'],
|
|
'unit' => $unit['name'],
|
|
'lesson' => $lesson['title'],
|
|
'file_path' => $lesson['file'],
|
|
'outcomes' => $lesson['outcomes']
|
|
];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* Retrieve Context for Video Analysis
|
|
*/
|
|
public static function getCurriculumContext(int $courseId, string $lessonTitle): array
|
|
{
|
|
$tree = self::getCurriculumTree();
|
|
// Return matching or generic curriculum context
|
|
return [
|
|
'grade' => 'المرحلة التعليمية المعتمدة',
|
|
'subject' => 'المبحث الدراسي',
|
|
'unit' => 'الوحدة التعليمية',
|
|
'core_topics' => ['تحليل المفاهيم الأساسية', 'التطبيقات والمسائل النموذجية']
|
|
];
|
|
}
|
|
}
|