c12eb9798710158c2d62c1fab88ab3d8039e4d31
[platal.git] / modules / stats.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 function serv_to_str($params) {
23 $flags = explode(',',$params);
24 $trad = Array('web' => 'site web', 'mail'=> 'redirection mail',
25 'smtp' => 'serveur sécurisé d\'envoi de mails',
26 'nntp' => 'serveur des forums de discussion');
27 $ret = Array();
28 foreach ($flags as $flag) {
29 $ret[] = $trad[$flag];
30 }
31 return implode(', ',$ret);
32 }
33
34 class StatsModule extends PLModule
35 {
36 function handlers()
37 {
38 return array(
39 'stats' => $this->make_hook('stats', AUTH_COOKIE),
40 'stats/evolution' => $this->make_hook('evolution', AUTH_COOKIE),
41 'stats/graph' => $this->make_hook('graph', AUTH_COOKIE),
42 'stats/graph/evolution'
43 => $this->make_hook('graph_evo', AUTH_COOKIE),
44 'stats/promos' => $this->make_hook('promos', AUTH_COOKIE),
45
46 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC),
47 );
48 }
49
50 function handler_stats(&$page)
51 {
52 $page->changeTpl('stats/index.tpl');
53 }
54
55 function handler_evolution(&$page, $jours = 365)
56 {
57 $page->changeTpl('stats/evolution_inscrits.tpl');
58 $page->assign('jours', $jours);
59 }
60
61 function handler_graph_evo(&$page, $jours = 365)
62 {
63 global $globals;
64
65 define('DUREEJOUR',24*3600);
66
67 //recupere le nombre d'inscriptions par jour sur la plage concernée
68 $res = XDB::iterRow(
69 "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY),
70 TO_DAYS(date_ins)-TO_DAYS(NOW()),
71 ".(-($jours+1)).") AS jour,
72 COUNT(user_id) AS nb
73 FROM auth_user_md5
74 WHERE perms IN ('admin','user')
75 GROUP BY jour");
76
77 //genere des donnees compatibles avec GNUPLOT
78 $inscrits='';
79
80 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
81 list(,$init_nb) = $res->next();
82 $total = $init_nb;
83
84 list($numjour, $nb) = $res->next();
85
86 for ($i = -$jours; $i<=0; $i++) {
87 if ($numjour<$i) {
88 if(!list($numjour, $nb) = $res->next()) {
89 $numjour = 0;
90 $nb = 0;
91 }
92 }
93 if ($numjour==$i) $total+=$nb;
94 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
95 }
96
97 //Genere le graphique à la volée avec GNUPLOT
98 header( "Content-type: image/png");
99
100 $delt = ($total - $init_nb)/10;
101 $ymin = round($init_nb - $delt,0);
102 $ymax = round($total + $delt,0);
103
104 $gnuplot = <<<EOF2
105 gnuplot <<EOF
106
107 set term png small color
108 set size 640/480
109 set xdata time
110 set timefmt "%d/%m/%y"
111
112 set format x "%m/%y"
113 set yr [$ymin:$ymax]
114
115 set title "Nombre d'inscrits"
116
117 plot "-" using 1:2 title 'inscrits' with lines;
118 {$inscrits}
119 EOF
120 EOF2;
121
122 passthru($gnuplot);
123 exit;
124 }
125
126 function handler_graph(&$page, $promo = null)
127 {
128 global $globals;
129
130 if ($promo == 'all') {
131 // date de départ
132 $depart = 1920;
133
134 //recupere le nombre d'inscriptions par jour sur la plage concernée
135 $res = XDB::iterRow(
136 "SELECT promo, SUM(perms IN ('admin', 'user')) / COUNT(*) * 100
137 FROM auth_user_md5
138 WHERE promo >= $depart AND deces = 0
139 GROUP BY promo");
140
141 //genere des donnees compatibles avec GNUPLOT
142 $inscrits='';
143
144 // la première ligne contient le total des inscrits avant la date de départ
145 list($annee, $nb) = $res->next();
146
147 for ($i = $depart; $i <= date("Y"); $i++) {
148 if ($annee < $i) {
149 if(!list($annee, $nb) = $res->next()) {
150 $annee = 0;
151 $nb = 0;
152 }
153 }
154 if ($nb > 0 || $i < date('Y'))
155 $inscrits .= $i.' '.$nb."\n";
156 }
157
158 //Genere le graphique à la volée avec GNUPLOT
159 $fin = $i+2;
160
161 $gnuplot = <<<EOF2
162 gnuplot <<EOF
163
164 set term png small color
165 set size 640/480
166 set timefmt "%d/%m/%y"
167
168 set xr [$depart:$fin]
169 set yr [0:100]
170
171 set title "Nombre d'inscrits par promotion depuis $depart."
172
173 plot "-" using 1:2 title 'inscrits' with boxes;
174 {$inscrits}
175 EOF
176 EOF2;
177
178 } else {
179 //nombre de jours sur le graph
180 $jours = 365;
181 define('DUREEJOUR',24*3600);
182 $res = XDB::query("SELECT min(TO_DAYS(date_ins)-TO_DAYS(now()))
183 FROM auth_user_md5
184 WHERE promo = {?}
185 AND perms IN ('admin', 'user')",
186 $promo);
187 $jours = -$res->fetchOneCell();
188
189 //recupere le nombre d'inscriptions par jour sur la plage concernée
190 $res = XDB::iterRow(
191 "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY),
192 TO_DAYS(date_ins)-TO_DAYS(NOW()),
193 ".(-($jours+1)).") AS jour,
194 COUNT(user_id) AS nb
195 FROM auth_user_md5
196 WHERE promo = {?} AND perms IN ('admin','user')
197 GROUP BY jour", $promo);
198
199 //genere des donnees compatibles avec GNUPLOT
200 $inscrits='';
201
202 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
203 list(,$init_nb) = $res->next();
204 $total = $init_nb;
205
206 list($numjour, $nb) = $res->next();
207
208 for ($i = -$jours;$i<=0;$i++) {
209 if ($numjour<$i) {
210 if(!list($numjour, $nb) = $res->next()) {
211 $numjour = 0;
212 $nb = 0;
213 }
214 }
215 if ($numjour==$i) $total+=$nb;
216 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
217 }
218
219 //Genere le graphique à la volée avec GNUPLOT
220 $delt = ($total - $init_nb) / 10;
221 $delt += ($delt < 1);
222 $ymin = round($init_nb - $delt,0);
223 $ymax = round($total + $delt,0);
224
225 $gnuplot = <<<EOF2
226 gnuplot <<EOF
227
228 set term png small color
229 set size 640/480
230 set xdata time
231 set timefmt "%d/%m/%y"
232
233 set format x "%m/%y"
234 set yr [$ymin:$ymax]
235
236 set title "Nombre d'inscrits de la promotion $promo."
237
238 plot "-" using 1:2 title 'inscrits' with lines;
239 {$inscrits}e
240 EOF
241 EOF2;
242 }
243
244 header('Content-type: image/png');
245 passthru($gnuplot);
246 exit;
247 }
248
249 function handler_promos(&$page, $promo = null)
250 {
251 global $globals;
252
253 $page->changeTpl('stats/nb_by_promo.tpl');
254
255 $res = XDB::iterRow(
256 "SELECT promo,COUNT(*)
257 FROM auth_user_md5
258 WHERE promo > 1900 AND perms IN ('admin','user')
259 GROUP BY promo
260 ORDER BY promo");
261 $max=0; $min=3000;
262
263 while (list($p,$nb) = $res->next()) {
264 $p = intval($p);
265 if(!isset($nbpromo[$p/10])) {
266 $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides
267 }
268 $nbpromo[$p/10][$p%10]=Array('promo' => $p, 'nb' => $nb);
269 }
270
271 $page->assign_by_ref('nbs', $nbpromo);
272 $page->assign('min', $min-$min % 10);
273 $page->assign('max', $max+10-$max%10);
274 $page->assign('promo', $promo);
275 }
276
277 function handler_coupures(&$page, $cp_id = null)
278 {
279 global $globals;
280
281 $page->changeTpl('stats/coupure.tpl');
282
283 if (!is_null($cp_id)) {
284 $res = XDB::query("SELECT UNIX_TIMESTAMP(debut) AS debut,
285 TIME_FORMAT(duree,'%kh%i') AS duree,
286 resume, description, services
287 FROM coupures
288 WHERE id = {?}", $cp_id);
289 $cp = $res->fetchOneAssoc();
290 }
291
292 if($cp) {
293 $cp['lg_services'] = serv_to_str($cp['services']);
294 $page->assign_by_ref('cp',$cp);
295 } else {
296 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
297 $sql = "SELECT id, UNIX_TIMESTAMP(debut) AS debut, resume, services
298 FROM coupures where debut > '$beginning_date' order by debut desc";
299 $page->assign('coupures', XDB::iterator($sql));
300 }
301 }
302 }
303
304 ?>