Last active
August 29, 2015 14:10
-
-
Save Korko/9b595841dfbeddd9889b to your computer and use it in GitHub Desktop.
Outils pour reformater un tableau pour l'organisation des papiers kdo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Horodateur,Prénom,Nom,Ãge,Groupe,Unité,Numéro de téléphone,Adresse mail,<dates> | |
// https://docs.google.com/spreadsheets/d/<id>/export?format=csv&id=<id>&gid=<gid> -> "File" -> "Save as" -> ".csv" (copy url) | |
$csv = file_get_contents("https://docs.google.com/spreadsheets/d/<id>/export?format=csv&id=<id>&gid=<gid>"); | |
$lines = array_map('str_getcsv', explode("\n", $csv)); | |
// Supprimer la première ligne (en têtes) | |
$headers = array_shift($lines); | |
$stats = array(); | |
$slots = array(); | |
foreach($lines as $line) { | |
// Déterminer si la personne est un chef (ou un compa adulte) ou un jeune | |
$age = (strcasecmp('majeur', $line[3]) === 0 || stripos($line[5], 'chef') !== FALSE) ? 'majeur' : 'mineur'; | |
for($i=8;$i<count($line);$i++) { | |
// Calculer la date correspondante à la colonne | |
preg_match('#\d+#', $headers[$i], $match); | |
$day = str_pad($match[0], 2, '0', STR_PAD_LEFT).'/12'; | |
// Récupérer le créneaux correspondant | |
preg_match_all('#(\d{1,2})h00\s*-\s*(\d{1,2})h00#', $line[$i], $matches, PREG_SET_ORDER); | |
foreach($matches as $slotMatch) { | |
$stats[$line[4]] = isset($stats[$line[4]]) ? $stats[$line[4]]+1 : 1; | |
$slot = str_pad($slotMatch[1], 2, '0', STR_PAD_LEFT)."-".str_pad($slotMatch[2], 2, '0', STR_PAD_LEFT); | |
$scout = mb_convert_case($line[1], MB_CASE_TITLE, "UTF-8")." ".mb_strtoupper($line[2], "UTF-8"); | |
$data = implode('-', str_split(str_pad(str_replace(['O', '-', '+33'], [0, '', 0], $line[6]), 10, '0', STR_PAD_LEFT), 2))." / ".strtolower($line[7]); | |
$slots[$age][$day][$slot][] = $scout." (".mb_convert_case($line[4], MB_CASE_TITLE, "UTF-8").") -- ".$data; | |
} | |
} | |
} | |
// Trier le tableau par jour/créneau horaire | |
foreach($slots as &$date) { | |
ksort($date); | |
foreach($date as &$slot) { | |
ksort($slot); | |
} | |
unset($slot); | |
} | |
unset($date); | |
// Exporter sous forme de tableaux | |
echo "<html> | |
<header> | |
<meta charset=\"UTF-8\"> | |
<style type=\"text/css\"> | |
table,td,th{border:1px solid #000} | |
table{background-color:#aaa} | |
tbody{background-color:#ddd;height:100px;overflow:auto} | |
td{padding:3px 10px;white-space:nowrap} | |
th:empty{border:0} | |
thead th:empty,tbody th{white-space:nowrap;padding:0 5px} | |
</style> | |
</header> | |
<body>"; | |
export($headers, $slots, 'mineur'); | |
export($headers, $slots, 'majeur'); | |
echo "<h1>Stats inscription</h1>"; | |
foreach($stats as $group => $stat) { | |
echo "<strong>".$group."</strong> : ".number_format($stat/array_sum($stats)*100, 2)."% (".$stat."), "; | |
} | |
echo "</body></html>"; | |
function export($headers, $slots, $age){ | |
if($age === 'mineur') { | |
echo "<h1>Mineurs</h1>"; | |
} else { | |
echo "<h1>Majeurs</h1>"; | |
} | |
echo "<table>"; | |
// Dates | |
echo "<thead><tr><th></th>"; // first case top left is empty | |
for($i=8;$i<count($headers);$i++) { | |
echo "<th scope=\"col\">".$headers[$i]."</th>"; | |
} | |
echo "</tr></thead>"; | |
// Créneaux | |
// Ouverture : 9h, Fermeture : 21h => 12 créneaux | |
echo "<tbody>"; | |
for($i=9;$i<21;$i++) { | |
echo "<tr>"; | |
$slot = str_pad($i, 2, '0', STR_PAD_LEFT)."-".str_pad($i+1, 2, '0', STR_PAD_LEFT); | |
echo "<th scope=\"row\">".$slot."</th>"; | |
for($j=8;$j<count($headers);$j++) { | |
// Calculer la date correspondante à la colonne | |
preg_match('#\d+#', $headers[$j], $match); | |
$day = str_pad($match[0], 2, '0', STR_PAD_LEFT).'/12'; | |
echo "<td><ol>"; | |
$slots[$age][$day][$slot] = isset($slots[$age][$day][$slot]) ? $slots[$age][$day][$slot] : array(); | |
foreach($slots[$age][$day][$slot] as $scout) { | |
echo "<li>".$scout."</li>"; | |
} | |
echo "</ol></td>"; | |
} | |
echo "</tr>"; | |
} | |
echo "</tbody>"; | |
echo "</table>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment