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