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, Visibility
$visibility)
166 $this->pid
= $this->data
['pid'];
167 $this->hrpid
= $this->data
['hrpid'];
168 $this->visibility
= $visibility;
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 // Returns the profile's color.
303 public function promoColor()
305 switch ($this->mainEducation()) {
307 if (($this->yearpromo() %
2) === 0) {
321 /** Print a name with the given formatting:
329 public function name($format)
331 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
332 array($this->isFemale() ?
'•' : '',
333 $this->firstName(), $this->lastName(),
334 $this->fullName(), $this->shortName(),
335 $this->promo()), $format);
338 public function fullName($with_promo = false
)
341 return $this->full_name
. ' (' . $this->promo
. ')';
343 return $this->full_name
;
346 public function shortName($with_promo = false
)
349 return $this->short_name
. ' (' . $this->promo
. ')';
351 return $this->short_name
;
354 public function firstName()
356 return $this->firstname
;
359 public function firstNames()
361 return $this->nameVariants(self
::FIRSTNAME
);
364 public function lastName()
366 return $this->lastname
;
369 public function lastNames()
371 return $this->nameVariants(self
::LASTNAME
);
374 public function isFemale()
376 return $this->sex
== PlUser
::GENDER_FEMALE
;
379 public function isDead()
381 return ($this->deathdate
!= null
);
384 public function displayEmail()
387 if ($o != null
&& $this->isVisible(Visibility
::EXPORT_PRIVATE
)) {
388 return $o->bestEmail();
390 return $this->email_directory
;
394 public function data()
400 private function nameVariants($type)
402 $vals = array($this->$type);
403 foreach (self
::$name_variants[$type] as $var) {
404 $vartype = $type . '_' . $var;
405 $varname = $this->$vartype;
406 if ($varname != null
&& $varname != "") {
410 return array_unique($vals);
413 public function nationalities()
416 $nationalities = DirEnum
::getOptions(DirEnum
::NATIONALITIES
);
417 if ($this->nationality1
) {
418 $nats[$this->nationality1
] = $nationalities[$this->nationality1
];
420 if ($this->nationality2
) {
421 $nats[$this->nationality2
] = $nationalities[$this->nationality2
];
423 if ($this->nationality3
) {
424 $nats[$this->nationality3
] = $nationalities[$this->nationality3
];
429 public function __get($name)
431 if (property_exists($this, $name)) {
435 if (isset($this->data
[$name])) {
436 return $this->data
[$name];
442 public function __isset($name)
444 return property_exists($this, $name) ||
isset($this->data
[$name]);
447 public function __unset($name)
449 if (property_exists($this, $name)) {
452 unset($this->data
[$name]);
459 * *always deletes in: profile_addresses, profile_binets, profile_deltaten,
460 * profile_job, profile_langskills, profile_mentor, profile_networking,
461 * profile_phones, profile_skills, watch_profile
462 * *always keeps in: profile_corps, profile_display, profile_education,
463 * profile_medals, profile_*_names, profile_photos, search_name
464 * *modifies: profiles
466 public function clear()
469 'profile_job', 'profile_langskills', 'profile_mentor',
470 'profile_networking', 'profile_skills', 'watch_profile',
471 'profile_phones', 'profile_addresses', 'profile_binets',
474 foreach ($tables as $t) {
475 XDB
::execute('DELETE FROM ' . $t . '
480 XDB
::execute("UPDATE profiles
481 SET cv = NULL, freetext = NULL, freetext_pub = 'private',
482 medals_pub = 'private', alias_pub = 'private',
483 email_directory = NULL
488 /** Determine whether an item with visibility $visibility can be displayed
489 * with the current level of visibility of the profile
490 * @param $visibility The level of visibility to be checked
492 public function isVisible($visibility)
494 return $this->visibility
->isVisible($visibility);
497 /** Stores the list of fields which have already been fetched for this Profile
499 public function setFetchedFields($fields)
501 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
502 Platal
::page()->kill("Invalid fetched fields: $fields");
505 $this->fetched_fields
= $fields;
508 /** Have we already fetched this field ?
510 private function fetched($field)
512 if (!array_key_exists($field, ProfileField
::$fields)) {
513 Platal
::page()->kill("Invalid field: $field");
516 return ($this->fetched_fields
& $field);
519 /** If not already done, fetches data for the given field
520 * @param $field One of the Profile::FETCH_*
521 * @return A ProfileField, or null
523 private function getProfileField($field)
525 if (!array_key_exists($field, ProfileField
::$fields)) {
526 Platal
::page()->kill("Invalid field: $field");
528 if ($this->fetched($field)) {
531 $this->fetched_fields
= $this->fetched_fields |
$field;
534 $cls = ProfileField
::$fields[$field];
536 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
539 /** Consolidates internal data (addresses, phones, jobs)
541 private function consolidateFields()
543 // Link phones to addresses
544 if ($this->phones
!= null
) {
545 if ($this->addresses
!= null
) {
546 $this->addresses
->addPhones($this->phones
);
549 if ($this->jobs
!= null
) {
550 $this->jobs
->addPhones($this->phones
);
554 // Link addresses to jobs
555 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
556 $this->jobs
->addAddresses($this->addresses
);
559 // Link jobterms to jobs
560 if ($this->jobs
!= null
&& $this->jobterms
!= null
) {
561 $this->jobs
->addJobTerms($this->jobterms
);
567 private $photo = null
;
568 public function getPhoto($fallback = true
, $data = false
)
570 if ($this->has_photo
) {
571 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
572 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y, last_update
574 WHERE pid = {?}', $this->pid
);
575 $this->photo
= PlImage
::fromData($res['attach'], 'image/' .$res['attachmime'], $res['x'], $res['y'], $res['last_update']);
576 } else if ($this->photo
== null
) {
577 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
580 } else if ($fallback) {
581 if ($this->mainEducation() == 'X') {
582 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_x.png', 'image/png');
584 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_md.png', 'image/png');
591 private $addresses = null
;
592 public function setAddresses(ProfileAddresses
$addr)
594 $this->addresses
= $addr;
595 $this->consolidateFields();
598 private function fetchAddresses()
600 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
601 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
603 $this->setAddresses($addr);
604 $this->fetchPhones();
609 public function getAddresses($flags, $limit = null
)
611 $this->fetchAddresses();
613 if ($this->addresses
== null
) {
616 return $this->addresses
->get($flags, $limit);
619 public function iterAddresses($flags, $limit = null
)
621 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
624 public function getMainAddress()
626 $main = $this->getAddresses(self
::ADDRESS_MAIN
);
627 $perso = $this->getAddresses(self
::ADDRESS_PERSO
);
630 return array_pop($main);
631 } else if (count($perso)) {
632 return array_pop($perso);
640 private $phones = null
;
641 public function setPhones(ProfilePhones
$phones)
643 $this->phones
= $phones;
644 $this->consolidateFields();
647 private function fetchPhones()
649 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
650 $phones = $this->getProfileField(self
::FETCH_PHONES
);
651 if (isset($phones)) {
652 $this->setPhones($phones);
657 public function getPhones($flags, $limit = null
)
659 $this->fetchPhones();
660 if ($this->phones
== null
) {
663 return $this->phones
->get($flags, $limit);
668 private $educations = null
;
669 public function setEducations(ProfileEducation
$edu)
671 $this->educations
= $edu;
674 public function getEducations($flags, $limit = null
)
676 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
677 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
680 if ($this->educations
== null
) {
683 return $this->educations
->get($flags, $limit);
686 public function getExtraEducations($limit = null
)
688 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
693 private $corps = null
;
694 public function setCorps(ProfileCorps
$corps)
696 $this->corps
= $corps;
699 public function getCorps()
701 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
702 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
709 private $networks = null
;
710 public function setNetworking(ProfileNetworking
$nw)
712 $this->networks
= $nw;
715 public function getNetworking($flags, $limit = null
)
717 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
718 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
720 $this->setNetworking($nw);
723 if ($this->networks
== null
) {
726 return $this->networks
->get($flags, $limit);
729 public function getWebSite()
731 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
732 if (count($site) != 1) {
735 $site = array_pop($site);
742 private $jobs = null
;
743 public function setJobs(ProfileJobs
$jobs)
746 $this->consolidateFields();
749 private function fetchJobs()
751 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
752 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
754 $this->setJobs($jobs);
755 $this->fetchAddresses();
760 public function getJobs($flags, $limit = null
)
764 if ($this->jobs
== null
) {
767 return $this->jobs
->get($flags, $limit);
770 public function getMainJob()
772 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
773 if (count($job) != 1) {
776 return array_pop($job);
781 private $jobterms = null
;
782 public function setJobTerms(ProfileJobTerms
$jobterms)
784 $this->jobterms
= $jobterms;
785 $this->consolidateFields();
788 private $mentor_countries = null
;
789 public function setMentoringCountries(ProfileMentoringCountries
$countries)
791 $this->mentor_countries
= $countries;
794 public function getMentoringCountries()
796 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
797 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
800 if ($this->mentor_countries
== null
) {
803 return $this->mentor_countries
->countries
;
807 /** List of job terms to specify mentoring */
808 private $mentor_terms = null
;
810 * set job terms to specify mentoring
811 * @param $terms a ProfileMentoringTerms object listing terms only for this profile
813 public function setMentoringTerms(ProfileMentoringTerms
$terms)
815 $this->mentor_terms
= $terms;
818 * get all job terms that specify mentoring
819 * @return an array of JobTerms objects
821 public function getMentoringTerms()
823 if ($this->mentor_terms
== null
&& !$this->fetched(self
::FETCH_MENTOR_TERMS
)) {
824 $this->setMentoringTerms($this->getProfileField(self
::FETCH_MENTOR_TERMS
));
827 if ($this->mentor_terms
== null
) {
830 return $this->mentor_terms
->get();
837 /** Find out whether this profile may take part to the "DeltaTen" operation.
838 * @param $role Which role to select ('young' or 'old')
839 * @return Boolean: whether it is enabled.
841 const DELTATEN_YOUNG
= 'young';
842 const DELTATEN_OLD
= 'old';
843 public function isDeltaTenEnabled($role)
847 case self
::DELTATEN_YOUNG
:
848 return ($this->mainGrade() == UserFilter
::GRADE_ING
&& $this->yearpromo() >= $globals->deltaten
->first_promo_young
);
849 case self
::DELTATEN_OLD
:
850 // Roughly compute the current promo in second year on the campus:
851 // Promo 2010 is in second year between 09/2011 and 08/2012 => use 2012.
852 // DeltaTen program begins around January of the second year.
853 $promo_on_platal = ((int) date('Y')) - 2;
854 return ($this->mainGrade() == UserFilter
::GRADE_ING
&& $this->yearpromo() >= $globals->deltaten
->first_promo_young
- 10 && $this->yearpromo() <= $promo_on_platal - 10);
856 Platal
::assert(false
, "Invalid DeltaTen role $role");
860 /** Retrieve the "Deltaten" message of the user.
861 * Returns "null" if the message is empty or the user is not taking part to the
862 * DeltaTen operation.
864 public function getDeltatenMessage()
866 if ($this->isDeltaTenEnabled(self
::DELTATEN_OLD
)) {
867 return $this->deltaten_message
;
875 public function getBinets()
877 if ($this->visibility
->isVisible(Visibility
::EXPORT_PRIVATE
)) {
878 return XDB
::fetchColumn('SELECT binet_id
880 WHERE pid = {?}', $this->id());
885 public function getBinetsNames()
887 if ($this->visibility
->isVisible(Visibility
::EXPORT_PRIVATE
)) {
888 return XDB
::fetchColumn('SELECT text
889 FROM profile_binets AS pb
890 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
891 WHERE pb.pid = {?}', $this->id());
899 private $medals = null
;
900 public function setMedals(ProfileMedals
$medals)
902 $this->medals
= $medals;
905 public function getMedals()
907 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
908 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
910 if ($this->medals
== null
) {
913 return $this->medals
->medals
;
916 public function compareNames($firstname, $lastname)
918 $_lastname = mb_strtoupper($this->lastName());
919 $_firstname = mb_strtoupper($this->firstName());
920 $lastname = mb_strtoupper($lastname);
921 $firstname = mb_strtoupper($firstname);
923 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
924 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
927 foreach ($tokens as $str) {
928 $isOk &= (strpos($_lastname, $str) !== false
);
929 $maxlen = max($maxlen, strlen($str));
932 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
937 public function export()
940 'hrpid' => $this->hrid(),
941 'display_name' => $this->shortName(),
942 'full_name' => $this->fullName(),
943 'directory_name' => $this->directory_name
,
944 'promo' => $this->promo(),
945 'year_promo' => $this->yearpromo(),
946 'is_active' => $this->isActive(),
947 'first_name' => $this->firstName(),
948 'last_name' => $this->lastName(),
949 'is_female' => $this->isFemale(),
953 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
955 if (count($pids) == 0) {
959 if ($respect_order) {
960 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
965 if ($visibility === null
) {
966 $visibility = Visibility
::defaultForRead();
969 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
970 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
971 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
972 p.last_change, p.nationality1, p.nationality2, p.nationality3,
973 IF (p.freetext_pub >= {?}, p.freetext, NULL) AS freetext,
974 pe.entry_year, pe.grad_year, pe.promo_year, pe.program, pe.fieldid,
975 IF ({?}, pse.text, NULL) AS section,
976 ppn.firstname_main AS firstname, ppn.lastname_main AS lastname, IF ({?}, pn.name, NULL) AS nickname,
977 IF (ppn.firstname_ordinary = \'\', ppn.firstname_main, ppn.firstname_ordinary) AS firstname_ordinary,
978 IF (ppn.lastname_ordinary = \'\', ppn.firstname_main, ppn.lastname_ordinary) AS lastname_ordinary,
979 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
980 pd.directory_name, pd.public_name, pd.private_name,
981 IF (pp.pub >= {?}, pp.display_tel, NULL) AS mobile,
982 (ph.pub >= {?} AND ph.attach IS NOT NULL) AS has_photo,
983 ph.x AS photo_width, ph.y AS photo_height,
984 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
985 pm.expertise AS mentor_expertise,
986 IF ({?}, pdt.message, NULL) AS deltaten_message,
989 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
990 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
991 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
992 INNER JOIN profile_public_names AS ppn ON (ppn.pid = p.pid)
993 LEFT JOIN profile_private_names AS pn ON (pn.pid = p.pid AND type = \'nickname\')
994 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
995 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
996 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
997 LEFT JOIN profile_deltaten AS pdt ON (pdt.pid = p.pid)
998 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
1002 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // CV
1003 $visibility->level(), // freetext
1004 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // section
1005 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // nickname
1006 $visibility->level(), // mobile
1007 $visibility->level(), // photo
1008 $visibility->isVisible(Visibility
::EXPORT_PRIVATE
), // deltaten_message
1011 return new ProfileIterator($it, $pids, $fields, $visibility);
1014 public static function getPID($login)
1016 if ($login instanceof PlUser
) {
1017 return XDB
::fetchOneCell('SELECT pid
1018 FROM account_profiles
1019 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
1021 } else if (ctype_digit($login)) {
1022 return XDB
::fetchOneCell('SELECT pid
1024 WHERE pid = {?}', $login);
1026 return XDB
::fetchOneCell('SELECT pid
1028 WHERE hrpid = {?}', $login);
1032 public static function getPIDsFromUIDs($uids, $respect_order = true
)
1034 if ($respect_order) {
1035 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
1039 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
1040 FROM account_profiles AS ap
1041 WHERE FIND_IN_SET(\'owner\', ap.perms)
1042 AND ap.uid IN ' . XDB
::formatArray($uids) .'
1046 /** Return the profile associated with the given login.
1048 public static function get($login, $fields = 0x0000, $visibility = null
)
1050 if ($visibility === null
) {
1051 $visibility = Visibility
::defaultForRead();
1054 if (is_array($login)) {
1055 $pf = new Profile($login, $visibility);
1058 $pid = self
::getPID($login);
1059 if (!is_null($pid)) {
1060 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
1063 /* Let say we can identify a profile using the identifiers of its owner.
1065 if (!($login instanceof PlUser
)) {
1066 $user = User
::getSilent($login);
1067 if ($user && $user->hasProfile()) {
1068 return $user->profile(false
, $fields, $visibility);
1075 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1077 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
1080 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1082 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
1085 /** Return profiles for the list of pids.
1087 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
1089 if (count($pids) == 0) {
1092 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
1093 $profiles = array();
1094 while ($p = $it->next()) {
1095 $profiles[$p->id()] = $p;
1100 /** Return profiles for uids.
1102 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
1104 if (count($uids) == 0) {
1107 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
1110 public static function isDisplayName($name)
1112 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
1113 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
1114 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1115 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1118 /** Returns the closest "accounts only" name type for $name
1120 public static function getAccountEquivalentName($name)
1124 case self
::DN_DIRECTORY
:
1126 return 'directory_name';
1128 case self
::DN_PUBLIC
:
1130 case self
::DN_PRIVATE
:
1131 case self
::DN_SHORT
:
1132 case self
::DN_YOURSELF
:
1134 return 'display_name';
1138 public static function rebuildSearchTokens($pids, $transaction = true
)
1140 require_once 'name.func.inc.php';
1141 if (!is_array($pids)) {
1142 $pids = array($pids);
1144 $keys = XDB
::iterator("(SELECT pid, name, type, IF(type = 'nickname', 2, 1) AS score, '' AS public
1145 FROM profile_private_names
1148 (SELECT pid, lastname_main, 'lastname' AS type, 10 AS score, 'public' AS public
1149 FROM profile_public_names
1150 WHERE lastname_main != '' AND pid IN {?})
1152 (SELECT pid, lastname_marital, 'lastname' AS type, 10 AS score, 'public' AS public
1153 FROM profile_public_names
1154 WHERE lastname_marital != '' AND pid IN {?})
1156 (SELECT pid, lastname_ordinary, 'lastname' AS type, 10 AS score, 'public' AS public
1157 FROM profile_public_names
1158 WHERE lastname_ordinary != '' AND pid IN {?})
1160 (SELECT pid, firstname_main, 'firstname' AS type, 10 AS score, 'public' AS public
1161 FROM profile_public_names
1162 WHERE firstname_main != '' AND pid IN {?})
1164 (SELECT pid, firstname_ordinary, 'firstname' AS type, 10 AS score, 'public' AS public
1165 FROM profile_public_names
1166 WHERE firstname_ordinary != '' AND pid IN {?})
1168 (SELECT pid, pseudonym, 'nickname' AS type, 10 AS score, 'public' AS public
1169 FROM profile_public_names
1170 WHERE pseudonym != '' AND pid IN {?})",
1171 $pids, $pids, $pids, $pids, $pids, $pids, $pids);
1173 while ($key = $keys->next()) {
1174 if ($key['name'] == '') {
1178 $toks = split_name_for_search($key['name']);
1179 $toks = array_reverse($toks);
1181 /* Split the score between the tokens to avoid the user to be over-rated.
1182 * Let says my user name is "Machin-Truc Bidule" and I also have a user named
1183 * 'Machin Truc'. Distributing the score force "Machin Truc" to be displayed
1184 * before "Machin-Truc" for both "Machin Truc" and "Machin" searches.
1186 $eltScore = ceil(((float)$key['score'])/((float)count($toks)));
1188 foreach ($toks as $tok) {
1189 $token = $tok . $token;
1190 $names["$pid-$token"] = XDB
::format('({?}, {?}, {?}, {?}, {?}, {?})',
1191 $token, $pid, soundex_fr($token),
1192 $eltScore, $key['public'], $key['type']);
1196 XDB
::startTransaction();
1198 XDB
::execute('DELETE FROM search_name
1201 if (count($names) > 0) {
1202 XDB
::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags, general_type)
1203 VALUES ' . implode(', ', $names));
1210 /** The school identifier consists of 6 digits. The first 3 represent the
1211 * promotion entry year. The last 3 indicate the student's rank.
1213 * Our identifier consists of 8 digits and both half have the same role.
1214 * This enables us to deal with bigger promotions and with a wider range
1217 * getSchoolId returns a school identifier given one of ours.
1218 * getXorgId returns a X.org identifier given a school identifier.
1220 public static function getSchoolId($xorgId)
1222 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1226 $year = intval(substr($xorgId, 0, 4));
1227 $rank = intval(substr($xorgId, 5, 3));
1230 } elseif ($year < 2000) {
1231 $year = intval(substr(1900 - $year, 1, 3));
1232 return sprintf('%02u0%03u', $year, $rank);
1234 $year = intval(substr(1900 - $year, 1, 3));
1235 return sprintf('%03u%03u', $year, $rank);
1239 public static function getXorgId($schoolId)
1241 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1245 $year = intval(substr($schoolId, 0, 3));
1246 $rank = intval(substr($schoolId, 3, 3));
1254 return sprintf('%04u%04u', 1900 +
$year, $rank);
1260 /** Iterator over a set of Profiles
1262 class ProfileIterator
implements PlIterator
1264 private $iterator = null
;
1266 private $visibility;
1268 const FETCH_ALL
= 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
1270 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, $visibility = null
)
1272 require_once 'profilefields.inc.php';
1274 if ($visibility === null
) {
1275 $visibility = Visibility
::defaultForRead();
1278 $this->fields
= $fields;
1279 $this->visibility
= $visibility;
1282 $callbacks = array();
1285 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1286 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1288 $fields = $fields & self
::FETCH_ALL
;
1289 for ($field = 1; $field < $fields; $field *= 2) {
1290 if (($fields & $field) ) {
1291 $callbacks[$field] = $cb;
1292 $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility);
1296 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1299 private function hasData($field, $vals)
1301 return ($this->fields
& $field) && ($vals[$field] != null
);
1304 private function fillProfile(array $vals)
1306 $pf = Profile
::get($vals[0], 0x0, $this->visibility
);
1307 $pf->setFetchedFields($this->fields
);
1309 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1310 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1312 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1313 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1315 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1316 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1318 if ($this->hasData(Profile
::FETCH_JOB_TERMS
, $vals)) {
1319 $pf->setJobTerms($vals[Profile
::FETCH_JOB_TERMS
]);
1322 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1323 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1325 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1326 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1328 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1329 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1331 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1332 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1338 public function next()
1340 $vals = $this->iterator
->next();
1341 if ($vals == null
) {
1344 return $this->fillProfile($vals);
1347 public function first()
1349 return $this->iterator
->first();
1352 public function last()
1354 return $this->iterator
->last();
1357 public function total()
1359 return $this->iterator
->total();
1363 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: