Exemplo n.º 1
0
 /**
  * Check whether there are unread notifications or not
  *
  * @return integer
  */
 public function getNumber()
 {
     $usersId = $this->session->get('identity');
     if (!$usersId) {
         return 0;
     }
     $number = ActivityNotifications::count(array('users_id = ?0 AND was_read = "N"', 'bind' => array($usersId)));
     return $number;
 }
Exemplo n.º 2
0
 public function statsAction()
 {
     $this->view->threads = Posts::count();
     $this->view->replies = Posts::sum(array('column' => 'number_replies'));
     $this->view->votes = Posts::sum(array('column' => 'votes_up + votes_down'));
     $this->view->users = Users::count();
     $this->view->karma = Users::sum(array('column' => 'karma'));
     $this->view->notifications = Notifications::count();
     $this->view->unotifications = ActivityNotifications::count();
     $this->view->views = Posts::sum(array('column' => 'number_views'));
     $this->view->irc = IrcLog::count();
 }
Exemplo n.º 3
0
 public function afterCreate()
 {
     $activity = new ActivityNotifications();
     $activity->users_id = $this->users_id;
     if ($this->type == 'P') {
         $activity->type = 'O';
         $activity->posts_id = $this->code1;
         $activity->posts_replies_id = 0;
     } else {
         if ($this->type == 'C') {
             $activity->type = 'V';
             $activity->posts_id = $this->code2;
             $activity->posts_replies_id = $this->code1;
         } else {
             $activity->type = 'B';
             $activity->posts_id = 1;
             $activity->posts_replies_id = 1;
         }
     }
     $activity->extra = $this->badge;
     $activity->users_origin_id = $this->users_id;
     var_dump($activity->save());
 }
Exemplo n.º 4
0
 /**
  * Shows the latest notifications for the current user
  */
 public function notificationsAction($offset = 0)
 {
     $usersId = $this->session->get('identity');
     if (!$usersId) {
         $this->flashSession->error('You must be logged first');
         return $this->response->redirect();
     }
     $user = Users::findFirstById($usersId);
     if (!$user) {
         $this->flashSession->error('The user does not exist');
         return $this->response->redirect();
     }
     $this->view->user = $user;
     $this->view->notifications = ActivityNotifications::find(['users_id = ?0', 'bind' => [$usersId], 'limit' => 128, 'order' => 'created_at DESC']);
     $this->tag->setTitle('Notifications');
 }
Exemplo n.º 5
0
 /**
  * Accepts a reply as correct answer
  */
 public function acceptAction($id = 0)
 {
     $response = new Response();
     /**
      * Find the post using get
      */
     $postReply = PostsReplies::findFirstById($id);
     if (!$postReply) {
         $contentNotExist = array('status' => 'error', 'message' => 'Post reply does not exist');
         return $response->setJsonContent($contentNotExist);
     }
     $user = Users::findFirstById($this->session->get('identity'));
     if (!$user) {
         $contentLogIn = array('status' => 'error', 'message' => 'You must log in first to vote');
         return $response->setJsonContent($contentLogIn);
     }
     if ($postReply->accepted == 'Y') {
         $contentAlready = array('status' => 'error', 'message' => 'This reply is already accepted as answer');
         return $response->setJsonContent($contentAlready);
     }
     if ($postReply->post->deleted) {
         $contentDeleted = array('status' => 'error', 'message' => 'Post associated to the reply is deleted');
         return $response->setJsonContent($contentDeleted);
     }
     if ($postReply->post->accepted_answer == 'Y') {
         $contentAlreadyAnswer = array('status' => 'error', 'message' => 'This post already has an accepted answer');
         return $response->setJsonContent($contentAlreadyAnswer);
     }
     if ($postReply->post->users_id != $user->id && $user->moderator != 'Y') {
         $contentCorrect = array('status' => 'error', 'message' => 'You can\'t accept this answer as correct');
         return $response->setJsonContent($contentCorrect);
     }
     if ($postReply->post->users_id != $postReply->users_id) {
         $postReply->post->user->karma += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY;
         $postReply->post->user->votes_points += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY;
         $points = 30 + intval(abs($user->karma - $postReply->user->karma) / 1000);
         $parametersBounty = array('users_id = ?0 AND posts_replies_id = ?1', 'bind' => array($postReply->users_id, $postReply->id));
         $postBounty = PostsBounties::findFirst($parametersBounty);
         if ($postBounty) {
             $points += $postBounty->points;
         }
         $postReply->user->karma += $points;
         $postReply->user->votes_points += $points;
         if ($postReply->users_id != $user->id && $postReply->post->users_id != $user->id) {
             $user->karma += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY;
             $user->votes_points += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY;
         }
     }
     $postReply->accepted = 'Y';
     $postReply->post->accepted_answer = 'Y';
     if ($postReply->save()) {
         if (!$user->save()) {
             foreach ($user->getMessages() as $message) {
                 $contentError = array('status' => 'error', 'message' => $message->getMessage());
                 return $response->setJsonContent($contentError);
             }
         }
     }
     if ($user->id != $postReply->users_id) {
         $activity = new ActivityNotifications();
         $activity->users_id = $postReply->users_id;
         $activity->posts_id = $postReply->post->id;
         $activity->posts_replies_id = $postReply->id;
         $activity->users_origin_id = $user->id;
         $activity->type = 'A';
         $activity->save();
     }
     $contentOk = array('status' => 'OK');
     return $response->setJsonContent($contentOk);
 }
