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 getCondition($promo)
94 return new UFC_Promo(UserFilterCondition
::OP_EQUALS
, $this->grade
, $promo);
97 public function export()
99 $export = $this->buildExport('promo');
100 if (!is_null($this->grade
)) {
101 $export['grade'] = $this->grade
;
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
114 class UFO_Name
extends PlFilterGroupableOrder
120 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
122 parent
::__construct($desc);
124 $this->variant
= $variant;
125 $this->particle
= $particle;
128 protected function getSortTokens(PlFilter
$uf)
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 . ')';
140 $sub = $uf->addNameFilter($this->type
, $this->variant
);
141 if ($this->particle
) {
142 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
144 return 'pn' . $sub . '.name';
149 public function getGroupToken(PlFilter
$pf)
151 return 'SUBSTRING(' . $this->_tokens
. ', 1, 1)';
154 public function getCondition($initial)
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
;
163 $type = Profile
::LASTNAME
;
168 return new UFC_Name($type, $initial, UFC_Name
::PREFIX
);
171 public function export()
173 $export = $this->buildExport($this->type
);
174 if (!is_null($this->variant
)) {
175 $export['variant'] = $this->variant
;
177 if ($this->particle
) {
178 $export['particle'] = true
;
184 // {{{ class UFO_Score
185 class UFO_Score
extends PlFilterOrder
187 protected function getSortTokens(PlFilter
$uf)
189 $toks = $uf->getNameTokens();
192 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
193 if (count($toks) == 0) {
197 foreach ($toks as $sub => $token) {
198 $scores[] = XDB
::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
200 return implode(' + ', $scores);
203 public function export()
205 return $this->buildExport('score');
209 // {{{ class UFO_Registration
210 /** Sorts users based on registration date
212 class UFO_Registration
extends PlFilterOrder
214 protected function getSortTokens(PlFilter
$uf)
216 $uf->requireAccounts();
217 return 'a.registration_date';
220 public function export()
222 return $this->buildExport('registration');
226 // {{{ class UFO_Birthday
227 /** Sorts users based on next birthday date
229 class UFO_Birthday
extends PlFilterOrder
231 protected function getSortTokens(PlFilter
$uf)
233 $uf->requireProfiles();
234 return 'p.next_birthday';
237 public function export()
239 return $this->buildExport('birthday');
243 // {{{ class UFO_ProfileUpdate
244 /** Sorts users based on last profile update
246 class UFO_ProfileUpdate
extends PlFilterOrder
248 protected function getSortTokens(PlFilter
$uf)
250 $uf->requireProfiles();
251 return 'p.last_change';
254 public function export()
256 return $this->buildExport('profile_update');
260 // {{{ class UFO_Death
261 /** Sorts users based on death date
263 class UFO_Death
extends PlFilterOrder
265 protected function getSortTokens(PlFilter
$uf)
267 $uf->requireProfiles();
268 return 'p.deathdate';
271 public function export()
273 return $this->buildExport('death');
278 /** Sorts users based on their uid
280 class UFO_Uid
extends PlFilterOrder
282 protected function getSortTokens(PlFilter
$uf)
284 $uf->requireAccounts();
288 public function export()
290 return $this->buildExport('uid');
294 // {{{ class UFO_Hruid
295 /** Sorts users based on their hruid
297 class UFO_Hruid
extends PlFilterOrder
299 protected function getSortTokens(PlFilter
$uf)
301 $uf->requireAccounts();
305 public function export()
307 return $this->buildExport('hruid');
312 /** Sorts users based on their pid
314 class UFO_Pid
extends PlFilterOrder
316 protected function getSortTokens(PlFilter
$uf)
318 $uf->requireProfiles();
322 public function export()
324 return $this->buildExport('pid');
328 // {{{ class UFO_Hrpid
329 /** Sorts users based on their hrpid
331 class UFO_Hrpid
extends PlFilterOrder
333 protected function getSortTokens(PlFilter
$uf)
335 $uf->requireProfiles();
339 public function export()
341 return $this->buildExport('hrpid');
345 // {{{ class UFO_IsAdmin
346 /** Sorts users, putting admins first
348 class UFO_IsAdmin
extends PlFilterOrder
350 protected function getSortTokens(PlFilter
$uf)
352 $uf->requireAccounts();
356 public function export()
358 return $this->buildExport('is_admin');
363 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: