/!\ la version php n'est pas terminée
This commit is contained in:
@@ -43,7 +43,7 @@
|
|||||||
{
|
{
|
||||||
"A":1,
|
"A":1,
|
||||||
"C":2,
|
"C":2,
|
||||||
"D":3,
|
"D":3
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"A":1,
|
"A":1,
|
||||||
|
|||||||
157
samples/parse_sample.php
Normal file
157
samples/parse_sample.php
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
#!/bin/env php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
if (count($argv) > 1) {
|
||||||
|
$poll_file=$argv[1];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print('missing file argument');
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$verbose=False;
|
||||||
|
|
||||||
|
# $poll_fd=fopen($poll_file,'r');
|
||||||
|
$poll_json=file_get_contents($poll_file);
|
||||||
|
$poll=json_decode($poll_json,True);
|
||||||
|
|
||||||
|
if ($verbose) {
|
||||||
|
print($poll_json);
|
||||||
|
print('=== json ===');
|
||||||
|
var_dump($poll);
|
||||||
|
}
|
||||||
|
|
||||||
|
$votants=$poll['votants']['decompte'];
|
||||||
|
$candidats=$poll['candidats'];
|
||||||
|
$nombre_candidats=count($candidats);
|
||||||
|
|
||||||
|
# l'ordre des mentions est de la meilleure à la pire (reject)
|
||||||
|
$nom_mentions=$poll['mentions'];
|
||||||
|
$mentions=count($nom_mentions);
|
||||||
|
|
||||||
|
$default_mention=$mentions-1;
|
||||||
|
|
||||||
|
$votes=$poll["votation"]["votes"];
|
||||||
|
if ($verbose) {
|
||||||
|
print('candidats:' . implode(",",$candidats));
|
||||||
|
print(implode(',',$votes));
|
||||||
|
}
|
||||||
|
|
||||||
|
$warnings=[];
|
||||||
|
$default_vote=[];
|
||||||
|
foreach ($candidats as $candidat ) {
|
||||||
|
$default_vote[$candidat]=$default_mention;
|
||||||
|
}
|
||||||
|
|
||||||
|
$collect=[];
|
||||||
|
foreach ($votes as $vote) {
|
||||||
|
# missing candidat in vote is the worts one
|
||||||
|
foreach ($candidats as $candidat) {
|
||||||
|
if (! array_key_exists($candidat,$vote)) {
|
||||||
|
$vote[$candidat]=$default_mention;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ( $vote as $candidat => $mention ) {
|
||||||
|
if ($mention > $default_mention or $mention < 0) {
|
||||||
|
$mention = $default_mention;
|
||||||
|
$warnings.append("index de mention invalide, soit la liste des mentions est erronnée soit la mention dans le vote est erronée");
|
||||||
|
}
|
||||||
|
if ( array_key_exists($candidat,$collect)) {
|
||||||
|
if ( array_key_exists($mention,$collect[$candidat])) {
|
||||||
|
$collect[$candidat][$mention]=$collect[$candidat][$mention]+1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$collect[$candidat][$mention]=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$collect[$candidat]=[$mention => 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($verbose) {
|
||||||
|
print($collect);
|
||||||
|
}
|
||||||
|
|
||||||
|
$votant_median_check= ($votants % 2 == 0) ? $votants / 2 : (($votants-1) / 2);
|
||||||
|
|
||||||
|
$votant_median=$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));
|
||||||
|
}
|
||||||
|
|
||||||
|
$merite=[];
|
||||||
|
$mention_mediane=[];
|
||||||
|
$merite_pourcent=[];
|
||||||
|
|
||||||
|
# cumul : du meilleur au pire
|
||||||
|
# range_mentions = range(count(mentions))
|
||||||
|
# cumul du pire au meilleur
|
||||||
|
$range_mentions = $mentions;
|
||||||
|
|
||||||
|
foreach ($candidats as $candidat) {
|
||||||
|
$vote=$collect[$candidat];
|
||||||
|
$cumul=0;
|
||||||
|
for ($i=0; $i < $range_mentions; $i ++) {
|
||||||
|
$m[$i]=0;
|
||||||
|
$pourcent[$i]=0;
|
||||||
|
}
|
||||||
|
$mention_m=null;
|
||||||
|
for ($i=$range_mentions -1; $i > 0; $i --) {
|
||||||
|
if ( array_key_exists($mention,$vote) ) {
|
||||||
|
$cumul=$cumul+$vote[$mention];
|
||||||
|
$pourcent[$mention]= ( 100 * $vote[$mention] ) / $votants;
|
||||||
|
}
|
||||||
|
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.');
|
||||||
|
}
|
||||||
|
$mention_mediane[$candidat]=$mention_m;
|
||||||
|
$merite[$candidat]=$m;
|
||||||
|
$merite_pourcent[$candidat]=$pourcent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($verbose) {
|
||||||
|
print($merite) ;
|
||||||
|
print($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 ++) {
|
||||||
|
foreach ($mention_mediane as $candidat => $merite_median)
|
||||||
|
if ($merite_median == $mention) {
|
||||||
|
$item=[];
|
||||||
|
$item[$candidat]=$candidats[$candidat];
|
||||||
|
array_push($found,$item);
|
||||||
|
}
|
||||||
|
if (count($found) > 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($verbose) {
|
||||||
|
print($found);
|
||||||
|
print($nom_mentions[$mention]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = ['resultat'=> ['mention'=>$nom_mentions[$mention],'candidats' => $found],'merite' => $merite,'profil' => $merite_pourcent,"warnings" => $warnings];
|
||||||
|
|
||||||
|
print(json_encode($result));
|
||||||
|
|
||||||
|
?>
|
||||||
@@ -20,14 +20,17 @@ candidats=poll['candidats']
|
|||||||
nombre_candidats=len(candidats)
|
nombre_candidats=len(candidats)
|
||||||
|
|
||||||
# l'ordre des mentions est de la meilleure à la pire (reject)
|
# l'ordre des mentions est de la meilleure à la pire (reject)
|
||||||
mentions=poll['mentions']
|
nom_mentions=poll['mentions']
|
||||||
default_mention=len(mentions)-1
|
mentions=len(nom_mentions)
|
||||||
|
|
||||||
|
default_mention=mentions-1
|
||||||
|
|
||||||
votes=poll["votation"]["votes"]
|
votes=poll["votation"]["votes"]
|
||||||
if verbose:
|
if verbose:
|
||||||
print('candidats:' + str(candidats))
|
print('candidats:' + str(candidats))
|
||||||
print(votes)
|
print(votes)
|
||||||
|
|
||||||
|
warnings=[]
|
||||||
default_vote={}
|
default_vote={}
|
||||||
for candidat in candidats:
|
for candidat in candidats:
|
||||||
default_vote[candidat]=default_mention
|
default_vote[candidat]=default_mention
|
||||||
@@ -41,6 +44,9 @@ for vote in votes:
|
|||||||
vote[candidat]=default_mention
|
vote[candidat]=default_mention
|
||||||
|
|
||||||
for candidat,mention in vote.items():
|
for candidat,mention in vote.items():
|
||||||
|
if mention > default_mention or mention < 0:
|
||||||
|
mention = default_mention
|
||||||
|
warnings.append("index de mention invalide, soit la liste des mentions est erronnée soit la mention dans le vote est erronée")
|
||||||
if candidat in collect:
|
if candidat in collect:
|
||||||
if mention in collect[candidat]:
|
if mention in collect[candidat]:
|
||||||
collect[candidat][mention]=collect[candidat][mention]+1
|
collect[candidat][mention]=collect[candidat][mention]+1
|
||||||
@@ -61,20 +67,23 @@ if votant_median_check != votant_median:
|
|||||||
|
|
||||||
merite={}
|
merite={}
|
||||||
mention_mediane={}
|
mention_mediane={}
|
||||||
|
merite_pourcent={}
|
||||||
|
|
||||||
# cumul : du meilleur au pire
|
# cumul : du meilleur au pire
|
||||||
# range_mentions = range(len(mentions))
|
# range_mentions = range(len(mentions))
|
||||||
# cumul du pire au meilleur
|
# cumul du pire au meilleur
|
||||||
range_mentions = range(len(mentions)-1,-1,-1)
|
range_mentions = range(mentions-1,-1,-1)
|
||||||
|
|
||||||
for candidat in candidats:
|
for candidat in candidats:
|
||||||
vote=collect[candidat]
|
vote=collect[candidat]
|
||||||
cumul=0
|
cumul=0
|
||||||
m=[0 for i in range(len(mentions))]
|
m=[0 for i in range(mentions)]
|
||||||
mention_m=None
|
mention_m=None
|
||||||
|
pourcent=[0 for i in range(mentions)]
|
||||||
for mention in range_mentions:
|
for mention in range_mentions:
|
||||||
if mention in vote:
|
if mention in vote:
|
||||||
cumul=cumul+vote[mention]
|
cumul=cumul+vote[mention]
|
||||||
|
pourcent[mention]= ( 100 * vote[mention] ) / votants
|
||||||
if cumul >= votant_median and mention_m is None:
|
if cumul >= votant_median and mention_m is None:
|
||||||
mention_m=mention
|
mention_m=mention
|
||||||
m[mention]=cumul
|
m[mention]=cumul
|
||||||
@@ -82,14 +91,15 @@ for candidat in candidats:
|
|||||||
print('[ERROR] le cumul des votes doit correspondre au nombre de votants. contactez le developpeur de ce code.')
|
print('[ERROR] le cumul des votes doit correspondre au nombre de votants. contactez le developpeur de ce code.')
|
||||||
mention_mediane[candidat]=mention_m
|
mention_mediane[candidat]=mention_m
|
||||||
merite[candidat]=m
|
merite[candidat]=m
|
||||||
|
merite_pourcent[candidat]=pourcent
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print(merite)
|
print(merite)
|
||||||
print(mention_mediane)
|
print(mention_mediane)
|
||||||
|
|
||||||
# les gagnants sont ceux qui ont la mention minimale ( la meilleure )
|
# les gagnants sont ceux qui ont la mention mediane minimale ( la meilleure )
|
||||||
found=[]
|
found=[]
|
||||||
for mention in range(0,len(mentions)):
|
for mention in range(0,mentions):
|
||||||
for candidat,merite_median in mention_mediane.items():
|
for candidat,merite_median in mention_mediane.items():
|
||||||
if merite_median == mention:
|
if merite_median == mention:
|
||||||
found.append({candidat:candidats[candidat]})
|
found.append({candidat:candidats[candidat]})
|
||||||
@@ -98,8 +108,8 @@ for mention in range(0,len(mentions)):
|
|||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print(found)
|
print(found)
|
||||||
print(mentions[mention])
|
print(nom_mentions[mention])
|
||||||
|
|
||||||
result = {'resultat':{'mention':mentions[mention],'candidats':found},'merite':merite}
|
result = {'resultat':{'mention':nom_mentions[mention],'candidats':found},'merite':merite,'profil':merite_pourcent,"warnings":warnings}
|
||||||
|
|
||||||
print(json.dumps(result))
|
print(json.dumps(result))
|
||||||
|
|||||||
Reference in New Issue
Block a user