2 /***************************************************************************
3 * Copyright (C) 2003-2011 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;
115 const FETCH_MINIFICHES
= 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
117 const FETCH_ALL
= 0x001FFF; // OR of FETCH_*
119 static public $descriptions = array(
120 'search_names' => 'Noms',
121 'nationality1' => 'Nationalité',
122 'nationality2' => '2e nationalité',
123 'nationality3' => '3e nationalité',
124 'promo_display' => 'Promotion affichée',
125 'email_directory' => 'Email annuaire papier',
126 'networking' => 'Messageries…',
127 'tels' => 'Téléphones',
128 'edus' => 'Formations',
129 'main_edus' => 'Formations à l\'X',
130 'promo' => 'Promotion de sortie',
131 'birthdate' => 'Date de naissance',
132 'yourself' => 'Nom affiché',
133 'freetext' => 'Commentaire',
134 'freetext_pub' => 'Affichage du commentaire',
135 'photo' => 'Photographie',
136 'photo_pub' => 'Affichage de la photographie',
137 'addresses' => 'Adresses',
141 'section' => 'Section',
142 'binets' => 'Binets',
143 'medals' => 'Décorations',
144 'medals_pub' => 'Affichage des décorations',
145 'competences' => 'Compétences',
146 'langues' => 'Langues',
147 'expertise' => 'Expertises (mentoring)',
148 'terms' => 'Compétences (mentoring)',
149 'countries' => 'Pays (mentoring)',
150 'deltaten' => 'Opération N N-10',
153 private $fetched_fields = 0x000000;
158 private $owner_fetched = false
;
159 private $data = array();
161 private $visibility = null
;
164 private function __construct(array $data, Visibility
$visibility)
167 $this->pid
= $this->data
['pid'];
168 $this->hrpid
= $this->data
['hrpid'];
169 $this->visibility
= $visibility;
177 public function hrid()
182 public function owner()
184 if ($this->owner
== null
&& !$this->owner_fetched
) {
185 $this->owner_fetched
= true
;
186 $this->owner
= User
::getSilent($this);
191 public function isActive()
193 if ($this->owner()) {
194 return $this->owner
->isActive();
199 public function promo($details = false
)
201 if ($details && ($this->program ||
$this->fieldid
)) {
203 if ($this->program
) {
204 $text[] = $this->program
;
206 if ($this->fieldid
) {
207 $fieldsList = DirEnum
::getOptions(DirEnum
::EDUFIELDS
);
208 $text[] = $fieldsList[$this->fieldid
];
210 return $this->promo
. ' (' . implode(', ', $text) . ')';
216 public function yearpromo()
218 return $this->promo_year
;
221 /** Check if user is an orange (associated with several promos)
223 public function isMultiPromo()
225 return $this->grad_year
!= $this->entry_year +
$this->mainEducationDuration();
228 /** Returns an array with all associated promo years.
230 public function yearspromo()
233 $d = -$this->deltaPromoToGradYear();
234 for ($g = $this->entry_year +
$this->mainEducationDuration(); $g <= $this->grad_year
; ++
$g) {
240 public function mainEducation()
242 if (empty($this->promo
)) {
245 return $this->promo
{0};
249 public function mainGrade()
251 switch ($this->mainEducation()) {
253 return UserFilter
::GRADE_ING
;
255 return UserFilter
::GRADE_MST
;
257 return UserFilter
::GRADE_PHD
;
263 public function mainEducationDuration()
265 switch ($this->mainEducation()) {
277 public static function educationDuration($education)
279 switch ($education) {
291 /** Number of years between the promotion year until the
292 * graduation year. In standard schools it's 0, but for
293 * Polytechnique the promo year is the entry year.
295 public function deltaPromoToGradYear()
297 if ($this->mainEducation() == 'X') {
298 return $this->mainEducationDuration();
303 // Returns the profile's color.
304 public function promoColor()
306 switch ($this->mainEducation()) {
308 if (($this->yearpromo() %
2) === 0) {
322 /** Print a name with the given formatting:
330 public function name($format)
332 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
333 array($this->isFemale() ?
'•' : '',
334 $this->firstName(), $this->lastName(),
335 $this->fullName(), $this->shortName(),
336 $this->promo()), $format);
339 public function fullName($with_promo = false
)
342 return $this->full_name
. ' (' . $this->promo
. ')';
344 return $this->full_name
;
347 public function shortName($with_promo = false
)
350 return $this->short_name
. ' (' . $this->promo
. ')';
352 return $this->short_name
;
355 public function firstName()
357 return $this->firstname
;
360 public function firstNames()
362 return $this->nameVariants(self
::FIRSTNAME
);
365 public function lastName()
367 return $this->lastname
;
370 public function lastNames()
372 return $this->nameVariants(self
::LASTNAME
);
375 public function isFemale()
377 return $this->sex
== PlUser
::GENDER_FEMALE
;
380 public function isDead()
382 return ($this->deathdate
!= null
);
385 public function displayEmail()
388 if ($o != null
&& $this->isVisible(Visibility
::EXPORT_PRIVATE
)) {
389 return $o->bestEmail();
391 return $this->email_directory
;
395 public function data()
401 private function nameVariants($type)
403 $vals = array($this->$type);
404 foreach (self
::$name_variants[$type] as $var) {
405 $vartype = $type . '_' . $var;
406 $varname = $this->$vartype;
407 if ($varname != null
&& $varname != "") {
411 return array_unique($vals);
414 public function nationalities()
417 $nationalities = DirEnum
::getOptions(DirEnum
::NATIONALITIES
);
418 if ($this->nationality1
) {
419 $nats[$this->nationality1
] = $nationalities[$this->nationality1
];
421 if ($this->nationality2
) {
422 $nats[$this->nationality2
] = $nationalities[$this->nationality2
];
424 if ($this->nationality3
) {
425 $nats[$this->nationality3
] = $nationalities[$this->nationality3
];
430 public function __get($name)
432 if (property_exists($this, $name)) {
436 if (isset($this->data
[$name])) {
437 return $this->data
[$name];
443 public function __isset($name)
445 return property_exists($this, $name) ||
isset($this->data
[$name]);
448 public function __unset($name)
450 if (property_exists($this, $name)) {
453 unset($this->data
[$name]);
460 * *always deletes in: profile_addresses, profile_binets, profile_deltaten,
461 * profile_job, profile_langskills, profile_mentor, profile_networking,
462 * profile_partnersharing_settings, profile_phones, profile_skills,
464 * *always keeps in: profile_corps, profile_display, profile_education,
465 * profile_medals, profile_*_names, profile_photos, search_name
466 * *modifies: profiles
468 public function clear()
471 'profile_job', 'profile_langskills', 'profile_mentor',
472 'profile_networking', 'profile_skills', 'watch_profile',
473 'profile_phones', 'profile_addresses', 'profile_binets',
474 'profile_deltaten', 'profile_partnersharing_settings');
476 foreach ($tables as $t) {
477 XDB
::execute('DELETE FROM ' . $t . '
482 XDB
::execute("UPDATE profiles
483 SET cv = NULL, freetext = NULL, freetext_pub = 'private',
484 medals_pub = 'private', alias_pub = 'private',
485 email_directory = NULL
490 /** Determine whether an item with visibility $visibility can be displayed
491 * with the current level of visibility of the profile
492 * @param $visibility The level of visibility to be checked
494 public function isVisible($visibility)
496 return $this->visibility
->isVisible($visibility);
499 /** Stores the list of fields which have already been fetched for this Profile
501 public function setFetchedFields($fields)
503 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
504 Platal
::page()->kill("Invalid fetched fields: $fields");
507 $this->fetched_fields
= $fields;
510 /** Have we already fetched this field ?
512 private function fetched($field)
514 if (!array_key_exists($field, ProfileField
::$fields)) {
515 Platal
::page()->kill("Invalid field: $field");
518 return ($this->fetched_fields
& $field);
521 /** If not already done, fetches data for the given field
522 * @param $field One of the Profile::FETCH_*
523 * @return A ProfileField, or null
525 private function getProfileField($field)
527 if (!array_key_exists($field, ProfileField
::$fields)) {
528 Platal
::page()->kill("Invalid field: $field");
530 if ($this->fetched($field)) {
533 $this->fetched_fields
= $this->fetched_fields |
$field;
536 $cls = ProfileField
::$fields[$field];
538 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
541 /** Consolidates internal data (addresses, phones, jobs)
543 private function consolidateFields()
545 // Link phones to addresses
546 if ($this->phones
!= null
) {
547 if ($this->addresses
!= null
) {
548 $this->addresses
->addPhones($this->phones
);
551 if ($this->jobs
!= null
) {
552 $this->jobs
->addPhones($this->phones
);
556 // Link addresses to jobs
557 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
558 $this->jobs
->addAddresses($this->addresses
);
561 // Link jobterms to jobs
562 if ($this->jobs
!= null
&& $this->jobterms
!= null
) {
563 $this->jobs
->addJobTerms($this->jobterms
);
569 private $photo = null
;
570 public function getPhoto($fallback = true
, $data = false
)
572 if ($this->has_photo
) {
573 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
574 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y, last_update
576 WHERE pid = {?}', $this->pid
);
577 $this->photo
= PlImage
::fromData($res['attach'], 'image/' .$res['attachmime'], $res['x'], $res['y'], $res['last_update']);
578 } else if ($this->photo
== null
) {
579 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
582 } else if ($fallback) {
583 if ($this->mainEducation() == 'X') {
584 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_x.png', 'image/png');
586 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_md.png', 'image/png');
593 private $addresses = null
;
594 public function setAddresses(ProfileAddresses
$addr)
596 $this->addresses
= $addr;
597 $this->consolidateFields();
600 private function fetchAddresses()
602 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
603 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
605 $this->setAddresses($addr);
606 $this->fetchPhones();
611 public function getAddresses($flags, $limit = null
)
613 $this->fetchAddresses();
615 if ($this->addresses
== null
) {
618 return $this->addresses
->get($flags, $limit);
621 public function iterAddresses($flags, $limit = null
)
623 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
626 public function getMainAddress()
628 $main = $this->getAddresses(self
::ADDRESS_MAIN
);
629 $perso = $this->getAddresses(self
::ADDRESS_PERSO
);
632 return array_pop($main);
633 } else if (count($perso)) {
634 return array_pop($perso);
642 private $phones = null
;
643 public function setPhones(ProfilePhones
$phones)
645 $this->phones
= $phones;
646 $this->consolidateFields();
649 private function fetchPhones()
651 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
652 $phones = $this->getProfileField(self
::FETCH_PHONES
);
653 if (isset($phones)) {
654 $this->setPhones($phones);
659 public function getPhones($flags, $limit = null
)
661 $this->fetchPhones();
662 if ($this->phones
== null
) {
665 return $this->phones
->get($flags, $limit);
670 private $educations = null
;
671 public function setEducations(ProfileEducation
$edu)
673 $this->educations
= $edu;
676 public function getEducations($flags, $limit = null
)
678 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
679 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
682 if ($this->educations
== null
) {
685 return $this->educations
->get($flags, $limit);
688 public function getExtraEducations($limit = null
)
690 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
695 private $corps = null
;
696 public function setCorps(ProfileCorps
$corps)
698 $this->corps
= $corps;
701 public function getCorps()
703 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
704 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
711 private $networks = null
;
712 public function setNetworking(ProfileNetworking
$nw)
714 $this->networks
= $nw;
717 public function getNetworking($flags, $limit = null
)
719 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
720 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
722 $this->setNetworking($nw);
725 if ($this->networks
== null
) {
728 return $this->networks
->get($flags, $limit);
731 public function getWebSite()
733 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
734 if (count($site) != 1) {
737 $site = array_pop($site);
744 private $jobs = null
;
745 public function setJobs(ProfileJobs
$jobs)
748 $this->consolidateFields();
751 private function fetchJobs()
753 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
754 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
756 $this->setJobs($jobs);
757 $this->fetchAddresses();
762 public function getJobs($flags, $limit = null
)
766 if ($this->jobs
== null
) {
769 return $this->jobs
->get($flags, $limit);
772 public function getMainJob()
774 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
775 if (count($job) != 1) {
778 return array_pop($job);
783 private $jobterms = null
;
784 public function setJobTerms(ProfileJobTerms
$jobterms)
786 $this->jobterms
= $jobterms;
787 $this->consolidateFields();
790 private $mentor_countries = null
;
791 public function setMentoringCountries(ProfileMentoringCountries
$countries)
793 $this->mentor_countries
= $countries;
796 public function getMentoringCountries()
798 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
799 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
802 if ($this->mentor_countries
== null
) {
805 return $this->mentor_countries
->countries
;
809 /** List of job terms to specify mentoring */
810 private $mentor_terms = null
;
812 * set job terms to specify mentoring
813 * @param $terms a ProfileMentoringTerms object listing terms only for this profile
815 public function setMentoringTerms(ProfileMentoringTerms
$terms)
817 $this->mentor_terms
= $terms;
820 * get all job terms that specify mentoring
821 * @return an array of JobTerms objects
823 public function getMentoringTerms()
825 if ($this->mentor_terms
== null
&& !$this->fetched(self
::FETCH_MENTOR_TERMS
)) {
826 $this->setMentoringTerms($this->getProfileField(self
::FETCH_MENTOR_TERMS
));
829 if ($this->mentor_terms
== null
) {
832 return $this->mentor_terms
->get();
839 /** Find out whether this profile may take part to the "DeltaTen" operation.
840 * @param $role Which role to select ('young' or 'old')
841 * @return Boolean: whether it is enabled.
843 const DELTATEN_YOUNG
= 'young';
844 const DELTATEN_OLD
= 'old';
845 public function isDeltaTenEnabled($role)
849 case self
::DELTATEN_YOUNG
:
850 return ($this->mainGrade() == UserFilter
::GRADE_ING
&& $this->yearpromo() >= $globals->deltaten
->first_promo_young
);
851 case self
::DELTATEN_OLD
:
852 // Roughly compute the current promo in second year on the campus:
853 // Promo 2010 is in second year between 09/2011 and 08/2012 => use 2012.
854 // DeltaTen program begins around January of the second year.
855 $promo_on_platal = ((int) date('Y')) - 2;
856 return ($this->mainGrade() == UserFilter
::GRADE_ING
&& $this->yearpromo() >= $globals->deltaten
->first_promo_young
- 10 && $this->yearpromo() <= $promo_on_platal - 10);
858 Platal
::assert(false
, "Invalid DeltaTen role $role");
862 /** Retrieve the "Deltaten" message of the user.
863 * Returns "null" if the message is empty or the user is not taking part to the
864 * DeltaTen operation.
866 public function getDeltatenMessage()
868 if ($this->isDeltaTenEnabled(self
::DELTATEN_OLD
)) {
869 return $this->deltaten_message
;
877 public function getBinets()
879 if ($this->visibility
->isVisible(Visibility
::EXPORT_PRIVATE
)) {
880 return XDB
::fetchColumn('SELECT binet_id
882 WHERE pid = {?}', $this->id());
887 public function getBinetsNames()
889 if ($this->visibility
->isVisible(Visibility
::EXPORT_PRIVATE
)) {
890 return XDB
::fetchColumn('SELECT text
891 FROM profile_binets AS pb
892 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
893 WHERE pb.pid = {?}', $this->id());
901 private $medals = null
;
902 public function setMedals(ProfileMedals
$medals)
904 $this->medals
= $medals;
907 public function getMedals()
909 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
910 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
912 if ($this->medals
== null
) {
915 return $this->medals
->medals
;
918 /** Sharing data with partner websites
920 private $partners_settings = null
;
921 public function setPartnersSettings(ProfilePartnerSharing
$partners_settings)
923 $this->partners_settings
= $partners_settings;
926 public function getPartnerSettings($partner_id)
928 if ($this->partners_settings
=== null
&& !$this->fetched(self
::FETCH_PARTNER
)) {
929 $this->setPartnersSettings($this->getProfileField(self
::FETCH_PARTNER
));
931 if ($this->partners_settings
=== null
) {
932 return PartnerSettings
::getEmpty($partner_id);
934 return $this->partners_settings
->get($partner_id);
937 public function compareNames($firstname, $lastname)
939 $_lastname = mb_strtoupper($this->lastName());
940 $_firstname = mb_strtoupper($this->firstName());
941 $lastname = mb_strtoupper($lastname);
942 $firstname = mb_strtoupper($firstname);
944 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
945 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
948 foreach ($tokens as $str) {
949 $isOk &= (strpos($_lastname, $str) !== false
);
950 $maxlen = max($maxlen, strlen($str));
953 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
958 public function export()
961 'hrpid' => $this->hrid(),
962 'display_name' => $this->shortName(),
963 'full_name' => $this->fullName(),
964 'directory_name' => $this->directory_name
,
965 'promo' => $this->promo(),
966 'year_promo' => $this->yearpromo(),
967 'is_active' => $this->isActive(),
968 'first_name' => $this->firstName(),
969 'last_name' => $this->lastName(),
970 'is_female' => $this->isFemale(),
974 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
976 if (count($pids) == 0) {
980 if ($respect_order) {
981 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
986 if ($visibility === null
) {
987 $visibility = Visibility
::defaultForRead();
990 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
991 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
992 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
993 p.last_change, p.nationality1, p.nationality2, p.nationality3,
994 IF (p.freetext_pub >= {?}, p.freetext, NULL) AS freetext,
995 pe.entry_year, pe.grad_year, pe.promo_year, pe.program, pe.fieldid,
996 IF ({?}, pse.text, NULL) AS section,
997 ppn.firstname_main AS firstname, ppn.lastname_main AS lastname, IF ({?}, pn.name, NULL) AS nickname,
998 IF (ppn.firstname_ordinary = \'\', ppn.firstname_main, ppn.firstname_ordinary) AS firstname_ordinary,
999 IF (ppn.lastname_ordinary = \'\', ppn.firstname_main, ppn.lastname_ordinary) AS lastname_ordinary,
1000 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
1001 pd.directory_name, pd.public_name, pd.private_name,
1002 IF (pp.pub >= {?}, pp.display_tel, NULL) AS mobile,
1003 (ph.pub >= {?} AND ph.attach IS NOT NULL) AS has_photo, ph.pub as photo_pub,
1004 ph.x AS photo_width, ph.y AS photo_height,
1005 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
1006 pm.expertise AS mentor_expertise,
1007 IF ({?}, pdt.message, NULL) AS deltaten_message,
1010 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
1011 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
1012 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
1013 INNER JOIN profile_public_names AS ppn ON (ppn.pid = p.pid)
1014 LEFT JOIN profile_private_names AS pn ON (pn.pid = p.pid AND type = \'nickname\')
1015 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
1016 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
1017 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
1018 LEFT JOIN profile_deltaten AS pdt ON (pdt.pid = p.pid)
1019 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
1023 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // CV
1024 $visibility->level(), // freetext
1025 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // section
1026 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // nickname
1027 $visibility->level(), // mobile
1028 $visibility->level(), // photo
1029 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // deltaten_message
1032 return new ProfileIterator($it, $pids, $fields, $visibility);
1035 public static function getPID($login)
1037 if ($login instanceof PlUser
) {
1038 return XDB
::fetchOneCell('SELECT pid
1039 FROM account_profiles
1040 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
1042 } else if (ctype_digit($login)) {
1043 return XDB
::fetchOneCell('SELECT pid
1045 WHERE pid = {?}', $login);
1047 return XDB
::fetchOneCell('SELECT pid
1049 WHERE hrpid = {?}', $login);
1053 public static function getPIDsFromUIDs($uids, $respect_order = true
)
1055 if ($respect_order) {
1056 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
1060 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
1061 FROM account_profiles AS ap
1062 WHERE FIND_IN_SET(\'owner\', ap.perms)
1063 AND ap.uid IN ' . XDB
::formatArray($uids) .'
1067 /** Return the profile associated with the given login.
1069 public static function get($login, $fields = 0x0000, $visibility = null
)
1071 if ($visibility === null
) {
1072 $visibility = Visibility
::defaultForRead();
1075 if (is_array($login)) {
1076 $pf = new Profile($login, $visibility);
1079 $pid = self
::getPID($login);
1080 if (!is_null($pid)) {
1081 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
1084 /* Let say we can identify a profile using the identifiers of its owner.
1086 if (!($login instanceof PlUser
)) {
1087 $user = User
::getSilent($login);
1088 if ($user && $user->hasProfile()) {
1089 return $user->profile(false
, $fields, $visibility);
1096 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1098 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
1101 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1103 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
1106 /** Return profiles for the list of pids.
1108 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
1110 if (count($pids) == 0) {
1113 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
1114 $profiles = array();
1115 while ($p = $it->next()) {
1116 $profiles[$p->id()] = $p;
1121 /** Return profiles for uids.
1123 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
1125 if (count($uids) == 0) {
1128 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
1131 public static function isDisplayName($name)
1133 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
1134 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
1135 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1136 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1139 /** Returns the closest "accounts only" name type for $name
1141 public static function getAccountEquivalentName($name)
1145 case self
::DN_DIRECTORY
:
1147 return 'directory_name';
1149 case self
::DN_PUBLIC
:
1151 case self
::DN_PRIVATE
:
1152 case self
::DN_SHORT
:
1153 case self
::DN_YOURSELF
:
1155 return 'display_name';
1159 public static function rebuildSearchTokens($pids, $transaction = true
)
1161 require_once 'name.func.inc.php';
1162 if (!is_array($pids)) {
1163 $pids = array($pids);
1165 $keys = XDB
::iterator("(SELECT pid, name, type, IF(type = 'nickname', 2, 1) AS score, '' AS public
1166 FROM profile_private_names
1169 (SELECT pid, lastname_main, 'lastname' AS type, 10 AS score, 'public' AS public
1170 FROM profile_public_names
1171 WHERE lastname_main != '' AND pid IN {?})
1173 (SELECT pid, lastname_marital, 'lastname' AS type, 10 AS score, 'public' AS public
1174 FROM profile_public_names
1175 WHERE lastname_marital != '' AND pid IN {?})
1177 (SELECT pid, lastname_ordinary, 'lastname' AS type, 10 AS score, 'public' AS public
1178 FROM profile_public_names
1179 WHERE lastname_ordinary != '' AND pid IN {?})
1181 (SELECT pid, firstname_main, 'firstname' AS type, 10 AS score, 'public' AS public
1182 FROM profile_public_names
1183 WHERE firstname_main != '' AND pid IN {?})
1185 (SELECT pid, firstname_ordinary, 'firstname' AS type, 10 AS score, 'public' AS public
1186 FROM profile_public_names
1187 WHERE firstname_ordinary != '' AND pid IN {?})
1189 (SELECT pid, pseudonym, 'nickname' AS type, 10 AS score, 'public' AS public
1190 FROM profile_public_names
1191 WHERE pseudonym != '' AND pid IN {?})",
1192 $pids, $pids, $pids, $pids, $pids, $pids, $pids);
1194 while ($key = $keys->next()) {
1195 if ($key['name'] == '') {
1199 $toks = split_name_for_search($key['name']);
1200 $toks = array_reverse($toks);
1202 /* Split the score between the tokens to avoid the user to be over-rated.
1203 * Let says my user name is "Machin-Truc Bidule" and I also have a user named
1204 * 'Machin Truc'. Distributing the score force "Machin Truc" to be displayed
1205 * before "Machin-Truc" for both "Machin Truc" and "Machin" searches.
1207 $eltScore = ceil(((float)$key['score'])/((float)count($toks)));
1209 foreach ($toks as $tok) {
1210 $token = $tok . $token;
1211 $names["$pid-$token"] = XDB
::format('({?}, {?}, {?}, {?}, {?}, {?})',
1212 $token, $pid, soundex_fr($token),
1213 $eltScore, $key['public'], $key['type']);
1217 XDB
::startTransaction();
1219 XDB
::execute('DELETE FROM search_name
1222 if (count($names) > 0) {
1223 XDB
::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags, general_type)
1224 VALUES ' . implode(', ', $names));
1231 /** The school identifier consists of 6 digits. The first 3 represent the
1232 * promotion entry year. The last 3 indicate the student's rank.
1234 * Our identifier consists of 8 digits and both half have the same role.
1235 * This enables us to deal with bigger promotions and with a wider range
1238 * getSchoolId returns a school identifier given one of ours.
1239 * getXorgId returns a X.org identifier given a school identifier.
1241 public static function getSchoolId($xorgId)
1243 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1247 $year = intval(substr($xorgId, 0, 4));
1248 $rank = intval(substr($xorgId, 5, 3));
1251 } elseif ($year < 2000) {
1252 $year = intval(substr(1900 - $year, 1, 3));
1253 return sprintf('%02u0%03u', $year, $rank);
1255 $year = intval(substr(1900 - $year, 1, 3));
1256 return sprintf('%03u%03u', $year, $rank);
1260 public static function getXorgId($schoolId)
1262 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1266 $year = intval(substr($schoolId, 0, 3));
1267 $rank = intval(substr($schoolId, 3, 3));
1275 return sprintf('%04u%04u', 1900 +
$year, $rank);
1281 /** Iterator over a set of Profiles
1283 class ProfileIterator
implements PlIterator
1285 private $iterator = null
;
1287 private $visibility;
1289 const FETCH_ALL
= 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
1291 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, $visibility = null
)
1293 require_once 'profilefields.inc.php';
1295 if ($visibility === null
) {
1296 $visibility = Visibility
::defaultForRead();
1299 $this->fields
= $fields;
1300 $this->visibility
= $visibility;
1303 $callbacks = array();
1306 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1307 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1309 $fields = $fields & self
::FETCH_ALL
;
1310 for ($field = 1; $field < $fields; $field *= 2) {
1311 if (($fields & $field) ) {
1312 $callbacks[$field] = $cb;
1313 $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility);
1317 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1320 private function hasData($field, $vals)
1322 return ($this->fields
& $field) && ($vals[$field] != null
);
1325 private function fillProfile(array $vals)
1327 $pf = Profile
::get($vals[0], 0x0, $this->visibility
);
1328 $pf->setFetchedFields($this->fields
);
1330 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1331 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1333 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1334 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1336 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1337 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1339 if ($this->hasData(Profile
::FETCH_JOB_TERMS
, $vals)) {
1340 $pf->setJobTerms($vals[Profile
::FETCH_JOB_TERMS
]);
1343 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1344 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1346 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1347 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1349 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1350 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1352 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1353 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1359 public function next()
1361 $vals = $this->iterator
->next();
1362 if ($vals == null
) {
1365 return $this->fillProfile($vals);
1368 public function first()
1370 return $this->iterator
->first();
1373 public function last()
1375 return $this->iterator
->last();
1378 public function total()
1380 return $this->iterator
->total();
1384 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: