Happy New Year !
[platal.git] / include / validations / medals.inc.php
CommitLineData
20d7932b 1<?php
2/***************************************************************************
ba6ae046 3 * Copyright (C) 2003-2013 Polytechnique.org *
20d7932b 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
22// {{{ class MedalReq
23
c3d1e6b6 24class MedalReq extends ProfileValidate
20d7932b 25{
26 // {{{ properties
27
612a2d8a 28 public $mid;
29 public $gid;
15d26d45
SJ
30 public $level;
31 public $has_levels;
20d7932b 32
33 // }}}
34 // {{{ constructor
35
15d26d45 36 public function __construct(User $_user, Profile $_profile, $_idmedal, $_subidmedal, $_level, $has_levels, $_stamp = 0)
20d7932b 37 {
c3d1e6b6 38 parent::__construct($_user, $_profile, false, 'medal', $_stamp);
5daf68f6 39 $this->mid = $_idmedal;
20d7932b 40 $this->gid = $_subidmedal;
15d26d45
SJ
41 $this->level = $_level;
42 $this->has_levels = $has_levels;
a1ee34a4
FB
43 if (is_null($this->gid)) {
44 $this->gid = 0;
45 }
15d26d45
SJ
46 if (!$this->has_levels) {
47 $this->level = '';
48 }
20d7932b 49 }
50
51 // }}}
52 // {{{ function formu()
53
612a2d8a 54 public function formu()
787bb3d7 55 {
612a2d8a 56 return 'include/form.valid.medals.tpl';
57 }
20d7932b 58
59 // }}}
60 // {{{ function _mail_subj
61
612a2d8a 62 protected function _mail_subj()
20d7932b 63 {
c3d1e6b6 64 return '[Polytechnique.org/Décoration] Demande de décoration : ' . $this->medal_name();
20d7932b 65 }
66
67 // }}}
68 // {{{ function _mail_body
69
612a2d8a 70 protected function _mail_body($isok)
20d7932b 71 {
72 if ($isok) {
c3d1e6b6 73 return ' La décoration ' . $this->medal_name() . ' vient d\'être ajoutée à ta fiche.';
20d7932b 74 } else {
c3d1e6b6 75 return ' La demande que tu avais faite pour la décoration ' . $this->medal_name() . ' a été refusée.';
20d7932b 76 }
77 }
78
79 // }}}
80 // {{{ function medal_name
81
612a2d8a 82 public function medal_name()
20d7932b 83 {
9a87e760
SJ
84 $name = XDB::fetchOneCell('SELECT text
85 FROM profile_medal_enum
86 WHERE id = {?}', $this->mid);
87 $grade = XDB::fetchOneCell('SELECT text
88 FROM profile_medal_grade_enum
89 WHERE mid = {?} AND gid = {?}',
90 $this->mid, $this->gid);
91 if (is_null($grade)) {
92 return $name;
93 }
94 return $name . ' (' . $grade . ')';
20d7932b 95 }
96
97 // }}}
d02b8359 98 // {{{ function submit()
99
612a2d8a 100 public function submit()
d02b8359 101 {
010268b2 102 $res = XDB::query("SELECT FIND_IN_SET('validation', flags)
5c8a71f2 103 FROM profile_medal_enum
d02b8359 104 WHERE id = {?}", $this->mid);
105 if ($res->fetchOneCell()) {
106 parent::submit();
107 } else {
108 $this->commit();
109 }
110 }
612a2d8a 111
d02b8359 112 // }}}
20d7932b 113 // {{{ function commit()
114
612a2d8a 115 public function commit ()
20d7932b 116 {
15d26d45
SJ
117 return XDB::execute('INSERT INTO profile_medals (pid, mid, gid, level)
118 VALUES ({?}, {?}, {?}, {?})
00ba8a74 119 ON DUPLICATE KEY UPDATE gid = VALUES(gid)',
c3d1e6b6 120 $this->profile->id(), $this->mid,
15d26d45 121 is_null($this->gid) ? 0 : $this->gid, $this->level);
20d7932b 122 }
123
124 // }}}
85cc366b
FB
125 // {{{ function get_request($medal)
126
15d26d45 127 static public function get_request($pid, $type, $grade, $level)
85cc366b 128 {
c3d1e6b6 129 $reqs = parent::get_typed_requests($pid, 'medal');
85cc366b 130 foreach ($reqs as &$req) {
15d26d45 131 if ($req->mid == $type && $req->gid == $grade && $req->level == $level) {
85cc366b
FB
132 return $req;
133 }
134 }
135 return null;
136 }
137
138 // }}}
20d7932b 139}
140
141// }}}
142
a7de4ef7 143// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
20d7932b 144?>