Exemplo n.º 6
0
 public function afterCreate()
 {
     if ($this->id > 0) {
         $activity = new Activities();
         $activity->users_id = $this->users_id;
         $activity->posts_id = $this->posts_id;
         $activity->type = Activities::NEW_REPLY;
         $activity->save();
         $toNotify = array();
         /**
          * Notify users that always want notifications
          */
         foreach (Users::find(array('notifications = "Y"', 'columns' => 'id')) as $user) {
             if ($this->users_id != $user->id) {
                 $notification = new Notifications();
                 $notification->users_id = $user->id;
                 $notification->posts_id = $this->posts_id;
                 $notification->posts_replies_id = $this->id;
                 $notification->type = 'C';
                 $notification->save();
                 $activity = new ActivityNotifications();
                 $activity->users_id = $user->id;
                 $activity->posts_id = $this->posts_id;
                 $activity->posts_replies_id = $this->id;
                 $activity->users_origin_id = $this->users_id;
                 $activity->type = 'C';
                 $activity->save();
                 $toNotify[$user->id] = $notification->id;
             }
         }
         /**
          * Register users subscribed to the post
          */
         foreach (PostsSubscribers::findByPostsId($this->posts_id) as $subscriber) {
             if (!isset($toNotify[$subscriber->users_id])) {
                 $notification = new Notifications();
                 $notification->users_id = $subscriber->users_id;
                 $notification->posts_id = $this->posts_id;
                 $notification->posts_replies_id = $this->id;
                 $notification->type = 'C';
                 $notification->save();
                 $activity = new ActivityNotifications();
                 $activity->users_id = $subscriber->users_id;
                 $activity->posts_id = $this->posts_id;
                 $activity->posts_replies_id = $this->id;
                 $activity->users_origin_id = $this->users_id;
                 $activity->type = 'C';
                 $activity->save();
                 $toNotify[$subscriber->users_id] = $notification->id;
             }
         }
         /**
          * Register the user in the post's notifications
          */
         if (!isset($toNotify[$this->users_id])) {
             $parameters = array('users_id = ?0 AND posts_id = ?1', 'bind' => array($this->users_id, $this->posts_id));
             $hasNotifications = PostsNotifications::count($parameters);
             if (!$hasNotifications) {
                 $notification = new PostsNotifications();
                 $notification->users_id = $this->users_id;
                 $notification->posts_id = $this->posts_id;
                 $notification->save();
             }
         }
         /**
          * Notify users that have commented in the same post
          */
         $postsNotifications = PostsNotifications::findByPostsId($this->posts_id);
         foreach ($postsNotifications as $postNotification) {
             if (!isset($toNotify[$postNotification->users_id])) {
                 if ($postNotification->users_id != $this->users_id) {
                     /**
                      * Generate an e-mail notification
                      */
                     $notification = new Notifications();
                     $notification->users_id = $postNotification->users_id;
                     $notification->posts_id = $this->posts_id;
                     $notification->posts_replies_id = $this->id;
                     $notification->type = 'C';
                     $notification->save();
                     $activity = new ActivityNotifications();
                     $activity->users_id = $postNotification->users_id;
                     $activity->posts_id = $this->posts_id;
                     $activity->posts_replies_id = $this->id;
                     $activity->users_origin_id = $this->users_id;
                     $activity->type = 'C';
                     $activity->save();
                     $toNotify[$postNotification->users_id] = $notification->id;
                 }
             }
         }
         /**
          * Queue notifications to be sent
          */
         $this->getDI()->getQueue()->put($toNotify);
     }
 }
Exemplo n.º 7
0
 public function statsAction()
 {
     $this->breadcrumbs->add('Help', '/help')->add('Statistics', '/help/stats', ['linked' => false]);
     $this->tag->setTitle("Statistics");
     $this->view->setVars(['threads' => Posts::count(), 'replies' => Posts::sum(['column' => 'number_replies']), 'votes' => Posts::sum(['column' => 'votes_up + votes_down']), 'users' => Users::count(), 'karma' => Users::sum(['column' => 'karma']), 'notifications' => Notifications::count(), 'unotifications' => ActivityNotifications::count(), 'views' => Posts::sum(['column' => 'number_views']), 'irc' => IrcLog::count()]);
 }