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