Moves skills edition to mentor edition page.
[platal.git] / classes / profile.php
index 988c1c6..6574d0a 100644 (file)
@@ -110,10 +110,11 @@ class Profile implements PlExportable
     const FETCH_JOB_TERMS      = 0x000200;
     const FETCH_MENTOR_TERMS   = 0x000400;
     const FETCH_DELTATEN       = 0x000800;
+    const FETCH_PARTNER        = 0x001000;
 
     const FETCH_MINIFICHES   = 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
 
-    const FETCH_ALL          = 0x000FFF; // OR of FETCH_*
+    const FETCH_ALL          = 0x001FFF; // OR of FETCH_*
 
     static public $descriptions = array(
         'search_names'    => 'Noms',
@@ -160,12 +161,12 @@ class Profile implements PlExportable
     private $visibility = null;
 
 
-    private function __construct(array $data)
+    private function __construct(array $data, Visibility $visibility)
     {
         $this->data = $data;
         $this->pid = $this->data['pid'];
         $this->hrpid = $this->data['hrpid'];
-        $this->visibility = new ProfileVisibility();
+        $this->visibility = $visibility;
     }
 
     public function id()
@@ -273,6 +274,20 @@ class Profile implements PlExportable
         }
     }
 
+    public static function educationDuration($education)
+    {
+        switch ($education) {
+          case self::DEGREE_X:
+            return 3;
+          case self::DEGREE_M:
+            return 2;
+          case self::DEGREE_D:
+            return 3;
+          default:
+            return 0;
+        }
+    }
+
     /** Number of years between the promotion year until the
      * graduation year. In standard schools it's 0, but for
      * Polytechnique the promo year is the entry year.
@@ -285,6 +300,25 @@ class Profile implements PlExportable
         return 0;
     }
 
+    // Returns the profile's color.
+    public function promoColor()
+    {
+        switch ($this->mainEducation()) {
+          case 'X':
+            if (($this->yearpromo() % 2) === 0) {
+                return 'red';
+            } else {
+                return 'yellow';
+            }
+          case 'M':
+            return 'green';
+          case 'D':
+            return 'blue';
+          default:
+            return 'gray';
+        }
+    }
+
     /** Print a name with the given formatting:
      * %s = • for women
      * %f = firstname
@@ -351,7 +385,7 @@ class Profile implements PlExportable
     public function displayEmail()
     {
         $o = $this->owner();
-        if ($o != null) {
+        if ($o != null && $this->isVisible(Visibility::EXPORT_PRIVATE)) {
             return $o->bestEmail();
         } else {
             return $this->email_directory;
@@ -380,15 +414,15 @@ class Profile implements PlExportable
     public function nationalities()
     {
         $nats = array();
-        $countries = DirEnum::getOptions(DirEnum::COUNTRIES);
+        $nationalities = DirEnum::getOptions(DirEnum::NATIONALITIES);
         if ($this->nationality1) {
-            $nats[$this->nationality1] = $countries[$this->nationality1];
+            $nats[$this->nationality1] = $nationalities[$this->nationality1];
         }
         if ($this->nationality2) {
-            $nats[$this->nationality2] = $countries[$this->nationality2];
+            $nats[$this->nationality2] = $nationalities[$this->nationality2];
         }
         if ($this->nationality3) {
-            $nats[$this->nationality3] = $countries[$this->nationality3];
+            $nats[$this->nationality3] = $nationalities[$this->nationality3];
         }
         return $nats;
     }
@@ -425,7 +459,8 @@ class Profile implements PlExportable
      * Clears a profile.
      *  *always deletes in: profile_addresses, profile_binets, profile_deltaten,
      *      profile_job, profile_langskills, profile_mentor, profile_networking,
-     *      profile_phones, profile_skills, watch_profile
+     *      profile_partnersharing_settings, profile_phones, profile_skills,
+     *      watch_profile
      *  *always keeps in: profile_corps, profile_display, profile_education,
      *      profile_medals, profile_*_names, profile_photos, search_name
      *  *modifies: profiles
@@ -436,7 +471,7 @@ class Profile implements PlExportable
             'profile_job', 'profile_langskills', 'profile_mentor',
             'profile_networking', 'profile_skills', 'watch_profile',
             'profile_phones', 'profile_addresses', 'profile_binets',
-            'profile_deltaten');
+            'profile_deltaten', 'profile_partnersharing_settings');
 
         foreach ($tables as $t) {
             XDB::execute('DELETE FROM  ' . $t . '
@@ -452,15 +487,6 @@ class Profile implements PlExportable
                      $this->id());
     }
 
-    /** Sets the level of visibility of the profile
-     * Sets $this->visibility to a list of valid visibilities.
-     * @param one of the self::VIS_* values
-     */
-    public function setVisibilityLevel($visibility)
-    {
-        $this->visibility->setLevel($visibility);
-    }
-
     /** Determine whether an item with visibility $visibility can be displayed
      * with the current level of visibility of the profile
      * @param $visibility The level of visibility to be checked
@@ -850,7 +876,7 @@ class Profile implements PlExportable
      */
     public function getBinets()
     {
-        if ($this->visibility->isVisible(ProfileVisibility::VIS_PRIVATE)) {
+        if ($this->visibility->isVisible(Visibility::EXPORT_PRIVATE)) {
             return XDB::fetchColumn('SELECT  binet_id
                                        FROM  profile_binets
                                       WHERE  pid = {?}', $this->id());
@@ -860,7 +886,7 @@ class Profile implements PlExportable
     }
     public function getBinetsNames()
     {
-        if ($this->visibility->isVisible(ProfileVisibility::VIS_PRIVATE)) {
+        if ($this->visibility->isVisible(Visibility::EXPORT_PRIVATE)) {
             return XDB::fetchColumn('SELECT  text
                                        FROM  profile_binets AS pb
                                   LEFT JOIN  profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
@@ -889,6 +915,25 @@ class Profile implements PlExportable
         return $this->medals->medals;
     }
 
+    /** Sharing data with partner websites
+     */
+    private $partners_settings = null;
+    public function setPartnersSettings(ProfilePartnerSharing $partners_settings)
+    {
+        $this->partners_settings = $partners_settings;
+    }
+
+    public function getPartnerSettings($partner_id)
+    {
+        if ($this->partners_settings === null && !$this->fetched(self::FETCH_PARTNER)) {
+            $this->setPartnersSettings($this->getProfileField(self::FETCH_PARTNER));
+        }
+        if ($this->partners_settings === null) {
+            return PartnerSettings::getEmpty($partner_id);
+        }
+        return $this->partners_settings->get($partner_id);
+    }
+
     public function compareNames($firstname, $lastname)
     {
         $_lastname  = mb_strtoupper($this->lastName());
@@ -938,13 +983,15 @@ class Profile implements PlExportable
             $order = '';
         }
 
-        $visibility = new ProfileVisibility($visibility);
+        if ($visibility === null) {
+            $visibility = Visibility::defaultForRead();
+        }
 
         $it = XDB::Iterator('SELECT  p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
                                      p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
                                      IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
                                      p.last_change, p.nationality1, p.nationality2, p.nationality3,
-                                     IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
+                                     IF (p.freetext_pub >= {?}, p.freetext, NULL) AS freetext,
                                      pe.entry_year, pe.grad_year, pe.promo_year, pe.program, pe.fieldid,
                                      IF ({?}, pse.text, NULL) AS section,
                                      ppn.firstname_main AS firstname, ppn.lastname_main AS lastname, IF ({?}, pn.name, NULL) AS nickname,
@@ -952,8 +999,8 @@ class Profile implements PlExportable
                                      IF (ppn.lastname_ordinary = \'\', ppn.firstname_main, ppn.lastname_ordinary) AS lastname_ordinary,
                                      pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
                                      pd.directory_name, pd.public_name, pd.private_name,
-                                     IF (pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
-                                     (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
+                                     IF (pp.pub >= {?}, pp.display_tel, NULL) AS mobile,
+                                     (ph.pub >= {?} AND ph.attach IS NOT NULL) AS has_photo, ph.pub as photo_pub,
                                      ph.x AS photo_width, ph.y AS photo_height,
                                      p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
                                      pm.expertise AS mentor_expertise,
@@ -973,13 +1020,13 @@ class Profile implements PlExportable
                               WHERE  p.pid IN {?}
                            GROUP BY  p.pid
                                      ' . $order,
-                           $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // CV
-                           $visibility->levels(), // freetext
-                           $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // section
-                           $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // nickname
-                           $visibility->levels(), // mobile
-                           $visibility->levels(), // photo
-                           $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // deltaten_message
+                           $visibility->isVisible(Visibility::EXPORT_PRIVATE), // CV
+                           $visibility->level(), // freetext
+                           $visibility->isVisible(Visibility::EXPORT_PRIVATE), // section
+                           $visibility->isVisible(Visibility::EXPORT_PRIVATE), // nickname
+                           $visibility->level(), // mobile
+                           $visibility->level(), // photo
+                           $visibility->isVisible(Visibility::EXPORT_PRIVATE), // deltaten_message
                            $pids
                        );
         return new ProfileIterator($it, $pids, $fields, $visibility);
@@ -1021,9 +1068,12 @@ class Profile implements PlExportable
      */
     public static function get($login, $fields = 0x0000, $visibility = null)
     {
+        if ($visibility === null) {
+            $visibility = Visibility::defaultForRead();
+        }
+
         if (is_array($login)) {
-            $pf = new Profile($login);
-            $pf->setVisibilityLevel($visibility);
+            $pf = new Profile($login, $visibility);
             return $pf;
         }
         $pid = self::getPID($login);
@@ -1036,7 +1086,7 @@ class Profile implements PlExportable
             if (!($login instanceof PlUser)) {
                 $user = User::getSilent($login);
                 if ($user && $user->hasProfile()) {
-                    return $user->profile();
+                    return $user->profile(false, $fields, $visibility);
                 }
             }
             return null;
@@ -1238,12 +1288,12 @@ class ProfileIterator implements PlIterator
 
     const FETCH_ALL    = 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
 
-    public function __construct(PlIterator $it, array $pids, $fields = 0x0000, ProfileVisibility $visibility = null)
+    public function __construct(PlIterator $it, array $pids, $fields = 0x0000, $visibility = null)
     {
         require_once 'profilefields.inc.php';
 
-        if ($visibility == null) {
-            $visibility = new ProfileVisibility();
+        if ($visibility === null) {
+            $visibility = Visibility::defaultForRead();
         }
 
         $this->fields = $fields;
@@ -1274,8 +1324,7 @@ class ProfileIterator implements PlIterator
 
     private function fillProfile(array $vals)
     {
-        $pf = Profile::get($vals[0]);
-        $pf->setVisibilityLevel($this->visibility->level());
+        $pf = Profile::get($vals[0], 0x0, $this->visibility);
         $pf->setFetchedFields($this->fields);
 
         if ($this->hasData(Profile::FETCH_PHONES, $vals)) {