0 ? get_application($applicationId) : null;
$isApprovedSchool = $application && (string) $application['status'] === 'approved';
$values = student_defaults();
$errors = [];
$cycleContext = ['cycles' => [], 'selected' => null, 'active' => null, 'read_only' => false];
$selectedCycle = null;
$selectedCycleId = 0;
$isCycleReadOnly = false;
$cycleLabel = 'لا توجد دورة بعد';
$nextStudentCode = '';
if ($application && $isApprovedSchool) {
$cycleContext = resolve_school_cycle_context((int) $application['id'], $application, $requestedCycleId);
$selectedCycle = $cycleContext['selected'];
$selectedCycleId = $selectedCycle ? (int) ($selectedCycle['id'] ?? 0) : 0;
$isCycleReadOnly = (bool) $cycleContext['read_only'];
$cycleLabel = $selectedCycle ? (string) $selectedCycle['cycle_name'] : $cycleLabel;
if ($selectedCycleId > 0) {
$nextStudentCode = next_student_code_for_cycle((int) $application['id'], $selectedCycleId);
$values['student_code'] = $nextStudentCode;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $application) {
$action = $_POST['action'] ?? 'add';
$studentId = filter_input(INPUT_POST, 'student_id', FILTER_VALIDATE_INT) ?: 0;
$isEditAction = $action === 'edit' && $studentId > 0;
[$values, $errors] = validate_student_input($_POST, $isEditAction);
if (!$isEditAction && $selectedCycleId > 0 && $values['student_code'] === '') {
$values['student_code'] = $nextStudentCode !== '' ? $nextStudentCode : next_student_code_for_cycle((int) $application['id'], $selectedCycleId);
}
if (!$isApprovedSchool) {
$errors['form'] = 'لا يمكن فتح تسجيل الطلاب قبل اعتماد المركز.';
} elseif ($selectedCycleId <= 0) {
$errors['form'] = 'يرجى إنشاء دورة موسمية أولاً من صفحة المركز.';
} elseif ($isCycleReadOnly) {
$errors['form'] = 'هذه الدورة مؤرشفة للقراءة فقط. افتح دورة جديدة أو اختر دورة نشطة لإضافة/تعديل طلاب.';
}
if ($errors === []) {
try {
if ($action === 'edit' && $studentId > 0) {
update_student_in_cycle((int) $application['id'], $selectedCycleId, $studentId, $values);
set_flash('success', 'تم تحديث بيانات الطالب بنجاح.');
} else {
create_student_in_cycle((int) $application['id'], $selectedCycleId, $values);
set_flash('success', 'تم تسجيل الطالب بنجاح داخل الدورة الموسمية المحددة.');
}
header('Location: ' . school_page_url('students.php', (int) $application['id'], $selectedCycleId));
exit;
} catch (PDOException $exception) {
$duplicateCode = isset($exception->errorInfo[1]) && (int) $exception->errorInfo[1] === 1062;
if ($duplicateCode) {
$errors['student_code'] = 'هذا الكود مستخدم مسبقاً داخل نفس الدورة الموسمية.';
$errors['form'] = 'تعذر الحفظ لوجود تعارض في الكود.';
} else {
$errors['form'] = 'تعذر حفظ بيانات الطالب حالياً. يرجى المحاولة مرة أخرى.';
}
} catch (Throwable $exception) {
$errors['form'] = 'تعذر حفظ بيانات الطالب حالياً. يرجى المحاولة مرة أخرى.';
}
}
}
$search = clean_text($_GET['search'] ?? '', 255);
$filters = [
'gender' => clean_text($_GET['gender'] ?? '', 50),
'grade_level' => clean_text($_GET['grade_level'] ?? '', 50),
'enrollment_status' => clean_text($_GET['enrollment_status'] ?? '', 50),
];
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT) ?: 1;
$limit = 15;
$offset = ($page - 1) * $limit;
$students = $isApprovedSchool && $selectedCycleId > 0 ? list_school_students_by_cycle((int) $application['id'], $selectedCycleId, $search, $limit, $offset, $filters) : [];
$totalStudents = $isApprovedSchool && $selectedCycleId > 0 ? count_school_students_by_cycle((int) $application['id'], $selectedCycleId, $search, $filters) : 0;
$metrics = $isApprovedSchool && $selectedCycleId > 0 ? school_student_metrics_by_cycle((int) $application['id'], $selectedCycleId) : [
'total' => 0,
'boys' => 0,
'girls' => 0,
'active' => 0,
'waiting' => 0,
'withdrawn' => 0,
];
$expectedCapacity = $application ? (int) ($application['expected_students'] ?? 0) : 0;
$remainingSeats = max(0, $expectedCapacity - $metrics['total']);
$bulkCertificateParams = ['autoprint' => '1'];
if ($search !== '') {
$bulkCertificateParams['search'] = $search;
}
foreach ($filters as $filterKey => $filterValue) {
if ($filterValue !== '') {
$bulkCertificateParams[$filterKey] = $filterValue;
}
}
$bulkCompletionCertificatesUrl = $application && $selectedCycleId > 0
? school_page_url('student_completion_certificates.php', (int) $application['id'], $selectedCycleId) . '&' . http_build_query($bulkCertificateParams)
: '#';
$pageTitle = $application ? 'تسجيل الطلاب: ' . (string) $application['center_name'] . ($selectedCycle ? ' — ' . $cycleLabel : '') : 'تسجيل الطلاب';
$pageDescription = 'صفحة مستقلة لتسجيل الطلاب وإدارة كشف المدرسة بعد الاعتماد، مع ربط كل البيانات بالدورة الموسمية النشطة أو المؤرشفة.';
$approvedSchoolUrl = $application ? school_page_url('approved_school.php', (int) $application['id'], $selectedCycleId) : 'approved_school.php';
$teachersUrl = $application ? school_page_url('teachers.php', (int) $application['id'], $selectedCycleId) : 'teachers.php';
$assessmentsUrl = $application ? school_page_url('assessments.php', (int) $application['id'], $selectedCycleId) : 'assessments.php';
$attendanceUrl = $application ? school_page_url('attendance.php', (int) $application['id'], $selectedCycleId) : 'attendance.php';
$applicationDetailUrl = $application ? 'application_detail.php?id=' . urlencode((string) $application['id']) : 'application_detail.php';
if (!$application) {
http_response_code(404);
}
render_page_start($pageTitle, 'approved', $pageDescription, (string) ($application['favicon'] ?? ''));
render_flash($flash);
?>
ابدأ من إضافة طالب أو قم بتغيير الفلاتر.
الكود
الطالب
الصف
ولي الأمر
الهاتف
الحالة
إجراءات
= e((string) $student['student_code']) ?>
= e((string) $student['full_name']) ?>
= e((string) $student['grade_level']) ?>
= e((string) $student['guardian_name']) ?>
= e((string) $student['guardian_phone']) ?>
= student_enrollment_status_badge((string) $student['enrollment_status']) ?>