39669-vm/students.php
2026-04-17 02:53:02 +00:00

472 lines
27 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/app.php';
$flash = consume_flash();
$applicationId = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT) ?: 0;
$requestedCycleId = filter_input(INPUT_GET, 'cycle', FILTER_VALIDATE_INT) ?: 0;
$application = $applicationId > 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']);
$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);
?>
<section class="py-4 py-lg-5">
<div class="container-xxl">
<div class="row g-4 align-items-start">
<div class="col-lg-3">
<?php if ($application) { require __DIR__ . '/includes/center_sidebar.php'; } else { require __DIR__ . '/includes/sidebar.php'; } ?>
</div>
<div class="col-lg-9">
<?php if (!$application): ?>
<div class="app-card text-center py-5">
<div class="empty-title mb-2">المدرسة غير موجودة</div>
<p class="text-muted mb-3">تحقق من رابط المدرسة أو ارجع إلى قائمة المراكز المعتمدة.</p>
<a class="btn btn-primary" href="applications.php?status=approved">المراكز المعتمدة</a>
</div>
<?php elseif (!$isApprovedSchool): ?>
<div class="page-banner mb-4 mb-lg-5">
<div class="row g-4 align-items-center">
<div class="col-lg-8">
<span class="eyebrow mb-3">التسجيل يبدأ بعد الاعتماد</span>
<h1 class="page-title mb-3"><?= e((string) $application['center_name']) ?></h1>
<p class="page-copy mb-3">هذه الصفحة جاهزة، لكن فتح سجل الطلاب مرتبط بتحويل حالة المركز إلى <strong>معتمد</strong> أولاً حتى يبقى التسلسل الإداري منظمًا.</p>
<div class="hero-meta">
<span>الحالة الحالية: <?= e(status_meta((string) $application['status'])['label']) ?></span>
<span>المدينة: <?= e((string) $application['city']) ?></span>
</div>
<div class="cta-stack mt-4">
<a class="btn btn-primary" href="<?= e($applicationDetailUrl) ?>">العودة لملف الاعتماد</a>
<a class="btn btn-outline-secondary" href="<?= e($approvedSchoolUrl) ?>">صفحة المركز</a>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="page-banner approved-hero mb-4 mb-lg-5">
<div class="row g-4 align-items-start">
<div class="col-lg-8">
<span class="approved-kicker mb-3">صفحة مستقلة لتسجيل الطلاب</span>
<h1 class="page-title mb-3">سجل الطلاب — <?= e((string) $application['center_name']) ?></h1>
<p class="page-copy mb-3">هذا هو أول موديول تشغيلي بعد اعتماد المدرسة. يتم عرض الطلاب مع إمكانية التصفية، بالإضافة إلى إدارة البيانات عن طريق نموذج مدمج.</p>
<div class="hero-meta">
<span><?= e((string) $application['city']) ?></span>
<span>السعة المعتمدة <?= e((string) $expectedCapacity) ?> طالب</span>
<span>المقاعد المتبقية <?= e((string) $remainingSeats) ?></span>
</div>
<div class="cta-stack mt-4">
<a class="btn btn-outline-secondary" href="<?= e($approvedSchoolUrl) ?>">صفحة المركز</a>
<a class="btn btn-outline-secondary" href="<?= e($teachersUrl) ?>">المعلمين</a>
</div>
</div>
<div class="col-lg-4">
<div class="app-card approved-note h-100">
<div class="section-title mb-3">ملخص التسجيل</div>
<div class="launch-metrics">
<div class="launch-metric"><strong><?= e((string) $metrics['total']) ?></strong><span>إجمالي الطلاب</span></div>
<div class="launch-metric"><strong><?= e((string) $metrics['active']) ?></strong><span>طلاب مؤكدون</span></div>
<div class="launch-metric"><strong><?= e((string) $metrics['waiting']) ?></strong><span>قائمة الانتظار</span></div>
</div>
</div>
</div>
</div>
</div>
<?php if ($selectedCycle): ?>
<?php $cycleStatusMap = school_cycle_status_map(); ?>
<div class="row g-4 mb-4 align-items-start">
<div class="col-lg-<?= is_super_admin() ? '7' : '12' ?>">
<div class="app-card h-100">
<div class="section-head mb-3">
<div>
<div class="section-title">الدورة الموسمية الحالية</div>
<div class="section-copy">كل بيانات هذه الصفحة مرتبطة الآن بالدورة <strong><?= e($cycleLabel) ?></strong>.</div>
</div>
<?= school_cycle_status_badge((string) $selectedCycle['status']) ?>
</div>
<div class="row g-3">
<div class="col-md-4"><div class="school-data-item"><strong>اسم الدورة</strong><span><?= e($cycleLabel) ?></span></div></div>
<div class="col-md-4"><div class="school-data-item"><strong>الفترة</strong><span><?= e((string) $selectedCycle['start_date']) ?> → <?= e((string) $selectedCycle['end_date']) ?></span></div></div>
<div class="col-md-4"><div class="school-data-item"><strong>عدد الدورات</strong><span><?= e((string) count($cycleContext['cycles'])) ?> دورة للمركز</span></div></div>
</div>
</div>
</div>
<?php if (is_super_admin()): ?>
<div class="col-lg-5">
<div class="app-card sidebar-card h-100">
<div class="section-title mb-3">التبديل بين الدورات</div>
<div class="quick-link-stack">
<?php foreach ($cycleContext['cycles'] as $cycle): ?>
<?php
$isCurrentCycleLink = (int) $cycle['id'] === $selectedCycleId;
$isActiveCycleLink = (int) $cycle['id'] === (int) (($cycleContext['active']['id'] ?? 0));
$cycleStatusLabel = (string) ($cycleStatusMap[$cycle['status']]['label'] ?? 'غير معروف');
$cycleMetaLine = (string) $cycle['start_date'] . ' → ' . (string) $cycle['end_date'] . ' — ' . $cycleStatusLabel;
?>
<a class="quick-link-item <?= $isCurrentCycleLink ? 'is-current' : '' ?>" href="<?= e(school_page_url('students.php', (int) $application['id'], (int) $cycle['id'])) ?>">
<div>
<strong><?= e((string) $cycle['cycle_name']) ?><?= $isCurrentCycleLink ? ' — المعروضة' : '' ?></strong>
<span><?= e($cycleMetaLine) ?></span>
</div>
</a>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<div class="row g-4 align-items-start">
<div class="col-lg-12">
<?php if (!empty($errors['form'])): ?>
<div class="alert alert-danger mb-4"><?= e($errors['form']) ?></div>
<?php endif; ?>
<div class="app-card mb-4">
<div class="section-head mb-3">
<div>
<div class="section-title">كشف المدرسة</div>
<div class="section-copy">الطلاب المسجلون حالياً في هذه المدرسة فقط.</div>
</div>
<div class="d-flex gap-2">
<span class="header-chip"><?= e((string) $metrics['boys']) ?> طلاب / <?= e((string) $metrics['girls']) ?> طالبات</span>
<?php if (!$isCycleReadOnly): ?>
<button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#studentModal" onclick="resetStudentModal()">
+ إضافة طالب
</button>
<?php endif; ?>
</div>
</div>
<form method="get" class="mb-4 bg-light p-3 rounded">
<input type="hidden" name="id" value="<?= e((string) $application['id']) ?>">
<input type="hidden" name="cycle" value="<?= e((string) $selectedCycleId) ?>">
<div class="row g-2">
<div class="col-md-3">
<input type="text" name="search" class="form-control form-control-sm" placeholder="ابحث باسم الطالب، الكود..." value="<?= e($search) ?>">
</div>
<div class="col-md-2">
<select name="gender" class="form-select form-select-sm">
<option value="">كل الأنواع</option>
<?php foreach (student_gender_options() as $option): ?>
<option value="<?= e($option) ?>" <?= $filters['gender'] === $option ? 'selected' : '' ?>><?= e($option) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-3">
<select name="grade_level" class="form-select form-select-sm">
<option value="">كل الصفوف</option>
<?php foreach (student_grade_options() as $option): ?>
<option value="<?= e($option) ?>" <?= $filters['grade_level'] === $option ? 'selected' : '' ?>><?= e($option) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<select name="enrollment_status" class="form-select form-select-sm">
<option value="">كل الحالات</option>
<?php foreach (student_enrollment_status_map() as $key => $meta): ?>
<option value="<?= e($key) ?>" <?= $filters['enrollment_status'] === $key ? 'selected' : '' ?>><?= e($meta['label']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-outline-secondary btn-sm w-100">تصفية</button>
</div>
</div>
</form>
<?php if ($students === []): ?>
<div class="empty-state text-center p-4">
<div class="empty-title mb-2">لا يوجد طلاب مطابقون</div>
<p class="text-muted mb-0">ابدأ من إضافة طالب أو قم بتغيير الفلاتر.</p>
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table app-table align-middle table-hover">
<thead>
<tr>
<th>الكود</th>
<th>الطالب</th>
<th>الصف</th>
<th>ولي الأمر</th>
<th>الهاتف</th>
<th>الحالة</th>
<?php if (!$isCycleReadOnly): ?><th>إجراءات</th><?php endif; ?>
</tr>
</thead>
<tbody>
<?php foreach ($students as $student): ?>
<tr>
<td><strong><?= e((string) $student['student_code']) ?></strong></td>
<td>
<strong><?= e((string) $student['full_name']) ?></strong>
<div class="text-muted small"><?= e((string) $student['gender']) ?></div>
</td>
<td><?= e((string) $student['grade_level']) ?></td>
<td>
<strong><?= e((string) $student['guardian_name']) ?></strong>
<?php if (!empty($student['notes'])): ?><div class="text-muted small"><?= e((string) $student['notes']) ?></div><?php endif; ?>
</td>
<td><a href="tel:<?= e((string) $student['guardian_phone']) ?>" class="text-decoration-none text-primary"><?= e((string) $student['guardian_phone']) ?></a></td>
<td><?= student_enrollment_status_badge((string) $student['enrollment_status']) ?></td>
<?php if (!$isCycleReadOnly): ?>
<td>
<button type="button" class="btn btn-sm btn-outline-secondary"
data-bs-toggle="modal" data-bs-target="#studentModal"
data-student='<?= htmlspecialchars(json_encode($student), ENT_QUOTES, 'UTF-8') ?>'
onclick="editStudentModal(this)">تعديل</button>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php render_pagination($totalStudents, $limit, $page, $_GET); ?>
<?php endif; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
</section>
<!-- Student Modal -->
<div class="modal fade" id="studentModal" tabindex="-1" aria-labelledby="studentModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<form method="post" id="studentForm" novalidate>
<div class="modal-header">
<h5 class="modal-title" id="studentModalLabel">إضافة طالب جديد</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="إغلاق"></button>
</div>
<div class="modal-body">
<input type="hidden" name="action" id="formAction" value="add">
<input type="hidden" name="student_id" id="formStudentId" value="">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label" for="student_code">رقم الطالب</label>
<input class="form-control <?= isset($errors['student_code']) ? 'is-invalid' : '' ?>" id="student_code" name="student_code" value="<?= e($values['student_code'] !== '' ? $values['student_code'] : $nextStudentCode) ?>" readonly>
<div class="form-text">يتم توليد رقم الطالب تلقائياً عند إضافة سجل جديد.</div>
<?php if (isset($errors['student_code'])): ?><div class="invalid-feedback"><?= e($errors['student_code']) ?></div><?php endif; ?>
</div>
<div class="col-md-6">
<label class="form-label" for="full_name">اسم الطالب / الطالبة</label>
<input class="form-control <?= isset($errors['full_name']) ? 'is-invalid' : '' ?>" id="full_name" name="full_name" value="<?= e($values['full_name']) ?>" placeholder="الاسم الثلاثي" required>
<?php if (isset($errors['full_name'])): ?><div class="invalid-feedback"><?= e($errors['full_name']) ?></div><?php endif; ?>
</div>
<div class="col-md-6">
<label class="form-label" for="gender">النوع</label>
<select class="form-select <?= isset($errors['gender']) ? 'is-invalid' : '' ?>" id="gender" name="gender" required>
<option value="">اختر</option>
<?php foreach (student_gender_options() as $option): ?>
<option value="<?= e($option) ?>" <?= $values['gender'] === $option ? 'selected' : '' ?>><?= e($option) ?></option>
<?php endforeach; ?>
</select>
<?php if (isset($errors['gender'])): ?><div class="invalid-feedback"><?= e($errors['gender']) ?></div><?php endif; ?>
</div>
<div class="col-md-6">
<label class="form-label" for="grade_level">الصف الدراسي</label>
<select class="form-select <?= isset($errors['grade_level']) ? 'is-invalid' : '' ?>" id="grade_level" name="grade_level" required>
<option value="">اختر</option>
<?php foreach (student_grade_options() as $option): ?>
<option value="<?= e($option) ?>" <?= $values['grade_level'] === $option ? 'selected' : '' ?>><?= e($option) ?></option>
<?php endforeach; ?>
</select>
<?php if (isset($errors['grade_level'])): ?><div class="invalid-feedback"><?= e($errors['grade_level']) ?></div><?php endif; ?>
</div>
<div class="col-md-6">
<label class="form-label" for="guardian_name">اسم ولي الأمر</label>
<input class="form-control <?= isset($errors['guardian_name']) ? 'is-invalid' : '' ?>" id="guardian_name" name="guardian_name" value="<?= e($values['guardian_name']) ?>" placeholder="الاسم الكامل" required>
<?php if (isset($errors['guardian_name'])): ?><div class="invalid-feedback"><?= e($errors['guardian_name']) ?></div><?php endif; ?>
</div>
<div class="col-md-6">
<label class="form-label" for="guardian_phone">هاتف ولي الأمر</label>
<input class="form-control <?= isset($errors['guardian_phone']) ? 'is-invalid' : '' ?>" id="guardian_phone" name="guardian_phone" value="<?= e($values['guardian_phone']) ?>" placeholder="0500000000" required>
<?php if (isset($errors['guardian_phone'])): ?><div class="invalid-feedback"><?= e($errors['guardian_phone']) ?></div><?php endif; ?>
</div>
<div class="col-md-6">
<label class="form-label" for="birth_date">تاريخ الميلاد</label>
<input type="date" class="form-control <?= isset($errors['birth_date']) ? 'is-invalid' : '' ?>" id="birth_date" name="birth_date" value="<?= e($values['birth_date']) ?>">
<?php if (isset($errors['birth_date'])): ?><div class="invalid-feedback"><?= e($errors['birth_date']) ?></div><?php endif; ?>
</div>
<div class="col-md-6">
<label class="form-label" for="enrollment_status">حالة التسجيل</label>
<select class="form-select <?= isset($errors['enrollment_status']) ? 'is-invalid' : '' ?>" id="enrollment_status" name="enrollment_status" required>
<?php foreach (student_enrollment_status_map() as $key => $meta): ?>
<option value="<?= e($key) ?>" <?= $values['enrollment_status'] === $key ? 'selected' : '' ?>><?= e($meta['label']) ?></option>
<?php endforeach; ?>
</select>
<?php if (isset($errors['enrollment_status'])): ?><div class="invalid-feedback"><?= e($errors['enrollment_status']) ?></div><?php endif; ?>
</div>
<div class="col-12">
<label class="form-label" for="notes">ملاحظات</label>
<textarea class="form-control" id="notes" name="notes" rows="2" placeholder="مثال: احتياج تعليمي، ملاحظة صحية، أو حالة انتظار."><?= e($values['notes']) ?></textarea>
</div>
</div>
</div>
<div class="modal-footer d-flex justify-content-between">
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">إلغاء</button>
<button type="submit" class="btn btn-primary">حفظ التغييرات</button>
</div>
</form>
</div>
</div>
</div>
<script>
const defaultStudentCode = <?= json_encode($nextStudentCode !== '' ? $nextStudentCode : ($values['student_code'] ?? '')) ?>;
function resetStudentModal() {
document.getElementById('studentModalLabel').innerText = 'إضافة طالب جديد';
document.getElementById('formAction').value = 'add';
document.getElementById('formStudentId').value = '';
// Clear inputs except defaults
const form = document.getElementById('studentForm');
form.reset();
document.getElementById('student_code').value = defaultStudentCode || '';
document.getElementById('enrollment_status').value = 'active';
}
function editStudentModal(btn) {
const student = JSON.parse(btn.getAttribute('data-student'));
document.getElementById('studentModalLabel').innerText = 'تعديل طالب: ' + student.full_name;
document.getElementById('formAction').value = 'edit';
document.getElementById('formStudentId').value = student.id;
document.getElementById('student_code').value = student.student_code || '';
document.getElementById('full_name').value = student.full_name || '';
document.getElementById('gender').value = student.gender || '';
document.getElementById('grade_level').value = student.grade_level || '';
document.getElementById('guardian_name').value = student.guardian_name || '';
document.getElementById('guardian_phone').value = student.guardian_phone || '';
document.getElementById('birth_date').value = student.birth_date || '';
document.getElementById('enrollment_status').value = student.enrollment_status || '';
document.getElementById('notes').value = student.notes || '';
}
// Show modal if there are errors (from POST)
<?php if (!empty($errors) && $_SERVER['REQUEST_METHOD'] === 'POST'): ?>
document.addEventListener('DOMContentLoaded', function() {
var myModal = new bootstrap.Modal(document.getElementById('studentModal'));
myModal.show();
<?php if (isset($_POST['action']) && $_POST['action'] === 'edit'): ?>
document.getElementById('studentModalLabel').innerText = 'تعديل طالب';
document.getElementById('formAction').value = 'edit';
document.getElementById('formStudentId').value = '<?= e($_POST['student_id'] ?? '') ?>';
<?php endif; ?>
});
<?php endif; ?>
</script>
<?php render_page_end(); ?>