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 define('SIZE_MAX', 32768);
25 require_once $globals->spoolroot
. '/core/classes/xdb.php';
28 * Iterator class, that lists objects through the database
30 class ValidateIterator
extends XOrgDBIterator
34 public function __construct ()
36 parent
::__construct('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s") FROM requests ORDER BY stamp', MYSQL_NUM
);
40 // {{{ function next()
42 public function next ()
44 if (list($result, $stamp) = parent
::next()) {
45 $result = unserialize($result);
46 $result->stamp
= $stamp;
56 /** classe "virtuelle" à dériver pour chaque nouvelle implémentation
58 abstract class Validate
72 // enable the refuse button
73 public $refuse = true
;
76 public $comments = Array();
77 // the validations rules : comments for admins
78 public $rules = "Mieux vaut laisser une demande de validation à un autre admin que de valider une requête illégale ou que de refuser une demande légitime";
84 * @param $_uid user id
85 * @param $_unique requête pouvant être multiple ou non
86 * @param $_type type de la donnée comme dans le champ type de x4dat.requests
88 public function __construct($_uid, $_unique, $_type)
91 $this->stamp
= date('YmdHis');
92 $this->unique
= $_unique;
95 "SELECT u.prenom, u.nom, u.promo, FIND_IN_SET('femme', u.flags) AS sexe, a.alias, b.alias
96 FROM auth_user_md5 AS u
97 INNER JOIN aliases AS a ON ( u.user_id=a.id AND a.type='a_vie' )
98 INNER JOIN aliases AS b ON ( u.user_id=b.id AND b.type!='homonyme' AND FIND_IN_SET('bestalias', b.flags) )
99 WHERE u.user_id={?}", $_uid);
100 list($this->prenom
, $this->nom
, $this->promo
, $this->sexe
, $this->forlife
, $this->bestalias
) = $res->fetchOneRow();
104 // {{{ function submit()
106 /** fonction à utiliser pour envoyer les données à la modération
107 * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai
109 public function submit()
112 XDB
::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', $this->uid
, $this->type
);
115 $this->stamp
= date('YmdHis');
116 XDB
::execute('INSERT INTO requests (user_id, type, data, stamp) VALUES ({?}, {?}, {?}, {?})',
117 $this->uid
, $this->type
, $this, $this->stamp
);
120 $globals->updateNbValid();
125 // {{{ function update()
127 protected function update()
129 XDB
::execute('UPDATE requests SET data={?}, stamp=stamp
130 WHERE user_id={?} AND type={?} AND stamp={?}',
131 $this, $this->uid
, $this->type
, $this->stamp
);
136 // {{{ function clean()
138 /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests
139 * attention, tout est supprimé si c'est un unique
141 public function clean()
146 $success = XDB
::execute('DELETE FROM requests WHERE user_id={?} AND type={?}',
147 $this->uid
, $this->type
);
149 $success = XDB
::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}',
150 $this->uid
, $this->type
, $this->stamp
);
152 $globals->updateNbValid();
157 // {{{ function handle_formu()
159 /** fonction à réaliser en cas de validation du formulaire
161 public function handle_formu()
163 if (Env
::has('delete')) {
165 $this->trigSuccess('Requête supprimée');
169 // mise à jour des informations
170 if (Env
::has('edit')) {
171 if ($this->handle_editor()) {
173 $this->trigSuccess('Requête mise à jour');
179 // ajout d'un commentaire
180 if (Env
::has('hold') && Env
::has('comm')) {
181 $formid = Env
::i('formid');
182 foreach ($this->comments
as $comment) {
183 if ($comment[2] === $formid) {
187 if (!strlen(trim(Env
::v('comm')))) {
190 $this->comments
[] = Array(S
::v('bestalias'), Env
::v('comm'), $formid);
192 // envoi d'un mail à hotliners
194 $mailer = new PlMailer();
195 $mailer->setSubject("Commentaires de validation {$this->type}");
196 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
197 $mailer->addTo($globals->core
->admin_email
);
199 $body = "Validation {$this->type} pour {$this->prenom} {$this->nom}\n\n"
200 . S
::v('bestalias')." a ajouté le commentaire :\n\n"
201 . Env
::v('comm')."\n\n"
202 . "cf la discussion sur : ".$globals->baseurl
."/admin/validate";
204 $mailer->setTxtBody(wordwrap($body));
208 $this->trigSuccess('Commentaire ajouté');
212 if (Env
::has('accept')) {
213 if ($this->commit()) {
214 $this->sendmail(true
);
216 $this->trigSuccess('Email de validation envoyé');
219 $this->trigError('Erreur lors de la validation');
224 if (Env
::has('refuse')) {
225 if (Env
::v('comm')) {
226 $this->sendmail(false
);
228 $this->trigSuccess('Email de refus envoyé');
231 $this->trigError('pas de motivation pour le refus !!!');
239 // {{{ function sendmail
241 protected function sendmail($isok)
244 $mailer = new PlMailer();
245 $mailer->setSubject($this->_mail_subj());
246 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
247 $mailer->addTo("\"{$this->prenom} {$this->nom}\" <{$this->bestalias}@{$globals->mail->domain}>");
248 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
250 $body = ($this->sexe ?
"Chère camarade,\n\n" : "Cher camarade,\n\n")
251 . $this->_mail_body($isok)
252 . (Env
::has('comm') ?
"\n\n".Env
::v('comm') : '')
253 . "\n\nCordialement,\n\n-- \nL'équipe de Polytechnique.org\n";
255 $mailer->setTxtBody(wordwrap($body));
260 // {{{ function trig()
262 protected function trigError($msg)
264 Platal
::page()->trigError($msg);
267 protected function trigWarning($msg)
269 Platal
::page()->trigWarning($msg);
272 protected function trigSuccess($msg)
274 Platal
::page()->trigSuccess($msg);
278 // {{{ function get_typed_request()
280 /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t
281 * @param $uid l'id de l'utilisateur concerné
282 * @param $type le type de la requête
283 * @param $stamp le timestamp de la requête
285 * XXX fonction "statique" XXX
286 * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_typed_request(...)
288 static public function get_typed_request($uid, $type, $stamp = -1)
291 $res = XDB
::query('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type);
293 $res = XDB
::query('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s") FROM requests WHERE user_id={?} AND type={?} and stamp={?}', $uid, $type, $stamp);
295 if ($result = $res->fetchOneCell()) {
296 $result = unserialize($result);
304 // {{{ function get_request_by_id()
306 static public function get_request_by_id($id)
308 list($uid, $type, $stamp) = explode('_', $id, 3);
309 return Validate
::get_typed_request($uid, $type, $stamp);
313 // {{{ function get_typed_requests()
315 /** same as get_typed_request() but return an array of objects
317 static public function get_typed_requests($uid, $type)
319 $res = XDB
::iterRow('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type);
321 while (list($data) = $res->next()) {
322 $array[] = unserialize($data);
328 // {{{ function get_typed_requests_count()
330 /** same as get_typed_requests() but return the count of available requests.
332 static public function get_typed_requests_count($uid, $type)
334 $res = XDB
::query('SELECT COUNT(data) FROM requests WHERE user_id={?} and type={?}', $uid, $type);
335 return $res->fetchOneCell();
339 // {{{ function _mail_body
341 abstract protected function _mail_body($isok);
344 // {{{ function _mail_subj
346 abstract protected function _mail_subj();
349 // {{{ function commit()
351 /** fonction à utiliser pour insérer les données dans x4dat
353 abstract public function commit();
356 // {{{ function formu()
358 /** nom du template qui contient le formulaire */
359 abstract public function formu();
362 // {{{ function editor()
364 /** nom du formulaire d'édition */
365 public function editor()
371 // {{{ function answers()
373 /** automatic answers table for this type of validation */
374 public function answers()
376 static $answers_table;
377 if (!isset($answers_table[$this->type
])) {
378 $r = XDB
::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type
);
379 $answers_table[$this->type
] = $r->fetchAllAssoc($r);
381 return $answers_table[$this->type
];
389 return $this->uid
. '_' . $this->type
. '_' . $this->stamp
;
393 // {{{ function ruleText()
395 public function ruleText()
397 return str_replace('\'', '\\\'', $this->rules
);
403 foreach (glob(dirname(__FILE__
).'/validations/*.inc.php') as $file) {
407 /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker enc=utf-8: */