elektronisk-kadaver-tidtaki.../table.php

295 lines
8.4 KiB
PHP
Raw Permalink Normal View History

2024-02-16 11:14:45 +00:00
<?php
2024-02-17 18:23:27 +00:00
date_default_timezone_set('UTC');
2024-11-05 11:57:13 +00:00
$GLOBALS['start_time'] = DateTime::createFromFormat(DateTime::ISO8601, "2024-11-02T08:53:00+01");
2024-02-17 18:23:27 +00:00
$GLOBALS['number_of_controls'] = 3;
2024-11-05 17:02:15 +00:00
// Caching
header("Last-Modified: " . date("F d Y H:i:s.", filemtime("passering.csv")));
$etag = '"' . md5_file("passering.csv"). '"';
header(header: 'ETag: ' . $etag );
if(isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
// If HTTP_IF_NONE_MATCH is same as the generated ETag => content is the same as browser cache
// So send a 304 Not Modified response header and exit
if($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
header('HTTP/1.1 304 Not Modified', true, 304);
exit();
}
}
2024-10-08 10:55:19 +00:00
class Runner
{
2024-02-17 18:23:27 +00:00
public int $id;
public string $name;
2024-10-11 16:04:42 +00:00
public string $club;
public string $course;
2024-02-17 18:23:27 +00:00
public array $splits;
2024-10-11 16:04:42 +00:00
function __construct($id, $name, $club, $course)
2024-10-08 10:55:19 +00:00
{
//echo($id);
//echo($name);
if ($id == null) {
$id = 0;
$name = "";
}
2024-02-17 18:23:27 +00:00
$this->id = $id;
$this->name = $name;
2024-10-11 16:04:42 +00:00
$this->club = $club;
$this->course = $course;
2024-10-08 10:55:19 +00:00
for ($i = 0; $i < $GLOBALS['number_of_controls']; $i++) {
$this->splits[$i] = false;
2024-02-17 18:23:27 +00:00
}
}
2024-10-08 10:55:19 +00:00
function set_split($control, $timestamp)
{
2024-02-17 18:23:27 +00:00
$this->splits[$control] = $timestamp;
}
2024-10-08 19:40:46 +00:00
function get_control()
2024-11-05 11:57:13 +00:00
{ for ($i = 0; $i < count($this->splits); $i++) {
2024-10-08 19:40:46 +00:00
if (!is_object($this->splits[$i])) {
return $i-1;
}
2024-11-05 11:57:13 +00:00
}
return count($this->splits)-1;
2024-10-08 19:40:46 +00:00
}
2024-02-17 18:23:27 +00:00
}
//find runner by id in list of Runner objects
2024-10-08 10:55:19 +00:00
function get_runner($runnner_list, int $id)
{
2024-02-17 18:23:27 +00:00
for ($i = 0; $i < count($runnner_list); $i++) {
2024-10-08 10:55:19 +00:00
if ($runnner_list[$i]->id == $id) {
2024-02-17 18:23:27 +00:00
return $runnner_list[$i];
}
}
return false;
}
2024-10-08 19:40:46 +00:00
function cmp(Runner $a, Runner $b) {
$a_control = $a->get_control();
$b_control = $b->get_control();
if ($a_control > $b_control){
return -1;
}
if ($a_control < $b_control){
return 1;
}
if ($a_control == -1) {
return 1;
}
if ($b_control == -1) {
return -1;
}
if ($a->splits[$a_control]->getTimestamp() < $b->splits[$b_control]->getTimestamp()){
return -1;
}
if ($a->splits[$a_control]->getTimestamp() > $b->splits[$b_control]->getTimestamp()){
return 1;
}
return 0;
}
2024-10-13 21:08:38 +00:00
function cmp_course(Runner $a, Runner $b) {
return strcmp($a->course, $b->course);
}
2024-11-01 12:32:18 +00:00
function filter_runners(Runner $runner, $id) {
if ($runner->id == $id) {
return True;
}
else {
return False;
}
}
2024-11-05 17:02:15 +00:00
function time_diff(DateTime $date_1, DateTime $date_2) {
return $date_2->getTimestamp() - $date_1->getTimestamp();
}
2024-10-08 19:40:46 +00:00
2024-02-17 18:23:27 +00:00
$runners = [];
$csv_runners = file_get_contents("db.csv");
$csv_runners = str_getcsv($csv_runners, "\n");
2024-10-08 10:55:19 +00:00
//print_r($csv_runners);
for ($i = 1; $i < count($csv_runners); $i++) {
2024-02-17 18:23:27 +00:00
$line = str_getcsv($csv_runners[$i]);
2024-10-11 16:04:42 +00:00
array_push($runners, new Runner($line[0], $line[1], $line[2], $line[3]));
2024-02-17 18:23:27 +00:00
}
2024-02-16 11:14:45 +00:00
$timings = file_get_contents("passering.csv");
$timings = str_getcsv($timings, "\n");
2024-10-08 10:55:19 +00:00
for ($i = 0; $i < count($timings); $i++) {
2024-02-16 11:14:45 +00:00
$line = str_getcsv($timings[$i]);
2024-02-17 18:23:27 +00:00
$time = DateTime::createFromFormat("Y-m-d\TH:i:sp", $line[2]);
if (!$time) {
//error
2024-02-16 11:14:45 +00:00
continue;
}
2024-10-08 10:55:19 +00:00
$runner = get_runner($runners, (int) $line[1]);
2024-02-17 18:23:27 +00:00
if (!$runner) {
//error
continue;
}
2024-02-16 11:14:45 +00:00
2024-10-08 10:55:19 +00:00
$runner->set_split($line[0]-1, $time);
2024-02-17 18:23:27 +00:00
}
2024-02-16 11:14:45 +00:00
2024-10-11 15:41:20 +00:00
2024-10-11 16:04:42 +00:00
if (!isset($query)){
2024-11-01 12:32:18 +00:00
parse_str($_SERVER['QUERY_STRING'], $query);
2024-10-11 16:04:42 +00:00
}
2024-10-11 15:41:20 +00:00
2024-11-01 12:32:18 +00:00
if ($query["type"] == "registrering"){
$matpost = $query["control"];
$runners_filtered = [];
if ($query["filter"]) {
for ($i = 0; $i < count($runners); $i++) {
if (filter_runners($runners[$i], $query["filter"])) {
array_push($runners_filtered, $runners[$i]);
}
}
$runners = $runners_filtered;
}
2024-10-27 17:15:45 +00:00
echo(" <thead><tr>
2024-10-08 19:40:46 +00:00
<th>#</th>
<th>Navn</th>
2024-10-27 17:15:45 +00:00
<th>Tid</th>
<th></th>
</tr></thead>
<tbody>");
2024-10-11 15:41:20 +00:00
for ($i = 0; $i < count($runners); $i++) {
$runner = $runners[$i];
2024-10-27 17:15:45 +00:00
// Klokkeslett for denne posten
if ($runner->splits[$matpost-1] != false) {
$tid_passering = $GLOBALS['start_time']->diff($runner->splits[$matpost-1])->format('%H:%I:%S');
2024-10-11 15:41:20 +00:00
}
2024-10-27 17:15:45 +00:00
else {
$tid_passering = "";
2024-10-11 15:41:20 +00:00
}
2024-10-27 17:15:45 +00:00
2024-10-11 15:41:20 +00:00
if ($runner->get_control() == $matpost-1) {
// Løperen har vært på denne matposten og vi farger raden grønn
$button = "<button onclick=\"register_runner($runner->id)\">✓</button>";
2024-11-01 10:54:45 +00:00
$cssclass = "class=\"bg-success\"";
2024-10-11 15:41:20 +00:00
}
elseif ($runner->get_control() > $matpost-1) {
// Løperen har vært på denne matposten og vi farger raden grønn
$button = "<button onclick=\"register_runner($runner->id)\">✓</button>";
2024-11-01 10:54:45 +00:00
$cssclass = "class=\"bg-active\"";
2024-10-11 15:41:20 +00:00
}
else {
$button = "<button onclick=\"register_runner($runner->id)\">✓</button>";
$cssclass = "";
}
2024-10-27 17:15:45 +00:00
echo ("<tr $cssclass><td>$runner->id</td><td>$runner->name</td><td>$tid_passering</td><td>$button</td></tr>\n");
2024-10-08 10:55:19 +00:00
}
2024-10-27 17:15:45 +00:00
echo("</tbody>");
2024-10-11 15:41:20 +00:00
}
2024-11-01 12:32:18 +00:00
elseif ($query["type"] == "paameldte") {
2024-10-13 21:08:38 +00:00
usort($runners, "cmp_course");
$kadaverløpere = 0;
$minikadaverløpere = 0;
for ($i = 0; $i < count($runners); $i++) {
if ($runners[$i]->course == "Kadaverløpet") {
$kadaverløpere++;
}
elseif ($runners[$i]->course == "Minikadaver'n") {
$minikadaverløpere++;
}
}
echo("<div class=\"flex space-evenly\">
2024-10-27 17:15:45 +00:00
<div class=\"flash accent\">$kadaverløpere påmeldt Kadaverløpet</div><div class=\"flash accent\">$minikadaverløpere påmeldte i Minikadaver'n</div>
2024-10-13 21:08:38 +00:00
</div>");
2024-11-01 10:54:45 +00:00
echo("<table><thead>
2024-10-13 21:08:38 +00:00
<tr>
2024-11-01 10:54:45 +00:00
<th>S.nr</th>
2024-10-11 16:04:42 +00:00
<th>Navn</th>
<th>Klubb/Forening</th>
<th>Variant</th>
2024-11-01 10:54:45 +00:00
</tr></thead>
<tbody>");
2024-10-11 16:04:42 +00:00
for ($i = 0; $i < count($runners); $i++) {
$runner = $runners[$i];
2024-11-01 10:54:45 +00:00
echo ("<tr><td>$runner->id</td><td>$runner->name</td><td>$runner->club</td><td>$runner->course</td></tr>\n");
2024-10-11 16:04:42 +00:00
}
2024-10-13 21:08:38 +00:00
echo("<table><tbody>");
2024-10-11 16:04:42 +00:00
}
2024-10-11 15:41:20 +00:00
else {
2024-10-11 16:04:42 +00:00
usort($runners, "cmp");
2024-11-01 10:54:45 +00:00
usort($runners, "cmp_course");
$kadaver_table = "<table><thead>
2024-10-13 21:08:38 +00:00
<tr>
2024-10-11 15:41:20 +00:00
<th>#</th>
2024-11-05 11:57:13 +00:00
<th>Navn</th>
2024-10-11 15:41:20 +00:00
<th>1. matpost</th>
<th>2. matpost</th>
<th>Mål</th>
2024-11-05 17:02:15 +00:00
<th>Sprekkindeks</th>
2024-11-01 10:54:45 +00:00
</tr></thead>
<tbody>";
$minikadaver_table = "<table><thead>
<tr>
<th>#</th>
<th>Navn</th>
<th>Mål</th>
</tr></thead>
<tbody>";
2024-11-05 11:57:13 +00:00
$kadaver_num = 0;
$mini_num = 0;
2024-10-11 15:41:20 +00:00
for ($i = 0; $i < count($runners); $i++) {
$runner = $runners[$i];
2024-11-05 17:02:15 +00:00
$tid_maal = "";
if ($runner->splits[2] != false) {
$tid_maal = $GLOBALS['start_time']->diff($runner->splits[2])->format('%H:%I:%S');
}
if ($runner->course == "Kadaverløpet") {
2024-11-05 11:57:13 +00:00
$kadaver_num++;
2024-11-01 10:54:45 +00:00
$tid_1_mat = "";
if ($runner->splits[0] != false) {
// https://www.php.net/manual/en/class.dateinterval.php
$tid_1_mat = $GLOBALS['start_time']->diff($runner->splits[0])->format('%H:%I:%S');
}
$tid_2_mat = "";
if ($runner->splits[1] != false) {
$tid_2_mat = $GLOBALS['start_time']->diff($runner->splits[1])->format('%H:%I:%S');
2024-11-05 17:02:15 +00:00
try {
$sprekk = "<td>" . number_format(100*(time_diff($GLOBALS['start_time'],$runner->splits[2]) - time_diff($GLOBALS['start_time'],$runner->splits[1])) / time_diff($GLOBALS['start_time'],$runner->splits[2]), 0) . "%</td>";
}
catch (DivisionByZeroError $e){
$sprekk = "<td></td>";
}
catch (TypeError $e) {
$sprekk = "<td></td>";
}
2024-11-01 10:54:45 +00:00
}
$matposter = "<td>$tid_1_mat</td><td>$tid_2_mat</td>";
2024-11-05 11:57:13 +00:00
}
else {
$mini_num++;
}
2024-11-05 17:02:15 +00:00
2024-11-01 10:54:45 +00:00
if ($runner->course == "Kadaverløpet") {
2024-11-05 17:02:15 +00:00
$kadaver_table .= "<tr><td>". $kadaver_num .".</td><td>$runner->name</td>$matposter<td>$tid_maal</td>$sprekk</tr>\n";
2024-11-01 10:54:45 +00:00
}
elseif ($runner->course == "Minikadaver'n") {
2024-11-05 11:57:13 +00:00
$minikadaver_table .= "<tr><td>". "" .".</td><td>$runner->name</td><td>$tid_maal</td></tr>\n";
2024-11-01 10:54:45 +00:00
}
2024-10-08 10:55:19 +00:00
}
2024-11-01 10:54:45 +00:00
$kadaver_table .= "</tbody></table>";
$minikadaver_table .= "</tbody></table>";
echo($kadaver_table);
2024-11-05 17:02:15 +00:00
2024-11-05 11:57:13 +00:00
}