2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
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');
28 foreach ($flags as $flag) {
29 $ret[] = $trad[$flag];
31 return implode(', ',$ret);
34 class StatsModule
extends PLModule
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
),
44 'stats/profile' => $this->make_hook('profile', AUTH_COOKIE
),
46 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC
),
50 function handler_stats(&$page)
52 $page->changeTpl('stats/index.tpl');
55 function handler_evolution(&$page, $jours = 365)
57 $page->changeTpl('stats/evolution_inscrits.tpl');
58 $page->assign('jours', $jours);
61 function handler_graph_evo(&$page, $jours = 365)
63 define('DUREEJOUR', 24 * 3600);
65 //recupere le nombre d'inscriptions par jour sur la plage concernée
66 $res = XDB
::iterRow('SELECT IF(registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
67 TO_DAYS(registration_date) - TO_DAYS(NOW()),
71 LEFT JOIN account_profiles AS ap ON(ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.flags))
72 LEFT JOIN profiles AS p ON (ap.pid = p.pid)
73 WHERE state = \'active\' AND p.deathdate IS NULL
74 GROUP BY jour', (int)$jours, 1 +
(int)$jours);
76 //genere des donnees compatibles avec GNUPLOT
79 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
80 list(,$init_nb) = $res->next();
82 $numjour = - $jours - 1;
84 for ($i = -$jours; $i<=0; $i++
) {
86 if(!list($numjour, $nb) = $res->next()) {
91 if ($numjour==$i) $total+
=$nb;
92 $inscrits .= date('d/m/y',$i*DUREEJOUR+
time())." ".$total."\n";
95 //Genere le graphique à la volée avec GNUPLOT
96 header( "Content-type: image/png");
98 $delt = ($total - $init_nb)/10;
99 $delt = $delt ?
$delt : 5;
100 $ymin = round($init_nb - $delt,0);
101 $ymax = round($total +
$delt,0);
106 set term png small color
109 set timefmt
"%d/%m/%y"
114 set title
"Nombre d'inscrits"
116 plot
"-" using
1:2 title
'inscrits' with lines
;
125 function handler_graph(&$page, $promo = null
)
127 if ($promo == 'all') {
131 //recupere le nombre d'inscriptions par jour sur la plage concernée
132 $res = XDB
::iterRow("SELECT pe.entry_year AS promo, SUM(state = 'active') / COUNT(*) * 100
134 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
135 INNER JOIN profiles AS p ON (p.pid = ap.pid)
136 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET('primary', pe.flags))
137 WHERE pe.entry_year >= {?} AND p.deathdate IS NULL
138 GROUP BY promo", $depart);
140 //genere des donnees compatibles avec GNUPLOT
143 // la première ligne contient le total des inscrits avant la date de départ
144 list($annee, $nb) = $res->next();
146 for ($i = $depart; $i <= date("Y"); $i++
) {
148 if(!list($annee, $nb) = $res->next()) {
153 if ($nb > 0 ||
$i < date('Y'))
154 $inscrits .= $i.' '.$nb."\n";
157 //Genere le graphique à la volée avec GNUPLOT
163 set term png small color
165 set timefmt
"%d/%m/%y"
167 set xr
[$depart:$fin]
170 set title
"Proportion d'inscrits par promotion depuis $depart, en %."
172 plot
"-" using
1:2 title
'inscrits' with boxes
;
178 //nombre de jours sur le graph
180 define('DUREEJOUR', 24 * 3600);
182 $res = XDB
::query("SELECT MIN(TO_DAYS(a.registration_date) - TO_DAYS(NOW()))
184 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
185 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET('primary', pe.flags))
186 WHERE pe.entry_year = {?} AND a.state = 'active'", (int)$promo);
187 $jours = -$res->fetchOneCell();
189 //recupere le nombre d'inscriptions par jour sur la plage concernée
190 $res = XDB
::iterRow("SELECT IF(a.registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
191 TO_DAYS(a.registration_date) - TO_DAYS(NOW()),
195 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
196 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET('primary', pe.flags))
197 WHERE pe.entry_year = {?} AND a.state = 'active'
198 GROUP BY jour", (int)$jours, 1 +
(int)$jours, (int)$promo);
200 //genere des donnees compatibles avec GNUPLOT
203 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
204 list(,$init_nb) = $res->next();
207 list($numjour, $nb) = $res->next();
209 for ($i = -$jours;$i<=0;$i++
) {
211 if(!list($numjour, $nb) = $res->next()) {
216 if ($numjour==$i) $total+
=$nb;
217 $inscrits .= date('d/m/y',$i*DUREEJOUR+
time())." ".$total."\n";
220 //Genere le graphique à la volée avec GNUPLOT
221 $delt = ($total - $init_nb) / 10;
222 $delt +
= ($delt < 1);
223 $ymin = round($init_nb - $delt,0);
224 $ymax = round($total +
$delt,0);
229 set term png small color
232 set timefmt
"%d/%m/%y"
237 set title
"Nombre d'inscrits de la promotion $promo."
239 plot
"-" using
1:2 title
'inscrits' with lines
;
245 header('Content-type: image/png');
250 function handler_promos(&$page, $promo = null
)
252 $page->changeTpl('stats/nb_by_promo.tpl');
254 $res = XDB
::iterRow('SELECT pe.entry_year AS promo, COUNT(*)
256 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
257 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET(\'primary\', pe.flags))
258 WHERE pe.entry_year >= 1900 AND a.state = \'active\'
263 while (list($p,$nb) = $res->next()) {
265 if(!isset($nbpromo[$p/10])) {
266 $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides
268 $nbpromo[$p/10][$p%10
]=Array('promo' => $p, 'nb' => $nb);
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);
277 function handler_coupures(&$page, $cp_id = null
)
279 $page->changeTpl('stats/coupure.tpl');
281 if (!is_null($cp_id)) {
282 $res = XDB
::query("SELECT debut,
283 TIME_FORMAT(duree,'%kh%i') AS duree,
284 resume, description, services
286 WHERE id = {?}", $cp_id);
287 $cp = $res->fetchOneAssoc();
291 $cp['lg_services'] = serv_to_str($cp['services']);
292 $page->assign_by_ref('cp',$cp);
294 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
295 $sql = "SELECT id, debut, resume, services
296 FROM coupures where debut > '$beginning_date' order by debut desc";
297 $page->assign('coupures', XDB
::iterator($sql));
298 $res = XDB
::iterator("SELECT host, text
300 WHERE state != 'ok'");
301 $page->assign('mxs', $res);
305 function handler_profile(&$page, $period = 'overall')
307 $page->changeTpl('stats/profile.tpl');
311 case 'week': case 'month': case 'year':
312 $time = ' AND e.stamp > DATE_SUB(CURDATE(), INTERVAL 1 ' . strtoupper($period) . ')';
315 $rows = XDB
::fetchAllAssoc("SELECT p.pid AS profile, COUNT(*) AS count
316 FROM logger.events AS e
317 INNER JOIN logger.actions AS act ON (e.action = act.id)
318 INNER JOIN profiles AS p ON (p.hrpid = e.data)
319 WHERE act.text = 'view_profile' $time
323 foreach ($rows as $key=>$row) {
324 $rows[$key]['profile'] = Profile
::get($rows[$key]['profile']);
326 $page->assign('profiles', $rows);
327 $page->assign('period', $period);
331 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: