2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 ProfileVisibility
24 static private $v_values = array(self
::VIS_PUBLIC
=> array(self
::VIS_PUBLIC
),
25 self
::VIS_AX
=> array(self
::VIS_AX
, self
::VIS_PUBLIC
),
26 self
::VIS_PRIVATE
=> array(self
::VIS_PRIVATE
, self
::VIS_AX
, self
::VIS_PUBLIC
));
28 const VIS_PUBLIC
= 'public';
30 const VIS_PRIVATE
= 'private';
34 public function __construct($level = null
)
36 $this->setLevel($level);
39 public function setLevel($level = self
::VIS_PUBLIC
)
41 if ($level != null
&& $level != self
::VIS_PRIVATE
&& $level != self
::VIS_AX
&& $level != self
::VIS_PUBLIC
) {
42 Platal
::page()->kill("Invalid visibility: " . $level);
46 $level = self
::VIS_PUBLIC
;
47 } else if ($level == null
) {
48 $level = self
::VIS_PRIVATE
;
51 if ($this->level
== null ||
$this->level
== self
::VIS_PRIVATE
) {
52 $this->level
= $level;
53 } else if ($this->level
== self
::VIS_AX
&& $level == self
::VIS_PRIVATE
) {
56 $this->level
= self
::VIS_PUBLIC
;
60 public function level()
62 if ($this->level
== null
) {
63 return self
::VIS_PUBLIC
;
69 public function levels()
71 return self
::$v_values[$this->level()];
74 public function isVisible($visibility)
76 return in_array($visibility, $this->levels());
84 const LASTNAME
= 'lastname';
85 const FIRSTNAME
= 'firstname';
86 const NICKNAME
= 'nickname';
87 const PSEUDONYM
= 'pseudonym';
90 const VN_MARITAL
= 'marital';
91 const VN_ORDINARY
= 'ordinary';
92 const VN_OTHER
= 'other';
95 const DN_FULL
= 'directory_name';
96 const DN_DISPLAY
= 'yourself';
97 const DN_YOURSELF
= 'yourself';
98 const DN_DIRECTORY
= 'directory_name';
99 const DN_PRIVATE
= 'private_name';
100 const DN_PUBLIC
= 'public_name';
101 const DN_SHORT
= 'short_name';
102 const DN_SORT
= 'sort_name';
103 /* education related names */
104 const EDU_X
= 'École polytechnique';
105 const DEGREE_X
= 'Ingénieur';
106 const DEGREE_M
= 'Master';
107 const DEGREE_D
= 'Doctorat';
109 static public $name_variants = array(
110 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
111 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
114 const ADDRESS_MAIN
= 0x000001;
115 const ADDRESS_PERSO
= 0x000002;
116 const ADDRESS_PRO
= 0x000004;
117 const ADDRESS_ALL
= 0x000006;
118 const ADDRESS_POSTAL
= 0x000008;
120 const EDUCATION_MAIN
= 0x000010;
121 const EDUCATION_EXTRA
= 0x000020;
122 const EDUCATION_ALL
= 0x000040;
123 const EDUCATION_FINISHED
= 0x000080;
124 const EDUCATION_CURRENT
= 0x000100;
126 const JOBS_MAIN
= 0x001000;
127 const JOBS_ALL
= 0x002000;
128 const JOBS_FINISHED
= 0x004000;
129 const JOBS_CURRENT
= 0x008000;
131 const NETWORKING_ALL
= 0x000000;
132 const NETWORKING_WEB
= 0x010000;
133 const NETWORKING_IM
= 0x020000;
134 const NETWORKING_SOCIAL
= 0x040000;
136 const FETCH_ADDRESSES
= 0x000001;
137 const FETCH_CORPS
= 0x000002;
138 const FETCH_EDU
= 0x000004;
139 const FETCH_JOBS
= 0x000008;
140 const FETCH_MEDALS
= 0x000010;
141 const FETCH_NETWORKING
= 0x000020;
142 const FETCH_MENTOR_SECTOR
= 0x000040;
143 const FETCH_MENTOR_COUNTRY
= 0x000080;
144 const FETCH_PHONES
= 0x000100;
146 const FETCH_MINIFICHES
= 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
148 const FETCH_ALL
= 0x0001FF; // OR of FETCH_*
150 private $fetched_fields = 0x000000;
155 private $owner_fetched = false
;
156 private $data = array();
158 private $visibility = null
;
161 private function __construct(array $data)
164 $this->pid
= $this->data
['pid'];
165 $this->hrpid
= $this->data
['hrpid'];
166 $this->visibility
= new ProfileVisibility();
174 public function hrid()
179 public function owner()
181 if ($this->owner
== null
&& !$this->owner_fetched
) {
182 $this->owner_fetched
= true
;
183 $this->owner
= User
::getSilent($this);
188 public function promo()
193 public function yearpromo()
195 return intval(substr($this->promo
, 1, 4));
198 public function mainEducation()
200 if (empty($this->promo
)) {
203 return $this->promo
{0};
207 public function mainGrade()
209 switch ($this->mainEducation()) {
211 return UserFilter
::GRADE_ING
;
213 return UserFilter
::GRADE_MST
;
215 return UserFilter
::GRADE_PHD
;
221 public function mainEducationDuration()
223 switch ($this->mainEducation()) {
235 /** Print a name with the given formatting:
243 public function name($format)
245 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
246 array($this->isFemale() ?
'•' : '',
247 $this->firstName(), $this->lastName(),
248 $this->fullName(), $this->shortName(),
249 $this->promo()), $format);
252 public function fullName($with_promo = false
)
255 return $this->full_name
. ' (' . $this->promo
. ')';
257 return $this->full_name
;
260 public function shortName($with_promo = false
)
263 return $this->short_name
. ' (' . $this->promo
. ')';
265 return $this->short_name
;
268 public function firstName()
270 return $this->firstname
;
273 public function firstNames()
275 return $this->nameVariants(self
::FIRSTNAME
);
278 public function lastName()
280 return $this->lastname
;
283 public function lastNames()
285 return $this->nameVariants(self
::LASTNAME
);
288 public function isFemale()
290 return $this->sex
== PlUser
::GENDER_FEMALE
;
293 public function isDead()
295 return ($this->deathdate
!= null
);
298 public function displayEmail()
302 return $o->bestEmail();
304 return $this->email_directory
;
308 public function data()
314 private function nameVariants($type)
316 $vals = array($this->$type);
317 foreach (self
::$name_variants[$type] as $var) {
318 $vartype = $type . '_' . $var;
319 $varname = $this->$vartype;
320 if ($varname != null
&& $varname != "") {
324 return array_unique($vals);
327 public function nationalities()
330 if ($this->nationality1
) {
331 $nats[] = $this->nationality1
;
333 if ($this->nationality2
) {
334 $nats[] = $this->nationality2
;
336 if ($this->nationality3
) {
337 $nats[] = $this->nationality3
;
342 public function __get($name)
344 if (property_exists($this, $name)) {
348 if (isset($this->data
[$name])) {
349 return $this->data
[$name];
355 public function __isset($name)
357 return property_exists($this, $name) ||
isset($this->data
[$name]);
360 /** Sets the level of visibility of the profile
361 * Sets $this->visibility to a list of valid visibilities.
362 * @param one of the self::VIS_* values
364 public function setVisibilityLevel($visibility)
366 $this->visibility
->setLevel($visibility);
369 /** Determine whether an item with visibility $visibility can be displayed
370 * with the current level of visibility of the profile
371 * @param $visibility The level of visibility to be checked
373 public function isVisible($visibility)
375 return $this->visibility
->isVisible($visibility);
378 /** Stores the list of fields which have already been fetched for this Profile
380 public function setFetchedFields($fields)
382 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
383 Platal
::page()->kill("Invalid fetched fields: $fields");
386 $this->fetched_fields
= $fields;
389 private function fetched($field)
391 if (!array_key_exists($field, ProfileField
::$fields)) {
392 Platal
::page()->kill("Invalid field: $field");
395 return ($this->fetched_fields
& $field);
398 /** If not already done, fetches data for the given field
399 * @param $field One of the Profile::FETCH_*
400 * @return A ProfileField, or null
402 private function getProfileField($field)
404 if ($this->fetched($field)) {
407 $this->fetched_fields
= $this->fetched_fields |
$field;
410 $cls = ProfileField
::$fields[$field];
412 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
415 /** Consolidates internal data (addresses, phones, jobs)
417 private function consolidateFields()
419 if ($this->phones
!= null
) {
420 if ($this->addresses
!= null
) {
421 $this->addresses
->addPhones($this->phones
);
424 if ($this->jobs
!= null
) {
425 $this->jobs
->addPhones($this->phones
);
429 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
430 $this->jobs
->addAddresses($this->addresses
);
436 private $photo = null
;
437 public function getPhoto($fallback = true
, $data = false
)
439 if ($this->has_photo
) {
440 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
441 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y
443 WHERE pid = {?}', $this->pid
);
444 $this->photo
= PlImage
::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
445 } else if ($this->photo
== null
) {
446 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
449 } else if ($fallback) {
450 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
458 private $addresses = null
;
459 public function setAddresses(ProfileAddresses
$addr)
461 $this->addresses
= $addr;
462 $this->consolidateFields();
465 public function getAddresses($flags, $limit = null
)
467 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
468 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
470 $this->setAddresses($addr);
474 if ($this->addresses
== null
) {
477 return $this->addresses
->get($flags, $limit);
480 public function iterAddresses($flags, $limit = null
)
482 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
485 public function getMainAddress()
487 $addr = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
488 if (count($addr) == 0) {
491 return array_pop($addr);
497 private $phones = null
;
498 public function setPhones(ProfilePhones
$phones)
500 $this->phones
= $phones;
501 $this->consolidateFields();
504 public function getPhones($flags, $limit = null
)
506 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
507 $this->setPhones($this->getProfileField(self
::FETCH_PHONES
));
510 if ($this->phones
== null
) {
513 return $this->phones
->get($flags, $limit);
518 private $educations = null
;
519 public function setEducations(ProfileEducation
$edu)
521 $this->educations
= $edu;
524 public function getEducations($flags, $limit = null
)
526 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
527 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
530 if ($this->educations
== null
) {
533 return $this->educations
->get($flags, $limit);
536 public function getExtraEducations($limit = null
)
538 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
543 private $corps = null
;
544 public function setCorps(ProfileCorps
$corps)
546 $this->corps
= $corps;
549 public function getCorps()
551 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
552 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
559 private $networks = null
;
560 public function setNetworking(ProfileNetworking
$nw)
562 $this->networks
= $nw;
565 public function getNetworking($flags, $limit = null
)
567 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
568 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
570 $this->setNetworking($nw);
573 if ($this->networks
== null
) {
576 return $this->networks
->get($flags, $limit);
579 public function getWebSite()
581 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
582 if (count($site) != 1) {
585 $site = array_pop($site);
592 private $jobs = null
;
593 public function setJobs(ProfileJobs
$jobs)
596 $this->consolidateFields();
599 public function getJobs($flags, $limit = null
)
601 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
602 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
604 $this->setJobs($jobs);
608 if ($this->jobs
== null
) {
611 return $this->jobs
->get($flags, $limit);
614 public function getMainJob()
616 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
617 if (count($job) != 1) {
620 return array_pop($job);
625 private $mentor_sectors = null
;
626 public function setMentoringSectors(ProfileMentoringSectors
$sectors)
628 $this->mentor_sectors
= $sectors;
631 public function getMentoringSectors()
633 if ($this->mentor_sectors
== null
&& !$this->fetched(self
::FETCH_MENTOR_SECTOR
)) {
634 $this->setMentoringSectors($this->getProfileField(self
::FETCH_MENTOR_SECTOR
));
637 if ($this->mentor_sectors
== null
) {
640 return $this->mentor_sectors
->sectors
;
644 private $mentor_countries = null
;
645 public function setMentoringCountries(ProfileMentoringCountries
$countries)
647 $this->mentor_countries
= $countries;
650 public function getMentoringCountries()
652 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
653 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
656 if ($this->mentor_countries
== null
) {
659 return $this->mentor_countries
->countries
;
665 public function getBinets()
667 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
668 return XDB
::fetchColumn('SELECT binet_id
670 WHERE pid = {?}', $this->id());
675 public function getBinetsNames()
677 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
678 return XDB
::fetchColumn('SELECT text
679 FROM profile_binets AS pb
680 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
681 WHERE pb.pid = {?}', $this->id());
689 private $medals = null
;
690 public function setMedals(ProfileMedals
$medals)
692 $this->medals
= $medals;
695 public function getMedals()
697 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
698 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
700 if ($this->medals
== null
) {
703 return $this->medals
->medals
;
706 public function compareNames($firstname, $lastname)
708 $_lastname = mb_strtoupper($this->lastName());
709 $_firstname = mb_strtoupper($this->firstName());
710 $lastname = mb_strtoupper($lastname);
711 $firstname = mb_strtoupper($firstname);
713 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
714 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
717 foreach ($tokens as $str) {
718 $isOk &= (strpos($_lastname, $str) !== false
);
719 $maxlen = max($maxlen, strlen($str));
722 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
725 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
727 if (count($pids) == 0) {
731 if ($respect_order) {
732 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
737 $visibility = new ProfileVisibility($visibility);
739 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
740 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
741 p.cv, p.medals_pub, p.alias_pub, p.email_directory, p.last_change,
742 p.nationality1, p.nationality2, p.nationality3,
743 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
744 pe.entry_year, pe.grad_year,
745 IF ({?} IN {?}, pse.text, NULL) AS section,
746 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
747 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
748 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
749 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
750 pd.directory_name, IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
751 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
752 ph.x AS photo_width, ph.y AS photo_height,
753 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
754 pm.expertise AS mentor_expertise,
757 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
758 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
759 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
760 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
761 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
762 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
763 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
764 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
765 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
766 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
767 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
768 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
769 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
770 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
771 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
772 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
773 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
777 $visibility->levels(),
778 ProfileVisibility
::VIS_PRIVATE
, $visibility->levels(),
779 $visibility->levels(), $visibility->levels(),
782 return new ProfileIterator($it, $pids, $fields, $visibility);
785 public static function getPID($login)
787 if ($login instanceof PlUser
) {
788 return XDB
::fetchOneCell('SELECT pid
789 FROM account_profiles
790 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
792 } else if (ctype_digit($login)) {
793 return XDB
::fetchOneCell('SELECT pid
795 WHERE pid = {?}', $login);
797 return XDB
::fetchOneCell('SELECT pid
799 WHERE hrpid = {?}', $login);
803 public static function getPIDsFromUIDs($uids, $respect_order = true
)
805 if ($respect_order) {
806 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
810 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
811 FROM account_profiles AS ap
812 WHERE FIND_IN_SET(\'owner\', ap.perms)
813 AND ap.uid IN ' . XDB
::formatArray($uids) .'
817 /** Return the profile associated with the given login.
819 public static function get($login, $fields = 0x0000, $visibility = null
)
821 if (is_array($login)) {
822 $pf = new Profile($login);
823 $pf->setVisibilityLevel($visibility);
826 $pid = self
::getPID($login);
827 if (!is_null($pid)) {
828 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
831 /* Let say we can identify a profile using the identifiers of its owner.
833 if (!($login instanceof PlUser
)) {
834 $user = User
::getSilent($login);
835 if ($user && $user->hasProfile()) {
836 return $user->profile();
843 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
845 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
848 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
850 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
853 /** Return profiles for the list of pids.
855 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
857 if (count($pids) == 0) {
860 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
862 while ($p = $it->next()) {
863 $profiles[$p->id()] = $p;
868 /** Return profiles for uids.
870 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
872 if (count($uids) == 0) {
875 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
878 public static function isDisplayName($name)
880 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
881 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
882 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
883 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
886 public static function getNameTypeId($type, $for_sql = false
)
888 if (!S
::has('name_types')) {
889 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
890 FROM profile_name_enum');
891 S
::set('name_types', $table);
893 $table = S
::v('name_types');
896 return XDB
::escape($table[$type]);
898 return $table[$type];
902 public static function rebuildSearchTokens($pid)
904 XDB
::execute('DELETE FROM search_name
907 $keys = XDB
::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
908 FIND_IN_SET('public', e.flags) AS public
909 FROM profile_name AS n
910 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
914 while ($key = $keys->next()) {
915 if ($key['name'] == '') {
918 $toks = preg_split('/[ \'\-]+/', $key['name']);
922 $token = strtolower(replace_accent(array_pop($toks) . $token));
923 $score = ($toks ?
0 : 10 +
$first) * ($key['score'] / 10);
924 XDB
::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
925 VALUES ({?}, {?}, {?}, {?}, {?})',
926 $token, $pid, soundex_fr($token), $score, $key['public']);
932 /** The school identifier consists of 6 digits. The first 3 represent the
933 * promotion entry year. The last 3 indicate the student's rank.
935 * Our identifier consists of 8 digits and both half have the same role.
936 * This enables us to deal with bigger promotions and with a wider range
939 * getSchoolId returns a school identifier given one of ours.
940 * getXorgId returns a X.org identifier given a school identifier.
942 public static function getSchoolId($xorgId)
944 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
948 $year = intval(substr($xorgId, 0, 4));
949 $rank = intval(substr($xorgId, 5, 3));
952 } elseif ($year < 2000) {
953 $year = intval(substr(1900 - $year, 1, 3));
954 return sprintf('%02u0%03u', $year, $rank);
956 $year = intval(substr(1900 - $year, 1, 3));
957 return sprintf('%03u%03u', $year, $rank);
961 public static function getXorgId($schoolId)
963 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
967 $year = intval(substr($schoolId, 0, 3));
968 $rank = intval(substr($schoolId, 3, 3));
976 return sprintf('%04u%04u', 1900 +
$year, $rank);
982 /** Iterator over a set of Profiles
984 class ProfileIterator
implements PlIterator
986 private $iterator = null
;
990 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
992 require_once 'profilefields.inc.php';
994 if ($visibility == null
) {
995 $visibility = new ProfileVisibility();
998 $this->fields
= $fields;
999 $this->visibility
= $visibility;
1002 $callbacks = array();
1005 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1006 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1008 if ($fields & Profile
::FETCH_ADDRESSES
) {
1009 $callbacks[Profile
::FETCH_ADDRESSES
] = $cb;
1010 $subits[Profile
::FETCH_ADDRESSES
] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
1013 if ($fields & Profile
::FETCH_CORPS
) {
1014 $callbacks[Profile
::FETCH_CORPS
] = $cb;
1015 $subits[Profile
::FETCH_CORPS
] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
1018 if ($fields & Profile
::FETCH_EDU
) {
1019 $callbacks[Profile
::FETCH_EDU
] = $cb;
1020 $subits[Profile
::FETCH_EDU
] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
1023 if ($fields & Profile
::FETCH_JOBS
) {
1024 $callbacks[Profile
::FETCH_JOBS
] = $cb;
1025 $subits[Profile
::FETCH_JOBS
] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
1028 if ($fields & Profile
::FETCH_MEDALS
) {
1029 $callbacks[Profile
::FETCH_MEDALS
] = $cb;
1030 $subits[Profile
::FETCH_MEDALS
] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
1033 if ($fields & Profile
::FETCH_NETWORKING
) {
1034 $callbacks[Profile
::FETCH_NETWORKING
] = $cb;
1035 $subits[Profile
::FETCH_NETWORKING
] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
1038 if ($fields & Profile
::FETCH_PHONES
) {
1039 $callbacks[Profile
::FETCH_PHONES
] = $cb;
1040 $subits[Profile
::FETCH_PHONES
] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
1043 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1046 private function hasData($field, $vals)
1048 return ($this->fields
& $field) && ($vals[$field] != null
);
1051 private function fillProfile(array $vals)
1053 $pf = Profile
::get($vals[0]);
1054 $pf->setVisibilityLevel($this->visibility
->level());
1055 $pf->setFetchedFields($this->fields
);
1057 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1058 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1060 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1061 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1063 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1064 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1067 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1068 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1070 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1071 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1073 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1074 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1076 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1077 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1083 public function next()
1085 $vals = $this->iterator
->next();
1086 if ($vals == null
) {
1089 return $this->fillProfile($vals);
1092 public function first()
1094 return $this->iterator
->first();
1097 public function last()
1099 return $this->iterator
->last();
1102 public function total()
1104 return $this->iterator
->total();
1108 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: