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;
114 const FETCH_MINIFICHES
= 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
116 const FETCH_ALL
= 0x000FFF; // OR of FETCH_*
118 static public $descriptions = array(
119 'search_names' => 'Noms',
120 'nationality1' => 'Nationalité',
121 'nationality2' => '2e nationalité',
122 'nationality3' => '3e nationalité',
123 'promo_display' => 'Promotion affichée',
124 'email_directory' => 'Email annuaire papier',
125 'networking' => 'Messageries…',
126 'tels' => 'Téléphones',
127 'edus' => 'Formations',
128 'main_edus' => 'Formations à l\'X',
129 'promo' => 'Promotion de sortie',
130 'birthdate' => 'Date de naissance',
131 'yourself' => 'Nom affiché',
132 'freetext' => 'Commentaire',
133 'freetext_pub' => 'Affichage du commentaire',
134 'photo' => 'Photographie',
135 'photo_pub' => 'Affichage de la photographie',
136 'addresses' => 'Adresses',
140 'section' => 'Section',
141 'binets' => 'Binets',
142 'medals' => 'Décorations',
143 'medals_pub' => 'Affichage des décorations',
144 'competences' => 'Compétences',
145 'langues' => 'Langues',
146 'expertise' => 'Expertises (mentoring)',
147 'terms' => 'Compétences (mentoring)',
148 'countries' => 'Pays (mentoring)',
149 'deltaten' => 'Opération N N-10',
152 private $fetched_fields = 0x000000;
157 private $owner_fetched = false
;
158 private $data = array();
160 private $visibility = null
;
163 private function __construct(array $data)
166 $this->pid
= $this->data
['pid'];
167 $this->hrpid
= $this->data
['hrpid'];
168 $this->visibility
= new ProfileVisibility();
176 public function hrid()
181 public function owner()
183 if ($this->owner
== null
&& !$this->owner_fetched
) {
184 $this->owner_fetched
= true
;
185 $this->owner
= User
::getSilent($this);
190 public function isActive()
192 if ($this->owner()) {
193 return $this->owner
->isActive();
198 public function promo($details = false
)
200 if ($details && ($this->program ||
$this->fieldid
)) {
202 if ($this->program
) {
203 $text[] = $this->program
;
205 if ($this->fieldid
) {
206 $fieldsList = DirEnum
::getOptions(DirEnum
::EDUFIELDS
);
207 $text[] = $fieldsList[$this->fieldid
];
209 return $this->promo
. ' (' . implode(', ', $text) . ')';
215 public function yearpromo()
217 return $this->promo_year
;
220 /** Check if user is an orange (associated with several promos)
222 public function isMultiPromo()
224 return $this->grad_year
!= $this->entry_year +
$this->mainEducationDuration();
227 /** Returns an array with all associated promo years.
229 public function yearspromo()
232 $d = -$this->deltaPromoToGradYear();
233 for ($g = $this->entry_year +
$this->mainEducationDuration(); $g <= $this->grad_year
; ++
$g) {
239 public function mainEducation()
241 if (empty($this->promo
)) {
244 return $this->promo
{0};
248 public function mainGrade()
250 switch ($this->mainEducation()) {
252 return UserFilter
::GRADE_ING
;
254 return UserFilter
::GRADE_MST
;
256 return UserFilter
::GRADE_PHD
;
262 public function mainEducationDuration()
264 switch ($this->mainEducation()) {
276 public static function educationDuration($education)
278 switch ($education) {
290 /** Number of years between the promotion year until the
291 * graduation year. In standard schools it's 0, but for
292 * Polytechnique the promo year is the entry year.
294 public function deltaPromoToGradYear()
296 if ($this->mainEducation() == 'X') {
297 return $this->mainEducationDuration();
302 /** Print a name with the given formatting:
310 public function name($format)
312 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
313 array($this->isFemale() ?
'•' : '',
314 $this->firstName(), $this->lastName(),
315 $this->fullName(), $this->shortName(),
316 $this->promo()), $format);
319 public function fullName($with_promo = false
)
322 return $this->full_name
. ' (' . $this->promo
. ')';
324 return $this->full_name
;
327 public function shortName($with_promo = false
)
330 return $this->short_name
. ' (' . $this->promo
. ')';
332 return $this->short_name
;
335 public function firstName()
337 return $this->firstname
;
340 public function firstNames()
342 return $this->nameVariants(self
::FIRSTNAME
);
345 public function lastName()
347 return $this->lastname
;
350 public function lastNames()
352 return $this->nameVariants(self
::LASTNAME
);
355 public function isFemale()
357 return $this->sex
== PlUser
::GENDER_FEMALE
;
360 public function isDead()
362 return ($this->deathdate
!= null
);
365 public function displayEmail()
369 return $o->bestEmail();
371 return $this->email_directory
;
375 public function data()
381 private function nameVariants($type)
383 $vals = array($this->$type);
384 foreach (self
::$name_variants[$type] as $var) {
385 $vartype = $type . '_' . $var;
386 $varname = $this->$vartype;
387 if ($varname != null
&& $varname != "") {
391 return array_unique($vals);
394 public function nationalities()
397 $countries = DirEnum
::getOptions(DirEnum
::COUNTRIES
);
398 if ($this->nationality1
) {
399 $nats[$this->nationality1
] = $countries[$this->nationality1
];
401 if ($this->nationality2
) {
402 $nats[$this->nationality2
] = $countries[$this->nationality2
];
404 if ($this->nationality3
) {
405 $nats[$this->nationality3
] = $countries[$this->nationality3
];
410 public function __get($name)
412 if (property_exists($this, $name)) {
416 if (isset($this->data
[$name])) {
417 return $this->data
[$name];
423 public function __isset($name)
425 return property_exists($this, $name) ||
isset($this->data
[$name]);
428 public function __unset($name)
430 if (property_exists($this, $name)) {
433 unset($this->data
[$name]);
440 * *always deletes in: profile_addresses, profile_binets, profile_deltaten,
441 * profile_job, profile_langskills, profile_mentor, profile_networking,
442 * profile_phones, profile_skills, watch_profile
443 * *always keeps in: profile_corps, profile_display, profile_education,
444 * profile_medals, profile_*_names, profile_photos, search_name
445 * *modifies: profiles
447 public function clear()
450 'profile_job', 'profile_langskills', 'profile_mentor',
451 'profile_networking', 'profile_skills', 'watch_profile',
452 'profile_phones', 'profile_addresses', 'profile_binets',
455 foreach ($tables as $t) {
456 XDB
::execute('DELETE FROM ' . $t . '
461 XDB
::execute("UPDATE profiles
462 SET cv = NULL, freetext = NULL, freetext_pub = 'private',
463 medals_pub = 'private', alias_pub = 'private',
464 email_directory = NULL
469 /** Sets the level of visibility of the profile
470 * Sets $this->visibility to a list of valid visibilities.
471 * @param one of the self::VIS_* values
473 public function setVisibilityLevel($visibility)
475 $this->visibility
->setLevel($visibility);
478 /** Determine whether an item with visibility $visibility can be displayed
479 * with the current level of visibility of the profile
480 * @param $visibility The level of visibility to be checked
482 public function isVisible($visibility)
484 return $this->visibility
->isVisible($visibility);
487 /** Stores the list of fields which have already been fetched for this Profile
489 public function setFetchedFields($fields)
491 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
492 Platal
::page()->kill("Invalid fetched fields: $fields");
495 $this->fetched_fields
= $fields;
498 /** Have we already fetched this field ?
500 private function fetched($field)
502 if (!array_key_exists($field, ProfileField
::$fields)) {
503 Platal
::page()->kill("Invalid field: $field");
506 return ($this->fetched_fields
& $field);
509 /** If not already done, fetches data for the given field
510 * @param $field One of the Profile::FETCH_*
511 * @return A ProfileField, or null
513 private function getProfileField($field)
515 if (!array_key_exists($field, ProfileField
::$fields)) {
516 Platal
::page()->kill("Invalid field: $field");
518 if ($this->fetched($field)) {
521 $this->fetched_fields
= $this->fetched_fields |
$field;
524 $cls = ProfileField
::$fields[$field];
526 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
529 /** Consolidates internal data (addresses, phones, jobs)
531 private function consolidateFields()
533 // Link phones to addresses
534 if ($this->phones
!= null
) {
535 if ($this->addresses
!= null
) {
536 $this->addresses
->addPhones($this->phones
);
539 if ($this->jobs
!= null
) {
540 $this->jobs
->addPhones($this->phones
);
544 // Link addresses to jobs
545 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
546 $this->jobs
->addAddresses($this->addresses
);
549 // Link jobterms to jobs
550 if ($this->jobs
!= null
&& $this->jobterms
!= null
) {
551 $this->jobs
->addJobTerms($this->jobterms
);
557 private $photo = null
;
558 public function getPhoto($fallback = true
, $data = false
)
560 if ($this->has_photo
) {
561 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
562 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y, last_update
564 WHERE pid = {?}', $this->pid
);
565 $this->photo
= PlImage
::fromData($res['attach'], 'image/' .$res['attachmime'], $res['x'], $res['y'], $res['last_update']);
566 } else if ($this->photo
== null
) {
567 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
570 } else if ($fallback) {
571 if ($this->mainEducation() == 'X') {
572 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_x.png', 'image/png');
574 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_md.png', 'image/png');
581 private $addresses = null
;
582 public function setAddresses(ProfileAddresses
$addr)
584 $this->addresses
= $addr;
585 $this->consolidateFields();
588 private function fetchAddresses()
590 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
591 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
593 $this->setAddresses($addr);
594 $this->fetchPhones();
599 public function getAddresses($flags, $limit = null
)
601 $this->fetchAddresses();
603 if ($this->addresses
== null
) {
606 return $this->addresses
->get($flags, $limit);
609 public function iterAddresses($flags, $limit = null
)
611 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
614 public function getMainAddress()
616 $main = $this->getAddresses(self
::ADDRESS_MAIN
);
617 $perso = $this->getAddresses(self
::ADDRESS_PERSO
);
620 return array_pop($main);
621 } else if (count($perso)) {
622 return array_pop($perso);
630 private $phones = null
;
631 public function setPhones(ProfilePhones
$phones)
633 $this->phones
= $phones;
634 $this->consolidateFields();
637 private function fetchPhones()
639 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
640 $phones = $this->getProfileField(self
::FETCH_PHONES
);
641 if (isset($phones)) {
642 $this->setPhones($phones);
647 public function getPhones($flags, $limit = null
)
649 $this->fetchPhones();
650 if ($this->phones
== null
) {
653 return $this->phones
->get($flags, $limit);
658 private $educations = null
;
659 public function setEducations(ProfileEducation
$edu)
661 $this->educations
= $edu;
664 public function getEducations($flags, $limit = null
)
666 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
667 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
670 if ($this->educations
== null
) {
673 return $this->educations
->get($flags, $limit);
676 public function getExtraEducations($limit = null
)
678 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
683 private $corps = null
;
684 public function setCorps(ProfileCorps
$corps)
686 $this->corps
= $corps;
689 public function getCorps()
691 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
692 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
699 private $networks = null
;
700 public function setNetworking(ProfileNetworking
$nw)
702 $this->networks
= $nw;
705 public function getNetworking($flags, $limit = null
)
707 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
708 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
710 $this->setNetworking($nw);
713 if ($this->networks
== null
) {
716 return $this->networks
->get($flags, $limit);
719 public function getWebSite()
721 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
722 if (count($site) != 1) {
725 $site = array_pop($site);
732 private $jobs = null
;
733 public function setJobs(ProfileJobs
$jobs)
736 $this->consolidateFields();
739 private function fetchJobs()
741 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
742 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
744 $this->setJobs($jobs);
745 $this->fetchAddresses();
750 public function getJobs($flags, $limit = null
)
754 if ($this->jobs
== null
) {
757 return $this->jobs
->get($flags, $limit);
760 public function getMainJob()
762 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
763 if (count($job) != 1) {
766 return array_pop($job);
771 private $jobterms = null
;
772 public function setJobTerms(ProfileJobTerms
$jobterms)
774 $this->jobterms
= $jobterms;
775 $this->consolidateFields();
778 private $mentor_countries = null
;
779 public function setMentoringCountries(ProfileMentoringCountries
$countries)
781 $this->mentor_countries
= $countries;
784 public function getMentoringCountries()
786 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
787 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
790 if ($this->mentor_countries
== null
) {
793 return $this->mentor_countries
->countries
;
797 /** List of job terms to specify mentoring */
798 private $mentor_terms = null
;
800 * set job terms to specify mentoring
801 * @param $terms a ProfileMentoringTerms object listing terms only for this profile
803 public function setMentoringTerms(ProfileMentoringTerms
$terms)
805 $this->mentor_terms
= $terms;
808 * get all job terms that specify mentoring
809 * @return an array of JobTerms objects
811 public function getMentoringTerms()
813 if ($this->mentor_terms
== null
&& !$this->fetched(self
::FETCH_MENTOR_TERMS
)) {
814 $this->setMentoringTerms($this->getProfileField(self
::FETCH_MENTOR_TERMS
));
817 if ($this->mentor_terms
== null
) {
820 return $this->mentor_terms
->get();
827 /** Find out whether this profile may take part to the "DeltaTen" operation.
828 * @param $role Which role to select ('young' or 'old')
829 * @return Boolean: whether it is enabled.
831 const DELTATEN_YOUNG
= 'young';
832 const DELTATEN_OLD
= 'old';
833 public function isDeltaTenEnabled($role)
837 case self
::DELTATEN_YOUNG
:
838 return ($this->mainGrade() == UserFilter
::GRADE_ING
&& $this->yearpromo() >= $globals->deltaten
->first_promo_young
);
839 case self
::DELTATEN_OLD
:
840 // Roughly compute the current promo in second year on the campus:
841 // Promo 2010 is in second year between 09/2011 and 08/2012 => use 2012.
842 // DeltaTen program begins around January of the second year.
843 $promo_on_platal = ((int) date('Y')) - 2;
844 return ($this->mainGrade() == UserFilter
::GRADE_ING
&& $this->yearpromo() >= $globals->deltaten
->first_promo_young
- 10 && $this->yearpromo() <= $promo_on_platal - 10);
846 Platal
::assert(false
, "Invalid DeltaTen role $role");
850 /** Retrieve the "Deltaten" message of the user.
851 * Returns "null" if the message is empty or the user is not taking part to the
852 * DeltaTen operation.
854 public function getDeltatenMessage()
856 if ($this->isDeltaTenEnabled(self
::DELTATEN_OLD
)) {
857 return $this->deltaten_message
;
865 public function getBinets()
867 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
868 return XDB
::fetchColumn('SELECT binet_id
870 WHERE pid = {?}', $this->id());
875 public function getBinetsNames()
877 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
878 return XDB
::fetchColumn('SELECT text
879 FROM profile_binets AS pb
880 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
881 WHERE pb.pid = {?}', $this->id());
889 private $medals = null
;
890 public function setMedals(ProfileMedals
$medals)
892 $this->medals
= $medals;
895 public function getMedals()
897 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
898 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
900 if ($this->medals
== null
) {
903 return $this->medals
->medals
;
906 public function compareNames($firstname, $lastname)
908 $_lastname = mb_strtoupper($this->lastName());
909 $_firstname = mb_strtoupper($this->firstName());
910 $lastname = mb_strtoupper($lastname);
911 $firstname = mb_strtoupper($firstname);
913 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
914 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
917 foreach ($tokens as $str) {
918 $isOk &= (strpos($_lastname, $str) !== false
);
919 $maxlen = max($maxlen, strlen($str));
922 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
927 public function export()
930 'hrpid' => $this->hrid(),
931 'display_name' => $this->shortName(),
932 'full_name' => $this->fullName(),
933 'directory_name' => $this->directory_name
,
934 'promo' => $this->promo(),
935 'year_promo' => $this->yearpromo(),
936 'is_active' => $this->isActive(),
937 'first_name' => $this->firstName(),
938 'last_name' => $this->lastName(),
939 'is_female' => $this->isFemale(),
943 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
945 if (count($pids) == 0) {
949 if ($respect_order) {
950 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
955 $visibility = new ProfileVisibility($visibility);
957 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
958 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
959 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
960 p.last_change, p.nationality1, p.nationality2, p.nationality3,
961 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
962 pe.entry_year, pe.grad_year, pe.promo_year, pe.program, pe.fieldid,
963 IF ({?}, pse.text, NULL) AS section,
964 ppn.firstname_main AS firstname, ppn.lastname_main AS lastname, IF ({?}, pn.name, NULL) AS nickname,
965 IF (ppn.firstname_ordinary = \'\', ppn.firstname_main, ppn.firstname_ordinary) AS firstname_ordinary,
966 IF (ppn.lastname_ordinary = \'\', ppn.firstname_main, ppn.lastname_ordinary) AS lastname_ordinary,
967 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
968 pd.directory_name, pd.public_name, pd.private_name,
969 IF (pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
970 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
971 ph.x AS photo_width, ph.y AS photo_height,
972 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
973 pm.expertise AS mentor_expertise,
974 IF ({?}, pdt.message, NULL) AS deltaten_message,
977 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
978 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
979 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
980 INNER JOIN profile_public_names AS ppn ON (ppn.pid = p.pid)
981 LEFT JOIN profile_private_names AS pn ON (pn.pid = p.pid AND type = \'nickname\')
982 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
983 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
984 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
985 LEFT JOIN profile_deltaten AS pdt ON (pdt.pid = p.pid)
986 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
990 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // CV
991 $visibility->levels(), // freetext
992 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // section
993 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // nickname
994 $visibility->levels(), // mobile
995 $visibility->levels(), // photo
996 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // deltaten_message
999 return new ProfileIterator($it, $pids, $fields, $visibility);
1002 public static function getPID($login)
1004 if ($login instanceof PlUser
) {
1005 return XDB
::fetchOneCell('SELECT pid
1006 FROM account_profiles
1007 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
1009 } else if (ctype_digit($login)) {
1010 return XDB
::fetchOneCell('SELECT pid
1012 WHERE pid = {?}', $login);
1014 return XDB
::fetchOneCell('SELECT pid
1016 WHERE hrpid = {?}', $login);
1020 public static function getPIDsFromUIDs($uids, $respect_order = true
)
1022 if ($respect_order) {
1023 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
1027 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
1028 FROM account_profiles AS ap
1029 WHERE FIND_IN_SET(\'owner\', ap.perms)
1030 AND ap.uid IN ' . XDB
::formatArray($uids) .'
1034 /** Return the profile associated with the given login.
1036 public static function get($login, $fields = 0x0000, $visibility = null
)
1038 if (is_array($login)) {
1039 $pf = new Profile($login);
1040 $pf->setVisibilityLevel($visibility);
1043 $pid = self
::getPID($login);
1044 if (!is_null($pid)) {
1045 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
1048 /* Let say we can identify a profile using the identifiers of its owner.
1050 if (!($login instanceof PlUser
)) {
1051 $user = User
::getSilent($login);
1052 if ($user && $user->hasProfile()) {
1053 return $user->profile();
1060 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1062 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
1065 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1067 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
1070 /** Return profiles for the list of pids.
1072 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
1074 if (count($pids) == 0) {
1077 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
1078 $profiles = array();
1079 while ($p = $it->next()) {
1080 $profiles[$p->id()] = $p;
1085 /** Return profiles for uids.
1087 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
1089 if (count($uids) == 0) {
1092 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
1095 public static function isDisplayName($name)
1097 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
1098 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
1099 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1100 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1103 /** Returns the closest "accounts only" name type for $name
1105 public static function getAccountEquivalentName($name)
1109 case self
::DN_DIRECTORY
:
1111 return 'directory_name';
1113 case self
::DN_PUBLIC
:
1115 case self
::DN_PRIVATE
:
1116 case self
::DN_SHORT
:
1117 case self
::DN_YOURSELF
:
1119 return 'display_name';
1123 public static function rebuildSearchTokens($pids, $transaction = true
)
1125 require_once 'name.func.inc.php';
1126 if (!is_array($pids)) {
1127 $pids = array($pids);
1129 $keys = XDB
::iterator("(SELECT pid, name, type, IF(type = 'nickname', 2, 1) AS score, '' AS public
1130 FROM profile_private_names
1133 (SELECT pid, lastname_main, 'lastname' AS type, 10 AS score, 'public' AS public
1134 FROM profile_public_names
1135 WHERE lastname_main != '' AND pid IN {?})
1137 (SELECT pid, lastname_marital, 'lastname' AS type, 10 AS score, 'public' AS public
1138 FROM profile_public_names
1139 WHERE lastname_marital != '' AND pid IN {?})
1141 (SELECT pid, lastname_ordinary, 'lastname' AS type, 10 AS score, 'public' AS public
1142 FROM profile_public_names
1143 WHERE lastname_ordinary != '' AND pid IN {?})
1145 (SELECT pid, firstname_main, 'firstname' AS type, 10 AS score, 'public' AS public
1146 FROM profile_public_names
1147 WHERE firstname_main != '' AND pid IN {?})
1149 (SELECT pid, firstname_ordinary, 'firstname' AS type, 10 AS score, 'public' AS public
1150 FROM profile_public_names
1151 WHERE firstname_ordinary != '' AND pid IN {?})
1153 (SELECT pid, pseudonym, 'nickname' AS type, 10 AS score, 'public' AS public
1154 FROM profile_public_names
1155 WHERE pseudonym != '' AND pid IN {?})",
1156 $pids, $pids, $pids, $pids, $pids, $pids, $pids);
1158 while ($key = $keys->next()) {
1159 if ($key['name'] == '') {
1163 $toks = split_name_for_search($key['name']);
1164 $toks = array_reverse($toks);
1166 /* Split the score between the tokens to avoid the user to be over-rated.
1167 * Let says my user name is "Machin-Truc Bidule" and I also have a user named
1168 * 'Machin Truc'. Distributing the score force "Machin Truc" to be displayed
1169 * before "Machin-Truc" for both "Machin Truc" and "Machin" searches.
1171 $eltScore = ceil(((float)$key['score'])/((float)count($toks)));
1173 foreach ($toks as $tok) {
1174 $token = $tok . $token;
1175 $names["$pid-$token"] = XDB
::format('({?}, {?}, {?}, {?}, {?}, {?})',
1176 $token, $pid, soundex_fr($token),
1177 $eltScore, $key['public'], $key['type']);
1181 XDB
::startTransaction();
1183 XDB
::execute('DELETE FROM search_name
1186 if (count($names) > 0) {
1187 XDB
::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags, general_type)
1188 VALUES ' . implode(', ', $names));
1195 /** The school identifier consists of 6 digits. The first 3 represent the
1196 * promotion entry year. The last 3 indicate the student's rank.
1198 * Our identifier consists of 8 digits and both half have the same role.
1199 * This enables us to deal with bigger promotions and with a wider range
1202 * getSchoolId returns a school identifier given one of ours.
1203 * getXorgId returns a X.org identifier given a school identifier.
1205 public static function getSchoolId($xorgId)
1207 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1211 $year = intval(substr($xorgId, 0, 4));
1212 $rank = intval(substr($xorgId, 5, 3));
1215 } elseif ($year < 2000) {
1216 $year = intval(substr(1900 - $year, 1, 3));
1217 return sprintf('%02u0%03u', $year, $rank);
1219 $year = intval(substr(1900 - $year, 1, 3));
1220 return sprintf('%03u%03u', $year, $rank);
1224 public static function getXorgId($schoolId)
1226 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1230 $year = intval(substr($schoolId, 0, 3));
1231 $rank = intval(substr($schoolId, 3, 3));
1239 return sprintf('%04u%04u', 1900 +
$year, $rank);
1245 /** Iterator over a set of Profiles
1247 class ProfileIterator
implements PlIterator
1249 private $iterator = null
;
1251 private $visibility;
1253 const FETCH_ALL
= 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
1255 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
1257 require_once 'profilefields.inc.php';
1259 if ($visibility == null
) {
1260 $visibility = new ProfileVisibility();
1263 $this->fields
= $fields;
1264 $this->visibility
= $visibility;
1267 $callbacks = array();
1270 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1271 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1273 $fields = $fields & self
::FETCH_ALL
;
1274 for ($field = 1; $field < $fields; $field *= 2) {
1275 if (($fields & $field) ) {
1276 $callbacks[$field] = $cb;
1277 $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility);
1281 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1284 private function hasData($field, $vals)
1286 return ($this->fields
& $field) && ($vals[$field] != null
);
1289 private function fillProfile(array $vals)
1291 $pf = Profile
::get($vals[0]);
1292 $pf->setVisibilityLevel($this->visibility
->level());
1293 $pf->setFetchedFields($this->fields
);
1295 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1296 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1298 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1299 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1301 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1302 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1304 if ($this->hasData(Profile
::FETCH_JOB_TERMS
, $vals)) {
1305 $pf->setJobTerms($vals[Profile
::FETCH_JOB_TERMS
]);
1308 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1309 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1311 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1312 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1314 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1315 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1317 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1318 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1324 public function next()
1326 $vals = $this->iterator
->next();
1327 if ($vals == null
) {
1330 return $this->fillProfile($vals);
1333 public function first()
1335 return $this->iterator
->first();
1338 public function last()
1340 return $this->iterator
->last();
1343 public function total()
1345 return $this->iterator
->total();
1349 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: