From 0f8fd03d51847a8ddee7599d78b6c59aa0c988a9 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Wed, 24 Sep 2025 21:22:36 +0000 Subject: [PATCH] 1.3 --- add_player.php | 10 ++-- add_score.php | 6 +- admin.php | 55 ++++++++++++++++++- assets/js/main.js | 9 +++ coach.php | 4 +- .../20250925_add_team_id_to_players.php | 12 ++++ .../20250926_drop_team_members_table.php | 12 ++++ delete_player.php | 7 +-- delete_score.php | 33 +++++++++++ edit_player.php | 29 ++++++++-- edit_score.php | 7 +++ results.php | 35 +++++++----- submit_score.php | 25 ++++++++- 13 files changed, 209 insertions(+), 35 deletions(-) create mode 100644 db/migrations/20250925_add_team_id_to_players.php create mode 100644 db/migrations/20250926_drop_team_members_table.php create mode 100644 delete_score.php diff --git a/add_player.php b/add_player.php index f56cf27..d2f0752 100644 --- a/add_player.php +++ b/add_player.php @@ -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(); diff --git a/add_score.php b/add_score.php index 778d2a9..eb88f6f 100644 --- a/add_score.php +++ b/add_score.php @@ -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 ''; endif; ?> - + '; @@ -141,7 +141,7 @@ try {
- Your score has been submitted successfully! (Client-side demo) + Score submitted successfully.
diff --git a/admin.php b/admin.php index 0c2771e..eee1f3e 100644 --- a/admin.php +++ b/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(); +} + ?> @@ -202,7 +217,45 @@ try {
- ... +
+
+
+

Manage Players

+ Add New Player +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameEmailHigh School YearSeason YearTeamActions
No players found.
+ Edit + Delete +
+
+
diff --git a/assets/js/main.js b/assets/js/main.js index ffb45e2..019b2ed 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -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(); } diff --git a/coach.php b/coach.php index 3cb7d37..b3f03d6 100644 --- a/coach.php +++ b/coach.php @@ -80,7 +80,7 @@ unset($_SESSION['error_message']);

Players

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']); Edit - Delete + Delete diff --git a/db/migrations/20250925_add_team_id_to_players.php b/db/migrations/20250925_add_team_id_to_players.php new file mode 100644 index 0000000..7d296e7 --- /dev/null +++ b/db/migrations/20250925_add_team_id_to_players.php @@ -0,0 +1,12 @@ +exec($sql); + echo "Migration successful: added team_id to players table.\n"; +} catch (PDOException $e) { + die("Migration failed: " . $e->getMessage() . "\n"); +} + diff --git a/db/migrations/20250926_drop_team_members_table.php b/db/migrations/20250926_drop_team_members_table.php new file mode 100644 index 0000000..64b0c56 --- /dev/null +++ b/db/migrations/20250926_drop_team_members_table.php @@ -0,0 +1,12 @@ +exec($sql); + echo "Migration successful: dropped team_members table.\n"; +} catch (PDOException $e) { + die("Migration failed: " . $e->getMessage() . "\n"); +} + diff --git a/delete_player.php b/delete_player.php index 4c748e1..ca91986 100644 --- a/delete_player.php +++ b/delete_player.php @@ -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(); diff --git a/delete_score.php b/delete_score.php new file mode 100644 index 0000000..d34f6bf --- /dev/null +++ b/delete_score.php @@ -0,0 +1,33 @@ +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; +} +?> \ No newline at end of file diff --git a/edit_player.php b/edit_player.php index a54351f..0e31b0f 100644 --- a/edit_player.php +++ b/edit_player.php @@ -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') { +
+ + +
diff --git a/edit_score.php b/edit_score.php index f7ae28c..3f92ac1 100644 --- a/edit_score.php +++ b/edit_score.php @@ -1,6 +1,13 @@ 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 @@
-

Individual Standings

+

Individual Standings

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 "

Database error: " . $e->getMessage() . "

"; } } else { $results = []; @@ -113,18 +115,19 @@ Total To Par + Actions - No results yet for this course. + No results yet for this course. $row): ?> - + @@ -144,6 +147,10 @@ 0 ? '+' : '') . $row['total_to_par']; ?> + + Edit + Delete + @@ -155,21 +162,23 @@
-

Team Standings

+

Team Standings

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 "

Database error: " . $e->getMessage() . "

"; } } else { $team_results = []; diff --git a/submit_score.php b/submit_score.php index ebfd4ad..93355f9 100644 --- a/submit_score.php +++ b/submit_score.php @@ -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 = [