backport patch 441
[platal.git] / modules / stats.php
CommitLineData
bf2692e3 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
22function 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
34class 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 return PL_OK;
54 }
55
56 function handler_evolution(&$page, $jours = 365)
57 {
58 $page->changeTpl('stats/evolution_inscrits.tpl');
59 $page->assign('jours', $jours);
60 return PL_OK;
61 }
62
63 function handler_graph_evo(&$page, $jours = 365)
64 {
65 global $globals;
66
67 define('DUREEJOUR',24*3600);
68
69 //recupere le nombre d'inscriptions par jour sur la plage concernée
70 $res = $globals->xdb->iterRow(
71 "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY),
72 TO_DAYS(date_ins)-TO_DAYS(NOW()),
73 ".(-($jours+1)).") AS jour,
74 COUNT(user_id) AS nb
75 FROM auth_user_md5
76 WHERE perms IN ('admin','user')
77 GROUP BY jour");
78
79 //genere des donnees compatibles avec GNUPLOT
80 $inscrits='';
81
82 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
83 list(,$init_nb) = $res->next();
84 $total = $init_nb;
85
86 list($numjour, $nb) = $res->next();
87
88 for ($i = -$jours; $i<=0; $i++) {
89 if ($numjour<$i) {
90 if(!list($numjour, $nb) = $res->next()) {
91 $numjour = 0;
92 $nb = 0;
93 }
94 }
95 if ($numjour==$i) $total+=$nb;
96 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
97 }
98
99 //Genere le graphique à la volée avec GNUPLOT
100 header( "Content-type: image/png");
101
102 $delt = ($total - $init_nb)/10;
103 $ymin = round($init_nb - $delt,0);
104 $ymax = round($total + $delt,0);
105
106 $gnuplot = <<<EOF2
107gnuplot <<EOF
108
109set term png small color
110set size 640/480
111set xdata time
112set timefmt "%d/%m/%y"
113
114set format x "%m/%y"
115set yr [$ymin:$ymax]
116
117set title "Nombre d'inscrits"
118
119plot "-" using 1:2 title 'inscrits' with lines;
120{$inscrits}
121EOF
122EOF2;
123
124 passthru($gnuplot);
125 exit;
126 }
127
128 function handler_graph(&$page, $promo = null)
129 {
130 global $globals;
131
132 if ($promo == 'all') {
133 // date de départ
134 $depart = 1920;
135
136 //recupere le nombre d'inscriptions par jour sur la plage concernée
137 $res = $globals->xdb->iterRow(
138 "SELECT promo, SUM(perms IN ('admin', 'user')) / COUNT(*) * 100
139 FROM auth_user_md5
140 WHERE promo >= $depart AND deces = 0
141 GROUP BY promo");
142
143 //genere des donnees compatibles avec GNUPLOT
144 $inscrits='';
145
146 // la première ligne contient le total des inscrits avant la date de départ
147 list($annee, $nb) = $res->next();
148
149 for ($i = $depart; $i <= date("Y"); $i++) {
150 if ($annee < $i) {
151 if(!list($annee, $nb) = $res->next()) {
152 $annee = 0;
153 $nb = 0;
154 }
155 }
156 if ($nb > 0 || $i < date('Y'))
157 $inscrits .= $i.' '.$nb."\n";
158 }
159
160 //Genere le graphique à la volée avec GNUPLOT
161 $fin = $i+2;
162
163 $gnuplot = <<<EOF2
164gnuplot <<EOF
165
166set term png small color
167set size 640/480
168set timefmt "%d/%m/%y"
169
170set xr [$depart:$fin]
171set yr [0:100]
172
173set title "Nombre d'inscrits par promotion depuis $depart."
174
175plot "-" using 1:2 title 'inscrits' with boxes;
176{$inscrits}
177EOF
178EOF2;
179
180 } else {
181 //nombre de jours sur le graph
182 $jours = 365;
183 define('DUREEJOUR',24*3600);
184 $res = $globals->xdb->query("SELECT min(TO_DAYS(date_ins)-TO_DAYS(now()))
185 FROM auth_user_md5
186 WHERE promo = {?}
187 AND perms IN ('admin', 'user')",
188 $promo);
189 $jours = -$res->fetchOneCell();
190
191 //recupere le nombre d'inscriptions par jour sur la plage concernée
192 $res = $globals->xdb->iterRow(
193 "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY),
194 TO_DAYS(date_ins)-TO_DAYS(NOW()),
195 ".(-($jours+1)).") AS jour,
196 COUNT(user_id) AS nb
197 FROM auth_user_md5
198 WHERE promo = {?} AND perms IN ('admin','user')
199 GROUP BY jour", $promo);
200
201 //genere des donnees compatibles avec GNUPLOT
202 $inscrits='';
203
204 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
205 list(,$init_nb) = $res->next();
206 $total = $init_nb;
207
208 list($numjour, $nb) = $res->next();
209
210 for ($i = -$jours;$i<=0;$i++) {
211 if ($numjour<$i) {
212 if(!list($numjour, $nb) = $res->next()) {
213 $numjour = 0;
214 $nb = 0;
215 }
216 }
217 if ($numjour==$i) $total+=$nb;
218 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
219 }
220
221 //Genere le graphique à la volée avec GNUPLOT
222 $delt = ($total - $init_nb) / 10;
223 $delt += ($delt < 1);
224 $ymin = round($init_nb - $delt,0);
225 $ymax = round($total + $delt,0);
226
227 $gnuplot = <<<EOF2
228gnuplot <<EOF
229
230set term png small color
231set size 640/480
232set xdata time
233set timefmt "%d/%m/%y"
234
235set format x "%m/%y"
236set yr [$ymin:$ymax]
237
238set title "Nombre d'inscrits de la promotion $promo."
239
240plot "-" using 1:2 title 'inscrits' with lines;
241{$inscrits}e
242EOF
243EOF2;
244 }
245
246 header('Content-type: image/png');
247 passthru($gnuplot);
248 exit;
249 }
250
251 function handler_promos(&$page, $promo = null)
252 {
253 global $globals;
254
255 $page->changeTpl('stats/nb_by_promo.tpl');
256
257 $res = $globals->xdb->iterRow(
258 "SELECT promo,COUNT(*)
259 FROM auth_user_md5
260 WHERE promo > 1900 AND perms IN ('admin','user')
261 GROUP BY promo
262 ORDER BY promo");
263 $max=0; $min=3000;
264
265 while (list($p,$nb) = $res->next()) {
266 $p = intval($p);
267 if(!isset($nbpromo[$p/10])) {
268 $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides
269 }
270 $nbpromo[$p/10][$p%10]=Array('promo' => $p, 'nb' => $nb);
271 }
272
273 $page->assign_by_ref('nbs', $nbpromo);
274 $page->assign('min', $min-$min % 10);
275 $page->assign('max', $max+10-$max%10);
276 $page->assign('promo', $promo);
277
278 return PL_OK;
279 }
280
281 function handler_coupures(&$page, $cp_id = null)
282 {
283 global $globals;
284
285 $page->changeTpl('stats/coupure.tpl');
286
287 if (!is_null($cp_id)) {
288 $res = $globals->xdb->query("SELECT UNIX_TIMESTAMP(debut) AS debut,
289 TIME_FORMAT(duree,'%kh%i') AS duree,
290 resume, description, services
291 FROM coupures
292 WHERE id = {?}", $cp_id);
293 $cp = $res->fetchOneAssoc();
294 }
295
296 if($cp) {
297 $cp['lg_services'] = serv_to_str($cp['services']);
298 $page->assign_by_ref('cp',$cp);
299 } else {
300 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
301 $sql = "SELECT id, UNIX_TIMESTAMP(debut) AS debut, resume, services
302 FROM coupures where debut > '$beginning_date' order by debut desc";
303 $page->assign('coupures', $globals->xdb->iterator($sql));
304 }
305
306 return PL_OK;
307 }
308}
309
310?>