116 lines
3.5 KiB
PHP
116 lines
3.5 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
die('Method Not Allowed');
|
|
}
|
|
|
|
// Handle score update from edit_score.php
|
|
if (isset($_POST['action']) && $_POST['action'] === 'update') {
|
|
$score_id = $_POST['score_id'] ?? null;
|
|
$scores = $_POST['scores'] ?? [];
|
|
|
|
if (!$score_id || empty($scores)) {
|
|
die('Invalid data for update.');
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Get course and existing score details
|
|
$stmt = $pdo->prepare('SELECT course_id, holes_played FROM scores WHERE id = ?');
|
|
$stmt->execute([$score_id]);
|
|
$score_info = $stmt->fetch();
|
|
|
|
if (!$score_info) {
|
|
die('Score not found.');
|
|
}
|
|
|
|
$stmt = $pdo->prepare('SELECT * FROM courses WHERE id = ?');
|
|
$stmt->execute([$score_info['course_id']]);
|
|
$course = $stmt->fetch();
|
|
|
|
$total_score = 0;
|
|
$total_par = 0;
|
|
$update_sql_parts = [];
|
|
$params = [];
|
|
|
|
for ($i = 1; $i <= $score_info['holes_played']; $i++) {
|
|
$hole_score = $scores[$i] ?? 0;
|
|
$total_score += $hole_score;
|
|
$total_par += $course['par_hole_' . $i];
|
|
$update_sql_parts[] = "hole_{$i}_score = :hole_{$i}_score";
|
|
$params[":hole_{$i}_score"] = $hole_score;
|
|
}
|
|
|
|
$total_to_par = $total_score - $total_par;
|
|
|
|
$sql = "UPDATE scores SET total_score = :total_score, total_to_par = :total_to_par, " . implode(', ', $update_sql_parts) . " WHERE id = :score_id";
|
|
$params[':total_score'] = $total_score;
|
|
$params[':total_to_par'] = $total_to_par;
|
|
$params[':score_id'] = $score_id;
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
|
|
header('Location: coach.php');
|
|
exit;
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database error: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
// Existing logic for new score submission (API-style)
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$data) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Invalid data']);
|
|
exit;
|
|
}
|
|
|
|
$required_fields = ['playerName', 'courseId', 'holes', 'scores'];
|
|
foreach ($required_fields as $field) {
|
|
if (empty($data[$field])) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => "Missing required field: {$field}"]);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// This part needs to be updated to use player_id and team_id
|
|
// For now, it will fail if player_name column is removed as per migration.
|
|
// This should be addressed in a future step.
|
|
|
|
$sql = "INSERT INTO scores (player_name, team_name, course_id, holes_played, total_score, total_to_par";
|
|
$params = [
|
|
':player_name' => $data['playerName'],
|
|
':team_name' => $data['teamName'] ?? null,
|
|
':course_id' => $data['courseId'],
|
|
':holes_played' => $data['holes'],
|
|
':total_score' => $data['totalScore'],
|
|
':total_to_par' => $data['totalToPar']
|
|
];
|
|
|
|
for ($i = 1; $i <= $data['holes']; $i++) {
|
|
$sql .= ", hole_{$i}_score";
|
|
$params[":hole_{$i}_score"] = $data['scores']["hole{$i}_score"] ?? null;
|
|
}
|
|
|
|
$sql .= ") VALUES (" . implode(', ', array_keys($params)) . ")";
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
|
|
http_response_code(201);
|
|
echo json_encode(['success' => 'Score submitted successfully']);
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
|
|
} |