This commit is contained in:
Flatlogic Bot 2025-09-24 18:59:48 +00:00
parent 1268ae0578
commit 354c764c97
7 changed files with 429 additions and 139 deletions

View File

@ -229,12 +229,14 @@ try {
holeInputDiv.style.display = 'none'; holeInputDiv.style.display = 'none';
holeInput.value = 0; holeInput.value = 0;
holeInput.required = false; holeInput.required = false;
holeInput.min = 0;
} else { } else {
holeInputDiv.style.display = ''; holeInputDiv.style.display = '';
if (holeInput.value == 0) { // Only reset if it was 0 if (holeInput.value == 0) { // Only reset if it was 0
holeInput.value = 4; // reset to default holeInput.value = 4; // reset to default
} }
holeInput.required = true; holeInput.required = true;
holeInput.min = 1;
} }
} }
} }

170
coach.php
View File

@ -1,3 +1,17 @@
<?php
require_once __DIR__ . '/db/config.php';
// Assume a logged-in coach with ID 1
$coach_id = 1;
$pdo = db();
// Get teams for the coach
$stmt = $pdo->prepare('SELECT * FROM teams WHERE coach_id = ?');
$stmt->execute([$coach_id]);
$teams = $stmt->fetchAll();
?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
@ -5,102 +19,82 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Coach Dashboard - Golf Tournament</title> <title>Coach Dashboard - Golf Tournament</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap">
<link rel="stylesheet" href="assets/css/custom.css"> <link rel="stylesheet" href="assets/css/custom.css">
</head> </head>
<body> <body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary"> <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container"> <div class="container">
<a class="navbar-brand" href="index.php">Golf Tournament</a> <a class="navbar-brand" href="index.php">Golf Tournament</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <div class="collapse navbar-collapse" id="navbarNav">
<span class="navbar-toggler-icon"></span> <ul class="navbar-nav ms-auto">
</button> <li class="nav-item"><a class="nav-link" href="index.php">Home</a></li>
<div class="collapse navbar-collapse" id="navbarNav"> <li class="nav-item"><a class="nav-link" href="add_score.php">Add Score</a></li>
<ul class="navbar-nav ms-auto"> <li class="nav-item"><a class="nav-link" href="admin.php">Admin</a></li>
<li class="nav-item"> <li class="nav-item"><a class="nav-link active" href="coach.php">Coach</a></li>
<a class="nav-link" href="index.php">Home</a> <li class="nav-item"><a class="nav-link" href="results.php">Results</a></li>
</li> </ul>
<li class="nav-item">
<a class="nav-link" href="add_score.php">Add Score</a>
</li>
<li class="nav-item">
<a class="nav-link" href="admin.php">Admin</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="coach.php">Coach</a>
</li>
<li class="nav-item">
<a class="nav-link" href="results.php">Results</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-5">
<h1 class="text-center mb-4">Coach's Dashboard</h1>
<div class="card shadow-sm">
<div class="card-body">
<h2 class="card-title">Team: Sharks</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Player</th>
<th>Last Round Score</th>
<th>Handicap</th>
<th>View History</th>
</tr>
</thead>
<tbody>
<tr>
<td>Jane Smith</td>
<td>85</td>
<td>12</td>
<td><a href="#" class="btn btn-sm btn-primary">View Details</a></td>
</tr>
<tr>
<td>Mike Johnson</td>
<td>92</td>
<td>18</td>
<td><a href="#" class="btn btn-sm btn-primary">View Details</a></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="card shadow-sm mt-4">
<div class="card-body">
<h2 class="card-title">Team: Bears</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Player</th>
<th>Last Round Score</th>
<th>Handicap</th>
<th>View History</th>
</tr>
</thead>
<tbody>
<tr>
<td>Peter Jones</td>
<td>88</td>
<td>14</td>
<td><a href="#" class="btn btn-sm btn-primary">View Details</a></td>
</tr>
</tbody>
</table>
</div>
</div> </div>
</div> </div>
</nav>
<footer class="bg-light text-center text-lg-start mt-5"> <div class="container mt-5">
<div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.05);"> <h1 class="text-center mb-4">Coach's Dashboard</h1>
© 2025 Golf Tournament
<?php foreach ($teams as $team): ?>
<div class="card shadow-sm mt-4">
<div class="card-body">
<h2 class="card-title">Team: <?= htmlspecialchars($team['name']) ?></h2>
<?php
$stmt = $pdo->prepare('
SELECT p.name as player_name, s.id as score_id, s.total_score, c.name as course_name, s.played_at
FROM scores s
JOIN players p ON s.player_id = p.id
JOIN courses c ON s.course_id = c.id
WHERE s.team_id = ?
ORDER BY s.played_at DESC
');
$stmt->execute([$team['id']]);
$scores = $stmt->fetchAll();
?>
<?php if ($scores): ?>
<table class="table table-striped">
<thead>
<tr>
<th>Player</th>
<th>Course</th>
<th>Score</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($scores as $score): ?>
<tr>
<td><?= htmlspecialchars($score['player_name']) ?></td>
<td><?= htmlspecialchars($score['course_name']) ?></td>
<td><?= htmlspecialchars($score['total_score']) ?></td>
<td><?= date('Y-m-d', strtotime($score['played_at'])) ?></td>
<td><a href="edit_score.php?score_id=<?= $score['score_id'] ?>" class="btn btn-sm btn-primary">Edit</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>No scores recorded for this team yet.</p>
<?php endif; ?>
</div>
</div> </div>
</footer> <?php endforeach; ?>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </div>
<footer class="bg-light text-center text-lg-start mt-5">
<div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.05);">
© 2025 Golf Tournament
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body> </body>
</html> </html>

