0) { require_permission('courses', 'delete'); $stmt = db()->prepare("DELETE FROM courses WHERE id = ?"); $stmt->execute([$post_id]); header('Location: ' . app_url('admin.php', ['page' => 'courses'])); exit; } if ($post_action === 'close_all_registration') { db()->query("UPDATE courses SET registration_open = 0"); header('Location: ' . app_url('admin.php', ['page' => 'courses'])); exit; } if ($post_action === 'open_all_registration') { db()->query("UPDATE courses SET registration_open = 1"); header('Location: ' . app_url('admin.php', ['page' => 'courses'])); exit; } if ($post_action === 'reset_all_students') { db()->query("TRUNCATE TABLE course_students"); header('Location: ' . app_url('admin.php', ['page' => 'courses'])); exit; } if ($post_action === 'edit' || $post_action === 'add') { require_permission('courses', $post_action === 'add' ? 'add' : 'edit'); $name_en = $_POST['name_en'] ?? ''; $name_ar = $_POST['name_ar'] ?? ''; $desc_en = $_POST['description_en'] ?? ''; $desc_ar = $_POST['description_ar'] ?? ''; $status = $_POST['status'] ?? 'active'; $price = (float)($_POST['price'] ?? 0); $max_students = (isset($_POST['max_students']) && $_POST['max_students'] !== '') ? (int)$_POST['max_students'] : null; $registration_open = isset($_POST['registration_open']) ? 1 : 0; $picture = null; if ($post_action === 'edit' && $post_id > 0) { $stmt = db()->prepare("SELECT picture FROM courses WHERE id = ?"); $stmt->execute([$post_id]); $picture = $stmt->fetchColumn(); } if (isset($_FILES['picture']) && $_FILES['picture']['error'] === UPLOAD_ERR_OK) { $upload_dir = __DIR__ . '/assets/images/uploads/'; if (!is_dir($upload_dir)) { mkdir($upload_dir, 0775, true); } $filename = time() . '_' . basename($_FILES['picture']['name']); $target_file = $upload_dir . $filename; if (move_uploaded_file($_FILES['picture']['tmp_name'], $target_file)) { $picture = 'assets/images/uploads/' . $filename; } } if ($post_action === 'edit' && $post_id > 0) { $stmt = db()->prepare("UPDATE courses SET name_en=?, name_ar=?, description_en=?, description_ar=?, status=?, price=?, picture=?, max_students=?, registration_open=? WHERE id=?"); $stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar, $status, $price, $picture, $max_students, $registration_open, $post_id]); } else { $stmt = db()->prepare("INSERT INTO courses (name_en, name_ar, description_en, description_ar, status, price, picture, max_students, registration_open) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar, $status, $price, $picture, $max_students, $registration_open]); } header('Location: ' . app_url('admin.php', ['page' => 'courses'])); exit; } if ($post_action === 'manage_students') { $course_id = (int)$_POST['course_id']; $student_ids = $_POST['student_ids'] ?? []; db()->prepare("DELETE FROM course_students WHERE course_id=?")->execute([$course_id]); $insert_stmt = db()->prepare("INSERT INTO course_students (course_id, student_id) VALUES (?, ?)"); foreach ($student_ids as $sid) { $insert_stmt->execute([$course_id, (int)$sid]); } header('Location: ' . app_url('admin.php', ['page' => 'courses'])); exit; } } // Fetch all students for assignment mapping $all_students_stmt = db()->query("SELECT id, full_name, email FROM student_subscriptions ORDER BY full_name ASC"); $all_students = $all_students_stmt->fetchAll(PDO::FETCH_ASSOC); $course_students_stmt = db()->query("SELECT course_id, student_id FROM course_students"); $all_course_students = $course_students_stmt->fetchAll(PDO::FETCH_ASSOC); $course_students_map = []; foreach ($all_course_students as $cs) { $course_students_map[$cs['course_id']][] = $cs['student_id']; } // list view $search = $_GET['search'] ?? ''; $page_num = max(1, (int)($_GET['p'] ?? 1)); $limit = 10; $offset = ($page_num - 1) * $limit; $where = ""; $params = []; if ($search !== '') { $where = "WHERE name_en LIKE ? OR name_ar LIKE ?"; $params[] = "%$search%"; $params[] = "%$search%"; } $total_stmt = db()->prepare("SELECT COUNT(*) FROM courses $where"); $total_stmt->execute($params); $total = $total_stmt->fetchColumn(); $pages = ceil($total / $limit); $stmt = db()->prepare("SELECT * FROM courses $where ORDER BY id DESC LIMIT $limit OFFSET $offset"); $stmt->execute($params); $items = $stmt->fetchAll(PDO::FETCH_ASSOC); ?>
| ID | = h(t('Name', 'الاسم')) ?> | = h(t('Price', 'السعر')) ?> | = h(t('Status', 'الحالة')) ?> | = h(t('Assigned', 'مخصص')) ?> | = h(t('Reg.', 'التسجيل')) ?> | = h(t('Description', 'الوصف')) ?> | = h(t('Actions', 'إجراءات')) ?> |
|---|---|---|---|---|---|---|---|
| = h((string)$row['id']) ?> |
= h(current_lang() === 'ar' ? $row['name_ar'] : $row['name_en']) ?>
|
= h(format_price((float)($row['price'] ?? 0))) ?> | = h(t('Active', 'نشط')) ?> = h(t('Inactive', 'غير نشط')) ?> | = $assigned_count ?> / = h($row['max_students']) ?> | = h(t('Open', 'مفتوح')) ?> = h(t('Closed', 'مغلق')) ?> | = h(current_lang() === 'ar' ? $row['description_ar'] : $row['description_en']) ?> | |
| = h(t('No courses found.', 'لا توجد دورات.')) ?> | |||||||