This commit is contained in:
Flatlogic Bot 2025-09-24 19:44:13 +00:00
parent 354c764c97
commit 6424a2c0f6
9 changed files with 324 additions and 44 deletions

44
add_player.php Normal file
View File

@ -0,0 +1,44 @@
<?php
require_once __DIR__ . '/db/config.php';
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$team_id = $_POST['team_id'];
$player_name = trim($_POST['player_name']);
$player_email = trim($_POST['player_email']);
$high_school_year = trim($_POST['high_school_year']);
$season_year = trim($_POST['season_year']);
if (!empty($player_name) && !empty($player_email) && !empty($team_id)) {
try {
$pdo = db();
// Check if player already exists
$stmt = $pdo->prepare("SELECT id FROM players WHERE email = ?");
$stmt->execute([$player_email]);
$player = $stmt->fetch();
if ($player) {
$player_id = $player['id'];
} else {
// Insert new player
$stmt = $pdo->prepare("INSERT INTO players (name, email, high_school_year, season_year) VALUES (?, ?, ?, ?)");
$stmt->execute([$player_name, $player_email, $high_school_year, $season_year]);
$player_id = $pdo->lastInsertId();
}
// Add player to team
$stmt = $pdo->prepare("INSERT INTO team_members (team_id, player_id) VALUES (?, ?)");
$stmt->execute([$team_id, $player_id]);
$_SESSION['success_message'] = 'Player added successfully!';
} catch (PDOException $e) {
$_SESSION['error_message'] = 'Error adding player: ' . $e->getMessage();
}
}
header('Location: coach.php');
exit;
}
?>

View File

