Can get the number of unread notifications

This commit is contained in:
Pierre 2018-02-18 18:57:19 +01:00
parent abecb78111
commit b9406731a4
2 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,30 @@
<?php
/**
* Notifications REST controller
*
* @author Pierre HUBERT
*/
class notificationsController {
/**
* Get the number of unread notifications of a user
*
* @url POST notifications/count_unread
*/
public function count_unread(){
user_login_required();
//Get the number of unread notifications
$number = components()->notifications->count_unread(userID);
//Return it
return array(
"number" => $number
);
}
}

View File

@ -13,6 +13,24 @@ class notificationComponent {
*/ */
const NOTIFICATIONS_TABLE = "comunic_notifications"; const NOTIFICATIONS_TABLE = "comunic_notifications";
/**
* Count the number of unread notifications of a user
*
* @param int $userID Target user ID
* @return int The number of unread notifications
*/
public function count_unread(int $userID) : int {
//Set the conditions of the request
$conditions = "WHERE dest_user_id = ? AND seen = 0";
$values = array($userID);
//Compute and return result
return CS::get()->db->count(self::NOTIFICATIONS_TABLE, $conditions, $values);
}
/** /**
* Push a new notification * Push a new notification
* *