2 /***************************************************************************
3 * Copyright (C) 2003-2013 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 'photo' => 'Photographie',
138 'photo_pub' => 'Affichage de la photographie',
139 'addresses' => 'Adresses',
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)',
151 'countries' => 'Pays (mentoring)',
152 'deltaten' => 'Opération N N-10',
155 private $fetched_fields = 0x000000;
160 private $owner_fetched = false
;
161 private $data = array();
163 private $visibility = null
;
166 private function __construct(array $data, Visibility
$visibility)
169 $this->pid
= $this->data
['pid'];
170 $this->hrpid
= $this->data
['hrpid'];
171 $this->visibility
= $visibility;
174 public function __destruct()
176 // Need to delete objects allocated by the profile
177 // http://www.php.net/manual/en/function.unset.php#98692
186 public function hrid()
191 public function owner()
193 if ($this->owner
== null
&& !$this->owner_fetched
) {
194 $this->owner_fetched
= true
;
195 $this->owner
= User
::getSilent($this);
200 public function isActive()
202 if ($this->owner()) {
203 return $this->owner
->isActive();
208 public function promo($details = false
)
210 if ($details && ($this->program ||
$this->fieldid
)) {
212 if ($this->program
) {
213 $text[] = $this->program
;
215 if ($this->fieldid
) {
216 $fieldsList = DirEnum
::getOptions(DirEnum
::EDUFIELDS
);
217 $text[] = $fieldsList[$this->fieldid
];
219 return $this->promo
. ' (' . implode(', ', $text) . ')';
225 public function yearpromo()
227 return $this->promo_year
;
230 /** Check if user is an orange (associated with several promos)
232 public function isMultiPromo()
234 return $this->grad_year
!= $this->entry_year +
$this->mainEducationDuration();
237 /** Returns an array with all associated promo years.
239 public function yearspromo()
242 $d = -$this->deltaPromoToGradYear();
243 for ($g = $this->entry_year +
$this->mainEducationDuration(); $g <= $this->grad_year
; ++
$g) {
249 public function mainEducation()
251 if (empty($this->promo
)) {
254 return $this->promo
{0};
258 public function mainGrade()
260 switch ($this->mainEducation()) {
262 return UserFilter
::GRADE_ING
;
264 return UserFilter
::GRADE_MST
;
266 return UserFilter
::GRADE_PHD
;
272 public function mainEducationDuration()
274 switch ($this->mainEducation()) {
286 public static function educationDuration($education)
288 switch ($education) {
300 /** Number of years between the promotion year until the
301 * graduation year. In standard schools it's 0, but for
302 * Polytechnique the promo year is the entry year.
304 public function deltaPromoToGradYear()
306 if ($this->mainEducation() == 'X') {
307 return $this->mainEducationDuration();
312 // Returns the profile's color.
313 public function promoColor()
315 switch ($this->mainEducation()) {
317 if (($this->yearpromo() %
2) === 0) {
331 // Returns younger/older promotion year for a given education.
332 static public function extremePromotions($education)
334 return XDB
::fetchOneRow("SELECT MIN(pe.promo_year) AS min, MAX(pe.promo_year) AS max
335 FROM profile_education AS pe
336 INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
337 WHERE pede.degree = {?} AND FIND_IN_SET('primary', pe.flags)",
341 /** Print a name with the given formatting:
349 public function name($format)
351 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
352 array($this->isFemale() ?
'•' : '',
353 $this->firstName(), $this->lastName(),
354 $this->fullName(), $this->shortName(),
355 $this->promo()), $format);
358 public function fullName($with_promo = false
)
361 return $this->full_name
. ' (' . $this->promo
. ')';
363 return $this->full_name
;
366 public function shortName($with_promo = false
)
369 return $this->short_name
. ' (' . $this->promo
. ')';
371 return $this->short_name
;
374 public function firstName()
376 return $this->firstname_ordinary
;
379 public function firstNames()
381 return $this->nameVariants(self
::FIRSTNAME
);
384 public function lastName()
386 return $this->lastname_ordinary
;
389 public function lastNames()
391 return $this->nameVariants(self
::LASTNAME
);
394 public function isFemale()
396 return $this->sex
== PlUser
::GENDER_FEMALE
;
399 public function isDead()
401 return ($this->deathdate
!= null
);
404 public function displayEmail()
407 if ($o != null
&& $this->isVisible(Visibility
::EXPORT_PRIVATE
)) {
408 return $o->bestEmail();
410 return $this->email_directory
;
414 public function data()
420 private function nameVariants($type)
422 $vals = array($this->$type);
423 foreach (self
::$name_variants[$type] as $var) {
424 $vartype = $type . '_' . $var;
425 $varname = $this->$vartype;
426 if ($varname != null
&& $varname != "") {
430 return array_unique($vals);
433 public function nationalities()
436 $nationalities = DirEnum
::getOptions(DirEnum
::NATIONALITIES
);
437 if ($this->nationality1
) {
438 $nats[$this->nationality1
] = $nationalities[$this->nationality1
];
440 if ($this->nationality2
) {
441 $nats[$this->nationality2
] = $nationalities[$this->nationality2
];
443 if ($this->nationality3
) {
444 $nats[$this->nationality3
] = $nationalities[$this->nationality3
];
449 public function __get($name)
451 if (property_exists($this, $name)) {
455 if (isset($this->data
[$name])) {
456 return $this->data
[$name];
462 public function __isset($name)
464 return property_exists($this, $name) ||
isset($this->data
[$name]);
467 public function __unset($name)
469 if (property_exists($this, $name)) {
472 unset($this->data
[$name]);
479 * *always deletes in: profile_addresses, profile_binets, profile_deltaten,
480 * profile_job, profile_langskills, profile_mentor, profile_networking,
481 * profile_partnersharing_settings, profile_phones, profile_skills,
483 * *always keeps in: profile_corps, profile_display, profile_education,
484 * profile_medals, profile_*_names, profile_photos, search_name
485 * *modifies: profiles
487 public function clear()
490 'profile_job', 'profile_langskills', 'profile_mentor',
491 'profile_networking', 'profile_skills', 'watch_profile',
492 'profile_phones', 'profile_addresses', 'profile_binets',
493 'profile_deltaten', 'profile_partnersharing_settings');
495 foreach ($tables as $t) {
496 XDB
::execute('DELETE FROM ' . $t . '
501 XDB
::execute("UPDATE profiles
502 SET cv = NULL, freetext = NULL, freetext_pub = 'private',
503 medals_pub = 'private', alias_pub = 'hidden',
504 email_directory = NULL
509 /** Determine whether an item with visibility $visibility can be displayed
510 * with the current level of visibility of the profile
511 * @param $visibility The level of visibility to be checked
513 public function isVisible($visibility)
515 return $this->visibility
->isVisible($visibility);
518 /** Stores the list of fields which have already been fetched for this Profile
520 public function setFetchedFields($fields)
522 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
523 Platal
::page()->kill("Invalid fetched fields: $fields");
526 $this->fetched_fields
= $fields;
529 /** Have we already fetched this field ?
531 private function fetched($field)
533 if (!array_key_exists($field, ProfileField
::$fields)) {
534 Platal
::page()->kill("Invalid field: $field");
537 return ($this->fetched_fields
& $field);
540 /** If not already done, fetches data for the given field
541 * @param $field One of the Profile::FETCH_*
542 * @return A ProfileField, or null
544 private function getProfileField($field)
546 if (!array_key_exists($field, ProfileField
::$fields)) {
547 Platal
::page()->kill("Invalid field: $field");
549 if ($this->fetched($field)) {
552 $this->fetched_fields
= $this->fetched_fields |
$field;
555 $cls = ProfileField
::$fields[$field];
557 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
560 /** Consolidates internal data (addresses, phones, jobs)
562 private function consolidateFields()
564 // Link phones to addresses
565 if ($this->phones
!= null
) {
566 if ($this->addresses
!= null
) {
567 $this->addresses
->addPhones($this->phones
);
570 if ($this->jobs
!= null
) {
571 $this->jobs
->addPhones($this->phones
);
575 // Link addresses to jobs
576 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
577 $this->jobs
->addAddresses($this->addresses
);
580 // Link jobterms to jobs
581 if ($this->jobs
!= null
&& $this->jobterms
!= null
) {
582 $this->jobs
->addJobTerms($this->jobterms
);
588 private $photo = null
;
589 public function getPhoto($fallback = true
, $data = false
)
591 if ($this->has_photo
) {
592 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
593 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y, last_update
595 WHERE pid = {?}', $this->pid
);
596 $this->photo
= PlImage
::fromData($res['attach'], 'image/' .$res['attachmime'], $res['x'], $res['y'], $res['last_update']);
597 } else if ($this->photo
== null
) {
598 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
601 } else if ($fallback) {
602 if ($this->mainEducation() == 'X') {
603 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_x.png', 'image/png');
605 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_md.png', 'image/png');
612 private $addresses = null
;
613 public function setAddresses(ProfileAddresses
$addr)
615 $this->addresses
= $addr;
616 $this->consolidateFields();
619 private function fetchAddresses()
621 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
622 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
624 $this->setAddresses($addr);
625 $this->fetchPhones();
630 public function getAddresses($flags, $limit = null
)
632 $this->fetchAddresses();
634 if ($this->addresses
== null
) {
637 return $this->addresses
->get($flags, $limit);
640 public function iterAddresses($flags, $limit = null
)
642 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
645 public function getMainAddress()
647 $main = $this->getAddresses(self
::ADDRESS_MAIN
);
648 $perso = $this->getAddresses(self
::ADDRESS_PERSO
);
651 return array_pop($main);
652 } else if (count($perso)) {
653 return array_pop($perso);
661 private $phones = null
;
662 public function setPhones(ProfilePhones
$phones)
664 $this->phones
= $phones;
665 $this->consolidateFields();
668 private function fetchPhones()
670 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
671 $phones = $this->getProfileField(self
::FETCH_PHONES
);
672 if (isset($phones)) {
673 $this->setPhones($phones);
678 public function getPhones($flags, $limit = null
)
680 $this->fetchPhones();
681 if ($this->phones
== null
) {
684 return $this->phones
->get($flags, $limit);
689 private $educations = null
;
690 public function setEducations(ProfileEducation
$edu)
692 $this->educations
= $edu;
695 public function getEducations($flags, $limit = null
)
697 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
698 $educations = $this->getProfileField(self
::FETCH_EDU
);
700 $this->setEducations($educations);
704 if ($this->educations
== null
) {
707 return $this->educations
->get($flags, $limit);
710 public function getExtraEducations($limit = null
)
712 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
717 private $corps = null
;
718 public function setCorps(ProfileCorps
$corps)
720 $this->corps
= $corps;
723 public function getCorps()
725 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
726 $corps = $this->getProfileField(self
::FETCH_CORPS
);
728 $this->setCorps($corps);
736 private $networks = null
;
737 public function setNetworking(ProfileNetworking
$nw)
739 $this->networks
= $nw;
742 public function getNetworking($flags, $limit = null
)
744 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
745 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
747 $this->setNetworking($nw);
750 if ($this->networks
== null
) {
753 return $this->networks
->get($flags, $limit);
756 public function getWebSite()
758 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
759 if (count($site) != 1) {
762 $site = array_pop($site);
769 private $jobs = null
;
770 public function setJobs(ProfileJobs
$jobs)
773 $this->consolidateFields();
776 private function fetchJobs()
778 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
779 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
781 $this->setJobs($jobs);
782 $this->fetchAddresses();
787 public function getJobs($flags, $limit = null
)
791 if ($this->jobs
== null
) {
794 return $this->jobs
->get($flags, $limit);
797 public function getMainJob()
799 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
800 if (count($job) != 1) {
803 return array_pop($job);
808 private $jobterms = null
;
809 public function setJobTerms(ProfileJobTerms
$jobterms)
811 $this->jobterms
= $jobterms;
812 $this->consolidateFields();
815 private $mentor_countries = null
;
816 public function setMentoringCountries(ProfileMentoringCountries
$countries)
818 $this->mentor_countries
= $countries;
821 public function getMentoringCountries()
823 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
824 $countries = $this->getProfileField(self
::FETCH_MENTOR_COUNTRY
);
826 $this->setMentoringCountries($countries);
830 if ($this->mentor_countries
== null
) {
833 return $this->mentor_countries
->countries
;
837 /** List of job terms to specify mentoring */
838 private $mentor_terms = null
;
840 * set job terms to specify mentoring
841 * @param $terms a ProfileMentoringTerms object listing terms only for this profile
843 public function setMentoringTerms(ProfileMentoringTerms
$terms)
845 $this->mentor_terms
= $terms;
848 * get all job terms that specify mentoring
849 * @return an array of JobTerms objects
851 public function getMentoringTerms()
853 if ($this->mentor_terms
== null
&& !$this->fetched(self
::FETCH_MENTOR_TERMS
)) {
854 $terms = $this->getProfileField(self
::FETCH_MENTOR_TERMS
);
856 $this->setMentoringTerms($terms);
860 if ($this->mentor_terms
== null
) {
863 return $this->mentor_terms
->get();
868 private $skills = null
;
869 public function setSkills(ProfileSkills
$skills)
871 $this->skills
= $skills;
873 public function getSkills()
875 if ($this->skills
== null
&& !$this->fetched(self
::FETCH_SKILL
)) {
876 $skills = $this->getProfileField(self
::FETCH_SKILL
);
878 $this->setSkills($skills);
882 if ($this->skills
== null
) {
885 return $this->skills
->skills
;
890 private $languages = null
;
891 public function setLanguages(ProfileLanguages
$languages)
893 $this->languages
= $languages;
895 public function getLanguages()
897 if ($this->languages
== null
&& !$this->fetched(self
::FETCH_LANGUAGE
)) {
898 $languages = $this->getProfileField(self
::FETCH_LANGUAGE
);
900 $this->setLanguages($languages);
904 if ($this->languages
== null
) {
907 return $this->languages
->languages
;
914 /** Find out whether this profile may take part to the "DeltaTen" operation.
915 * @param $role Which role to select ('young' or 'old')
916 * @return Boolean: whether it is enabled.
918 const DELTATEN_YOUNG
= 'young';
919 const DELTATEN_OLD
= 'old';
920 public function isDeltaTenEnabled($role)
924 case self
::DELTATEN_YOUNG
:
925 return ($this->mainGrade() == UserFilter
::GRADE_ING
&& $this->yearpromo() >= $globals->deltaten
->first_promo_young
);
926 case self
::DELTATEN_OLD
:
927 // Roughly compute the current promo in second year on the campus:
928 // Promo 2010 is in second year between 09/2011 and 08/2012 => use 2012.
929 // DeltaTen program begins around January of the second year.
930 $promo_on_platal = ((int) date('Y')) - 2;
931 return ($this->mainGrade() == UserFilter
::GRADE_ING
&& $this->yearpromo() >= $globals->deltaten
->first_promo_young
- 10 && $this->yearpromo() <= $promo_on_platal - 10);
933 Platal
::assert(false
, "Invalid DeltaTen role $role");
937 /** Retrieve the "Deltaten" message of the user.
938 * Returns "null" if the message is empty or the user is not taking part to the
939 * DeltaTen operation.
941 public function getDeltatenMessage()
943 if ($this->isDeltaTenEnabled(self
::DELTATEN_OLD
)) {
944 return $this->deltaten_message
;
952 public function getBinets()
954 if ($this->visibility
->isVisible(Visibility
::EXPORT_PRIVATE
)) {
955 return XDB
::fetchColumn('SELECT binet_id
957 WHERE pid = {?}', $this->id());
963 public function getFullBinets()
965 if ($this->visibility
->isVisible(Visibility
::EXPORT_PRIVATE
)) {
966 return XDB
::fetchAllAssoc('SELECT binet_id, text, url
967 FROM profile_binets AS pb
968 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
969 WHERE pid = {?}', $this->id());
975 public function getBinetsNames()
977 if ($this->visibility
->isVisible(Visibility
::EXPORT_PRIVATE
)) {
978 return XDB
::fetchColumn('SELECT text
979 FROM profile_binets AS pb
980 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
981 WHERE pb.pid = {?}', $this->id());
989 private $medals = null
;
990 public function setMedals(ProfileMedals
$medals)
992 $this->medals
= $medals;
995 public function getMedals()
997 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
998 $medals = $this->getProfileField(self
::FETCH_MEDALS
);
1000 $this->setMedals($medals);
1003 if ($this->medals
== null
) {
1006 return $this->medals
->medals
;
1009 /** Sharing data with partner websites
1011 private $partners_settings = null
;
1012 public function setPartnersSettings(ProfilePartnerSharing
$partners_settings)
1014 $this->partners_settings
= $partners_settings;
1017 public function getPartnerSettings($partner_id)
1019 if ($this->partners_settings
=== null
&& !$this->fetched(self
::FETCH_PARTNER
)) {
1020 $settings = $this->getProfileField(self
::FETCH_PARTNER
);
1022 $this->setPartnersSettings($settings);
1025 if ($this->partners_settings
=== null
) {
1026 return PartnerSettings
::getEmpty($partner_id);
1028 return $this->partners_settings
->get($partner_id);
1031 public function compareNames($firstname, $lastname)
1033 $_lastname = mb_strtoupper($this->lastName());
1034 $_firstname = mb_strtoupper($this->firstName());
1035 $lastname = mb_strtoupper($lastname);
1036 $firstname = mb_strtoupper($firstname);
1038 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
1039 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
1042 foreach ($tokens as $str) {
1043 $isOk &= (strpos($_lastname, $str) !== false
);
1044 $maxlen = max($maxlen, strlen($str));
1047 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
1052 public function export()
1055 'hrpid' => $this->hrid(),
1056 'display_name' => $this->shortName(),
1057 'full_name' => $this->fullName(),
1058 'directory_name' => $this->directory_name
,
1059 'promo' => $this->promo(),
1060 'year_promo' => $this->yearpromo(),
1061 'is_active' => $this->isActive(),
1062 'first_name' => $this->firstName(),
1063 'last_name' => $this->lastName(),
1064 'is_female' => $this->isFemale(),
1068 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1070 if (count($pids) == 0) {
1074 if ($respect_order) {
1075 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
1080 if ($visibility === null
) {
1081 $visibility = Visibility
::defaultForRead();
1084 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
1085 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
1086 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
1087 p.last_change, p.nationality1, p.nationality2, p.nationality3,
1088 IF (p.freetext_pub >= {?}, p.freetext, NULL) AS freetext,
1089 pe.entry_year, pe.grad_year, pe.promo_year, pe.program, pe.fieldid,
1090 IF ({?}, pse.text, NULL) AS section,
1091 ppn.firstname_main AS firstname, ppn.lastname_main AS lastname, IF ({?}, pn.name, NULL) AS nickname,
1092 IF (ppn.firstname_ordinary = \'\', ppn.firstname_main, ppn.firstname_ordinary) AS firstname_ordinary,
1093 IF (ppn.lastname_ordinary = \'\', ppn.lastname_main, ppn.lastname_ordinary) AS lastname_ordinary,
1094 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
1095 pd.directory_name, pd.public_name, pd.private_name,
1096 IF (pp.pub >= {?}, pp.display_tel, NULL) AS mobile,
1097 (ph.pub >= {?} AND ph.attach IS NOT NULL) AS has_photo, ph.pub as photo_pub,
1098 ph.x AS photo_width, ph.y AS photo_height,
1099 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
1100 pm.expertise AS mentor_expertise,
1101 IF ({?}, pdt.message, NULL) AS deltaten_message,
1104 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
1105 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
1106 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
1107 INNER JOIN profile_public_names AS ppn ON (ppn.pid = p.pid)
1108 LEFT JOIN profile_private_names AS pn ON (pn.pid = p.pid AND type = \'nickname\')
1109 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
1110 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
1111 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
1112 LEFT JOIN profile_deltaten AS pdt ON (pdt.pid = p.pid)
1113 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
1117 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // CV
1118 $visibility->level(), // freetext
1119 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // section
1120 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // nickname
1121 $visibility->level(), // mobile
1122 $visibility->level(), // photo
1123 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // deltaten_message
1126 return new ProfileIterator($it, $pids, $fields, $visibility);
1129 public static function getPID($login)
1131 if ($login instanceof PlUser
) {
1132 return XDB
::fetchOneCell('SELECT pid
1133 FROM account_profiles
1134 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
1136 } else if (ctype_digit($login)) {
1137 return XDB
::fetchOneCell('SELECT pid
1139 WHERE pid = {?}', $login);
1141 return XDB
::fetchOneCell('SELECT pid
1143 WHERE hrpid = {?}', $login);
1147 public static function getPIDsFromUIDs($uids, $respect_order = true
)
1149 if ($respect_order) {
1150 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
1154 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
1155 FROM account_profiles AS ap
1156 WHERE FIND_IN_SET(\'owner\', ap.perms)
1157 AND ap.uid IN ' . XDB
::formatArray($uids) .'
1161 /** Return the profile associated with the given login.
1163 public static function get($login, $fields = 0x0000, $visibility = null
)
1165 if ($visibility === null
) {
1166 $visibility = Visibility
::defaultForRead();
1169 if (is_array($login)) {
1170 $pf = new Profile($login, $visibility);
1173 $pid = self
::getPID($login);
1174 if (!is_null($pid)) {
1175 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
1178 /* Let say we can identify a profile using the identifiers of its owner.
1180 if (!($login instanceof PlUser
)) {
1181 $user = User
::getSilent($login);
1182 if ($user && $user->hasProfile()) {
1183 return $user->profile(false
, $fields, $visibility);
1190 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1192 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
1195 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1197 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
1200 /** Return profiles for the list of pids.
1202 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
1204 if (count($pids) == 0) {
1207 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
1208 $profiles = array();
1209 while ($p = $it->next()) {
1210 $profiles[$p->id()] = $p;
1215 /** Return profiles for uids.
1217 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
1219 if (count($uids) == 0) {
1222 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
1225 public static function isDisplayName($name)
1227 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
1228 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
1229 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1230 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1233 /** Returns the closest "accounts only" name type for $name
1235 public static function getAccountEquivalentName($name)
1238 case self
::DN_DIRECTORY
:
1239 return 'directory_name';
1243 case self
::DN_PUBLIC
:
1244 case self
::DN_PRIVATE
:
1245 case self
::DN_SHORT
:
1247 case self
::DN_YOURSELF
:
1248 return 'display_name';
1250 return 'display_name';
1254 public static function rebuildSearchTokens($pids, $transaction = true
)
1256 require_once 'name.func.inc.php';
1257 if (!is_array($pids)) {
1258 $pids = array($pids);
1260 $keys = XDB
::iterator("(SELECT pid, name, type, IF(type = 'nickname', 2, 1) AS score, '' AS public
1261 FROM profile_private_names
1264 (SELECT pid, lastname_main, 'lastname' AS type, 10 AS score, 'public' AS public
1265 FROM profile_public_names
1266 WHERE lastname_main != '' AND pid IN {?})
1268 (SELECT pid, lastname_marital, 'lastname' AS type, 10 AS score, 'public' AS public
1269 FROM profile_public_names
1270 WHERE lastname_marital != '' AND pid IN {?})
1272 (SELECT pid, lastname_ordinary, 'lastname' AS type, 10 AS score, 'public' AS public
1273 FROM profile_public_names
1274 WHERE lastname_ordinary != '' AND pid IN {?})
1276 (SELECT pid, firstname_main, 'firstname' AS type, 10 AS score, 'public' AS public
1277 FROM profile_public_names
1278 WHERE firstname_main != '' AND pid IN {?})
1280 (SELECT pid, firstname_ordinary, 'firstname' AS type, 10 AS score, 'public' AS public
1281 FROM profile_public_names
1282 WHERE firstname_ordinary != '' AND pid IN {?})
1284 (SELECT pid, pseudonym, 'nickname' AS type, 10 AS score, 'public' AS public
1285 FROM profile_public_names
1286 WHERE pseudonym != '' AND pid IN {?})",
1287 $pids, $pids, $pids, $pids, $pids, $pids, $pids);
1289 while ($key = $keys->next()) {
1290 if ($key['name'] == '') {
1294 $toks = split_name_for_search($key['name']);
1295 $toks = array_reverse($toks);
1297 /* Split the score between the tokens to avoid the user to be over-rated.
1298 * Let says my user name is "Machin-Truc Bidule" and I also have a user named
1299 * 'Machin Truc'. Distributing the score force "Machin Truc" to be displayed
1300 * before "Machin-Truc" for both "Machin Truc" and "Machin" searches.
1302 $eltScore = ceil(((float)$key['score'])/((float)count($toks)));
1304 foreach ($toks as $tok) {
1305 $token = $tok . $token;
1306 $names["$pid-$token"] = XDB
::format('({?}, {?}, {?}, {?}, {?}, {?})',
1307 $token, $pid, soundex_fr($token),
1308 $eltScore, $key['public'], $key['type']);
1312 XDB
::startTransaction();
1314 XDB
::execute('DELETE FROM search_name
1317 if (count($names) > 0) {
1318 XDB
::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags, general_type)
1319 VALUES ' . implode(', ', $names));
1326 /** The school identifier consists of 6 digits. The first 3 represent the
1327 * promotion entry year. The last 3 indicate the student's rank.
1329 * Our identifier consists of 8 digits and both half have the same role.
1330 * This enables us to deal with bigger promotions and with a wider range
1333 * getSchoolId returns a school identifier given one of ours.
1334 * getXorgId returns a X.org identifier given a school identifier.
1336 public static function getSchoolId($xorgId)
1338 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1342 $year = intval(substr($xorgId, 0, 4));
1343 $rank = intval(substr($xorgId, 5, 3));
1346 } elseif ($year < 2000) {
1347 $year = intval(substr(1900 - $year, 1, 3));
1348 return sprintf('%02u0%03u', $year, $rank);
1350 $year = intval(substr(1900 - $year, 1, 3));
1351 return sprintf('%03u%03u', $year, $rank);
1355 public static function getXorgId($schoolId)
1357 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1361 $year = intval(substr($schoolId, 0, 3));
1362 $rank = intval(substr($schoolId, 3, 3));
1370 return sprintf('%04u%04u', 1900 +
$year, $rank);
1376 /** Iterator over a set of Profiles
1378 class ProfileIterator
implements PlIterator
1380 private $iterator = null
;
1382 private $visibility;
1384 const FETCH_ALL
= 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
1386 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, $visibility = null
)
1388 require_once 'profilefields.inc.php';
1390 if ($visibility === null
) {
1391 $visibility = Visibility
::defaultForRead();
1394 $this->fields
= $fields;
1395 $this->visibility
= $visibility;
1398 $callbacks = array();
1401 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1402 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1404 $fields = $fields & self
::FETCH_ALL
;
1405 for ($field = 1; $field < $fields; $field *= 2) {
1406 if (($fields & $field) ) {
1407 $callbacks[$field] = $cb;
1408 $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility);
1412 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1415 private function hasData($field, $vals)
1417 return ($this->fields
& $field) && ($vals[$field] != null
);
1420 private function fillProfile(array $vals)
1422 $pf = Profile
::get($vals[0], 0x0, $this->visibility
);
1423 $pf->setFetchedFields($this->fields
);
1425 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1426 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1428 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1429 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1431 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1432 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1434 if ($this->hasData(Profile
::FETCH_JOB_TERMS
, $vals)) {
1435 $pf->setJobTerms($vals[Profile
::FETCH_JOB_TERMS
]);
1438 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1439 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1441 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1442 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1444 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1445 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1447 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1448 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1454 public function next()
1456 $vals = $this->iterator
->next();
1457 if ($vals == null
) {
1460 return $this->fillProfile($vals);
1463 public function first()
1465 return $this->iterator
->first();
1468 public function last()
1470 return $this->iterator
->last();
1473 public function total()
1475 return $this->iterator
->total();
1479 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: