erreur thunmbail4
[aidenligne_francais_universite.git] / plugins / forms_1_9_1 / forms_fonctions.php
1 <?php
2
3 /*
4 * forms
5 * version plug-in de spip_form
6 *
7 * Auteur :
8 * Antoine Pitrou
9 * adaptation en 182e puis plugin par cedric.morin@yterium.com
10 * © 2005,2006 - Distribue sous licence GNU/GPL
11 *
12 */
13
14 include_spip('base/forms_temporaire');
15
16 //
17 // Afficher le diagramme de resultats d'un sondage
18 //
19
20 function Forms_afficher_reponses_sondage($id_form) {
21 $r = '';
22 $id_form = intval($id_form);
23
24 $query = "SELECT * FROM spip_forms WHERE id_form=$id_form";
25 $result = spip_query($query);
26 if (!$row = spip_fetch_array($result)) return '';
27
28 $sondage = $row['sondage'];
29 $structure = unserialize($row['structure']);
30 $champs = array();
31 foreach ($structure as $index => $t) {
32 if ($t['type'] != 'select' && $t['type'] != 'multiple' && $t['type'] != 'mot')
33 continue;
34 $champs[] = $t;
35 }
36
37 $r .= "<div class='spip_sondage'>\n";
38
39 // Compter les reponses pour chaque champ de type choix (unique / multiple / mot-cle)
40 foreach ($champs as $t) {
41 // On recompte le nombre total de reponses reelles
42 // car les champs ne sont pas forcement obligatoires
43 $query = "SELECT COUNT(DISTINCT c.id_reponse) AS num ".
44 "FROM spip_reponses AS r LEFT JOIN spip_reponses_champs AS c USING (id_reponse) ".
45 "WHERE r.id_form=$id_form AND r.statut='valide' AND c.champ='".addslashes($t['code'])."'";
46 $result = spip_query($query);
47 list ($total_reponses) = spip_fetch_array($result,SPIP_NUM);
48 if (!$total_reponses) continue;
49
50 // Construire la liste des valeurs autorisees pour le champ
51 if ($t['type'] != 'mot')
52 $liste = $t['type_ext'];
53 else {
54 $id_groupe = intval($t['type_ext']['id_groupe']);
55 $query = "SELECT id_mot, titre FROM spip_mots WHERE id_groupe=$id_groupe ORDER BY titre";
56 $result = spip_query($query);
57 $liste = array();
58 while ($row = spip_fetch_array($result)) {
59 $id_mot = $row['id_mot'];
60 $titre = $row['titre'];
61 $liste[$id_mot] = $titre;
62 }
63 }
64
65 // Nombre de reponses pour chaque valeur autorisee
66 $query = "SELECT c.valeur, COUNT(*) AS num ".
67 "FROM spip_reponses AS r LEFT JOIN spip_reponses_champs AS c USING (id_reponse) ".
68 "WHERE r.id_form=$id_form AND r.statut='valide' ".
69 "AND c.champ='".addslashes($t['code'])."' GROUP BY c.valeur";
70 $result = spip_query($query);
71 $chiffres = array();
72 // Stocker pour regurgiter dans l'ordre
73 while ($row = spip_fetch_array($result)) {
74 $chiffres[$row['valeur']] = $row['num'];
75 }
76
77 // Afficher les resultats
78 $r .= "<strong>".propre($t['nom'])." :</strong><br />\n";
79 $r .= "<div class='sondage_table'>";
80 foreach ($liste as $valeur => $nom) {
81 $r .= "<div class='sondage_ligne'>";
82 $n = $chiffres[$valeur];
83 $taux = floor($n * 100.0 / $total_reponses);
84 $r .= "<div class='ligne_nom'>".typo($nom)." </div>";
85 $r .= "<div style='width: 60%;'><div class='ligne_barre' style='width: $taux%;'></div></div>";
86 $r .= "<div class='ligne_chiffres'>$n ($taux&nbsp;%)</div>";
87 $r .= "</div>\n";
88 }
89 $r .= "</div>\n";
90 $r .= "<br />\n";
91 }
92
93 $query = "SELECT COUNT(*) AS num FROM spip_reponses ".
94 "WHERE id_form=$id_form AND statut='valide'";
95 $result = spip_query($query);
96 list($num) = spip_fetch_array($result,SPIP_NUM);
97 $r .= "<strong>"._T("forms:total_votes")." : $num</strong>";
98
99 $r .= "</div>\n";
100
101 return $r;
102 }
103
104
105 function balise_RESULTATS_SONDAGE($p) {
106 $_id_form = champ_sql('id_form', $p);
107
108 $p->code = "Forms_afficher_reponses_sondage(" . $_id_form . ")";
109 $p->statut = 'html';
110 return $p;
111 }
112
113 /*function boucle_FORMS_dist($id_boucle, &$boucles) {
114 $boucle = &$boucles[$id_boucle];
115 $id_table = $boucle->id_table;
116 $boucle->from[$id_table] = "spip_forms";
117 $boucle->hash = '
118 // CREER les table temporaire forms_champs et forms_champs_choix
119 forms_creer_tables_temporaires_boucles();
120 ';
121 return calculer_boucle($id_boucle, $boucles);
122 }*/
123
124 function forms_valeur($tableserialisee,$cle,$defaut=''){
125 $t = unserialize($tableserialisee);
126 return isset($t[$cle])?$t[$cle]:$defaut;
127 }
128
129 ?>