Adds the possibility to search on diplomas (closes #822), updates ChangeLog
[platal.git] / include / validations.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
0337d704 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
0337d704 22define('SIZE_MAX', 32768);
23
ce8ca505
FB
24global $globals;
25require_once $globals->spoolroot . '/core/classes/xdb.php';
2f151d5f 26
0337d704 27/**
28 * Iterator class, that lists objects through the database
29 */
30class ValidateIterator extends XOrgDBIterator
31{
32 // {{{ constuctor
d71befc4 33
612a2d8a 34 public function __construct ()
0337d704 35 {
96b00435 36 parent::__construct('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s") FROM requests ORDER BY stamp', MYSQL_NUM);
0337d704 37 }
38
39 // }}}
40 // {{{ function next()
41
612a2d8a 42 public function next ()
0337d704 43 {
44 if (list($result, $stamp) = parent::next()) {
45 $result = unserialize($result);
46 $result->stamp = $stamp;
47 return($result);
48 } else {
49 return null;
50 }
51 }
52
53 // }}}
54}
55
a7de4ef7 56/** classe "virtuelle" à dériver pour chaque nouvelle implémentation
0337d704 57 */
612a2d8a 58abstract class Validate
0337d704 59{
60 // {{{ properties
eaf30d86 61
612a2d8a 62 public $uid;
63 public $prenom;
64 public $nom;
65 public $promo;
66 public $sexe;
67 public $bestalias;
68 public $forlife;
69
70 public $stamp;
71 public $unique;
0337d704 72 // enable the refuse button
612a2d8a 73 public $refuse = true;
eaf30d86 74
612a2d8a 75 public $type;
76 public $comments = Array();
0337d704 77 // the validations rules : comments for admins
612a2d8a 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";
0337d704 79
80 // }}}
81 // {{{ constructor
eaf30d86 82
0337d704 83 /** constructeur
84 * @param $_uid user id
a7de4ef7 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
0337d704 87 */
612a2d8a 88 public function __construct($_uid, $_unique, $_type)
0337d704 89 {
0337d704 90 $this->uid = $_uid;
91 $this->stamp = date('YmdHis');
92 $this->unique = $_unique;
93 $this->type = $_type;
08cce2ff 94 $res = XDB::query(
69b7b6e9 95 "SELECT u.prenom, u.nom, u.promo, FIND_IN_SET('femme', u.flags) AS sexe, a.alias, b.alias
0337d704 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);
69b7b6e9 100 list($this->prenom, $this->nom, $this->promo, $this->sexe, $this->forlife, $this->bestalias) = $res->fetchOneRow();
0337d704 101 }
eaf30d86 102
0337d704 103 // }}}
104 // {{{ function submit()
105
a7de4ef7 106 /** fonction à utiliser pour envoyer les données à la modération
0337d704 107 * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai
108 */
612a2d8a 109 public function submit()
0337d704 110 {
0337d704 111 if ($this->unique) {
08cce2ff 112 XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', $this->uid, $this->type);
0337d704 113 }
eaf30d86 114
0337d704 115 $this->stamp = date('YmdHis');
08cce2ff 116 XDB::execute('INSERT INTO requests (user_id, type, data, stamp) VALUES ({?}, {?}, {?}, {?})',
0337d704 117 $this->uid, $this->type, $this, $this->stamp);
118
84868ee9 119 global $globals;
ebfdf077 120 $globals->updateNbValid();
0337d704 121 return true;
122 }
123
124 // }}}
125 // {{{ function update()
126
612a2d8a 127 protected function update()
0337d704 128 {
08cce2ff 129 XDB::execute('UPDATE requests SET data={?}, stamp=stamp
612a2d8a 130 WHERE user_id={?} AND type={?} AND stamp={?}',
131 $this, $this->uid, $this->type, $this->stamp);
0337d704 132 return true;
133 }
134
135 // }}}
136 // {{{ function clean()
eaf30d86 137
a7de4ef7 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
0337d704 140 */
d17761d8 141 public function clean()
0337d704 142 {
0337d704 143 if ($this->unique) {
84868ee9
FB
144 $success = XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}',
145 $this->uid, $this->type);
0337d704 146 } else {
84868ee9
FB
147 $success = XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}',
148 $this->uid, $this->type, $this->stamp);
0337d704 149 }
ebfdf077 150 $globals->updateNbValid();
84868ee9 151 return $success;
0337d704 152 }
153
154 // }}}
155 // {{{ function handle_formu()
eaf30d86 156
cb04af2c 157 /** fonction à réaliser en cas de validation du formulaire
0337d704 158 */
612a2d8a 159 public function handle_formu()
0337d704 160 {
161 if (Env::has('delete')) {
162 $this->clean();
a7d35093 163 $this->trigSuccess('Requête supprimée');
0337d704 164 return true;
165 }
166
a7de4ef7 167 // mise à jour des informations
6aa01fed 168 if (Env::has('edit')) {
169 if ($this->handle_editor()) {
170 $this->update();
a7d35093 171 $this->trigSuccess('Requête mise à jour');
6aa01fed 172 return true;
173 }
174 return false;
175 }
176
0337d704 177 // ajout d'un commentaire
178 if (Env::has('hold') && Env::has('comm')) {
4791ff77 179 $formid = Env::i('formid');
180 foreach ($this->comments as $comment) {
181 if ($comment[2] === $formid) {
182 return true;
183 }
184 }
90608d68 185 if (!strlen(trim(Env::v('comm')))) {
186 return true;
187 }
4791ff77 188 $this->comments[] = Array(S::v('bestalias'), Env::v('comm'), $formid);
0337d704 189
a7de4ef7 190 // envoi d'un mail à hotliners
0337d704 191 global $globals;
b9c53090 192 $mailer = new PlMailer();
0337d704 193 $mailer->setSubject("Commentaires de validation {$this->type}");
194 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
b9c53090 195 $mailer->addTo($globals->core->admin_email);
0337d704 196
197 $body = "Validation {$this->type} pour {$this->prenom} {$this->nom}\n\n"
eaf30d86 198 . S::v('bestalias')." a ajouté le commentaire :\n\n"
5e2307dc 199 . Env::v('comm')."\n\n"
8c7809b5 200 . "cf la discussion sur : ".$globals->baseurl."/admin/validate";
0337d704 201
202 $mailer->setTxtBody(wordwrap($body));
203 $mailer->send();
204
205 $this->update();
a7d35093 206 $this->trigSuccess('Commentaire ajouté');
0337d704 207 return true;
208 }
209
210 if (Env::has('accept')) {
211 if ($this->commit()) {
212 $this->sendmail(true);
213 $this->clean();
faefdbb7 214 $this->trigSuccess('Email de validation envoyé');
0337d704 215 return true;
216 } else {
a7d35093 217 $this->trigError('Erreur lors de la validation');
0337d704 218 return false;
219 }
220 }
221
222 if (Env::has('refuse')) {
5e2307dc 223 if (Env::v('comm')) {
0337d704 224 $this->sendmail(false);
225 $this->clean();
faefdbb7 226 $this->trigSuccess('Email de refus envoyé');
0337d704 227 return true;
228 } else {
a7d35093 229 $this->trigError('pas de motivation pour le refus !!!');
0337d704 230 }
231 }
232
233 return false;
234 }
235
236 // }}}
237 // {{{ function sendmail
238
612a2d8a 239 protected function sendmail($isok)
0337d704 240 {
241 global $globals;
1e33266a 242 $mailer = new PlMailer();
0337d704 243 $mailer->setSubject($this->_mail_subj());
244 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
245 $mailer->addTo("\"{$this->prenom} {$this->nom}\" <{$this->bestalias}@{$globals->mail->domain}>");
246 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
247
a7de4ef7 248 $body = ($this->sexe ? "Chère camarade,\n\n" : "Cher camarade,\n\n")
0337d704 249 . $this->_mail_body($isok)
5e2307dc 250 . (Env::has('comm') ? "\n\n".Env::v('comm') : '')
780bc68d 251 . "\n\nCordialement,\n\n-- \nL'équipe de Polytechnique.org\n";
0337d704 252
253 $mailer->setTxtBody(wordwrap($body));
254 $mailer->send();
255 }
256
257 // }}}
258 // {{{ function trig()
eaf30d86 259
a7d35093 260 protected function trigError($msg)
612a2d8a 261 {
d7610c35 262 Platal::page()->trigError($msg);
a7d35093
FB
263 }
264
265 protected function trigWarning($msg)
266 {
d7610c35 267 Platal::page()->trigWarning($msg);
a7d35093
FB
268 }
269
270 protected function trigSuccess($msg)
271 {
d7610c35 272 Platal::page()->trigSuccess($msg);
0337d704 273 }
eaf30d86 274
0337d704 275 // }}}
20d7932b 276 // {{{ function get_typed_request()
0337d704 277
a7de4ef7 278 /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t
279 * @param $uid l'id de l'utilisateur concerné
280 * @param $type le type de la requête
281 * @param $stamp le timestamp de la requête
0337d704 282 *
283 * XXX fonction "statique" XXX
a7de4ef7 284 * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_typed_request(...)
0337d704 285 */
612a2d8a 286 static public function get_typed_request($uid, $type, $stamp = -1)
0337d704 287 {
0337d704 288 if ($stamp == -1) {
08cce2ff 289 $res = XDB::query('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type);
0337d704 290 } else {
96b00435 291 $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);
0337d704 292 }
293 if ($result = $res->fetchOneCell()) {
294 $result = unserialize($result);
295 } else {
296 $result = false;
297 }
298 return($result);
299 }
300
301 // }}}
02838718 302 // {{{ function get_request_by_id()
303
304 static public function get_request_by_id($id)
305 {
306 list($uid, $type, $stamp) = explode('_', $id, 3);
307 return Validate::get_typed_request($uid, $type, $stamp);
308 }
309
310 // }}}
5b0dc389 311 // {{{ function get_typed_requests()
312
313 /** same as get_typed_request() but return an array of objects
314 */
612a2d8a 315 static public function get_typed_requests($uid, $type)
5b0dc389 316 {
317 $res = XDB::iterRow('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type);
318 $array = array();
319 while (list($data) = $res->next()) {
320 $array[] = unserialize($data);
321 }
322 return $array;
323 }
324
325 // }}}
bb0727ea
VZ
326 // {{{ function get_typed_requests_count()
327
328 /** same as get_typed_requests() but return the count of available requests.
329 */
330 static public function get_typed_requests_count($uid, $type)
331 {
332 $res = XDB::query('SELECT COUNT(data) FROM requests WHERE user_id={?} and type={?}', $uid, $type);
333 return $res->fetchOneCell();
334 }
335
336 // }}}
0337d704 337 // {{{ function _mail_body
338
612a2d8a 339 abstract protected function _mail_body($isok);
eaf30d86 340
0337d704 341 // }}}
342 // {{{ function _mail_subj
343
612a2d8a 344 abstract protected function _mail_subj();
eaf30d86 345
0337d704 346 // }}}
347 // {{{ function commit()
eaf30d86 348
a7de4ef7 349 /** fonction à utiliser pour insérer les données dans x4dat
0337d704 350 */
612a2d8a 351 abstract public function commit();
0337d704 352
353 // }}}
354 // {{{ function formu()
eaf30d86 355
0337d704 356 /** nom du template qui contient le formulaire */
612a2d8a 357 abstract public function formu();
0337d704 358
359 // }}}
6aa01fed 360 // {{{ function editor()
361
a7de4ef7 362 /** nom du formulaire d'édition */
612a2d8a 363 public function editor()
364 {
365 return null;
366 }
6aa01fed 367
368 // }}}
e18888f4 369 // {{{ function answers()
370
371 /** automatic answers table for this type of validation */
612a2d8a 372 public function answers()
373 {
e18888f4 374 static $answers_table;
375 if (!isset($answers_table[$this->type])) {
376 $r = XDB::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type);
377 $answers_table[$this->type] = $r->fetchAllAssoc($r);
378 }
379 return $answers_table[$this->type];
380 }
381
382 // }}}
a7de4ef7 383 // {{{ function id()
ed5b9703 384
612a2d8a 385 public function id()
ed5b9703 386 {
387 return $this->uid . '_' . $this->type . '_' . $this->stamp;
388 }
389
390 // }}}
fba760d2 391 // {{{ function ruleText()
392
393 public function ruleText()
394 {
395 return str_replace('\'', '\\\'', $this->rules);
396 }
397
398 // }}}
0337d704 399}
400
0337d704 401foreach (glob(dirname(__FILE__).'/validations/*.inc.php') as $file) {
402 require_once($file);
403}
404
a7de4ef7 405/* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker enc=utf-8: */
0337d704 406?>