0 ? get_application($applicationId) : null;
$isApprovedSchool = $application && (string) $application['status'] === 'approved';
$values = teacher_defaults();
$errors = [];
$cycleContext = ['cycles' => [], 'selected' => null, 'active' => null, 'read_only' => false];
$selectedCycle = null;
$selectedCycleId = 0;
$isCycleReadOnly = false;
$cycleLabel = 'لا توجد دورة بعد';
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;
}
$allEnabledSubjectMap = get_enabled_subject_map();
$applicationSubjectIds = $application ? normalize_id_list($application['subjects'] ?? []) : [];
$teacherSubjectMap = $allEnabledSubjectMap;
if ($applicationSubjectIds !== [] && $allEnabledSubjectMap !== []) {
$applicationSubjectLookup = array_fill_keys($applicationSubjectIds, true);
$teacherSubjectMap = array_filter(
$allEnabledSubjectMap,
static fn (string $_label, int|string $subjectId): bool => isset($applicationSubjectLookup[(int) $subjectId]),
ARRAY_FILTER_USE_BOTH
);
}
if ($teacherSubjectMap === []) {
$teacherSubjectMap = $allEnabledSubjectMap;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $application) {
$action = $_POST['action'] ?? 'add';
$teacherId = filter_input(INPUT_POST, 'teacher_id', FILTER_VALIDATE_INT) ?: 0;
[$values, $errors] = validate_teacher_input($_POST, array_keys($teacherSubjectMap));
if (!$isApprovedSchool) {
$errors['form'] = 'لا يمكن فتح صفحة المعلمين قبل اعتماد المركز.';
} elseif ($selectedCycleId <= 0) {
$errors['form'] = 'يرجى إنشاء دورة موسمية أولاً من صفحة المركز.';
} elseif ($isCycleReadOnly) {
$errors['form'] = 'هذه الدورة مؤرشفة للقراءة فقط. افتح دورة جديدة أو اختر دورة نشطة لإضافة/تعديل أعضاء.';
}
if ($errors === []) {
try {
if ($action === 'edit' && $teacherId > 0) {
update_teacher_in_cycle((int) $application['id'], $selectedCycleId, $teacherId, $values);
set_flash('success', 'تم تحديث بيانات عضو الفريق بنجاح.');
} else {
create_teacher_in_cycle((int) $application['id'], $selectedCycleId, $values);
set_flash('success', 'تمت إضافة عضو الفريق داخل الدورة الموسمية المحددة بنجاح.');
}
header('Location: ' . school_page_url('teachers.php', (int) $application['id'], $selectedCycleId));
exit;
} catch (Throwable $exception) {
$errors['form'] = 'تعذر حفظ بيانات الفريق حالياً. يرجى المحاولة مرة أخرى.';
}
}
}
$search = clean_text($_GET['search'] ?? '', 255);
$filters = [
'role_title' => clean_text($_GET['role_title'] ?? '', 50),
'employment_status' => clean_text($_GET['employment_status'] ?? '', 50),
'search' => $search,
];
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT) ?: 1;
$limit = 15;
$offset = ($page - 1) * $limit;
$teachers = $isApprovedSchool && $selectedCycleId > 0 ? list_school_teachers_by_cycle((int) $application['id'], $selectedCycleId, $filters, $limit, $offset) : [];
foreach ($teachers as &$teacher) {
$teacher['subject_ids'] = normalize_id_list($teacher['subject_ids'] ?? []);
$teacher['subject_labels'] = teacher_subject_labels($teacher, $allEnabledSubjectMap);
}
unset($teacher);
$totalTeachers = $isApprovedSchool && $selectedCycleId > 0 ? count_school_teachers_by_cycle((int) $application['id'], $selectedCycleId, $filters) : 0;
$metrics = $isApprovedSchool && $selectedCycleId > 0 ? school_teacher_metrics_by_cycle((int) $application['id'], $selectedCycleId) : [
'total' => 0,
'active' => 0,
'pending' => 0,
'inactive' => 0,
'teachers' => 0,
'supervisors' => 0,
'email_ready' => 0,
];
$studentMetrics = $isApprovedSchool && $selectedCycleId > 0 ? school_student_metrics_by_cycle((int) $application['id'], $selectedCycleId) : [
'total' => 0,
'boys' => 0,
'girls' => 0,
'active' => 0,
'waiting' => 0,
'withdrawn' => 0,
];
$pageTitle = $application ? 'فريق المعلمين: ' . (string) $application['center_name'] . ($selectedCycle ? ' — ' . $cycleLabel : '') : 'فريق المعلمين';
$pageDescription = 'صفحة مستقلة لإدارة المعلمين والمشرفين بعد اعتماد المدرسة، مع ربط كل فريق بالدورة الموسمية المناسبة.';
$approvedSchoolUrl = $application ? school_page_url('approved_school.php', (int) $application['id'], $selectedCycleId) : 'approved_school.php';
$studentsUrl = $application ? school_page_url('students.php', (int) $application['id'], $selectedCycleId) : 'students.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) $teacher['full_name']) ?>
= e((string) $teacher['role_title']) ?>
= e((string) ($teacher['specialization'] ?: '—')) ?>
= e((string) ($teacher['phone'] ?: 'بدون هاتف')) ?>
= teacher_employment_status_badge((string) $teacher['employment_status']) ?>