39669-vm/student_completion_certificates.php
2026-04-17 06:59:52 +00:00

300 lines
15 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/app.php';
function completion_certificate_date_label(string $date): string
{
if ($date === '' || strtotime($date) === false) {
return date('Y/m/d');
}
return date('Y/m/d', strtotime($date));
}
function completion_certificate_decimal_value(float $value): string
{
return rtrim(rtrim(number_format($value, 2, '.', ''), '0'), '.');
}
function completion_certificate_message_text(string $template, array $tokens, string $fallback): string
{
$message = trim($template);
if ($message === '') {
$message = $fallback;
}
return strtr($message, $tokens);
}
function completion_certificate_view_model(array $application, array $settings, int $cycleId, string $cycleLabel, int $studentId): ?array
{
$certificate = school_student_certificate_summary((int) $application['id'], $cycleId, $studentId);
$student = is_array($certificate['student'] ?? null) ? $certificate['student'] : null;
if ($student === null) {
return null;
}
$performance = $certificate['performance'] ?? student_certificate_performance_meta(0.0);
$hasResults = !empty($certificate['has_results']);
$honor = $hasResults
? student_completion_certificate_honor_meta((float) ($certificate['overall_percentage'] ?? 0))
: [
'key' => 'completion',
'title' => 'Successful Completion',
'title_ar' => 'إتمام ناجح',
'completion_line_ar' => 'أتمّ/أتمّت الدورة بنجاح واستفاد/استفادت من محتواها العلمي.',
];
$template = (string) ($settings['completion_certificate_template'] ?? 'modern');
if (!in_array($template, ['modern', 'classic'], true)) {
$template = 'modern';
}
$centerName = trim((string) ($application['center_name'] ?? ''));
if ($centerName === '') {
$centerName = (string) ($settings['app_name'] ?? project_name());
}
$centerLogo = trim((string) ($application['logo'] ?? ''));
if ($centerLogo === '') {
$centerLogo = trim((string) ($settings['app_logo'] ?? ''));
}
$tagline = trim((string) ($settings['completion_certificate_tagline'] ?? ''));
if ($tagline === '') {
$tagline = 'شهادة إتمام وتكريم';
}
$overallPercentage = (float) ($certificate['overall_percentage'] ?? 0);
$percentageLabel = $hasResults ? completion_certificate_decimal_value($overallPercentage) . '%' : '—';
$fallbackMessage = sprintf(
'تشهد إدارة %s بأن الطالب/الطالبة %s قد أتمّ/أتمّت متطلبات %s %s.',
$centerName,
(string) ($student['full_name'] ?? 'الطالب/الطالبة'),
$cycleLabel,
$honor['title_ar']
);
$bodyMessage = completion_certificate_message_text(
(string) ($settings['completion_certificate_message'] ?? ''),
[
'{student}' => (string) ($student['full_name'] ?? ''),
'{center}' => $centerName,
'{cycle}' => $cycleLabel,
'{performance}' => (string) ($performance['label_ar'] ?? 'مميز'),
'{honor}' => (string) ($honor['title_ar'] ?? 'بنجاح'),
'{percentage}' => $hasResults ? completion_certificate_decimal_value($overallPercentage) : '0',
],
$fallbackMessage
);
return [
'certificate' => $certificate,
'student' => $student,
'performance' => $performance,
'has_results' => $hasResults,
'honor' => $honor,
'template' => $template,
'center_name' => $centerName,
'center_logo' => $centerLogo,
'tagline' => $tagline,
'percentage_label' => $percentageLabel,
'body_message' => $bodyMessage,
'issue_date_label' => completion_certificate_date_label((string) ($certificate['latest_assessed_on'] ?? '')),
];
}
$flash = consume_flash();
$settings = get_app_settings();
$applicationId = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT) ?: 0;
$requestedCycleId = filter_input(INPUT_GET, 'cycle', FILTER_VALIDATE_INT) ?: 0;
$autoprint = filter_input(INPUT_GET, 'autoprint', FILTER_VALIDATE_INT) ?: 0;
$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),
];
$application = $applicationId > 0 ? get_application($applicationId) : null;
$isApprovedSchool = $application && (string) $application['status'] === 'approved';
$cycleContext = ['cycles' => [], 'selected' => null, 'active' => null, 'read_only' => false];
$selectedCycle = null;
$selectedCycleId = 0;
$cycleLabel = 'الدورة الحالية';
$certificateItems = [];
if ($application && $isApprovedSchool) {
$cycleContext = resolve_school_cycle_context((int) $application['id'], $application, $requestedCycleId);
$selectedCycle = $cycleContext['selected'];
$selectedCycleId = $selectedCycle ? (int) ($selectedCycle['id'] ?? 0) : 0;
$cycleLabel = $selectedCycle ? (string) ($selectedCycle['cycle_name'] ?? 'الدورة الحالية') : $cycleLabel;
if ($selectedCycleId > 0) {
$students = list_school_students_by_cycle((int) $application['id'], $selectedCycleId, $search, 0, 0, $filters);
foreach ($students as $studentRow) {
$viewModel = completion_certificate_view_model($application, $settings, $selectedCycleId, $cycleLabel, (int) ($studentRow['id'] ?? 0));
if ($viewModel !== null) {
$certificateItems[] = $viewModel;
}
}
}
}
if (!$application || !$isApprovedSchool || $selectedCycleId <= 0) {
http_response_code(404);
}
$studentsUrl = $application ? school_page_url('students.php', (int) $application['id'], $selectedCycleId) : 'students.php';
$pageTitle = $application ? 'تنزيل شهادات الإتمام: ' . (string) ($application['center_name'] ?? '') : 'تنزيل شهادات الإتمام';
$pageDescription = 'صفحة مجمعة لطباعة أو حفظ جميع شهادات الإتمام والتكريم كملف PDF واحد للمركز والدورة المختارة.';
render_page_start($pageTitle, 'approved', $pageDescription);
render_flash($flash);
?>
<section class="student-certificate-page completion-certificate-page completion-batch-page py-4 py-lg-5">
<div class="container-xl">
<div class="certificate-toolbar">
<div>
<div class="section-title">جميع شهادات الإتمام والتكريم</div>
<div class="section-subtle"><?= e((string) count($certificateItems)) ?> شهادة<?= $search !== '' ? ' — نتائج البحث الحالية' : '' ?>. سيتم فتح نافذة الطباعة لتتمكن من الحفظ كملف PDF واحد.</div>
</div>
<div class="certificate-toolbar-actions">
<a class="btn btn-outline-secondary" href="<?= e($studentsUrl) ?>">العودة إلى الطلاب</a>
<?php if ($certificateItems !== []): ?>
<button type="button" class="btn btn-primary" onclick="window.print()">تنزيل PDF / طباعة</button>
<?php endif; ?>
</div>
</div>
<?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="app-card text-center py-5">
<div class="empty-title mb-2">الشهادات غير متاحة حالياً</div>
<p class="text-muted mb-0">يمكن إصدار شهادات الإتمام للمراكز المعتمدة فقط.</p>
</div>
<?php elseif ($selectedCycleId <= 0): ?>
<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="<?= e($studentsUrl) ?>">العودة إلى سجل الطلاب</a>
</div>
<?php elseif ($certificateItems === []): ?>
<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="<?= e($studentsUrl) ?>">العودة إلى سجل الطلاب</a>
</div>
<?php else: ?>
<div class="batch-certificate-stack">
<?php foreach ($certificateItems as $item): ?>
<?php
$student = $item['student'];
$certificate = $item['certificate'];
$performance = $item['performance'];
$honor = $item['honor'];
?>
<article class="completion-certificate-card completion-batch-item completion-template-<?= e((string) $item['template']) ?>" aria-labelledby="completion-certificate-title-<?= e((string) ($student['id'] ?? 0)) ?>">
<div class="completion-certificate-layer completion-certificate-layer-one" aria-hidden="true"></div>
<div class="completion-certificate-layer completion-certificate-layer-two" aria-hidden="true"></div>
<div class="completion-certificate-inner">
<header class="completion-certificate-header">
<div class="completion-branding">
<?php if ((string) $item['center_logo'] !== ''): ?>
<img src="<?= e(asset_url((string) $item['center_logo'])) ?>" alt="شعار <?= e((string) $item['center_name']) ?>" class="completion-brand-logo" width="96" height="96">
<?php else: ?>
<div class="completion-brand-logo completion-brand-badge" aria-hidden="true"><?= e(mb_substr((string) $item['center_name'], 0, 1)) ?></div>
<?php endif; ?>
<div>
<div class="completion-overline">Certificate of Completion</div>
<h1 id="completion-certificate-title-<?= e((string) ($student['id'] ?? 0)) ?>" class="completion-certificate-title"><?= e((string) $item['tagline']) ?></h1>
<p class="completion-certificate-center mb-0"><?= e((string) $item['center_name']) ?></p>
</div>
</div>
<div class="completion-certificate-stamps">
<span class="completion-stamp"><?= e($cycleLabel) ?></span>
<span class="completion-stamp completion-stamp-accent"><?= e((string) ($honor['title_ar'] ?? 'بنجاح')) ?></span>
</div>
</header>
<div class="completion-certificate-content">
<p class="completion-intro mb-2">تُمنح هذه الشهادة إلى</p>
<div class="completion-student-name"><?= e((string) ($student['full_name'] ?? '')) ?></div>
<?php if (!empty($student['student_code'])): ?>
<div class="completion-student-code">رقم الطالب: <?= e((string) $student['student_code']) ?></div>
<?php endif; ?>
<p class="completion-message"><?= e((string) $item['body_message']) ?></p>
<p class="completion-honor-line mb-0"><?= e((string) ($honor['completion_line_ar'] ?? '')) ?></p>
</div>
<div class="completion-certificate-grid">
<section class="completion-summary-card">
<span class="completion-summary-label">التقدير النهائي</span>
<div class="performance-pill performance-<?= e((string) ($performance['key'] ?? 'good')) ?> completion-performance-pill">
<?= e((string) ($performance['label_ar'] ?? 'مميز')) ?>
</div>
<div class="completion-honor-title"><?= e((string) ($honor['title_ar'] ?? 'بنجاح')) ?></div>
<div class="completion-honor-subtitle"><?= e((string) ($honor['title'] ?? 'Successful Completion')) ?></div>
</section>
<section class="completion-meta-grid" aria-label="بيانات الشهادة">
<div class="completion-meta-item">
<strong>الدورة</strong>
<span><?= e($cycleLabel) ?></span>
</div>
<div class="completion-meta-item">
<strong>تاريخ الإصدار</strong>
<span><?= e((string) $item['issue_date_label']) ?></span>
</div>
<div class="completion-meta-item">
<strong>نسبة الأداء</strong>
<span><?= e((string) $item['percentage_label']) ?></span>
</div>
<div class="completion-meta-item">
<strong>التقييمات المكتملة</strong>
<span><?= e((string) ((int) ($certificate['completed_assessments'] ?? 0))) ?></span>
</div>
</section>
</div>
<?php if (empty($item['has_results'])): ?>
<div class="completion-note">
لا توجد تقييمات مرصودة لهذا الطالب بعد، لذلك تم إصدار شهادة إتمام مختصرة بدون مرتبة أداء رقمية.
</div>
<?php endif; ?>
<footer class="completion-signature-row">
<div class="completion-signature-block">
<strong>مدير/ة المركز</strong>
<span><?= e((string) ($application['director_name'] ?? $item['center_name'])) ?></span>
</div>
<div class="completion-signature-block">
<strong>اعتماد الشهادة</strong>
<span><?= e((string) ($honor['title_ar'] ?? 'إتمام ناجح')) ?></span>
</div>
</footer>
</div>
</article>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</section>
<?php if ($autoprint === 1 && $certificateItems !== []): ?>
<script>
window.addEventListener('load', function () {
window.setTimeout(function () {
window.print();
}, 250);
});
</script>
<?php endif; ?>
<?php render_page_end(); ?>