951e6f29f163c529f895eefeeab96d8523f6cc63
[platal.git] / classes / profile.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 class Profile
23 {
24 static private $v_values = array('public' => array('public'),
25 'ax' => array('ax', 'public'),
26 'private' => array('private', 'ax', 'public'));
27
28 const VISIBILITY_PUBLIC = 'public';
29 const VISIBILITY_AX = 'ax';
30 const VISIBILITY_PRIVATE = 'private';
31
32 /* name tokens */
33 const LASTNAME = 'lastname';
34 const FIRSTNAME = 'firstname';
35 const NICKNAME = 'nickname';
36 const PSEUDONYM = 'pseudonym';
37 const NAME = 'name';
38 /* name variants */
39 const VN_MARITAL = 'marital';
40 const VN_ORDINARY = 'ordinary';
41 const VN_OTHER = 'other';
42 const VN_INI = 'ini';
43 /* display names */
44 const DN_FULL = 'directory_name';
45 const DN_DISPLAY = 'yourself';
46 const DN_YOURSELF = 'yourself';
47 const DN_DIRECTORY = 'directory_name';
48 const DN_PRIVATE = 'private_name';
49 const DN_PUBLIC = 'public_name';
50 const DN_SHORT = 'short_name';
51 const DN_SORT = 'sort_name';
52 /* education related names */
53 const EDU_X = 'École polytechnique';
54 const DEGREE_X = 'Ingénieur';
55 const DEGREE_M = 'Master';
56 const DEGREE_D = 'Doctorat';
57
58 static public $name_variants = array(
59 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
60 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
61 );
62
63 const ADDRESS_MAIN = 0x000001;
64 const ADDRESS_PERSO = 0x000002;
65 const ADDRESS_PRO = 0x000004;
66 const ADDRESS_ALL = 0x000006;
67 const ADDRESS_POSTAL = 0x000008;
68
69 const EDUCATION_MAIN = 0x000010;
70 const EDUCATION_EXTRA = 0x000020;
71 const EDUCATION_ALL = 0x000040;
72 const EDUCATION_FINISHED = 0x000080;
73 const EDUCATION_CURRENT = 0x000100;
74
75 const JOBS_MAIN = 0x001000;
76 const JOBS_ALL = 0x002000;
77 const JOBS_FINISHED = 0x004000;
78 const JOBS_CURRENT = 0x008000;
79
80 const NETWORKING_ALL = 0x000000;
81 const NETWORKING_WEB = 0x010000;
82 const NETWORKING_IM = 0x020000;
83 const NETWORKING_SOCIAL = 0x040000;
84
85 const FETCH_ADDRESSES = 0x000001;
86 const FETCH_CORPS = 0x000002;
87 const FETCH_EDU = 0x000004;
88 const FETCH_JOBS = 0x000008;
89 const FETCH_MEDALS = 0x000010;
90 const FETCH_NETWORKING = 0x000020;
91 const FETCH_PHONES = 0x000040;
92
93 const FETCH_MINIFICHES = 0x00004D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_PHONES
94
95 const FETCH_ALL = 0x0000FF; // OR of FETCH_*
96
97 private $pid;
98 private $hrpid;
99 private $data = array();
100
101 private $visibility = null;
102
103 private function __construct(array $data)
104 {
105 $this->data = $data;
106 $this->pid = $this->data['pid'];
107 $this->hrpid = $this->data['hrpid'];
108 if (!S::logged()) {
109 $this->setVisibilityLevel(self::VISIBILITY_PUBLIC);
110 } else {
111 $this->setVisibilityLevel(self::VISIBILITY_PRIVATE);
112 }
113 }
114
115 static private $contexts = array();
116
117 /** Returns the best visibility context toward $visibility
118 * @param $visibility A wished visibility level
119 * @return An array of compatible visibilities
120 *
121 * if $visibility is null, the best visibility is returned
122 */
123 static public function getVisibilityContext($visibility = null)
124 {
125 if (array_key_exists($visibility, self::$contexts)) {
126 return self::$contexts[$visibility];
127 }
128
129 $asked_vis = $visibility;
130
131 if (S::logged()) {
132 $minvis = self::VISIBILITY_PRIVATE;
133 } else {
134 $minvis = self::VISIBILITY_PUBLIC;
135 }
136 if ($visibility == null) {
137 $visibility = $minvis;
138 }
139
140 if ($minvis == self::VISIBILITY_PUBLIC) {
141 $visibility = self::VISIBILITY_PUBLIC;
142 }
143
144 $visibility = self::$v_values[$visibility];
145 self::$contexts[$asked_vis] = $visibility;
146
147 return $visibility;
148 }
149
150 public function id()
151 {
152 return $this->pid;
153 }
154
155 public function hrid()
156 {
157 return $this->hrpid;
158 }
159
160 public function promo()
161 {
162 return $this->promo;
163 }
164
165 public function yearpromo()
166 {
167 return intval(substr($this->promo, 1, 4));
168 }
169
170 /** Print a name with the given formatting:
171 * %s = • for women
172 * %f = firstname
173 * %l = lastname
174 * %F = fullname
175 * %S = shortname
176 * %p = promo
177 */
178 public function name($format)
179 {
180 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
181 array($this->isFemale() ? '•' : '',
182 $this->firstName(), $this->lastName(),
183 $this->fullName(), $this->shortName(),
184 $this->promo()), $format);
185 }
186
187 public function fullName($with_promo = false)
188 {
189 if ($with_promo) {
190 return $this->full_name . ' (' . $this->promo . ')';
191 }
192 return $this->full_name;
193 }
194
195 public function shortName($with_promo = false)
196 {
197 if ($with_promo) {
198 return $this->short_name . ' (' . $this->promo . ')';
199 }
200 return $this->short_name;
201 }
202
203 public function firstName()
204 {
205 return $this->firstname;
206 }
207
208 public function firstNames()
209 {
210 return $this->nameVariants(self::FIRSTNAME);
211 }
212
213 public function lastName()
214 {
215 return $this->lastname;
216 }
217
218 public function lastNames()
219 {
220 return $this->nameVariants(self::LASTNAME);
221 }
222
223 public function isFemale()
224 {
225 return $this->sex == PlUser::GENDER_FEMALE;
226 }
227
228 public function isDead()
229 {
230 return ($this->deathdate != null);
231 }
232
233 public function data()
234 {
235 $this->first_name;
236 return $this->data;
237 }
238
239 private function nameVariants($type)
240 {
241 $vals = array($this->$type);
242 foreach (self::$name_variants[$type] as $var) {
243 $vartype = $type . '_' . $var;
244 $varname = $this->$vartype;
245 if ($varname != null && $varname != "") {
246 $vals[] = $varname;
247 }
248 }
249 return array_unique($vals);
250 }
251
252 public function nationalities()
253 {
254 $nats = array();
255 if ($this->nationality1) {
256 $nats[] = $this->nationality1;
257 }
258 if ($this->nationality2) {
259 $nats[] = $this->nationality2;
260 }
261 if ($this->nationality3) {
262 $nats[] = $this->nationality3;
263 }
264 return $nats;
265 }
266
267 public function __get($name)
268 {
269 if (property_exists($this, $name)) {
270 return $this->$name;
271 }
272
273 if (isset($this->data[$name])) {
274 return $this->data[$name];
275 }
276
277 return null;
278 }
279
280 public function __isset($name)
281 {
282 return property_exists($this, $name) || isset($this->data[$name]);
283 }
284
285 /** Sets the level of visibility of the profile
286 * Sets $this->visibility to a list of valid visibilities.
287 * @param one of the self::VISIBILITY_* values
288 */
289 public function setVisibilityLevel($visibility)
290 {
291 if ($visibility != self::VISIBILITY_PRIVATE
292 && $visibility != self::VISIBILITY_AX
293 && $visibility != self::VISIBILITY_PUBLIC) {
294 Platal::page()->kill("Visibility invalide: " . $visibility);
295 }
296 $this->visibility = self::$v_values[$visibility];
297 if ($this->mobile && !in_array($this->mobile_pub, $this->visibility)) {
298 unset($this->data['mobile']);
299 }
300 }
301
302 /** Determine whether an item with visibility $visibility can be displayed
303 * with the current level of visibility of the profile
304 * @param $visibility The level of visibility to be checked
305 */
306 public function isVisible($visibility)
307 {
308 return in_array($visibility, $this->visibility);
309 }
310
311 public static function getCompatibleVisibilities($visibility)
312 {
313 return self::$v_values[$visibility];
314 }
315
316 private function getProfileField($cls)
317 {
318 return ProfileField::getForPID($cls, $this->id(), $this->visibility);
319 }
320
321 /** Consolidates internal data (addresses, phones, jobs)
322 */
323 private function consolidateFields()
324 {
325 if ($this->phones != null) {
326 if ($this->addresses != null) {
327 $this->addresses->addPhones($this->phones);
328 }
329
330 if ($this->jobs != null) {
331 $this->jobs->addPhones($this->phones);
332 }
333 }
334
335 if ($this->addresses != null && $this->jobs != null) {
336 $this->jobs->addAddresses($this->addresses);
337 }
338 }
339
340 /* Photo
341 */
342 private $_photo = null;
343 public function getPhoto($fallback = true, $data = false)
344 {
345 if ($data && $this->has_photo && $this->isVisible($this->photo_pub) &&
346 ($this->_photo == null || $this->_photo->mimeType() == null)) {
347 $res = XDB::fetchOneAssoc('SELECT attach, attachmime, x, y
348 FROM profile_photos
349 WHERE pid = {?}', $this->pid);
350 $this->_photo = PlImage::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
351 }
352
353 if (!$data && $this->_photo == null) {
354 $this->_photo = PlImage::fromData(null, null, $this->photo_width, $this->photo_height);
355 }
356
357 if ($this->has_photo && $this->isVisible($this->photo_pub)) {
358 return $this->_photo;
359 } else if ($fallback) {
360 return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/none.png',
361 'image/png');
362 }
363 return null;
364 }
365
366 /* Addresses
367 */
368 private $addresses = null;
369 public function setAddresses(ProfileAddresses $addr)
370 {
371 $this->addresses = $addr;
372 $this->consolidateFields();
373 }
374
375 public function getAddresses($flags, $limit = null)
376 {
377 if ($this->addresses == null) {
378 $this->setAddresses($this->getProfileField('ProfileAddresses'));
379 }
380
381 if ($this->addresses == null) {
382 return array();
383 }
384 return $this->addresses->get($flags, $limit);
385 }
386
387 public function iterAddresses($flags, $limit = null)
388 {
389 return PlIteratorUtils::fromArray($this->getAddresses($flags, $limit), 1, true);
390 }
391
392 public function getMainAddress()
393 {
394 $addr = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN);
395 if (count($addr) == 0) {
396 return null;
397 } else {
398 return array_pop($addr);
399 }
400 }
401
402 /* Phones
403 */
404 private $phones = null;
405 public function setPhones(ProfilePhones $phones)
406 {
407 $this->phones = $phones;
408 $this->consolidateFields();
409 }
410
411 public function getPhones($flags, $limit = null)
412 {
413 if ($this->phones == null) {
414 $this->setPhones($this->getProfileField('ProfilePhones'));
415 }
416
417 if ($this->phones == null) {
418 return array();
419 }
420 return $this->phones->get($flags, $limit);
421 }
422
423 /* Educations
424 */
425 private $educations = null;
426 public function setEducations(ProfileEducation $edu)
427 {
428 $this->educations = $edu;
429 }
430
431 public function getEducations($flags, $limit = null)
432 {
433 if ($this->educations == null) {
434 $this->setEducations($this->getProfileField('ProfileEducation'));
435 }
436
437 if ($this->educations == null) {
438 return array();
439 }
440 return $this->educations->get($flags, $limit);
441 }
442
443 public function getExtraEducations($limit = null)
444 {
445 return $this->getEducations(self::EDUCATION_EXTRA, $limit);
446 }
447
448 /* Corps
449 */
450 private $corps = null;
451 public function setCorps(ProfileCorps $corps)
452 {
453 $this->corps = $corps;
454 }
455
456 public function getCorps()
457 {
458 if ($this->corps == null) {
459 $this->setCorps($this->getProfileField('ProfileCorps'));
460 }
461 return $this->corps;
462 }
463
464 /** Networking
465 */
466 private $networks = null;
467 public function setNetworking(ProfileNetworking $nw)
468 {
469 $this->networks = $nw;
470 }
471
472 public function getNetworking($flags, $limit = null)
473 {
474 if ($this->networks == null) {
475 $this->setNetworking($this->getProfileField('ProfileNetworking'));
476 }
477 if ($this->networks == null) {
478 return array();
479 }
480 return $this->networks->get($flags, $limit);
481 }
482
483 public function getWebSite()
484 {
485 $site = $this->getNetworking(self::NETWORKING_WEB, 1);
486 if (count($site) != 1) {
487 return null;
488 }
489 $site = array_pop($site);
490 return $site['address'];
491 }
492
493
494 /** Jobs
495 */
496 private $jobs = null;
497 public function setJobs(ProfileJobs $jobs)
498 {
499 $this->jobs = $jobs;
500 $this->consolidateFields();
501 }
502
503 public function getJobs($flags, $limit = null)
504 {
505 if ($this->jobs == null) {
506 $this->setJobs($this->getProfileField('ProfileJobs'));
507 }
508
509 if ($this->jobs == null) {
510 return array();
511 }
512 return $this->jobs->get($flags, $limit);
513 }
514
515 public function getMainJob()
516 {
517 $job = $this->getJobs(self::JOBS_MAIN, 1);
518 if (count($job) != 1) {
519 return null;
520 }
521 return array_pop($job);
522 }
523
524 /* Binets
525 */
526 public function getBinets()
527 {
528 return XDB::fetchColumn('SELECT binet_id
529 FROM profile_binets
530 WHERE pid = {?}', $this->id());
531 }
532 public function getBinetsNames()
533 {
534 return XDB::fetchColumn('SELECT text
535 FROM profile_binets AS pb
536 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
537 WHERE pb.pid = {?}', $this->id());
538 }
539
540 /* Medals
541 */
542 private $medals = null;
543 public function setMedals(ProfileMedals $medals)
544 {
545 $this->medals = $medals;
546 }
547
548 public function getMedals()
549 {
550 if ($this->medals == null) {
551 $this->setMedals($this->getProfileField('ProfileMedals'));
552 }
553 if ($this->medals == null) {
554 return array();
555 }
556 return $this->medals->medals;
557 }
558
559 public function owner()
560 {
561 return User::getSilent($this);
562 }
563
564 public function compareNames($firstname, $lastname)
565 {
566 $_lastname = mb_strtoupper($this->lastName());
567 $_firstname = mb_strtoupper($this->firstName());
568 $lastname = mb_strtoupper($lastname);
569 $firstname = mb_strtoupper($firstname);
570
571 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
572 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY);
573 $maxlen = 0;
574
575 foreach ($tokens as $str) {
576 $isOk &= (strpos($_lastname, $str) !== false);
577 $maxlen = max($maxlen, strlen($str));
578 }
579
580 return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname)));
581 }
582
583 private static function fetchProfileData(array $pids, $respect_order = true, $fields = 0x0000, $visibility = null)
584 {
585 if (count($pids) == 0) {
586 return array();
587 }
588
589 if ($respect_order) {
590 $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids);
591 } else {
592 $order = '';
593 }
594
595
596 $it = XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, pse.text AS section,
597 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
598 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
599 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
600 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
601 pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub,
602 ph.attach IS NOT NULL AS has_photo, ph.pub AS photo_pub,
603 ph.x AS photo_width, ph.y AS photo_height,
604 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
605 pm.expertise AS mentor_expertise,
606 ap.uid AS owner_id
607 FROM profiles AS p
608 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
609 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
610 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
611 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
612 AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ')
613 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
614 AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ')
615 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
616 AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ')
617 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
618 AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ')
619 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
620 AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ')
621 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
622 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
623 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
624 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
625 WHERE p.pid IN ' . XDB::formatArray($pids) . '
626 GROUP BY p.pid
627 ' . $order);
628 return new ProfileIterator($it, $pids, $fields, $visibility);
629 }
630
631 public static function getPID($login)
632 {
633 if ($login instanceof PlUser) {
634 return XDB::fetchOneCell('SELECT pid
635 FROM account_profiles
636 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
637 $login->id());
638 } else if (ctype_digit($login)) {
639 return XDB::fetchOneCell('SELECT pid
640 FROM profiles
641 WHERE pid = {?}', $login);
642 } else {
643 return XDB::fetchOneCell('SELECT pid
644 FROM profiles
645 WHERE hrpid = {?}', $login);
646 }
647 }
648
649 public static function getPIDsFromUIDs($uids, $respect_order = true)
650 {
651 if ($respect_order) {
652 $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids);
653 } else {
654 $order = '';
655 }
656 return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
657 FROM account_profiles AS ap
658 WHERE FIND_IN_SET(\'owner\', ap.perms)
659 AND ap.uid IN ' . XDB::formatArray($uids) .'
660 ' . $order);
661 }
662
663 /** Return the profile associated with the given login.
664 */
665 public static function get($login, $fields = 0x0000, $visibility = null)
666 {
667 if (is_array($login)) {
668 return new Profile($login);
669 }
670 $pid = self::getPID($login);
671 if (!is_null($pid)) {
672 $it = self::iterOverPIDs(array($pid), false, $fields, $visibility);
673 return $it->next();
674 } else {
675 /* Let say we can identify a profile using the identifiers of its owner.
676 */
677 if (!($login instanceof PlUser)) {
678 $user = User::getSilent($login);
679 if ($user && $user->hasProfile()) {
680 return $user->profile();
681 }
682 }
683 return null;
684 }
685 }
686
687 public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null)
688 {
689 return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
690 }
691
692 public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null)
693 {
694 return self::fetchProfileData($pids, $respect_order, $fields, $visibility);
695 }
696
697 /** Return profiles for the list of pids.
698 */
699 public static function getBulkProfilesWithPIDs(array $pids, $fields = self::FETCH_ADDRESSES, $visibility = null)
700 {
701 if (count($pids) == 0) {
702 return array();
703 }
704 $it = self::iterOverPIDs($pids, true, $fields, $visibility);
705 $profiles = array();
706 while ($p = $it->next()) {
707 $profiles[$p->id()] = $p;
708 }
709 return $profiles;
710 }
711
712 /** Return profiles for uids.
713 */
714 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null)
715 {
716 if (count($uids) == 0) {
717 return array();
718 }
719 return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility);
720 }
721
722 public static function isDisplayName($name)
723 {
724 return $name == self::DN_FULL || $name == self::DN_DISPLAY
725 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
726 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
727 || $name == self::DN_SHORT || $name == self::DN_SORT;
728 }
729
730 public static function getNameTypeId($type, $for_sql = false)
731 {
732 if (!S::has('name_types')) {
733 $table = XDB::fetchAllAssoc('type', 'SELECT id, type
734 FROM profile_name_enum');
735 S::set('name_types', $table);
736 } else {
737 $table = S::v('name_types');
738 }
739 if ($for_sql) {
740 return XDB::escape($table[$type]);
741 } else {
742 return $table[$type];
743 }
744 }
745
746 public static function rebuildSearchTokens($pid)
747 {
748 XDB::execute('DELETE FROM search_name
749 WHERE pid = {?}',
750 $pid);
751 $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
752 FIND_IN_SET('public', e.flags) AS public
753 FROM profile_name AS n
754 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
755 WHERE n.pid = {?}",
756 $pid);
757
758 foreach ($keys as $i => $key) {
759 if ($key['name'] == '') {
760 continue;
761 }
762 $toks = preg_split('/[ \'\-]+/', $key['name']);
763 $token = '';
764 $first = 5;
765 while ($toks) {
766 $token = strtolower(replace_accent(array_pop($toks) . $token));
767 $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10);
768 XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
769 VALUES ({?}, {?}, {?}, {?}, {?})',
770 $token, $pid, soundex_fr($token), $score, $key['public']);
771 $first = 0;
772 }
773 }
774 }
775
776 /** The school identifier consists of 6 digits. The first 3 represent the
777 * promotion entry year. The last 3 indicate the student's rank.
778 *
779 * Our identifier consists of 8 digits and both half have the same role.
780 * This enables us to deal with bigger promotions and with a wider range
781 * of promotions.
782 *
783 * getSchoolId returns a school identifier given one of ours.
784 * getXorgId returns a X.org identifier given a school identifier.
785 */
786 public static function getSchoolId($xorgId)
787 {
788 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
789 return null;
790 }
791
792 $year = intval(substr($xorgId, 0, 4));
793 $rank = intval(substr($xorgId, 5, 3));
794 if ($year < 1996) {
795 return null;
796 } elseif ($year < 2000) {
797 $year = intval(substr(1900 - $year, 1, 3));
798 return sprintf('%02u0%03u', $year, $rank);
799 } else {
800 $year = intval(substr(1900 - $year, 1, 3));
801 return sprintf('%03u%03u', $year, $rank);
802 }
803 }
804
805 public static function getXorgId($schoolId)
806 {
807 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
808 return null;
809 }
810
811 $year = intval(substr($schoolId, 0, 3));
812 $rank = intval(substr($schoolId, 3, 3));
813
814 if ($year > 200) {
815 $year /= 10;
816 }
817 if ($year < 96) {
818 return null;
819 } else {
820 return sprintf('%04u%04u', 1900 + $year, $rank);
821 }
822 }
823 }
824
825
826 /** Iterator over a set of Profiles
827 */
828 class ProfileIterator implements PlIterator
829 {
830 private $iterator = null;
831 private $fields;
832
833 public function __construct(PlIterator $it, array $pids, $fields = 0x0000, $visibility = null)
834 {
835 require_once 'profilefields.inc.php';
836 $visibility = Profile::getVisibilityContext($visibility);
837 $this->fields = $fields;
838
839 $subits = array();
840 $callbacks = array();
841
842 $subits[0] = $it;
843 $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid');
844 $cb = PlIteratorUtils::objectPropertyCallback('pid');
845
846 if ($fields & Profile::FETCH_ADDRESSES) {
847 $callbacks[Profile::FETCH_ADDRESSES] = $cb;
848 $subits[Profile::FETCH_ADDRESSES] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
849 }
850
851 if ($fields & Profile::FETCH_CORPS) {
852 $callbacks[Profile::FETCH_CORPS] = $cb;
853 $subits[Profile::FETCH_CORPS] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
854 }
855
856 if ($fields & Profile::FETCH_EDU) {
857 $callbacks[Profile::FETCH_EDU] = $cb;
858 $subits[Profile::FETCH_EDU] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
859 }
860
861 if ($fields & Profile::FETCH_JOBS) {
862 $callbacks[Profile::FETCH_JOBS] = $cb;
863 $subits[Profile::FETCH_JOBS] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
864 }
865
866 if ($fields & Profile::FETCH_MEDALS) {
867 $callbacks[Profile::FETCH_MEDALS] = $cb;
868 $subits[Profile::FETCH_MEDALS] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
869 }
870
871 if ($fields & Profile::FETCH_NETWORKING) {
872 $callbacks[Profile::FETCH_NETWORKING] = $cb;
873 $subits[Profile::FETCH_NETWORKING] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
874 }
875
876 if ($fields & Profile::FETCH_PHONES) {
877 $callbacks[Profile::FETCH_PHONES] = $cb;
878 $subits[Profile::FETCH_PHONES] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
879 }
880
881 $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0);
882 }
883
884 private function hasData($flag, $vals)
885 {
886 return ($this->fields & $flag) && ($vals[$flag] != null);
887 }
888
889 private function fillProfile(array $vals)
890 {
891 $pf = Profile::get($vals[0]);
892 if ($this->hasData(Profile::FETCH_PHONES, $vals)) {
893 $pf->setPhones($vals[Profile::FETCH_PHONES]);
894 }
895 if ($this->hasData(Profile::FETCH_ADDRESSES, $vals)) {
896 $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]);
897 }
898 if ($this->hasData(Profile::FETCH_JOBS, $vals)) {
899 $pf->setJobs($vals[Profile::FETCH_JOBS]);
900 }
901
902 if ($this->hasData(Profile::FETCH_CORPS, $vals)) {
903 $pf->setCorps($vals[Profile::FETCH_CORPS]);
904 }
905 if ($this->hasData(Profile::FETCH_EDU, $vals)) {
906 $pf->setEducations($vals[Profile::FETCH_EDU]);
907 }
908 if ($this->hasData(Profile::FETCH_MEDALS, $vals)) {
909 $pf->setMedals($vals[Profile::FETCH_MEDALS]);
910 }
911 if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) {
912 $pf->setNetworking($vals[Profile::FETCH_NETWORKING]);
913 }
914
915 return $pf;
916 }
917
918 public function next()
919 {
920 $vals = $this->iterator->next();
921 if ($vals == null) {
922 return null;
923 }
924 return $this->fillProfile($vals);
925 }
926
927 public function first()
928 {
929 return $this->iterator->first();
930 }
931
932 public function last()
933 {
934 return $this->iterator->last();
935 }
936
937 public function total()
938 {
939 return $this->iterator->total();
940 }
941 }
942
943 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
944 ?>