2400) { $db_year -= 543; } $server_year = intval(date('Y')); if ($server_year > 2400) { $server_year -= 543; } $age = $server_year - $db_year; return $age >= 0 ? $age : 0; } return 0; } // 2. ตรวจสอบสิทธิ์ผู้ใช้งาน $raw_school_id = isset($schoolID) ? $schoolID : (isset($_SESSION['schoolID']) ? $_SESSION['schoolID'] : ''); $raw_school_id = preg_replace('/[^a-zA-Z0-9_]/', '', $raw_school_id); $current_user_type = isset($user_type_id) ? intval($user_type_id) : (isset($_SESSION['user_type_id']) ? intval($_SESSION['user_type_id']) : 0); $is_admin = ($current_user_type === 9 || empty($raw_school_id) || strtolower($raw_school_id) === 'admin'); $show_search_box = true; $search_box_label = "ค้นหา"; if ($is_admin) { $user_school_id = ''; $role_name = "ส่วนกลาง (สิทธิ์เต็ม)"; $group_col = "s.province"; $chart_label = "จังหวัด"; $search_box_label = "ค้นหาชื่อจังหวัด"; } else { $user_school_id = $raw_school_id; if ($current_user_type === 1) { $role_name = "จังหวัด"; $group_col = "s.amphur"; $chart_label = "อำเภอ"; $search_box_label = "ค้นหาชื่ออำเภอ"; } else if ($current_user_type === 2) { $role_name = "อำเภอ"; $group_col = "s.firstname"; $chart_label = "สถานศึกษา"; $search_box_label = "ค้นหาชื่อตำบล"; } else if ($current_user_type === 3) { $role_name = "สถานศึกษาขึ้นตรง"; $group_col = "s.firstname"; $chart_label = "สถานศึกษา"; $search_box_label = "ค้นหาชื่อสถานศึกษา"; } else if ($current_user_type === 4) { $role_name = "ตำบล"; $group_col = "s.firstname"; $chart_label = "สถานศึกษา"; $show_search_box = false; } else { $role_name = "ไม่ระบุสิทธิ์"; $group_col = "s.firstname"; $chart_label = "สถานศึกษา"; } } $start_date = isset($_POST['start_date']) ? $_POST['start_date'] : ''; $end_date = isset($_POST['end_date']) ? $_POST['end_date'] : ''; $search_term = isset($_POST['search_term']) ? trim($_POST['search_term']) : ''; $where_c = array(); $params_c = array(); $where_srv = array(); $params_srv = array(); if (!$is_admin && $user_school_id !== '' && $user_school_id !== '0') { $like_val = ($current_user_type === 1) ? substr($user_school_id, 0, 4) . '%' : (($current_user_type === 2) ? substr($user_school_id, 0, 6) . '%' : $user_school_id . '%'); $where_c[] = "c.schoolID LIKE ?"; $params_c[] = $like_val; $where_srv[] = "srv.schoolID LIKE ?"; $params_srv[] = $like_val; } if (!empty($start_date)) { $where_c[] = "c.created_at >= ?"; $params_c[] = $start_date . ' 00:00:00'; $where_srv[] = "srv.service_request_date >= ?"; $params_srv[] = $start_date; } if (!empty($end_date)) { $where_c[] = "c.created_at <= ?"; $params_c[] = $end_date . ' 23:59:59'; $where_srv[] = "srv.service_request_date <= ?"; $params_srv[] = $end_date; } if (!empty($search_term)) { $search_val = '%' . $search_term . '%'; $search_clause = "(s.firstname LIKE ? OR s.province LIKE ? OR s.amphur LIKE ?)"; $where_c[] = $search_clause; $params_c[] = $search_val; $params_c[] = $search_val; $params_c[] = $search_val; $where_srv[] = $search_clause; $params_srv[] = $search_val; $params_srv[] = $search_val; $params_srv[] = $search_val; } $clause_c = !empty($where_c) ? " AND " . implode(" AND ", $where_c) : ""; $clause_srv = !empty($where_srv) ? " AND " . implode(" AND ", $where_srv) : ""; $db_error_message = ''; $totalServices = 0; $totalIndividualParticipants = 0; $total_success_1 = 0; $total_unsuccess_2 = 0; $total_referrals = 0; $total_reg = 0; $count_type1 = 0; $count_type2 = 0; $count_type3 = 0; $count_type4 = 0; $genders = array('ชาย' => 0, 'หญิง' => 0, 'ไม่ระบุ' => 0); $ages = array('under15' => 0, '15to18' => 0, '19to60' => 0, 'over60' => 0); $occupations = array(); $total_edu = 0; $total_career = 0; $total_personal = 0; $edu_items = array(); $career_items = array(); $personal_items = array(); $help_items = array(); $topItems = array(); $tableData = array(); $dict_edu = array(); $dict_car = array(); $dict_per = array(); $dict_hlp = array(); $occ_map = array(); try { if (!isset($pdo) || !$pdo) { throw new PDOException("ไม่สามารถเชื่อมต่อฐานข้อมูลได้"); } $stmt_occ = $pdo->query("SELECT * FROM typeOccupation"); while($row = $stmt_occ->fetch(PDO::FETCH_ASSOC)) { $occ_map[$row['id']] = $row['name'] ?? $row['title'] ?? $row['occupation_name'] ?? array_values($row)[1]; } $s1 = $pdo->query("SELECT id, title FROM typeEducational"); while($r = $s1->fetch(PDO::FETCH_ASSOC)) $dict_edu[$r['id']] = $r['title']; $s2 = $pdo->query("SELECT id, title FROM typeEduOccupation"); while($r = $s2->fetch(PDO::FETCH_ASSOC)) $dict_car[$r['id']] = $r['title']; $s3 = $pdo->query("SELECT id, title FROM typesocialSspects"); while($r = $s3->fetch(PDO::FETCH_ASSOC)) $dict_per[$r['id']] = $r['title']; $s4 = $pdo->query("SELECT id, title FROM typeGiveHelp"); while($r = $s4->fetch(PDO::FETCH_ASSOC)) $dict_hlp[$r['id']] = $r['title']; // สถิติภาพรวม $sql_hero = "SELECT COUNT(srv.id) as total_services, COUNT(DISTINCT srv.individual_counseling_id) as unique_people FROM individual_counseling_service srv LEFT JOIN school s ON srv.schoolID = s.schoolID WHERE 1=1 " . $clause_srv; $stmt_hero = $pdo->prepare($sql_hero); $stmt_hero->execute($params_srv); if($hero_row = $stmt_hero->fetch(PDO::FETCH_ASSOC)) { $totalServices = intval($hero_row['total_services']); $totalIndividualParticipants = intval($hero_row['unique_people']); } $inner_where = str_replace("srv.", "srv2.", $clause_srv); $sql_status = "SELECT SUM(CASE WHEN srv.service_success_id = '1' THEN 1 ELSE 0 END) as total_success_1, SUM(CASE WHEN srv.service_success_id = '2' THEN 1 ELSE 0 END) as total_unsuccess_2 FROM individual_counseling_service srv INNER JOIN (SELECT c.first_name, c.last_name, MAX(srv2.id) as max_id FROM individual_counseling_service srv2 JOIN individual_counseling c ON srv2.individual_counseling_id = c.id LEFT JOIN school s ON srv2.schoolID = s.schoolID WHERE 1=1 {$inner_where} GROUP BY c.first_name, c.last_name) latest ON srv.id = latest.max_id"; $stmt_status = $pdo->prepare($sql_status); $stmt_status->execute($params_srv); if($status_row = $stmt_status->fetch(PDO::FETCH_ASSOC)) { $total_success_1 = intval($status_row['total_success_1']); $total_unsuccess_2 = intval($status_row['total_unsuccess_2']); } $sql_person = "SELECT c.service_type_id, c.gender_id, c.birth_date, c.occupation_id FROM individual_counseling c LEFT JOIN school s ON c.schoolID = s.schoolID WHERE 1=1 " . $clause_c; $stmt_c = $pdo->prepare($sql_person); $stmt_c->execute($params_c); while($row = $stmt_c->fetch(PDO::FETCH_ASSOC)) { $total_reg++; $st = $row['service_type_id'] ?? ''; if($st == '1') $count_type1++; elseif($st == '2') $count_type2++; elseif($st == '3') $count_type3++; elseif($st == '4') $count_type4++; $g = $row['gender_id'] ?? ''; if($g == '1') $genders['ชาย']++; elseif($g == '2') $genders['หญิง']++; else $genders['ไม่ระบุ']++; $calculated_age = calculateAge($row['birth_date'] ?? ''); if ($calculated_age < 15) $ages['under15']++; elseif ($calculated_age <= 18) $ages['15to18']++; elseif ($calculated_age <= 60) $ages['19to60']++; else $ages['over60']++; $occ_id = $row['occupation_id'] ?? ''; $occ_name = $occ_map[$occ_id] ?? ($occ_id ? "อาชีพรหัส " . $occ_id : "ไม่ระบุ"); $occupations[$occ_name] = ($occupations[$occ_name] ?? 0) + 1; } // ========================================================= // 🌟 ระบบคำนวณด้านการศึกษา/อาชีพ/ส่วนตัว (ฉบับนับเมื่อมีข้อมูลจริง) 🌟 // ========================================================= $sql_service = "SELECT srv.education_issues, srv.career_issues, srv.personal_issues, srv.referral_services FROM individual_counseling_service srv LEFT JOIN school s ON srv.schoolID = s.schoolID WHERE 1=1 " . $clause_srv; $stmt_srv = $pdo->prepare($sql_service); $stmt_srv->execute($params_srv); while($row = $stmt_srv->fetch(PDO::FETCH_ASSOC)) { // --- 1. ด้านการศึกษา --- $e_val = $row['education_issues'] ?? ''; $e_arr = json_decode($e_val, true); if (!is_array($e_arr)) $e_arr = empty(trim($e_val)) ? [] : explode(',', $e_val); $has_valid_edu = false; foreach($e_arr as $id) { $id = trim($id); if($id !== '' && $id !== '0') { // ต้องมีค่าจริงๆ ไม่ใช่ช่องว่าง $edu_items[$id] = ($edu_items[$id] ?? 0) + 1; $has_valid_edu = true; } } if($has_valid_edu) { $total_edu++; } // --- 2. ด้านอาชีพ --- $c_val = $row['career_issues'] ?? ''; $c_arr = json_decode($c_val, true); if (!is_array($c_arr)) $c_arr = empty(trim($c_val)) ? [] : explode(',', $c_val); $has_valid_car = false; foreach($c_arr as $id) { $id = trim($id); if($id !== '' && $id !== '0') { $career_items[$id] = ($career_items[$id] ?? 0) + 1; $has_valid_car = true; } } if($has_valid_car) { $total_career++; } // --- 3. ด้านส่วนตัวและสังคม --- $p_val = $row['personal_issues'] ?? ''; $p_arr = json_decode($p_val, true); if (!is_array($p_arr)) $p_arr = empty(trim($p_val)) ? [] : explode(',', $p_val); $has_valid_per = false; foreach($p_arr as $id) { $id = trim($id); if($id !== '' && $id !== '0') { $personal_items[$id] = ($personal_items[$id] ?? 0) + 1; $has_valid_per = true; } } if($has_valid_per) { $total_personal++; } // --- 4. การส่งต่อ --- $h_val = $row['referral_services'] ?? ''; $h_arr = json_decode($h_val, true); if (!is_array($h_arr)) $h_arr = empty(trim($h_val)) ? [] : explode(',', $h_val); $count_this_row_as_referral = false; foreach($h_arr as $id) { $id = trim($id); if($id !== '' && $id !== '0') { $help_items[$id] = ($help_items[$id] ?? 0) + 1; $num_id = intval($id); if (($num_id >= 3 && $num_id <= 9) || strtolower($id) === 'referral-other') { $count_this_row_as_referral = true; } } } if ($count_this_row_as_referral) { $total_referrals++; } } arsort($edu_items); $top_edu = array_slice($edu_items, 0, 5, true); arsort($career_items); $top_career = array_slice($career_items, 0, 5, true); arsort($personal_items); $top_personal = array_slice($personal_items, 0, 5, true); arsort($help_items); $top_help = array_slice($help_items, 0, 5, true); arsort($occupations); if ($current_user_type != 4) { $sql_top10 = "SELECT IFNULL({$group_col}, 'ไม่ระบุ') as item_name, COUNT(srv.id) as total_count FROM individual_counseling_service srv LEFT JOIN school s ON srv.schoolID = s.schoolID WHERE srv.schoolID IS NOT NULL " . $clause_srv . " GROUP BY item_name HAVING total_count > 0 ORDER BY total_count DESC LIMIT 10"; $stmt_top10 = $pdo->prepare($sql_top10); $stmt_top10->execute($params_srv); $topItems = $stmt_top10->fetchAll(PDO::FETCH_ASSOC); if (!$topItems) $topItems = array(); } if ($current_user_type == 4) { $sql_table = "SELECT srv.id as srv_id, srv.service_request_date, srv.service_request_details, srv.counselor_first_name, srv.counselor_last_name, srv.education_issues, srv.career_issues, srv.personal_issues, srv.referral_services, srv.service_success_id, srv.follow_up_date, srv.follow_up_id, c.* FROM individual_counseling_service srv LEFT JOIN individual_counseling c ON srv.individual_counseling_id = c.id LEFT JOIN school s ON srv.schoolID = s.schoolID WHERE 1=1 " . $clause_srv . " ORDER BY srv.service_request_date DESC"; $stmt_table = $pdo->prepare($sql_table); $stmt_table->execute($params_srv); $tableData = $stmt_table->fetchAll(PDO::FETCH_ASSOC); } } catch (PDOException $e) { $db_error_message = $e->getMessage(); } function getIssueTextsDB($json_str, $dict, $type = '') { $arr = json_decode($json_str, true) ?: (empty($json_str) ? [] : explode(',', $json_str)); if (empty($arr)) return "-"; $texts = array(); foreach($arr as $id) { $id = trim($id); if($id && $id !== '0') { $label = $dict[$id] ?? "รหัส ".$id; if ($type == 'edu' && ($id == '12' || strtolower($id) === 'education-other')) $label = 'อื่น ๆ'; if ($type == 'car' && ($id == '10' || strtolower($id) === 'career-other')) $label = 'อื่น ๆ'; if ($type == 'per' && ($id == '10' || strtolower($id) === 'personal-other')) $label = 'อื่น ๆ'; if ($type == 'hlp' && ($id == '9' || strtolower($id) === 'referral-other')) $label = 'อื่น ๆ'; $texts[] = $label; } } return "• " . implode("
• ", $texts); } function prepareChartDataDB($top_array, $dict, $type = '') { $labels = array(); $data = array(); foreach($top_array as $id => $count) { $label = $dict[$id] ?? "รหัส ".$id; if ($type == 'edu' && ($id == '12' || strtolower($id) === 'education-other')) $label = 'อื่น ๆ'; if ($type == 'car' && ($id == '10' || strtolower($id) === 'career-other')) $label = 'อื่น ๆ'; if ($type == 'per' && ($id == '10' || strtolower($id) === 'personal-other')) $label = 'อื่น ๆ'; if ($type == 'hlp' && ($id == '9' || strtolower($id) === 'referral-other')) $label = 'อื่น ๆ'; $labels[] = $label; $data[] = $count; } return array('labels' => $labels, 'data' => $data); } $chart_edu = prepareChartDataDB($top_edu, $dict_edu, 'edu'); $chart_car = prepareChartDataDB($top_career, $dict_car, 'car'); $chart_per = prepareChartDataDB($top_personal, $dict_per, 'per'); $chart_hlp = prepareChartDataDB($top_help, $dict_hlp, 'hlp'); ?> Dashboard แบบรายบุคคล
🚨 พบข้อผิดพลาด:
สถานะการใช้งาน:

