2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 $this->promo
= $this->user
->promo();
92 // {{{ function submit()
94 /** fonction à utiliser pour envoyer les données à la modération
95 * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai
97 public function submit()
100 XDB
::execute('DELETE FROM requests
101 WHERE uid = {?} AND type = {?}',
102 $this->user
->id(), $this->type
);
105 $this->stamp
= date('YmdHis');
106 XDB
::execute('INSERT INTO requests (uid, type, data, stamp)
107 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
121 SET data = {?}, stamp = stamp
122 WHERE uid = {?} AND type = {?} AND stamp = {?}',
123 $this, $this->user
->id(), $this->type
, $this->stamp
);
128 // {{{ function clean()
130 /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests
131 * attention, tout est supprimé si c'est un unique
133 public function clean()
138 $success = XDB
::execute('DELETE FROM requests
139 WHERE uid = {?} AND type = {?}',
140 $this->user
->id(), $this->type
);
142 $success = XDB
::execute('DELETE FROM requests
143 WHERE uid = {?} AND type = {?} AND stamp = {?}',
144 $this->user
->id(), $this->type
, $this->stamp
);
146 $globals->updateNbValid();
151 // {{{ function handle_formu()
153 /** fonction à réaliser en cas de validation du formulaire
155 public function handle_formu()
157 if (Env
::has('delete')) {
159 $this->trigSuccess('Requête supprimée');
163 // mise à jour des informations
164 if (Env
::has('edit')) {
165 if ($this->handle_editor()) {
167 $this->trigSuccess('Requête mise à jour');
173 // ajout d'un commentaire
174 if (Env
::has('hold') && Env
::has('comm')) {
175 $formid = Env
::i('formid');
176 foreach ($this->comments
as $comment) {
177 if ($comment[2] === $formid) {
181 if (!strlen(trim(Env
::v('comm')))) {
184 $this->comments
[] = Array(S
::user()->login(), Env
::v('comm'), $formid);
186 // envoi d'un mail à hotliners
188 $mailer = new PlMailer();
189 $mailer->setSubject("Commentaires de validation {$this->type}");
190 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
191 $mailer->addTo($globals->core
->admin_email
);
193 $body = "Validation {$this->type} pour {$this->user->login()}\n\n"
194 . S
::user()->login() . " a ajouté le commentaire :\n\n"
195 . Env
::v('comm') . "\n\n"
196 . "cf la discussion sur : " . $globals->baseurl
. "/admin/validate";
198 $mailer->setTxtBody(wordwrap($body));
202 $this->trigSuccess('Commentaire ajouté');
206 if (Env
::has('accept')) {
207 if ($this->commit()) {
208 $this->sendmail(true
);
210 $this->trigSuccess('Email de validation envoyé');
213 $this->trigError('Erreur lors de la validation');
218 if (Env
::has('refuse')) {
219 if (Env
::v('comm')) {
220 $this->sendmail(false
);
222 $this->trigSuccess('Email de refus envoyé');
225 $this->trigError('Pas de motivation pour le refus !!!');
233 // {{{ function sendmail
235 protected function sendmail($isok)
238 $mailer = new PlMailer();
239 $mailer->setSubject($this->_mail_subj());
240 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
241 $mailer->addTo("\"{$this->user->fullName()}\" <{$this->user->bestEmail()}>");
242 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
244 $body = ($this->user
->isFemale() ?
"Chère camarade,\n\n" : "Cher camarade,\n\n")
245 . $this->_mail_body($isok)
246 . (Env
::has('comm') ?
"\n\n" . Env
::v('comm') : '')
247 . "\n\nCordialement,\n-- \nL'équipe de Polytechnique.org\n"
248 . $this->_mail_ps($isok);
250 $mailer->setTxtBody(wordwrap($body));
255 // {{{ function trig()
257 protected function trigError($msg)
259 Platal
::page()->trigError($msg);
262 protected function trigWarning($msg)
264 Platal
::page()->trigWarning($msg);
267 protected function trigSuccess($msg)
269 Platal
::page()->trigSuccess($msg);
273 // {{{ function get_typed_request()
275 /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t
276 * @param $uid l'id de l'utilisateur concerné
277 * @param $type le type de la requête
278 * @param $stamp le timestamp de la requête
280 * XXX fonction "statique" XXX
281 * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_typed_request(...)
283 static public function get_typed_request($uid, $type, $stamp = -1)
286 $res = XDB
::query('SELECT data
288 WHERE uid = {?} and type = {?}',
291 $res = XDB
::query('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
293 WHERE uid = {?} AND type = {?} and stamp = {?}',
294 $uid, $type, $stamp);
296 if ($result = $res->fetchOneCell()) {
297 $result = Validate
::unserialize($result);
305 // {{{ function get_request_by_id()
307 static public function get_request_by_id($id)
309 list($uid, $type, $stamp) = explode('_', $id, 3);
310 return Validate
::get_typed_request($uid, $type, $stamp);
314 // {{{ function get_typed_requests()
316 /** same as get_typed_request() but return an array of objects
318 static public function get_typed_requests($uid, $type)
320 $res = XDB
::iterRow('SELECT data
322 WHERE uid = {?} and type = {?}',
325 while (list($data) = $res->next()) {
326 $array[] = Validate
::unserialize($data);
332 // {{{ function get_typed_requests_count()
334 /** same as get_typed_requests() but return the count of available requests.
336 static public function get_typed_requests_count($uid, $type)
338 $res = XDB
::query('SELECT COUNT(data)
340 WHERE uid = {?} and type = {?}',
342 return $res->fetchOneCell();
346 // {{{ function _mail_body
348 abstract protected function _mail_body($isok);
351 // {{{ function _mail_subj
353 abstract protected function _mail_subj();
356 // {{{ function _mail_ps
358 protected function _mail_ps($isok)
364 // {{{ function commit()
366 /** fonction à utiliser pour insérer les données dans x4dat
368 abstract public function commit();
371 // {{{ function formu()
373 /** nom du template qui contient le formulaire */
374 abstract public function formu();
377 // {{{ function editor()
379 /** nom du formulaire d'édition */
380 public function editor()
386 // {{{ function answers()
388 /** automatic answers table for this type of validation */
389 public function answers()
391 static $answers_table;
392 if (!isset($answers_table[$this->type
])) {
393 $r = XDB
::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type
);
394 $answers_table[$this->type
] = $r->fetchAllAssoc();
396 return $answers_table[$this->type
];
404 return $this->user
->id() . '_' . $this->type
. '_' . $this->stamp
;
408 // {{{ function ruleText()
410 public function ruleText()
412 return str_replace('\'', '\\\'', $this->rules
);
416 // {{{ function unserialize()
417 public static function unserialize($data)
419 $obj = unserialize($data);
420 /* XXX: Temporary for hruid migration */
421 if (!isset($obj->user
) ||
!is_object($obj)) {
422 $obj->user
=& User
::get($obj->forlife
);
424 /* XXX: End temporary block */
429 foreach (glob(dirname(__FILE__
).'/validations/*.inc.php') as $file) {
433 /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker enc=utf-8: */