Add ProfileEducation
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Tue, 16 Mar 2010 17:58:09 +0000 (18:58 +0100)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Mon, 22 Mar 2010 12:49:28 +0000 (13:49 +0100)
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
classes/profile.php
include/profilefields.inc.php

index c374e1b..f3fb591 100644 (file)
@@ -354,31 +354,15 @@ class Profile
 
     /* Educations
      */
+    private $educations;
+    public function setEducations(ProfileEducation $edu)
+    {
+        $this->educations = $edu;
+    }
+
     public function getEducations($flags, $limit = null)
     {
-        $where = XDB::format('pe.pid = {?}', $this->id());
-        if ($flags & self::EDUCATION_MAIN) {
-            $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)';
-        } else if ($flags & self::EDUCATION_EXTRA) {
-            $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)';
-        } else if ($flags & self::EDUCATION_FINISHED) {
-            $where .= ' AND pe.grad_year <= YEAR(CURDATE())';
-        } else if ($flags & self::EDUCATION_CURRENT) {
-            $where .= ' AND pe.grad_year > YEAR(CURDATE())';
-        }
-        $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
-        return XDB::iterator('SELECT  pe.entry_year, pe.grad_year, pe.program,
-                                      pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country,
-                                      pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field,
-                                      FIND_IN_SET(\'primary\', pe.flags) AS prim
-                                FROM  profile_education AS pe
-                          INNER JOIN  profile_education_enum AS pee ON (pe.eduid = pee.id)
-                           LEFT JOIN  geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
-                          INNER JOIN  profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
-                           LEFT JOIN  profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
-                               WHERE  ' . $where . '
-                            ORDER BY  NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id
-                                      ' . $limit);
+        return $this->educations->get($flags, $limit);
     }
 
     public function getExtraEducations($limit = null)
index f97bbc4..ec64e92 100644 (file)
@@ -268,27 +268,81 @@ class Address
     }
 }
 // }}}
+// {{{ class Education
+class Education
+{
+    public $eduid;
+    public $degreeid;
+    public $fieldid;
+
+    public $entry_year;
+    public $grad_year;
+    public $program;
+    public $flags;
+
+    public function __construct(array $data)
+    {
+        $this->eduid    = $data['eduid'];
+        $this->degreeid = $data['degreeid'];
+        $this->fieldid  = $data['fieldid'];
+
+        $this->entry_year = $data['entry_year'];
+        $this->grad_year  = $data['grad_year'];
+        $this->program    = $data['program'];
+        $this->flags      = new PlFlagSet($data['flags']);
+    }
+}
+// }}}
 
 // {{{ class ProfileEducation                         [ Field ]
 class ProfileEducation extends ProfileField
 {
-    public $schools = array();
+    private $educations = array();
 
     private function __construct(PlIterator $it)
     {
+        $this->pid = $it->value();
         $this->visibility = Profile::VISIBILITY_PUBLIC;
         while ($edu = $it->next()) {
-            $this->schools[$edu['id']] = $edu;
+            $this->educations[$edu['id']] = new Education($edu);
         }
     }
 
+    public function get($flags, $limit)
+    {
+        $educations = array();
+        $year = getdate();
+        $year = $year['year'];
+        $nb = 0;
+        foreach ($this->educations as $id => $edu) {
+            if (
+                (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
+                ||
+                (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
+                ||
+                (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
+                ||
+                (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
+            ) {
+                $educations[$id] = $edu;
+                ++$nb;
+            }
+            if ($limit != null && $nb >= $limit) {
+                break;
+            }
+        }
+        return PlIteratorUtils::fromArray($educations);
+    }
+
     public static function fetchData(array $pids, $visibility)
     {
-        $data = XDB::iterator('SELECT  *
+        $data = XDB::iterator('SELECT  id, pid, eduid, degreeid, fieldid,
+                                       entry_year, grad_year, program, flags
                                  FROM  profile_education
                                 WHERE  pid IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                                 $pids);
+                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids) . ',
+                                       NOT FIND_IN_SET(\'primary\', flags), entry_year, id',
+                                $pids);
 
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }