This commit is contained in:
Flatlogic Bot 2025-09-24 21:22:36 +00:00
parent 6424a2c0f6
commit 0f8fd03d51
13 changed files with 209 additions and 35 deletions

View File

@ -23,15 +23,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$player_id = $player['id']; $player_id = $player['id'];
} else { } else {
// Insert new player // Insert new player
$stmt = $pdo->prepare("INSERT INTO players (name, email, high_school_year, season_year) VALUES (?, ?, ?, ?)"); $stmt = $pdo->prepare("INSERT INTO players (name, email, high_school_year, season_year, team_id) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$player_name, $player_email, $high_school_year, $season_year]); $stmt->execute([$player_name, $player_email, $high_school_year, $season_year, $team_id]);
$player_id = $pdo->lastInsertId(); $player_id = $pdo->lastInsertId();
} }
// Add player to team // Update player's team_id if they already existed but weren't assigned to this team
$stmt = $pdo->prepare("INSERT INTO team_members (team_id, player_id) VALUES (?, ?)"); $stmt = $pdo->prepare("UPDATE players SET team_id = ? WHERE id = ?");
$stmt->execute([$team_id, $player_id]); $stmt->execute([$team_id, $player_id]);
$_SESSION['success_message'] = 'Player added successfully!'; $_SESSION['success_message'] = 'Player added successfully!';
} catch (PDOException $e) { } catch (PDOException $e) {
$_SESSION['error_message'] = 'Error adding player: ' . $e->getMessage(); $_SESSION['error_message'] = 'Error adding player: ' . $e->getMessage();

View File

@ -8,7 +8,7 @@ try {
$stmt = $pdo->query("SELECT id, name FROM courses ORDER BY name"); $stmt = $pdo->query("SELECT id, name FROM courses ORDER BY name");
$courses = $stmt->fetchAll(); $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"); $stmt = $pdo->query("SELECT p.id, p.team_id, p.name AS player_name, t.name AS team_name FROM players p JOIN teams t ON p.team_id = t.id ORDER BY team_name, player_name");
$players = $stmt->fetchAll(); $players = $stmt->fetchAll();
} catch (PDOException $e) { } catch (PDOException $e) {
// If something goes wrong, we'll have empty arrays. // If something goes wrong, we'll have empty arrays.
@ -69,7 +69,7 @@ try {
echo '<optgroup label="' . htmlspecialchars($current_team) . '">'; echo '<optgroup label="' . htmlspecialchars($current_team) . '">';
endif; endif;
?> ?>
<option value="<?php echo $player['id']; ?>"><?php echo htmlspecialchars($player['player_name']); ?></option> <option value="<?php echo $player['id']; ?>" data-team-id="<?php echo $player['team_id']; ?>"><?php echo htmlspecialchars($player['player_name']); ?></option>
<?php endforeach; <?php endforeach;
if ($current_team !== null): if ($current_team !== null):
echo '</optgroup>'; echo '</optgroup>';
@ -141,7 +141,7 @@ try {
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="toast" aria-label="Close"></button> <button type="button" class="btn-close btn-close-white" data-bs-dismiss="toast" aria-label="Close"></button>
</div> </div>
<div class="toast-body"> <div class="toast-body">
Your score has been submitted successfully! (Client-side demo) Score submitted successfully.
</div> </div>
</div> </div>
</div> </div>

View File

@ -61,6 +61,21 @@ try {
$error = "Could not fetch courses: " . $e->getMessage(); $error = "Could not fetch courses: " . $e->getMessage();
} }
// Fetch players with their team names
try {
$pdo = db();
$stmt = $pdo->query("
SELECT p.id, p.name, p.email, p.high_school_year, p.season_year, t.name as team_name
FROM players p
LEFT JOIN teams t ON p.team_id = t.id
ORDER BY p.name
");
$players = $stmt->fetchAll();
} catch (PDOException $e) {
$players = [];
$error = "Could not fetch players: " . $e->getMessage();
}
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
@ -202,7 +217,45 @@ try {
<!-- Players Tab --> <!-- Players Tab -->
<div class="tab-pane fade" id="players" role="tabpanel" aria-labelledby="players-tab"> <div class="tab-pane fade" id="players" role="tabpanel" aria-labelledby="players-tab">
... <div class="card shadow-sm mt-4">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center mb-3">
<h2 class="card-title mb-0">Manage Players</h2>
<a href="add_player.php" class="btn btn-primary">Add New Player</a>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>High School Year</th>
<th>Season Year</th>
<th>Team</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($players)): ?>
<tr><td colspan="6">No players found.</td></tr>
<?php else: ?>
<?php foreach ($players as $player): ?>
<tr>
<td><?php echo htmlspecialchars($player['name']); ?></td>
<td><?php echo htmlspecialchars($player['email']); ?></td>
<td><?php echo htmlspecialchars($player['high_school_year']); ?></td>
<td><?php echo htmlspecialchars($player['season_year']); ?></td>
<td><?php echo htmlspecialchars($player['team_name'] ?? 'N/A'); ?></td>
<td>
<a href="edit_player.php?id=<?php echo $player['id']; ?>" class="btn btn-sm btn-warning">Edit</a>
<a href="delete_player.php?id=<?php echo $player['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this player?');">Delete</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -182,8 +182,12 @@ document.addEventListener('DOMContentLoaded', function () {
scores[`hole${i}_score`] = parseInt(document.getElementById(`hole${i}_score`).value) || 0; scores[`hole${i}_score`] = parseInt(document.getElementById(`hole${i}_score`).value) || 0;
} }
const selectedOption = playerSelect.options[playerSelect.selectedIndex];
const teamId = selectedOption.dataset.teamId;
const data = { const data = {
playerId: playerId, playerId: playerId,
teamId: teamId,
courseId: courseSelect.value, courseId: courseSelect.value,
holes: holes, holes: holes,
scores: scores, scores: scores,
@ -205,6 +209,11 @@ document.addEventListener('DOMContentLoaded', function () {
if (response.ok) { if (response.ok) {
const successToast = document.getElementById('successToast'); const successToast = document.getElementById('successToast');
if (successToast) { if (successToast) {
const playerName = selectedOption.text;
const toastBody = successToast.querySelector('.toast-body');
if (toastBody) {
toastBody.textContent = `Score submitted successfully for ${playerName}.`;
}
const toast = new bootstrap.Toast(successToast); const toast = new bootstrap.Toast(successToast);
toast.show(); toast.show();
} }

View File

@ -80,7 +80,7 @@ unset($_SESSION['error_message']);
<h3 class="mt-4">Players</h3> <h3 class="mt-4">Players</h3>
<?php <?php
$stmt = $pdo->prepare('SELECT p.* FROM players p JOIN team_members tm ON p.id = tm.player_id WHERE tm.team_id = ?'); $stmt = $pdo->prepare('SELECT * FROM players WHERE team_id = ?');
$stmt->execute([$team['id']]); $stmt->execute([$team['id']]);
$players = $stmt->fetchAll(); $players = $stmt->fetchAll();
?> ?>
@ -104,7 +104,7 @@ unset($_SESSION['error_message']);
<td><?= htmlspecialchars($player['season_year']) ?></td> <td><?= htmlspecialchars($player['season_year']) ?></td>
<td> <td>
<a href="edit_player.php?id=<?= $player['id'] ?>" class="btn btn-sm btn-primary">Edit</a> <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> <a href="delete_player.php?player_id=<?= $player['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> </td>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>

View File

@ -0,0 +1,12 @@
<?php
require_once __DIR__ . '/../config.php';
try {
$pdo = db();
$sql = "ALTER TABLE players ADD COLUMN team_id INT NULL AFTER season_year";
$pdo->exec($sql);
echo "Migration successful: added team_id to players table.\n";
} catch (PDOException $e) {
die("Migration failed: " . $e->getMessage() . "\n");
}

View File

@ -0,0 +1,12 @@
<?php
require_once __DIR__ . '/../config.php';
try {
$pdo = db();
$sql = "DROP TABLE IF EXISTS team_members";
$pdo->exec($sql);
echo "Migration successful: dropped team_members table.\n";
} catch (PDOException $e) {
die("Migration failed: " . $e->getMessage() . "\n");
}

View File

@ -4,13 +4,12 @@ require_once __DIR__ . '/db/config.php';
session_start(); session_start();
$player_id = $_GET['player_id'] ?? null; $player_id = $_GET['player_id'] ?? null;
$team_id = $_GET['team_id'] ?? null;
if ($player_id && $team_id) { if ($player_id) {
try { try {
$pdo = db(); $pdo = db();
$stmt = $pdo->prepare("DELETE FROM team_members WHERE player_id = ? AND team_id = ?"); $stmt = $pdo->prepare("UPDATE players SET team_id = NULL WHERE id = ?");
$stmt->execute([$player_id, $team_id]); $stmt->execute([$player_id]);
$_SESSION['success_message'] = 'Player removed from team successfully!'; $_SESSION['success_message'] = 'Player removed from team successfully!';
} catch (PDOException $e) { } catch (PDOException $e) {
$_SESSION['error_message'] = 'Error removing player: ' . $e->getMessage(); $_SESSION['error_message'] = 'Error removing player: ' . $e->getMessage();

33
delete_score.php Normal file
View File

@ -0,0 +1,33 @@
<?php
// TODO: Add authentication to ensure only admin users can access this page.
// For example:
// session_start();
// if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
// die('Access Denied: You do not have permission to perform this action.');
// }
require_once 'db/config.php';
if (isset($_GET['score_id']) && !empty($_GET['score_id'])) {
$score_id = (int)$_GET['score_id'];
try {
$pdo = db();
$stmt = $pdo->prepare("DELETE FROM scores WHERE id = ?");
$stmt->execute([$score_id]);
// Redirect back to the results page
header("Location: results.php?delete_success=1");
exit;
} catch (PDOException $e) {
// Optional: handle error, e.g., log it or show a generic error message
die("Error: Could not delete the score. " . $e->getMessage());
}
} else {
// No score_id provided
header("Location: results.php?delete_error=1");
exit;
}
?>

View File

@ -6,37 +6,45 @@ session_start();
$player_id = $_GET['id'] ?? null; $player_id = $_GET['id'] ?? null;
if (!$player_id) { if (!$player_id) {
header('Location: coach.php'); header('Location: admin.php'); // Redirect to admin page if no player ID
exit; exit;
} }
$pdo = db(); $pdo = db();
// Fetch player details
$stmt = $pdo->prepare("SELECT * FROM players WHERE id = ?"); $stmt = $pdo->prepare("SELECT * FROM players WHERE id = ?");
$stmt->execute([$player_id]); $stmt->execute([$player_id]);
$player = $stmt->fetch(); $player = $stmt->fetch();
if (!$player) { if (!$player) {
header('Location: coach.php'); header('Location: admin.php'); // Redirect if player not found
exit; exit;
} }
// Fetch all teams
$teams_stmt = $pdo->query("SELECT id, team_name FROM teams ORDER BY team_name");
$teams = $teams_stmt->fetchAll();
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$player_name = trim($_POST['player_name']); $player_name = trim($_POST['player_name']);
$player_email = trim($_POST['player_email']); $player_email = trim($_POST['player_email']);
$high_school_year = trim($_POST['high_school_year']); $high_school_year = trim($_POST['high_school_year']);
$season_year = trim($_POST['season_year']); $season_year = trim($_POST['season_year']);
$team_id = $_POST['team_id'] ?? null; // Get team_id from form
if (!empty($player_name) && !empty($player_email)) { if (!empty($player_name) && !empty($player_email)) {
try { try {
$stmt = $pdo->prepare("UPDATE players SET name = ?, email = ?, high_school_year = ?, season_year = ? WHERE id = ?"); // Update player details, including team_id
$stmt->execute([$player_name, $player_email, $high_school_year, $season_year, $player_id]); $stmt = $pdo->prepare("UPDATE players SET name = ?, email = ?, high_school_year = ?, season_year = ?, team_id = ? WHERE id = ?");
$stmt->execute([$player_name, $player_email, $high_school_year, $season_year, $team_id, $player_id]);
$_SESSION['success_message'] = 'Player updated successfully!'; $_SESSION['success_message'] = 'Player updated successfully!';
} catch (PDOException $e) { } catch (PDOException $e) {
$_SESSION['error_message'] = 'Error updating player: ' . $e->getMessage(); $_SESSION['error_message'] = 'Error updating player: ' . $e->getMessage();
} }
} }
header('Location: coach.php'); header('Location: admin.php'); // Redirect back to admin page
exit; exit;
} }
@ -69,6 +77,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
<label for="season_year">Season Year</label> <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']); ?>"> <input type="text" name="season_year" id="season_year" class="form-control" value="<?php echo htmlspecialchars($player['season_year']); ?>">
</div> </div>
<div class="form-group">
<label for="team_id">Team</label>
<select name="team_id" id="team_id" class="form-control">
<option value="">Select a team</option>
<?php foreach ($teams as $team): ?>
<option value="<?php echo $team['id']; ?>" <?php echo ($player['team_id'] == $team['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($team['team_name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary">Update Player</button> <button type="submit" class="btn btn-primary">Update Player</button>
</form> </form>
</div> </div>

View File

@ -1,6 +1,13 @@
<?php <?php
require_once __DIR__ . '/db/config.php'; require_once __DIR__ . '/db/config.php';
session_start();
// TODO: Add role-based authentication check here.
// For example, check if $_SESSION['user_role'] is 'admin' or 'coach'.
// if (!isset($_SESSION['user_role']) || !in_array($_SESSION['user_role'], ['admin', 'coach'])) {
// die('Access denied. You do not have permission to edit scores.');
// }
$score_id = $_GET['score_id'] ?? null; $score_id = $_GET['score_id'] ?? null;
if (!$score_id) { if (!$score_id) {
die('Score ID is required.'); die('Score ID is required.');

View File

@ -46,7 +46,7 @@
$pdo = db(); $pdo = db();
// Fetch all courses for the dropdown // Fetch all courses for the dropdown
$courses_stmt = $pdo->query("SELECT id, name FROM courses ORDER BY created_at DESC"); $courses_stmt = $pdo->query("SELECT id, name FROM courses ORDER BY name ASC");
$courses = $courses_stmt->fetchAll(); $courses = $courses_stmt->fetchAll();
$selected_course_id = isset($_GET['course_id']) ? (int)$_GET['course_id'] : ($courses[0]['id'] ?? 0); $selected_course_id = isset($_GET['course_id']) ? (int)$_GET['course_id'] : ($courses[0]['id'] ?? 0);
@ -70,7 +70,7 @@
<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 text-center">Individual Standings</h2>
<?php <?php
if ($selected_course_id) { if ($selected_course_id) {
try { try {
@ -81,16 +81,18 @@
// Fetch scores for the selected course // Fetch scores for the selected course
$stmt = $pdo->prepare(" $stmt = $pdo->prepare("
SELECT * SELECT s.*, p.name as player_name
FROM scores FROM scores s
WHERE course_id = ? JOIN players p ON s.player_id = p.id
ORDER BY total_to_par ASC WHERE s.course_id = ?
ORDER BY s.total_to_par ASC
"); ");
$stmt->execute([$selected_course_id]); $stmt->execute([$selected_course_id]);
$results = $stmt->fetchAll(); $results = $stmt->fetchAll();
} catch (PDOException $e) { } catch (PDOException $e) {
$results = []; $results = [];
$course_details = null; $course_details = null;
echo "<p class='text-danger text-center'>Database error: " . $e->getMessage() . "</p>";
} }
} else { } else {
$results = []; $results = [];
@ -113,18 +115,19 @@
<?php endif; ?> <?php endif; ?>
<th>Total</th> <th>Total</th>
<th>To Par</th> <th>To Par</th>
<th>Actions</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php if (empty($results)): ?> <?php if (empty($results)): ?>
<tr> <tr>
<td colspan="<?php echo $course_details ? 21 : 3; ?>" class="text-center">No results yet for this course.</td> <td colspan="<?php echo $course_details ? 23 : 5; ?>" class="text-center">No results yet for this course.</td>
</tr> </tr>
<?php else: ?> <?php else: ?>
<?php foreach ($results as $index => $row): ?> <?php foreach ($results as $index => $row): ?>
<tr> <tr>
<td><?php echo $index + 1; ?></td> <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> <td><a href="player.php?id=<?php echo $row['player_id']; ?>"><?php echo htmlspecialchars($row['player_name']); ?></a></td>
<?php if ($course_details): ?> <?php if ($course_details): ?>
<?php for ($i = 1; $i <= 18; $i++): ?> <?php for ($i = 1; $i <= 18; $i++): ?>
<td class="text-center"> <td class="text-center">
@ -144,6 +147,10 @@
<?php endif; ?> <?php endif; ?>
<td><?php echo $row['total_score']; ?></td> <td><?php echo $row['total_score']; ?></td>
<td><?php echo ($row['total_to_par'] > 0 ? '+' : '') . $row['total_to_par']; ?></td> <td><?php echo ($row['total_to_par'] > 0 ? '+' : '') . $row['total_to_par']; ?></td>
<td>
<a href="edit_score.php?score_id=<?php echo $row['id']; ?>" class="btn btn-sm btn-primary">Edit</a>
<a href="delete_score.php?score_id=<?php echo $row['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this score?');">Delete</a>
</td>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
<?php endif; ?> <?php endif; ?>
@ -155,21 +162,23 @@
<div class="card shadow-sm mt-4"> <div class="card shadow-sm mt-4">
<div class="card-body"> <div class="card-body">
<h2 class="card-title">Team Standings</h2> <h2 class="card-title text-center">Team Standings</h2>
<?php <?php
if ($selected_course_id) { if ($selected_course_id) {
try { try {
$stmt = $pdo->prepare(" $stmt = $pdo->prepare("
SELECT team_name, SUM(total_score) as total_score SELECT t.name as team_name, SUM(s.total_score) as total_score
FROM scores FROM scores s
WHERE course_id = ? AND team_name IS NOT NULL AND team_name != '' JOIN teams t ON s.team_id = t.id
GROUP BY team_name WHERE s.course_id = ?
GROUP BY s.team_id, t.name
ORDER BY total_score ASC ORDER BY total_score ASC
"); ");
$stmt->execute([$selected_course_id]); $stmt->execute([$selected_course_id]);
$team_results = $stmt->fetchAll(); $team_results = $stmt->fetchAll();
} catch (PDOException $e) { } catch (PDOException $e) {
$team_results = []; $team_results = [];
echo "<p class='text-danger text-center'>Database error: " . $e->getMessage() . "</p>";
} }
} else { } else {
$team_results = []; $team_results = [];

View File

@ -8,6 +8,13 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
// Handle score update from edit_score.php // Handle score update from edit_score.php
if (isset($_POST['action']) && $_POST['action'] === 'update') { if (isset($_POST['action']) && $_POST['action'] === 'update') {
session_start();
// TODO: Add role-based authentication check here.
// For example, check if $_SESSION['user_role'] is 'admin' or 'coach'.
// if (!isset($_SESSION['user_role']) || !in_array($_SESSION['user_role'], ['admin', 'coach'])) {
// die('Access denied. You do not have permission to edit scores.');
// }
$score_id = $_POST['score_id'] ?? null; $score_id = $_POST['score_id'] ?? null;
$scores = $_POST['scores'] ?? []; $scores = $_POST['scores'] ?? [];
@ -54,7 +61,7 @@ if (isset($_POST['action']) && $_POST['action'] === 'update') {
$stmt = $pdo->prepare($sql); $stmt = $pdo->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
header('Location: coach.php'); header('Location: results.php?course_id=' . $score_info['course_id']);
exit; exit;
} catch (PDOException $e) { } catch (PDOException $e) {
@ -83,8 +90,7 @@ foreach ($required_fields as $field) {
try { try {
$pdo = db(); $pdo = db();
// Get team_id from player_id $stmt = $pdo->prepare("SELECT name, team_id FROM players WHERE id = ?");
$stmt = $pdo->prepare("SELECT team_id FROM players WHERE id = ?");
$stmt->execute([$data['playerId']]); $stmt->execute([$data['playerId']]);
$player = $stmt->fetch(); $player = $stmt->fetch();
@ -95,6 +101,19 @@ try {
} }
$team_id = $player['team_id']; $team_id = $player['team_id'];
$player_name = $player['name'];
$stmt = $pdo->prepare("SELECT name FROM teams WHERE id = ?");
$stmt->execute([$team_id]);
$team = $stmt->fetch();
if (!$team) {
http_response_code(404);
echo json_encode(['error' => 'Team not found']);
exit;
}
$team_name = $team['name'];
$sql = "INSERT INTO scores (player_id, team_id, course_id, holes_played, total_score, total_to_par"; $sql = "INSERT INTO scores (player_id, team_id, course_id, holes_played, total_score, total_to_par";
$params = [ $params = [