Files
shiftcounter/shiftcounter.php

119 lines
5.0 KiB
PHP

<?php
$monthTrans = ['jan' => 'Jan', 'feb' => 'Feb', 'maa' => 'Mar', 'apr' => 'Apr', 'mei' => 'May', 'jul' => 'Jul', 'aug' => 'Aug', 'sep' => 'Sep',
'okt' => 'Oct', 'nov' => 'Nov', 'dec' => 'Dec'];
$sites = ['https://www.wina.be/worklists/341', 'https://www.wina.be/worklists/336', 'https://www.wina.be/worklists/337'];
$all_names = [];
$responsibles = ['William Vandermaesen', 'Samuel Nassen', 'Robin Broekmans'];
$resp_icon = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-user-plus"><path d="M16 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path><circle cx="8.5" cy="7" r="4"></circle><line x1="20" y1="8" x2="20" y2="14"></line><line x1="23" y1="11" x2="17" y2="11"></line></svg>';
$totalShifted = 0;
$total = 0;
foreach ($sites as $site) {
$data = file_get_contents($site);
preg_match_all('/<div id="(data_shift.*?<table.*?>.*?<\/table>)/s', $data, $shiften);
$shiften = $shiften[1];
foreach ($shiften as $shift) {
preg_match_all('/data_shift.*?Start<\/td>.*?>.*?(\d.*?)<\/td>/s', $shift, $start);
preg_match_all('/data_shift.*?Einde<\/td>.*?>.*?(\d.*?)<\/td>/s', $shift, $einde);
$start = explode(' ', $start[1][0]);
$start[1] = $monthTrans[$start[1]];
$start = DateTime::createFromFormat('j M Y - G:i', implode(' ', $start));
$einde = explode(' ', $einde[1][0]);
$einde[1] = $monthTrans[$einde[1]];
$einde = DateTime::createFromFormat('j M Y - G:i', implode(' ', $einde));
$diff = ($einde->getTimestamp() - $start->getTimestamp()) / 3600;
preg_match_all('/data_shift.*?Invulling.*?<td.*?>(.*?)<\/td>/s', $shift, $blockNames);
if (count($blockNames[1]) !== 0) {
preg_match_all('/<span.*?>(.*?)<\/span>/s', $blockNames[1][0], $names);
foreach ($names[1] as $name) {
if (!array_key_exists($name, $all_names)) {
$all_names[$name] = $diff;
} else {
$all_names[$name] += $diff;
}
$totalShifted += $diff;
}
}
}
}
$avg = $totalShifted / count($all_names);
?>
<!doctype html>
<html lang="nl">
<head>
<title>Wina 24u shift counter</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs4/dt-1.10.18/datatables.min.css"/>
</head>
<body>
<main role="main" class="container mt-2">
<div class="text-center">
<h1>Wina 24 urenloop shift counter</h1>
<p><?php echo floor($totalShifted) . ' uur en ' . ($totalShifted - floor($totalShifted)) * 60 . ' minuten'; ?> shifted!<br>
Gemiddeld: <?php echo floor($avg) . ' uur en ' . round(($avg - floor($avg)) * 60) . ' minuten'; ?></p>
</div>
<p><?php echo $resp_icon; ?> = verantwoordelijke</p>
<div class="table-responsive">
<table id="table" class="table">
<thead>
<tr>
<th scope="col">Naam</th>
<th scope="col">Uren</th>
</tr>
</thead>
<tbody>
<?php
foreach ($all_names as $name => $val) {
echo "<tr><td data-order='$name'>$name" . (in_array($name, $responsibles) ? " $resp_icon" : '') . "</td><td data-order='$val'>" . floor($val) . 'h ' . ($val - floor($val)) * 60 . 'min' . "</td></tr>";
}
foreach($responsibles as $resp) {
if (!key_exists($resp, $all_names)) {
echo "<tr><td data-order='$resp'>$resp $resp_icon</td><td data-order='0'>0h 0min</td></tr>";
}
}
?>
</tbody>
</table>
</div>
</main><!-- /.container -->
<footer class="mt-5">
<div class="container">
<span class="text-muted">© 2018 Arthur Bols <a href="https://git.principis.be/Principis/shiftcounter">source</span>
</div>
</footer>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/dt-1.10.18/datatables.min.js"></script>
<script type="text/javascript">
$(document).ready( function () {
$('#table').DataTable();
});
</script>
</body>
</html>