@ -1,14 +1,19 @@
<?php
require_once 'db/config.php';
// Fetch existing courses
$courses = [];
$players = [];
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, name FROM courses ORDER BY name");
$courses = $stmt->fetchAll();
$stmt = $pdo->query("SELECT p.id, p.player_name, t.team_name FROM players p JOIN teams t ON p.team_id = t.id ORDER BY t.team_name, p.player_name");
$players = $stmt->fetchAll();
} catch (PDOException $e) {
$courses = [];
// Silently fail, the JS will handle the empty state
// If something goes wrong, we'll have empty arrays.
// For debugging, you might want to log the error:
// error_log($e->getMessage());
}
?>
<!DOCTYPE html>
@ -50,12 +55,27 @@ try {
<div class="row mb-3">
<div class="col-md-6">
<label for="playerName" class="form-label">Player Name</label>
<input type="text" class="form-control" id="playerName" required>
</div>
<div class="col-md-6">
<label for="teamName" class="form-label">Team Name (Optional)</label>
<input type="text" class="form-control" id="teamName">
<label for="playerSelect" class="form-label">Select Player</label>
<select class="form-select" id="playerSelect" required>
<option value="">-- Select a Player --</option>
<?php
$current_team = null;
foreach ($players as $player):
if ($player['team_name'] !== $current_team):
if ($current_team !== null):
echo '</optgroup>';
endif;
$current_team = $player['team_name'];
echo '<optgroup label="' . htmlspecialchars($current_team) . '">';
endif;
?>
<option value="<?php echo $player['id']; ?>"><?php echo htmlspecialchars($player['player_name']); ?></option>
<?php endforeach;
if ($current_team !== null):
echo '</optgroup>';
endif;
?>
</select>
</div>
</div>

View File

@ -169,9 +169,10 @@ document.addEventListener('DOMContentLoaded', function () {
form.addEventListener('submit', async function(e) {
e.preventDefault();
const playerName = document.getElementById('playerName').value;
if (!playerName) {
alert('Player name is required.');
const playerSelect = document.getElementById('playerSelect');
const playerId = playerSelect.value;
if (!playerId) {
alert('Please select a player.');
return;
}
@ -182,8 +183,7 @@ document.addEventListener('DOMContentLoaded', function () {
}
const data = {
playerName: playerName,
teamName: document.getElementById('teamName').value,
playerId: playerId,
courseId: courseSelect.value,
holes: holes,
scores: scores,
@ -210,6 +210,7 @@ document.addEventListener('DOMContentLoaded', function () {
}
form.reset();
courseSelect.value = '';
playerSelect.value = '';
coursePars = null;
renderTable();
toggleInputs(true);

111
coach.php
View File

@ -1,6 +1,8 @@
<?php
require_once __DIR__ . '/db/config.php';
session_start();
// Assume a logged-in coach with ID 1
$coach_id = 1;
@ -11,6 +13,12 @@ $stmt = $pdo->prepare('SELECT * FROM teams WHERE coach_id = ?');
$stmt->execute([$coach_id]);
$teams = $stmt->fetchAll();
$success_message = $_SESSION['success_message'] ?? null;
$error_message = $_SESSION['error_message'] ?? null;
unset($_SESSION['success_message']);
unset($_SESSION['error_message']);
?>
<!DOCTYPE html>
<html lang="en">
@ -41,48 +49,107 @@ $teams = $stmt->fetchAll();
<div class="container mt-5">
<h1 class="text-center mb-4">Coach's Dashboard</h1>
<?php if ($success_message): ?>
<div class="alert alert-success">
<?= $success_message ?>
</div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger">
<?= $error_message ?>
</div>
<?php endif; ?>
<div class="card shadow-sm mb-4">
<div class="card-body">
<h2 class="card-title">Create New Team</h2>
<form action="create_team.php" method="post">
<div class="input-group">
<input type="text" name="team_name" class="form-control" placeholder="Enter new team name" required>
<button type="submit" class="btn btn-primary">Create Team</button>
</div>
</form>
</div>
</div>
<?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>
<h3 class="mt-4">Players</h3>
<?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 = $pdo->prepare('SELECT p.* FROM players p JOIN team_members tm ON p.id = tm.player_id WHERE tm.team_id = ?');
$stmt->execute([$team['id']]);
$scores = $stmt->fetchAll();
$players = $stmt->fetchAll();
?>
<?php if ($scores): ?>
<?php if ($players): ?>
<table class="table table-striped">
<thead>
<tr>
<th>Player</th>
<th>Course</th>
<th>Score</th>
<th>Date</th>
<th>Action</th>
<th>Name</th>
<th>Email</th>
<th>High School Year</th>
<th>Season Year</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($scores as $score): ?>
<?php foreach ($players as $player): ?>
<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>
<td><?= htmlspecialchars($player['name']) ?></td>
<td><?= htmlspecialchars($player['email']) ?></td>
<td><?= htmlspecialchars($player['high_school_year']) ?></td>
<td><?= htmlspecialchars($player['season_year']) ?></td>
<td>
<a href="edit_player.php?id=<?= $player['id'] ?>" class="btn btn-sm btn-primary">Edit</a>
<a href="delete_player.php?player_id=<?= $player['id'] ?>&team_id=<?= $team['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to remove this player from the team?')">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>No scores recorded for this team yet.</p>
<p>No players in this team yet.</p>
<?php endif; ?>
<hr>
<h3 class="mt-4">Add Player to Team</h3>
<form action="add_player.php" method="post">
<input type="hidden" name="team_id" value="<?= $team['id'] ?>">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="player_name">Player Name</label>
<input type="text" name="player_name" class="form-control" required>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="player_email">Player Email</label>
<input type="email" name="player_email" class="form-control" required>
</div>
</div>
</div>
<div class="row mt-2">
<div class="col-md-6">
<div class="form-group">
<label for="high_school_year">High School Year</label>
<input type="text" name="high_school_year" class="form-control">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="season_year">Season Year</label>
<input type="text" name="season_year" class="form-control">
</div>
</div>
</div>
<button type="submit" class="btn btn-primary mt-3">Add Player</button>
</form>
</div>
</div>
<?php endforeach; ?>

26
create_team.php Normal file
View File

@ -0,0 +1,26 @@
<?php
require_once __DIR__ . '/db/config.php';
session_start();
// Assuming a logged-in coach with ID 1
$coach_id = 1;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$team_name = trim($_POST['team_name']);
if (!empty($team_name)) {
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO teams (name, coach_id) VALUES (?, ?)");
$stmt->execute([$team_name, $coach_id]);
$_SESSION['success_message'] = 'Team created successfully!';
} catch (PDOException $e) {
$_SESSION['error_message'] = 'Error creating team: ' . $e->getMessage();
}
}
header('Location: coach.php');
exit;
}
?>

View File

@ -0,0 +1,15 @@
<?php
require_once __DIR__ . '/../config.php';
try {
$pdo = db();
$pdo->exec("
ALTER TABLE players
ADD COLUMN high_school_year VARCHAR(255),
ADD COLUMN season_year VARCHAR(255);
");
echo "Table 'players' updated successfully with 'high_school_year' and 'season_year' columns.\n";
} catch (PDOException $e) {
die("DB ERROR: " . $e->getMessage());
}
?>

22
delete_player.php Normal file
View File

@ -0,0 +1,22 @@
<?php
require_once __DIR__ . '/db/config.php';
session_start();
$player_id = $_GET['player_id'] ?? null;
$team_id = $_GET['team_id'] ?? null;
if ($player_id && $team_id) {
try {
$pdo = db();
$stmt = $pdo->prepare("DELETE FROM team_members WHERE player_id = ? AND team_id = ?");
$stmt->execute([$player_id, $team_id]);
$_SESSION['success_message'] = 'Player removed from team successfully!';
} catch (PDOException $e) {
$_SESSION['error_message'] = 'Error removing player: ' . $e->getMessage();
}
}
header('Location: coach.php');
exit;
?>

76
edit_player.php Normal file
View File

@ -0,0 +1,76 @@
<?php
require_once __DIR__ . '/db/config.php';
session_start();
$player_id = $_GET['id'] ?? null;
if (!$player_id) {
header('Location: coach.php');
exit;
}
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM players WHERE id = ?");
$stmt->execute([$player_id]);
$player = $stmt->fetch();
if (!$player) {
header('Location: coach.php');
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$player_name = trim($_POST['player_name']);
$player_email = trim($_POST['player_email']);
$high_school_year = trim($_POST['high_school_year']);
$season_year = trim($_POST['season_year']);
if (!empty($player_name) && !empty($player_email)) {
try {
$stmt = $pdo->prepare("UPDATE players SET name = ?, email = ?, high_school_year = ?, season_year = ? WHERE id = ?");
$stmt->execute([$player_name, $player_email, $high_school_year, $season_year, $player_id]);
$_SESSION['success_message'] = 'Player updated successfully!';
} catch (PDOException $e) {
$_SESSION['error_message'] = 'Error updating player: ' . $e->getMessage();
}
}
header('Location: coach.php');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit Player</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h2>Edit Player</h2>
<form action="edit_player.php?id=<?php echo $player_id; ?>" method="post">
<div class="form-group">
<label for="player_name">Player Name</label>
<input type="text" name="player_name" id="player_name" class="form-control" value="<?php echo htmlspecialchars($player['name']); ?>" required>
</div>
<div class="form-group">
<label for="player_email">Player Email</label>
<input type="email" name="player_email" id="player_email" class="form-control" value="<?php echo htmlspecialchars($player['email']); ?>" required>
</div>
<div class="form-group">
<label for="high_school_year">High School Year</label>
<input type="text" name="high_school_year" id="high_school_year" class="form-control" value="<?php echo htmlspecialchars($player['high_school_year']); ?>">
</div>
<div class="form-group">
<label for="season_year">Season Year</label>
<input type="text" name="season_year" id="season_year" class="form-control" value="<?php echo htmlspecialchars($player['season_year']); ?>">
</div>
<button type="submit" class="btn btn-primary">Update Player</button>
</form>
</div>
</body>
</html>

View File

@ -71,7 +71,7 @@ if (!$data) {
exit;
}
$required_fields = ['playerName', 'courseId', 'holes', 'scores'];
$required_fields = ['playerId', 'courseId', 'holes', 'scores'];
foreach ($required_fields as $field) {
if (empty($data[$field])) {
http_response_code(400);
@ -83,14 +83,23 @@ foreach ($required_fields as $field) {
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.
// Get team_id from player_id
$stmt = $pdo->prepare("SELECT team_id FROM players WHERE id = ?");
$stmt->execute([$data['playerId']]);
$player = $stmt->fetch();
$sql = "INSERT INTO scores (player_name, team_name, course_id, holes_played, total_score, total_to_par";
if (!$player) {
http_response_code(404);
echo json_encode(['error' => 'Player not found']);
exit;
}
$team_id = $player['team_id'];
$sql = "INSERT INTO scores (player_id, team_id, course_id, holes_played, total_score, total_to_par";
$params = [
':player_name' => $data['playerName'],
':team_name' => $data['teamName'] ?? null,
':player_id' => $data['playerId'],
':team_id' => $team_id,
':course_id' => $data['courseId'],
':holes_played' => $data['holes'],
':total_score' => $data['totalScore'],