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