Campustream 1.0
A social network MQP for WPI
application/lib/notifications.php
Go to the documentation of this file.
00001 <?
00002 
00006 class notifications {
00007         private $user_id;
00008         private $unread, $read;
00009         private $r;
00010         
00011         private static $instance;
00012         
00013         private static $types = array(
00014                 'news',
00015                 'newscomment',
00016                 'collaborate',
00017                 'reply'
00018         );
00019         
00023         public static function load() {
00024                 if (self::$instance == null) {
00025                         self::$instance = new notifications();
00026                 }
00027 
00028                 return self::$instance;
00029         }
00030         
00035         public function __construct($user=false) {
00036                 if ($user instanceof User_Model) {
00037                         $this->user_id = $user->id;
00038                 } elseif (is_numeric($user)) {
00039                         $this->user_id = $user;
00040                 } else {
00041                         $this->user_id = sess::getUserID();
00042                 }
00043                 
00044                 $this->r = RedisManager::connection();
00045         }
00046         
00053         public function get($type, $min = 5) {
00054                 $msgs = array();
00055                 $len = $this->r->llen($this->getKey($type, 'unread'));
00056                 if ($len > 0) {
00057                         $temp = $this->r->lrange($this->getKey($type, 'unread'), 0, $len);
00058                         foreach ($temp as $t) {
00059                                 $msgs[] = unserialize($t);
00060                         }
00061                 }
00062                 
00063                 if (count($msgs) < $min) {
00064                         $temp = $this->r->lrange($this->getKey($type, 'read'), 0, ($min - count($msgs) - 1));
00065                         foreach ($temp as $t) {
00066                                 $msgs[] = unserialize($t);
00067                         }
00068                 }
00069                 
00070                 return $msgs;
00071         }
00072         
00081         public function send($to_user, $type, $msg, $link_id) {
00082                 // type = news | collaborate | reply
00083                 $from = $this->user_id;
00084                 
00085                 $n = new notification($from, $type, $msg, $link_id);
00086                 
00087                 if ($to_user instanceof User_Model) {
00088                         $to_user = $to_user->id;
00089                 }
00090                 
00091                 $this->r->lpush("notifications:{$to_user}:{$type}:unread", serialize($n));
00092         }
00093         
00097         public function markAllAsRead() {
00098                 foreach (self::$types as $type) {
00099                         while ($n = $this->r->rpop($this->getKey($type, 'unread'))) {
00100                                 $n = unserialize($n);
00101                                 $n->is_read = true;
00102                                 
00103                                 $this->r->lpush($this->getKey($type, 'read'), serialize($n));
00104                         }
00105                 }
00106         }
00107         
00113         public function markAsRead($type) {
00114                 if (!in_array($type, self::$types)) {
00115                         return false;
00116                 }
00117                 
00118                 while ($n = $this->r->rpop($this->getKey($type, 'unread'))) {
00119                         $n = unserialize($n);
00120                         $n->is_read = true;
00121                         
00122                         $this->r->lpush($this->getKey($type, 'read'), serialize($n));
00123                 }
00124                 
00125                 if ($type == 'news') {
00126                         return $this->markAsRead('newscomment');
00127                 }
00128                 
00129                 return true;
00130         }
00131         
00136         public function numUnread($type=false) {
00137                 $num = 0;
00138                 
00139                 if (!$type) {
00140                         foreach (self::$types as $type) {
00141                                 $num += $this->r->llen($this->getKey($type, 'unread'));
00142                         }
00143                 } else {
00144                         $num = $this->r->llen($this->getKey($type, 'unread'));
00145                         
00146                         if ($type == 'news') {
00147                                 $num += $this->r->llen($this->getKey('newscomment', 'unread'));
00148                         }
00149                 }
00150                 
00151                 
00152                 return $num;
00153         }
00154         
00155         private function getKey($type, $status) {
00156                 return "notifications:{$this->user_id}:$type:$status";
00157         }
00158 }
00159 
00164 class notification {
00165         public $from_user_id;
00166         public $message;
00167         public $time_sent;
00168         public $type;
00169         public $is_read;
00170         public $link_id;
00171         
00172         private $from_user;
00173         
00182         public function __construct($from, $type, $message, $link_id) {
00183                 if ($from instanceof User_Model) {
00184                         $this->from_user_id = $from->id;
00185                 } else {
00186                         $this->from_user_id = $from;
00187                 }
00188                 
00189                 $this->type = $type;
00190                 $this->message = $message;
00191                 $this->is_read = false;
00192                 $this->time_sent = time();
00193                 $this->link_id = $link_id;
00194         }
00195         
00196         private function get_user() {
00197                 if (!$this->from_user) {
00198                         $this->from_user = ActiveCache::find('User_Model', $this->from_user_id, 43200)->sql(
00199                                 "SELECT * FROM users WHERE id = {$this->from_user_id} LIMIT 1"
00200                         );
00201                 }
00202                 
00203                 return $this->from_user;
00204         }
00205         
00206         public function __get($key) {
00207                 switch ($key) {
00208                         case 'user': return $this->get_user();
00209                 }
00210         }
00211 }