Changes thoroughly education's implementation : allows multiple education, adds data...
[platal.git] / include / userset.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 require_once('user.func.inc.php');
23
24 global $globals;
25
26 @$globals->search->result_where_statement = '
27 LEFT JOIN profile_education AS edu ON (u.user_id = edu.uid)
28 LEFT JOIN profile_education_enum AS ede ON (ede.id = edu.eduid)
29 LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = u.user_id)
30 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
31 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
32 LEFT JOIN geoloc_pays AS n1 ON (u.nationalite = n1.a2)
33 LEFT JOIN geoloc_pays AS n2 ON (u.nationalite2 = n2.a2)
34 LEFT JOIN geoloc_pays AS n3 ON (u.nationalite2 = n3.a2)
35 LEFT JOIN adresses AS adr ON (u.user_id = adr.uid AND FIND_IN_SET(\'active\',adr.statut))
36 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
37 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
38 LEFT JOIN emails AS em ON (em.uid = u.user_id AND em.flags = \'active\')';
39
40 class UserSet extends PlSet
41 {
42 public function __construct($joins = '', $where = '')
43 {
44 global $globals;
45 parent::__construct('auth_user_md5 AS u',
46 (!empty($GLOBALS['IS_XNET_SITE']) ?
47 'INNER JOIN groupex.membres AS gxm ON (u.user_id = gxm.uid
48 AND gxm.asso_id = ' . $globals->asso('id') . ') ' : '')
49 . 'LEFT JOIN auth_user_quick AS q USING (user_id)
50 LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type = \'a_vie\')
51 ' . $joins,
52 $where,
53 'u.user_id');
54 }
55 }
56
57 class SearchSet extends UserSet
58 {
59 public $advanced = false;
60 private $score = null;
61 private $order = null;
62 private $quick = false;
63
64 public function __construct($quick = false, $no_search = false, $join = '', $where = '')
65 {
66 require_once dirname(__FILE__).'/../modules/search/search.inc.php';
67
68 if ($no_search) {
69 return;
70 }
71
72 $this->quick = $quick;
73 if ($quick) {
74 $this->getQuick($join, $where);
75 } else {
76 $this->getAdvanced($join, $where);
77 }
78 }
79
80 private function getQuick($join, $where)
81 {
82 require_once dirname(__FILE__).'/../modules/search/search.inc.php';
83 global $globals;
84 if (!S::logged()) {
85 Env::kill('with_soundex');
86 }
87 $qSearch = new QuickSearch('quick');
88 $fields = new SFieldGroup(true, array($qSearch));
89 if ($qSearch->isEmpty()) {
90 new ThrowError('Aucun critère de recherche n\'est spécifié.');
91 }
92 $this->score = $qSearch->get_score_statement();
93 $pwhere = $fields->get_where_statement();
94 if (trim($pwhere)) {
95 if (trim($where)) {
96 $where .= ' AND ';
97 }
98 $where .= $pwhere;
99 }
100 if (S::logged() && Env::has('nonins')) {
101 if (trim($where)) {
102 $where .= ' AND ';
103 }
104 $where .= 'u.perms="pending" AND u.deces=0';
105 }
106 parent::__construct($join . ' ' . $fields->get_select_statement(), $where);
107
108 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
109 'u.promo DESC, NomSortKey, prenom')));
110 }
111
112 private function getAdvanced($join, $where)
113 {
114 global $globals;
115 $this->advanced = true;
116 $fields = new SFieldGroup(true, advancedSearchFromInput());
117 if ($fields->too_large()) {
118 new ThrowError('Recherche trop générale.');
119 }
120 parent::__construct(@$join . ' ' . $fields->get_select_statement(),
121 @$where . ' ' . $fields->get_where_statement());
122 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
123 'promo DESC, NomSortKey, prenom')));
124 }
125
126 public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null)
127 {
128 if ($this->score) {
129 $fields .= ', ' . $this->score;
130 }
131 return parent::get($fields, $joins, $where, $groupby, $order, $limitcount, $limitfrom);
132 }
133 }
134
135 class ArraySet extends UserSet
136 {
137 public function __construct(array $users)
138 {
139 $where = $this->getUids($users);
140 if ($where) {
141 $where = "a.alias IN ($where)";
142 } else {
143 $where = " 0 ";
144 }
145 parent::__construct('', $where);
146 }
147
148 private function getUids(array $users)
149 {
150 $users = get_users_forlife_list($users, true, '_silent_user_callback');
151 if (is_null($users)) {
152 return '';
153 }
154 return '\'' . implode('\', \'', $users) . '\'';
155 }
156 }
157
158 class MinificheView extends MultipageView
159 {
160 public function __construct(PlSet &$set, $data, array $params)
161 {
162 require_once 'applis.func.inc.php';
163 global $globals;
164 $this->entriesPerPage = $globals->search->per_page;
165 if (@$params['with_score']) {
166 $this->addSortKey('score', array('-score', '-date', '-promo', 'name_sort'), 'pertinence');
167 }
168 $this->addSortKey('name', array('name_sort'), 'nom');
169 $this->addSortKey('promo', array('-promo', 'name_sort'), 'promotion');
170 $this->addSortKey('date_mod', array('-date', '-promo', 'name_sort'), 'dernière modification');
171 parent::__construct($set, $data, $params);
172 }
173
174 public function fields()
175 {
176 return "u.user_id AS id,
177 u.*, a.alias AS forlife,
178 u.perms != 'pending' AS inscrit,
179 u.perms != 'pending' AS wasinscrit,
180 u.deces != 0 AS dcd, u.deces, u.matricule_ax,
181 FIND_IN_SET('femme', u.flags) AS sexe,
182 e.entreprise, es.label AS secteur, ef.fonction_fr AS fonction,
183 IF(n1.nat='',n1.pays,n1.nat) AS nat1, n1.a2 AS iso3166_1,
184 IF(n2.nat='',n2.pays,n2.nat) AS nat2, n2.a2 AS iso3166_2,
185 IF(n3.nat='',n3.pays,n3.nat) AS nat3, n3.a2 AS iso3166_3,
186 ede0.name AS eduname0, ede0.url AS eduurl0, edd0.degree AS edudegree0, edu0.grad_year AS edugrad_year0, f0.field AS edufield0,
187 ede1.name AS eduname1, ede1.url AS eduurl1, edd1.degree AS edudegree1, edu1.grad_year AS edugrad_year1, f1.field AS edufield1,
188 ede2.name AS eduname2, ede2.url AS eduurl2, edd2.degree AS edudegree2, edu2.grad_year AS edugrad_year2, f2.field AS edufield2,
189 ede3.name AS eduname3, ede3.url AS eduurl3, edd3.degree AS edudegree3, edu3.grad_year AS edugrad_year3, f3.field AS edufield3,
190 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region,
191 (COUNT(em.email) > 0 OR FIND_IN_SET('googleapps', u.mail_storage) > 0) AS actif,
192 nd.display AS name_display, nd.tooltip AS name_tooltip, nd.sort AS name_sort" .
193 (S::logged() ? ", c.contact AS contact" : '');
194 }
195
196 public function joins()
197 {
198 return "LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = u.user_id".(S::logged() ? "" : " AND e.pub = 'public'").")
199 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
200 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
201 LEFT JOIN geoloc_pays AS n1 ON (u.nationalite = n1.a2)
202 LEFT JOIN geoloc_pays AS n2 ON (u.nationalite2 = n2.a2)
203 LEFT JOIN geoloc_pays AS n3 ON (u.nationalite3 = n3.a2)
204 LEFT JOIN profile_education AS edu0 ON (u.user_id = edu0.uid AND edu0.id = 0)
205 LEFT JOIN profile_education_enum AS ede0 ON (ede0.id = edu0.eduid)
206 LEFT JOIN profile_education_degree_enum AS edd0 ON (edd0.id = edu0.degreeid)
207 LEFT JOIN profile_education_field_enum AS f0 ON (f0.id = edu0.fieldid)
208 LEFT JOIN profile_education AS edu1 ON (u.user_id = edu1.uid AND edu1.id = 1)
209 LEFT JOIN profile_education_enum AS ede1 ON (ede1.id = edu1.eduid)
210 LEFT JOIN profile_education_degree_enum AS edd1 ON (edd1.id = edu1.degreeid)
211 LEFT JOIN profile_education_field_enum AS f1 ON (f1.id = edu1.fieldid)
212 LEFT JOIN profile_education AS edu2 ON (u.user_id = edu2.uid AND edu2.id = 2)
213 LEFT JOIN profile_education_enum AS ede2 ON (ede2.id = edu2.eduid)
214 LEFT JOIN profile_education_degree_enum AS edd2 ON (edd2.id = edu2.degreeid)
215 LEFT JOIN profile_education_field_enum AS f2 ON (f2.id = edu2.fieldid)
216 LEFT JOIN profile_education AS edu3 ON (u.user_id = edu3.uid AND edu3.id = 3)
217 LEFT JOIN profile_education_enum AS ede3 ON (ede3.id = edu3.eduid)
218 LEFT JOIN profile_education_degree_enum AS edd3 ON (edd3.id = edu3.degreeid)
219 LEFT JOIN profile_education_field_enum AS f3 ON (f3.id = edu3.fieldid)
220 LEFT JOIN adresses AS adr ON (u.user_id = adr.uid
221 AND FIND_IN_SET('active', adr.statut)".(S::logged() ? "" : " AND adr.pub = 'public'").")
222 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
223 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
224 LEFT JOIN emails AS em ON (em.uid = u.user_id AND em.flags = 'active')
225 INNER JOIN profile_names_display AS nd ON (nd.user_id = u.user_id)" .
226 (S::logged() ?
227 "LEFT JOIN contacts AS c On (c.contact = u.user_id AND c.uid = " . S::v('uid') . ")"
228 : "");
229 }
230
231 public function bounds()
232 {
233 $order = Env::v('order', $this->defaultkey);
234 $show_bounds = 0;
235 if (($order == "name") || ($order == "-name")) {
236 $this->bound_field = "nom";
237 $show_bounds = 1;
238 } elseif (($order == "promo") || ($order == "-promo")) {
239 $this->bound_field = "promo";
240 $show_bounds = -1;
241 }
242 if ($order{0} == '-') {
243 $show_bounds = -$show_bounds;
244 }
245 return $show_bounds;
246 }
247
248 public function templateName()
249 {
250 return 'include/plview.minifiche.tpl';
251 }
252 }
253
254 class MentorView extends MultipageView
255 {
256 public function __construct(PlSet &$set, $data, array $params)
257 {
258 $this->entriesPerPage = 10;
259 $this->addSortKey('rand', array('RAND(' . S::i('uid') . ')'), 'aléatoirement');
260 $this->addSortKey('name', array('name_sort'), 'nom');
261 $this->addSortKey('promo', array('-promo', 'name_sort'), 'promotion');
262 $this->addSortKey('date_mod', array('-date', '-promo', 'name_sort'), 'dernière modification');
263 parent::__construct($set, $data, $params);
264 }
265
266 public function fields()
267 {
268 return "m.uid, u.promo,
269 a.alias AS bestalias, m.expertise, mp.pid,
270 ms.secteur, ms.ss_secteur,
271 nd.display AS name_display, nd.tooltip AS name_tooltip, nd.sort AS name_sort";
272 }
273
274 public function joins()
275 {
276 return "INNER JOIN profile_names_display AS nd ON (nd.user_id = u.user_id)";
277 }
278
279 public function bounds()
280 {
281 $order = Env::v('order', $this->defaultkey);
282 $show_bounds = 0;
283 if (($order == "name") || ($order == "-name")) {
284 $this->bound_field = "nom";
285 $show_bounds = 1;
286 } elseif (($order == "promo") || ($order == "-promo")) {
287 $this->bound_field = "promo";
288 $show_bounds = -1;
289 }
290 if ($order{0} == '-') {
291 $show_bounds = -$show_bounds;
292 }
293 return $show_bounds;
294 }
295
296 public function templateName()
297 {
298 return 'include/plview.referent.tpl';
299 }
300 }
301
302 class TrombiView extends MultipageView
303 {
304 public function __construct(PlSet &$set, $data, array $params)
305 {
306 $this->entriesPerPage = 24;
307 $this->order = explode(',', Env::v('order', 'name_sort'));
308 if (@$params['with_score']) {
309 $this->addSortKey('score', array('-score', '-watch_last', '-promo', 'name_sort'), 'pertinence');
310 }
311 $this->addSortKey('name', array('name_sort'), 'nom');
312 $this->addSortKey('promo', array('-promo', 'name_sort'), 'promotion');
313 parent::__construct($set, $data, $params);
314 }
315
316 public function fields()
317 {
318 return "u.user_id, nd.display AS name_display, nd.tooltip AS name_tooltip, nd.sort AS name_sort, u.promo, a.alias AS forlife ";
319 }
320
321 public function joins()
322 {
323 return "INNER JOIN photo AS p ON (p.uid = u.user_id)
324 INNER JOIN profile_names_display AS nd ON (nd.user_id = u.user_id)";
325 }
326
327 public function bounds()
328 {
329 $order = Env::v('order', $this->defaultkey);
330 $show_bounds = 0;
331 if (($order == "name") || ($order == "-name")) {
332 $this->bound_field = "nom";
333 $show_bounds = 1;
334 } elseif (($order == "promo") || ($order == "-promo")) {
335 $this->bound_field = "promo";
336 $show_bounds = -1;
337 }
338 if ($order{0} == '-') {
339 $show_bounds = -$show_bounds;
340 }
341 return $show_bounds;
342 }
343
344 public function templateName()
345 {
346 return 'include/plview.trombi.tpl';
347 }
348
349 public function apply(PlPage &$page)
350 {
351 if (!empty($GLOBALS['IS_XNET_SITE'])) {
352 global $globals;
353 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
354 }
355 return parent::apply($page);
356 }
357 }
358
359 class GeolocView implements PlView
360 {
361 private $set;
362 private $type;
363 private $params;
364
365 public function __construct(PlSet &$set, $data, array $params)
366 {
367 $this->params = $params;
368 $this->set =& $set;
369 $this->type = $data;
370 }
371
372 private function use_map()
373 {
374 return is_file(dirname(__FILE__) . '/../modules/geoloc/dynamap.swf') &&
375 is_file(dirname(__FILE__) . '/../modules/geoloc/icon.swf');
376 }
377
378 public function args()
379 {
380 $args = $this->set->args();
381 unset($args['initfile']);
382 unset($args['mapid']);
383 return $args;
384 }
385
386 public function apply(PlPage &$page)
387 {
388 require_once 'geoloc.inc.php';
389 require_once '../modules/search/search.inc.php';
390
391 switch ($this->type) {
392 case 'icon.swf':
393 header("Content-type: application/x-shockwave-flash");
394 header("Pragma:");
395 readfile(dirname(__FILE__).'/../modules/geoloc/icon.swf');
396 exit;
397
398 case 'dynamap.swf':
399 header("Content-type: application/x-shockwave-flash");
400 header("Pragma:");
401 readfile(dirname(__FILE__).'/../modules/geoloc/dynamap.swf');
402 exit;
403
404 case 'init':
405 $page->changeTpl('geoloc/init.tpl', NO_SKIN);
406 header('Content-Type: text/xml');
407 header('Pragma:');
408 if (!empty($GLOBALS['IS_XNET_SITE'])) {
409 $page->assign('background', 0xF2E9D0);
410 }
411 break;
412
413 case 'city':
414 $page->changeTpl('geoloc/city.tpl', NO_SKIN);
415 header('Content-Type: text/xml');
416 header('Pragma:');
417 $only_current = Env::v('only_current', false)? ' AND FIND_IN_SET(\'active\', adrf.statut)' : '';
418 $it =& $this->set->get('u.user_id AS id, u.prenom, u.nom, u.promo, al.alias',
419 "INNER JOIN adresses AS adrf ON (adrf.uid = u.user_id $only_current)
420 LEFT JOIN aliases AS al ON (u.user_id = al.id
421 AND FIND_IN_SET('bestalias', al.flags))
422 INNER JOIN adresses AS avg ON (" . getadr_join('avg') . ")",
423 'adrf.cityid = ' . Env::i('cityid'), null, null, 11);
424 $page->assign('users', $it);
425 break;
426
427 case 'country':
428 if (Env::has('debug')) {
429 $page->changeTpl('geoloc/country.tpl', SIMPLE);
430 } else {
431 $page->changeTpl('geoloc/country.tpl', NO_SKIN);
432 header('Content-Type: text/xml');
433 header('Pragma:');
434 }
435 $mapid = Env::has('mapid') ? Env::i('mapid', -2) : false;
436 list($countries, $cities) = geoloc_getData_subcountries($mapid, $this->set, 10);
437 $page->assign('countries', $countries);
438 $page->assign('cities', $cities);
439 break;
440
441 default:
442 global $globals;
443 if (!$this->use_map()) {
444 $page->assign('request_geodesix', true);
445 }
446 $page->assign('annu', @$this->params['with_annu']);
447 $page->assign('protocole', @$_SERVER['HTTPS'] ? 'https' : 'http');
448 $this->set->get('u.user_id', null, "u.perms != 'pending' AND u.deces = 0", "u.user_id", null);
449 return 'include/plview.geoloc.tpl';
450 }
451 }
452 }
453
454 class GadgetView implements PlView
455 {
456 public function __construct(PlSet &$set, $data, array $params)
457 {
458 $this->set =& $set;
459 }
460
461 public function fields()
462 {
463 return "u.user_id AS id,
464 u.*, a.alias AS forlife," .
465 "u.perms != 'pending' AS inscrit,
466 u.perms != 'pending' AS wasinscrit,
467 u.deces != 0 AS dcd, u.deces,
468 FIND_IN_SET('femme', u.flags) AS sexe,
469 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region" .
470 (S::logged() ? ", c.contact AS contact" : '');
471 }
472
473 public function joins()
474 {
475 return "LEFT JOIN adresses AS adr ON (u.user_id = adr.uid AND FIND_IN_SET('active', adr.statut)".(S::logged() ? "" : " AND adr.pub = 'public'").")
476 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
477 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)" .
478 (S::logged() ?
479 "LEFT JOIN contacts AS c On (c.contact = u.user_id AND c.uid = " . S::v('uid') . ")"
480 : "");
481 }
482
483 public function apply(PlPage &$page)
484 {
485 $page->assign_by_ref('set',
486 $this->set->get($this->fields(), $this->joins(), null, null, null, 5, 0));
487 }
488
489 public function args()
490 {
491 return null;
492 }
493 }
494
495 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
496 ?>