56
db/migrate.php Normal file
View File

@ -0,0 +1,56 @@
<?php
require_once __DIR__ . '/config.php';
try {
$pdo = db();
// Alter table engine to InnoDB
$pdo->exec("ALTER TABLE scores ENGINE=InnoDB");
echo "Table 'scores' engine converted to InnoDB.<br>";
// Check if player_id column exists
$stmt = $pdo->query("SHOW COLUMNS FROM scores LIKE 'player_id'");
if ($stmt->rowCount() == 0) {
// Add player_id column
$pdo->exec("ALTER TABLE scores ADD COLUMN player_id INT NOT NULL AFTER course_id");
// Add foreign key constraint
$pdo->exec("ALTER TABLE scores ADD FOREIGN KEY (player_id) REFERENCES players(id)");
echo "Column 'player_id' added to 'scores' table and foreign key created.<br>";
} else {
echo "Column 'player_id' already exists in 'scores' table.<br>";
}
// Check if team_id column exists
$stmt = $pdo->query("SHOW COLUMNS FROM scores LIKE 'team_id'");
if ($stmt->rowCount() == 0) {
// Add team_id column
$pdo->exec("ALTER TABLE scores ADD COLUMN team_id INT NOT NULL AFTER player_id");
// Add foreign key constraint
$pdo->exec("ALTER TABLE scores ADD FOREIGN KEY (team_id) REFERENCES teams(id)");
echo "Column 'team_id' added to 'scores' table and foreign key created.<br>";
} else {
echo "Column 'team_id' already exists in 'scores' table.<br>";
}
// Safely remove player_name and team_name if they exist
$stmt = $pdo->query("SHOW COLUMNS FROM scores LIKE 'player_name'");
if ($stmt->rowCount() > 0) {
$pdo->exec("ALTER TABLE scores DROP COLUMN player_name");
echo "Column 'player_name' removed from 'scores' table.<br>";
}
$stmt = $pdo->query("SHOW COLUMNS FROM scores LIKE 'team_name'");
if ($stmt->rowCount() > 0) {
$pdo->exec("ALTER TABLE scores DROP COLUMN team_name");
echo "Column 'team_name' removed from 'scores' table.<br>";
}
} catch (PDOException $e) {
die("DB ERROR: " . $e->getMessage());
}
?>

View File

