= ?"; $params[] = $start_date . ' 00:00:00'; } if (!empty($end_date)) { $where_conditions[] = "g.created_at <= ?"; $params[] = $end_date . ' 23:59:59'; } if (!empty($service_type)) { $where_conditions[] = "g.guidance_type = ?"; $params[] = $service_type; } if (!empty($search_term)) { $where_conditions[] = "(s.school_name LIKE ? OR s.province LIKE ? OR s.district LIKE ?)"; $search_param = '%' . $search_term . '%'; $params[] = $search_param; $params[] = $search_param; $params[] = $search_param; } $where_clause = ''; if (!empty($where_conditions)) { $where_clause = ' AND ' . implode(' AND ', $where_conditions); } return array('clause' => $where_clause, 'params' => $params); } $filter_conditions = buildWhereClause($start_date, $end_date, $service_type, $search_term, $user_school_id, $current_user_type); $where_clause = $filter_conditions['clause']; $filter_params = $filter_conditions['params']; try { if (!isset($pdo) || !$pdo) { throw new PDOException("ไม่สามารถเชื่อมต่อฐานข้อมูลได้"); } $check_tables = $pdo->query("SHOW TABLES LIKE 'group_guidance'"); if (!$check_tables || $check_tables->rowCount() == 0) { throw new PDOException("ไม่พบตาราง group_guidance"); } $has_school_table = false; try { $check_school = $pdo->query("SHOW TABLES LIKE 'school'"); $has_school_table = ($check_school && $check_school->rowCount() > 0); } catch (Exception $e) { $has_school_table = false; } // 1. ดึงจำนวนผู้เข้าร่วมทั้งหมด if ($has_school_table) { $sql = "SELECT COUNT(*) as total_participants FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE 1=1" . $where_clause; } else { $sql = "SELECT COUNT(*) as total_participants FROM group_guidance g WHERE 1=1" . str_replace(array("s.school_name", "s.province", "s.district"), array("''", "''", "''"), $where_clause); } $stmt = $pdo->prepare($sql); $stmt->execute($filter_params); $result = $stmt->fetch(PDO::FETCH_ASSOC); $totalParticipants = $result ? (empty($result['total_participants']) ? 0 : $result['total_participants']) : 0; // 2. ดึงประเภทผู้เข้าร่วม $sql = "SELECT CASE WHEN g.participant_type = '1' THEN 'การเรียนรู้ตลอดชีวิต' WHEN g.participant_type = '2' THEN 'การเรียนรู้เพื่อการพัฒนาตนเอง' WHEN g.participant_type = '3' THEN 'การเรียนรู้เพื่อคุณวุฒิตามระดับ' WHEN g.participant_type = '4' THEN 'ประชาชน' ELSE 'อื่นๆ' END as type_name, COUNT(*) as count FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE g.participant_type IS NOT NULL" . $where_clause . " GROUP BY g.participant_type"; $stmt = $pdo->prepare($sql); $stmt->execute($filter_params); $participantTypes = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!$participantTypes) { $participantTypes = array(); } // 3. ดึงประเภทการแนะแนว $sql = "SELECT CASE WHEN g.guidance_type = '1' THEN 'ด้านการศึกษา' WHEN g.guidance_type = '2' THEN 'ด้านอาชีพ' WHEN g.guidance_type = '3' THEN 'ด้านส่วนตัวและสังคม' ELSE 'อื่นๆ' END as guidance_name, IFNULL(SUM(g.participant_count), 0) as count FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE g.participant_count IS NOT NULL AND g.guidance_type IS NOT NULL" . $where_clause . " GROUP BY g.guidance_type ORDER BY count DESC"; $stmt = $pdo->prepare($sql); $stmt->execute($filter_params); $guidanceTypes = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!$guidanceTypes) { $guidanceTypes = array(); } // 4. ดึงอัตราความสำเร็จ $sql = "SELECT CASE WHEN g.guidance_type = '1' THEN 'ด้านการศึกษา' WHEN g.guidance_type = '2' THEN 'ด้านอาชีพ' WHEN g.guidance_type = '3' THEN 'ด้านส่วนตัวและสังคม' ELSE 'อื่นๆ' END as guidance_name, CASE WHEN SUM(g.participant_count) > 0 THEN ROUND((SUM(CASE WHEN g.goal_achieved = '1' THEN g.participant_count ELSE 0 END) / SUM(g.participant_count) * 100), 2) ELSE 0 END as success_rate FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE g.participant_count IS NOT NULL AND g.guidance_type IS NOT NULL" . $where_clause . " GROUP BY g.guidance_type ORDER BY success_rate DESC"; $stmt = $pdo->prepare($sql); $stmt->execute($filter_params); $successRates = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!$successRates) { $successRates = array(); } // 5. ดึงข้อมูล TOP 10 if ($has_school_table) { $sql = "SELECT IFNULL({$group_col}, 'ไม่ระบุ') as item_name, COUNT(*) as total_count FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE g.schoolID IS NOT NULL" . $where_clause . " GROUP BY {$group_col} HAVING total_count > 0 ORDER BY total_count DESC LIMIT 10"; } else { $sql = "SELECT 'กรุงเทพมหานคร' as item_name, COUNT(*) as total_count FROM group_guidance g WHERE 1=1" . str_replace(array("s.school_name", "s.province", "s.district"), array("''", "'กรุงเทพมหานคร'", "''"), $where_clause) . " HAVING total_count > 0 LIMIT 10"; } $stmt = $pdo->prepare($sql); $stmt->execute($filter_params); $topItems = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!$topItems) { $topItems = array(); } // 6. ดึงรายละเอียดแต่ละพื้นที่ $itemDetails = array(); foreach($topItems as $item) { if ($has_school_table) { $stmt = $pdo->prepare("SELECT CASE WHEN g.guidance_type = '1' THEN 'ด้านการศึกษา' WHEN g.guidance_type = '2' THEN 'ด้านอาชีพ' WHEN g.guidance_type = '3' THEN 'ด้านส่วนตัวและสังคม' ELSE 'อื่นๆ' END as guidance_name, COUNT(*) as count, g.guidance_type FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE {$group_col} = ? AND g.guidance_type IS NOT NULL" . $where_clause . " GROUP BY g.guidance_type ORDER BY g.guidance_type"); $item_params = array($item['item_name']); if (!empty($filter_params)) { foreach($filter_params as $param) { $item_params[] = $param; } } $stmt->execute($item_params); } else { $stmt = $pdo->prepare("SELECT COUNT(*) as count, g.guidance_type FROM group_guidance g WHERE g.guidance_type IS NOT NULL" . str_replace(array("s.school_name", "s.province", "s.district"), array("''", "''", "''"), $where_clause) . " GROUP BY g.guidance_type"); $stmt->execute($filter_params); } $details = $stmt->fetchAll(PDO::FETCH_ASSOC); $guidanceOrder = array('1' => 0, '2' => 0, '3' => 0); foreach($details as $detail) { $guidanceOrder[$detail['guidance_type']] = $detail['count']; } $total = array_sum($guidanceOrder); $percentages = array(); if ($total > 0) { foreach($guidanceOrder as $count) { $percentages[] = round(($count / $total) * 100, 1); } } else { $percentages = array(0, 0, 0); } $itemDetails[$item['item_name']] = $percentages; } } catch (PDOException $e) { $totalParticipants = 0; $participantTypes = array(); $guidanceTypes = array(); $successRates = array(); $topItems = array(); $itemDetails = array(); } ?>
(ระบบนำไปค้นหาด้วยเงื่อนไข: )