Update the UFO_Promo and UFO_Name to work cleanly with the new MultiPageView grouping...
[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 getCondition($promo)
93 {
94 return new UFC_Promo(UserFilterCondition::OP_EQUALS, $this->grade, $promo);
95 }
96
97 public function export()
98 {
99 $export = $this->buildExport('promo');
100 if (!is_null($this->grade)) {
101 $export['grade'] = $this->grade;
102 }
103 return $export;
104 }
105 }
106 // }}}
107 // {{{ class UFO_Name
108 /** Sorts users by name
109 * @param $type Type of name on which to sort (firstname...)
110 * @param $variant Variant of that name to use (marital, ordinary...)
111 * @param $particle Set to true if particles should be included in the sorting order
112 * @param $desc If sort order should be descending
113 */
114 class UFO_Name extends PlFilterGroupableOrder
115 {
116 private $type;
117 private $variant;
118 private $particle;
119
120 public function __construct($type, $variant = null, $particle = false, $desc = false)
121 {
122 parent::__construct($desc);
123 $this->type = $type;
124 $this->variant = $variant;
125 $this->particle = $particle;
126 }
127
128 protected function getSortTokens(PlFilter $uf)
129 {
130 if (Profile::isDisplayName($this->type)) {
131 $sub = $uf->addDisplayFilter();
132 $token = 'pd' . $sub . '.' . $this->type;
133 if ($uf->accountsRequired()) {
134 $account_token = Profile::getAccountEquivalentName($this->type);
135 return 'IFNULL(' . $token . ', a.' . $account_token . ')';
136 } else {
137 return $token;
138 }
139 } else {
140 $sub = $uf->addNameFilter($this->type, $this->variant);
141 if ($this->particle) {
142 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
143 } else {
144 return 'pn' . $sub . '.name';
145 }
146 }
147 }
148
149 public function getGroupToken(PlFilter $pf)
150 {
151 return 'SUBSTRING(' . $this->_tokens . ', 1, 1)';
152 }
153
154 public function getCondition($initial)
155 {
156 if (Profile::isDisplayName($this->type)) {
157 switch ($this->type) {
158 case Profile::DN_PRIVATE:
159 case Profile::DN_SHORT:
160 case Profile::DN_YOURSELF:
161 $type = Profile::FIRSTNAME;
162 default:
163 $type = Profile::LASTNAME;
164 }
165 } else {
166 $type = $this->type;
167 }
168 return new UFC_Name($type, $initial, UFC_Name::PREFIX);
169 }
170
171 public function export()
172 {
173 $export = $this->buildExport($this->type);
174 if (!is_null($this->variant)) {
175 $export['variant'] = $this->variant;
176 }
177 if ($this->particle) {
178 $export['particle'] = true;
179 }
180 return $export;
181 }
182 }
183 // }}}
184 // {{{ class UFO_Score
185 class UFO_Score extends PlFilterOrder
186 {
187 protected function getSortTokens(PlFilter $uf)
188 {
189 $toks = $uf->getNameTokens();
190 $scores = array();
191
192 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
193 if (count($toks) == 0) {
194 return 'NULL';
195 }
196
197 foreach ($toks as $sub => $token) {
198 $scores[] = XDB::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
199 }
200 return implode(' + ', $scores);
201 }
202
203 public function export()
204 {
205 return $this->buildExport('score');
206 }
207 }
208 // }}}
209 // {{{ class UFO_Registration
210 /** Sorts users based on registration date
211 */
212 class UFO_Registration extends PlFilterOrder
213 {
214 protected function getSortTokens(PlFilter $uf)
215 {
216 $uf->requireAccounts();
217 return 'a.registration_date';
218 }
219
220 public function export()
221 {
222 return $this->buildExport('registration');
223 }
224 }
225 // }}}
226 // {{{ class UFO_Birthday
227 /** Sorts users based on next birthday date
228 */
229 class UFO_Birthday extends PlFilterOrder
230 {
231 protected function getSortTokens(PlFilter $uf)
232 {
233 $uf->requireProfiles();
234 return 'p.next_birthday';
235 }
236
237 public function export()
238 {
239 return $this->buildExport('birthday');
240 }
241 }
242 // }}}
243 // {{{ class UFO_ProfileUpdate
244 /** Sorts users based on last profile update
245 */
246 class UFO_ProfileUpdate extends PlFilterOrder
247 {
248 protected function getSortTokens(PlFilter $uf)
249 {
250 $uf->requireProfiles();
251 return 'p.last_change';
252 }
253
254 public function export()
255 {
256 return $this->buildExport('profile_update');
257 }
258 }
259 // }}}
260 // {{{ class UFO_Death
261 /** Sorts users based on death date
262 */
263 class UFO_Death extends PlFilterOrder
264 {
265 protected function getSortTokens(PlFilter $uf)
266 {
267 $uf->requireProfiles();
268 return 'p.deathdate';
269 }
270
271 public function export()
272 {
273 return $this->buildExport('death');
274 }
275 }
276 // }}}
277 // {{{ class UFO_Uid
278 /** Sorts users based on their uid
279 */
280 class UFO_Uid extends PlFilterOrder
281 {
282 protected function getSortTokens(PlFilter $uf)
283 {
284 $uf->requireAccounts();
285 return '$UID';
286 }
287
288 public function export()
289 {
290 return $this->buildExport('uid');
291 }
292 }
293 // }}}
294 // {{{ class UFO_Hruid
295 /** Sorts users based on their hruid
296 */
297 class UFO_Hruid extends PlFilterOrder
298 {
299 protected function getSortTokens(PlFilter $uf)
300 {
301 $uf->requireAccounts();
302 return 'a.hruid';
303 }
304
305 public function export()
306 {
307 return $this->buildExport('hruid');
308 }
309 }
310 // }}}
311 // {{{ class UFO_Pid
312 /** Sorts users based on their pid
313 */
314 class UFO_Pid extends PlFilterOrder
315 {
316 protected function getSortTokens(PlFilter $uf)
317 {
318 $uf->requireProfiles();
319 return '$PID';
320 }
321
322 public function export()
323 {
324 return $this->buildExport('pid');
325 }
326 }
327 // }}}
328 // {{{ class UFO_Hrpid
329 /** Sorts users based on their hrpid
330 */
331 class UFO_Hrpid extends PlFilterOrder
332 {
333 protected function getSortTokens(PlFilter $uf)
334 {
335 $uf->requireProfiles();
336 return 'p.hrpid';
337 }
338
339 public function export()
340 {
341 return $this->buildExport('hrpid');
342 }
343 }
344 // }}}
345 // {{{ class UFO_IsAdmin
346 /** Sorts users, putting admins first
347 */
348 class UFO_IsAdmin extends PlFilterOrder
349 {
350 protected function getSortTokens(PlFilter $uf)
351 {
352 $uf->requireAccounts();
353 return 'a.is_admin';
354 }
355
356 public function export()
357 {
358 return $this->buildExport('is_admin');
359 }
360 }
361 // }}}
362
363 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
364 ?>