@ -28,7 +28,7 @@ try {
hole_count INT NOT NULL DEFAULT 18, hole_count INT NOT NULL DEFAULT 18,
tournament_date DATE, tournament_date DATE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ) ENGINE=InnoDB;
"); ");
echo "Table 'courses' created successfully or already exists.<br>"; echo "Table 'courses' created successfully or already exists.<br>";
@ -61,7 +61,7 @@ try {
total_to_par INT, total_to_par INT,
played_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, played_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (course_id) REFERENCES courses(id) FOREIGN KEY (course_id) REFERENCES courses(id)
) ) ENGINE=InnoDB;
"); ");
echo "Table 'scores' created successfully or already exists.<br>"; echo "Table 'scores' created successfully or already exists.<br>";
@ -83,3 +83,60 @@ try {
} catch (PDOException $e) { } catch (PDOException $e) {
die("DB ERROR: " . $e->getMessage()); die("DB ERROR: " . $e->getMessage());
} }
// New tables for coaches and players
try {
$pdo = db();
// Coaches table
$pdo->exec("
CREATE TABLE IF NOT EXISTS coaches (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL, -- Hashed password
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;
");
echo "Table 'coaches' created successfully or already exists.<br>";
// Players table
$pdo->exec("
CREATE TABLE IF NOT EXISTS players (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
handicap INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;
");
echo "Table 'players' created successfully or already exists.<br>";
// Team table to link coaches and players
$pdo->exec("
CREATE TABLE IF NOT EXISTS teams (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
coach_id INT NOT NULL,
FOREIGN KEY (coach_id) REFERENCES coaches(id)
) ENGINE=InnoDB;
");
echo "Table 'teams' created successfully or already exists.<br>";
// Team_members table to link players to a team
$pdo->exec("
CREATE TABLE IF NOT EXISTS team_members (
id INT AUTO_INCREMENT PRIMARY KEY,
team_id INT NOT NULL,
player_id INT NOT NULL,
FOREIGN KEY (team_id) REFERENCES teams(id),
FOREIGN KEY (player_id) REFERENCES players(id)
) ENGINE=InnoDB;
");
echo "Table 'team_members' created successfully or already exists.<br>";
} catch (PDOException $e) {
die("DB ERROR: " . $e->getMessage());
}
?>

53
edit_score.php Normal file
View File

@ -0,0 +1,53 @@
<?php
require_once __DIR__ . '/db/config.php';
$score_id = $_GET['score_id'] ?? null;
if (!$score_id) {
die('Score ID is required.');
}
$pdoc = db();
$stmt = $pdoc->prepare('SELECT s.*, p.name as player_name, c.name as course_name, c.hole_count FROM scores s JOIN players p ON s.player_id = p.id JOIN courses c ON s.course_id = c.id WHERE s.id = ?');
$stmt->execute([$score_id]);
$score = $stmt->fetch();
if (!$score) {
die('Score not found.');
}
$pars = [];
$stmt = $pdoc->prepare('SELECT * FROM courses WHERE id = ?');
$stmt->execute([$score['course_id']]);
$course = $stmt->fetch();
for ($i = 1; $i <= 18; $i++) {
$pars[$i] = $course['par_hole_' . $i];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Score</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h1>Edit Score for <?= htmlspecialchars($score['player_name']) ?> on <?= htmlspecialchars($score['course_name']) ?></h1>
<form action="submit_score.php" method="post">
<input type="hidden" name="score_id" value="<?= $score_id ?>">
<input type="hidden" name="action" value="update">
<div class="row">
<?php for ($i = 1; $i <= $score['hole_count']; $i++): ?>
<div class="col-md-4 mb-3">
<label for="hole_<?= $i ?>" class="form-label">Hole <?= $i ?> (Par <?= $pars[$i] ?>)</label>
<input type="number" class="form-control" id="hole_<?= $i ?>" name="scores[<?= $i ?>]" value="<?= htmlspecialchars($score['hole_' . $i . '_score']) ?>" min="1" required>
</div>
<?php endfor; ?>
</div>
<button type="submit" class="btn btn-primary">Update Score</button>
</form>
</div>
</body>
</html>

View File

@ -41,52 +41,115 @@
<div class="container mt-5"> <div class="container mt-5">
<h1 class="text-center mb-4">Tournament Leaderboard</h1> <h1 class="text-center mb-4">Tournament Leaderboard</h1>
<?php
require_once 'db/config.php';
$pdo = db();
// Fetch all courses for the dropdown
$courses_stmt = $pdo->query("SELECT id, name FROM courses ORDER BY created_at DESC");
$courses = $courses_stmt->fetchAll();
$selected_course_id = isset($_GET['course_id']) ? (int)$_GET['course_id'] : ($courses[0]['id'] ?? 0);
?>
<div class="row mb-4">
<div class="col-md-6 mx-auto">
<form action="results.php" method="GET" class="d-flex">
<select name="course_id" class="form-select me-2">
<?php foreach ($courses as $course): ?>
<option value="<?php echo $course['id']; ?>" <?php echo $selected_course_id == $course['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($course['name']); ?>
</option>
<?php endforeach; ?>
</select>
<button type="submit" class="btn btn-primary">View</button>
</form>
</div>
</div>
<div class="card shadow-sm"> <div class="card shadow-sm">
<div class="card-body"> <div class="card-body">
<h2 class="card-title">Individual Standings</h2> <h2 class="card-title">Individual Standings</h2>
<?php <?php
require_once 'db/config.php'; if ($selected_course_id) {
try { try {
$pdo = db(); // Fetch course details for pars
$stmt = $pdo->query(" $course_stmt = $pdo->prepare("SELECT * FROM courses WHERE id = ?");
SELECT player_name, team_name, SUM(total_score) as total_score, SUM(total_to_par) as total_to_par $course_stmt->execute([$selected_course_id]);
FROM scores $course_details = $course_stmt->fetch();
GROUP BY player_name, team_name
ORDER BY total_to_par ASC // Fetch scores for the selected course
"); $stmt = $pdo->prepare("
$results = $stmt->fetchAll(); SELECT *
} catch (PDOException $e) { FROM scores
WHERE course_id = ?
ORDER BY total_to_par ASC
");
$stmt->execute([$selected_course_id]);
$results = $stmt->fetchAll();
} catch (PDOException $e) {
$results = [];
$course_details = null;
}
} else {
$results = []; $results = [];
$course_details = null;
} }
?> ?>
<table class="table table-striped"> <div class="table-responsive">
<thead> <table class="table table-striped table-bordered">
<tr> <thead class="table-dark">
<th>Rank</th>
<th>Player</th>
<th>Team</th>
<th>Total Score</th>
<th>To Par</th>
</tr>
</thead>
<tbody>
<?php if (empty($results)): ?>
<tr> <tr>
<td colspan="5" class="text-center">No results yet.</td> <th>Rank</th>
<th>Player</th>
<?php if ($course_details): ?>
<?php for ($i = 1; $i <= 18; $i++): ?>
<th class="text-center">
<?php echo $i; ?><br>
<small>(<?php echo $course_details['par_hole_' . $i]; ?>)</small>
</th>
<?php endfor; ?>
<?php endif; ?>
<th>Total</th>
<th>To Par</th>
</tr> </tr>
<?php else: ?> </thead>
<?php foreach ($results as $index => $row): ?> <tbody>
<?php if (empty($results)): ?>
<tr> <tr>
<td><?php echo $index + 1; ?></td> <td colspan="<?php echo $course_details ? 21 : 3; ?>" class="text-center">No results yet for this course.</td>
<td><a href="player.php?player=<?php echo urlencode($row['player_name']); ?>"><?php echo htmlspecialchars($row['player_name']); ?></a></td>
<td><?php echo htmlspecialchars($row['team_name']); ?></td>
<td><?php echo $row['total_score']; ?></td>
<td><?php echo ($row['total_to_par'] > 0 ? '+' : '') . $row['total_to_par']; ?></td>
</tr> </tr>
<?php endforeach; ?> <?php else: ?>
<?php endif; ?> <?php foreach ($results as $index => $row): ?>
</tbody> <tr>
</table> <td><?php echo $index + 1; ?></td>
<td><a href="player.php?player=<?php echo urlencode($row['player_name']); ?>"><?php echo htmlspecialchars($row['player_name']); ?></a></td>
<?php if ($course_details): ?>
<?php for ($i = 1; $i <= 18; $i++): ?>
<td class="text-center">
<?php
$score = $row['hole_' . $i . '_score'];
$par = $course_details['par_hole_' . $i];
$class = '';
if ($score !== null) {
if ($score < $par) $class = 'bg-success text-white'; // Birdie or better
elseif ($score > $par) $class = 'bg-warning'; // Bogey
else $class = 'bg-light'; // Par
}
echo "<span class='{$class} d-block'>{$score}</span>";
?>
</td>
<?php endfor; ?>
<?php endif; ?>
<td><?php echo $row['total_score']; ?></td>
<td><?php echo ($row['total_to_par'] > 0 ? '+' : '') . $row['total_to_par']; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div> </div>
</div> </div>
@ -94,16 +157,21 @@
<div class="card-body"> <div class="card-body">
<h2 class="card-title">Team Standings</h2> <h2 class="card-title">Team Standings</h2>
<?php <?php
try { if ($selected_course_id) {
$stmt = $pdo->query(" try {
SELECT team_name, SUM(total_score) as total_score $stmt = $pdo->prepare("
FROM scores SELECT team_name, SUM(total_score) as total_score
WHERE team_name IS NOT NULL AND team_name != '' FROM scores
GROUP BY team_name WHERE course_id = ? AND team_name IS NOT NULL AND team_name != ''
ORDER BY total_score ASC GROUP BY team_name
"); ORDER BY total_score ASC
$team_results = $stmt->fetchAll(); ");
} catch (PDOException $e) { $stmt->execute([$selected_course_id]);
$team_results = $stmt->fetchAll();
} catch (PDOException $e) {
$team_results = [];
}
} else {
$team_results = []; $team_results = [];
} }
?> ?>

View File

@ -3,10 +3,66 @@ require_once 'db/config.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405); http_response_code(405);
echo json_encode(['error' => 'Method Not Allowed']); die('Method Not Allowed');
exit;
} }
// 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); $data = json_decode(file_get_contents('php://input'), true);
if (!$data) { if (!$data) {
@ -27,6 +83,10 @@ foreach ($required_fields as $field) {
try { try {
$pdo = db(); $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"; $sql = "INSERT INTO scores (player_name, team_name, course_id, holes_played, total_score, total_to_par";
$params = [ $params = [
':player_name' => $data['playerName'], ':player_name' => $data['playerName'],