= ?"; $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.firstname LIKE ? OR s.province LIKE ? OR s.amphur 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']; $db_error_message = ''; try { if (!isset($pdo) || !$pdo) { throw new PDOException("ไม่สามารถเชื่อมต่อฐานข้อมูลได้"); } $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.firstname", "s.province", "s.amphur"), 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 item_name 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.firstname", "s.province", "s.amphur"), 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. ดึงรายละเอียดเชิงลึกของแต่ละพื้นที่ เพื่ออัปเดตกราฟทั้ง 3 ตัว $itemDetails = array(); $dynamicGuidance = array(); $dynamicSuccess = array(); foreach($topItems as $item) { $iname = $item['item_name']; $item_params = array($iname); if (!empty($filter_params)) { foreach($filter_params as $param) { $item_params[] = $param; } } if ($has_school_table) { $stmt = $pdo->prepare("SELECT COUNT(*) as count, g.guidance_type FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE IFNULL({$group_col}, 'ไม่ระบุ') = ? AND g.guidance_type IS NOT NULL" . $where_clause . " GROUP BY g.guidance_type ORDER BY g.guidance_type"); $stmt->execute($item_params); $details = $stmt->fetchAll(PDO::FETCH_ASSOC); $gOrder = array('1' => 0, '2' => 0, '3' => 0); foreach($details as $detail) { $gOrder[$detail['guidance_type']] = $detail['count']; } $tot = array_sum($gOrder); $perc = array(); if ($tot > 0) { foreach($gOrder as $count) { $perc[] = round(($count / $tot) * 100, 1); } } else { $perc = array(0, 0, 0); } $itemDetails[$iname] = $perc; $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, IFNULL(SUM(g.participant_count), 0) as count FROM group_guidance g LEFT JOIN school s ON g.schoolID = s.schoolID WHERE IFNULL({$group_col}, 'ไม่ระบุ') = ? AND 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->execute($item_params); $g_res = $stmt->fetchAll(PDO::FETCH_ASSOC); $g_labels = array(); $g_data = array(); $g_tot = 0; foreach($g_res as $r) { $g_tot += $r['count']; } foreach($g_res as $r) { $g_labels[] = $r['guidance_name']; $g_data[] = $g_tot > 0 ? round(($r['count'] / $g_tot) * 100, 1) : 0; } $dynamicGuidance[$iname] = array('labels' => $g_labels ? $g_labels : array('ไม่มีข้อมูล'), 'data' => $g_data ? $g_data : array(100)); $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, 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 IFNULL({$group_col}, 'ไม่ระบุ') = ? AND 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->execute($item_params); $s_res = $stmt->fetchAll(PDO::FETCH_ASSOC); $s_labels = array(); $s_data = array(); foreach($s_res as $r) { $s_labels[] = $r['guidance_name']; $s_data[] = floatval($r['success_rate']); } $dynamicSuccess[$iname] = array('labels' => $s_labels ? $s_labels : array('ไม่มีข้อมูล'), 'data' => $s_data ? $s_data : array(0)); } else { $itemDetails[$iname] = array(0,0,0); $dynamicGuidance[$iname] = array('labels' => array('ไม่มีข้อมูล'), 'data' => array(100)); $dynamicSuccess[$iname] = array('labels' => array('ไม่มีข้อมูล'), 'data' => array(0)); } } } catch (PDOException $e) { $db_error_message = $e->getMessage(); $totalParticipants = 0; $participantTypes = array(); $guidanceTypes = array(); $successRates = array(); $topItems = array(); $itemDetails = array(); } ?>