f640e0039e6f4bf6d11d15ad192ce65660d3638a
2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // {{{ abstract class UserFilterOrder
23 /** Class providing factories for the UserFilterOrders.
25 abstract class UserFilterOrders
27 public static function fromExport(array $export)
29 $export = new PlDict($export);
30 if (!$export->has('type')) {
31 throw new Exception("Missing type in export");
33 $type = $export->s('type');
34 $desc = ($export->s('order') == 'desc');
37 return new UFO_Promo($export->v('grade'), $desc);
44 return new UFO_Name($type, $export->v('variant'),
45 $export->b('particle'), $desc);
50 case 'profile_update':
57 $class = 'UFO_' . str_replace('_', '', $type);
58 return new $class($desc);
61 throw new Exception("Unknown order field: $type");
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
71 class UFO_Promo
extends PlFilterGroupableOrder
75 public function __construct($grade = null
, $desc = false
)
77 parent
::__construct($desc);
78 $this->grade
= $grade;
81 protected function getSortTokens(PlFilter
$uf)
83 if (UserFilter
::isGrade($this->grade
)) {
84 $sub = $uf->addEducationFilter($this->grade
);
85 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
87 $sub = $uf->addDisplayFilter();
88 return 'pd' . $sub . '.promo';
92 public function export()
94 $export = $this->buildExport('promo');
95 if (!is_null($this->grade
)) {
96 $export['grade'] = $this->grade
;
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
109 class UFO_Name
extends PlFilterOrder
115 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
117 parent
::__construct($desc);
119 $this->variant
= $variant;
120 $this->particle
= $particle;
123 protected function getSortTokens(PlFilter
$uf)
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 . ')';
135 $sub = $uf->addNameFilter($this->type
, $this->variant
);
136 if ($this->particle
) {
137 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
139 return 'pn' . $sub . '.name';
144 public function export()
146 $export = $this->buildExport($this->type
);
147 if (!is_null($this->variant
)) {
148 $export['variant'] = $this->variant
;
150 if ($this->particle
) {
151 $export['particle'] = true
;
157 // {{{ class UFO_Score
158 class UFO_Score
extends PlFilterOrder
160 protected function getSortTokens(PlFilter
$uf)
162 $toks = $uf->getNameTokens();
165 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
166 if (count($toks) == 0) {
170 foreach ($toks as $sub => $token) {
171 $scores[] = XDB
::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
173 return implode(' + ', $scores);
176 public function export()
178 return $this->buildExport('score');
182 // {{{ class UFO_Registration
183 /** Sorts users based on registration date
185 class UFO_Registration
extends PlFilterOrder
187 protected function getSortTokens(PlFilter
$uf)
189 $uf->requireAccounts();
190 return 'a.registration_date';
193 public function export()
195 return $this->buildExport('registration');
199 // {{{ class UFO_Birthday
200 /** Sorts users based on next birthday date
202 class UFO_Birthday
extends PlFilterOrder
204 protected function getSortTokens(PlFilter
$uf)
206 $uf->requireProfiles();
207 return 'p.next_birthday';
210 public function export()
212 return $this->buildExport('birthday');
216 // {{{ class UFO_ProfileUpdate
217 /** Sorts users based on last profile update
219 class UFO_ProfileUpdate
extends PlFilterOrder
221 protected function getSortTokens(PlFilter
$uf)
223 $uf->requireProfiles();
224 return 'p.last_change';
227 public function export()
229 return $this->buildExport('profile_update');
233 // {{{ class UFO_Death
234 /** Sorts users based on death date
236 class UFO_Death
extends PlFilterOrder
238 protected function getSortTokens(PlFilter
$uf)
240 $uf->requireProfiles();
241 return 'p.deathdate';
244 public function export()
246 return $this->buildExport('death');
251 /** Sorts users based on their uid
253 class UFO_Uid
extends PlFilterOrder
255 protected function getSortTokens(PlFilter
$uf)
257 $uf->requireAccounts();
261 public function export()
263 return $this->buildExport('uid');
267 // {{{ class UFO_Hruid
268 /** Sorts users based on their hruid
270 class UFO_Hruid
extends PlFilterOrder
272 protected function getSortTokens(PlFilter
$uf)
274 $uf->requireAccounts();
278 public function export()
280 return $this->buildExport('hruid');
285 /** Sorts users based on their pid
287 class UFO_Pid
extends PlFilterOrder
289 protected function getSortTokens(PlFilter
$uf)
291 $uf->requireProfiles();
295 public function export()
297 return $this->buildExport('pid');
301 // {{{ class UFO_Hrpid
302 /** Sorts users based on their hrpid
304 class UFO_Hrpid
extends PlFilterOrder
306 protected function getSortTokens(PlFilter
$uf)
308 $uf->requireProfiles();
312 public function export()
314 return $this->buildExport('hrpid');
318 // {{{ class UFO_IsAdmin
319 /** Sorts users, putting admins first
321 class UFO_IsAdmin
extends PlFilterOrder
323 protected function getSortTokens(PlFilter
$uf)
325 $uf->requireAccounts();
329 public function export()
331 return $this->buildExport('is_admin');
336 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: