Moving to GitHub.
[platal.git] / modules / stats.php
CommitLineData
bf2692e3 1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 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(
e5ceaa8c
RB
39 'stats' => $this->make_hook('stats', AUTH_COOKIE, 'user'),
40 'stats/evolution' => $this->make_hook('evolution', AUTH_COOKIE, 'user'),
41 'stats/graph' => $this->make_hook('graph', AUTH_COOKIE, 'user'),
42 'stats/graph/evolution' => $this->make_hook('graph_evo', AUTH_COOKIE, 'user'),
43 'stats/promos' => $this->make_hook('promos', AUTH_COOKIE, 'user'),
bf2692e3 44
eb5a266d 45 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC),
bf2692e3 46 );
47 }
48
26ba053e 49 function handler_stats($page)
bf2692e3 50 {
51 $page->changeTpl('stats/index.tpl');
bf2692e3 52 }
53
9fd2ca04 54 function handler_evolution($page, $days = 365)
bf2692e3 55 {
56 $page->changeTpl('stats/evolution_inscrits.tpl');
9fd2ca04 57 $page->assign('days', $days);
bf2692e3 58 }
59
9fd2ca04 60 function handler_graph_evo($page, $days = 365)
bf2692e3 61 {
9fd2ca04 62 $day_length = 24 * 3600;
bf2692e3 63
9fd2ca04 64 // Retrieve the registration count per days during the given date range.
b1be6d17
FB
65 $res = XDB::iterRow('SELECT IF(registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
66 TO_DAYS(registration_date) - TO_DAYS(NOW()),
9fd2ca04 67 - {?}) AS day,
edf5e979 68 COUNT(a.uid) AS nb
9fd2ca04
SJ
69 FROM accounts AS a
70 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
71 INNER JOIN profiles AS p ON (ap.pid = p.pid)
72 WHERE a.state = \'active\' AND p.deathdate IS NULL
73 GROUP BY day',
74 (int)$days, 1 + (int)$days);
75
76 // The first contains the registration count before the starting date (J - $days)
77 list(, $init_nb) = $res->next();
78 $total = $init_nb;
79 $num_day = - $days - 1;
80
81 $registered = '';
82 for ($i = -$days; $i <= 0; ++$i) {
83 if ($num_day < $i) {
84 if(!list($num_day, $nb) = $res->next()) {
85 $num_day = 0;
bf2692e3 86 $nb = 0;
87 }
88 }
9fd2ca04
SJ
89 if ($num_day == $i) {
90 $total += $nb;
91 }
92 $registered .= date('d/m/y', $i * $day_length + time()) . ' ' . $total . "\n";
bf2692e3 93 }
94
a7de4ef7 95 //Genere le graphique à la volée avec GNUPLOT
3cb500d5 96 pl_cached_dynamic_content_headers("image/png");
bf2692e3 97
9fd2ca04 98 $delt = ($total - $init_nb) / 10;
0521940c 99 $delt = $delt ? $delt : 5;
9fd2ca04
SJ
100 $ymin = round($init_nb - $delt, 0);
101 $ymax = round($total + $delt, 0);
bf2692e3 102
103 $gnuplot = <<<EOF2
104gnuplot <<EOF
105
106set term png small color
107set size 640/480
108set xdata time
109set timefmt "%d/%m/%y"
110
4490b764 111set format x "%d/%m\\n%Y"
bf2692e3 112set yr [$ymin:$ymax]
113
114set title "Nombre d'inscrits"
115
116plot "-" using 1:2 title 'inscrits' with lines;
9fd2ca04 117{$registered}
bf2692e3 118EOF
119EOF2;
120
121 passthru($gnuplot);
122 exit;
123 }
124
26ba053e 125 function handler_graph($page, $promo = null)
bf2692e3 126 {
9fd2ca04
SJ
127 if (in_array($promo, array(Profile::DEGREE_X, Profile::DEGREE_M, Profile::DEGREE_D))) {
128 $cycle = Profile::$cycles[$promo] . 's';
129 $res = XDB::iterRow("SELECT pe.promo_year, SUM(a.state = 'active') / COUNT(*) * 100
130 FROM accounts AS a
131 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
132 INNER JOIN profiles AS p ON (p.pid = ap.pid)
133 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET('primary', pe.flags))
134 INNER JOIN profile_education_degree_enum AS ped ON (pe.degreeid = ped.id)
135 WHERE p.deathdate IS NULL AND ped.degree = {?}
136 GROUP BY pe.promo_year",
137 $promo);
138
139 list($promo, $count) = $res->next();
140 $first = $promo;
141 $registered = $promo . ' ' . $count . "\n";
142 while ($next = $res->next()) {
143 list($promo, $count) = $next;
144 $registered .= $promo . ' ' . $count . "\n";
bf2692e3 145 }
9fd2ca04 146 $last = $promo + 2;
bf2692e3 147
9fd2ca04 148 // Generate drawing thanks to Gnuplot.
bf2692e3 149 $gnuplot = <<<EOF2
150gnuplot <<EOF
151
152set term png small color
153set size 640/480
154set timefmt "%d/%m/%y"
155
9fd2ca04 156set xr [$first:$last]
bf2692e3 157set yr [0:100]
158
9fd2ca04 159set title "Proportion de $cycle inscrits par promotion, en %."
c1512af9 160set key left top
bf2692e3 161
162plot "-" using 1:2 title 'inscrits' with boxes;
9fd2ca04 163{$registered}
bf2692e3 164EOF
165EOF2;
166
167 } else {
9fd2ca04
SJ
168 $day_length = 24 * 3600;
169 $days = 365;
b1be6d17 170
b1be6d17 171 $res = XDB::query("SELECT MIN(TO_DAYS(a.registration_date) - TO_DAYS(NOW()))
9fd2ca04 172 FROM accounts AS a
b1be6d17 173 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
9fd2ca04
SJ
174 INNER JOIN profile_display AS pd ON (ap.pid = pd.pid)
175 WHERE pd.promo = {?} AND a.state = 'active'",
176 $promo);
177 $days = -$res->fetchOneCell();
bf2692e3 178
9fd2ca04 179 // Retrieve the registration count per days during the given date range.
b1be6d17
FB
180 $res = XDB::iterRow("SELECT IF(a.registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
181 TO_DAYS(a.registration_date) - TO_DAYS(NOW()),
9fd2ca04 182 - {?}) AS day,
b1be6d17 183 COUNT(a.uid) AS nb
9fd2ca04 184 FROM accounts AS a
b1be6d17 185 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
9fd2ca04
SJ
186 INNER JOIN profile_display AS pd ON (ap.pid = pd.pid)
187 WHERE pd.promo = {?} AND a.state = 'active'
188 GROUP BY day",
189 (int)$days, 1 + (int)$days, $promo);
bf2692e3 190
9fd2ca04
SJ
191 // The first line contains the registration count before starting date (D - $days).
192 list(, $init_nb) = $res->next();
bf2692e3 193 $total = $init_nb;
9fd2ca04 194 $registered = '';
bf2692e3 195
9fd2ca04 196 list($num_day, $nb) = $res->next();
bf2692e3 197
9fd2ca04
SJ
198 for ($i = -$days; $i <= 0; ++$i) {
199 if ($num_day < $i) {
200 if(!list($num_day, $nb) = $res->next()) {
201 $num_day = 0;
bf2692e3 202 $nb = 0;
203 }
204 }
9fd2ca04
SJ
205 if ($num_day == $i) {
206 $total += $nb;
207 }
208 $registered .= date('d/m/y', $i * $day_length + time()) . ' ' . $total . "\n";
bf2692e3 209 }
210
9fd2ca04 211 // Generate drawing thanks to Gnuplot.
bf2692e3 212 $delt = ($total - $init_nb) / 10;
213 $delt += ($delt < 1);
9fd2ca04
SJ
214 $ymin = round($init_nb - $delt, 0);
215 $ymax = round($total + $delt, 0);
bf2692e3 216
217 $gnuplot = <<<EOF2
218gnuplot <<EOF
219
220set term png small color
221set size 640/480
222set xdata time
223set timefmt "%d/%m/%y"
224
225set format x "%m/%y"
226set yr [$ymin:$ymax]
227
228set title "Nombre d'inscrits de la promotion $promo."
229
230plot "-" using 1:2 title 'inscrits' with lines;
9fd2ca04 231{$registered}e
bf2692e3 232EOF
233EOF2;
234 }
235
3cb500d5 236 pl_cached_dynamic_content_headers("image/png");
bf2692e3 237 passthru($gnuplot);
238 exit;
239 }
240
9fd2ca04 241 function handler_promos($page, $required_promo = null)
bf2692e3 242 {
bf2692e3 243 $page->changeTpl('stats/nb_by_promo.tpl');
750727e8 244 $cycles = array('X' => 'Polytechniciens', 'M' => 'Masters', 'D' => 'Docteurs');
bf2692e3 245
9fd2ca04
SJ
246 $res = XDB::iterRow('SELECT pd.promo, COUNT(*)
247 FROM accounts AS a
b1be6d17 248 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
9fd2ca04
SJ
249 INNER JOIN profiles AS p ON (p.pid = ap.pid)
250 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid)
251 WHERE a.state = \'active\' AND p.deathdate IS NULL AND pd.promo != \'D (en cours)\'
252 GROUP BY pd.promo
253 ORDER BY pd.promo LIKE \'D%\', pd.promo LIKE \'M%\', pd.promo LIKE \'X%\', pd.promo');
254
255 $nbpromo = array();
256 while (list($promo, $count) = $res->next()) {
257 $prefix = substr($promo, 0, 4) . '-';
258 $unit = substr($promo, -1);
750727e8
SJ
259 if(!isset($nbpromo[$cycles[$promo{0}]][$prefix])) {
260 $nbpromo[$cycles[$promo{0}]][$prefix] = array('', '', '', '', '', '', '', '', '', ''); // Empty array containing 10 cells.
bf2692e3 261 }
750727e8 262 $nbpromo[$cycles[$promo{0}]][$prefix][$unit] = array('promo' => $promo, 'nb' => $count);
bf2692e3 263 }
264
9fd2ca04
SJ
265 $count = XDB::fetchOneCell('SELECT COUNT(*)
266 FROM accounts AS a
267 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
268 INNER JOIN profiles AS p ON (p.pid = ap.pid)
269 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid)
270 WHERE a.state = \'active\' AND p.deathdate IS NULL AND pd.promo = \'D (en cours)\'');
750727e8 271 $nbpromo[$cycles['D']]['D (en cours)'][0] = array('promo' => 'D (en cours)', 'nb' => $count);
9fd2ca04 272
bf2692e3 273 $page->assign_by_ref('nbs', $nbpromo);
9fd2ca04 274 $page->assign('promo', $required_promo);
bf2692e3 275 }
276
26ba053e 277 function handler_coupures($page, $cp_id = null)
bf2692e3 278 {
bf2692e3 279 $page->changeTpl('stats/coupure.tpl');
280
281 if (!is_null($cp_id)) {
da398501 282 $res = XDB::query("SELECT debut,
283 TIME_FORMAT(duree,'%kh%i') AS duree,
284 resume, description, services
06f4daf9 285 FROM downtimes
da398501 286 WHERE id = {?}", $cp_id);
bf2692e3 287 $cp = $res->fetchOneAssoc();
288 }
289
ccdbc270 290 if(@$cp) {
bf2692e3 291 $cp['lg_services'] = serv_to_str($cp['services']);
292 $page->assign_by_ref('cp',$cp);
293 } else {
294 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
da398501 295 $sql = "SELECT id, debut, resume, services
06f4daf9 296 FROM downtimes where debut > '$beginning_date' order by debut desc";
08cce2ff 297 $page->assign('coupures', XDB::iterator($sql));
ccdbc270 298 $res = XDB::iterator("SELECT host, text
299 FROM mx_watch
300 WHERE state != 'ok'");
301 $page->assign('mxs', $res);
bf2692e3 302 }
bf2692e3 303 }
304}
305
448c8cdc 306// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
bf2692e3 307?>