| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2014 Polytechnique.org * |
| 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 | |
| 22 | // {{{ abstract class UserFilterOrder |
| 23 | /** Class providing factories for the UserFilterOrders. |
| 24 | */ |
| 25 | abstract 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 | |
| 39 | case 'name': |
| 40 | return new UFO_Name($desc); |
| 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': |
| 51 | case 'is_admin': |
| 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 | // }}} |
| 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 | */ |
| 66 | class 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)) { |
| 79 | $sub = $uf->addEducationFilter(true, $this->grade); |
| 80 | return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade); |
| 81 | } else { |
| 82 | $sub = $uf->addDisplayFilter(); |
| 83 | return 'pd' . $sub . '.promo'; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public function getCondition($promo) |
| 88 | { |
| 89 | return new UFC_Promo(UserFilterCondition::OP_EQUALS, $this->grade, $promo); |
| 90 | } |
| 91 | |
| 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 | } |
| 100 | } |
| 101 | // }}} |
| 102 | // {{{ class UFO_Name |
| 103 | /** Sorts users by name |
| 104 | * @param $desc If sort order should be descending |
| 105 | */ |
| 106 | class UFO_Name extends PlFilterGroupableOrder |
| 107 | { |
| 108 | protected function getSortTokens(PlFilter $uf) |
| 109 | { |
| 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 . ')'; |
| 115 | } else { |
| 116 | return $token; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public function getGroupToken(PlFilter $pf) |
| 121 | { |
| 122 | return 'SUBSTRING(' . $this->_tokens . ', 1, 1)'; |
| 123 | } |
| 124 | |
| 125 | public function getCondition($initial) |
| 126 | { |
| 127 | return new UFC_NameInitial($initial); |
| 128 | } |
| 129 | |
| 130 | public function export() |
| 131 | { |
| 132 | $export = $this->buildExport(); |
| 133 | return $export; |
| 134 | } |
| 135 | } |
| 136 | // }}} |
| 137 | // {{{ class UFO_Score |
| 138 | class 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 | } |
| 155 | |
| 156 | public function export() |
| 157 | { |
| 158 | return $this->buildExport('score'); |
| 159 | } |
| 160 | } |
| 161 | // }}} |
| 162 | // {{{ class UFO_Registration |
| 163 | /** Sorts users based on registration date |
| 164 | */ |
| 165 | class UFO_Registration extends PlFilterOrder |
| 166 | { |
| 167 | protected function getSortTokens(PlFilter $uf) |
| 168 | { |
| 169 | $uf->requireAccounts(); |
| 170 | return 'a.registration_date'; |
| 171 | } |
| 172 | |
| 173 | public function export() |
| 174 | { |
| 175 | return $this->buildExport('registration'); |
| 176 | } |
| 177 | } |
| 178 | // }}} |
| 179 | // {{{ class UFO_Birthday |
| 180 | /** Sorts users based on next birthday date |
| 181 | */ |
| 182 | class UFO_Birthday extends PlFilterOrder |
| 183 | { |
| 184 | protected function getSortTokens(PlFilter $uf) |
| 185 | { |
| 186 | $uf->requireProfiles(); |
| 187 | return 'p.next_birthday'; |
| 188 | } |
| 189 | |
| 190 | public function export() |
| 191 | { |
| 192 | return $this->buildExport('birthday'); |
| 193 | } |
| 194 | } |
| 195 | // }}} |
| 196 | // {{{ class UFO_ProfileUpdate |
| 197 | /** Sorts users based on last profile update |
| 198 | */ |
| 199 | class UFO_ProfileUpdate extends PlFilterOrder |
| 200 | { |
| 201 | protected function getSortTokens(PlFilter $uf) |
| 202 | { |
| 203 | $uf->requireProfiles(); |
| 204 | return 'p.last_change'; |
| 205 | } |
| 206 | |
| 207 | public function export() |
| 208 | { |
| 209 | return $this->buildExport('profile_update'); |
| 210 | } |
| 211 | } |
| 212 | // }}} |
| 213 | // {{{ class UFO_Death |
| 214 | /** Sorts users based on death date |
| 215 | */ |
| 216 | class UFO_Death extends PlFilterOrder |
| 217 | { |
| 218 | protected function getSortTokens(PlFilter $uf) |
| 219 | { |
| 220 | $uf->requireProfiles(); |
| 221 | return 'p.deathdate'; |
| 222 | } |
| 223 | |
| 224 | public function export() |
| 225 | { |
| 226 | return $this->buildExport('death'); |
| 227 | } |
| 228 | } |
| 229 | // }}} |
| 230 | // {{{ class UFO_Uid |
| 231 | /** Sorts users based on their uid |
| 232 | */ |
| 233 | class UFO_Uid extends PlFilterOrder |
| 234 | { |
| 235 | protected function getSortTokens(PlFilter $uf) |
| 236 | { |
| 237 | $uf->requireAccounts(); |
| 238 | return '$UID'; |
| 239 | } |
| 240 | |
| 241 | public function export() |
| 242 | { |
| 243 | return $this->buildExport('uid'); |
| 244 | } |
| 245 | } |
| 246 | // }}} |
| 247 | // {{{ class UFO_Hruid |
| 248 | /** Sorts users based on their hruid |
| 249 | */ |
| 250 | class UFO_Hruid extends PlFilterOrder |
| 251 | { |
| 252 | protected function getSortTokens(PlFilter $uf) |
| 253 | { |
| 254 | $uf->requireAccounts(); |
| 255 | return 'a.hruid'; |
| 256 | } |
| 257 | |
| 258 | public function export() |
| 259 | { |
| 260 | return $this->buildExport('hruid'); |
| 261 | } |
| 262 | } |
| 263 | // }}} |
| 264 | // {{{ class UFO_Pid |
| 265 | /** Sorts users based on their pid |
| 266 | */ |
| 267 | class UFO_Pid extends PlFilterOrder |
| 268 | { |
| 269 | protected function getSortTokens(PlFilter $uf) |
| 270 | { |
| 271 | $uf->requireProfiles(); |
| 272 | return '$PID'; |
| 273 | } |
| 274 | |
| 275 | public function export() |
| 276 | { |
| 277 | return $this->buildExport('pid'); |
| 278 | } |
| 279 | } |
| 280 | // }}} |
| 281 | // {{{ class UFO_Hrpid |
| 282 | /** Sorts users based on their hrpid |
| 283 | */ |
| 284 | class UFO_Hrpid extends PlFilterOrder |
| 285 | { |
| 286 | protected function getSortTokens(PlFilter $uf) |
| 287 | { |
| 288 | $uf->requireProfiles(); |
| 289 | return 'p.hrpid'; |
| 290 | } |
| 291 | |
| 292 | public function export() |
| 293 | { |
| 294 | return $this->buildExport('hrpid'); |
| 295 | } |
| 296 | } |
| 297 | // }}} |
| 298 | // {{{ class UFO_IsAdmin |
| 299 | /** Sorts users, putting admins first |
| 300 | */ |
| 301 | class 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 | // }}} |
| 315 | |
| 316 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: |
| 317 | ?> |