216 lines
6.7 KiB
PHP
216 lines
6.7 KiB
PHP
<?php
|
|
|
|
function handle_pdo_exception($e) {
|
|
print "Error!: " . $e->getMessage() . "<br/>";
|
|
die();
|
|
}
|
|
|
|
function check_user_id($con, $user_id) {
|
|
try {
|
|
$stmt = $con->prepare('SELECT user_id FROM users WHERE user_id = :user_id limit 1');
|
|
$stmt->execute(['user_id' => $user_id]);
|
|
|
|
if($stmt) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function get_user_data_name($con, $user_name) {
|
|
try {
|
|
$stmt = $con->prepare('SELECT * FROM users WHERE user_name = :user_name limit 1');
|
|
$stmt->execute(['user_name' => $user_name]);
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
return $stmt->fetch();
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function get_user_data_id($con, $user_id) {
|
|
try {
|
|
$stmt = $con->prepare('SELECT * FROM users WHERE user_id = :user_id limit 1');
|
|
$stmt->execute(['user_id' => $user_id]);
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
return $stmt->fetch();
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function add_user($con, $username, $user_id, $user_group, $password, $salt) {
|
|
try {
|
|
$stmt = $con->prepare("INSERT INTO users (user_id, password, user_name, salt, user_group) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->bind_param("sssss", $user_id, $password, $username, $salt, $user_group);
|
|
$stmt->execute();
|
|
} catch(PDOExeption $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function get_stations($con) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT name, standort FROM Station");
|
|
$stmt->execute();
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
return $stmt;
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exceptio($e);
|
|
}
|
|
}
|
|
|
|
function get_stations_all($con) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT * FROM Station");
|
|
$stmt->execute();
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
return $stmt;
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exceptio($e);
|
|
}
|
|
}
|
|
|
|
function get_teams($con) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT name, feuerwehr FROM Mannschaft");
|
|
$stmt->execute();
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
return $stmt;
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function get_total_score($con) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT M.name as Mannschaftsname, M.feuerwehr as Feuerwehr, SUM(P.punkte) as Gesamtpunkte FROM Punkte as P, Mannschaft as M WHERE P.m_id = M.m_id GROUP BY M.m_id ORDER BY Gesamtpunkte DESC");
|
|
$stmt->execute();
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
return $stmt;
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function get_station_name($con, $s_id) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT name FROM Station WHERE s_id= :s_id");
|
|
$stmt->execute(['s_id' => $s_id]);
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
return $stmt->fetch();
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function get_station($con, $s_id) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT M.name as Name, M.feuerwehr as Feuerwehr, P.punkte as Punkte, P.zeit as Zeit FROM Punkte as P, Station as S, Mannschaft as M WHERE P.s_id = S.s_id AND S.s_id = :s_id AND M.m_id = P.m_id ORDER BY Punkte DESC");
|
|
$stmt->execute(['s_id' => $s_id]);
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
return $stmt;
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function get_teams_no_points($con, $s_id) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT M.* FROM Mannschaft M WHERE M.m_id NOT IN (SELECT P.m_id FROM Punkte P WHERE P.s_id = :s_id)");
|
|
$stmt->execute(['s_id' => $s_id]);
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
return $stmt;
|
|
} catch(PDOException $e) {
|
|
handle_pdo_excepiton($e);
|
|
}
|
|
}
|
|
|
|
function station_exists($con, $s_id) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT s_id FROM Station WHERE s_id = :s_id");
|
|
$stmt->execute(['s_id' => $s_id]);
|
|
if($stmt->rowCount() > 0) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function write_points($con, $s_id, $m_id, $points, $time) {
|
|
try {
|
|
$stmt = $con->prepare("INSERT INTO Punkte (m_id, s_id, punkte, zeit) VALUES (?, ?, ?, ?)");
|
|
$stmt->bindParam(1, $m_id, PDO::PARAM_INT);
|
|
$stmt->bindParam(2, $s_id, PDO::PARAM_INT);
|
|
$stmt->bindParam(3, $points, PDO::PARAM_INT);
|
|
$stmt->bindParam(4, $time, PDO::PARAM_STR);
|
|
$stmt->execute();
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function write_station($con, $station_name, $station_pos) {
|
|
try {
|
|
$stmt = $con->prepare("INSERT INTO Station (name, standort) VALUES (?, ?)");
|
|
$stmt->bindParam(1, $station_name, PDO::PARAM_STR);
|
|
$stmt->bindParam(2, $station_pos, PDO::PARAM_STR);
|
|
$stmt->execute();
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exceptio($e);
|
|
}
|
|
}
|
|
|
|
function get_fire_departments($con) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT DISTINCT feuerwehr FROM Mannschaft");
|
|
$stmt->execute();
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
return $stmt;
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function write_team($con, $team_name, $fire_department) {
|
|
try {
|
|
$stmt = $con->prepare("INSERT INTO Mannschaft (name, feuerwehr) VALUES (?, ?)");
|
|
$stmt->bindParam(1, $team_name, PDO::PARAM_STR);
|
|
$stmt->bindParam(2, $fire_department, PDO::PARAM_STR);
|
|
$stmt->execute();
|
|
} catch(PDOEXCEPTION $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function get_users($con) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT user_name, user_group, s_id FROM users");
|
|
$stmt->execute();
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
return $stmt;
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
}
|
|
|
|
function write_user($con, $user_name, $user_id, $phash, $salt, $user_group, $s_id) {
|
|
try {
|
|
$stmt = $con->prepare("INSERT INTO users (user_id, password, user_name, salt, user_group, s_id) VALUES (?, ?, ?, ?, ?, ?)");
|
|
$stmt->bindParam(1, $user_id, PDO::PARAM_STR);
|
|
$stmt->bindParam(2, $phash, PDO::PARAM_STR);
|
|
$stmt->bindParam(3, $user_name, PDO::PARAM_STR);
|
|
$stmt->bindParam(4, $salt, PDO::PARAM_STR);
|
|
$stmt->bindParam(5, $user_group, PDO::PARAM_STR);
|
|
$stmt->bindParam(6, $s_id, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
} catch(PDOException $e) {
|
|
handle_pdo_exception($e);
|
|
}
|
|
} |