diff --git a/samples/3.json b/samples/3.json new file mode 100644 index 0000000..a767354 --- /dev/null +++ b/samples/3.json @@ -0,0 +1,86 @@ +{ +"candidats":{ + "A":"albert", + "B":"Beatrice", + "C":"Chloé", + "D":"Deborah", + "E":"Eric", + "F":"Francçois", + "G":"Gisèle", + "H":"Hugo" +}, +"votants":{ + "decompte":11 +}, +"mentions":[ + "Très Bien", + "Bien", + "Assez Bien", + "Passable", + "Insuffisant", + "A rejeter" +], +"votation": + { + "date":"14 juin 2025", + "lieu":"liffré", + "votes":[ + { + "A":1, + "C":2, + "D":3 + }, + { + "B":1, + "C":2, + "E":3 + }, + { + "A":1, + "C":2, + "F":3 + }, + { + "G":1, + "C":2, + "D":3 + }, + { + "A":1, + "G":2, + "E":3 + }, + { + "E":1, + "C":2, + "D":3 + }, + { + "E":1, + "C":2, + "D":3 + }, + { + "A":1, + "D":2 + }, + { + "A":5, + "C":2, + "D":3 + }, + { + "A":4 + }, + { + "A":1, + "C":2, + "D":3, + "B":4, + "E":3, + "F":4, + "G":0 + } + ] + } +} diff --git a/samples/parse_sample.php b/samples/parse_sample.php index 462428a..2c5702d 100644 --- a/samples/parse_sample.php +++ b/samples/parse_sample.php @@ -33,20 +33,22 @@ $default_mention=$mentions-1; $votes=$poll["votation"]["votes"]; if ($verbose) { - print('candidats:' . implode(",",$candidats)); - print(implode(',',$votes)); + print("candidats\n"); + var_dump($candidats); + print("votes:\n"); + var_dump($votes); } $warnings=[]; $default_vote=[]; -foreach ($candidats as $candidat ) { +foreach ($candidats as $candidat => $nom_candidat ) { $default_vote[$candidat]=$default_mention; } $collect=[]; foreach ($votes as $vote) { # missing candidat in vote is the worts one - foreach ($candidats as $candidat) { + foreach ($candidats as $candidat => $nom_candidat ) { if (! array_key_exists($candidat,$vote)) { $vote[$candidat]=$default_mention; } @@ -72,15 +74,16 @@ foreach ($votes as $vote) { } if ($verbose) { - print($collect); + print("collect:\n"); + print(json_encode($collect)); } -$votant_median_check= ($votants % 2 == 0) ? $votants / 2 : (($votants-1) / 2); +$votant_median_check= ($votants % 2 == 0) ? intdiv($votants,2) : intdiv(($votants-1),2) + 1; -$votant_median=$votants / 2 + $votants % 2; +$votant_median=intdiv($votants, 2) + $votants % 2; if ( $votant_median_check != $votant_median ) { - print('[ERROR] le nombre median de votants semble erroné. contactez le developpeur de ce code.' + str($votant_median_check)); + print('[ERROR] le nombre median de votants (' . $votant_median_check . '/' . $votant_median . ') semble erroné. contactez le developpeur de ce code.'); } $merite=[]; @@ -92,7 +95,7 @@ $merite_pourcent=[]; # cumul du pire au meilleur $range_mentions = $mentions; -foreach ($candidats as $candidat) { +foreach ($candidats as $candidat => $nom_candidat) { $vote=$collect[$candidat]; $cumul=0; for ($i=0; $i < $range_mentions; $i ++) { @@ -100,7 +103,7 @@ foreach ($candidats as $candidat) { $pourcent[$i]=0; } $mention_m=null; - for ($i=$range_mentions -1; $i > 0; $i --) { + for ($mention=$range_mentions -1; $mention >=0; $mention --) { if ( array_key_exists($mention,$vote) ) { $cumul=$cumul+$vote[$mention]; $pourcent[$mention]= ( 100 * $vote[$mention] ) / $votants; @@ -108,11 +111,10 @@ foreach ($candidats as $candidat) { if ($cumul >= $votant_median and $mention_m == null ) { $mention_m=$mention; } - $m[$mention]=$cumul; } if ($cumul < $votants) { - print('[ERROR] le cumul des votes doit correspondre au nombre de votants. contactez le developpeur de ce code.'); + print("[ERROR] le cumul des votes " . $cumul . " doit correspondre au nombre de votants. contactez le developpeur de ce code."); } $mention_mediane[$candidat]=$mention_m; $merite[$candidat]=$m; @@ -120,20 +122,16 @@ foreach ($candidats as $candidat) { } if ($verbose) { - print($merite) ; - print($mention_mediane); + print("\nmerite:\n"); + print(json_encode($merite)); + print("\nmention_mediane:\n"); + print(json_encode($mention_mediane)); } # les gagnants sont ceux qui ont la mention mediane minimale ( la meilleure ) $found=[]; - print(json_encode($mention_mediane)); - -# la clé de candidats est un shortcut - print(json_encode($candidats)); - - -for ($i = 0; $i < $mentions; $i ++) { +for ($mention = 0; $mention < $mentions; $mention ++) { foreach ($mention_mediane as $candidat => $merite_median) if ($merite_median == $mention) { $item=[];