Implements export() and fromExport() on UFOs.
[platal.git] / classes / userfilter / orders.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 // {{{ abstract class UserFilterOrder
23 /** Class providing factories for the UserFilterOrders.
24 */
25 abstract class UserFilterOrders
26 {
27 public static function fromExport(array $export)
28 {
29 $export = new PlDict($export);
30 if (!$export->has('type')) {
31 throw new Exception("Missing type in export");
32 }
33 $type = $export->s('type');
34 $desc = ($export->s('order') == 'desc');
35 switch ($type) {
36 case 'promo':
37 return new UFO_Promo($export->v('grade'), $desc);
38
39 case 'lastname':
40 case 'name':
41 case 'firstname':
42 case 'nickname':
43 case 'pseudonym':
44 return new UFO_Name($type, $export->v('variant'),
45 $export->b('particle'), $desc);
46
47 case 'score':
48 case 'registration':
49 case 'birthday':
50 case 'profile_update':
51 case 'death':
52 case 'uid':
53 case 'hruid':
54 case 'pid':
55 case 'hrpid':
56 $class = 'UFO_' . str_replace('_', '', $type);
57 return new $class($desc);
58
59 default:
60 throw new Exception("Unknown order field: $type");
61 }
62 }
63 }
64 // }}}
65 // {{{ class UFO_Promo
66 /** Orders users by promotion
67 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
68 * @param $desc Whether sort is descending
69 */
70 class UFO_Promo extends PlFilterGroupableOrder
71 {
72 private $grade;
73
74 public function __construct($grade = null, $desc = false)
75 {
76 parent::__construct($desc);
77 $this->grade = $grade;
78 }
79
80 protected function getSortTokens(PlFilter $uf)
81 {
82 if (UserFilter::isGrade($this->grade)) {
83 $sub = $uf->addEducationFilter($this->grade);
84 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
85 } else {
86 $sub = $uf->addDisplayFilter();
87 return 'pd' . $sub . '.promo';
88 }
89 }
90
91 public function export()
92 {
93 $export = $this->buildExport('promo');
94 if (!is_null($this->grade)) {
95 $export['grade'] = $this->grade;
96 }
97 return $export;
98 }
99 }
100 // }}}
101 // {{{ class UFO_Name
102 /** Sorts users by name
103 * @param $type Type of name on which to sort (firstname...)
104 * @param $variant Variant of that name to use (marital, ordinary...)
105 * @param $particle Set to true if particles should be included in the sorting order
106 * @param $desc If sort order should be descending
107 */
108 class UFO_Name extends PlFilterOrder
109 {
110 private $type;
111 private $variant;
112 private $particle;
113
114 public function __construct($type, $variant = null, $particle = false, $desc = false)
115 {
116 parent::__construct($desc);
117 $this->type = $type;
118 $this->variant = $variant;
119 $this->particle = $particle;
120 }
121
122 protected function getSortTokens(PlFilter $uf)
123 {
124 if (Profile::isDisplayName($this->type)) {
125 $sub = $uf->addDisplayFilter();
126 $token = 'pd' . $sub . '.' . $this->type;
127 if ($uf->accountsRequired()) {
128 $account_token = Profile::getAccountEquivalentName($this->type);
129 return 'IFNULL(' . $token . ', a.' . $account_token . ')';
130 } else {
131 return $token;
132 }
133 } else {
134 $sub = $uf->addNameFilter($this->type, $this->variant);
135 if ($this->particle) {
136 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
137 } else {
138 return 'pn' . $sub . '.name';
139 }
140 }
141 }
142
143 public function export()
144 {
145 $export = $this->buildExport($this->type);
146 if (!is_null($this->variant)) {
147 $export['variant'] = $this->variant;
148 }
149 if ($this->particle) {
150 $export['particle'] = true;
151 }
152 return $export;
153 }
154 }
155 // }}}
156 // {{{ class UFO_Score
157 class UFO_Score extends PlFilterOrder
158 {
159 protected function getSortTokens(PlFilter $uf)
160 {
161 $toks = $uf->getNameTokens();
162 $scores = array();
163
164 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
165 if (count($toks) == 0) {
166 return 'NULL';
167 }
168
169 foreach ($toks as $sub => $token) {
170 $scores[] = XDB::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
171 }
172 return implode(' + ', $scores);
173 }
174
175 public function export()
176 {
177 return $this->buildExport('score');
178 }
179 }
180 // }}}
181 // {{{ class UFO_Registration
182 /** Sorts users based on registration date
183 */
184 class UFO_Registration extends PlFilterOrder
185 {
186 protected function getSortTokens(PlFilter $uf)
187 {
188 $uf->requireAccounts();
189 return 'a.registration_date';
190 }
191
192 public function export()
193 {
194 return $this->buildExport('registration');
195 }
196 }
197 // }}}
198 // {{{ class UFO_Birthday
199 /** Sorts users based on next birthday date
200 */
201 class UFO_Birthday extends PlFilterOrder
202 {
203 protected function getSortTokens(PlFilter $uf)
204 {
205 $uf->requireProfiles();
206 return 'p.next_birthday';
207 }
208
209 public function export()
210 {
211 return $this->buildExport('birthday');
212 }
213 }
214 // }}}
215 // {{{ class UFO_ProfileUpdate
216 /** Sorts users based on last profile update
217 */
218 class UFO_ProfileUpdate extends PlFilterOrder
219 {
220 protected function getSortTokens(PlFilter $uf)
221 {
222 $uf->requireProfiles();
223 return 'p.last_change';
224 }
225
226 public function export()
227 {
228 return $this->buildExport('profile_update');
229 }
230 }
231 // }}}
232 // {{{ class UFO_Death
233 /** Sorts users based on death date
234 */
235 class UFO_Death extends PlFilterOrder
236 {
237 protected function getSortTokens(PlFilter $uf)
238 {
239 $uf->requireProfiles();
240 return 'p.deathdate';
241 }
242
243 public function export()
244 {
245 return $this->buildExport('death');
246 }
247 }
248 // }}}
249 // {{{ class UFO_Uid
250 /** Sorts users based on their uid
251 */
252 class UFO_Uid extends PlFilterOrder
253 {
254 protected function getSortTokens(PlFilter $uf)
255 {
256 $uf->requireAccounts();
257 return '$UID';
258 }
259
260 public function export()
261 {
262 return $this->buildExport('uid');
263 }
264 }
265 // }}}
266 // {{{ class UFO_Hruid
267 /** Sorts users based on their hruid
268 */
269 class UFO_Hruid extends PlFilterOrder
270 {
271 protected function getSortTokens(PlFilter $uf)
272 {
273 $uf->requireAccounts();
274 return 'a.hruid';
275 }
276
277 public function export()
278 {
279 return $this->buildExport('hruid');
280 }
281 }
282 // }}}
283 // {{{ class UFO_Pid
284 /** Sorts users based on their pid
285 */
286 class UFO_Pid extends PlFilterOrder
287 {
288 protected function getSortTokens(PlFilter $uf)
289 {
290 $uf->requireProfiles();
291 return '$PID';
292 }
293
294 public function export()
295 {
296 return $this->buildExport('pid');
297 }
298 }
299 // }}}
300 // {{{ class UFO_Hrpid
301 /** Sorts users based on their hrpid
302 */
303 class UFO_Hrpid extends PlFilterOrder
304 {
305 protected function getSortTokens(PlFilter $uf)
306 {
307 $uf->requireProfiles();
308 return 'p.hrpid';
309 }
310
311 public function export()
312 {
313 return $this->buildExport('hrpid');
314 }
315 }
316 // }}}
317
318 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
319 ?>