Latest posts thread can includes groups posts.

This commit is contained in:
Pierre HUBERT 2018-07-20 15:31:21 +02:00
parent af304d7409
commit a9f4afdcbc
3 changed files with 47 additions and 4 deletions

View File

@ -98,8 +98,11 @@ class PostsController {
else else
$startFrom = 0; //No start point $startFrom = 0; //No start point
//Check whether groups posts should be included or not
$include_groups = isset($_POST['include_groups']) ? postBool("include_groups") : FALSE;
//Get the post of the user //Get the post of the user
$posts = CS::get()->components->posts->get_latest(userID, $startFrom, 10); $posts = CS::get()->components->posts->get_latest(userID, $startFrom, 10, $include_groups);
//Return parsed list of posts //Return parsed list of posts
return $this->parsePostsList($posts); return $this->parsePostsList($posts);

View File

@ -254,6 +254,29 @@ class GroupsComponent {
} }
/**
* Get the list of groups a user is following
*
* @param int $userID The ID of the target group
* @return array The IDs of the groups followed by the user
*/
public function getListFollowedByUser(int $userID) : array {
$result = db()->select(
self::GROUPS_MEMBERS_TABLE,
"WHERE user_id = ? AND following = 1",
array($userID),
array("groups_id")
);
//Parse the list of IDs
$list = array();
foreach($result as $el)
$list[] = $el["groups_id"];
return $list;
}
/** /**
* Count the number of a kind of membership in a group * Count the number of a kind of membership in a group
* *

View File

@ -218,9 +218,12 @@ class Posts {
* @param int $userID The ID of the user requesting its list of posts * @param int $userID The ID of the user requesting its list of posts
* @param int $startPoint The startpoint of the research (default: 0 = none) * @param int $startPoint The startpoint of the research (default: 0 = none)
* @param int $limit The limit of the research (default: 10) * @param int $limit The limit of the research (default: 10)
* @param bool $include_groups Specify whether groups post can be selected
* too or not
* @return array The list of newest posts for the user * @return array The list of newest posts for the user
*/ */
public function get_latest(int $userID, int $startPoint = 0, int $limit = 10) : array { public function get_latest(int $userID, int $startPoint = 0,
int $limit = 10, bool $include_groups) : array {
//Check the value of limit (security) //Check the value of limit (security)
if($limit < 1){ if($limit < 1){
@ -236,7 +239,7 @@ class Posts {
//Prepare the request on the database //Prepare the request on the database
//Add the visibility level conditions //Add the visibility level conditions
$conditions = "WHERE group_id = 0 AND niveau_visibilite <= ? AND (ID_personne = ?"; $conditions = "WHERE (group_id = 0 AND niveau_visibilite <= ? AND (ID_personne = ?";
$dataConds = array($visibilityLevel, $userID); $dataConds = array($visibilityLevel, $userID);
//Process the list of friends of the user //Process the list of friends of the user
@ -247,7 +250,21 @@ class Posts {
} }
//Close user list conditions //Close user list conditions
$conditions .= ")"; $conditions .= "))";
//Check whether posts from groups should be included too
if($include_groups){
//Get the list of groups the user is following
$groups = components()->groups->getListFollowedByUser($userID);
//Process the list of groups
foreach($groups as $groupID){
$conditions .= " OR (group_id = ?)";
$dataConds[] = $groupID;
}
}
//Add startpoint condition if required (and get older messages) //Add startpoint condition if required (and get older messages)
if($startPoint != 0){ if($startPoint != 0){