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 $name_variants = array(
52 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
53 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
56 const ADDRESS_MAIN
= 0x00000001;
57 const ADDRESS_PERSO
= 0x00000002;
58 const ADDRESS_PRO
= 0x00000004;
59 const ADDRESS_ALL
= 0x00000006;
60 const ADDRESS_POSTAL
= 0x00000008;
62 const EDUCATION_MAIN
= 0x00000010;
63 const EDUCATION_EXTRA
= 0x00000020;
64 const EDUCATION_ALL
= 0x00000040;
65 const EDUCATION_FINISHED
= 0x00000080;
66 const EDUCATION_CURRENT
= 0x00000100;
68 const JOBS_MAIN
= 0x00001000;
69 const JOBS_ALL
= 0x00002000;
70 const JOBS_FINISHED
= 0x00004000;
71 const JOBS_CURRENT
= 0x00008000;
73 const NETWORKING_ALL
= 0x00070000;
74 const NETWORKING_WEB
= 0x00010000;
75 const NETWORKING_IM
= 0x00020000;
76 const NETWORKING_SOCIAL
= 0x00040000;
78 const PHONE_LINK_JOB
= 0x00100000;
79 const PHONE_LINK_ADDRESS
= 0x00200000;
80 const PHONE_LINK_PROFILE
= 0x00400000;
81 const PHONE_LINK_COMPANY
= 0x00800000;
82 const PHONE_LINK_ANY
= 0x00F00000;
84 const PHONE_TYPE_FAX
= 0x01000000;
85 const PHONE_TYPE_FIXED
= 0x02000000;
86 const PHONE_TYPE_MOBILE
= 0x04000000;
87 const PHONE_TYPE_ANY
= 0x07000000;
89 const PHONE_ANY
= 0x07F00000;
91 const FETCH_ADDRESSES
= 0x000001;
92 const FETCH_CORPS
= 0x000002;
93 const FETCH_EDU
= 0x000004;
94 const FETCH_JOBS
= 0x000008;
95 const FETCH_MEDALS
= 0x000010;
96 const FETCH_NETWORKING
= 0x000020;
97 const FETCH_MENTOR_COUNTRY
= 0x000080;
98 const FETCH_PHONES
= 0x000100;
99 const FETCH_JOB_TERMS
= 0x000200;
100 const FETCH_MENTOR_TERMS
= 0x000400;
102 const FETCH_MINIFICHES
= 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
104 const FETCH_ALL
= 0x0007FF; // OR of FETCH_*
106 static public $descriptions = array(
107 'search_names' => 'Noms',
108 'nationality1' => 'Nationalité',
109 'nationality2' => '2e nationalité',
110 'nationality3' => '3e nationalité',
111 'promo_display' => 'Promotion affichée',
112 'email_directory' => 'Email annuaire papier',
113 'networking' => 'Messageries…',
114 'tels' => 'Téléphones',
115 'edus' => 'Formations',
116 'main_edus' => 'Formations à l\'X',
117 'promo' => 'Promotion de sortie',
118 'birthdate' => 'Date de naissance',
119 'yourself' => 'Nom affiché',
120 'freetext' => 'Commentaire',
121 'freetext_pub' => 'Affichage du commentaire',
122 'photo' => 'Photographie',
123 'photo_pub' => 'Affichage de la photographie',
124 'addresses' => 'Adresses',
128 'section' => 'Section',
129 'binets' => 'Binets',
130 'medals' => 'Décorations',
131 'medals_pub' => 'Affichage des décorations',
132 'competences' => 'Compétences',
133 'langues' => 'Langues',
134 'expertise' => 'Expertises (mentoring)',
135 'terms' => 'Compétences (mentoring)',
136 'countries' => 'Pays (mentoring)'
139 private $fetched_fields = 0x000000;
144 private $owner_fetched = false
;
145 private $data = array();
147 private $visibility = null
;
150 private function __construct(array $data)
153 $this->pid
= $this->data
['pid'];
154 $this->hrpid
= $this->data
['hrpid'];
155 $this->visibility
= new ProfileVisibility();
163 public function hrid()
168 public function owner()
170 if ($this->owner
== null
&& !$this->owner_fetched
) {
171 $this->owner_fetched
= true
;
172 $this->owner
= User
::getSilent($this);
177 public function isActive()
179 if ($this->owner()) {
180 return $this->owner
->isActive();
185 public function promo($details = false
)
187 if ($details && ($this->program ||
$this->fieldid
)) {
189 if ($this->program
) {
190 $text[] = $this->program
;
192 if ($this->fieldid
) {
193 $fieldsList = DirEnum
::getOptions(DirEnum
::EDUFIELDS
);
194 $text[] = $fieldsList[$this->fieldid
];
196 return $this->promo
. ' (' . implode(', ', $text) . ')';
202 public function yearpromo()
204 return $this->promo_year
;
207 /** Check if user is an orange (associated with several promos)
209 public function isMultiPromo()
211 return $this->grad_year
!= $this->entry_year +
$this->mainEducationDuration();
214 /** Returns an array with all associated promo years.
216 public function yearspromo()
219 $d = -$this->deltaPromoToGradYear();
220 for ($g = $this->entry_year +
$this->mainEducationDuration(); $g <= $this->grad_year
; ++
$g) {
226 public function mainEducation()
228 if (empty($this->promo
)) {
231 return $this->promo
{0};
235 public function mainGrade()
237 switch ($this->mainEducation()) {
239 return UserFilter
::GRADE_ING
;
241 return UserFilter
::GRADE_MST
;
243 return UserFilter
::GRADE_PHD
;
249 public function mainEducationDuration()
251 switch ($this->mainEducation()) {
263 /** Number of years between the promotion year until the
264 * graduation year. In standard schools it's 0, but for
265 * Polytechnique the promo year is the entry year.
267 public function deltaPromoToGradYear()
269 if ($this->mainEducation() == 'X') {
270 return $this->mainEducationDuration();
275 /** Print a name with the given formatting:
283 public function name($format)
285 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
286 array($this->isFemale() ?
'•' : '',
287 $this->firstName(), $this->lastName(),
288 $this->fullName(), $this->shortName(),
289 $this->promo()), $format);
292 public function fullName($with_promo = false
)
295 return $this->full_name
. ' (' . $this->promo
. ')';
297 return $this->full_name
;
300 public function shortName($with_promo = false
)
303 return $this->short_name
. ' (' . $this->promo
. ')';
305 return $this->short_name
;
308 public function firstName()
310 return $this->firstname
;
313 public function firstNames()
315 return $this->nameVariants(self
::FIRSTNAME
);
318 public function lastName()
320 return $this->lastname
;
323 public function lastNames()
325 return $this->nameVariants(self
::LASTNAME
);
328 public function isFemale()
330 return $this->sex
== PlUser
::GENDER_FEMALE
;
333 public function isDead()
335 return ($this->deathdate
!= null
);
338 public function displayEmail()
342 return $o->bestEmail();
344 return $this->email_directory
;
348 public function data()
354 private function nameVariants($type)
356 $vals = array($this->$type);
357 foreach (self
::$name_variants[$type] as $var) {
358 $vartype = $type . '_' . $var;
359 $varname = $this->$vartype;
360 if ($varname != null
&& $varname != "") {
364 return array_unique($vals);
367 public function nationalities()
370 $countries = DirEnum
::getOptions(DirEnum
::COUNTRIES
);
371 if ($this->nationality1
) {
372 $nats[$this->nationality1
] = $countries[$this->nationality1
];
374 if ($this->nationality2
) {
375 $nats[$this->nationality2
] = $countries[$this->nationality2
];
377 if ($this->nationality3
) {
378 $nats[$this->nationality3
] = $countries[$this->nationality3
];
383 public function __get($name)
385 if (property_exists($this, $name)) {
389 if (isset($this->data
[$name])) {
390 return $this->data
[$name];
396 public function __isset($name)
398 return property_exists($this, $name) ||
isset($this->data
[$name]);
401 public function __unset($name)
403 if (property_exists($this, $name)) {
406 unset($this->data
[$name]);
413 * *always deletes in: profile_addresses, profile_binets, profile_job,
414 * profile_langskills, profile_mentor, profile_networking,
415 * profile_phones, profile_skills, watch_profile
416 * *always keeps in: profile_corps, profile_display, profile_education,
417 * profile_medals, profile_name, profile_photos, search_name
418 * *modifies: profiles
420 public function clear()
423 'profile_job', 'profile_langskills', 'profile_mentor',
424 'profile_networking', 'profile_skills', 'watch_profile',
425 'profile_phones', 'profile_addresses', 'profile_binets');
427 foreach ($tables as $t) {
428 XDB
::execute('DELETE FROM ' . $t . '
433 XDB
::execute("UPDATE profiles
434 SET cv = NULL, freetext = NULL, freetext_pub = 'private',
435 medals_pub = 'private', alias_pub = 'private',
436 email_directory = NULL
441 /** Sets the level of visibility of the profile
442 * Sets $this->visibility to a list of valid visibilities.
443 * @param one of the self::VIS_* values
445 public function setVisibilityLevel($visibility)
447 $this->visibility
->setLevel($visibility);
450 /** Determine whether an item with visibility $visibility can be displayed
451 * with the current level of visibility of the profile
452 * @param $visibility The level of visibility to be checked
454 public function isVisible($visibility)
456 return $this->visibility
->isVisible($visibility);
459 /** Stores the list of fields which have already been fetched for this Profile
461 public function setFetchedFields($fields)
463 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
464 Platal
::page()->kill("Invalid fetched fields: $fields");
467 $this->fetched_fields
= $fields;
470 /** Have we already fetched this field ?
472 private function fetched($field)
474 if (!array_key_exists($field, ProfileField
::$fields)) {
475 Platal
::page()->kill("Invalid field: $field");
478 return ($this->fetched_fields
& $field);
481 /** If not already done, fetches data for the given field
482 * @param $field One of the Profile::FETCH_*
483 * @return A ProfileField, or null
485 private function getProfileField($field)
487 if (!array_key_exists($field, ProfileField
::$fields)) {
488 Platal
::page()->kill("Invalid field: $field");
490 if ($this->fetched($field)) {
493 $this->fetched_fields
= $this->fetched_fields |
$field;
496 $cls = ProfileField
::$fields[$field];
498 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
501 /** Consolidates internal data (addresses, phones, jobs)
503 private function consolidateFields()
505 // Link phones to addresses
506 if ($this->phones
!= null
) {
507 if ($this->addresses
!= null
) {
508 $this->addresses
->addPhones($this->phones
);
511 if ($this->jobs
!= null
) {
512 $this->jobs
->addPhones($this->phones
);
516 // Link addresses to jobs
517 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
518 $this->jobs
->addAddresses($this->addresses
);
521 // Link jobterms to jobs
522 if ($this->jobs
!= null
&& $this->jobterms
!= null
) {
523 $this->jobs
->addJobTerms($this->jobterms
);
529 private $photo = null
;
530 public function getPhoto($fallback = true
, $data = false
)
532 if ($this->has_photo
) {
533 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
534 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y, last_update
536 WHERE pid = {?}', $this->pid
);
537 $this->photo
= PlImage
::fromData($res['attach'], 'image/' .$res['attachmime'], $res['x'], $res['y'], $res['last_update']);
538 } else if ($this->photo
== null
) {
539 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
542 } else if ($fallback) {
543 if ($this->mainEducation() == 'X') {
544 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_x.png', 'image/png');
546 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_md.png', 'image/png');
553 private $addresses = null
;
554 public function setAddresses(ProfileAddresses
$addr)
556 $this->addresses
= $addr;
557 $this->consolidateFields();
560 private function fetchAddresses()
562 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
563 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
565 $this->setAddresses($addr);
566 $this->fetchPhones();
571 public function getAddresses($flags, $limit = null
)
573 $this->fetchAddresses();
575 if ($this->addresses
== null
) {
578 return $this->addresses
->get($flags, $limit);
581 public function iterAddresses($flags, $limit = null
)
583 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
586 public function getMainAddress()
588 $main = $this->getAddresses(self
::ADDRESS_MAIN
);
589 $perso = $this->getAddresses(self
::ADDRESS_PERSO
);
592 return array_pop($main);
593 } else if (count($perso)) {
594 return array_pop($perso);
602 private $phones = null
;
603 public function setPhones(ProfilePhones
$phones)
605 $this->phones
= $phones;
606 $this->consolidateFields();
609 private function fetchPhones()
611 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
612 $phones = $this->getProfileField(self
::FETCH_PHONES
);
613 if (isset($phones)) {
614 $this->setPhones($phones);
619 public function getPhones($flags, $limit = null
)
621 $this->fetchPhones();
622 if ($this->phones
== null
) {
625 return $this->phones
->get($flags, $limit);
630 private $educations = null
;
631 public function setEducations(ProfileEducation
$edu)
633 $this->educations
= $edu;
636 public function getEducations($flags, $limit = null
)
638 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
639 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
642 if ($this->educations
== null
) {
645 return $this->educations
->get($flags, $limit);
648 public function getExtraEducations($limit = null
)
650 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
655 private $corps = null
;
656 public function setCorps(ProfileCorps
$corps)
658 $this->corps
= $corps;
661 public function getCorps()
663 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
664 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
671 private $networks = null
;
672 public function setNetworking(ProfileNetworking
$nw)
674 $this->networks
= $nw;
677 public function getNetworking($flags, $limit = null
)
679 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
680 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
682 $this->setNetworking($nw);
685 if ($this->networks
== null
) {
688 return $this->networks
->get($flags, $limit);
691 public function getWebSite()
693 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
694 if (count($site) != 1) {
697 $site = array_pop($site);
704 private $jobs = null
;
705 public function setJobs(ProfileJobs
$jobs)
708 $this->consolidateFields();
711 private function fetchJobs()
713 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
714 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
716 $this->setJobs($jobs);
717 $this->fetchAddresses();
722 public function getJobs($flags, $limit = null
)
726 if ($this->jobs
== null
) {
729 return $this->jobs
->get($flags, $limit);
732 public function getMainJob()
734 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
735 if (count($job) != 1) {
738 return array_pop($job);
743 private $jobterms = null
;
744 public function setJobTerms(ProfileJobTerms
$jobterms)
746 $this->jobterms
= $jobterms;
747 $this->consolidateFields();
750 private $mentor_countries = null
;
751 public function setMentoringCountries(ProfileMentoringCountries
$countries)
753 $this->mentor_countries
= $countries;
756 public function getMentoringCountries()
758 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
759 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
762 if ($this->mentor_countries
== null
) {
765 return $this->mentor_countries
->countries
;
769 /** List of job terms to specify mentoring */
770 private $mentor_terms = null
;
772 * set job terms to specify mentoring
773 * @param $terms a ProfileMentoringTerms object listing terms only for this profile
775 public function setMentoringTerms(ProfileMentoringTerms
$terms)
777 $this->mentor_terms
= $terms;
780 * get all job terms that specify mentoring
781 * @return an array of JobTerms objects
783 public function getMentoringTerms()
785 if ($this->mentor_terms
== null
&& !$this->fetched(self
::FETCH_MENTOR_TERMS
)) {
786 $this->setMentoringTerms($this->getProfileField(self
::FETCH_MENTOR_TERMS
));
789 if ($this->mentor_terms
== null
) {
792 return $this->mentor_terms
->get();
799 public function getBinets()
801 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
802 return XDB
::fetchColumn('SELECT binet_id
804 WHERE pid = {?}', $this->id());
809 public function getBinetsNames()
811 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
812 return XDB
::fetchColumn('SELECT text
813 FROM profile_binets AS pb
814 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
815 WHERE pb.pid = {?}', $this->id());
823 private $medals = null
;
824 public function setMedals(ProfileMedals
$medals)
826 $this->medals
= $medals;
829 public function getMedals()
831 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
832 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
834 if ($this->medals
== null
) {
837 return $this->medals
->medals
;
840 public function compareNames($firstname, $lastname)
842 $_lastname = mb_strtoupper($this->lastName());
843 $_firstname = mb_strtoupper($this->firstName());
844 $lastname = mb_strtoupper($lastname);
845 $firstname = mb_strtoupper($firstname);
847 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
848 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
851 foreach ($tokens as $str) {
852 $isOk &= (strpos($_lastname, $str) !== false
);
853 $maxlen = max($maxlen, strlen($str));
856 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
861 public function export()
864 'hrpid' => $this->hrid(),
865 'display_name' => $this->shortName(),
866 'full_name' => $this->fullName(),
867 'directory_name' => $this->directory_name
,
868 'promo' => $this->promo(),
869 'year_promo' => $this->yearpromo(),
870 'is_active' => $this->isActive(),
871 'first_name' => $this->firstName(),
872 'last_name' => $this->lastName(),
873 'is_female' => $this->isFemale(),
877 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
879 if (count($pids) == 0) {
883 if ($respect_order) {
884 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
889 $visibility = new ProfileVisibility($visibility);
891 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
892 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
893 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
894 p.last_change, p.nationality1, p.nationality2, p.nationality3,
895 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
896 pe.entry_year, pe.grad_year, pe.promo_year, pe.program, pe.fieldid,
897 IF ({?}, pse.text, NULL) AS section,
898 pn_f.name AS firstname, pn_l.name AS lastname,
899 IF( {?}, pn_n.name, NULL) AS nickname,
900 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
901 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
902 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
903 pd.directory_name, pd.public_name, pd.private_name,
904 IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
905 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
906 ph.x AS photo_width, ph.y AS photo_height,
907 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
908 pm.expertise AS mentor_expertise,
911 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
912 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
913 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
914 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
915 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
916 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
917 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
918 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
919 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
920 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
921 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
922 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
923 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
924 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
925 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
926 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
927 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
931 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // CV
932 $visibility->levels(), // freetext
933 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // section
934 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // nickname
935 $visibility->levels(), // mobile
936 $visibility->levels(), // photo
939 return new ProfileIterator($it, $pids, $fields, $visibility);
942 public static function getPID($login)
944 if ($login instanceof PlUser
) {
945 return XDB
::fetchOneCell('SELECT pid
946 FROM account_profiles
947 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
949 } else if (ctype_digit($login)) {
950 return XDB
::fetchOneCell('SELECT pid
952 WHERE pid = {?}', $login);
954 return XDB
::fetchOneCell('SELECT pid
956 WHERE hrpid = {?}', $login);
960 public static function getPIDsFromUIDs($uids, $respect_order = true
)
962 if ($respect_order) {
963 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
967 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
968 FROM account_profiles AS ap
969 WHERE FIND_IN_SET(\'owner\', ap.perms)
970 AND ap.uid IN ' . XDB
::formatArray($uids) .'
974 /** Return the profile associated with the given login.
976 public static function get($login, $fields = 0x0000, $visibility = null
)
978 if (is_array($login)) {
979 $pf = new Profile($login);
980 $pf->setVisibilityLevel($visibility);
983 $pid = self
::getPID($login);
984 if (!is_null($pid)) {
985 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
988 /* Let say we can identify a profile using the identifiers of its owner.
990 if (!($login instanceof PlUser
)) {
991 $user = User
::getSilent($login);
992 if ($user && $user->hasProfile()) {
993 return $user->profile();
1000 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1002 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
1005 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1007 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
1010 /** Return profiles for the list of pids.
1012 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
1014 if (count($pids) == 0) {
1017 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
1018 $profiles = array();
1019 while ($p = $it->next()) {
1020 $profiles[$p->id()] = $p;
1025 /** Return profiles for uids.
1027 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
1029 if (count($uids) == 0) {
1032 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
1035 public static function isDisplayName($name)
1037 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
1038 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
1039 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1040 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1043 /** Returns the closest "accounts only" name type for $name
1045 public static function getAccountEquivalentName($name)
1049 case self
::DN_DIRECTORY
:
1051 return 'directory_name';
1053 case self
::DN_PUBLIC
:
1055 case self
::DN_PRIVATE
:
1056 case self
::DN_SHORT
:
1057 case self
::DN_YOURSELF
:
1059 return 'display_name';
1063 public static function getNameTypeId($type, $for_sql = false
)
1065 if (!S
::has('name_types')) {
1066 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
1067 FROM profile_name_enum');
1068 S
::set('name_types', $table);
1070 $table = S
::v('name_types');
1073 return XDB
::escape($table[$type]);
1075 return $table[$type];
1079 public static function rebuildSearchTokens($pids, $transaction = true
)
1081 if (!is_array($pids)) {
1082 $pids = array($pids);
1084 $keys = XDB
::iterator("(SELECT n.pid AS pid, n.name AS name, e.score AS score,
1085 IF(FIND_IN_SET('public', e.flags), 'public', '') AS public
1086 FROM profile_name AS n
1087 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
1088 WHERE n.pid IN {?} AND NOT FIND_IN_SET('not_displayed', e.flags))
1090 (SELECT n.pid AS pid, n.particle AS name, 0 AS score,
1091 IF(FIND_IN_SET('public', e.flags), 'public', '') AS public
1092 FROM profile_name AS n
1093 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
1094 WHERE n.pid IN {?} AND NOT FIND_IN_SET('not_displayed', e.flags))
1098 while ($key = $keys->next()) {
1099 if ($key['name'] == '') {
1103 require_once 'name.func.inc.php';
1104 $toks = split_name_for_search($key['name']);
1105 $toks = array_reverse($toks);
1107 /* Split the score between the tokens to avoid the user to be over-rated.
1108 * Let says my user name is "Machin-Truc Bidule" and I also have a user named
1109 * 'Machin Truc'. Distributing the score force "Machin Truc" to be displayed
1110 * before "Machin-Truc" for both "Machin Truc" and "Machin" searches.
1112 $eltScore = ceil(((float)$key['score'])/((float)count($toks)));
1114 foreach ($toks as $tok) {
1115 $token = $tok . $token;
1116 $names["$pid-$token"] = XDB
::format('({?}, {?}, {?}, {?}, {?})',
1117 $token, $pid, soundex_fr($token),
1118 $eltScore, $key['public']);
1122 XDB
::startTransaction();
1124 XDB
::execute('DELETE FROM search_name
1127 if (count($names) > 0) {
1128 XDB
::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags)
1129 VALUES ' . implode(', ', $names));
1136 /** The school identifier consists of 6 digits. The first 3 represent the
1137 * promotion entry year. The last 3 indicate the student's rank.
1139 * Our identifier consists of 8 digits and both half have the same role.
1140 * This enables us to deal with bigger promotions and with a wider range
1143 * getSchoolId returns a school identifier given one of ours.
1144 * getXorgId returns a X.org identifier given a school identifier.
1146 public static function getSchoolId($xorgId)
1148 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1152 $year = intval(substr($xorgId, 0, 4));
1153 $rank = intval(substr($xorgId, 5, 3));
1156 } elseif ($year < 2000) {
1157 $year = intval(substr(1900 - $year, 1, 3));
1158 return sprintf('%02u0%03u', $year, $rank);
1160 $year = intval(substr(1900 - $year, 1, 3));
1161 return sprintf('%03u%03u', $year, $rank);
1165 public static function getXorgId($schoolId)
1167 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1171 $year = intval(substr($schoolId, 0, 3));
1172 $rank = intval(substr($schoolId, 3, 3));
1180 return sprintf('%04u%04u', 1900 +
$year, $rank);
1186 /** Iterator over a set of Profiles
1188 class ProfileIterator
implements PlIterator
1190 private $iterator = null
;
1192 private $visibility;
1194 const FETCH_ALL
= 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
1196 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
1198 require_once 'profilefields.inc.php';
1200 if ($visibility == null
) {
1201 $visibility = new ProfileVisibility();
1204 $this->fields
= $fields;
1205 $this->visibility
= $visibility;
1208 $callbacks = array();
1211 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1212 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1214 $fields = $fields & self
::FETCH_ALL
;
1215 for ($field = 1; $field < $fields; $field *= 2) {
1216 if (($fields & $field) ) {
1217 $callbacks[$field] = $cb;
1218 $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility);
1222 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1225 private function hasData($field, $vals)
1227 return ($this->fields
& $field) && ($vals[$field] != null
);
1230 private function fillProfile(array $vals)
1232 $pf = Profile
::get($vals[0]);
1233 $pf->setVisibilityLevel($this->visibility
->level());
1234 $pf->setFetchedFields($this->fields
);
1236 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1237 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1239 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1240 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1242 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1243 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1245 if ($this->hasData(Profile
::FETCH_JOB_TERMS
, $vals)) {
1246 $pf->setJobTerms($vals[Profile
::FETCH_JOB_TERMS
]);
1249 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1250 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1252 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1253 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1255 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1256 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1258 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1259 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1265 public function next()
1267 $vals = $this->iterator
->next();
1268 if ($vals == null
) {
1271 return $this->fillProfile($vals);
1274 public function first()
1276 return $this->iterator
->first();
1279 public function last()
1281 return $this->iterator
->last();
1284 public function total()
1286 return $this->iterator
->total();
1290 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: