Happy New Year!
[platal.git] / modules / stats.php
CommitLineData
bf2692e3 1<?php
2/***************************************************************************
5e1513f6 3 * Copyright (C) 2003-2011 Polytechnique.org *
bf2692e3 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',
a7de4ef7 25 'smtp' => 'serveur sécurisé d\'envoi de mails',
bf2692e3 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(
eb5a266d
SJ
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' => $this->make_hook('graph_evo', AUTH_COOKIE),
43 'stats/promos' => $this->make_hook('promos', AUTH_COOKIE),
bf2692e3 44
eb5a266d 45 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC),
bf2692e3 46 );
47 }
48
49 function handler_stats(&$page)
50 {
51 $page->changeTpl('stats/index.tpl');
bf2692e3 52 }
53
54 function handler_evolution(&$page, $jours = 365)
55 {
56 $page->changeTpl('stats/evolution_inscrits.tpl');
57 $page->assign('jours', $jours);
bf2692e3 58 }
59
60 function handler_graph_evo(&$page, $jours = 365)
61 {
b1be6d17 62 define('DUREEJOUR', 24 * 3600);
bf2692e3 63
a7de4ef7 64 //recupere le nombre d'inscriptions par jour sur la plage concernée
b1be6d17
FB
65 $res = XDB::iterRow('SELECT IF(registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
66 TO_DAYS(registration_date) - TO_DAYS(NOW()),
67 -{?}) AS jour,
edf5e979 68 COUNT(a.uid) AS nb
f476ca60 69 FROM accounts AS a
edf5e979 70 LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
f476ca60
FB
71 LEFT JOIN profiles AS p ON (ap.pid = p.pid)
72 WHERE state = \'active\' AND p.deathdate IS NULL
b1be6d17 73 GROUP BY jour', (int)$jours, 1 + (int)$jours);
bf2692e3 74
75 //genere des donnees compatibles avec GNUPLOT
76 $inscrits='';
77
a7de4ef7 78 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
bf2692e3 79 list(,$init_nb) = $res->next();
0521940c 80 $total = $init_nb;
81 $numjour = - $jours - 1;
bf2692e3 82
83 for ($i = -$jours; $i<=0; $i++) {
84 if ($numjour<$i) {
85 if(!list($numjour, $nb) = $res->next()) {
86 $numjour = 0;
87 $nb = 0;
88 }
89 }
90 if ($numjour==$i) $total+=$nb;
91 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
92 }
93
a7de4ef7 94 //Genere le graphique à la volée avec GNUPLOT
3cb500d5 95 pl_cached_dynamic_content_headers("image/png");
bf2692e3 96
97 $delt = ($total - $init_nb)/10;
0521940c 98 $delt = $delt ? $delt : 5;
bf2692e3 99 $ymin = round($init_nb - $delt,0);
100 $ymax = round($total + $delt,0);
101
102 $gnuplot = <<<EOF2
103gnuplot <<EOF
104
105set term png small color
106set size 640/480
107set xdata time
108set timefmt "%d/%m/%y"
109
110set format x "%m/%y"
111set yr [$ymin:$ymax]
112
113set title "Nombre d'inscrits"
114
115plot "-" using 1:2 title 'inscrits' with lines;
116{$inscrits}
117EOF
118EOF2;
119
120 passthru($gnuplot);
121 exit;
122 }
123
124 function handler_graph(&$page, $promo = null)
125 {
bf2692e3 126 if ($promo == 'all') {
a7de4ef7 127 // date de départ
203bc766 128 $depart = 1930;
bf2692e3 129
a7de4ef7 130 //recupere le nombre d'inscriptions par jour sur la plage concernée
b1be6d17
FB
131 $res = XDB::iterRow("SELECT pe.entry_year AS promo, SUM(state = 'active') / COUNT(*) * 100
132 FROM accounts AS a
133 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
f476ca60 134 INNER JOIN profiles AS p ON (p.pid = ap.pid)
ce0b2c6f 135 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET('primary', pe.flags))
f476ca60 136 WHERE pe.entry_year >= {?} AND p.deathdate IS NULL
b1be6d17 137 GROUP BY promo", $depart);
bf2692e3 138
139 //genere des donnees compatibles avec GNUPLOT
140 $inscrits='';
141
a7de4ef7 142 // la première ligne contient le total des inscrits avant la date de départ
bf2692e3 143 list($annee, $nb) = $res->next();
144
145 for ($i = $depart; $i <= date("Y"); $i++) {
146 if ($annee < $i) {
147 if(!list($annee, $nb) = $res->next()) {
148 $annee = 0;
149 $nb = 0;
150 }
151 }
152 if ($nb > 0 || $i < date('Y'))
153 $inscrits .= $i.' '.$nb."\n";
154 }
155
a7de4ef7 156 //Genere le graphique à la volée avec GNUPLOT
bf2692e3 157 $fin = $i+2;
158
159 $gnuplot = <<<EOF2
160gnuplot <<EOF
161
162set term png small color
163set size 640/480
164set timefmt "%d/%m/%y"
165
166set xr [$depart:$fin]
167set yr [0:100]
168
203bc766 169set title "Proportion d'inscrits par promotion depuis $depart, en %."
bf2692e3 170
171plot "-" using 1:2 title 'inscrits' with boxes;
172{$inscrits}
173EOF
174EOF2;
175
176 } else {
177 //nombre de jours sur le graph
178 $jours = 365;
b1be6d17
FB
179 define('DUREEJOUR', 24 * 3600);
180
b1be6d17
FB
181 $res = XDB::query("SELECT MIN(TO_DAYS(a.registration_date) - TO_DAYS(NOW()))
182 FROM accounts AS a
183 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
ce0b2c6f 184 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET('primary', pe.flags))
b1be6d17 185 WHERE pe.entry_year = {?} AND a.state = 'active'", (int)$promo);
bf2692e3 186 $jours = -$res->fetchOneCell();
187
a7de4ef7 188 //recupere le nombre d'inscriptions par jour sur la plage concernée
b1be6d17
FB
189 $res = XDB::iterRow("SELECT IF(a.registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
190 TO_DAYS(a.registration_date) - TO_DAYS(NOW()),
191 -{?}) AS jour,
192 COUNT(a.uid) AS nb
193 FROM accounts AS a
194 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
ce0b2c6f 195 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET('primary', pe.flags))
b1be6d17
FB
196 WHERE pe.entry_year = {?} AND a.state = 'active'
197 GROUP BY jour", (int)$jours, 1 + (int)$jours, (int)$promo);
bf2692e3 198
199 //genere des donnees compatibles avec GNUPLOT
200 $inscrits='';
201
a7de4ef7 202 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
bf2692e3 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
a7de4ef7 219 //Genere le graphique à la volée avec GNUPLOT
bf2692e3 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
226gnuplot <<EOF
227
228set term png small color
229set size 640/480
230set xdata time
231set timefmt "%d/%m/%y"
232
233set format x "%m/%y"
234set yr [$ymin:$ymax]
235
236set title "Nombre d'inscrits de la promotion $promo."
237
238plot "-" using 1:2 title 'inscrits' with lines;
239{$inscrits}e
240EOF
241EOF2;
242 }
243
3cb500d5 244 pl_cached_dynamic_content_headers("image/png");
bf2692e3 245 passthru($gnuplot);
246 exit;
247 }
248
249 function handler_promos(&$page, $promo = null)
250 {
bf2692e3 251 $page->changeTpl('stats/nb_by_promo.tpl');
252
b1be6d17
FB
253 $res = XDB::iterRow('SELECT pe.entry_year AS promo, COUNT(*)
254 FROM accounts AS a
255 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
ce0b2c6f 256 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'primary\', pe.flags))
b1be6d17
FB
257 WHERE pe.entry_year >= 1900 AND a.state = \'active\'
258 GROUP BY promo
259 ORDER BY promo');
bf2692e3 260 $max=0; $min=3000;
261
262 while (list($p,$nb) = $res->next()) {
263 $p = intval($p);
264 if(!isset($nbpromo[$p/10])) {
265 $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides
266 }
267 $nbpromo[$p/10][$p%10]=Array('promo' => $p, 'nb' => $nb);
268 }
269
270 $page->assign_by_ref('nbs', $nbpromo);
271 $page->assign('min', $min-$min % 10);
272 $page->assign('max', $max+10-$max%10);
273 $page->assign('promo', $promo);
bf2692e3 274 }
275
276 function handler_coupures(&$page, $cp_id = null)
277 {
bf2692e3 278 $page->changeTpl('stats/coupure.tpl');
279
280 if (!is_null($cp_id)) {
da398501 281 $res = XDB::query("SELECT debut,
282 TIME_FORMAT(duree,'%kh%i') AS duree,
283 resume, description, services
06f4daf9 284 FROM downtimes
da398501 285 WHERE id = {?}", $cp_id);
bf2692e3 286 $cp = $res->fetchOneAssoc();
287 }
288
ccdbc270 289 if(@$cp) {
bf2692e3 290 $cp['lg_services'] = serv_to_str($cp['services']);
291 $page->assign_by_ref('cp',$cp);
292 } else {
293 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
da398501 294 $sql = "SELECT id, debut, resume, services
06f4daf9 295 FROM downtimes where debut > '$beginning_date' order by debut desc";
08cce2ff 296 $page->assign('coupures', XDB::iterator($sql));
ccdbc270 297 $res = XDB::iterator("SELECT host, text
298 FROM mx_watch
299 WHERE state != 'ok'");
300 $page->assign('mxs', $res);
bf2692e3 301 }
bf2692e3 302 }
303}
304
a7de4ef7 305// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
bf2692e3 306?>