Dashboard แบบรายบุคคล

จำนวนผู้ลงทะเบียน (คน)
จำนวนผู้รับบริการ (คน)
จำนวนครั้งที่ให้บริการ (ครั้ง)
สำเร็จ (คน)
ไม่สำเร็จ (คน)
การส่งต่อ (ครั้ง)

สถิติผู้รับบริการ (แบ่งตามกลุ่มเป้าหมาย)

ผู้เรียนการเรียนรู้ตลอดชีวิต
(คน)
ผู้เรียนการเรียนรู้เพื่อการพัฒนาตนเอง
(คน)
ผู้เรียนการเรียนรู้เพื่อคุณวุฒิตามระดับ
(คน)
ประชาชน
(ทั่วไป)

สถิติการให้คำปรึกษา (แบ่งตามด้าน)

ด้านการศึกษา (ครั้ง)
ด้านอาชีพ (ครั้ง)
ด้านส่วนตัวและสังคม (ครั้ง)

สถานศึกษาที่ให้บริการแนะแนวรายบุคคลมากที่สุด

TOP 10 ที่ให้บริการมากที่สุด

ข้อมูลประชากรศาสตร์

เพศของผู้รับบริการ
ช่วงอายุของผู้รับบริการ
(รวม คน)
ประกอบอาชีพ

