Fixes vim mode line.
[platal.git] / classes / userfilter / orders.inc.php
CommitLineData
cd0c2ac4
FB
1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 Polytechnique.org *
cd0c2ac4
FB
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
e312c7f3
FB
22// {{{ abstract class UserFilterOrder
23/** Class providing factories for the UserFilterOrders.
24 */
25abstract 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
e312c7f3 39 case 'name':
3e6ab778 40 return new UFO_Name($desc);
e312c7f3
FB
41
42 case 'score':
43 case 'registration':
44 case 'birthday':
45 case 'profile_update':
46 case 'death':
47 case 'uid':
48 case 'hruid':
49 case 'pid':
50 case 'hrpid':
6c4e3e5a 51 case 'is_admin':
e312c7f3
FB
52 $class = 'UFO_' . str_replace('_', '', $type);
53 return new $class($desc);
54
55 default:
56 throw new Exception("Unknown order field: $type");
57 }
58 }
59}
60// }}}
cd0c2ac4
FB
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
65 */
66class UFO_Promo extends PlFilterGroupableOrder
67{
68 private $grade;
69
70 public function __construct($grade = null, $desc = false)
71 {
72 parent::__construct($desc);
73 $this->grade = $grade;
74 }
75
76 protected function getSortTokens(PlFilter $uf)
77 {
78 if (UserFilter::isGrade($this->grade)) {
29d68ff3 79 $sub = $uf->addEducationFilter(true, $this->grade);
cd0c2ac4
FB
80 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
81 } else {
82 $sub = $uf->addDisplayFilter();
83 return 'pd' . $sub . '.promo';
84 }
85 }
e312c7f3 86
20938467
RB
87 public function getCondition($promo)
88 {
89 return new UFC_Promo(UserFilterCondition::OP_EQUALS, $this->grade, $promo);
90 }
91
e312c7f3
FB
92 public function export()
93 {
94 $export = $this->buildExport('promo');
95 if (!is_null($this->grade)) {
96 $export['grade'] = $this->grade;
97 }
98 return $export;
99 }
cd0c2ac4
FB
100}
101// }}}
cd0c2ac4
FB
102// {{{ class UFO_Name
103/** Sorts users by name
cd0c2ac4
FB
104 * @param $desc If sort order should be descending
105 */
20938467 106class UFO_Name extends PlFilterGroupableOrder
cd0c2ac4 107{
cd0c2ac4
FB
108 protected function getSortTokens(PlFilter $uf)
109 {
3e6ab778
SJ
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 . ')';
cd0c2ac4 115 } else {
3e6ab778 116 return $token;
cd0c2ac4
FB
117 }
118 }
e312c7f3 119
20938467
RB
120 public function getGroupToken(PlFilter $pf)
121 {
122 return 'SUBSTRING(' . $this->_tokens . ', 1, 1)';
123 }
124
125 public function getCondition($initial)
126 {
3e6ab778 127 return new UFC_NameInitial($initial);
20938467
RB
128 }
129
e312c7f3
FB
130 public function export()
131 {
3e6ab778 132 $export = $this->buildExport();
e312c7f3
FB
133 return $export;
134 }
cd0c2ac4
FB
135}
136// }}}
cd0c2ac4
FB
137// {{{ class UFO_Score
138class UFO_Score extends PlFilterOrder
139{
140 protected function getSortTokens(PlFilter $uf)
141 {
142 $toks = $uf->getNameTokens();
143 $scores = array();
144
145 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
146 if (count($toks) == 0) {
147 return 'NULL';
148 }
149
150 foreach ($toks as $sub => $token) {
151 $scores[] = XDB::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
152 }
153 return implode(' + ', $scores);
154 }
e312c7f3
FB
155
156 public function export()
157 {
158 return $this->buildExport('score');
159 }
cd0c2ac4
FB
160}
161// }}}
cd0c2ac4
FB
162// {{{ class UFO_Registration
163/** Sorts users based on registration date
164 */
165class UFO_Registration extends PlFilterOrder
166{
167 protected function getSortTokens(PlFilter $uf)
168 {
169 $uf->requireAccounts();
170 return 'a.registration_date';
171 }
e312c7f3
FB
172
173 public function export()
174 {
175 return $this->buildExport('registration');
176 }
cd0c2ac4
FB
177}
178// }}}
cd0c2ac4
FB
179// {{{ class UFO_Birthday
180/** Sorts users based on next birthday date
181 */
182class UFO_Birthday extends PlFilterOrder
183{
184 protected function getSortTokens(PlFilter $uf)
185 {
186 $uf->requireProfiles();
187 return 'p.next_birthday';
188 }
e312c7f3
FB
189
190 public function export()
191 {
192 return $this->buildExport('birthday');
193 }
cd0c2ac4
FB
194}
195// }}}
cd0c2ac4
FB
196// {{{ class UFO_ProfileUpdate
197/** Sorts users based on last profile update
198 */
199class UFO_ProfileUpdate extends PlFilterOrder
200{
201 protected function getSortTokens(PlFilter $uf)
202 {
203 $uf->requireProfiles();
204 return 'p.last_change';
205 }
e312c7f3
FB
206
207 public function export()
208 {
209 return $this->buildExport('profile_update');
210 }
cd0c2ac4
FB
211}
212// }}}
cd0c2ac4
FB
213// {{{ class UFO_Death
214/** Sorts users based on death date
215 */
216class UFO_Death extends PlFilterOrder
217{
218 protected function getSortTokens(PlFilter $uf)
219 {
220 $uf->requireProfiles();
221 return 'p.deathdate';
222 }
e312c7f3
FB
223
224 public function export()
225 {
226 return $this->buildExport('death');
227 }
cd0c2ac4
FB
228}
229// }}}
cd0c2ac4
FB
230// {{{ class UFO_Uid
231/** Sorts users based on their uid
232 */
233class UFO_Uid extends PlFilterOrder
234{
235 protected function getSortTokens(PlFilter $uf)
236 {
237 $uf->requireAccounts();
238 return '$UID';
239 }
e312c7f3
FB
240
241 public function export()
242 {
243 return $this->buildExport('uid');
244 }
cd0c2ac4
FB
245}
246// }}}
cd0c2ac4
FB
247// {{{ class UFO_Hruid
248/** Sorts users based on their hruid
249 */
250class UFO_Hruid extends PlFilterOrder
251{
252 protected function getSortTokens(PlFilter $uf)
253 {
254 $uf->requireAccounts();
255 return 'a.hruid';
256 }
e312c7f3
FB
257
258 public function export()
259 {
260 return $this->buildExport('hruid');
261 }
cd0c2ac4
FB
262}
263// }}}
cd0c2ac4
FB
264// {{{ class UFO_Pid
265/** Sorts users based on their pid
266 */
267class UFO_Pid extends PlFilterOrder
268{
269 protected function getSortTokens(PlFilter $uf)
270 {
271 $uf->requireProfiles();
272 return '$PID';
273 }
e312c7f3
FB
274
275 public function export()
276 {
277 return $this->buildExport('pid');
278 }
cd0c2ac4
FB
279}
280// }}}
cd0c2ac4
FB
281// {{{ class UFO_Hrpid
282/** Sorts users based on their hrpid
283 */
284class UFO_Hrpid extends PlFilterOrder
285{
286 protected function getSortTokens(PlFilter $uf)
287 {
288 $uf->requireProfiles();
289 return 'p.hrpid';
290 }
e312c7f3
FB
291
292 public function export()
293 {
294 return $this->buildExport('hrpid');
295 }
cd0c2ac4
FB
296}
297// }}}
6c4e3e5a
RB
298// {{{ class UFO_IsAdmin
299/** Sorts users, putting admins first
300 */
301class UFO_IsAdmin extends PlFilterOrder
302{
303 protected function getSortTokens(PlFilter $uf)
304 {
305 $uf->requireAccounts();
306 return 'a.is_admin';
307 }
308
309 public function export()
310 {
311 return $this->buildExport('is_admin');
312 }
313}
314// }}}
cd0c2ac4 315
448c8cdc 316// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
cd0c2ac4 317?>