Fixes deletion of first education (Closes #1095).
[platal.git] / include / validations.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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()) {
33b47675 45 $result = Validate::unserialize($result);
0337d704 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
5daf68f6 62 public $user;
612a2d8a 63
64 public $stamp;
65 public $unique;
0337d704 66 // enable the refuse button
612a2d8a 67 public $refuse = true;
eaf30d86 68
612a2d8a 69 public $type;
70 public $comments = Array();
0337d704 71 // the validations rules : comments for admins
612a2d8a 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";
0337d704 73
74 // }}}
75 // {{{ constructor
eaf30d86 76
0337d704 77 /** constructeur
5daf68f6 78 * @param $_user user object
a7de4ef7 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
0337d704 81 */
532c06cf 82 public function __construct(User &$_user, $_unique, $_type)
0337d704 83 {
532c06cf 84 $this->user = &$_user;
0337d704 85 $this->stamp = date('YmdHis');
86 $this->unique = $_unique;
87 $this->type = $_type;
8f2104cb 88 $this->promo = $this->user->promo();
0337d704 89 }
eaf30d86 90
0337d704 91 // }}}
92 // {{{ function submit()
93
a7de4ef7 94 /** fonction à utiliser pour envoyer les données à la modération
0337d704 95 * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai
96 */
612a2d8a 97 public function submit()
0337d704 98 {
0337d704 99 if ($this->unique) {
1bf36cd1
SJ
100 XDB::execute('DELETE FROM requests
101 WHERE uid = {?} AND type = {?}',
102 $this->user->id(), $this->type);
0337d704 103 }
eaf30d86 104
0337d704 105 $this->stamp = date('YmdHis');
1bf36cd1
SJ
106 XDB::execute('INSERT INTO requests (uid, type, data, stamp)
107 VALUES ({?}, {?}, {?}, {?})',
5daf68f6 108 $this->user->id(), $this->type, $this, $this->stamp);
0337d704 109
84868ee9 110 global $globals;
ebfdf077 111 $globals->updateNbValid();
0337d704 112 return true;
113 }
114
115 // }}}
116 // {{{ function update()
117
612a2d8a 118 protected function update()
0337d704 119 {
1bf36cd1
SJ
120 XDB::execute('UPDATE requests
121 SET data = {?}, stamp = stamp
122 WHERE uid = {?} AND type = {?} AND stamp = {?}',
5daf68f6 123 $this, $this->user->id(), $this->type, $this->stamp);
0337d704 124 return true;
125 }
126
127 // }}}
128 // {{{ function clean()
eaf30d86 129
a7de4ef7 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
0337d704 132 */
d17761d8 133 public function clean()
0337d704 134 {
95e36b0f
SJ
135 global $globals;
136
0337d704 137 if ($this->unique) {
1bf36cd1
SJ
138 $success = XDB::execute('DELETE FROM requests
139 WHERE uid = {?} AND type = {?}',
5daf68f6 140 $this->user->id(), $this->type);
0337d704 141 } else {
1bf36cd1
SJ
142 $success = XDB::execute('DELETE FROM requests
143 WHERE uid = {?} AND type = {?} AND stamp = {?}',
5daf68f6 144 $this->user->id(), $this->type, $this->stamp);
0337d704 145 }
ebfdf077 146 $globals->updateNbValid();
84868ee9 147 return $success;
0337d704 148 }
149
150 // }}}
151 // {{{ function handle_formu()
eaf30d86 152
cb04af2c 153 /** fonction à réaliser en cas de validation du formulaire
0337d704 154 */
612a2d8a 155 public function handle_formu()
0337d704 156 {
157 if (Env::has('delete')) {
158 $this->clean();
a7d35093 159 $this->trigSuccess('Requête supprimée');
0337d704 160 return true;
161 }
162
a7de4ef7 163 // mise à jour des informations
6aa01fed 164 if (Env::has('edit')) {
165 if ($this->handle_editor()) {
166 $this->update();
a7d35093 167 $this->trigSuccess('Requête mise à jour');
6aa01fed 168 return true;
169 }
170 return false;
171 }
172
0337d704 173 // ajout d'un commentaire
174 if (Env::has('hold') && Env::has('comm')) {
4791ff77 175 $formid = Env::i('formid');
176 foreach ($this->comments as $comment) {
177 if ($comment[2] === $formid) {
178 return true;
179 }
180 }
90608d68 181 if (!strlen(trim(Env::v('comm')))) {
182 return true;
183 }
5daf68f6 184 $this->comments[] = Array(S::user()->login(), Env::v('comm'), $formid);
0337d704 185
a7de4ef7 186 // envoi d'un mail à hotliners
0337d704 187 global $globals;
b9c53090 188 $mailer = new PlMailer();
0337d704 189 $mailer->setSubject("Commentaires de validation {$this->type}");
190 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
b9c53090 191 $mailer->addTo($globals->core->admin_email);
0337d704 192
53092def 193 $body = "Validation {$this->type} pour {$this->user->login()}\n\n"
5daf68f6
VZ
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";
0337d704 197
198 $mailer->setTxtBody(wordwrap($body));
199 $mailer->send();
200
201 $this->update();
a7d35093 202 $this->trigSuccess('Commentaire ajouté');
0337d704 203 return true;
204 }
205
206 if (Env::has('accept')) {
207 if ($this->commit()) {
208 $this->sendmail(true);
209 $this->clean();
faefdbb7 210 $this->trigSuccess('Email de validation envoyé');
0337d704 211 return true;
212 } else {
a7d35093 213 $this->trigError('Erreur lors de la validation');
0337d704 214 return false;
215 }
216 }
217
218 if (Env::has('refuse')) {
5e2307dc 219 if (Env::v('comm')) {
0337d704 220 $this->sendmail(false);
221 $this->clean();
faefdbb7 222 $this->trigSuccess('Email de refus envoyé');
0337d704 223 return true;
224 } else {
6bb2f79a 225 $this->trigError('Pas de motivation pour le refus&nbsp;!!!');
0337d704 226 }
227 }
228
229 return false;
230 }
231
232 // }}}
233 // {{{ function sendmail
234
612a2d8a 235 protected function sendmail($isok)
0337d704 236 {
237 global $globals;
1e33266a 238 $mailer = new PlMailer();
0337d704 239 $mailer->setSubject($this->_mail_subj());
240 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
5daf68f6 241 $mailer->addTo("\"{$this->user->fullName()}\" <{$this->user->bestEmail()}>");
0337d704 242 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
243
5daf68f6 244 $body = ($this->user->isFemale() ? "Chère camarade,\n\n" : "Cher camarade,\n\n")
0337d704 245 . $this->_mail_body($isok)
9be4a981 246 . (Env::has('comm') ? "\n\n" . Env::v('comm') : '')
5353e51e
SJ
247 . "\n\nCordialement,\n-- \nL'équipe de Polytechnique.org\n"
248 . $this->_mail_ps($isok);
0337d704 249
250 $mailer->setTxtBody(wordwrap($body));
251 $mailer->send();
252 }
253
254 // }}}
255 // {{{ function trig()
eaf30d86 256
a7d35093 257 protected function trigError($msg)
612a2d8a 258 {
d7610c35 259 Platal::page()->trigError($msg);
a7d35093
FB
260 }
261
262 protected function trigWarning($msg)
263 {
d7610c35 264 Platal::page()->trigWarning($msg);
a7d35093
FB
265 }
266
267 protected function trigSuccess($msg)
268 {
d7610c35 269 Platal::page()->trigSuccess($msg);
0337d704 270 }
eaf30d86 271
0337d704 272 // }}}
20d7932b 273 // {{{ function get_typed_request()
0337d704 274
a7de4ef7 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
0337d704 279 *
280 * XXX fonction "statique" XXX
a7de4ef7 281 * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_typed_request(...)
0337d704 282 */
612a2d8a 283 static public function get_typed_request($uid, $type, $stamp = -1)
0337d704 284 {
0337d704 285 if ($stamp == -1) {
1bf36cd1
SJ
286 $res = XDB::query('SELECT data
287 FROM requests
288 WHERE uid = {?} and type = {?}',
289 $uid, $type);
0337d704 290 } else {
1bf36cd1
SJ
291 $res = XDB::query('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
292 FROM requests
293 WHERE uid = {?} AND type = {?} and stamp = {?}',
294 $uid, $type, $stamp);
0337d704 295 }
296 if ($result = $res->fetchOneCell()) {
33b47675 297 $result = Validate::unserialize($result);
0337d704 298 } else {
299 $result = false;
300 }
301 return($result);
302 }
303
304 // }}}
02838718 305 // {{{ function get_request_by_id()
306
307 static public function get_request_by_id($id)
308 {
309 list($uid, $type, $stamp) = explode('_', $id, 3);
310 return Validate::get_typed_request($uid, $type, $stamp);
311 }
312
313 // }}}
5b0dc389 314 // {{{ function get_typed_requests()
315
316 /** same as get_typed_request() but return an array of objects
317 */
612a2d8a 318 static public function get_typed_requests($uid, $type)
5b0dc389 319 {
1bf36cd1
SJ
320 $res = XDB::iterRow('SELECT data
321 FROM requests
322 WHERE uid = {?} and type = {?}',
323 $uid, $type);
5b0dc389 324 $array = array();
325 while (list($data) = $res->next()) {
33b47675 326 $array[] = Validate::unserialize($data);
5b0dc389 327 }
328 return $array;
329 }
330
331 // }}}
bb0727ea
VZ
332 // {{{ function get_typed_requests_count()
333
334 /** same as get_typed_requests() but return the count of available requests.
335 */
336 static public function get_typed_requests_count($uid, $type)
337 {
1bf36cd1
SJ
338 $res = XDB::query('SELECT COUNT(data)
339 FROM requests
340 WHERE uid = {?} and type = {?}',
341 $uid, $type);
bb0727ea
VZ
342 return $res->fetchOneCell();
343 }
344
345 // }}}
0337d704 346 // {{{ function _mail_body
347
612a2d8a 348 abstract protected function _mail_body($isok);
eaf30d86 349
0337d704 350 // }}}
351 // {{{ function _mail_subj
352
612a2d8a 353 abstract protected function _mail_subj();
eaf30d86 354
0337d704 355 // }}}
9be4a981
SJ
356 // {{{ function _mail_ps
357
358 protected function _mail_ps($isok)
359 {
5353e51e 360 return '';
9be4a981
SJ
361 }
362
363 // }}}
0337d704 364 // {{{ function commit()
eaf30d86 365
a7de4ef7 366 /** fonction à utiliser pour insérer les données dans x4dat
0337d704 367 */
612a2d8a 368 abstract public function commit();
0337d704 369
370 // }}}
371 // {{{ function formu()
eaf30d86 372
0337d704 373 /** nom du template qui contient le formulaire */
612a2d8a 374 abstract public function formu();
0337d704 375
376 // }}}
6aa01fed 377 // {{{ function editor()
378
a7de4ef7 379 /** nom du formulaire d'édition */
612a2d8a 380 public function editor()
381 {
382 return null;
383 }
6aa01fed 384
385 // }}}
e18888f4 386 // {{{ function answers()
387
388 /** automatic answers table for this type of validation */
612a2d8a 389 public function answers()
390 {
e18888f4 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);
745539c0 394 $answers_table[$this->type] = $r->fetchAllAssoc();
e18888f4 395 }
396 return $answers_table[$this->type];
397 }
398
399 // }}}
a7de4ef7 400 // {{{ function id()
ed5b9703 401
612a2d8a 402 public function id()
ed5b9703 403 {
5daf68f6 404 return $this->user->id() . '_' . $this->type . '_' . $this->stamp;
ed5b9703 405 }
406
407 // }}}
fba760d2 408 // {{{ function ruleText()
409
410 public function ruleText()
411 {
412 return str_replace('\'', '\\\'', $this->rules);
413 }
414
415 // }}}
33b47675
FB
416 // {{{ function unserialize()
417 public static function unserialize($data)
418 {
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);
423 }
424 /* XXX: End temporary block */
425 return $obj;
426 }
0337d704 427}
428
0337d704 429foreach (glob(dirname(__FILE__).'/validations/*.inc.php') as $file) {
430 require_once($file);
431}
432
a7de4ef7 433/* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker enc=utf-8: */
0337d704 434?>