2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 interface UserFilterCondition
24 /** Check that the given user matches the rule.
26 public function check(PlUser
&$user);
29 abstract class UFC_OneChild
implements UserFilterCondition
33 public function setChild(UserFilterCondition
&$cond)
35 $this->child
=& $cond;
39 abstract class UFC_NChildren
implements UserFilterCondition
41 protected $children = array();
43 public function addChild(UserFilterCondition
&$cond)
45 $this->children
[] =& $cond;
49 class UFC_True
implements UserFilterCondition
51 public function check(PlUser
&$user)
57 class UFC_False
implements UserFilterCondition
59 public function check(PlUser
&$user)
65 class UFC_Not
extends UFC_OneChild
67 public function check(PlUser
&$user)
69 return !$this->child
->child($user);
73 class UFC_And
extends UFC_NChildren
75 public function check(PlUser
&$user)
77 foreach ($this->children
as &$cond) {
78 if (!$cond->check($user)) {
86 class UFC_Or
extends UFC_NChildren
88 public function check(PlUser
&$user)
90 foreach ($this->children
as &$cond) {
91 if ($cond->check($user)) {
99 class UFC_Promo
implements UserFilterCondition
101 const GRADE_ING
= 'Ing.';
102 const GRADE_PHD
= 'PhD';
103 const GRADE_MST
= 'M%';
109 public function __construct($comparison, $grade, $promo)
111 $this->grade
= $grade;
112 $this->comparison
= $comparison;
113 $this->promo
= $promo;
116 public function check(PlUser
&$user)
118 if (!$user->hasProfile()) {
121 // XXX: Definition of promotion for phds and masters might change in near future.
122 if ($this->grade
== self
::GRADE_ING
) {
123 $promo_year = 'entry_year';
125 $promo_year = 'grad_year';
127 $req = XDB
::fetchOneCell('SELECT COUNT(pe.id)
128 FROM profile_education AS pe
129 INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id AND pede.abbreviation LIKE {?})
130 INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id AND pee.abbreviation = \'X\')
131 WHERE pe.' . $promo_year . ' ' . $this->comparison
. ' {?} AND pe.uid = {?}',
132 $this->grade
, $this->promo
, $user->profile()->id());
133 return intval($req) > 0;
141 /** Check that the user match the given rule.
143 public function checkUser(PlUser
&$user)
145 return $this->root
->check($user);
148 /** Filter a list of user to extract the users matching the rule.
150 public function filter(array $users)
153 foreach ($users as &$user) {
154 if ($this->checkUser($user)) {
161 public function setCondition(UserFilterCondition
&$cond)
163 $this->root
=& $cond;
167 static public function getLegacy($promo_min, $promo_max)
170 if ($promo_min != 0) {
171 $min = new UFC_Promo('>=', UFC_Promo
::GRADE_ING
, intval($promo_min));
174 if ($promo_max != 0) {
175 $max = new UFC_Promo('<=', UFC_Promo
::GRADE_ING
, intval($promo_max));
177 $uf = new UserFilter();
178 if (is_null($max) && is_null($min)) {
179 $uf->setCondition(new UFC_True());
180 } else if (is_null($max)) {
181 $uf->setCondition($min);
182 } else if (is_null($min)) {
183 $uf->setCondition($max);
185 $cond = new UFC_And();
186 $cond->addChild($min);
187 $cond->addChild($max);
188 $uf->setCondition($cond);
194 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: