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