2 /***************************************************************************
3 * Copyright (C) 2003-2014 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class Profile
implements PlExportable
26 const LASTNAME
= 'lastname';
27 const FIRSTNAME
= 'firstname';
28 const NICKNAME
= 'nickname';
29 const PSEUDONYM
= 'pseudonym';
32 const VN_MARITAL
= 'marital';
33 const VN_ORDINARY
= 'ordinary';
34 const VN_OTHER
= 'other';
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';
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';
51 static public $cycles = array(
52 self
::DEGREE_X
=> 'polytechnicien',
53 self
::DEGREE_M
=> 'master',
54 self
::DEGREE_D
=> 'docteur'
56 static public $cycle_prefixes = array(
57 self
::DEGREE_X
=> 'X',
58 self
::DEGREE_M
=> 'M',
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
)
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;
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;
79 const JOBS_MAIN
= 0x00001000;
80 const JOBS_ALL
= 0x00002000;
81 const JOBS_FINISHED
= 0x00004000;
82 const JOBS_CURRENT
= 0x00008000;
84 const NETWORKING_ALL
= 0x00070000;
85 const NETWORKING_WEB
= 0x00010000;
86 const NETWORKING_IM
= 0x00020000;
87 const NETWORKING_SOCIAL
= 0x00040000;
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;
95 const PHONE_TYPE_FAX
= 0x01000000;
96 const PHONE_TYPE_FIXED
= 0x02000000;
97 const PHONE_TYPE_MOBILE
= 0x04000000;
98 const PHONE_TYPE_ANY
= 0x07000000;
100 const PHONE_ANY
= 0x07F00000;
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;
108 const FETCH_MENTOR_COUNTRY
= 0x000080;
109 const FETCH_PHONES
= 0x000100;
110 const FETCH_JOB_TERMS
= 0x000200;
111 const FETCH_MENTOR_TERMS
= 0x000400;
112 const FETCH_DELTATEN
= 0x000800;
113 const FETCH_PARTNER
= 0x001000;
114 const FETCH_SKILL
= 0x002000;
115 const FETCH_LANGUAGE
= 0x004000;
117 const FETCH_MINIFICHES
= 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
119 const FETCH_ALL
= 0x007FFF; // OR of FETCH_*
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',
131 'main_edus' => 'Formations à l\'X',
132 'promo' => 'Promotion de sortie',
133 'birthdate' => 'Date de naissance',
134 'yourself' => 'Nom affiché',
135 'freetext' => 'Commentaire',
136 'freetext_pub' => 'Affichage du commentaire',
137 'axfreetext' => 'Commentaire AX',
138 'photo' => 'Photographie',
139 'photo_pub' => 'Affichage de la photographie',
140 'addresses' => 'Adresses',
144 'section' => 'Section',
145 'binets' => 'Binets',
146 'medals' => 'Décorations',
147 'medals_pub' => 'Affichage des décorations',
148 'competences' => 'Compétences',
149 'langues' => 'Langues',
150 'expertise' => 'Expertises (mentoring)',
151 'terms' => 'Compétences (mentoring)',
152 'countries' => 'Pays (mentoring)',
153 'deltaten' => 'Opération N N-10',
156 private $fetched_fields = 0x000000;
161 private $owner_fetched = false
;
162 private $data = array();
164 private $visibility = null
;
167 private function __construct(array $data, Visibility
$visibility)
170 $this->pid
= $this->data
['pid'];
171 $this->hrpid
= $this->data
['hrpid'];
172 $this->visibility
= $visibility;
175 public function __destruct()
177 // Need to delete objects allocated by the profile
178 // http://www.php.net/manual/en/function.unset.php#98692
187 public function hrid()
192 public function owner()
194 if ($this->owner
== null
&& !$this->owner_fetched
) {
195 $this->owner_fetched
= true
;
196 $this->owner
= User
::getSilent($this);
201 public function isActive()
203 if ($this->owner()) {
204 return $this->owner
->isActive();
209 public function promo($details = false
)
211 if ($details && ($this->program ||
$this->fieldid
)) {
213 if ($this->program
) {
214 $text[] = $this->program
;
216 if ($this->fieldid
) {
217 $fieldsList = DirEnum
::getOptions(DirEnum
::EDUFIELDS
);
218 $text[] = $fieldsList[$this->fieldid
];
220 return $this->promo
. ' (' . implode(', ', $text) . ')';
226 public function yearpromo()
228 return $this->promo_year
;
231 /** Check if user is an orange (associated with several promos)
233 public function isMultiPromo()
235 return $this->grad_year
!= $this->entry_year +
$this->mainEducationDuration();
238 /** Returns an array with all associated promo years.
240 public function yearspromo()
243 $d = -$this->deltaPromoToGradYear();
244 for ($g = $this->entry_year +
$this->mainEducationDuration(); $g <= $this->grad_year
; ++
$g) {
250 public function mainEducation()
252 if (empty($this->promo
)) {
255 return $this->promo
{0};
259 public function mainGrade()
261 switch ($this->mainEducation()) {
263 return UserFilter
::GRADE_ING
;
265 return UserFilter
::GRADE_MST
;
267 return UserFilter
::GRADE_PHD
;
273 public function mainEducationDuration()
275 switch ($this->mainEducation()) {
287 public static function educationDuration($education)
289 switch ($education) {
301 /** Number of years between the promotion year until the
302 * graduation year. In standard schools it's 0, but for
303 * Polytechnique the promo year is the entry year.
305 public function deltaPromoToGradYear()
307 if ($this->mainEducation() == 'X') {
308 return $this->mainEducationDuration();
313 // Returns the profile's color.
314 public function promoColor()
316 switch ($this->mainEducation()) {
318 if (($this->yearpromo() %
2) === 0) {
332 // Returns younger/older promotion year for a given education.
333 static public function extremePromotions($education)
335 return XDB
::fetchOneRow("SELECT MIN(pe.promo_year) AS min, MAX(pe.promo_year) AS max
336 FROM profile_education AS pe
337 INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
338 WHERE pede.degree = {?} AND FIND_IN_SET('primary', pe.flags)",
342 /** Print a name with the given formatting:
350 public function name($format)
352 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
353 array($this->isFemale() ?
'•' : '',
354 $this->firstName(), $this->lastName(),
355 $this->fullName(), $this->shortName(),
356 $this->promo()), $format);
359 public function fullName($with_promo = false
)
362 return $this->full_name
. ' (' . $this->promo
. ')';
364 return $this->full_name
;
367 public function shortName($with_promo = false
)
370 return $this->short_name
. ' (' . $this->promo
. ')';
372 return $this->short_name
;
375 public function firstName()
377 return $this->firstname_ordinary
;
380 public function firstNames()
382 return $this->nameVariants(self
::FIRSTNAME
);
385 public function lastName()
387 return $this->lastname_ordinary
;
390 public function lastNames()
392 return $this->nameVariants(self
::LASTNAME
);
395 public function isFemale()
397 return $this->sex
== PlUser
::GENDER_FEMALE
;
400 public function isDead()
402 return ($this->deathdate
!= null
);
405 public function displayEmail()
408 if ($o != null
&& $this->isVisible(Visibility
::EXPORT_PRIVATE
)) {
409 return $o->bestEmail();
411 return $this->email_directory
;
415 public function data()
421 private function nameVariants($type)
423 $vals = array($this->$type);
424 foreach (self
::$name_variants[$type] as $var) {
425 $vartype = $type . '_' . $var;
426 $varname = $this->$vartype;
427 if ($varname != null
&& $varname != "") {
431 return array_unique($vals);
434 public function nationalities()
437 $nationalities = DirEnum
::getOptions(DirEnum
::NATIONALITIES
);
438 if ($this->nationality1
) {
439 $nats[$this->nationality1
] = $nationalities[$this->nationality1
];
441 if ($this->nationality2
) {
442 $nats[$this->nationality2
] = $nationalities[$this->nationality2
];
444 if ($this->nationality3
) {
445 $nats[$this->nationality3
] = $nationalities[$this->nationality3
];
450 public function __get($name)
452 if (property_exists($this, $name)) {
456 if (isset($this->data
[$name])) {
457 return $this->data
[$name];
463 public function __isset($name)
465 return property_exists($this, $name) ||
isset($this->data
[$name]);
468 public function __unset($name)
470 if (property_exists($this, $name)) {
473 unset($this->data
[$name]);
480 * *always deletes in: profile_addresses, profile_binets, profile_deltaten,
481 * profile_job, profile_langskills, profile_mentor, profile_networking,
482 * profile_partnersharing_settings, profile_phones, profile_skills,
484 * *always keeps in: profile_corps, profile_display, profile_education,
485 * profile_medals, profile_*_names, profile_photos, search_name
486 * *modifies: profiles
488 public function clear()
491 'profile_job', 'profile_langskills', 'profile_mentor',
492 'profile_networking', 'profile_skills', 'watch_profile',
493 'profile_phones', 'profile_addresses', 'profile_binets',
494 'profile_deltaten', 'profile_partnersharing_settings');
496 foreach ($tables as $t) {
497 XDB
::execute('DELETE FROM ' . $t . '
502 XDB
::execute("UPDATE profiles
503 SET cv = NULL, freetext = NULL, freetext_pub = 'private', axfreetext = NULL,
504 medals_pub = 'private', alias_pub = 'hidden',
505 email_directory = NULL
510 /** Determine whether an item with visibility $visibility can be displayed
511 * with the current level of visibility of the profile
512 * @param $visibility The level of visibility to be checked
514 public function isVisible($visibility)
516 return $this->visibility
->isVisible($visibility);
519 /** Stores the list of fields which have already been fetched for this Profile
521 public function setFetchedFields($fields)
523 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
524 Platal
::page()->kill("Invalid fetched fields: $fields");
527 $this->fetched_fields
= $fields;
530 /** Have we already fetched this field ?
532 private function fetched($field)
534 if (!array_key_exists($field, ProfileField
::$fields)) {
535 Platal
::page()->kill("Invalid field: $field");
538 return ($this->fetched_fields
& $field);
541 /** If not already done, fetches data for the given field
542 * @param $field One of the Profile::FETCH_*
543 * @return A ProfileField, or null
545 private function getProfileField($field)
547 if (!array_key_exists($field, ProfileField
::$fields)) {
548 Platal
::page()->kill("Invalid field: $field");
550 if ($this->fetched($field)) {
553 $this->fetched_fields
= $this->fetched_fields |
$field;
556 $cls = ProfileField
::$fields[$field];
558 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
561 /** Consolidates internal data (addresses, phones, jobs)
563 private function consolidateFields()
565 // Link phones to addresses
566 if ($this->phones
!= null
) {
567 if ($this->addresses
!= null
) {
568 $this->addresses
->addPhones($this->phones
);
571 if ($this->jobs
!= null
) {
572 $this->jobs
->addPhones($this->phones
);
576 // Link addresses to jobs
577 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
578 $this->jobs
->addAddresses($this->addresses
);
581 // Link jobterms to jobs
582 if ($this->jobs
!= null
&& $this->jobterms
!= null
) {
583 $this->jobs
->addJobTerms($this->jobterms
);
589 private $photo = null
;
590 public function getPhoto($fallback = true
, $data = false
)
592 if ($this->has_photo
) {
593 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
594 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y, last_update
596 WHERE pid = {?}', $this->pid
);
597 $this->photo
= PlImage
::fromData($res['attach'], 'image/' .$res['attachmime'], $res['x'], $res['y'], $res['last_update']);
598 } else if ($this->photo
== null
) {
599 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
602 } else if ($fallback) {
603 if ($this->mainEducation() == 'X') {
604 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_x.png', 'image/png');
606 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_md.png', 'image/png');
613 private $addresses = null
;
614 public function setAddresses(ProfileAddresses
$addr)
616 $this->addresses
= $addr;
617 $this->consolidateFields();
620 private function fetchAddresses()
622 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
623 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
625 $this->setAddresses($addr);
626 $this->fetchPhones();
631 public function getAddresses($flags, $limit = null
)
633 $this->fetchAddresses();
635 if ($this->addresses
== null
) {
638 return $this->addresses
->get($flags, $limit);
641 public function iterAddresses($flags, $limit = null
)
643 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
646 public function getMainAddress()
648 $main = $this->getAddresses(self
::ADDRESS_MAIN
);
649 $perso = $this->getAddresses(self
::ADDRESS_PERSO
);
652 return array_pop($main);
653 } else if (count($perso)) {
654 return array_pop($perso);
662 private $phones = null
;
663 public function setPhones(ProfilePhones
$phones)
665 $this->phones
= $phones;
666 $this->consolidateFields();
669 private function fetchPhones()
671 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
672 $phones = $this->getProfileField(self
::FETCH_PHONES
);
673 if (isset($phones)) {
674 $this->setPhones($phones);
679 public function getPhones($flags, $limit = null
)
681 $this->fetchPhones();
682 if ($this->phones
== null
) {
685 return $this->phones
->get($flags, $limit);
690 private $educations = null
;
691 public function setEducations(ProfileEducation
$edu)
693 $this->educations
= $edu;
696 public function getEducations($flags, $limit = null
)
698 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
699 $educations = $this->getProfileField(self
::FETCH_EDU
);
701 $this->setEducations($educations);
705 if ($this->educations
== null
) {
708 return $this->educations
->get($flags, $limit);
711 public function getExtraEducations($limit = null
)
713 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
718 private $corps = null
;
719 public function setCorps(ProfileCorps
$corps)
721 $this->corps
= $corps;
724 public function getCorps()
726 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
727 $corps = $this->getProfileField(self
::FETCH_CORPS
);
729 $this->setCorps($corps);
736 * Retrieve the name of the corps which has been done.
738 * Note: this function first tries getCorps(), and if this field is blank
739 * tries to find an education which degree is "Corps".
741 * Returns an empty string if nothing has been found.
743 public function getCorpsName()
745 $corps = $this->getCorps();
746 if ($corps && $corps->current
) {
747 $corpsList = DirEnum
::getOptions(DirEnum
::CURRENTCORPS
);
748 return $corpsList[$corps->current
];
751 foreach ($this->getExtraEducations() as $edu) {
752 if (!strcasecmp($edu->degree
, 'Corps')) {
753 if ($edu->school_short
) {
754 return $edu->school_short
;
755 } elseif ($edu->school
) {
765 private $networks = null
;
766 public function setNetworking(ProfileNetworking
$nw)
768 $this->networks
= $nw;
771 public function getNetworking($flags, $limit = null
)
773 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
774 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
776 $this->setNetworking($nw);
779 if ($this->networks
== null
) {
782 return $this->networks
->get($flags, $limit);
785 public function getWebSite()
787 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
788 if (count($site) != 1) {
791 $site = array_pop($site);
798 private $jobs = null
;
799 public function setJobs(ProfileJobs
$jobs)
802 $this->consolidateFields();
805 private function fetchJobs()
807 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
808 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
810 $this->setJobs($jobs);
811 $this->fetchAddresses();
816 public function getJobs($flags, $limit = null
)
820 if ($this->jobs
== null
) {
823 return $this->jobs
->get($flags, $limit);
826 public function getMainJob()
828 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
829 if (count($job) != 1) {
832 return array_pop($job);
837 private $jobterms = null
;
838 public function setJobTerms(ProfileJobTerms
$jobterms)
840 $this->jobterms
= $jobterms;
841 $this->consolidateFields();
844 private $mentor_countries = null
;
845 public function setMentoringCountries(ProfileMentoringCountries
$countries)
847 $this->mentor_countries
= $countries;
850 public function getMentoringCountries()
852 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
853 $countries = $this->getProfileField(self
::FETCH_MENTOR_COUNTRY
);
855 $this->setMentoringCountries($countries);
859 if ($this->mentor_countries
== null
) {
862 return $this->mentor_countries
->countries
;
866 /** List of job terms to specify mentoring */
867 private $mentor_terms = null
;
869 * set job terms to specify mentoring
870 * @param $terms a ProfileMentoringTerms object listing terms only for this profile
872 public function setMentoringTerms(ProfileMentoringTerms
$terms)
874 $this->mentor_terms
= $terms;
877 * get all job terms that specify mentoring
878 * @return an array of JobTerms objects
880 public function getMentoringTerms()
882 if ($this->mentor_terms
== null
&& !$this->fetched(self
::FETCH_MENTOR_TERMS
)) {
883 $terms = $this->getProfileField(self
::FETCH_MENTOR_TERMS
);
885 $this->setMentoringTerms($terms);
889 if ($this->mentor_terms
== null
) {
892 return $this->mentor_terms
->get();
897 private $skills = null
;
898 public function setSkills(ProfileSkills
$skills)
900 $this->skills
= $skills;
902 public function getSkills()
904 if ($this->skills
== null
&& !$this->fetched(self
::FETCH_SKILL
)) {
905 $skills = $this->getProfileField(self
::FETCH_SKILL
);
907 $this->setSkills($skills);
911 if ($this->skills
== null
) {
914 return $this->skills
->skills
;
919 private $languages = null
;
920 public function setLanguages(ProfileLanguages
$languages)
922 $this->languages
= $languages;
924 public function getLanguages()
926 if ($this->languages
== null
&& !$this->fetched(self
::FETCH_LANGUAGE
)) {
927 $languages = $this->getProfileField(self
::FETCH_LANGUAGE
);
929 $this->setLanguages($languages);
933 if ($this->languages
== null
) {
936 return $this->languages
->languages
;
943 /** Find out whether this profile may take part to the "DeltaTen" operation.
944 * @param $role Which role to select ('young' or 'old')
945 * @return Boolean: whether it is enabled.
947 const DELTATEN_YOUNG
= 'young';
948 const DELTATEN_OLD
= 'old';
949 public function isDeltaTenEnabled($role)
953 case self
::DELTATEN_YOUNG
:
954 return ($this->mainGrade() == UserFilter
::GRADE_ING
&& $this->yearpromo() >= $globals->deltaten
->first_promo_young
);
955 case self
::DELTATEN_OLD
:
956 // Roughly compute the current promo in second year on the campus:
957 // Promo 2010 is in second year between 09/2011 and 08/2012 => use 2012.
958 // DeltaTen program begins around January of the second year.
959 $promo_on_platal = ((int) date('Y')) - 2;
960 return ($this->mainGrade() == UserFilter
::GRADE_ING
&& $this->yearpromo() >= $globals->deltaten
->first_promo_young
- 10 && $this->yearpromo() <= $promo_on_platal - 10);
962 Platal
::assert(false
, "Invalid DeltaTen role $role");
966 /** Retrieve the "Deltaten" message of the user.
967 * Returns "null" if the message is empty or the user is not taking part to the
968 * DeltaTen operation.
970 public function getDeltatenMessage()
972 if ($this->isDeltaTenEnabled(self
::DELTATEN_OLD
)) {
973 return $this->deltaten_message
;
981 public function getBinets()
983 if ($this->visibility
->isVisible(Visibility
::EXPORT_PRIVATE
)) {
984 return XDB
::fetchColumn('SELECT binet_id
986 WHERE pid = {?}', $this->id());
992 public function getFullBinets()
994 if ($this->visibility
->isVisible(Visibility
::EXPORT_PRIVATE
)) {
995 return XDB
::fetchAllAssoc('SELECT binet_id, text, url
996 FROM profile_binets AS pb
997 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
998 WHERE pid = {?}', $this->id());
1004 public function getBinetsNames()
1006 if ($this->visibility
->isVisible(Visibility
::EXPORT_PRIVATE
)) {
1007 return XDB
::fetchColumn('SELECT text
1008 FROM profile_binets AS pb
1009 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
1010 WHERE pb.pid = {?}', $this->id());
1018 public function getHobbies() {
1019 if ($this->visibility
->isVisible(Visibility
::EXPORT_PRIVATE
)) {
1020 return XDB
::fetchAllAssoc('type', 'SELECT type, GROUP_CONCAT(text)
1023 GROUP BY type', $this->id());
1025 return XDB
::fetchAllAssoc('type', 'SELECT type, GROUP_CONCAT(text)
1027 WHERE pub = \'public\' AND pid = {?}
1028 GROUP BY type', $this->id());
1034 private $medals = null
;
1035 public function setMedals(ProfileMedals
$medals)
1037 $this->medals
= $medals;
1040 public function getMedals()
1042 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
1043 $medals = $this->getProfileField(self
::FETCH_MEDALS
);
1045 $this->setMedals($medals);
1048 if ($this->medals
== null
) {
1051 return $this->medals
->medals
;
1054 /** Sharing data with partner websites
1056 private $partners_settings = null
;
1057 public function setPartnersSettings(ProfilePartnerSharing
$partners_settings)
1059 $this->partners_settings
= $partners_settings;
1062 public function getPartnerSettings($partner_id)
1064 if ($this->partners_settings
=== null
&& !$this->fetched(self
::FETCH_PARTNER
)) {
1065 $settings = $this->getProfileField(self
::FETCH_PARTNER
);
1067 $this->setPartnersSettings($settings);
1070 if ($this->partners_settings
=== null
) {
1071 return PartnerSettings
::getEmpty($partner_id);
1073 return $this->partners_settings
->get($partner_id);
1076 public function compareNames($firstname, $lastname)
1078 $_lastname = mb_strtoupper($this->lastName());
1079 $_firstname = mb_strtoupper($this->firstName());
1080 $lastname = mb_strtoupper($lastname);
1081 $firstname = mb_strtoupper($firstname);
1083 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
1084 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
1087 foreach ($tokens as $str) {
1088 $isOk &= (strpos($_lastname, $str) !== false
);
1089 $maxlen = max($maxlen, strlen($str));
1092 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
1097 public function export()
1100 'hrpid' => $this->hrid(),
1101 'display_name' => $this->shortName(),
1102 'full_name' => $this->fullName(),
1103 'directory_name' => $this->directory_name
,
1104 'promo' => $this->promo(),
1105 'year_promo' => $this->yearpromo(),
1106 'is_active' => $this->isActive(),
1107 'first_name' => $this->firstName(),
1108 'last_name' => $this->lastName(),
1109 'is_female' => $this->isFemale(),
1113 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1115 if (count($pids) == 0) {
1119 if ($respect_order) {
1120 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
1125 if ($visibility === null
) {
1126 $visibility = Visibility
::defaultForRead();
1129 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
1130 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
1131 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
1132 p.last_change, p.nationality1, p.nationality2, p.nationality3,
1133 IF (p.freetext_pub >= {?}, p.freetext, NULL) AS freetext,
1134 pe.entry_year, pe.grad_year, pe.promo_year, pe.program, pe.fieldid,
1135 IF ({?}, pse.text, NULL) AS section,
1136 ppn.firstname_main AS firstname, ppn.lastname_main AS lastname, IF ({?}, pn.name, NULL) AS nickname,
1137 IF (ppn.firstname_ordinary = \'\', ppn.firstname_main, ppn.firstname_ordinary) AS firstname_ordinary,
1138 IF (ppn.lastname_ordinary = \'\', ppn.lastname_main, ppn.lastname_ordinary) AS lastname_ordinary,
1139 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
1140 pd.directory_name, pd.public_name, pd.private_name,
1141 IF (pp.pub >= {?}, pp.display_tel, NULL) AS mobile,
1142 (ph.pub >= {?} AND ph.attach IS NOT NULL) AS has_photo, ph.pub as photo_pub,
1143 ph.x AS photo_width, ph.y AS photo_height,
1144 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
1145 pm.expertise AS mentor_expertise,
1146 IF ({?}, pdt.message, NULL) AS deltaten_message,
1149 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
1150 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
1151 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
1152 INNER JOIN profile_public_names AS ppn ON (ppn.pid = p.pid)
1153 LEFT JOIN profile_private_names AS pn ON (pn.pid = p.pid AND type = \'nickname\')
1154 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
1155 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
1156 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
1157 LEFT JOIN profile_deltaten AS pdt ON (pdt.pid = p.pid)
1158 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
1162 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // CV
1163 $visibility->level(), // freetext
1164 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // section
1165 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // nickname
1166 $visibility->level(), // mobile
1167 $visibility->level(), // photo
1168 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // deltaten_message
1171 return new ProfileIterator($it, $pids, $fields, $visibility);
1174 public static function getPID($login)
1176 if ($login instanceof PlUser
) {
1177 return XDB
::fetchOneCell('SELECT pid
1178 FROM account_profiles
1179 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
1181 } else if (ctype_digit($login)) {
1182 return XDB
::fetchOneCell('SELECT pid
1184 WHERE pid = {?}', $login);
1186 return XDB
::fetchOneCell('SELECT pid
1188 WHERE hrpid = {?}', $login);
1192 public static function getPIDsFromUIDs($uids, $respect_order = true
)
1194 if ($respect_order) {
1195 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
1199 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
1200 FROM account_profiles AS ap
1201 WHERE FIND_IN_SET(\'owner\', ap.perms)
1202 AND ap.uid IN ' . XDB
::formatArray($uids) .'
1206 /** Return the profile associated with the given login.
1208 public static function get($login, $fields = 0x0000, $visibility = null
)
1210 if ($visibility === null
) {
1211 $visibility = Visibility
::defaultForRead();
1214 if (is_array($login)) {
1215 $pf = new Profile($login, $visibility);
1218 $pid = self
::getPID($login);
1219 if (!is_null($pid)) {
1220 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
1223 /* Let say we can identify a profile using the identifiers of its owner.
1225 if (!($login instanceof PlUser
)) {
1226 $user = User
::getSilent($login);
1227 if ($user && $user->hasProfile()) {
1228 return $user->profile(false
, $fields, $visibility);
1235 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1237 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
1240 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1242 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
1245 /** Return profiles for the list of pids.
1247 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
1249 if (count($pids) == 0) {
1252 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
1253 $profiles = array();
1254 while ($p = $it->next()) {
1255 $profiles[$p->id()] = $p;
1260 /** Return profiles for uids.
1262 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
1264 if (count($uids) == 0) {
1267 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
1270 public static function isDisplayName($name)
1272 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
1273 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
1274 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1275 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1278 /** Returns the closest "accounts only" name type for $name
1280 public static function getAccountEquivalentName($name)
1283 case self
::DN_DIRECTORY
:
1284 return 'directory_name';
1288 case self
::DN_PUBLIC
:
1289 case self
::DN_PRIVATE
:
1290 case self
::DN_SHORT
:
1292 case self
::DN_YOURSELF
:
1293 return 'display_name';
1295 return 'display_name';
1299 public static function rebuildSearchTokens($pids, $transaction = true
)
1301 require_once 'name.func.inc.php';
1302 if (!is_array($pids)) {
1303 $pids = array($pids);
1305 $keys = XDB
::iterator("(SELECT pid, name, type, IF(type = 'nickname', 2, 1) AS score, '' AS public
1306 FROM profile_private_names
1309 (SELECT pid, lastname_main, 'lastname' AS type, 10 AS score, 'public' AS public
1310 FROM profile_public_names
1311 WHERE lastname_main != '' AND pid IN {?})
1313 (SELECT pid, lastname_marital, 'lastname' AS type, 10 AS score, 'public' AS public
1314 FROM profile_public_names
1315 WHERE lastname_marital != '' AND pid IN {?})
1317 (SELECT pid, lastname_ordinary, 'lastname' AS type, 10 AS score, 'public' AS public
1318 FROM profile_public_names
1319 WHERE lastname_ordinary != '' AND pid IN {?})
1321 (SELECT pid, firstname_main, 'firstname' AS type, 10 AS score, 'public' AS public
1322 FROM profile_public_names
1323 WHERE firstname_main != '' AND pid IN {?})
1325 (SELECT pid, firstname_ordinary, 'firstname' AS type, 10 AS score, 'public' AS public
1326 FROM profile_public_names
1327 WHERE firstname_ordinary != '' AND pid IN {?})
1329 (SELECT pid, pseudonym, 'nickname' AS type, 10 AS score, 'public' AS public
1330 FROM profile_public_names
1331 WHERE pseudonym != '' AND pid IN {?})",
1332 $pids, $pids, $pids, $pids, $pids, $pids, $pids);
1334 while ($key = $keys->next()) {
1335 if ($key['name'] == '') {
1339 $toks = split_name_for_search($key['name']);
1340 $toks = array_reverse($toks);
1342 /* Split the score between the tokens to avoid the user to be over-rated.
1343 * Let says my user name is "Machin-Truc Bidule" and I also have a user named
1344 * 'Machin Truc'. Distributing the score force "Machin Truc" to be displayed
1345 * before "Machin-Truc" for both "Machin Truc" and "Machin" searches.
1347 $eltScore = ceil(((float)$key['score'])/((float)count($toks)));
1349 foreach ($toks as $tok) {
1350 $token = $tok . $token;
1351 $names["$pid-$token"] = XDB
::format('({?}, {?}, {?}, {?}, {?}, {?})',
1352 $token, $pid, soundex_fr($token),
1353 $eltScore, $key['public'], $key['type']);
1357 XDB
::startTransaction();
1359 XDB
::execute('DELETE FROM search_name
1362 if (count($names) > 0) {
1363 XDB
::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags, general_type)
1364 VALUES ' . implode(', ', $names));
1371 /** The school identifier consists of 6 digits. The first 3 represent the
1372 * promotion entry year. The last 3 indicate the student's rank.
1374 * Our identifier consists of 8 digits and both half have the same role.
1375 * This enables us to deal with bigger promotions and with a wider range
1378 * getSchoolId returns a school identifier given one of ours.
1379 * getXorgId returns a X.org identifier given a school identifier.
1381 public static function getSchoolId($xorgId)
1383 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1387 $year = intval(substr($xorgId, 0, 4));
1388 $rank = intval(substr($xorgId, 5, 3));
1391 } elseif ($year < 2000) {
1392 $year = intval(substr(1900 - $year, 1, 3));
1393 return sprintf('%02u0%03u', $year, $rank);
1395 $year = intval(substr(1900 - $year, 1, 3));
1396 return sprintf('%03u%03u', $year, $rank);
1400 public static function getXorgId($schoolId)
1402 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1406 $year = intval(substr($schoolId, 0, 3));
1407 $rank = intval(substr($schoolId, 3, 3));
1415 return sprintf('%04u%04u', 1900 +
$year, $rank);
1421 /** Iterator over a set of Profiles
1423 class ProfileIterator
implements PlIterator
1425 private $iterator = null
;
1427 private $visibility;
1429 const FETCH_ALL
= 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
1431 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, $visibility = null
)
1433 require_once 'profilefields.inc.php';
1435 if ($visibility === null
) {
1436 $visibility = Visibility
::defaultForRead();
1439 $this->fields
= $fields;
1440 $this->visibility
= $visibility;
1443 $callbacks = array();
1446 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1447 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1449 $fields = $fields & self
::FETCH_ALL
;
1450 for ($field = 1; $field < $fields; $field *= 2) {
1451 if (($fields & $field) ) {
1452 $callbacks[$field] = $cb;
1453 $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility);
1457 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1460 private function hasData($field, $vals)
1462 return ($this->fields
& $field) && ($vals[$field] != null
);
1465 private function fillProfile(array $vals)
1467 $pf = Profile
::get($vals[0], 0x0, $this->visibility
);
1468 $pf->setFetchedFields($this->fields
);
1470 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1471 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1473 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1474 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1476 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1477 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1479 if ($this->hasData(Profile
::FETCH_JOB_TERMS
, $vals)) {
1480 $pf->setJobTerms($vals[Profile
::FETCH_JOB_TERMS
]);
1483 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1484 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1486 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1487 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1489 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1490 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1492 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1493 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1499 public function next()
1501 $vals = $this->iterator
->next();
1502 if ($vals == null
) {
1505 return $this->fillProfile($vals);
1508 public function first()
1510 return $this->iterator
->first();
1513 public function last()
1515 return $this->iterator
->last();
1518 public function total()
1520 return $this->iterator
->total();
1524 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: