1.3
This commit is contained in:
parent
6424a2c0f6
commit
0f8fd03d51
@ -23,15 +23,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$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]);
|
||||
$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, $team_id]);
|
||||
$player_id = $pdo->lastInsertId();
|
||||
}
|
||||
|
||||
// Add player to team
|
||||
$stmt = $pdo->prepare("INSERT INTO team_members (team_id, player_id) VALUES (?, ?)");
|
||||
// Update player's team_id if they already existed but weren't assigned to this team
|
||||
$stmt = $pdo->prepare("UPDATE players SET team_id = ? WHERE id = ?");
|
||||
$stmt->execute([$team_id, $player_id]);
|
||||
|
||||
|
||||
|
||||
$_SESSION['success_message'] = 'Player added successfully!';
|
||||
} catch (PDOException $e) {
|
||||
$_SESSION['error_message'] = 'Error adding player: ' . $e->getMessage();
|
||||
|
||||
@ -8,7 +8,7 @@ try {
|
||||
$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");
|
||||
$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();
|
||||
} catch (PDOException $e) {
|
||||
// If something goes wrong, we'll have empty arrays.
|
||||
@ -69,7 +69,7 @@ try {
|
||||
echo '<optgroup label="' . htmlspecialchars($current_team) . '">';
|
||||
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;
|
||||
if ($current_team !== null):
|
||||
echo '</optgroup>';
|
||||
@ -141,7 +141,7 @@ try {
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="toast" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="toast-body">
|
||||
Your score has been submitted successfully! (Client-side demo)
|
||||
Score submitted successfully.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
55
admin.php
55
admin.php
@ -61,6 +61,21 @@ try {
|
||||
$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>
|
||||
<html lang="en">
|
||||
@ -202,7 +217,45 @@ try {
|
||||
|
||||
<!-- 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>
|
||||
|
||||
@ -182,8 +182,12 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
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 = {
|
||||
playerId: playerId,
|
||||
teamId: teamId,
|
||||
courseId: courseSelect.value,
|
||||
holes: holes,
|
||||
scores: scores,
|
||||
@ -205,6 +209,11 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
if (response.ok) {
|
||||
const successToast = document.getElementById('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);
|
||||
toast.show();
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ unset($_SESSION['error_message']);
|
||||
|
||||
<h3 class="mt-4">Players</h3>
|
||||
<?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']]);
|
||||
$players = $stmt->fetchAll();
|
||||
?>
|
||||
@ -104,7 +104,7 @@ unset($_SESSION['error_message']);
|
||||
<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>
|
||||
<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>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
|
||||
12
db/migrations/20250925_add_team_id_to_players.php
Normal file
12
db/migrations/20250925_add_team_id_to_players.php
Normal 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");
|
||||
}
|
||||
|
||||
12
db/migrations/20250926_drop_team_members_table.php
Normal file
12
db/migrations/20250926_drop_team_members_table.php
Normal 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");
|
||||
}
|
||||
|
||||
@ -4,13 +4,12 @@ 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) {
|
||||
if ($player_id) {
|
||||
try {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->prepare("DELETE FROM team_members WHERE player_id = ? AND team_id = ?");
|
||||
$stmt->execute([$player_id, $team_id]);
|
||||
$stmt = $pdo->prepare("UPDATE players SET team_id = NULL WHERE id = ?");
|
||||
$stmt->execute([$player_id]);
|
||||
$_SESSION['success_message'] = 'Player removed from team successfully!';
|
||||
} catch (PDOException $e) {
|
||||
$_SESSION['error_message'] = 'Error removing player: ' . $e->getMessage();
|
||||
|
||||
33
delete_score.php
Normal file
33
delete_score.php
Normal 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;
|
||||
}
|
||||
?>
|
||||
@ -6,37 +6,45 @@ session_start();
|
||||
$player_id = $_GET['id'] ?? null;
|
||||
|
||||
if (!$player_id) {
|
||||
header('Location: coach.php');
|
||||
header('Location: admin.php'); // Redirect to admin page if no player ID
|
||||
exit;
|
||||
}
|
||||
|
||||
$pdo = db();
|
||||
|
||||
// Fetch player details
|
||||
$stmt = $pdo->prepare("SELECT * FROM players WHERE id = ?");
|
||||
$stmt->execute([$player_id]);
|
||||
$player = $stmt->fetch();
|
||||
|
||||
if (!$player) {
|
||||
header('Location: coach.php');
|
||||
header('Location: admin.php'); // Redirect if player not found
|
||||
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') {
|
||||
$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']);
|
||||
$team_id = $_POST['team_id'] ?? null; // Get team_id from form
|
||||
|
||||
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]);
|
||||
// Update player details, including team_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!';
|
||||
} catch (PDOException $e) {
|
||||
$_SESSION['error_message'] = 'Error updating player: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
header('Location: coach.php');
|
||||
header('Location: admin.php'); // Redirect back to admin page
|
||||
exit;
|
||||
}
|
||||
|
||||
@ -69,6 +77,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
<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>
|
||||
<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>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@ -1,6 +1,13 @@
|
||||
<?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;
|
||||
if (!$score_id) {
|
||||
die('Score ID is required.');
|
||||
|
||||
35
results.php
35
results.php
@ -46,7 +46,7 @@
|
||||
$pdo = db();
|
||||
|
||||
// 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();
|
||||
|
||||
$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-body">
|
||||
<h2 class="card-title">Individual Standings</h2>
|
||||
<h2 class="card-title text-center">Individual Standings</h2>
|
||||
<?php
|
||||
if ($selected_course_id) {
|
||||
try {
|
||||
@ -81,16 +81,18 @@
|
||||
|
||||
// Fetch scores for the selected course
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT *
|
||||
FROM scores
|
||||
WHERE course_id = ?
|
||||
ORDER BY total_to_par ASC
|
||||
SELECT s.*, p.name as player_name
|
||||
FROM scores s
|
||||
JOIN players p ON s.player_id = p.id
|
||||
WHERE s.course_id = ?
|
||||
ORDER BY s.total_to_par ASC
|
||||
");
|
||||
$stmt->execute([$selected_course_id]);
|
||||
$results = $stmt->fetchAll();
|
||||
} catch (PDOException $e) {
|
||||
$results = [];
|
||||
$course_details = null;
|
||||
echo "<p class='text-danger text-center'>Database error: " . $e->getMessage() . "</p>";
|
||||
}
|
||||
} else {
|
||||
$results = [];
|
||||
@ -113,18 +115,19 @@
|
||||
<?php endif; ?>
|
||||
<th>Total</th>
|
||||
<th>To Par</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if (empty($results)): ?>
|
||||
<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>
|
||||
<?php else: ?>
|
||||
<?php foreach ($results as $index => $row): ?>
|
||||
<tr>
|
||||
<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 for ($i = 1; $i <= 18; $i++): ?>
|
||||
<td class="text-center">
|
||||
@ -144,6 +147,10 @@
|
||||
<?php endif; ?>
|
||||
<td><?php echo $row['total_score']; ?></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>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
@ -155,21 +162,23 @@
|
||||
|
||||
<div class="card shadow-sm mt-4">
|
||||
<div class="card-body">
|
||||
<h2 class="card-title">Team Standings</h2>
|
||||
<h2 class="card-title text-center">Team Standings</h2>
|
||||
<?php
|
||||
if ($selected_course_id) {
|
||||
try {
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT team_name, SUM(total_score) as total_score
|
||||
FROM scores
|
||||
WHERE course_id = ? AND team_name IS NOT NULL AND team_name != ''
|
||||
GROUP BY team_name
|
||||
SELECT t.name as team_name, SUM(s.total_score) as total_score
|
||||
FROM scores s
|
||||
JOIN teams t ON s.team_id = t.id
|
||||
WHERE s.course_id = ?
|
||||
GROUP BY s.team_id, t.name
|
||||
ORDER BY total_score ASC
|
||||
");
|
||||
$stmt->execute([$selected_course_id]);
|
||||
$team_results = $stmt->fetchAll();
|
||||
} catch (PDOException $e) {
|
||||
$team_results = [];
|
||||
echo "<p class='text-danger text-center'>Database error: " . $e->getMessage() . "</p>";
|
||||
}
|
||||
} else {
|
||||
$team_results = [];
|
||||
|
||||
@ -8,6 +8,13 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
|
||||
// Handle score update from edit_score.php
|
||||
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;
|
||||
$scores = $_POST['scores'] ?? [];
|
||||
|
||||
@ -54,7 +61,7 @@ if (isset($_POST['action']) && $_POST['action'] === 'update') {
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
|
||||
header('Location: coach.php');
|
||||
header('Location: results.php?course_id=' . $score_info['course_id']);
|
||||
exit;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
@ -83,8 +90,7 @@ foreach ($required_fields as $field) {
|
||||
try {
|
||||
$pdo = db();
|
||||
|
||||
// Get team_id from player_id
|
||||
$stmt = $pdo->prepare("SELECT team_id FROM players WHERE id = ?");
|
||||
$stmt = $pdo->prepare("SELECT name, team_id FROM players WHERE id = ?");
|
||||
$stmt->execute([$data['playerId']]);
|
||||
$player = $stmt->fetch();
|
||||
|
||||
@ -95,6 +101,19 @@ try {
|
||||
}
|
||||
|
||||
$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";
|
||||
$params = [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user