Get membership level of a user

This commit is contained in:
Pierre HUBERT 2018-07-03 14:33:53 +02:00
parent 7ef4d438c9
commit d6312f4b38
3 changed files with 70 additions and 1 deletions

View File

@ -7,6 +7,17 @@
class GroupsController {
/**
* API groups membership levels
*/
const GROUPS_MEMBERSHIP_LEVELS = array(
0 => "administrator",
1 => "moderator",
2 => "member",
3 => "pending",
4 => "visitor"
);
/**
* Create a group
*
@ -95,6 +106,7 @@ class GroupsController {
$data["name"] = $info->get_name();
$data["icon_url"] = $info->get_icon_url();
$data["number_members"] = $info->get_number_members();
$data["membership"] = self::GROUPS_MEMBERSHIP_LEVELS[$info->get_membership_level()];
return $data;
}

View File

@ -106,6 +106,48 @@ class GroupsComponent {
));
}
/**
* Check whether a user has already a saved membership in a group or not
*
* @param int $userID The ID of the target user
* @param int $groupID The ID of the target group
* @return bool TRUE if the database includes a membership for the user / FALSE else
*/
private function hasMembership(int $userID, int $groupID) : bool {
return db()->count(
self::GROUPS_MEMBERS_TABLE,
"WHERE groups_id = ? AND user_id = ?",
array($groupID, $userID)) > 0;
}
/**
* Get the membership level of a user to a group
*
* @param int $userID The ID of the queried user
* @param int $groupID The ID of the target group
* @return int The membership level of the user
*/
public function getMembershipLevel(int $userID, int $groupID) : int {
//Check for membership
if(!$this->hasMembership($userID, $groupID))
return GroupMember::VISITOR;
//Fetch the database to get membership
$results = db()->select(
self::GROUPS_MEMBERS_TABLE,
"WHERE groups_id = ? AND user_id = ?",
array($groupID, $userID),
array("level")
);
//Check for results
if(count($results) < 0)
return GroupMember::VISITOR; //Security first
return $results[0]["level"];
}
/**
* Count the number of members of a group
*
@ -133,6 +175,7 @@ class GroupsComponent {
$info->set_id($data["id"]);
$info->set_name($data["name"]);
$info->set_number_members($this->countMembers($info->get_id()));
$info->set_membership_level($this->getMembershipLevel(userID, $info->get_id()));
return $info;

View File

@ -14,6 +14,7 @@ class GroupInfo extends BaseUniqueObject {
private $name;
private $number_members = -1;
private $icon;
private $membership_level = -1;
//Get and set the name of group
@ -57,5 +58,18 @@ class GroupInfo extends BaseUniqueObject {
public function get_icon_url() : string {
return path_user_data($this->get_icon());
}
}
//Get and set the membership level of the current user
public function set_membership_level(int $membership_level){
$this->membership_level = $membership_level;
}
public function has_membership_level() : bool {
return $this->membership_level > -1;
}
public function get_membership_level() : int {
return $this->membership_level;
}
}