fef4df2f294d589f0eebf20790bea61c64e22b0a
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 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 = Validate
::unserialize($result);
46 $result->stamp
= $stamp;
56 /** classe "virtuelle" à dériver pour chaque nouvelle implémentation
58 abstract class Validate
66 // enable the refuse button
67 public $refuse = true
;
70 public $comments = Array();
71 // the validations rules : comments for admins
72 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";
78 * @param $_user user object
79 * @param $_unique requête pouvant être multiple ou non
80 * @param $_type type de la donnée comme dans le champ type de x4dat.requests
82 public function __construct(User
&$_user, $_unique, $_type)
84 $this->user
= &$_user;
85 $this->stamp
= date('YmdHis');
86 $this->unique
= $_unique;
88 $res = XDB
::query("SELECT promo
90 WHERE pid={?}", $this->user
->id());
91 $this->promo
= $res->fetchOneCell();
95 // {{{ function submit()
97 /** fonction à utiliser pour envoyer les données à la modération
98 * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai
100 public function submit()
103 XDB
::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', $this->user
->id(), $this->type
);
106 $this->stamp
= date('YmdHis');
107 XDB
::execute('INSERT INTO requests (user_id, type, data, stamp) VALUES ({?}, {?}, {?}, {?})',
108 $this->user
->id(), $this->type
, $this, $this->stamp
);
111 $globals->updateNbValid();
116 // {{{ function update()
118 protected function update()
120 XDB
::execute('UPDATE requests SET data={?}, stamp=stamp
121 WHERE user_id={?} AND type={?} AND stamp={?}',
122 $this, $this->user
->id(), $this->type
, $this->stamp
);
127 // {{{ function clean()
129 /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests
130 * attention, tout est supprimé si c'est un unique
132 public function clean()
137 $success = XDB
::execute('DELETE FROM requests WHERE user_id={?} AND type={?}',
138 $this->user
->id(), $this->type
);
140 $success = XDB
::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}',
141 $this->user
->id(), $this->type
, $this->stamp
);
143 $globals->updateNbValid();
148 // {{{ function handle_formu()
150 /** fonction à réaliser en cas de validation du formulaire
152 public function handle_formu()
154 if (Env
::has('delete')) {
156 $this->trigSuccess('Requête supprimée');
160 // mise à jour des informations
161 if (Env
::has('edit')) {
162 if ($this->handle_editor()) {
164 $this->trigSuccess('Requête mise à jour');
170 // ajout d'un commentaire
171 if (Env
::has('hold') && Env
::has('comm')) {
172 $formid = Env
::i('formid');
173 foreach ($this->comments
as $comment) {
174 if ($comment[2] === $formid) {
178 if (!strlen(trim(Env
::v('comm')))) {
181 $this->comments
[] = Array(S
::user()->login(), Env
::v('comm'), $formid);
183 // envoi d'un mail à hotliners
185 $mailer = new PlMailer();
186 $mailer->setSubject("Commentaires de validation {$this->type}");
187 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
188 $mailer->addTo($globals->core
->admin_email
);
190 $body = "Validation {$this->type} pour {$this->user->login()}\n\n"
191 . S
::user()->login() . " a ajouté le commentaire :\n\n"
192 . Env
::v('comm') . "\n\n"
193 . "cf la discussion sur : " . $globals->baseurl
. "/admin/validate";
195 $mailer->setTxtBody(wordwrap($body));
199 $this->trigSuccess('Commentaire ajouté');
203 if (Env
::has('accept')) {
204 if ($this->commit()) {
205 $this->sendmail(true
);
207 $this->trigSuccess('Email de validation envoyé');
210 $this->trigError('Erreur lors de la validation');
215 if (Env
::has('refuse')) {
216 if (Env
::v('comm')) {
217 $this->sendmail(false
);
219 $this->trigSuccess('Email de refus envoyé');
222 $this->trigError('pas de motivation pour le refus !!!');
230 // {{{ function sendmail
232 protected function sendmail($isok)
235 $mailer = new PlMailer();
236 $mailer->setSubject($this->_mail_subj());
237 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
238 $mailer->addTo("\"{$this->user->fullName()}\" <{$this->user->bestEmail()}>");
239 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
241 $body = ($this->user
->isFemale() ?
"Chère camarade,\n\n" : "Cher camarade,\n\n")
242 . $this->_mail_body($isok)
243 . (Env
::has('comm') ?
"\n\n".Env
::v('comm') : '')
244 . "\n\nCordialement,\n\n-- \nL'équipe de Polytechnique.org\n";
246 $mailer->setTxtBody(wordwrap($body));
251 // {{{ function trig()
253 protected function trigError($msg)
255 Platal
::page()->trigError($msg);
258 protected function trigWarning($msg)
260 Platal
::page()->trigWarning($msg);
263 protected function trigSuccess($msg)
265 Platal
::page()->trigSuccess($msg);
269 // {{{ function get_typed_request()
271 /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t
272 * @param $uid l'id de l'utilisateur concerné
273 * @param $type le type de la requête
274 * @param $stamp le timestamp de la requête
276 * XXX fonction "statique" XXX
277 * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_typed_request(...)
279 static public function get_typed_request($uid, $type, $stamp = -1)
282 $res = XDB
::query('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type);
284 $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);
286 if ($result = $res->fetchOneCell()) {
287 $result = Validate
::unserialize($result);
295 // {{{ function get_request_by_id()
297 static public function get_request_by_id($id)
299 list($uid, $type, $stamp) = explode('_', $id, 3);
300 return Validate
::get_typed_request($uid, $type, $stamp);
304 // {{{ function get_typed_requests()
306 /** same as get_typed_request() but return an array of objects
308 static public function get_typed_requests($uid, $type)
310 $res = XDB
::iterRow('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type);
312 while (list($data) = $res->next()) {
313 $array[] = Validate
::unserialize($data);
319 // {{{ function get_typed_requests_count()
321 /** same as get_typed_requests() but return the count of available requests.
323 static public function get_typed_requests_count($uid, $type)
325 $res = XDB
::query('SELECT COUNT(data) FROM requests WHERE user_id={?} and type={?}', $uid, $type);
326 return $res->fetchOneCell();
330 // {{{ function _mail_body
332 abstract protected function _mail_body($isok);
335 // {{{ function _mail_subj
337 abstract protected function _mail_subj();
340 // {{{ function commit()
342 /** fonction à utiliser pour insérer les données dans x4dat
344 abstract public function commit();
347 // {{{ function formu()
349 /** nom du template qui contient le formulaire */
350 abstract public function formu();
353 // {{{ function editor()
355 /** nom du formulaire d'édition */
356 public function editor()
362 // {{{ function answers()
364 /** automatic answers table for this type of validation */
365 public function answers()
367 static $answers_table;
368 if (!isset($answers_table[$this->type
])) {
369 $r = XDB
::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type
);
370 $answers_table[$this->type
] = $r->fetchAllAssoc($r);
372 return $answers_table[$this->type
];
380 return $this->user
->id() . '_' . $this->type
. '_' . $this->stamp
;
384 // {{{ function ruleText()
386 public function ruleText()
388 return str_replace('\'', '\\\'', $this->rules
);
392 // {{{ function unserialize()
393 public static function unserialize($data)
395 $obj = unserialize($data);
396 /* XXX: Temporary for hruid migration */
397 if (!isset($obj->user
) ||
!is_object($obj)) {
398 $obj->user
=& User
::get($obj->forlife
);
400 /* XXX: End temporary block */
405 foreach (glob(dirname(__FILE__
).'/validations/*.inc.php') as $file) {
409 /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker enc=utf-8: */