Adds levels to medals (Closes #1381).
[platal.git] / modules / profile / decos.inc.php
CommitLineData
a7c28fff
FB
1<?php
2/***************************************************************************
12262f13 3 * Copyright (C) 2003-2011 Polytechnique.org *
a7c28fff
FB
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
12bcf04b 22class ProfileSettingDeco implements ProfileSetting
a7c28fff 23{
e18807a8
SJ
24 private static function compareMedals(array $a, array $b)
25 {
26 if ($a['id'] == $b['id']) {
15d26d45
SJ
27 if ($a['grade'] == $b['grade']) {
28 return $a['level'] > $b['level'];
29 }
e18807a8
SJ
30 return $a['grade'] > $b['grade'];
31 }
32 return $a['id'] > $b['id'];
33 }
34
26ba053e 35 public function value(ProfilePage $page, $field, $value, &$success)
a7c28fff
FB
36 {
37 $success = true;
38 if (is_null($value)) {
39 // Fetch already attributed medals
15d26d45
SJ
40 $value = XDB::fetchAllAssoc("SELECT m.mid AS id, m.gid AS grade, 1 AS valid, FIND_IN_SET('has_levels', e.flags) AS has_levels, m.level
41 FROM profile_medals AS m
42 INNER JOIN profile_medal_enum AS e ON (m.mid = e.id)
43 WHERE m.pid = {?}",
e18807a8 44 $page->pid());
a7c28fff
FB
45
46 // Fetch not yet validated medals
024ec1e5 47 $medals = ProfileValidate::get_typed_requests($page->pid(), 'medal');
a7c28fff 48 foreach ($medals as &$medal) {
e18807a8 49 $value[] = array(
15d26d45
SJ
50 'id' => $medal->mid,
51 'grade' => $medal->gid,
52 'level' => $medal->level,
53 'has_levels' => $medal->has_levels,
54 'valid' => '0'
e18807a8 55 );
a7c28fff 56 }
e18807a8 57 } elseif (!is_array($value)) {
a7c28fff
FB
58 $value = array();
59 }
e18807a8 60 usort($value, 'self::compareMedals');
a7c28fff
FB
61 return $value;
62 }
63
26ba053e 64 public function save(ProfilePage $page, $field, $value)
a7c28fff 65 {
e18807a8
SJ
66 $original =& $page->orig[$field];
67
68 $i = $j = 0;
69 $total_original = count($original);
70 $total_value = count($value);
85cc366b 71
e18807a8
SJ
72 while ($i < $total_original || $j < $total_value) {
73 if (isset($value[$j]) && (!isset($original[$i]) || self::compareMedals($original[$i], $value[$j]))) {
15d26d45 74 $req = new MedalReq(S::user(), $page->profile, $value[$j]['id'], $value[$j]['grade'], $value[$j]['level'], $value[$j]['has_levels']);
e18807a8
SJ
75 $req->submit();
76 sleep(1);
77 ++$j;
78 } elseif (isset($original[$i]) && (!isset($value[$j]) || self::compareMedals($value[$j], $original[$i]))) {
79 if ($original[$i]['valid']) {
80 XDB::execute('DELETE FROM profile_medals
81 WHERE pid = {?} AND mid = {?} AND gid = {?}',
82 $page->pid(), $original[$i]['id'], $original[$i]['grade']);
a7c28fff 83 } else {
15d26d45 84 $req = MedalReq::get_request($page->pid(), $original[$i]['id'], $original[$i]['grade'], $value[$j]['level']);
85cc366b
FB
85 if ($req) {
86 $req->clean();
87 }
a7c28fff 88 }
e18807a8
SJ
89 ++$i;
90 } else {
91 ++$i;
92 ++$j;
a7c28fff
FB
93 }
94 }
95 }
a0fce0c6
SJ
96
97 public function getText($value) {
98 $medalsList = DirEnum::getOptions(DirEnum::MEDALS);
99 $medals = array();
100 foreach ($value as $id => $medal) {
101 $medals[] = $medalsList[$id];
102 }
103 return implode(', ', $medals);
104 }
a7c28fff
FB
105}
106
66c4bdaf 107class ProfilePageDecos extends ProfilePage
a7c28fff
FB
108{
109 protected $pg_template = 'profile/deco.tpl';
110
26ba053e 111 public function __construct(PlWizard $wiz)
a7c28fff
FB
112 {
113 parent::__construct($wiz);
12bcf04b
RB
114 $this->settings['medals'] = new ProfileSettingDeco();
115 $this->settings['medals_pub'] = new ProfileSettingPub();
a2a1c2f2 116 $this->watched['medals'] = true;
a7c28fff
FB
117 }
118
7c2e0f0d 119 protected function _fetchData()
a7c28fff 120 {
e5bcd851
FB
121 $res = XDB::query("SELECT medals_pub
122 FROM profiles
123 WHERE pid = {?}",
124 $this->pid());
a7c28fff 125 $this->values['medals_pub'] = $res->fetchOneCell();
a7c28fff
FB
126 }
127
7c2e0f0d 128 protected function _saveData()
a7c28fff 129 {
a7c28fff 130 if ($this->changed['medals_pub']) {
e5bcd851
FB
131 XDB::execute("UPDATE profiles
132 SET medals_pub = {?}
133 WHERE pid = {?}",
134 $this->values['medals_pub'], $this->pid());
a7c28fff
FB
135 }
136 }
137
26ba053e 138 public function _prepare(PlPage $page, $id)
a7c28fff 139 {
cfcb8b62
SJ
140 $res = XDB::iterator('SELECT *, FIND_IN_SET(\'validation\', flags) AS validate
141 FROM profile_medal_enum
142 ORDER BY type, text');
143 $mlist = array();
a7c28fff
FB
144 while ($tmp = $res->next()) {
145 $mlist[$tmp['type']][] = $tmp;
146 }
147 $page->assign('medal_list', $mlist);
cfcb8b62
SJ
148 $fullType = array(
149 'ordre' => 'Ordres',
150 'croix' => 'Croix',
151 'militaire' => 'Médailles militaires',
152 'honneur' => 'Médailles d\'honneur',
153 'resistance' => 'Médailles de la résistance',
154 'prix' => 'Prix',
155 'sport' => 'Médailles sportives'
156 );
157 $page->assign('fullType', $fullType);
a7c28fff
FB
158 }
159}
160
161// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
162?>