ปัญหาและอุปสรรคที่พบมากที่สุด (TOP 5)

TOP 5 ปัญหาด้านการศึกษา
TOP 5 ปัญหาด้านอาชีพ
TOP 5 ปัญหาด้านส่วนตัวและสังคม
TOP 5 การให้ความช่วยเหลือ (ส่งต่อ)

ตารางข้อมูลผู้รับบริการแนะแนว (ระดับตำบล)

สำเร็จ'; $status_text = 'สำเร็จ'; } elseif (!empty($row['follow_up_date'])) { $status_badge = 'มีการนัดหมายติดตาม'; $status_text = 'มีการนัดหมายติดตาม'; } elseif ($success_id === 2) { $status_badge = 'ไม่สำเร็จ'; $status_text = 'ไม่สำเร็จ'; } else { $status_badge = 'ดำเนินการอยู่'; $status_text = 'ดำเนินการอยู่'; } $date_parts = explode('-', explode(' ', $row['service_request_date'] ?? '')[0]); $thai_date = count($date_parts) == 3 ? $date_parts[2].'/'.$date_parts[1].'/'.($date_parts[0]+543) : ($row['service_request_date'] ?? '-'); $g_text = ($row['gender_id'] ?? '') == '1' ? 'ชาย' : (($row['gender_id'] ?? '') == '2' ? 'หญิง' : 'ไม่ระบุ'); $age_val = calculateAge($row['birth_date'] ?? ''); $age_text = ($age_val > 0) ? $age_val . ' ปี' : 'ไม่ระบุ'; $addr = ''; if(!empty($row['house_number'])) $addr .= "เลขที่ " . $row['house_number'] . " "; if(!empty($row['village_number'])) $addr .= "หมู่ " . $row['village_number'] . " "; if(!empty($row['subdistrict'])) $addr .= "ต." . $row['subdistrict'] . " "; if(!empty($row['district'])) $addr .= "อ." . $row['district'] . " "; if(!empty($row['province'])) $addr .= "จ." . $row['province'] . " "; if($addr == '') $addr = '-'; $srv_id = $row['service_type_id'] ?? ''; if($srv_id == '1') $st_text = 'การเรียนรู้ตลอดชีวิต'; elseif($srv_id == '2') $st_text = 'การเรียนรู้เพื่อการพัฒนาตนเอง'; elseif($srv_id == '3') $st_text = 'การเรียนรู้เพื่อคุณวุฒิตามระดับ'; elseif($srv_id == '4') $st_text = 'ประชาชน'; else $st_text = 'ไม่ระบุ'; $occ_id = $row['occupation_id'] ?? ''; $occ_text = $occ_map[$occ_id] ?? ($occ_id ? "รหัส " . $occ_id : "ไม่ระบุ"); $edu_txt = getIssueTextsDB($row['education_issues'] ?? '', $dict_edu, 'edu'); $car_txt = getIssueTextsDB($row['career_issues'] ?? '', $dict_car, 'car'); $per_txt = getIssueTextsDB($row['personal_issues'] ?? '', $dict_per, 'per'); $ref_txt = getIssueTextsDB($row['referral_services'] ?? '', $dict_hlp, 'hlp'); $fname = trim(($row['first_name'] ?? '') . ' ' . ($row['last_name'] ?? '')); $cfname = trim(($row['counselor_first_name'] ?? '') . ' ' . ($row['counselor_last_name'] ?? '')); $emname = trim(($row['emergency_first_name'] ?? '') . ' ' . ($row['emergency_last_name'] ?? '')); $modalData = array( 'patientName' => $fname, 'phone' => $row['phone'] ?: '-', 'gender' => $g_text, 'age' => $age_text, 'nat' => $row['nationality'] ?: '-', 'eth' => $row['ethnicity'] ?: '-', 'rel' => $row['religion'] ?: '-', 'blood' => $row['blood_type'] ?: '-', 'addr' => $addr, 'em_name' => $emname ?: '-', 'em_phone' => $row['emergency_phone'] ?: '-', 'target_group' => $st_text, 'occupation' => $occ_text, 'date' => $thai_date, 'consultant' => $cfname, 'status' => $status_text, 'details' => $row['service_request_details'] ?: '-', 'edu' => $edu_txt, 'car' => $car_txt, 'per' => $per_txt, 'ref' => $ref_txt ); $json_modal_data = htmlspecialchars(json_encode($modalData), ENT_QUOTES, 'UTF-8'); ?>
ลำดับวันที่รับการแนะแนวประเภทบริการชื่อผู้รับบริการชื่อผู้ให้บริการสถานะดูข้อมูล