2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 class MarketingModule
extends PLModule
27 'marketing' => $this->make_hook('marketing', AUTH_MDP
, 'admin'),
28 'marketing/promo' => $this->make_hook('promo', AUTH_MDP
, 'admin'),
29 'marketing/relance' => $this->make_hook('relance', AUTH_MDP
, 'admin'),
30 'marketing/this_week' => $this->make_hook('week', AUTH_MDP
, 'admin'),
31 'marketing/volontaire' => $this->make_hook('volontaire', AUTH_MDP
, 'admin'),
33 'marketing/private' => $this->make_hook('private', AUTH_MDP
, 'admin'),
34 'marketing/public' => $this->make_hook('public', AUTH_COOKIE
),
35 'marketing/broken' => $this->make_hook('broken', AUTH_COOKIE
),
39 function handler_marketing(&$page)
41 $page->changeTpl('marketing/index.tpl');
43 $page->setTitle('Marketing');
45 // Quelques statistiques
48 "SELECT COUNT(*) AS vivants,
49 COUNT(NULLIF(perms='admin' OR perms='user', 0)) AS inscrits,
50 100*COUNT(NULLIF(perms='admin' OR perms='user', 0))/COUNT(*) AS ins_rate,
51 COUNT(NULLIF(promo >= 1972, 0)) AS vivants72,
52 COUNT(NULLIF(promo >= 1972 AND (perms='admin' OR perms='user'), 0)) AS inscrits72,
53 100 * COUNT(NULLIF(promo >= 1972 AND (perms='admin' OR perms='user'), 0)) /
54 COUNT(NULLIF(promo >= 1972, 0)) AS ins72_rate,
55 COUNT(NULLIF(FIND_IN_SET('femme', flags), 0)) AS vivantes,
56 COUNT(NULLIF(FIND_IN_SET('femme', flags) AND (perms='admin' OR perms='user'), 0)) AS inscrites,
57 100 * COUNT(NULLIF(FIND_IN_SET('femme', flags) AND (perms='admin' OR perms='user'), 0)) /
58 COUNT(NULLIF(FIND_IN_SET('femme', flags), 0)) AS inse_rate
61 $stats = $res->fetchOneAssoc();
62 $page->assign('stats', $stats);
64 $res = XDB
::query("SELECT count(*) FROM auth_user_md5 WHERE date_ins > ".
65 date('Ymd000000', strtotime('1 week ago')));
66 $page->assign('nbInsSem', $res->fetchOneCell());
68 $res = XDB
::query("SELECT count(*) FROM register_pending WHERE hash != 'INSCRIT'");
69 $page->assign('nbInsEnCours', $res->fetchOneCell());
71 $res = XDB
::query("SELECT count(*) FROM register_marketing");
72 $page->assign('nbInsMarket', $res->fetchOneCell());
74 $res = XDB
::query("SELECT count(*) FROM register_mstats
75 WHERE TO_DAYS(NOW()) - TO_DAYS(success) <= 7");
76 $page->assign('nbInsMarkOK', $res->fetchOneCell());
79 function handler_private(&$page, $hruid = null
,
80 $action = null
, $value = null
)
83 $page->changeTpl('marketing/private.tpl');
85 $user = User
::getSilent($hruid);
90 // Retrieves marketed user details.
94 WHERE user_id = {?} AND perms = 'pending'", $user->id());
95 if (!($matricule = $res->fetchOneCell())) {
96 $page->kill("Cet utilisateur est déjà inscrit au site.");
99 require_once('user.func.inc.php');
100 $matricule = $res->fetchOneCell();
101 $matricule_X = get_X_mat($matricule);
103 $page->assign('full_name', $user->fullName());
104 $page->assign('promo', $user->promo());
105 $page->assign('matricule', $matricule);
106 $page->assign('matricule_X',$matricule_X);
108 // Applies in-parameter action to the user.
109 if ($action == 'del') {
110 S
::assert_xsrf_token();
111 Marketing
::clear($user->id(), $value);
114 if ($action == 'rel') {
115 $market = Marketing
::get($user->id(), $value);
116 if ($market == null
) {
117 $page->trigWarning("Aucun marketing n'a été effectué vers $value");
119 $to = $market->user
['to'];
120 $title = $market->getTitle();
121 $text = $market->getText();
122 $from = $market->sender_mail
;
123 $page->assign('rel_from_user', $from);
124 $page->assign('rel_from_staff',
125 '"Equipe Polytechnique.org" <register@' . $globals->mail
->domain
. '>');
126 $page->assign('rel_to', $to);
127 $page->assign('rel_title', $title);
128 $page->assign('rel_text', $text);
129 $page->assign('rel_email', $value);
133 if ($action == 'relforce') {
134 S
::assert_xsrf_token();
136 $market = Marketing
::get($user->id(), Post
::v('to'));
137 if (is_null($market)) {
138 $market = new Marketing($user->id(), Post
::v('to'), 'default', null
, 'staff');
140 $market->send(Post
::v('title'), Post
::v('message'));
141 $page->trigSuccess("Email envoyé");
144 if ($action == 'insrel') {
145 S
::assert_xsrf_token();
146 if (Marketing
::relance($user->id())) {
147 $page->trigSuccess('relance faite');
151 if ($action == 'add' && Post
::has('email') && Post
::has('type')) {
152 $market = new Marketing($user->id(), Post
::v('email'), 'default', null
, Post
::v('type'), S
::v('uid'));
156 // Retrieves and display the existing marketing attempts.
157 $res = XDB
::iterator(
159 FROM register_marketing AS r
160 LEFT JOIN aliases AS a ON (r.sender=a.id AND a.type = 'a_vie')
162 ORDER BY date", $user->id());
163 $page->assign('addr', $res);
165 $res = XDB
::query("SELECT date, relance FROM register_pending
166 WHERE uid = {?}", $user->id());
167 if (list($pending, $relance) = $res->fetchOneRow()) {
168 $page->assign('pending', $pending);
169 $page->assign('relance', $relance);
172 $page->assign('path', 'marketing/private/' . $user->login());
175 function handler_broken(&$page, $uid = null
)
177 $page->changeTpl('marketing/broken.tpl');
183 $user = User
::get($uid);
186 } elseif ($user->login() == S
::user()->login()) {
187 pl_redirect('emails/redirect');
191 "SELECT u.deces = '0000-00-00' AS alive, e.last,
192 IF(e.email IS NOT NULL, e.email, IF(FIND_IN_SET('googleapps', u.mail_storage), 'googleapps', NULL)) AS email
193 FROM auth_user_md5 AS u
194 LEFT JOIN emails AS e ON (e.flags = 'active' AND e.uid = u.user_id)
195 WHERE u.user_id = {?}
196 ORDER BY e.panne_level, e.last", $user->id());
197 if (!$res->numRows()) {
200 $user->addProperties($res->fetchOneAssoc());
201 $page->assign('user', $user);
204 require_once 'emails.inc.php';
205 if (Post
::has('mail')) {
206 $email = valide_email(Post
::v('mail'));
208 if (Post
::has('valide') && isvalid_email_redirection($email)) {
209 S
::assert_xsrf_token();
212 check_email($email, "Proposition d'une adresse surveillee pour " . $user->login() . " par " . S
::user()->login());
213 $res = XDB
::query("SELECT flags
215 WHERE email = {?} AND uid = {?}", $email, $user->id());
216 $state = $res->numRows() ?
$res->fetchOneCell() : null
;
217 if ($state == 'panne') {
218 $page->trigWarning("L'adresse que tu as fournie est l'adresse actuelle de {$user->fullName()} et est en panne.");
219 } elseif ($state == 'active') {
220 $page->trigWarning("L'adresse que tu as fournie est l'adresse actuelle de {$user->fullName()}");
221 } elseif ($user->email
&& !trim(Post
::v('comment'))) {
222 $page->trigError("Il faut que tu ajoutes un commentaire à ta proposition pour justifier le "
223 . "besoin de changer la redirection de {$user->fullName()}.");
225 require_once 'validations.inc.php';
226 $valid = new BrokenReq(S
::user(), $user, $email, trim(Post
::v('comment')));
228 $page->assign('sent', true
);
231 $page->trigError("L'adresse proposée n'est pas une adresse acceptable pour une redirection");
235 function handler_promo(&$page, $promo = null
)
237 $page->changeTpl('marketing/promo.tpl');
239 if (is_null($promo)) {
240 $promo = S
::v('promo');
242 $page->assign('promo', $promo);
244 $sql = "SELECT u.user_id, u.nom, u.prenom, u.last_known_email, u.matricule_ax,
245 IF(MAX(m.last) > p.relance, MAX(m.last), p.relance) AS dern_rel, p.email
246 FROM auth_user_md5 AS u
247 LEFT JOIN register_pending AS p ON p.uid = u.user_id
248 LEFT JOIN register_marketing AS m ON m.uid = u.user_id
249 WHERE u.promo = {?} AND u.deces = 0 AND u.perms='pending'
251 ORDER BY nom, prenom";
252 $page->assign('nonins', XDB
::iterator($sql, $promo));
255 function handler_public(&$page, $hruid = null
)
257 $page->changeTpl('marketing/public.tpl');
259 // Retrieves the user info, and checks the user is not yet registered.
260 $user = User
::getSilent($hruid);
268 WHERE user_id = {?} AND perms = 'pending'", $user->id());
269 if (!$res->fetchOneCell()) {
270 $page->kill("Cet utilisateur est déjà inscrit au site.");
273 // Displays the page, and handles the eventual user actions.
274 $page->assign('full_name', $user->fullName());
275 $page->assign('promo', $user->promo());
277 if (Post
::has('valide')) {
278 S
::assert_xsrf_token();
279 $email = trim(Post
::v('mail'));
281 require_once 'emails.inc.php';
282 if (!isvalid_email_redirection($email)) {
283 $page->trigError("Email invalide !");
285 // On cherche les marketings précédents sur cette adresse
286 // email, en se restreignant au dernier mois
288 if (Marketing
::get($user->id(), $email, true
)) {
289 $page->assign('already', true
);
291 $page->assign('ok', true
);
292 check_email($email, "Une adresse surveillée est proposée au marketing par " . S
::user()->login());
293 $market = new Marketing($user->id(), $email, 'default', null
, Post
::v('origine'), S
::v('uid'));
300 function handler_week(&$page, $sorting = 'per_promo')
302 $page->changeTpl('marketing/this_week.tpl');
304 $sort = $sorting == 'per_promo' ?
'promo' : 'date_ins';
306 $sql = "SELECT a.alias AS forlife, u.date_ins, u.promo, u.nom, u.prenom
307 FROM auth_user_md5 AS u
308 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
309 WHERE u.date_ins > ".date("Ymd000000", strtotime ('1 week ago'))."
310 ORDER BY u.$sort DESC";
311 $page->assign('ins', XDB
::iterator($sql));
314 function handler_volontaire(&$page, $promo = null
)
316 $page->changeTpl('marketing/volontaire.tpl');
321 FROM register_marketing AS m
322 INNER JOIN auth_user_md5 AS a ON a.user_id = m.uid
324 $page->assign('promos', $res->fetchColumn());
327 if (!is_null($promo)) {
328 $sql = "SELECT a.nom, a.prenom, a.user_id,
329 m.email, sa.alias AS forlife
330 FROM register_marketing AS m
331 INNER JOIN auth_user_md5 AS a ON a.user_id = m.uid AND a.promo = {?}
332 INNER JOIN aliases AS sa ON (m.sender = sa.id AND sa.type='a_vie')
334 $page->assign('addr', XDB
::iterator($sql, $promo));
338 function handler_relance(&$page)
340 $page->changeTpl('marketing/relance.tpl');
342 if (Post
::has('relancer')) {
343 $res = XDB
::query("SELECT COUNT(*) FROM auth_user_md5 WHERE deces=0");
344 $nbdix = $res->fetchOneCell();
347 foreach (array_keys($_POST['relance']) as $uid) {
348 if ($tmp = Marketing
::relance($uid, $nbdix)) {
349 $sent[] = $tmp.' a été relancé';
352 $page->assign('sent', $sent);
355 $sql = "SELECT r.date, r.relance, r.uid, u.promo, u.nom, u.prenom
356 FROM register_pending AS r
357 INNER JOIN auth_user_md5 AS u ON r. uid = u.user_id
358 WHERE hash!='INSCRIT'
360 $page->assign('relance', XDB
::iterator($sql));
364 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: