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'
57 static public $name_variants = array(
58 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
59 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
62 const ADDRESS_MAIN
= 0x00000001;
63 const ADDRESS_PERSO
= 0x00000002;
64 const ADDRESS_PRO
= 0x00000004;
65 const ADDRESS_ALL
= 0x00000006;
66 const ADDRESS_POSTAL
= 0x00000008;
68 const EDUCATION_MAIN
= 0x00000010;
69 const EDUCATION_EXTRA
= 0x00000020;
70 const EDUCATION_ALL
= 0x00000040;
71 const EDUCATION_FINISHED
= 0x00000080;
72 const EDUCATION_CURRENT
= 0x00000100;
74 const JOBS_MAIN
= 0x00001000;
75 const JOBS_ALL
= 0x00002000;
76 const JOBS_FINISHED
= 0x00004000;
77 const JOBS_CURRENT
= 0x00008000;
79 const NETWORKING_ALL
= 0x00070000;
80 const NETWORKING_WEB
= 0x00010000;
81 const NETWORKING_IM
= 0x00020000;
82 const NETWORKING_SOCIAL
= 0x00040000;
84 const PHONE_LINK_JOB
= 0x00100000;
85 const PHONE_LINK_ADDRESS
= 0x00200000;
86 const PHONE_LINK_PROFILE
= 0x00400000;
87 const PHONE_LINK_COMPANY
= 0x00800000;
88 const PHONE_LINK_ANY
= 0x00F00000;
90 const PHONE_TYPE_FAX
= 0x01000000;
91 const PHONE_TYPE_FIXED
= 0x02000000;
92 const PHONE_TYPE_MOBILE
= 0x04000000;
93 const PHONE_TYPE_ANY
= 0x07000000;
95 const PHONE_ANY
= 0x07F00000;
97 const FETCH_ADDRESSES
= 0x000001;
98 const FETCH_CORPS
= 0x000002;
99 const FETCH_EDU
= 0x000004;
100 const FETCH_JOBS
= 0x000008;
101 const FETCH_MEDALS
= 0x000010;
102 const FETCH_NETWORKING
= 0x000020;
103 const FETCH_MENTOR_COUNTRY
= 0x000080;
104 const FETCH_PHONES
= 0x000100;
105 const FETCH_JOB_TERMS
= 0x000200;
106 const FETCH_MENTOR_TERMS
= 0x000400;
108 const FETCH_MINIFICHES
= 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
110 const FETCH_ALL
= 0x0007FF; // OR of FETCH_*
112 static public $descriptions = array(
113 'search_names' => 'Noms',
114 'nationality1' => 'Nationalité',
115 'nationality2' => '2e nationalité',
116 'nationality3' => '3e nationalité',
117 'promo_display' => 'Promotion affichée',
118 'email_directory' => 'Email annuaire papier',
119 'networking' => 'Messageries…',
120 'tels' => 'Téléphones',
121 'edus' => 'Formations',
122 'main_edus' => 'Formations à l\'X',
123 'promo' => 'Promotion de sortie',
124 'birthdate' => 'Date de naissance',
125 'yourself' => 'Nom affiché',
126 'freetext' => 'Commentaire',
127 'freetext_pub' => 'Affichage du commentaire',
128 'photo' => 'Photographie',
129 'photo_pub' => 'Affichage de la photographie',
130 'addresses' => 'Adresses',
134 'section' => 'Section',
135 'binets' => 'Binets',
136 'medals' => 'Décorations',
137 'medals_pub' => 'Affichage des décorations',
138 'competences' => 'Compétences',
139 'langues' => 'Langues',
140 'expertise' => 'Expertises (mentoring)',
141 'terms' => 'Compétences (mentoring)',
142 'countries' => 'Pays (mentoring)'
145 private $fetched_fields = 0x000000;
150 private $owner_fetched = false
;
151 private $data = array();
153 private $visibility = null
;
156 private function __construct(array $data)
159 $this->pid
= $this->data
['pid'];
160 $this->hrpid
= $this->data
['hrpid'];
161 $this->visibility
= new ProfileVisibility();
169 public function hrid()
174 public function owner()
176 if ($this->owner
== null
&& !$this->owner_fetched
) {
177 $this->owner_fetched
= true
;
178 $this->owner
= User
::getSilent($this);
183 public function isActive()
185 if ($this->owner()) {
186 return $this->owner
->isActive();
191 public function promo($details = false
)
193 if ($details && ($this->program ||
$this->fieldid
)) {
195 if ($this->program
) {
196 $text[] = $this->program
;
198 if ($this->fieldid
) {
199 $fieldsList = DirEnum
::getOptions(DirEnum
::EDUFIELDS
);
200 $text[] = $fieldsList[$this->fieldid
];
202 return $this->promo
. ' (' . implode(', ', $text) . ')';
208 public function yearpromo()
210 return $this->promo_year
;
213 /** Check if user is an orange (associated with several promos)
215 public function isMultiPromo()
217 return $this->grad_year
!= $this->entry_year +
$this->mainEducationDuration();
220 /** Returns an array with all associated promo years.
222 public function yearspromo()
225 $d = -$this->deltaPromoToGradYear();
226 for ($g = $this->entry_year +
$this->mainEducationDuration(); $g <= $this->grad_year
; ++
$g) {
232 public function mainEducation()
234 if (empty($this->promo
)) {
237 return $this->promo
{0};
241 public function mainGrade()
243 switch ($this->mainEducation()) {
245 return UserFilter
::GRADE_ING
;
247 return UserFilter
::GRADE_MST
;
249 return UserFilter
::GRADE_PHD
;
255 public function mainEducationDuration()
257 switch ($this->mainEducation()) {
269 /** Number of years between the promotion year until the
270 * graduation year. In standard schools it's 0, but for
271 * Polytechnique the promo year is the entry year.
273 public function deltaPromoToGradYear()
275 if ($this->mainEducation() == 'X') {
276 return $this->mainEducationDuration();
281 /** Print a name with the given formatting:
289 public function name($format)
291 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
292 array($this->isFemale() ?
'•' : '',
293 $this->firstName(), $this->lastName(),
294 $this->fullName(), $this->shortName(),
295 $this->promo()), $format);
298 public function fullName($with_promo = false
)
301 return $this->full_name
. ' (' . $this->promo
. ')';
303 return $this->full_name
;
306 public function shortName($with_promo = false
)
309 return $this->short_name
. ' (' . $this->promo
. ')';
311 return $this->short_name
;
314 public function firstName()
316 return $this->firstname
;
319 public function firstNames()
321 return $this->nameVariants(self
::FIRSTNAME
);
324 public function lastName()
326 return $this->lastname
;
329 public function lastNames()
331 return $this->nameVariants(self
::LASTNAME
);
334 public function isFemale()
336 return $this->sex
== PlUser
::GENDER_FEMALE
;
339 public function isDead()
341 return ($this->deathdate
!= null
);
344 public function displayEmail()
348 return $o->bestEmail();
350 return $this->email_directory
;
354 public function data()
360 private function nameVariants($type)
362 $vals = array($this->$type);
363 foreach (self
::$name_variants[$type] as $var) {
364 $vartype = $type . '_' . $var;
365 $varname = $this->$vartype;
366 if ($varname != null
&& $varname != "") {
370 return array_unique($vals);
373 public function nationalities()
376 $countries = DirEnum
::getOptions(DirEnum
::COUNTRIES
);
377 if ($this->nationality1
) {
378 $nats[$this->nationality1
] = $countries[$this->nationality1
];
380 if ($this->nationality2
) {
381 $nats[$this->nationality2
] = $countries[$this->nationality2
];
383 if ($this->nationality3
) {
384 $nats[$this->nationality3
] = $countries[$this->nationality3
];
389 public function __get($name)
391 if (property_exists($this, $name)) {
395 if (isset($this->data
[$name])) {
396 return $this->data
[$name];
402 public function __isset($name)
404 return property_exists($this, $name) ||
isset($this->data
[$name]);
407 public function __unset($name)
409 if (property_exists($this, $name)) {
412 unset($this->data
[$name]);
419 * *always deletes in: profile_addresses, profile_binets, profile_job,
420 * profile_langskills, profile_mentor, profile_networking,
421 * profile_phones, profile_skills, watch_profile
422 * *always keeps in: profile_corps, profile_display, profile_education,
423 * profile_medals, profile_name, profile_photos, search_name
424 * *modifies: profiles
426 public function clear()
429 'profile_job', 'profile_langskills', 'profile_mentor',
430 'profile_networking', 'profile_skills', 'watch_profile',
431 'profile_phones', 'profile_addresses', 'profile_binets');
433 foreach ($tables as $t) {
434 XDB
::execute('DELETE FROM ' . $t . '
439 XDB
::execute("UPDATE profiles
440 SET cv = NULL, freetext = NULL, freetext_pub = 'private',
441 medals_pub = 'private', alias_pub = 'private',
442 email_directory = NULL
447 /** Sets the level of visibility of the profile
448 * Sets $this->visibility to a list of valid visibilities.
449 * @param one of the self::VIS_* values
451 public function setVisibilityLevel($visibility)
453 $this->visibility
->setLevel($visibility);
456 /** Determine whether an item with visibility $visibility can be displayed
457 * with the current level of visibility of the profile
458 * @param $visibility The level of visibility to be checked
460 public function isVisible($visibility)
462 return $this->visibility
->isVisible($visibility);
465 /** Stores the list of fields which have already been fetched for this Profile
467 public function setFetchedFields($fields)
469 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
470 Platal
::page()->kill("Invalid fetched fields: $fields");
473 $this->fetched_fields
= $fields;
476 /** Have we already fetched this field ?
478 private function fetched($field)
480 if (!array_key_exists($field, ProfileField
::$fields)) {
481 Platal
::page()->kill("Invalid field: $field");
484 return ($this->fetched_fields
& $field);
487 /** If not already done, fetches data for the given field
488 * @param $field One of the Profile::FETCH_*
489 * @return A ProfileField, or null
491 private function getProfileField($field)
493 if (!array_key_exists($field, ProfileField
::$fields)) {
494 Platal
::page()->kill("Invalid field: $field");
496 if ($this->fetched($field)) {
499 $this->fetched_fields
= $this->fetched_fields |
$field;
502 $cls = ProfileField
::$fields[$field];
504 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
507 /** Consolidates internal data (addresses, phones, jobs)
509 private function consolidateFields()
511 // Link phones to addresses
512 if ($this->phones
!= null
) {
513 if ($this->addresses
!= null
) {
514 $this->addresses
->addPhones($this->phones
);
517 if ($this->jobs
!= null
) {
518 $this->jobs
->addPhones($this->phones
);
522 // Link addresses to jobs
523 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
524 $this->jobs
->addAddresses($this->addresses
);
527 // Link jobterms to jobs
528 if ($this->jobs
!= null
&& $this->jobterms
!= null
) {
529 $this->jobs
->addJobTerms($this->jobterms
);
535 private $photo = null
;
536 public function getPhoto($fallback = true
, $data = false
)
538 if ($this->has_photo
) {
539 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
540 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y, last_update
542 WHERE pid = {?}', $this->pid
);
543 $this->photo
= PlImage
::fromData($res['attach'], 'image/' .$res['attachmime'], $res['x'], $res['y'], $res['last_update']);
544 } else if ($this->photo
== null
) {
545 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
548 } else if ($fallback) {
549 if ($this->mainEducation() == 'X') {
550 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_x.png', 'image/png');
552 return PlImage
::fromFile(dirname(__FILE__
) . '/../htdocs/images/none_md.png', 'image/png');
559 private $addresses = null
;
560 public function setAddresses(ProfileAddresses
$addr)
562 $this->addresses
= $addr;
563 $this->consolidateFields();
566 private function fetchAddresses()
568 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
569 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
571 $this->setAddresses($addr);
572 $this->fetchPhones();
577 public function getAddresses($flags, $limit = null
)
579 $this->fetchAddresses();
581 if ($this->addresses
== null
) {
584 return $this->addresses
->get($flags, $limit);
587 public function iterAddresses($flags, $limit = null
)
589 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
592 public function getMainAddress()
594 $main = $this->getAddresses(self
::ADDRESS_MAIN
);
595 $perso = $this->getAddresses(self
::ADDRESS_PERSO
);
598 return array_pop($main);
599 } else if (count($perso)) {
600 return array_pop($perso);
608 private $phones = null
;
609 public function setPhones(ProfilePhones
$phones)
611 $this->phones
= $phones;
612 $this->consolidateFields();
615 private function fetchPhones()
617 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
618 $phones = $this->getProfileField(self
::FETCH_PHONES
);
619 if (isset($phones)) {
620 $this->setPhones($phones);
625 public function getPhones($flags, $limit = null
)
627 $this->fetchPhones();
628 if ($this->phones
== null
) {
631 return $this->phones
->get($flags, $limit);
636 private $educations = null
;
637 public function setEducations(ProfileEducation
$edu)
639 $this->educations
= $edu;
642 public function getEducations($flags, $limit = null
)
644 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
645 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
648 if ($this->educations
== null
) {
651 return $this->educations
->get($flags, $limit);
654 public function getExtraEducations($limit = null
)
656 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
661 private $corps = null
;
662 public function setCorps(ProfileCorps
$corps)
664 $this->corps
= $corps;
667 public function getCorps()
669 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
670 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
677 private $networks = null
;
678 public function setNetworking(ProfileNetworking
$nw)
680 $this->networks
= $nw;
683 public function getNetworking($flags, $limit = null
)
685 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
686 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
688 $this->setNetworking($nw);
691 if ($this->networks
== null
) {
694 return $this->networks
->get($flags, $limit);
697 public function getWebSite()
699 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
700 if (count($site) != 1) {
703 $site = array_pop($site);
710 private $jobs = null
;
711 public function setJobs(ProfileJobs
$jobs)
714 $this->consolidateFields();
717 private function fetchJobs()
719 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
720 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
722 $this->setJobs($jobs);
723 $this->fetchAddresses();
728 public function getJobs($flags, $limit = null
)
732 if ($this->jobs
== null
) {
735 return $this->jobs
->get($flags, $limit);
738 public function getMainJob()
740 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
741 if (count($job) != 1) {
744 return array_pop($job);
749 private $jobterms = null
;
750 public function setJobTerms(ProfileJobTerms
$jobterms)
752 $this->jobterms
= $jobterms;
753 $this->consolidateFields();
756 private $mentor_countries = null
;
757 public function setMentoringCountries(ProfileMentoringCountries
$countries)
759 $this->mentor_countries
= $countries;
762 public function getMentoringCountries()
764 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
765 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
768 if ($this->mentor_countries
== null
) {
771 return $this->mentor_countries
->countries
;
775 /** List of job terms to specify mentoring */
776 private $mentor_terms = null
;
778 * set job terms to specify mentoring
779 * @param $terms a ProfileMentoringTerms object listing terms only for this profile
781 public function setMentoringTerms(ProfileMentoringTerms
$terms)
783 $this->mentor_terms
= $terms;
786 * get all job terms that specify mentoring
787 * @return an array of JobTerms objects
789 public function getMentoringTerms()
791 if ($this->mentor_terms
== null
&& !$this->fetched(self
::FETCH_MENTOR_TERMS
)) {
792 $this->setMentoringTerms($this->getProfileField(self
::FETCH_MENTOR_TERMS
));
795 if ($this->mentor_terms
== null
) {
798 return $this->mentor_terms
->get();
805 public function getBinets()
807 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
808 return XDB
::fetchColumn('SELECT binet_id
810 WHERE pid = {?}', $this->id());
815 public function getBinetsNames()
817 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
818 return XDB
::fetchColumn('SELECT text
819 FROM profile_binets AS pb
820 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
821 WHERE pb.pid = {?}', $this->id());
829 private $medals = null
;
830 public function setMedals(ProfileMedals
$medals)
832 $this->medals
= $medals;
835 public function getMedals()
837 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
838 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
840 if ($this->medals
== null
) {
843 return $this->medals
->medals
;
846 public function compareNames($firstname, $lastname)
848 $_lastname = mb_strtoupper($this->lastName());
849 $_firstname = mb_strtoupper($this->firstName());
850 $lastname = mb_strtoupper($lastname);
851 $firstname = mb_strtoupper($firstname);
853 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
854 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
857 foreach ($tokens as $str) {
858 $isOk &= (strpos($_lastname, $str) !== false
);
859 $maxlen = max($maxlen, strlen($str));
862 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
867 public function export()
870 'hrpid' => $this->hrid(),
871 'display_name' => $this->shortName(),
872 'full_name' => $this->fullName(),
873 'directory_name' => $this->directory_name
,
874 'promo' => $this->promo(),
875 'year_promo' => $this->yearpromo(),
876 'is_active' => $this->isActive(),
877 'first_name' => $this->firstName(),
878 'last_name' => $this->lastName(),
879 'is_female' => $this->isFemale(),
883 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
885 if (count($pids) == 0) {
889 if ($respect_order) {
890 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
895 $visibility = new ProfileVisibility($visibility);
897 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
898 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
899 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
900 p.last_change, p.nationality1, p.nationality2, p.nationality3,
901 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
902 pe.entry_year, pe.grad_year, pe.promo_year, pe.program, pe.fieldid,
903 IF ({?}, pse.text, NULL) AS section,
904 pn_f.name AS firstname, pn_l.name AS lastname,
905 IF( {?}, pn_n.name, NULL) AS nickname,
906 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
907 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
908 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
909 pd.directory_name, pd.public_name, pd.private_name,
910 IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
911 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
912 ph.x AS photo_width, ph.y AS photo_height,
913 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
914 pm.expertise AS mentor_expertise,
917 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
918 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
919 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
920 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
921 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
922 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
923 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
924 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
925 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
926 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
927 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
928 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
929 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
930 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
931 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
932 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
933 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
937 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // CV
938 $visibility->levels(), // freetext
939 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // section
940 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // nickname
941 $visibility->levels(), // mobile
942 $visibility->levels(), // photo
945 return new ProfileIterator($it, $pids, $fields, $visibility);
948 public static function getPID($login)
950 if ($login instanceof PlUser
) {
951 return XDB
::fetchOneCell('SELECT pid
952 FROM account_profiles
953 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
955 } else if (ctype_digit($login)) {
956 return XDB
::fetchOneCell('SELECT pid
958 WHERE pid = {?}', $login);
960 return XDB
::fetchOneCell('SELECT pid
962 WHERE hrpid = {?}', $login);
966 public static function getPIDsFromUIDs($uids, $respect_order = true
)
968 if ($respect_order) {
969 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
973 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
974 FROM account_profiles AS ap
975 WHERE FIND_IN_SET(\'owner\', ap.perms)
976 AND ap.uid IN ' . XDB
::formatArray($uids) .'
980 /** Return the profile associated with the given login.
982 public static function get($login, $fields = 0x0000, $visibility = null
)
984 if (is_array($login)) {
985 $pf = new Profile($login);
986 $pf->setVisibilityLevel($visibility);
989 $pid = self
::getPID($login);
990 if (!is_null($pid)) {
991 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
994 /* Let say we can identify a profile using the identifiers of its owner.
996 if (!($login instanceof PlUser
)) {
997 $user = User
::getSilent($login);
998 if ($user && $user->hasProfile()) {
999 return $user->profile();
1006 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1008 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
1011 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1013 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
1016 /** Return profiles for the list of pids.
1018 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
1020 if (count($pids) == 0) {
1023 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
1024 $profiles = array();
1025 while ($p = $it->next()) {
1026 $profiles[$p->id()] = $p;
1031 /** Return profiles for uids.
1033 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
1035 if (count($uids) == 0) {
1038 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
1041 public static function isDisplayName($name)
1043 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
1044 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
1045 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1046 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1049 /** Returns the closest "accounts only" name type for $name
1051 public static function getAccountEquivalentName($name)
1055 case self
::DN_DIRECTORY
:
1057 return 'directory_name';
1059 case self
::DN_PUBLIC
:
1061 case self
::DN_PRIVATE
:
1062 case self
::DN_SHORT
:
1063 case self
::DN_YOURSELF
:
1065 return 'display_name';
1069 public static function getNameTypeId($type, $for_sql = false
)
1071 if (!S
::has('name_types')) {
1072 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
1073 FROM profile_name_enum');
1074 S
::set('name_types', $table);
1076 $table = S
::v('name_types');
1079 return XDB
::escape($table[$type]);
1081 return $table[$type];
1085 public static function rebuildSearchTokens($pids, $transaction = true
)
1087 if (!is_array($pids)) {
1088 $pids = array($pids);
1090 $keys = XDB
::iterator("(SELECT n.pid AS pid, n.name AS name, e.score 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))
1096 (SELECT n.pid AS pid, n.particle AS name, 0 AS score,
1097 IF(FIND_IN_SET('public', e.flags), 'public', '') AS public
1098 FROM profile_name AS n
1099 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
1100 WHERE n.pid IN {?} AND NOT FIND_IN_SET('not_displayed', e.flags))
1104 while ($key = $keys->next()) {
1105 if ($key['name'] == '') {
1109 require_once 'name.func.inc.php';
1110 $toks = split_name_for_search($key['name']);
1111 $toks = array_reverse($toks);
1113 /* Split the score between the tokens to avoid the user to be over-rated.
1114 * Let says my user name is "Machin-Truc Bidule" and I also have a user named
1115 * 'Machin Truc'. Distributing the score force "Machin Truc" to be displayed
1116 * before "Machin-Truc" for both "Machin Truc" and "Machin" searches.
1118 $eltScore = ceil(((float)$key['score'])/((float)count($toks)));
1120 foreach ($toks as $tok) {
1121 $token = $tok . $token;
1122 $names["$pid-$token"] = XDB
::format('({?}, {?}, {?}, {?}, {?})',
1123 $token, $pid, soundex_fr($token),
1124 $eltScore, $key['public']);
1128 XDB
::startTransaction();
1130 XDB
::execute('DELETE FROM search_name
1133 if (count($names) > 0) {
1134 XDB
::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags)
1135 VALUES ' . implode(', ', $names));
1142 /** The school identifier consists of 6 digits. The first 3 represent the
1143 * promotion entry year. The last 3 indicate the student's rank.
1145 * Our identifier consists of 8 digits and both half have the same role.
1146 * This enables us to deal with bigger promotions and with a wider range
1149 * getSchoolId returns a school identifier given one of ours.
1150 * getXorgId returns a X.org identifier given a school identifier.
1152 public static function getSchoolId($xorgId)
1154 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1158 $year = intval(substr($xorgId, 0, 4));
1159 $rank = intval(substr($xorgId, 5, 3));
1162 } elseif ($year < 2000) {
1163 $year = intval(substr(1900 - $year, 1, 3));
1164 return sprintf('%02u0%03u', $year, $rank);
1166 $year = intval(substr(1900 - $year, 1, 3));
1167 return sprintf('%03u%03u', $year, $rank);
1171 public static function getXorgId($schoolId)
1173 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1177 $year = intval(substr($schoolId, 0, 3));
1178 $rank = intval(substr($schoolId, 3, 3));
1186 return sprintf('%04u%04u', 1900 +
$year, $rank);
1192 /** Iterator over a set of Profiles
1194 class ProfileIterator
implements PlIterator
1196 private $iterator = null
;
1198 private $visibility;
1200 const FETCH_ALL
= 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
1202 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
1204 require_once 'profilefields.inc.php';
1206 if ($visibility == null
) {
1207 $visibility = new ProfileVisibility();
1210 $this->fields
= $fields;
1211 $this->visibility
= $visibility;
1214 $callbacks = array();
1217 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1218 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1220 $fields = $fields & self
::FETCH_ALL
;
1221 for ($field = 1; $field < $fields; $field *= 2) {
1222 if (($fields & $field) ) {
1223 $callbacks[$field] = $cb;
1224 $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility);
1228 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1231 private function hasData($field, $vals)
1233 return ($this->fields
& $field) && ($vals[$field] != null
);
1236 private function fillProfile(array $vals)
1238 $pf = Profile
::get($vals[0]);
1239 $pf->setVisibilityLevel($this->visibility
->level());
1240 $pf->setFetchedFields($this->fields
);
1242 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1243 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1245 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1246 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1248 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1249 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1251 if ($this->hasData(Profile
::FETCH_JOB_TERMS
, $vals)) {
1252 $pf->setJobTerms($vals[Profile
::FETCH_JOB_TERMS
]);
1255 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1256 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1258 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1259 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1261 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1262 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1264 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1265 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1271 public function next()
1273 $vals = $this->iterator
->next();
1274 if ($vals == null
) {
1277 return $this->fillProfile($vals);
1280 public function first()
1282 return $this->iterator
->first();
1285 public function last()
1287 return $this->iterator
->last();
1290 public function total()
1292 return $this->iterator
->total();
1296 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: