39669-vm/center_subjects.php
2026-04-17 03:38:41 +00:00

129 lines
6.2 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;
$isApproved = $application && (string) $application['status'] === 'approved';
$selectedCycleId = 0;
if ($isApproved) {
$cycleContext = resolve_school_cycle_context((int) $application['id'], $application, $requestedCycleId);
$selectedCycle = $cycleContext['selected'];
$selectedCycleId = $selectedCycle ? (int) ($selectedCycle['id'] ?? 0) : 0;
}
if (!$application || !$isApproved) {
http_response_code(404);
render_page_start('المركز غير موجود', 'profile');
render_flash($flash);
?>
<section class="py-5">
<div class="container-xxl">
<div class="admin-layout row g-4 align-items-start">
<div class="col-lg-3 layout-sidebar-column">
<?php if ($application) { require __DIR__ . '/includes/center_sidebar.php'; } else { require __DIR__ . '/includes/sidebar.php'; } ?>
</div>
<div class="col-lg-9 layout-content-column">
<div class="app-card text-center py-5">
<div class="empty-title mb-2">المركز غير موجود</div>
<a class="btn btn-primary" href="applications.php?status=approved">العودة</a>
</div>
</div>
</div>
</div>
</section>
<?php
render_page_end();
exit;
}
$available_subjects = get_enabled_subjects();
$current_subjects = is_string($application['subjects']) ? json_decode($application['subjects'], true) : [];
if (!is_array($current_subjects)) $current_subjects = [];
$current_subjects_ids = array_map('strval', $current_subjects);
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update_subjects') {
$selected_subjects = $_POST['subjects'] ?? [];
if (!is_array($selected_subjects)) {
$selected_subjects = [];
}
// We can allow 0 subjects, but typically at least 1.
// Let's allow saving any selection.
$selected_subjects_ids = array_map('intval', $selected_subjects);
try {
update_application_subjects((int) $application['id'], $selected_subjects_ids);
set_flash('success', 'تم تحديث المواد الدراسية للمركز بنجاح.');
$selectedCycleIdStr = $selectedCycleId > 0 ? '&cycle=' . $selectedCycleId : '';
header('Location: center_subjects.php?id=' . $application['id'] . $selectedCycleIdStr);
exit;
} catch (Throwable $e) {
$errors['form'] = 'تعذر حفظ البيانات. يرجى المحاولة لاحقاً.';
}
}
render_page_start('إدارة المواد الدراسية: ' . (string) $application['center_name'], 'profile', 'تحديد المواد الدراسية التي يتم تدريسها في المركز.');
render_flash($flash);
?>
<section class="py-4 py-lg-5">
<div class="container-xxl">
<div class="admin-layout row g-4 align-items-start">
<div class="col-lg-3 layout-sidebar-column">
<?php if ($application) { require __DIR__ . '/includes/center_sidebar.php'; } else { require __DIR__ . '/includes/sidebar.php'; } ?>
</div>
<div class="col-lg-9 layout-content-column">
<div class="page-banner mb-4">
<h1 class="page-title mb-2">المواد الدراسية للمركز</h1>
<p class="page-copy mb-0">تحديد وتحديث المواد الدراسية التي يتم تقديمها في هذا المركز. سيتم توفير هذه المواد لاختيارها في التقييمات والجداول.</p>
</div>
<div class="app-card mb-4">
<form method="post">
<input type="hidden" name="action" value="update_subjects">
<?php if (isset($errors['form'])): ?><div class="alert alert-danger mb-3"><?= e($errors['form']) ?></div><?php endif; ?>
<div class="mb-4">
<label class="form-label d-block fw-bold mb-3">اختر المواد المتاحة في المركز</label>
<?php if (empty($available_subjects)): ?>
<div class="alert alert-info">لا توجد مواد دراسية مفعلة في النظام حالياً. يرجى إضافة مواد من الإدارة المركزية أولاً.</div>
<?php else: ?>
<div class="row g-3">
<?php foreach ($available_subjects as $subject): ?>
<div class="col-md-6 col-lg-4">
<div class="form-check border rounded-3 p-3 h-100 bg-light-subtle">
<input class="form-check-input ms-0 me-2 mt-1 float-end" type="checkbox" name="subjects[]" <?= is_super_admin() ? "" : "disabled" ?> value="<?= e((string) $subject['id']) ?>" id="subject_<?= e((string) $subject['id']) ?>" <?= in_array((string) $subject['id'], $current_subjects_ids, true) ? 'checked' : '' ?>>
<label class="form-check-label d-block me-4 pe-2" for="subject_<?= e((string) $subject['id']) ?>">
<span class="fw-bold d-block text-primary"><?= e((string) $subject['name']) ?></span>
<?php if (!empty($subject['description'])): ?>
<small class="text-muted d-block mt-1"><?= e((string) $subject['description']) ?></small>
<?php endif; ?>
</label>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<div class="text-start">
<?php if (is_super_admin()): ?>
<button type="submit" class="btn btn-primary px-4">حفظ المواد الدراسية</button>
<?php else: ?>
<div class="alert alert-warning mb-0">يمكن للمشرف العام فقط تعديل هذه القائمة.</div>
<?php endif; ?>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
<?php render_page_end();