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 mainEducationDuration()
209 switch ($this->mainEducation()) {
221 /** Print a name with the given formatting:
229 public function name($format)
231 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
232 array($this->isFemale() ?
'•' : '',
233 $this->firstName(), $this->lastName(),
234 $this->fullName(), $this->shortName(),
235 $this->promo()), $format);
238 public function fullName($with_promo = false
)
241 return $this->full_name
. ' (' . $this->promo
. ')';
243 return $this->full_name
;
246 public function shortName($with_promo = false
)
249 return $this->short_name
. ' (' . $this->promo
. ')';
251 return $this->short_name
;
254 public function firstName()
256 return $this->firstname
;
259 public function firstNames()
261 return $this->nameVariants(self
::FIRSTNAME
);
264 public function lastName()
266 return $this->lastname
;
269 public function lastNames()
271 return $this->nameVariants(self
::LASTNAME
);
274 public function isFemale()
276 return $this->sex
== PlUser
::GENDER_FEMALE
;
279 public function isDead()
281 return ($this->deathdate
!= null
);
284 public function displayEmail()
288 return $o->bestEmail();
290 return $this->email_directory
;
294 public function data()
300 private function nameVariants($type)
302 $vals = array($this->$type);
303 foreach (self
::$name_variants[$type] as $var) {
304 $vartype = $type . '_' . $var;
305 $varname = $this->$vartype;
306 if ($varname != null
&& $varname != "") {
310 return array_unique($vals);
313 public function nationalities()
316 if ($this->nationality1
) {
317 $nats[] = $this->nationality1
;
319 if ($this->nationality2
) {
320 $nats[] = $this->nationality2
;
322 if ($this->nationality3
) {
323 $nats[] = $this->nationality3
;
328 public function __get($name)
330 if (property_exists($this, $name)) {
334 if (isset($this->data
[$name])) {
335 return $this->data
[$name];
341 public function __isset($name)
343 return property_exists($this, $name) ||
isset($this->data
[$name]);
346 /** Sets the level of visibility of the profile
347 * Sets $this->visibility to a list of valid visibilities.
348 * @param one of the self::VIS_* values
350 public function setVisibilityLevel($visibility)
352 $this->visibility
->setLevel($visibility);
355 /** Determine whether an item with visibility $visibility can be displayed
356 * with the current level of visibility of the profile
357 * @param $visibility The level of visibility to be checked
359 public function isVisible($visibility)
361 return $this->visibility
->isVisible($visibility);
364 /** Stores the list of fields which have already been fetched for this Profile
366 public function setFetchedFields($fields)
368 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
369 Platal
::page()->kill("Invalid fetched fields: $fields");
372 $this->fetched_fields
= $fields;
375 private function fetched($field)
377 if (!array_key_exists($field, ProfileField
::$fields)) {
378 Platal
::page()->kill("Invalid field: $field");
381 return ($this->fetched_fields
& $field);
384 /** If not already done, fetches data for the given field
385 * @param $field One of the Profile::FETCH_*
386 * @return A ProfileField, or null
388 private function getProfileField($field)
390 if ($this->fetched($field)) {
393 $this->fetched_fields
= $this->fetched_fields |
$field;
396 $cls = ProfileField
::$fields[$field];
398 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
401 /** Consolidates internal data (addresses, phones, jobs)
403 private function consolidateFields()
405 if ($this->phones
!= null
) {
406 if ($this->addresses
!= null
) {
407 $this->addresses
->addPhones($this->phones
);
410 if ($this->jobs
!= null
) {
411 $this->jobs
->addPhones($this->phones
);
415 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
416 $this->jobs
->addAddresses($this->addresses
);
422 private $photo = null
;
423 public function getPhoto($fallback = true
, $data = false
)
425 if ($this->has_photo
) {
426 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
427 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y
429 WHERE pid = {?}', $this->pid
);
430 $this->photo
= PlImage
::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
431 } else if ($this->photo
== null
) {
432 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
435 } else if ($fallback) {
436 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
444 private $addresses = null
;
445 public function setAddresses(ProfileAddresses
$addr)
447 $this->addresses
= $addr;
448 $this->consolidateFields();
451 public function getAddresses($flags, $limit = null
)
453 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
454 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
456 $this->setAddresses($addr);
460 if ($this->addresses
== null
) {
463 return $this->addresses
->get($flags, $limit);
466 public function iterAddresses($flags, $limit = null
)
468 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
471 public function getMainAddress()
473 $addr = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
474 if (count($addr) == 0) {
477 return array_pop($addr);
483 private $phones = null
;
484 public function setPhones(ProfilePhones
$phones)
486 $this->phones
= $phones;
487 $this->consolidateFields();
490 public function getPhones($flags, $limit = null
)
492 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
493 $this->setPhones($this->getProfileField(self
::FETCH_PHONES
));
496 if ($this->phones
== null
) {
499 return $this->phones
->get($flags, $limit);
504 private $educations = null
;
505 public function setEducations(ProfileEducation
$edu)
507 $this->educations
= $edu;
510 public function getEducations($flags, $limit = null
)
512 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
513 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
516 if ($this->educations
== null
) {
519 return $this->educations
->get($flags, $limit);
522 public function getExtraEducations($limit = null
)
524 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
529 private $corps = null
;
530 public function setCorps(ProfileCorps
$corps)
532 $this->corps
= $corps;
535 public function getCorps()
537 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
538 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
545 private $networks = null
;
546 public function setNetworking(ProfileNetworking
$nw)
548 $this->networks
= $nw;
551 public function getNetworking($flags, $limit = null
)
553 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
554 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
556 $this->setNetworking($nw);
559 if ($this->networks
== null
) {
562 return $this->networks
->get($flags, $limit);
565 public function getWebSite()
567 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
568 if (count($site) != 1) {
571 $site = array_pop($site);
578 private $jobs = null
;
579 public function setJobs(ProfileJobs
$jobs)
582 $this->consolidateFields();
585 public function getJobs($flags, $limit = null
)
587 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
588 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
590 $this->setJobs($jobs);
594 if ($this->jobs
== null
) {
597 return $this->jobs
->get($flags, $limit);
600 public function getMainJob()
602 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
603 if (count($job) != 1) {
606 return array_pop($job);
611 private $mentor_sectors = null
;
612 public function setMentoringSectors(ProfileMentoringSectors
$sectors)
614 $this->mentor_sectors
= $sectors;
617 public function getMentoringSectors()
619 if ($this->mentor_sectors
== null
&& !$this->fetched(self
::FETCH_MENTOR_SECTOR
)) {
620 $this->setMentoringSectors($this->getProfileField(self
::FETCH_MENTOR_SECTOR
));
623 if ($this->mentor_sectors
== null
) {
626 return $this->mentor_sectors
->sectors
;
630 private $mentor_countries = null
;
631 public function setMentoringCountries(ProfileMentoringCountries
$countries)
633 $this->mentor_countries
= $countries;
636 public function getMentoringCountries()
638 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
639 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
642 if ($this->mentor_countries
== null
) {
645 return $this->mentor_countries
->countries
;
651 public function getBinets()
653 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
654 return XDB
::fetchColumn('SELECT binet_id
656 WHERE pid = {?}', $this->id());
661 public function getBinetsNames()
663 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
664 return XDB
::fetchColumn('SELECT text
665 FROM profile_binets AS pb
666 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
667 WHERE pb.pid = {?}', $this->id());
675 private $medals = null
;
676 public function setMedals(ProfileMedals
$medals)
678 $this->medals
= $medals;
681 public function getMedals()
683 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
684 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
686 if ($this->medals
== null
) {
689 return $this->medals
->medals
;
692 public function compareNames($firstname, $lastname)
694 $_lastname = mb_strtoupper($this->lastName());
695 $_firstname = mb_strtoupper($this->firstName());
696 $lastname = mb_strtoupper($lastname);
697 $firstname = mb_strtoupper($firstname);
699 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
700 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
703 foreach ($tokens as $str) {
704 $isOk &= (strpos($_lastname, $str) !== false
);
705 $maxlen = max($maxlen, strlen($str));
708 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
711 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
713 if (count($pids) == 0) {
717 if ($respect_order) {
718 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
723 $visibility = new ProfileVisibility($visibility);
725 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
726 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
727 p.cv, p.medals_pub, p.alias_pub, p.email_directory, p.last_change,
728 p.nationality1, p.nationality2, p.nationality3,
729 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
730 pe.entry_year, pe.grad_year,
731 IF ({?} IN {?}, pse.text, NULL) AS section,
732 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
733 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
734 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
735 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
736 pd.directory_name, IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
737 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
738 ph.x AS photo_width, ph.y AS photo_height,
739 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
740 pm.expertise AS mentor_expertise,
743 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
744 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
745 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
746 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
747 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
748 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
749 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
750 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
751 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
752 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
753 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
754 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
755 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
756 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
757 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
758 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
759 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
763 $visibility->levels(),
764 ProfileVisibility
::VIS_PRIVATE
, $visibility->levels(),
765 $visibility->levels(), $visibility->levels(),
768 return new ProfileIterator($it, $pids, $fields, $visibility);
771 public static function getPID($login)
773 if ($login instanceof PlUser
) {
774 return XDB
::fetchOneCell('SELECT pid
775 FROM account_profiles
776 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
778 } else if (ctype_digit($login)) {
779 return XDB
::fetchOneCell('SELECT pid
781 WHERE pid = {?}', $login);
783 return XDB
::fetchOneCell('SELECT pid
785 WHERE hrpid = {?}', $login);
789 public static function getPIDsFromUIDs($uids, $respect_order = true
)
791 if ($respect_order) {
792 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
796 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
797 FROM account_profiles AS ap
798 WHERE FIND_IN_SET(\'owner\', ap.perms)
799 AND ap.uid IN ' . XDB
::formatArray($uids) .'
803 /** Return the profile associated with the given login.
805 public static function get($login, $fields = 0x0000, $visibility = null
)
807 if (is_array($login)) {
808 $pf = new Profile($login);
809 $pf->setVisibilityLevel($visibility);
812 $pid = self
::getPID($login);
813 if (!is_null($pid)) {
814 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
817 /* Let say we can identify a profile using the identifiers of its owner.
819 if (!($login instanceof PlUser
)) {
820 $user = User
::getSilent($login);
821 if ($user && $user->hasProfile()) {
822 return $user->profile();
829 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
831 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
834 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
836 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
839 /** Return profiles for the list of pids.
841 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
843 if (count($pids) == 0) {
846 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
848 while ($p = $it->next()) {
849 $profiles[$p->id()] = $p;
854 /** Return profiles for uids.
856 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
858 if (count($uids) == 0) {
861 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
864 public static function isDisplayName($name)
866 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
867 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
868 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
869 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
872 public static function getNameTypeId($type, $for_sql = false
)
874 if (!S
::has('name_types')) {
875 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
876 FROM profile_name_enum');
877 S
::set('name_types', $table);
879 $table = S
::v('name_types');
882 return XDB
::escape($table[$type]);
884 return $table[$type];
888 public static function rebuildSearchTokens($pid)
890 XDB
::execute('DELETE FROM search_name
893 $keys = XDB
::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
894 FIND_IN_SET('public', e.flags) AS public
895 FROM profile_name AS n
896 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
900 foreach ($keys as $i => $key) {
901 if ($key['name'] == '') {
904 $toks = preg_split('/[ \'\-]+/', $key['name']);
908 $token = strtolower(replace_accent(array_pop($toks) . $token));
909 $score = ($toks ?
0 : 10 +
$first) * ($key['score'] / 10);
910 XDB
::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
911 VALUES ({?}, {?}, {?}, {?}, {?})',
912 $token, $pid, soundex_fr($token), $score, $key['public']);
918 /** The school identifier consists of 6 digits. The first 3 represent the
919 * promotion entry year. The last 3 indicate the student's rank.
921 * Our identifier consists of 8 digits and both half have the same role.
922 * This enables us to deal with bigger promotions and with a wider range
925 * getSchoolId returns a school identifier given one of ours.
926 * getXorgId returns a X.org identifier given a school identifier.
928 public static function getSchoolId($xorgId)
930 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
934 $year = intval(substr($xorgId, 0, 4));
935 $rank = intval(substr($xorgId, 5, 3));
938 } elseif ($year < 2000) {
939 $year = intval(substr(1900 - $year, 1, 3));
940 return sprintf('%02u0%03u', $year, $rank);
942 $year = intval(substr(1900 - $year, 1, 3));
943 return sprintf('%03u%03u', $year, $rank);
947 public static function getXorgId($schoolId)
949 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
953 $year = intval(substr($schoolId, 0, 3));
954 $rank = intval(substr($schoolId, 3, 3));
962 return sprintf('%04u%04u', 1900 +
$year, $rank);
968 /** Iterator over a set of Profiles
970 class ProfileIterator
implements PlIterator
972 private $iterator = null
;
976 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
978 require_once 'profilefields.inc.php';
980 if ($visibility == null
) {
981 $visibility = new ProfileVisibility();
984 $this->fields
= $fields;
985 $this->visibility
= $visibility;
988 $callbacks = array();
991 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
992 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
994 if ($fields & Profile
::FETCH_ADDRESSES
) {
995 $callbacks[Profile
::FETCH_ADDRESSES
] = $cb;
996 $subits[Profile
::FETCH_ADDRESSES
] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
999 if ($fields & Profile
::FETCH_CORPS
) {
1000 $callbacks[Profile
::FETCH_CORPS
] = $cb;
1001 $subits[Profile
::FETCH_CORPS
] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
1004 if ($fields & Profile
::FETCH_EDU
) {
1005 $callbacks[Profile
::FETCH_EDU
] = $cb;
1006 $subits[Profile
::FETCH_EDU
] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
1009 if ($fields & Profile
::FETCH_JOBS
) {
1010 $callbacks[Profile
::FETCH_JOBS
] = $cb;
1011 $subits[Profile
::FETCH_JOBS
] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
1014 if ($fields & Profile
::FETCH_MEDALS
) {
1015 $callbacks[Profile
::FETCH_MEDALS
] = $cb;
1016 $subits[Profile
::FETCH_MEDALS
] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
1019 if ($fields & Profile
::FETCH_NETWORKING
) {
1020 $callbacks[Profile
::FETCH_NETWORKING
] = $cb;
1021 $subits[Profile
::FETCH_NETWORKING
] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
1024 if ($fields & Profile
::FETCH_PHONES
) {
1025 $callbacks[Profile
::FETCH_PHONES
] = $cb;
1026 $subits[Profile
::FETCH_PHONES
] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
1029 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1032 private function hasData($field, $vals)
1034 return ($this->fields
& $field) && ($vals[$field] != null
);
1037 private function fillProfile(array $vals)
1039 $pf = Profile
::get($vals[0]);
1040 $pf->setVisibilityLevel($this->visibility
->level());
1041 $pf->setFetchedFields($this->fields
);
1043 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1044 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1046 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1047 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1049 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1050 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1053 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1054 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1056 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1057 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1059 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1060 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1062 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1063 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1069 public function next()
1071 $vals = $this->iterator
->next();
1072 if ($vals == null
) {
1075 return $this->fillProfile($vals);
1078 public function first()
1080 return $this->iterator
->first();
1083 public function last()
1085 return $this->iterator
->last();
1088 public function total()
1090 return $this->iterator
->total();
1094 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: