Commit | Line | Data |
---|---|---|
e7b93962 FB |
1 | <?php |
2 | /*************************************************************************** | |
c441aabe | 3 | * Copyright (C) 2003-2014 Polytechnique.org * |
e7b93962 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 | ||
aaffb7b2 | 22 | class Profile implements PlExportable |
1a9affb7 | 23 | { |
f5642983 | 24 | |
913a4e90 RB |
25 | /* name tokens */ |
26 | const LASTNAME = 'lastname'; | |
27 | const FIRSTNAME = 'firstname'; | |
28 | const NICKNAME = 'nickname'; | |
29 | const PSEUDONYM = 'pseudonym'; | |
30 | const NAME = 'name'; | |
31 | /* name variants */ | |
32 | const VN_MARITAL = 'marital'; | |
33 | const VN_ORDINARY = 'ordinary'; | |
34 | const VN_OTHER = 'other'; | |
35 | const VN_INI = 'ini'; | |
36 | /* display names */ | |
37 | const DN_FULL = 'directory_name'; | |
38 | const DN_DISPLAY = 'yourself'; | |
39 | const DN_YOURSELF = 'yourself'; | |
40 | const DN_DIRECTORY = 'directory_name'; | |
41 | const DN_PRIVATE = 'private_name'; | |
42 | const DN_PUBLIC = 'public_name'; | |
43 | const DN_SHORT = 'short_name'; | |
44 | const DN_SORT = 'sort_name'; | |
e02d9fbb SJ |
45 | /* education related names */ |
46 | const EDU_X = 'École polytechnique'; | |
47 | const DEGREE_X = 'Ingénieur'; | |
48 | const DEGREE_M = 'Master'; | |
49 | const DEGREE_D = 'Doctorat'; | |
913a4e90 | 50 | |
9fd2ca04 SJ |
51 | static public $cycles = array( |
52 | self::DEGREE_X => 'polytechnicien', | |
53 | self::DEGREE_M => 'master', | |
54 | self::DEGREE_D => 'docteur' | |
55 | ); | |
e4860774 SJ |
56 | static public $cycle_prefixes = array( |
57 | self::DEGREE_X => 'X', | |
58 | self::DEGREE_M => 'M', | |
59 | self::DEGREE_D => 'D' | |
60 | ); | |
9fd2ca04 | 61 | |
913a4e90 RB |
62 | static public $name_variants = array( |
63 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), | |
64 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) | |
65 | ); | |
66 | ||
3c985c08 RB |
67 | const ADDRESS_MAIN = 0x00000001; |
68 | const ADDRESS_PERSO = 0x00000002; | |
69 | const ADDRESS_PRO = 0x00000004; | |
70 | const ADDRESS_ALL = 0x00000006; | |
71 | const ADDRESS_POSTAL = 0x00000008; | |
72 | ||
73 | const EDUCATION_MAIN = 0x00000010; | |
74 | const EDUCATION_EXTRA = 0x00000020; | |
75 | const EDUCATION_ALL = 0x00000040; | |
76 | const EDUCATION_FINISHED = 0x00000080; | |
77 | const EDUCATION_CURRENT = 0x00000100; | |
78 | ||
79 | const JOBS_MAIN = 0x00001000; | |
80 | const JOBS_ALL = 0x00002000; | |
81 | const JOBS_FINISHED = 0x00004000; | |
82 | const JOBS_CURRENT = 0x00008000; | |
83 | ||
34de4b20 | 84 | const NETWORKING_ALL = 0x00070000; |
3c985c08 RB |
85 | const NETWORKING_WEB = 0x00010000; |
86 | const NETWORKING_IM = 0x00020000; | |
87 | const NETWORKING_SOCIAL = 0x00040000; | |
88 | ||
89 | const PHONE_LINK_JOB = 0x00100000; | |
90 | const PHONE_LINK_ADDRESS = 0x00200000; | |
91 | const PHONE_LINK_PROFILE = 0x00400000; | |
92 | const PHONE_LINK_COMPANY = 0x00800000; | |
93 | const PHONE_LINK_ANY = 0x00F00000; | |
94 | ||
95 | const PHONE_TYPE_FAX = 0x01000000; | |
96 | const PHONE_TYPE_FIXED = 0x02000000; | |
97 | const PHONE_TYPE_MOBILE = 0x04000000; | |
98 | const PHONE_TYPE_ANY = 0x07000000; | |
99 | ||
100 | const PHONE_ANY = 0x07F00000; | |
04a94b1d | 101 | |
4d1f0f6b RB |
102 | const FETCH_ADDRESSES = 0x000001; |
103 | const FETCH_CORPS = 0x000002; | |
104 | const FETCH_EDU = 0x000004; | |
105 | const FETCH_JOBS = 0x000008; | |
106 | const FETCH_MEDALS = 0x000010; | |
107 | const FETCH_NETWORKING = 0x000020; | |
4d1f0f6b RB |
108 | const FETCH_MENTOR_COUNTRY = 0x000080; |
109 | const FETCH_PHONES = 0x000100; | |
3ac45f10 PC |
110 | const FETCH_JOB_TERMS = 0x000200; |
111 | const FETCH_MENTOR_TERMS = 0x000400; | |
080632c7 | 112 | const FETCH_DELTATEN = 0x000800; |
148af7a9 | 113 | const FETCH_PARTNER = 0x001000; |
7e0635c2 SJ |
114 | const FETCH_SKILL = 0x002000; |
115 | const FETCH_LANGUAGE = 0x004000; | |
9f21bd15 | 116 | |
4d1f0f6b | 117 | const FETCH_MINIFICHES = 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES |
fa9994fa | 118 | |
7e0635c2 | 119 | const FETCH_ALL = 0x007FFF; // OR of FETCH_* |
6b5008ec | 120 | |
7a9680aa SJ |
121 | static public $descriptions = array( |
122 | 'search_names' => 'Noms', | |
123 | 'nationality1' => 'Nationalité', | |
124 | 'nationality2' => '2e nationalité', | |
125 | 'nationality3' => '3e nationalité', | |
126 | 'promo_display' => 'Promotion affichée', | |
127 | 'email_directory' => 'Email annuaire papier', | |
128 | 'networking' => 'Messageries…', | |
129 | 'tels' => 'Téléphones', | |
130 | 'edus' => 'Formations', | |
d7de04af | 131 | 'main_edus' => 'Formations à l\'X', |
7a9680aa SJ |
132 | 'promo' => 'Promotion de sortie', |
133 | 'birthdate' => 'Date de naissance', | |
134 | 'yourself' => 'Nom affiché', | |
135 | 'freetext' => 'Commentaire', | |
136 | 'freetext_pub' => 'Affichage du commentaire', | |
9ede5056 | 137 | 'axfreetext' => 'Commentaire AX', |
7a9680aa SJ |
138 | 'photo' => 'Photographie', |
139 | 'photo_pub' => 'Affichage de la photographie', | |
140 | 'addresses' => 'Adresses', | |
141 | 'corps' => 'Corps', | |
142 | 'cv' => 'CV', | |
143 | 'jobs' => 'Emplois', | |
144 | 'section' => 'Section', | |
145 | 'binets' => 'Binets', | |
146 | 'medals' => 'Décorations', | |
147 | 'medals_pub' => 'Affichage des décorations', | |
148 | 'competences' => 'Compétences', | |
149 | 'langues' => 'Langues', | |
150 | 'expertise' => 'Expertises (mentoring)', | |
151 | 'terms' => 'Compétences (mentoring)', | |
080632c7 RB |
152 | 'countries' => 'Pays (mentoring)', |
153 | 'deltaten' => 'Opération N N-10', | |
7a9680aa SJ |
154 | ); |
155 | ||
1f8250e4 RB |
156 | private $fetched_fields = 0x000000; |
157 | ||
e7b93962 FB |
158 | private $pid; |
159 | private $hrpid; | |
564e2b2a RB |
160 | private $owner; |
161 | private $owner_fetched = false; | |
3e53a496 FB |
162 | private $data = array(); |
163 | ||
f5642983 FB |
164 | private $visibility = null; |
165 | ||
1f8250e4 | 166 | |
22771578 | 167 | private function __construct(array $data, Visibility $visibility) |
e7b93962 | 168 | { |
b774ddab | 169 | $this->data = $data; |
832e6fcb FB |
170 | $this->pid = $this->data['pid']; |
171 | $this->hrpid = $this->data['hrpid']; | |
38a86f60 | 172 | $this->visibility = $visibility; |
9f21bd15 RB |
173 | } |
174 | ||
dcb7e67d | 175 | public function __destruct() |
2c61c11c RB |
176 | { |
177 | // Need to delete objects allocated by the profile | |
178 | // http://www.php.net/manual/en/function.unset.php#98692 | |
179 | unset($this->owner); | |
180 | } | |
181 | ||
e7b93962 FB |
182 | public function id() |
183 | { | |
184 | return $this->pid; | |
185 | } | |
186 | ||
187 | public function hrid() | |
188 | { | |
189 | return $this->hrpid; | |
190 | } | |
191 | ||
564e2b2a RB |
192 | public function owner() |
193 | { | |
194 | if ($this->owner == null && !$this->owner_fetched) { | |
195 | $this->owner_fetched = true; | |
196 | $this->owner = User::getSilent($this); | |
197 | } | |
198 | return $this->owner; | |
199 | } | |
200 | ||
280806d9 SJ |
201 | public function isActive() |
202 | { | |
203 | if ($this->owner()) { | |
204 | return $this->owner->isActive(); | |
205 | } | |
206 | return false; | |
207 | } | |
208 | ||
d718e91c | 209 | public function promo($details = false) |
d5e60905 | 210 | { |
d718e91c SJ |
211 | if ($details && ($this->program || $this->fieldid)) { |
212 | $text = array(); | |
213 | if ($this->program) { | |
214 | $text[] = $this->program; | |
215 | } | |
216 | if ($this->fieldid) { | |
217 | $fieldsList = DirEnum::getOptions(DirEnum::EDUFIELDS); | |
218 | $text[] = $fieldsList[$this->fieldid]; | |
219 | } | |
220 | return $this->promo . ' (' . implode(', ', $text) . ')'; | |
221 | } | |
222 | ||
d5e60905 FB |
223 | return $this->promo; |
224 | } | |
225 | ||
845f0a4d SJ |
226 | public function yearpromo() |
227 | { | |
7733ade1 | 228 | return $this->promo_year; |
845f0a4d SJ |
229 | } |
230 | ||
1168306a PC |
231 | /** Check if user is an orange (associated with several promos) |
232 | */ | |
233 | public function isMultiPromo() | |
234 | { | |
235 | return $this->grad_year != $this->entry_year + $this->mainEducationDuration(); | |
236 | } | |
237 | ||
238 | /** Returns an array with all associated promo years. | |
239 | */ | |
240 | public function yearspromo() | |
241 | { | |
242 | $promos = array(); | |
243 | $d = -$this->deltaPromoToGradYear(); | |
244 | for ($g = $this->entry_year + $this->mainEducationDuration(); $g <= $this->grad_year; ++$g) { | |
245 | $promos[] = $g + $d; | |
246 | } | |
247 | return $promos; | |
248 | } | |
249 | ||
963bc7fc SJ |
250 | public function mainEducation() |
251 | { | |
39e2c58c FB |
252 | if (empty($this->promo)) { |
253 | return null; | |
254 | } else { | |
255 | return $this->promo{0}; | |
256 | } | |
963bc7fc SJ |
257 | } |
258 | ||
02d9dff0 FB |
259 | public function mainGrade() |
260 | { | |
261 | switch ($this->mainEducation()) { | |
262 | case 'X': | |
263 | return UserFilter::GRADE_ING; | |
264 | case 'M': | |
265 | return UserFilter::GRADE_MST; | |
266 | case 'D': | |
267 | return UserFilter::GRADE_PHD; | |
268 | default: | |
269 | return null; | |
270 | } | |
271 | } | |
272 | ||
963bc7fc SJ |
273 | public function mainEducationDuration() |
274 | { | |
275 | switch ($this->mainEducation()) { | |
276 | case 'X': | |
277 | return 3; | |
278 | case 'M': | |
279 | return 2; | |
280 | case 'D': | |
281 | return 3; | |
282 | default: | |
283 | return 0; | |
284 | } | |
285 | } | |
286 | ||
edc03014 SJ |
287 | public static function educationDuration($education) |
288 | { | |
289 | switch ($education) { | |
290 | case self::DEGREE_X: | |
291 | return 3; | |
292 | case self::DEGREE_M: | |
293 | return 2; | |
294 | case self::DEGREE_D: | |
295 | return 3; | |
296 | default: | |
297 | return 0; | |
298 | } | |
299 | } | |
300 | ||
1168306a PC |
301 | /** Number of years between the promotion year until the |
302 | * graduation year. In standard schools it's 0, but for | |
303 | * Polytechnique the promo year is the entry year. | |
304 | */ | |
305 | public function deltaPromoToGradYear() | |
306 | { | |
307 | if ($this->mainEducation() == 'X') { | |
308 | return $this->mainEducationDuration(); | |
309 | } | |
310 | return 0; | |
311 | } | |
312 | ||
3ac6d61d SJ |
313 | // Returns the profile's color. |
314 | public function promoColor() | |
315 | { | |
316 | switch ($this->mainEducation()) { | |
317 | case 'X': | |
318 | if (($this->yearpromo() % 2) === 0) { | |
319 | return 'red'; | |
320 | } else { | |
321 | return 'yellow'; | |
322 | } | |
323 | case 'M': | |
324 | return 'green'; | |
325 | case 'D': | |
326 | return 'blue'; | |
327 | default: | |
328 | return 'gray'; | |
329 | } | |
330 | } | |
331 | ||
06740f52 SJ |
332 | // Returns younger/older promotion year for a given education. |
333 | static public function extremePromotions($education) | |
334 | { | |
335 | return XDB::fetchOneRow("SELECT MIN(pe.promo_year) AS min, MAX(pe.promo_year) AS max | |
336 | FROM profile_education AS pe | |
337 | INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) | |
338 | WHERE pede.degree = {?} AND FIND_IN_SET('primary', pe.flags)", | |
339 | $education); | |
340 | } | |
341 | ||
94b72319 FB |
342 | /** Print a name with the given formatting: |
343 | * %s = • for women | |
344 | * %f = firstname | |
345 | * %l = lastname | |
346 | * %F = fullname | |
347 | * %S = shortname | |
348 | * %p = promo | |
349 | */ | |
350 | public function name($format) | |
351 | { | |
352 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), | |
353 | array($this->isFemale() ? '•' : '', | |
faf75faf SJ |
354 | $this->firstName(), $this->lastName(), |
355 | $this->fullName(), $this->shortName(), | |
356 | $this->promo()), $format); | |
94b72319 FB |
357 | } |
358 | ||
359 | public function fullName($with_promo = false) | |
360 | { | |
361 | if ($with_promo) { | |
362 | return $this->full_name . ' (' . $this->promo . ')'; | |
363 | } | |
364 | return $this->full_name; | |
365 | } | |
366 | ||
367 | public function shortName($with_promo = false) | |
368 | { | |
369 | if ($with_promo) { | |
370 | return $this->short_name . ' (' . $this->promo . ')'; | |
371 | } | |
372 | return $this->short_name; | |
373 | } | |
374 | ||
27492fde AL |
375 | public function sortName() |
376 | { | |
377 | return $this->sort_name; | |
378 | } | |
379 | ||
94b72319 FB |
380 | public function firstName() |
381 | { | |
5d2e1546 | 382 | return $this->firstname_ordinary; |
94b72319 FB |
383 | } |
384 | ||
5c005b37 RB |
385 | public function firstNames() |
386 | { | |
387 | return $this->nameVariants(self::FIRSTNAME); | |
388 | } | |
389 | ||
94b72319 FB |
390 | public function lastName() |
391 | { | |
5d2e1546 | 392 | return $this->lastname_ordinary; |
94b72319 FB |
393 | } |
394 | ||
5c005b37 RB |
395 | public function lastNames() |
396 | { | |
397 | return $this->nameVariants(self::LASTNAME); | |
398 | } | |
399 | ||
94b72319 FB |
400 | public function isFemale() |
401 | { | |
402 | return $this->sex == PlUser::GENDER_FEMALE; | |
403 | } | |
404 | ||
400ac338 RB |
405 | public function isDead() |
406 | { | |
407 | return ($this->deathdate != null); | |
408 | } | |
409 | ||
564e2b2a RB |
410 | public function displayEmail() |
411 | { | |
412 | $o = $this->owner(); | |
22771578 | 413 | if ($o != null && $this->isVisible(Visibility::EXPORT_PRIVATE)) { |
564e2b2a RB |
414 | return $o->bestEmail(); |
415 | } else { | |
416 | return $this->email_directory; | |
417 | } | |
418 | } | |
419 | ||
94b72319 FB |
420 | public function data() |
421 | { | |
422 | $this->first_name; | |
423 | return $this->data; | |
424 | } | |
425 | ||
5c005b37 RB |
426 | private function nameVariants($type) |
427 | { | |
428 | $vals = array($this->$type); | |
429 | foreach (self::$name_variants[$type] as $var) { | |
430 | $vartype = $type . '_' . $var; | |
431 | $varname = $this->$vartype; | |
432 | if ($varname != null && $varname != "") { | |
433 | $vals[] = $varname; | |
434 | } | |
435 | } | |
436 | return array_unique($vals); | |
437 | } | |
438 | ||
b6569a95 FB |
439 | public function nationalities() |
440 | { | |
441 | $nats = array(); | |
3a2985f9 | 442 | $nationalities = DirEnum::getOptions(DirEnum::NATIONALITIES); |
b6569a95 | 443 | if ($this->nationality1) { |
3a2985f9 | 444 | $nats[$this->nationality1] = $nationalities[$this->nationality1]; |
b6569a95 FB |
445 | } |
446 | if ($this->nationality2) { | |
3a2985f9 | 447 | $nats[$this->nationality2] = $nationalities[$this->nationality2]; |
b6569a95 FB |
448 | } |
449 | if ($this->nationality3) { | |
3a2985f9 | 450 | $nats[$this->nationality3] = $nationalities[$this->nationality3]; |
b6569a95 FB |
451 | } |
452 | return $nats; | |
453 | } | |
454 | ||
3e53a496 FB |
455 | public function __get($name) |
456 | { | |
457 | if (property_exists($this, $name)) { | |
458 | return $this->$name; | |
459 | } | |
460 | ||
3e53a496 FB |
461 | if (isset($this->data[$name])) { |
462 | return $this->data[$name]; | |
463 | } | |
464 | ||
465 | return null; | |
466 | } | |
467 | ||
468 | public function __isset($name) | |
469 | { | |
470 | return property_exists($this, $name) || isset($this->data[$name]); | |
471 | } | |
472 | ||
ddbebe4c FB |
473 | public function __unset($name) |
474 | { | |
475 | if (property_exists($this, $name)) { | |
476 | $this->$name = null; | |
477 | } else { | |
478 | unset($this->data[$name]); | |
479 | } | |
480 | } | |
481 | ||
482 | ||
405d70cc RB |
483 | /** |
484 | * Clears a profile. | |
080632c7 RB |
485 | * *always deletes in: profile_addresses, profile_binets, profile_deltaten, |
486 | * profile_job, profile_langskills, profile_mentor, profile_networking, | |
148af7a9 RB |
487 | * profile_partnersharing_settings, profile_phones, profile_skills, |
488 | * watch_profile | |
405d70cc | 489 | * *always keeps in: profile_corps, profile_display, profile_education, |
0e1dfbad | 490 | * profile_medals, profile_*_names, profile_photos, search_name |
405d70cc RB |
491 | * *modifies: profiles |
492 | */ | |
493 | public function clear() | |
494 | { | |
495 | $tables = array( | |
496 | 'profile_job', 'profile_langskills', 'profile_mentor', | |
497 | 'profile_networking', 'profile_skills', 'watch_profile', | |
080632c7 | 498 | 'profile_phones', 'profile_addresses', 'profile_binets', |
148af7a9 | 499 | 'profile_deltaten', 'profile_partnersharing_settings'); |
405d70cc RB |
500 | |
501 | foreach ($tables as $t) { | |
502 | XDB::execute('DELETE FROM ' . $t . ' | |
503 | WHERE pid = {?}', | |
504 | $this->id()); | |
505 | } | |
506 | ||
507 | XDB::execute("UPDATE profiles | |
9ede5056 | 508 | SET cv = NULL, freetext = NULL, freetext_pub = 'private', axfreetext = NULL, |
fe6080a4 | 509 | medals_pub = 'private', alias_pub = 'hidden', |
405d70cc RB |
510 | email_directory = NULL |
511 | WHERE pid = {?}", | |
512 | $this->id()); | |
513 | } | |
514 | ||
c159e50f RB |
515 | /** Determine whether an item with visibility $visibility can be displayed |
516 | * with the current level of visibility of the profile | |
517 | * @param $visibility The level of visibility to be checked | |
518 | */ | |
519 | public function isVisible($visibility) | |
520 | { | |
1a9affb7 | 521 | return $this->visibility->isVisible($visibility); |
9f21bd15 RB |
522 | } |
523 | ||
a0f5fa32 | 524 | /** Stores the list of fields which have already been fetched for this Profile |
1f8250e4 RB |
525 | */ |
526 | public function setFetchedFields($fields) | |
990cb17b | 527 | { |
1f8250e4 RB |
528 | if (($fields | self::FETCH_ALL) != self::FETCH_ALL) { |
529 | Platal::page()->kill("Invalid fetched fields: $fields"); | |
530 | } | |
531 | ||
532 | $this->fetched_fields = $fields; | |
533 | } | |
534 | ||
4ec03752 RB |
535 | /** Have we already fetched this field ? |
536 | */ | |
1f8250e4 RB |
537 | private function fetched($field) |
538 | { | |
4ec03752 RB |
539 | if (!array_key_exists($field, ProfileField::$fields)) { |
540 | Platal::page()->kill("Invalid field: $field"); | |
1f8250e4 RB |
541 | } |
542 | ||
543 | return ($this->fetched_fields & $field); | |
544 | } | |
545 | ||
546 | /** If not already done, fetches data for the given field | |
547 | * @param $field One of the Profile::FETCH_* | |
548 | * @return A ProfileField, or null | |
549 | */ | |
550 | private function getProfileField($field) | |
551 | { | |
3ac45f10 PC |
552 | if (!array_key_exists($field, ProfileField::$fields)) { |
553 | Platal::page()->kill("Invalid field: $field"); | |
554 | } | |
1f8250e4 RB |
555 | if ($this->fetched($field)) { |
556 | return null; | |
557 | } else { | |
558 | $this->fetched_fields = $this->fetched_fields | $field; | |
559 | } | |
560 | ||
561 | $cls = ProfileField::$fields[$field]; | |
562 | ||
990cb17b RB |
563 | return ProfileField::getForPID($cls, $this->id(), $this->visibility); |
564 | } | |
565 | ||
8cf886dc RB |
566 | /** Consolidates internal data (addresses, phones, jobs) |
567 | */ | |
568 | private function consolidateFields() | |
569 | { | |
4ec03752 | 570 | // Link phones to addresses |
8cf886dc RB |
571 | if ($this->phones != null) { |
572 | if ($this->addresses != null) { | |
573 | $this->addresses->addPhones($this->phones); | |
574 | } | |
575 | ||
576 | if ($this->jobs != null) { | |
577 | $this->jobs->addPhones($this->phones); | |
578 | } | |
579 | } | |
580 | ||
4ec03752 | 581 | // Link addresses to jobs |
8cf886dc RB |
582 | if ($this->addresses != null && $this->jobs != null) { |
583 | $this->jobs->addAddresses($this->addresses); | |
584 | } | |
4ec03752 RB |
585 | |
586 | // Link jobterms to jobs | |
3ac45f10 PC |
587 | if ($this->jobs != null && $this->jobterms != null) { |
588 | $this->jobs->addJobTerms($this->jobterms); | |
589 | } | |
8cf886dc RB |
590 | } |
591 | ||
833a6e86 FB |
592 | /* Photo |
593 | */ | |
1a9affb7 | 594 | private $photo = null; |
7988f7d6 | 595 | public function getPhoto($fallback = true, $data = false) |
9f21bd15 | 596 | { |
1a9affb7 RB |
597 | if ($this->has_photo) { |
598 | if ($data && ($this->photo == null || $this->photo->mimeType == null)) { | |
8d6bbca3 | 599 | $res = XDB::fetchOneAssoc('SELECT attach, attachmime, x, y, last_update |
1f8250e4 RB |
600 | FROM profile_photos |
601 | WHERE pid = {?}', $this->pid); | |
8d6bbca3 | 602 | $this->photo = PlImage::fromData($res['attach'], 'image/' .$res['attachmime'], $res['x'], $res['y'], $res['last_update']); |
1a9affb7 RB |
603 | } else if ($this->photo == null) { |
604 | $this->photo = PlImage::fromData(null, null, $this->photo_width, $this->photo_height); | |
605 | } | |
606 | return $this->photo; | |
9f21bd15 | 607 | } else if ($fallback) { |
fa671a30 SJ |
608 | if ($this->mainEducation() == 'X') { |
609 | return PlImage::fromFile(dirname(__FILE__) . '/../htdocs/images/none_x.png', 'image/png'); | |
610 | } | |
611 | return PlImage::fromFile(dirname(__FILE__) . '/../htdocs/images/none_md.png', 'image/png'); | |
9f21bd15 RB |
612 | } |
613 | return null; | |
614 | } | |
7d0ebdf5 | 615 | |
4bc5b8f0 FB |
616 | /* Addresses |
617 | */ | |
7d0ebdf5 RB |
618 | private $addresses = null; |
619 | public function setAddresses(ProfileAddresses $addr) | |
620 | { | |
621 | $this->addresses = $addr; | |
8cf886dc | 622 | $this->consolidateFields(); |
7d0ebdf5 RB |
623 | } |
624 | ||
dbcc3b3d | 625 | private function fetchAddresses() |
f5642983 | 626 | { |
1f8250e4 | 627 | if ($this->addresses == null && !$this->fetched(self::FETCH_ADDRESSES)) { |
61ee68c7 FB |
628 | $addr = $this->getProfileField(self::FETCH_ADDRESSES); |
629 | if ($addr) { | |
630 | $this->setAddresses($addr); | |
dbcc3b3d | 631 | $this->fetchPhones(); |
61ee68c7 | 632 | } |
f5642983 | 633 | } |
dbcc3b3d RB |
634 | } |
635 | ||
636 | public function getAddresses($flags, $limit = null) | |
637 | { | |
638 | $this->fetchAddresses(); | |
0907501b RB |
639 | |
640 | if ($this->addresses == null) { | |
598c57f6 | 641 | return array(); |
0907501b | 642 | } |
990cb17b | 643 | return $this->addresses->get($flags, $limit); |
f5642983 FB |
644 | } |
645 | ||
598c57f6 RB |
646 | public function iterAddresses($flags, $limit = null) |
647 | { | |
648 | return PlIteratorUtils::fromArray($this->getAddresses($flags, $limit), 1, true); | |
649 | } | |
650 | ||
f5642983 FB |
651 | public function getMainAddress() |
652 | { | |
b0ffc881 RB |
653 | $main = $this->getAddresses(self::ADDRESS_MAIN); |
654 | $perso = $this->getAddresses(self::ADDRESS_PERSO); | |
655 | ||
656 | if (count($main)) { | |
657 | return array_pop($main); | |
658 | } else if (count($perso)) { | |
659 | return array_pop($perso); | |
f5642983 | 660 | } else { |
b0ffc881 | 661 | return null; |
f5642983 FB |
662 | } |
663 | } | |
3e53a496 | 664 | |
bdef0d33 RB |
665 | /* Phones |
666 | */ | |
667 | private $phones = null; | |
668 | public function setPhones(ProfilePhones $phones) | |
669 | { | |
670 | $this->phones = $phones; | |
671 | $this->consolidateFields(); | |
672 | } | |
673 | ||
dbcc3b3d | 674 | private function fetchPhones() |
bdef0d33 | 675 | { |
1f8250e4 | 676 | if ($this->phones == null && !$this->fetched(self::FETCH_PHONES)) { |
56721636 PC |
677 | $phones = $this->getProfileField(self::FETCH_PHONES); |
678 | if (isset($phones)) { | |
679 | $this->setPhones($phones); | |
680 | } | |
bdef0d33 | 681 | } |
dbcc3b3d | 682 | } |
bdef0d33 | 683 | |
dbcc3b3d RB |
684 | public function getPhones($flags, $limit = null) |
685 | { | |
686 | $this->fetchPhones(); | |
bdef0d33 | 687 | if ($this->phones == null) { |
598c57f6 | 688 | return array(); |
bdef0d33 RB |
689 | } |
690 | return $this->phones->get($flags, $limit); | |
691 | } | |
4bc5b8f0 FB |
692 | |
693 | /* Educations | |
694 | */ | |
d4d395bb | 695 | private $educations = null; |
a060e1c3 RB |
696 | public function setEducations(ProfileEducation $edu) |
697 | { | |
698 | $this->educations = $edu; | |
699 | } | |
700 | ||
4bc5b8f0 FB |
701 | public function getEducations($flags, $limit = null) |
702 | { | |
1f8250e4 | 703 | if ($this->educations == null && !$this->fetched(self::FETCH_EDU)) { |
67972120 SJ |
704 | $educations = $this->getProfileField(self::FETCH_EDU); |
705 | if ($educations) { | |
706 | $this->setEducations($educations); | |
707 | } | |
d4d395bb | 708 | } |
0907501b RB |
709 | |
710 | if ($this->educations == null) { | |
598c57f6 | 711 | return array(); |
0907501b | 712 | } |
a060e1c3 | 713 | return $this->educations->get($flags, $limit); |
4bc5b8f0 FB |
714 | } |
715 | ||
716 | public function getExtraEducations($limit = null) | |
717 | { | |
718 | return $this->getEducations(self::EDUCATION_EXTRA, $limit); | |
719 | } | |
720 | ||
0396c259 RB |
721 | /* Corps |
722 | */ | |
723 | private $corps = null; | |
724 | public function setCorps(ProfileCorps $corps) | |
725 | { | |
726 | $this->corps = $corps; | |
727 | } | |
728 | ||
729 | public function getCorps() | |
730 | { | |
1f8250e4 | 731 | if ($this->corps == null && !$this->fetched(self::FETCH_CORPS)) { |
67972120 SJ |
732 | $corps = $this->getProfileField(self::FETCH_CORPS); |
733 | if ($corps) { | |
734 | $this->setCorps($corps); | |
735 | } | |
0396c259 RB |
736 | } |
737 | return $this->corps; | |
738 | } | |
4bc5b8f0 | 739 | |
bc0276a2 NI |
740 | /** |
741 | * Retrieve the name of the corps which has been done. | |
742 | * | |
743 | * Note: this function first tries getCorps(), and if this field is blank | |
744 | * tries to find an education which degree is "Corps". | |
745 | * | |
746 | * Returns an empty string if nothing has been found. | |
747 | */ | |
748 | public function getCorpsName() | |
749 | { | |
750 | $corps = $this->getCorps(); | |
751 | if ($corps && $corps->current) { | |
752 | $corpsList = DirEnum::getOptions(DirEnum::CURRENTCORPS); | |
753 | return $corpsList[$corps->current]; | |
754 | } | |
755 | ||
756 | foreach ($this->getExtraEducations() as $edu) { | |
757 | if (!strcasecmp($edu->degree, 'Corps')) { | |
758 | if ($edu->school_short) { | |
759 | return $edu->school_short; | |
760 | } elseif ($edu->school) { | |
761 | return $edu->school; | |
762 | } | |
763 | } | |
764 | } | |
765 | return ''; | |
766 | } | |
767 | ||
04a94b1d FB |
768 | /** Networking |
769 | */ | |
d4d395bb RB |
770 | private $networks = null; |
771 | public function setNetworking(ProfileNetworking $nw) | |
772 | { | |
773 | $this->networks = $nw; | |
774 | } | |
04a94b1d FB |
775 | |
776 | public function getNetworking($flags, $limit = null) | |
777 | { | |
1f8250e4 | 778 | if ($this->networks == null && !$this->fetched(self::FETCH_NETWORKING)) { |
dad85695 FB |
779 | $nw = $this->getProfileField(self::FETCH_NETWORKING); |
780 | if ($nw) { | |
781 | $this->setNetworking($nw); | |
782 | } | |
04a94b1d | 783 | } |
0907501b | 784 | if ($this->networks == null) { |
598c57f6 | 785 | return array(); |
0907501b | 786 | } |
d4d395bb | 787 | return $this->networks->get($flags, $limit); |
04a94b1d FB |
788 | } |
789 | ||
790 | public function getWebSite() | |
791 | { | |
792 | $site = $this->getNetworking(self::NETWORKING_WEB, 1); | |
598c57f6 | 793 | if (count($site) != 1) { |
04a94b1d FB |
794 | return null; |
795 | } | |
598c57f6 | 796 | $site = array_pop($site); |
dad85695 | 797 | return $site; |
04a94b1d FB |
798 | } |
799 | ||
800 | ||
e718bd18 FB |
801 | /** Jobs |
802 | */ | |
949fc736 RB |
803 | private $jobs = null; |
804 | public function setJobs(ProfileJobs $jobs) | |
805 | { | |
806 | $this->jobs = $jobs; | |
807 | $this->consolidateFields(); | |
808 | } | |
e718bd18 | 809 | |
dbcc3b3d | 810 | private function fetchJobs() |
e718bd18 | 811 | { |
1f8250e4 | 812 | if ($this->jobs == null && !$this->fetched(self::FETCH_JOBS)) { |
61ee68c7 FB |
813 | $jobs = $this->getProfileField(self::FETCH_JOBS); |
814 | if ($jobs) { | |
815 | $this->setJobs($jobs); | |
dbcc3b3d | 816 | $this->fetchAddresses(); |
61ee68c7 | 817 | } |
e718bd18 | 818 | } |
dbcc3b3d RB |
819 | } |
820 | ||
821 | public function getJobs($flags, $limit = null) | |
822 | { | |
823 | $this->fetchJobs(); | |
949fc736 | 824 | |
0907501b | 825 | if ($this->jobs == null) { |
598c57f6 | 826 | return array(); |
0907501b | 827 | } |
949fc736 | 828 | return $this->jobs->get($flags, $limit); |
e718bd18 FB |
829 | } |
830 | ||
44ec5eb5 | 831 | public function getMainJob() |
e718bd18 FB |
832 | { |
833 | $job = $this->getJobs(self::JOBS_MAIN, 1); | |
598c57f6 | 834 | if (count($job) != 1) { |
e718bd18 FB |
835 | return null; |
836 | } | |
598c57f6 | 837 | return array_pop($job); |
e718bd18 FB |
838 | } |
839 | ||
3ac45f10 PC |
840 | /** JobTerms |
841 | */ | |
842 | private $jobterms = null; | |
843 | public function setJobTerms(ProfileJobTerms $jobterms) | |
844 | { | |
845 | $this->jobterms = $jobterms; | |
846 | $this->consolidateFields(); | |
847 | } | |
848 | ||
4d1f0f6b RB |
849 | private $mentor_countries = null; |
850 | public function setMentoringCountries(ProfileMentoringCountries $countries) | |
851 | { | |
852 | $this->mentor_countries = $countries; | |
853 | } | |
854 | ||
855 | public function getMentoringCountries() | |
856 | { | |
857 | if ($this->mentor_countries == null && !$this->fetched(self::FETCH_MENTOR_COUNTRY)) { | |
67972120 SJ |
858 | $countries = $this->getProfileField(self::FETCH_MENTOR_COUNTRY); |
859 | if ($countries) { | |
860 | $this->setMentoringCountries($countries); | |
861 | } | |
4d1f0f6b RB |
862 | } |
863 | ||
864 | if ($this->mentor_countries == null) { | |
865 | return array(); | |
866 | } else { | |
867 | return $this->mentor_countries->countries; | |
868 | } | |
869 | } | |
870 | ||
3ac45f10 PC |
871 | /** List of job terms to specify mentoring */ |
872 | private $mentor_terms = null; | |
873 | /** | |
874 | * set job terms to specify mentoring | |
875 | * @param $terms a ProfileMentoringTerms object listing terms only for this profile | |
876 | */ | |
877 | public function setMentoringTerms(ProfileMentoringTerms $terms) | |
878 | { | |
879 | $this->mentor_terms = $terms; | |
880 | } | |
881 | /** | |
882 | * get all job terms that specify mentoring | |
883 | * @return an array of JobTerms objects | |
884 | */ | |
885 | public function getMentoringTerms() | |
886 | { | |
887 | if ($this->mentor_terms == null && !$this->fetched(self::FETCH_MENTOR_TERMS)) { | |
67972120 SJ |
888 | $terms = $this->getProfileField(self::FETCH_MENTOR_TERMS); |
889 | if ($terms) { | |
890 | $this->setMentoringTerms($terms); | |
891 | } | |
3ac45f10 PC |
892 | } |
893 | ||
894 | if ($this->mentor_terms == null) { | |
895 | return array(); | |
896 | } else { | |
897 | return $this->mentor_terms->get(); | |
898 | } | |
899 | } | |
900 | ||
7e0635c2 SJ |
901 | /* Skills */ |
902 | private $skills = null; | |
903 | public function setSkills(ProfileSkills $skills) | |
904 | { | |
905 | $this->skills = $skills; | |
906 | } | |
907 | public function getSkills() | |
908 | { | |
909 | if ($this->skills == null && !$this->fetched(self::FETCH_SKILL)) { | |
67972120 SJ |
910 | $skills = $this->getProfileField(self::FETCH_SKILL); |
911 | if ($skills) { | |
912 | $this->setSkills($skills); | |
913 | } | |
7e0635c2 SJ |
914 | } |
915 | ||
916 | if ($this->skills == null) { | |
917 | return array(); | |
918 | } else { | |
919 | return $this->skills->skills; | |
920 | } | |
921 | } | |
922 | ||
923 | /* Languades */ | |
924 | private $languages = null; | |
925 | public function setLanguages(ProfileLanguages $languages) | |
926 | { | |
927 | $this->languages = $languages; | |
928 | } | |
929 | public function getLanguages() | |
930 | { | |
931 | if ($this->languages == null && !$this->fetched(self::FETCH_LANGUAGE)) { | |
67972120 SJ |
932 | $languages = $this->getProfileField(self::FETCH_LANGUAGE); |
933 | if ($languages) { | |
934 | $this->setLanguages($languages); | |
935 | } | |
7e0635c2 SJ |
936 | } |
937 | ||
938 | if ($this->languages == null) { | |
939 | return array(); | |
940 | } else { | |
941 | return $this->languages->languages; | |
942 | } | |
943 | } | |
944 | ||
080632c7 RB |
945 | /** DeltaTen |
946 | */ | |
947 | ||
948 | /** Find out whether this profile may take part to the "DeltaTen" operation. | |
949 | * @param $role Which role to select ('young' or 'old') | |
950 | * @return Boolean: whether it is enabled. | |
951 | */ | |
952 | const DELTATEN_YOUNG = 'young'; | |
953 | const DELTATEN_OLD = 'old'; | |
954 | public function isDeltaTenEnabled($role) | |
955 | { | |
956 | global $globals; | |
957 | switch ($role) { | |
958 | case self::DELTATEN_YOUNG: | |
959 | return ($this->mainGrade() == UserFilter::GRADE_ING && $this->yearpromo() >= $globals->deltaten->first_promo_young); | |
960 | case self::DELTATEN_OLD: | |
961 | // Roughly compute the current promo in second year on the campus: | |
962 | // Promo 2010 is in second year between 09/2011 and 08/2012 => use 2012. | |
963 | // DeltaTen program begins around January of the second year. | |
964 | $promo_on_platal = ((int) date('Y')) - 2; | |
965 | return ($this->mainGrade() == UserFilter::GRADE_ING && $this->yearpromo() >= $globals->deltaten->first_promo_young - 10 && $this->yearpromo() <= $promo_on_platal - 10); | |
966 | default: | |
967 | Platal::assert(false, "Invalid DeltaTen role $role"); | |
968 | } | |
969 | } | |
970 | ||
971 | /** Retrieve the "Deltaten" message of the user. | |
972 | * Returns "null" if the message is empty or the user is not taking part to the | |
973 | * DeltaTen operation. | |
974 | */ | |
975 | public function getDeltatenMessage() | |
976 | { | |
977 | if ($this->isDeltaTenEnabled(self::DELTATEN_OLD)) { | |
978 | return $this->deltaten_message; | |
979 | } else { | |
980 | return null; | |
981 | } | |
982 | } | |
3ac45f10 | 983 | |
5c005b37 RB |
984 | /* Binets |
985 | */ | |
986 | public function getBinets() | |
987 | { | |
22771578 | 988 | if ($this->visibility->isVisible(Visibility::EXPORT_PRIVATE)) { |
875a88d3 RB |
989 | return XDB::fetchColumn('SELECT binet_id |
990 | FROM profile_binets | |
991 | WHERE pid = {?}', $this->id()); | |
992 | } else { | |
993 | return array(); | |
994 | } | |
5c005b37 | 995 | } |
ccf88cca | 996 | |
7f3c6bf4 TM |
997 | public function getFullBinets() |
998 | { | |
999 | if ($this->visibility->isVisible(Visibility::EXPORT_PRIVATE)) { | |
1000 | return XDB::fetchAllAssoc('SELECT binet_id, text, url | |
1001 | FROM profile_binets AS pb | |
1002 | LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id) | |
1003 | WHERE pid = {?}', $this->id()); | |
1004 | } else { | |
1005 | return array(); | |
1006 | } | |
1007 | } | |
ccf88cca | 1008 | |
97b71de5 RB |
1009 | public function getBinetsNames() |
1010 | { | |
22771578 | 1011 | if ($this->visibility->isVisible(Visibility::EXPORT_PRIVATE)) { |
875a88d3 RB |
1012 | return XDB::fetchColumn('SELECT text |
1013 | FROM profile_binets AS pb | |
1014 | LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id) | |
1015 | WHERE pb.pid = {?}', $this->id()); | |
1016 | } else { | |
1017 | return array(); | |
1018 | } | |
97b71de5 | 1019 | } |
5c005b37 | 1020 | |
c96e6bd0 VB |
1021 | /* Hobbies |
1022 | */ | |
1023 | public function getHobbies() { | |
1024 | if ($this->visibility->isVisible(Visibility::EXPORT_PRIVATE)) { | |
1025 | return XDB::fetchAllAssoc('type', 'SELECT type, GROUP_CONCAT(text) | |
1026 | FROM profile_hobby | |
1027 | WHERE pid = {?} | |
1028 | GROUP BY type', $this->id()); | |
1029 | } else { | |
1030 | return XDB::fetchAllAssoc('type', 'SELECT type, GROUP_CONCAT(text) | |
1031 | FROM profile_hobby | |
1032 | WHERE pub = \'public\' AND pid = {?} | |
1033 | GROUP BY type', $this->id()); | |
1034 | } | |
1035 | } | |
1036 | ||
26ca919b RB |
1037 | /* Medals |
1038 | */ | |
1039 | private $medals = null; | |
1040 | public function setMedals(ProfileMedals $medals) | |
1041 | { | |
1042 | $this->medals = $medals; | |
1043 | } | |
1044 | ||
1045 | public function getMedals() | |
1046 | { | |
1f8250e4 | 1047 | if ($this->medals == null && !$this->fetched(self::FETCH_MEDALS)) { |
67972120 SJ |
1048 | $medals = $this->getProfileField(self::FETCH_MEDALS); |
1049 | if ($medals) { | |
1050 | $this->setMedals($medals); | |
1051 | } | |
26ca919b RB |
1052 | } |
1053 | if ($this->medals == null) { | |
1054 | return array(); | |
1055 | } | |
1056 | return $this->medals->medals; | |
1057 | } | |
e718bd18 | 1058 | |
148af7a9 RB |
1059 | /** Sharing data with partner websites |
1060 | */ | |
1061 | private $partners_settings = null; | |
1062 | public function setPartnersSettings(ProfilePartnerSharing $partners_settings) | |
1063 | { | |
1064 | $this->partners_settings = $partners_settings; | |
1065 | } | |
1066 | ||
1067 | public function getPartnerSettings($partner_id) | |
1068 | { | |
f0f8c375 | 1069 | if ($this->partners_settings === null && !$this->fetched(self::FETCH_PARTNER)) { |
67972120 SJ |
1070 | $settings = $this->getProfileField(self::FETCH_PARTNER); |
1071 | if ($settings) { | |
1072 | $this->setPartnersSettings($settings); | |
1073 | } | |
148af7a9 | 1074 | } |
f0f8c375 | 1075 | if ($this->partners_settings === null) { |
148af7a9 RB |
1076 | return PartnerSettings::getEmpty($partner_id); |
1077 | } | |
1078 | return $this->partners_settings->get($partner_id); | |
1079 | } | |
1080 | ||
94590511 SJ |
1081 | public function compareNames($firstname, $lastname) |
1082 | { | |
1083 | $_lastname = mb_strtoupper($this->lastName()); | |
1084 | $_firstname = mb_strtoupper($this->firstName()); | |
1085 | $lastname = mb_strtoupper($lastname); | |
1086 | $firstname = mb_strtoupper($firstname); | |
1087 | ||
1088 | $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname)); | |
1089 | $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY); | |
1090 | $maxlen = 0; | |
1091 | ||
1092 | foreach ($tokens as $str) { | |
1093 | $isOk &= (strpos($_lastname, $str) !== false); | |
1094 | $maxlen = max($maxlen, strlen($str)); | |
1095 | } | |
1096 | ||
1097 | return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname))); | |
1098 | } | |
1099 | ||
aaffb7b2 FB |
1100 | /* Export to JSON |
1101 | */ | |
1102 | public function export() | |
1103 | { | |
1104 | return array( | |
1105 | 'hrpid' => $this->hrid(), | |
1106 | 'display_name' => $this->shortName(), | |
1107 | 'full_name' => $this->fullName(), | |
1108 | 'directory_name' => $this->directory_name, | |
1109 | 'promo' => $this->promo(), | |
1110 | 'year_promo' => $this->yearpromo(), | |
1111 | 'is_active' => $this->isActive(), | |
1112 | 'first_name' => $this->firstName(), | |
1113 | 'last_name' => $this->lastName(), | |
1114 | 'is_female' => $this->isFemale(), | |
1115 | ); | |
1116 | } | |
1117 | ||
22771578 | 1118 | private static function fetchProfileData(array $pids, $respect_order = true, $fields = 0x0000, $visibility = null) |
b774ddab FB |
1119 | { |
1120 | if (count($pids) == 0) { | |
7a8da8e8 | 1121 | return null; |
b774ddab | 1122 | } |
0d906109 RB |
1123 | |
1124 | if ($respect_order) { | |
1125 | $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids); | |
1126 | } else { | |
1127 | $order = ''; | |
1128 | } | |
1129 | ||
22771578 RB |
1130 | if ($visibility === null) { |
1131 | $visibility = Visibility::defaultForRead(); | |
38a86f60 | 1132 | } |
9f21bd15 | 1133 | |
3e2442cd RB |
1134 | $it = XDB::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref, |
1135 | p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex, | |
77d2ae52 RB |
1136 | IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory, |
1137 | p.last_change, p.nationality1, p.nationality2, p.nationality3, | |
38a86f60 | 1138 | IF (p.freetext_pub >= {?}, p.freetext, NULL) AS freetext, |
d718e91c | 1139 | pe.entry_year, pe.grad_year, pe.promo_year, pe.program, pe.fieldid, |
348b3844 | 1140 | IF ({?}, pse.text, NULL) AS section, |
0e1dfbad SJ |
1141 | ppn.firstname_main AS firstname, ppn.lastname_main AS lastname, IF ({?}, pn.name, NULL) AS nickname, |
1142 | IF (ppn.firstname_ordinary = \'\', ppn.firstname_main, ppn.firstname_ordinary) AS firstname_ordinary, | |
8fc92b60 | 1143 | IF (ppn.lastname_ordinary = \'\', ppn.lastname_main, ppn.lastname_ordinary) AS lastname_ordinary, |
09e54905 | 1144 | pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name, |
27492fde | 1145 | pd.directory_name, pd.public_name, pd.private_name, pd.sort_name, |
38a86f60 | 1146 | IF (pp.pub >= {?}, pp.display_tel, NULL) AS mobile, |
db492b02 | 1147 | (ph.pub >= {?} AND ph.attach IS NOT NULL) AS has_photo, ph.pub as photo_pub, |
7988f7d6 | 1148 | ph.x AS photo_width, ph.y AS photo_height, |
9f21bd15 | 1149 | p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old, |
bdef0d33 | 1150 | pm.expertise AS mentor_expertise, |
080632c7 | 1151 | IF ({?}, pdt.message, NULL) AS deltaten_message, |
9f21bd15 RB |
1152 | ap.uid AS owner_id |
1153 | FROM profiles AS p | |
1154 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) | |
1155 | INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) | |
97b71de5 | 1156 | LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section) |
0e1dfbad SJ |
1157 | INNER JOIN profile_public_names AS ppn ON (ppn.pid = p.pid) |
1158 | LEFT JOIN profile_private_names AS pn ON (pn.pid = p.pid AND type = \'nickname\') | |
9f21bd15 RB |
1159 | LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\') |
1160 | LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid) | |
bdef0d33 | 1161 | LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid) |
080632c7 | 1162 | LEFT JOIN profile_deltaten AS pdt ON (pdt.pid = p.pid) |
9f21bd15 | 1163 | LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
3e2442cd | 1164 | WHERE p.pid IN {?} |
9f21bd15 | 1165 | GROUP BY p.pid |
3e2442cd | 1166 | ' . $order, |
22771578 | 1167 | $visibility->isVisible(Visibility::EXPORT_PRIVATE), // CV |
38a86f60 | 1168 | $visibility->level(), // freetext |
22771578 RB |
1169 | $visibility->isVisible(Visibility::EXPORT_PRIVATE), // section |
1170 | $visibility->isVisible(Visibility::EXPORT_PRIVATE), // nickname | |
38a86f60 RB |
1171 | $visibility->level(), // mobile |
1172 | $visibility->level(), // photo | |
22771578 | 1173 | $visibility->isVisible(Visibility::EXPORT_PRIVATE), // deltaten_message |
3e2442cd RB |
1174 | $pids |
1175 | ); | |
31ef12ef | 1176 | return new ProfileIterator($it, $pids, $fields, $visibility); |
b774ddab FB |
1177 | } |
1178 | ||
1179 | public static function getPID($login) | |
1180 | { | |
1181 | if ($login instanceof PlUser) { | |
1182 | return XDB::fetchOneCell('SELECT pid | |
1183 | FROM account_profiles | |
1184 | WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
1185 | $login->id()); | |
9f21bd15 | 1186 | } else if (ctype_digit($login)) { |
b774ddab FB |
1187 | return XDB::fetchOneCell('SELECT pid |
1188 | FROM profiles | |
1189 | WHERE pid = {?}', $login); | |
1190 | } else { | |
1191 | return XDB::fetchOneCell('SELECT pid | |
1192 | FROM profiles | |
1193 | WHERE hrpid = {?}', $login); | |
1194 | } | |
1195 | } | |
1196 | ||
0d906109 RB |
1197 | public static function getPIDsFromUIDs($uids, $respect_order = true) |
1198 | { | |
1199 | if ($respect_order) { | |
1200 | $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids); | |
1201 | } else { | |
1202 | $order = ''; | |
1203 | } | |
1204 | return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid | |
1205 | FROM account_profiles AS ap | |
1206 | WHERE FIND_IN_SET(\'owner\', ap.perms) | |
9f21bd15 RB |
1207 | AND ap.uid IN ' . XDB::formatArray($uids) .' |
1208 | ' . $order); | |
0d906109 | 1209 | } |
b774ddab | 1210 | |
e7b93962 FB |
1211 | /** Return the profile associated with the given login. |
1212 | */ | |
22771578 | 1213 | public static function get($login, $fields = 0x0000, $visibility = null) |
a3118782 | 1214 | { |
22771578 RB |
1215 | if ($visibility === null) { |
1216 | $visibility = Visibility::defaultForRead(); | |
38a86f60 RB |
1217 | } |
1218 | ||
641f2115 | 1219 | if (is_array($login)) { |
38a86f60 | 1220 | $pf = new Profile($login, $visibility); |
1a9affb7 | 1221 | return $pf; |
641f2115 | 1222 | } |
b774ddab FB |
1223 | $pid = self::getPID($login); |
1224 | if (!is_null($pid)) { | |
9f21bd15 | 1225 | $it = self::iterOverPIDs(array($pid), false, $fields, $visibility); |
0d906109 | 1226 | return $it->next(); |
b774ddab | 1227 | } else { |
efe597c5 FB |
1228 | /* Let say we can identify a profile using the identifiers of its owner. |
1229 | */ | |
455ea0c9 FB |
1230 | if (!($login instanceof PlUser)) { |
1231 | $user = User::getSilent($login); | |
1232 | if ($user && $user->hasProfile()) { | |
22771578 | 1233 | return $user->profile(false, $fields, $visibility); |
455ea0c9 | 1234 | } |
efe597c5 | 1235 | } |
3e53a496 | 1236 | return null; |
e7b93962 FB |
1237 | } |
1238 | } | |
a3118782 | 1239 | |
22771578 | 1240 | public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null) |
0d906109 | 1241 | { |
9f21bd15 | 1242 | return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility); |
0d906109 RB |
1243 | } |
1244 | ||
22771578 | 1245 | public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null) |
0d906109 | 1246 | { |
9f21bd15 | 1247 | return self::fetchProfileData($pids, $respect_order, $fields, $visibility); |
0d906109 RB |
1248 | } |
1249 | ||
b774ddab FB |
1250 | /** Return profiles for the list of pids. |
1251 | */ | |
22771578 | 1252 | public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null) |
b774ddab FB |
1253 | { |
1254 | if (count($pids) == 0) { | |
1255 | return array(); | |
1256 | } | |
9f21bd15 | 1257 | $it = self::iterOverPIDs($pids, true, $fields, $visibility); |
b774ddab | 1258 | $profiles = array(); |
0d906109 RB |
1259 | while ($p = $it->next()) { |
1260 | $profiles[$p->id()] = $p; | |
b774ddab FB |
1261 | } |
1262 | return $profiles; | |
1263 | } | |
1264 | ||
1265 | /** Return profiles for uids. | |
1266 | */ | |
22771578 | 1267 | public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null) |
b774ddab FB |
1268 | { |
1269 | if (count($uids) == 0) { | |
1270 | return array(); | |
1271 | } | |
e4f1d258 | 1272 | return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility); |
b774ddab FB |
1273 | } |
1274 | ||
913a4e90 RB |
1275 | public static function isDisplayName($name) |
1276 | { | |
1277 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
1278 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
1279 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
1280 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
1281 | } | |
1282 | ||
a9ef52c9 RB |
1283 | /** Returns the closest "accounts only" name type for $name |
1284 | */ | |
1285 | public static function getAccountEquivalentName($name) | |
1286 | { | |
b0788bf4 SJ |
1287 | switch ($name) { |
1288 | case self::DN_DIRECTORY: | |
a9ef52c9 | 1289 | return 'directory_name'; |
b0788bf4 SJ |
1290 | case self::DN_SORT: |
1291 | return 'sort_name'; | |
1292 | case self::DN_FULL: | |
1293 | case self::DN_PUBLIC: | |
1294 | case self::DN_PRIVATE: | |
1295 | case self::DN_SHORT: | |
a9ef52c9 | 1296 | return 'full_name'; |
b0788bf4 SJ |
1297 | case self::DN_YOURSELF: |
1298 | return 'display_name'; | |
1299 | default: | |
a9ef52c9 RB |
1300 | return 'display_name'; |
1301 | } | |
1302 | } | |
1303 | ||
bedc4295 | 1304 | public static function rebuildSearchTokens($pids, $transaction = true) |
726eaf7a | 1305 | { |
0e1dfbad | 1306 | require_once 'name.func.inc.php'; |
5acd53a9 FB |
1307 | if (!is_array($pids)) { |
1308 | $pids = array($pids); | |
1309 | } | |
0e1dfbad SJ |
1310 | $keys = XDB::iterator("(SELECT pid, name, type, IF(type = 'nickname', 2, 1) AS score, '' AS public |
1311 | FROM profile_private_names | |
1312 | WHERE pid IN {?}) | |
1313 | UNION | |
1314 | (SELECT pid, lastname_main, 'lastname' AS type, 10 AS score, 'public' AS public | |
1315 | FROM profile_public_names | |
1316 | WHERE lastname_main != '' AND pid IN {?}) | |
1317 | UNION | |
1318 | (SELECT pid, lastname_marital, 'lastname' AS type, 10 AS score, 'public' AS public | |
1319 | FROM profile_public_names | |
1320 | WHERE lastname_marital != '' AND pid IN {?}) | |
1321 | UNION | |
1322 | (SELECT pid, lastname_ordinary, 'lastname' AS type, 10 AS score, 'public' AS public | |
1323 | FROM profile_public_names | |
1324 | WHERE lastname_ordinary != '' AND pid IN {?}) | |
1325 | UNION | |
1326 | (SELECT pid, firstname_main, 'firstname' AS type, 10 AS score, 'public' AS public | |
1327 | FROM profile_public_names | |
1328 | WHERE firstname_main != '' AND pid IN {?}) | |
1329 | UNION | |
1330 | (SELECT pid, firstname_ordinary, 'firstname' AS type, 10 AS score, 'public' AS public | |
1331 | FROM profile_public_names | |
1332 | WHERE firstname_ordinary != '' AND pid IN {?}) | |
1333 | UNION | |
1334 | (SELECT pid, pseudonym, 'nickname' AS type, 10 AS score, 'public' AS public | |
1335 | FROM profile_public_names | |
1336 | WHERE pseudonym != '' AND pid IN {?})", | |
1337 | $pids, $pids, $pids, $pids, $pids, $pids, $pids); | |
5acd53a9 | 1338 | $names = array(); |
2f62eba7 | 1339 | while ($key = $keys->next()) { |
726eaf7a SJ |
1340 | if ($key['name'] == '') { |
1341 | continue; | |
1342 | } | |
0e1dfbad | 1343 | $pid = $key['pid']; |
5ed1cce8 | 1344 | $toks = split_name_for_search($key['name']); |
80bc45c2 FB |
1345 | $toks = array_reverse($toks); |
1346 | ||
1347 | /* Split the score between the tokens to avoid the user to be over-rated. | |
1348 | * Let says my user name is "Machin-Truc Bidule" and I also have a user named | |
1349 | * 'Machin Truc'. Distributing the score force "Machin Truc" to be displayed | |
1350 | * before "Machin-Truc" for both "Machin Truc" and "Machin" searches. | |
1351 | */ | |
1352 | $eltScore = ceil(((float)$key['score'])/((float)count($toks))); | |
726eaf7a | 1353 | $token = ''; |
80bc45c2 FB |
1354 | foreach ($toks as $tok) { |
1355 | $token = $tok . $token; | |
5600b2fe | 1356 | $names["$pid-$token"] = XDB::format('({?}, {?}, {?}, {?}, {?}, {?})', |
5acd53a9 | 1357 | $token, $pid, soundex_fr($token), |
0e1dfbad | 1358 | $eltScore, $key['public'], $key['type']); |
726eaf7a SJ |
1359 | } |
1360 | } | |
bedc4295 FB |
1361 | if ($transaction) { |
1362 | XDB::startTransaction(); | |
1363 | } | |
5acd53a9 FB |
1364 | XDB::execute('DELETE FROM search_name |
1365 | WHERE pid IN {?}', | |
1366 | $pids); | |
1367 | if (count($names) > 0) { | |
5600b2fe | 1368 | XDB::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags, general_type) |
80bc45c2 | 1369 | VALUES ' . implode(', ', $names)); |
5acd53a9 | 1370 | } |
bedc4295 FB |
1371 | if ($transaction) { |
1372 | XDB::commit(); | |
1373 | } | |
69b46857 SJ |
1374 | } |
1375 | ||
1376 | /** The school identifier consists of 6 digits. The first 3 represent the | |
1377 | * promotion entry year. The last 3 indicate the student's rank. | |
aab2ffdd | 1378 | * |
69b46857 SJ |
1379 | * Our identifier consists of 8 digits and both half have the same role. |
1380 | * This enables us to deal with bigger promotions and with a wider range | |
1381 | * of promotions. | |
1382 | * | |
1383 | * getSchoolId returns a school identifier given one of ours. | |
1384 | * getXorgId returns a X.org identifier given a school identifier. | |
1385 | */ | |
1386 | public static function getSchoolId($xorgId) | |
1387 | { | |
1388 | if (!preg_match('/^[0-9]{8}$/', $xorgId)) { | |
1389 | return null; | |
1390 | } | |
1391 | ||
1392 | $year = intval(substr($xorgId, 0, 4)); | |
1393 | $rank = intval(substr($xorgId, 5, 3)); | |
1394 | if ($year < 1996) { | |
1395 | return null; | |
1396 | } elseif ($year < 2000) { | |
1397 | $year = intval(substr(1900 - $year, 1, 3)); | |
1398 | return sprintf('%02u0%03u', $year, $rank); | |
1399 | } else { | |
1400 | $year = intval(substr(1900 - $year, 1, 3)); | |
1401 | return sprintf('%03u%03u', $year, $rank); | |
1402 | } | |
1403 | } | |
726eaf7a | 1404 | |
69b46857 SJ |
1405 | public static function getXorgId($schoolId) |
1406 | { | |
94590511 | 1407 | if (!preg_match('/^[0-9]{6}$/', $schoolId)) { |
a292484d SJ |
1408 | return null; |
1409 | } | |
1410 | ||
69b46857 SJ |
1411 | $year = intval(substr($schoolId, 0, 3)); |
1412 | $rank = intval(substr($schoolId, 3, 3)); | |
726eaf7a | 1413 | |
69b46857 SJ |
1414 | if ($year > 200) { |
1415 | $year /= 10; | |
1416 | } | |
1417 | if ($year < 96) { | |
1418 | return null; | |
1419 | } else { | |
1420 | return sprintf('%04u%04u', 1900 + $year, $rank); | |
1421 | } | |
726eaf7a | 1422 | } |
e7b93962 FB |
1423 | } |
1424 | ||
31ef12ef RB |
1425 | |
1426 | /** Iterator over a set of Profiles | |
1427 | */ | |
1428 | class ProfileIterator implements PlIterator | |
9f21bd15 RB |
1429 | { |
1430 | private $iterator = null; | |
1431 | private $fields; | |
1a9affb7 | 1432 | private $visibility; |
9f21bd15 | 1433 | |
3ac45f10 PC |
1434 | const FETCH_ALL = 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS |
1435 | ||
22771578 | 1436 | public function __construct(PlIterator $it, array $pids, $fields = 0x0000, $visibility = null) |
9f21bd15 RB |
1437 | { |
1438 | require_once 'profilefields.inc.php'; | |
1a9affb7 | 1439 | |
22771578 RB |
1440 | if ($visibility === null) { |
1441 | $visibility = Visibility::defaultForRead(); | |
1a9affb7 RB |
1442 | } |
1443 | ||
9f21bd15 | 1444 | $this->fields = $fields; |
1a9affb7 | 1445 | $this->visibility = $visibility; |
9f21bd15 RB |
1446 | |
1447 | $subits = array(); | |
1448 | $callbacks = array(); | |
1449 | ||
1450 | $subits[0] = $it; | |
1451 | $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid'); | |
1452 | $cb = PlIteratorUtils::objectPropertyCallback('pid'); | |
1453 | ||
3ac45f10 PC |
1454 | $fields = $fields & self::FETCH_ALL; |
1455 | for ($field = 1; $field < $fields; $field *= 2) { | |
1456 | if (($fields & $field) ) { | |
1457 | $callbacks[$field] = $cb; | |
1458 | $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility); | |
1459 | } | |
9f21bd15 RB |
1460 | } |
1461 | ||
9f21bd15 RB |
1462 | $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0); |
1463 | } | |
1464 | ||
1f8250e4 | 1465 | private function hasData($field, $vals) |
9f21bd15 | 1466 | { |
1f8250e4 | 1467 | return ($this->fields & $field) && ($vals[$field] != null); |
9f21bd15 RB |
1468 | } |
1469 | ||
1470 | private function fillProfile(array $vals) | |
1471 | { | |
22771578 | 1472 | $pf = Profile::get($vals[0], 0x0, $this->visibility); |
1f8250e4 | 1473 | $pf->setFetchedFields($this->fields); |
1a9affb7 | 1474 | |
8cf886dc RB |
1475 | if ($this->hasData(Profile::FETCH_PHONES, $vals)) { |
1476 | $pf->setPhones($vals[Profile::FETCH_PHONES]); | |
9f21bd15 | 1477 | } |
8cf886dc RB |
1478 | if ($this->hasData(Profile::FETCH_ADDRESSES, $vals)) { |
1479 | $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]); | |
1480 | } | |
1481 | if ($this->hasData(Profile::FETCH_JOBS, $vals)) { | |
1482 | $pf->setJobs($vals[Profile::FETCH_JOBS]); | |
1483 | } | |
3ac45f10 PC |
1484 | if ($this->hasData(Profile::FETCH_JOB_TERMS, $vals)) { |
1485 | $pf->setJobTerms($vals[Profile::FETCH_JOB_TERMS]); | |
1486 | } | |
8cf886dc RB |
1487 | |
1488 | if ($this->hasData(Profile::FETCH_CORPS, $vals)) { | |
9f21bd15 RB |
1489 | $pf->setCorps($vals[Profile::FETCH_CORPS]); |
1490 | } | |
8cf886dc | 1491 | if ($this->hasData(Profile::FETCH_EDU, $vals)) { |
26ca919b | 1492 | $pf->setEducations($vals[Profile::FETCH_EDU]); |
9f21bd15 | 1493 | } |
8cf886dc | 1494 | if ($this->hasData(Profile::FETCH_MEDALS, $vals)) { |
9f21bd15 RB |
1495 | $pf->setMedals($vals[Profile::FETCH_MEDALS]); |
1496 | } | |
8cf886dc | 1497 | if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) { |
9f21bd15 RB |
1498 | $pf->setNetworking($vals[Profile::FETCH_NETWORKING]); |
1499 | } | |
9f21bd15 RB |
1500 | |
1501 | return $pf; | |
1502 | } | |
1503 | ||
1504 | public function next() | |
1505 | { | |
1506 | $vals = $this->iterator->next(); | |
1507 | if ($vals == null) { | |
1508 | return null; | |
1509 | } | |
1510 | return $this->fillProfile($vals); | |
1511 | } | |
1512 | ||
1513 | public function first() | |
1514 | { | |
1515 | return $this->iterator->first(); | |
1516 | } | |
1517 | ||
1518 | public function last() | |
1519 | { | |
1520 | return $this->iterator->last(); | |
1521 | } | |
1522 | ||
1523 | public function total() | |
1524 | { | |
1525 | return $this->iterator->total(); | |
1526 | } | |
1527 | } | |
1528 | ||
448c8cdc | 1529 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: |
e7b93962 | 1530 | ?> |