b64a8e8628646593984ea42fbda581c4d6a1ef05
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);
40 return new UFO_Name($desc);
45 case 'profile_update':
52 $class = 'UFO_' . str_replace('_', '', $type);
53 return new $class($desc);
56 throw new Exception("Unknown order field: $type");
61 // {{{ class UFO_Promo
62 /** Orders users by promotion
63 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
64 * @param $desc Whether sort is descending
66 class UFO_Promo
extends PlFilterGroupableOrder
70 public function __construct($grade = null
, $desc = false
)
72 parent
::__construct($desc);
73 $this->grade
= $grade;
76 protected function getSortTokens(PlFilter
$uf)
78 if (UserFilter
::isGrade($this->grade
)) {
79 $sub = $uf->addEducationFilter($this->grade
);
80 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
82 $sub = $uf->addDisplayFilter();
83 return 'pd' . $sub . '.promo';
87 public function getCondition($promo)
89 return new UFC_Promo(UserFilterCondition
::OP_EQUALS
, $this->grade
, $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 $desc If sort order should be descending
106 class UFO_Name
extends PlFilterGroupableOrder
108 protected function getSortTokens(PlFilter
$uf)
110 $sub = $uf->addDisplayFilter();
111 $token = 'pd.sort_name';
112 if ($uf->accountsRequired()) {
113 $account_token = Profile
::getAccountEquivalentName('sort_name');
114 return 'IFNULL(' . $token . ', a.' . $account_token . ')';
120 public function getGroupToken(PlFilter
$pf)
122 return 'SUBSTRING(' . $this->_tokens
. ', 1, 1)';
125 public function getCondition($initial)
127 return new UFC_NameInitial($initial);
130 public function export()
132 $export = $this->buildExport();
137 // {{{ class UFO_Score
138 class UFO_Score
extends PlFilterOrder
140 protected function getSortTokens(PlFilter
$uf)
142 $toks = $uf->getNameTokens();
145 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
146 if (count($toks) == 0) {
150 foreach ($toks as $sub => $token) {
151 $scores[] = XDB
::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
153 return implode(' + ', $scores);
156 public function export()
158 return $this->buildExport('score');
162 // {{{ class UFO_Registration
163 /** Sorts users based on registration date
165 class UFO_Registration
extends PlFilterOrder
167 protected function getSortTokens(PlFilter
$uf)
169 $uf->requireAccounts();
170 return 'a.registration_date';
173 public function export()
175 return $this->buildExport('registration');
179 // {{{ class UFO_Birthday
180 /** Sorts users based on next birthday date
182 class UFO_Birthday
extends PlFilterOrder
184 protected function getSortTokens(PlFilter
$uf)
186 $uf->requireProfiles();
187 return 'p.next_birthday';
190 public function export()
192 return $this->buildExport('birthday');
196 // {{{ class UFO_ProfileUpdate
197 /** Sorts users based on last profile update
199 class UFO_ProfileUpdate
extends PlFilterOrder
201 protected function getSortTokens(PlFilter
$uf)
203 $uf->requireProfiles();
204 return 'p.last_change';
207 public function export()
209 return $this->buildExport('profile_update');
213 // {{{ class UFO_Death
214 /** Sorts users based on death date
216 class UFO_Death
extends PlFilterOrder
218 protected function getSortTokens(PlFilter
$uf)
220 $uf->requireProfiles();
221 return 'p.deathdate';
224 public function export()
226 return $this->buildExport('death');
231 /** Sorts users based on their uid
233 class UFO_Uid
extends PlFilterOrder
235 protected function getSortTokens(PlFilter
$uf)
237 $uf->requireAccounts();
241 public function export()
243 return $this->buildExport('uid');
247 // {{{ class UFO_Hruid
248 /** Sorts users based on their hruid
250 class UFO_Hruid
extends PlFilterOrder
252 protected function getSortTokens(PlFilter
$uf)
254 $uf->requireAccounts();
258 public function export()
260 return $this->buildExport('hruid');
265 /** Sorts users based on their pid
267 class UFO_Pid
extends PlFilterOrder
269 protected function getSortTokens(PlFilter
$uf)
271 $uf->requireProfiles();
275 public function export()
277 return $this->buildExport('pid');
281 // {{{ class UFO_Hrpid
282 /** Sorts users based on their hrpid
284 class UFO_Hrpid
extends PlFilterOrder
286 protected function getSortTokens(PlFilter
$uf)
288 $uf->requireProfiles();
292 public function export()
294 return $this->buildExport('hrpid');
298 // {{{ class UFO_IsAdmin
299 /** Sorts users, putting admins first
301 class UFO_IsAdmin
extends PlFilterOrder
303 protected function getSortTokens(PlFilter
$uf)
305 $uf->requireAccounts();
309 public function export()
311 return $this->buildExport('is_admin');
316 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: