c06863398925837fe5e70142775e72bf1778ec0b
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':
56 $class = 'UFO_' . str_replace('_', '', $type);
57 return new $class($desc);
60 throw new Exception("Unknown order field: $type");
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
70 class UFO_Promo
extends PlFilterGroupableOrder
74 public function __construct($grade = null
, $desc = false
)
76 parent
::__construct($desc);
77 $this->grade
= $grade;
80 protected function getSortTokens(PlFilter
$uf)
82 if (UserFilter
::isGrade($this->grade
)) {
83 $sub = $uf->addEducationFilter($this->grade
);
84 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
86 $sub = $uf->addDisplayFilter();
87 return 'pd' . $sub . '.promo';
91 public function export()
93 $export = $this->buildExport('promo');
94 if (!is_null($this->grade
)) {
95 $export['grade'] = $this->grade
;
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
108 class UFO_Name
extends PlFilterOrder
114 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
116 parent
::__construct($desc);
118 $this->variant
= $variant;
119 $this->particle
= $particle;
122 protected function getSortTokens(PlFilter
$uf)
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 . ')';
134 $sub = $uf->addNameFilter($this->type
, $this->variant
);
135 if ($this->particle
) {
136 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
138 return 'pn' . $sub . '.name';
143 public function export()
145 $export = $this->buildExport($this->type
);
146 if (!is_null($this->variant
)) {
147 $export['variant'] = $this->variant
;
149 if ($this->particle
) {
150 $export['particle'] = true
;
156 // {{{ class UFO_Score
157 class UFO_Score
extends PlFilterOrder
159 protected function getSortTokens(PlFilter
$uf)
161 $toks = $uf->getNameTokens();
164 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
165 if (count($toks) == 0) {
169 foreach ($toks as $sub => $token) {
170 $scores[] = XDB
::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
172 return implode(' + ', $scores);
175 public function export()
177 return $this->buildExport('score');
181 // {{{ class UFO_Registration
182 /** Sorts users based on registration date
184 class UFO_Registration
extends PlFilterOrder
186 protected function getSortTokens(PlFilter
$uf)
188 $uf->requireAccounts();
189 return 'a.registration_date';
192 public function export()
194 return $this->buildExport('registration');
198 // {{{ class UFO_Birthday
199 /** Sorts users based on next birthday date
201 class UFO_Birthday
extends PlFilterOrder
203 protected function getSortTokens(PlFilter
$uf)
205 $uf->requireProfiles();
206 return 'p.next_birthday';
209 public function export()
211 return $this->buildExport('birthday');
215 // {{{ class UFO_ProfileUpdate
216 /** Sorts users based on last profile update
218 class UFO_ProfileUpdate
extends PlFilterOrder
220 protected function getSortTokens(PlFilter
$uf)
222 $uf->requireProfiles();
223 return 'p.last_change';
226 public function export()
228 return $this->buildExport('profile_update');
232 // {{{ class UFO_Death
233 /** Sorts users based on death date
235 class UFO_Death
extends PlFilterOrder
237 protected function getSortTokens(PlFilter
$uf)
239 $uf->requireProfiles();
240 return 'p.deathdate';
243 public function export()
245 return $this->buildExport('death');
250 /** Sorts users based on their uid
252 class UFO_Uid
extends PlFilterOrder
254 protected function getSortTokens(PlFilter
$uf)
256 $uf->requireAccounts();
260 public function export()
262 return $this->buildExport('uid');
266 // {{{ class UFO_Hruid
267 /** Sorts users based on their hruid
269 class UFO_Hruid
extends PlFilterOrder
271 protected function getSortTokens(PlFilter
$uf)
273 $uf->requireAccounts();
277 public function export()
279 return $this->buildExport('hruid');
284 /** Sorts users based on their pid
286 class UFO_Pid
extends PlFilterOrder
288 protected function getSortTokens(PlFilter
$uf)
290 $uf->requireProfiles();
294 public function export()
296 return $this->buildExport('pid');
300 // {{{ class UFO_Hrpid
301 /** Sorts users based on their hrpid
303 class UFO_Hrpid
extends PlFilterOrder
305 protected function getSortTokens(PlFilter
$uf)
307 $uf->requireProfiles();
311 public function export()
313 return $this->buildExport('hrpid');
318 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: