carnet/panel is back.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Wed, 11 Feb 2009 21:38:43 +0000 (22:38 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Wed, 11 Feb 2009 22:52:58 +0000 (23:52 +0100)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/user.php
classes/userfilter.php
classes/xorgsession.php
include/notifs.inc.php
modules/carnet.php
modules/events.php
templates/carnet/panel.tpl

index 283ab44..6d1987f 100644 (file)
@@ -387,6 +387,65 @@ class User extends PlUser
         return $this->last_known_email;
     }
 
+
+    /** Get watch informations
+     */
+    private function fetchWatchData()
+    {
+        if (isset($this->watch_actions)) {
+            return;
+        }
+        $watch = XDB::fetchOneAssoc('SELECT  flags AS watch_flags, actions AS watch_actions,
+                                             UNIX_TIMESTAMP(last) AS watch_last
+                                       FROM  watch
+                                      WHERE  uid = {?}', $this->id());
+        $watch['watch_flags'] = new PlFlagSet($watch['watch_flags']);
+        $watch['watch_actions'] = new PlFlagSet($watch['watch_actions']);
+        $watch['watch_promos'] = XDB::fetchColumn('SELECT  promo
+                                                     FROM  watch_promo
+                                                    WHERE  uid = {?}', $this->id());
+        $watch['watch_users'] = XDB::fetchColumn('SELECT  ni_id
+                                                    FROM  watch_nonins
+                                                   WHERE  uid = {?}', $this->id());
+        $this->fillFromArray($watch);
+    }
+
+    public function watch($type)
+    {
+        $this->fetchWatchData();
+        return $this->watch_actions->hasFlag($type);
+    }
+
+    public function watchContacts()
+    {
+        $this->fetchWatchData();
+        return $this->watch_flags->hasFlag('contacts');
+    }
+
+    public function watchEmail()
+    {
+        $this->fetchWatchData();
+        return $this->watch_flags->hasFlag('mail');
+    }
+
+    public function watchPromos()
+    {
+        $this->fetchWatchData();
+        return $this->watch_promos;
+    }
+
+    public function watchUsers()
+    {
+        $this->fetchWatchData();
+        return $this->watch_users;
+    }
+
+    public function watchLast()
+    {
+        $this->fetchWatchData();
+        return $this->watch_last;
+    }
+
     // Return permission flags for a given permission level.
     public static function makePerms($perms, $is_admin)
     {
index 332f16a..a2e616e 100644 (file)
@@ -433,18 +433,10 @@ class UFC_EmailList implements UserFilterCondition
 
 abstract class UFC_UserRelated implements UserFilterCondition
 {
-    protected $uid;
-    public function __construct($uid = null)
-    {
-        if (is_null($uid)) {
-            $this->uid = S::i('uid');
-        } else if ($uid instanceof PlUser) {
-            $this->uid = $uid->id();
-        } else if (ctype_digit($uid)) {
-            $this->uid = (int)$uid;
-        } else {
-            Platal::page()->kill("Invalid contact type");
-        }
+    protected $user;
+    public function __construct(PlUser &$user)
+    {
+        $this->user =& $user;
     }
 }
 
@@ -452,7 +444,7 @@ class UFC_Contact extends UFC_UserRelated
 {
     public function buildCondition(UserFilter &$uf)
     {
-        $sub = $uf->addContactFilter($this->uid);
+        $sub = $uf->addContactFilter($this->user->id());
         return 'c' . $sub . '.contact IS NOT NULL';
     }
 }
@@ -461,41 +453,52 @@ class UFC_WatchRegistration extends UFC_UserRelated
 {
     public function buildCondition(UserFilter &$uf)
     {
-        $sub = $uf->addWatchRegistrationFilter($this->uid);
-        $su  = $uf->addWatchFilter($this->uid);
-        return 'FIND_IN_SET(\'registration\', w' . $su . '.flags) OR wn' . $sub . '.uid IS NOT NULL';
+        if (!$this->user->watch('registration')) {
+            return UserFilterCondition::COND_FALSE;
+        }
+        $uids = $this->user->watchUsers();
+        if (count($uids) == 0) {
+            return UserFilterCondition::COND_FALSE;
+        } else {
+            return '$UID IN (' . implode(', ', $uids) . ')';
+        }
     }
 }
 
 class UFC_WatchPromo extends UFC_UserRelated
 {
     private $grade;
-    public function __construct($uid = null, $grade = UserFilter::GRADE_ING)
+    public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING)
     {
-        parent::__construct($uid);
+        parent::__construct($user);
         $this->grade = $grade;
     }
 
     public function buildCondition(UserFilter &$uf)
     {
-        $sube = $uf->addEducationFilter(true, $this->grade);
-        $subw = $uf->addWatchPromoFilter($this->uid);
-        $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
-        return $field . ' IS NOT NULL AND ' . $field . ' = wp' . $subw . '.promo';
+        $promos = $this->user->watchPromos();
+        if (count($promos) == 0) {
+            return UserFilterCondition::COND_FALSE;
+        } else {
+            $sube = $uf->addEducationFilter(true, $this->grade);
+            $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
+            return $field . ' IN (' . implode(', ', $promos) . ')';
+        }
     }
 }
 
-class UFC_WatchContacts extends UFC_Contact
+class UFC_WatchContact extends UFC_Contact
 {
     public function buildCondition(UserFilter &$uf)
     {
-        $sub = $uf->addWatchFilter($this->uid);
-        return 'FIND_IN_SET(\'contacts\', w' . $sub . '.flags) AND ' . parent::buildCondition($uf);
+        if (!$this->user->watchContacts()) {
+            return UserFilterCondition::COND_FALSE;
+        }
+        return parent::buildCondition($uf);
     }
 }
 
 
-
 /******************
  * ORDERS
  ******************/
@@ -503,6 +506,10 @@ class UFC_WatchContacts extends UFC_Contact
 abstract class UserFilterOrder
 {
     protected $desc = false;
+    public function __construct($desc = false)
+    {
+        $this->desc = $desc;
+    }
 
     public function buildSort(UserFilter &$uf)
     {
@@ -527,8 +534,8 @@ class UFO_Promo extends UserFilterOrder
 
     public function __construct($grade = null, $desc = false)
     {
+        parent::__construct($desc);
         $this->grade = $grade;
-        $this->desc  = $desc;
     }
 
     protected function getSortTokens(UserFilter &$uf)
@@ -551,10 +558,10 @@ class UFO_Name extends UserFilterOrder
 
     public function __construct($type, $variant = null, $particle = false, $desc = false)
     {
+        parent::__construct($desc);
         $this->type = $type;
         $this->variant = $variant;
         $this->particle = $particle;
-        $this->desc = $desc;
     }
 
     protected function getSortTokens(UserFilter &$uf)
@@ -575,17 +582,37 @@ class UFO_Name extends UserFilterOrder
 
 class UFO_Registration extends UserFilterOrder
 {
-    public function __construct($desc = false)
+    protected function getSortTokens(UserFilter &$uf)
     {
-        $this->desc = $desc;
+        return 'a.registration_date';
     }
+}
 
+class UFO_Birthday extends UserFilterOrder
+{
     protected function getSortTokens(UserFilter &$uf)
     {
-        return 'a.registration_date';
+        return 'p.next_birthday';
     }
 }
 
+class UFO_ProfileUpdate extends UserFilterOrder
+{
+    protected function getSortTokens(UserFilter &$uf)
+    {
+        return 'p.last_change';
+    }
+}
+
+class UFO_Death extends UserFilterOrder
+{
+    protected function getSortTokens(UserFilter &$uf)
+    {
+        return 'p.deathdate';
+    }
+}
+
+
 /***********************************
   *********************************
           USER FILTER CLASS
index f091753..f757337 100644 (file)
@@ -321,6 +321,10 @@ class XorgSession extends PlSession
 
     public function updateNbNotifs()
     {
+        require_once 'notifs.inc.php';
+        $user = S::user();
+        $n = Watch::getCount($user);
+        S::set('notifs', $n);
     }
 
     public function setAccessCookie($replace = false, $log = true) {
index a07ca24..0f313fc 100644 (file)
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-class WatchProfileUpdate
+abstract class WatchOperation
 {
-    const ID = 1;
+    public function getTitle($count = 0)
+    {
+        if ($count == 1) {
+            return str_replace(array('$x', '$s'), '', $this->title);
+        } else {
+            return str_replace(array('$x', '$s'), array('x', 's'), $this->title);
+        }
+    }
+
+    public function getCondition(PlUser &$user, $date)
+    {
+        if (!$user->watch($this->flag)) {
+            return new UFC_False();
+        } else {
+            return $this->buildCondition($user, $date);
+        }
+    }
+
+    abstract protected function buildCondition(PlUser &$user, $date);
+    abstract public function getOrder();
+    abstract public function getDate(PlUser &$user);
+
+    public function seen(PlUser &$user, $last)
+    {
+        return strtotime($this->getDate($user)) > $last;
+    }
+}
+
+class WatchProfileUpdate extends WatchOperation
+{
+    public $flag  = 'profile';
+    public $title = 'Mise$s à jour de fiche';
 
     public static function register(Profile &$profile, $field)
     {
@@ -30,49 +61,105 @@ class WatchProfileUpdate
                      $profile->id(), $field);
     }
 
-    public function getCondition(PlUser &$user)
+    protected function buildCondition(PlUser &$user, $date)
     {
-        return new UFC_And(new UFC_ProfileUpdated('>', $user->watch_last),
-                           new UFC_WatchContacts($user->id()));
+        return new UFC_And(new UFC_ProfileUpdated('>', $date),
+                           new UFC_WatchContact($user));
+    }
+
+    public function getOrder()
+    {
+        return new UFO_ProfileUpdate();
+    }
+
+    public function getDate(PlUser &$user)
+    {
+        return $user->profile()->last_change;
     }
 }
 
-class WatchRegistration
+class WatchRegistration extends WatchOperation
 {
-    const ID = 2;
+    public $flag  = 'registration';
+    public $title = 'Inscription$s';
+
+    protected function buildCondition(PlUser &$user, $date)
+    {
+        return new UFC_And(new UFC_Registered(false, '>', $date),
+                           new UFC_Or(new UFC_WatchContact($user),
+                                      new UFC_WatchPromo($user)));
+    }
+
+    public function getOrder()
+    {
+        return new UFO_Registration();
+    }
 
-    public function getCondition(PlUser &$user)
+    public function getDate(PlUser &$user)
     {
-        return new UFC_And(new UFC_Registered(false, '>', $user->watch_last),
-                           new UFC_Or(new UFC_WatchContacts($user->id()),
-                                      new UFC_WatchPromo($user->id())),
-                           new UFC_WatchRegistration($user->id()));
+        return $user->registration_date;
     }
 }
 
-class WatchDeath
+class WatchDeath extends WatchOperation
 {
-    const ID = 3;
+    public $flag  = 'death';
+    public $title = 'Décès';
+
+    protected function buildCondition(PlUser &$user, $date)
+    {
+        return new UFC_And(new UFC_Dead('>', $date, true),
+                           new UFC_Or(new UFC_WatchPromo($user),
+                                      new UFC_WatchContact($user)));
+    }
 
-    public function getCondition(PlUser &$user)
+    public function getOrder()
     {
-        return new UFC_And(new UFC_Dead('>', $user->watch_last, true),
-                           new UFC_Or(new UFC_WatchPromo($user->id()),
-                                      new UFC_WatchContacts($user->id())));
+        return new UFO_Death();
+    }
+
+    public function getDate(PlUser &$user)
+    {
+        return $user->profile()->deathdate;
+    }
+
+    public function seen(PlUser &$user, $last)
+    {
+        return strtotime($user->profile()->deathdate_rec) > $last;
     }
 }
 
-class WatchBirthday
+class WatchBirthday extends WatchOperation
 {
-    const ID = 4;
+    const WATCH_LIMIT = 604800; // 1 week
 
-    public function getCondition(PlUser &$user)
+    public $flag  = 'birthday';
+    public $title = 'Anniversaire$s';
+
+    protected function buildCondition(PlUser &$user, $date)
     {
         return new UFC_And(new UFC_OR(new UFC_Birthday('=', time()),
-                                      new UFC_And(new UFC_Birthday('<=', time() + 864000),
-                                                  new UFC_Birthday('>', $user->watch_last + 864000))),
-                           new UFC_Or(new UFC_WatchPromo($user->id()),
-                                      new UFC_WatchContacts($user->id())));
+                                      new UFC_And(new UFC_Birthday('<=', time() + self::WATCH_LIMIT),
+                                                  new UFC_Birthday('>', $date + self::WATCH_LIMIT))),
+                           new UFC_Or(new UFC_WatchPromo($user),
+                                      new UFC_WatchContact($user)));
+    }
+
+    public function getOrder()
+    {
+        return new UFO_Birthday();
+    }
+
+    public function getDate(PlUser &$user)
+    {
+        return $user->profile()->next_birthday;
+    }
+
+    public function seen(PlUser &$user, $last)
+    {
+        $birthday = strtotime($user->profile()->next_birthday);
+        return $birthday >  $last + self::WATCH_LIMIT
+            || date('Ymd', $birthday) == date('Ymd');
     }
 }
 
@@ -83,21 +170,55 @@ class Watch
                                      'WatchDeath',
                                      'WatchBirthday');
 
-    private static function fetchCount(PlUser &$user, $class)
+    private static function fetchCount(PlUser &$user, $date, $class)
     {
         $obj = new $class();
-        $uf = new UserFilter($obj->getCondition($user));
+        $uf = new UserFilter($obj->getCondition($user, $date));
         return $uf->getTotalCount();
     }
 
-    public static function getCount(PlUser &$user)
+    public static function getCount(PlUser &$user, $date = null)
     {
         $count = 0;
+        if (is_null($date)) {
+            $date = $user->watchLast();
+        }
         foreach (self::$classes as $class) {
-            $count += self::fetchCount($user, $class);
+            $count += self::fetchCount($user, $date, $class);
         }
         return $count;
     }
+
+
+    private static function fetchEvents(PlUser &$user, $date, $class)
+    {
+        $obj = new $class();
+        $uf = new UserFilter($obj->getCondition($user, $date),
+                             array($obj->getOrder(), new UFO_Name(UserFilter::DN_SORT)));
+        $users = $uf->getUsers();
+        if (count($users) == 0) {
+            return null;
+        } else {
+            return array('operation' => $obj,
+                         'title'     => $obj->getTitle(count($users)),
+                         'users'     => $users);
+        }
+    }
+
+    public static function getEvents(PlUser &$user, $date = null)
+    {
+        if (is_null($date)) {
+            $date = $user->watchLast();
+        }
+        $events = array();
+        foreach (self::$classes as $class) {
+            $e = self::fetchEvents($user, $date, $class);
+            if (!is_null($e)) {
+                $events[] = $e;
+            }
+        }
+        return $events;
+    }
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
index 7afc6bc..e643505 100644 (file)
@@ -58,16 +58,20 @@ class CarnetModule extends PLModule
         $page->changeTpl('carnet/panel.tpl');
 
         if (Get::has('read')) {
-            S::set('watch_last', Get::v('read'));
+            XDB::execute('UPDATE  watch
+                             SET  last = FROM_UNIXTIME({?})
+                           WHERE  uid = {?}',
+                         Get::i('read'), S::i('uid'));
+            S::set('watch_last', Get::i('read'));
             Platal::session()->updateNbNotifs();
             pl_redirect('carnet/panel');
         }
 
         require_once 'notifs.inc.php';
+        $page->assign('now', time());
 
-        $page->assign('now',date('YmdHis'));
-        $notifs = new Notifs(S::v('uid'), true);
-
+        $user = S::user();
+        $notifs = Watch::getEvents($user, time() - (7 * 86400));
         $page->assign('notifs', $notifs);
         $page->assign('today', date('Y-m-d'));
         $this->_add_rss_link($page);
index 1a03dc3..063ffc4 100644 (file)
@@ -157,9 +157,6 @@ class EventsModule extends PLModule
             pl_redirect('events#newsid'.$eid);
         }
 
-        require_once 'notifs.inc.php';
-        echo Watch::getCount(S::user());
-
         function next_event(PlIterator &$it)
         {
             $user = S::user();
index aa25b55..3ba7edb 100644 (file)
@@ -43,56 +43,28 @@ Il faut pour cela se rendre sur la page de <a href='carnet/notifs'>configuration
 {/if}
 </div>
 
-{foreach from=$notifs->_data item=c key=cid}
-<h2>{if ($c|@count) > 1}
-{$notifs->_cats[$cid].mail}&nbsp;:
-{else}
-  {foreach from=$c item=promo}
-    {if ($promo|@count) > 1}
-      {$notifs->_cats[$cid].mail}&nbsp;:
-    {else}
-      {if $promo[0].sexe}
-        {$notifs->_cats[$cid].mail_sg_xette}&nbsp;:
-      {else}
-        {$notifs->_cats[$cid].mail_sg}&nbsp;:
-      {/if}
+{foreach from=$notifs item=cat}
+<fieldset style="width: 75%; margin-left: auto; margin-right: auto">
+  <legend>{$cat.title}</legend>
+  {assign var=date value=false}
+    {foreach from=$cat.users item=user}
+    {assign var=userdate value=$cat.operation->getDate($user)}
+    {if !$date || $date ne $userdate}
+    {if $date}
+    </ul>
     {/if}
-  {/foreach}
-{/if}</h2>
-
-<br />
-
-<table class='tinybicol'>
-  {foreach from=$c key=p item=promo}
-  {section name=row loop=$promo}
-  <tr {if ( $promo[row].known > $smarty.session.watch_last ) || ( $promo[row].date eq $today ) }style="font-weight: bold"{/if}>
-    <td class='titre' style="width:15%" {if $promo[row].data}rowspan="2"{/if}>{if $smarty.section.row.first}{$p}{/if}</td>
-    <td>
-      {if $promo[row].inscrit}
-      <a href="profile/{$promo[row].bestalias}" class="popup2">
-        {$promo[row].prenom} {$promo[row].nom}
-      </a>
-      {if !$promo[row].contact}
-      <a href="carnet/contacts?action=ajouter&amp;user={$promo[row].bestalias}&amp;token={xsrf_token}">{*
-        *}{icon name=add title="ajouter à mes contacts"}</a>
-      {/if}
-      {else}
-      {$promo[row].prenom} {$promo[row].nom}
-      {/if}
-    </td>
-    <td style="width:25%">
-      {$promo[row].date|date_format}
-    </td>
-    {if $promo[row].data}
-    </tr><tr><td colspan="2">{$promo[row].data|smarty:nodefaults}</td>
+    {assign var=date value=$userdate}
+    <p>Le {$date|date_format}&nbsp;:</p>
+    <ul>
     {/if}
-  </tr>
-  {/section}
-  {/foreach}
-</table>
-
-<br />
+    <li>
+      {if $cat.operation->seen($user,$smarty.session.watch_last)}<strong>{/if}
+      {profile user=$user promo=true}
+      {if $cat.operation->seen($user,$smarty.session.watch_last)}</strong>{/if}
+    </li>
+    {/foreach}
+  </ul>
+</fieldset>
 {/foreach}
 
-
 {* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}