Can get information about a group

This commit is contained in:
Pierre HUBERT 2018-07-03 09:10:04 +02:00
parent 30d6a1fd9d
commit 8367bd81ac
4 changed files with 231 additions and 0 deletions

View File

@ -40,4 +40,75 @@ class GroupsController {
); );
} }
/**
* Get information about a group
*
* @url POST /groups/get_info
*/
public function getInfo(){
//Get the ID of the requested group
$id = postInt("id");
//Get information about the group
$group = components()->groups->get_info($id);
//Check if the group was not found
if(!$group->isValid())
Rest_fatal_error(404, "The requested group was not found !");
//Parse and return information about the group
return self::GroupInfoToAPI($group);
}
/**
* Get advanced information about a group
*
* @url POST /groups/get_advanced_info
*/
public function getAdvancedInfo(){
//Get the ID of the requested group
$id = postInt("id");
//Get information about the group
$group = components()->groups->get_advanced_info($id);
//Check if the group was not found
if(!$group->isValid())
Rest_fatal_error(404, "The requested group was not found !");
//Parse and return information about the group
return self::AdvancedGroupInfoToAPI($group);
}
/**
* Parse a GroupInfo object into an array for the API
*
* @param GroupInfo $info Information about the group
* @return array Generated API data
*/
public static function GroupInfoToAPI(GroupInfo $info) : array {
$data = array();
$data["id"] = $info->get_id();
$data["name"] = $info->get_name();
$data["number_members"] = $info->get_number_members();
return $data;
}
/**
* Parse an AdvancedGroupInfo object into an array for the API
*
* @param AdvancedGroupInfo $info Information about the group
* @return array Generated API data
*/
public static function AdvancedGroupInfoToAPI(AdvancedGroupInfo $info) : array {
$data = self::GroupInfoToAPI($info);
$data["time_create"] = $info->get_time_create();
return $data;
}
} }

View File

@ -51,6 +51,46 @@ class GroupsComponent {
return $groupID; return $groupID;
} }
/**
* Get and return information about a group
*
* @param int $id The ID of the target group
* @return GroupInfo Information about the group / invalid
* object in case of failure
*/
public function get_info(int $id) : GroupInfo {
//Query the database
$info = db()->select(self::GROUPS_LIST_TABLE, "WHERE id = ?", array($id));
//Check for results
if(count($info) == 0)
return new GroupInfo(); //Return invalid object
//Create and fill GroupInfo object with database entry
return $this->dbToGroupInfo($info[0]);
}
/**
* Get and return advanced information about a group
*
* @param int $id The ID of the target group
* @return GroupInfo Information about the group / invalid
* object in case of failure
*/
public function get_advanced_info(int $id) : AdvancedGroupInfo {
//Query the database
$info = db()->select(self::GROUPS_LIST_TABLE, "WHERE id = ?", array($id));
//Check for results
if(count($info) == 0)
return new AdvancedGroupInfo(); //Return invalid object
//Create and fill GroupInfo object with database entry
return $this->dbToAdvancedGroupInfo($info[0]);
}
/** /**
* Insert a new group member * Insert a new group member
* *
@ -66,6 +106,56 @@ class GroupsComponent {
)); ));
} }
/**
* Count the number of members of a group
*
* @param int $id The ID of the target group
* @return int The number of members of the group
*/
private function countMembers(int $id) : int {
return db()->count(self::GROUPS_MEMBERS_TABLE,
"WHERE groups_id = ?",
array($id));
}
/**
* Turn a database entry into a GroupInfo object
*
* @param array $data Database entry
* @param GroupInfo $group The object to fill with the information (optionnal)
* @return GroupInfo Generated object
*/
private function dbToGroupInfo(array $data, GroupInfo $info = null) : GroupInfo {
if($info == null)
$info = new GroupInfo();
$info->set_id($data["id"]);
$info->set_name($data["name"]);
$info->set_number_members($this->countMembers($info->get_id()));
return $info;
}
/**
* Turn a database entry into AdvancedGroupInfo object entry
*
* @param array $data Database entry
* @return AdvancedGroupInfo Advanced information about the group
*/
private function dbToAdvancedGroupInfo(array $data) : AdvancedGroupInfo {
//Parse basical information about the group
$info = new AdvancedGroupInfo();
$this->dbToGroupInfo($data, $info);
//Parse advanced information
$info->set_time_create($data["time_create"]);
return $info;
}
} }
//Register component //Register component

View File

@ -0,0 +1,29 @@
<?php
/**
* Advanced information about a group model
*
* @author Pierre HUBERT
*/
//Make sure that GroupInfo has already been included
require_once __DIR__."/GroupInfo.php";
class AdvancedGroupInfo extends GroupInfo {
//Private fields
private $time_create = -1;
//Get and set the creation time of the group
public function set_time_create(int $time_create){
$this->time_create = $time_create;
}
public function has_time_create() : bool {
return $this->time_create > -1;
}
public function get_time_create() : int {
return $this->time_create;
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* Group information model
*
* @author Pierre HUBERT
*/
class GroupInfo extends BaseUniqueObject {
//Private fields
private $name;
private $number_members = -1;
//Get and set the name of group
public function set_name(string $name){
$this->name = $name == "" ? null : $name;
}
public function has_name() : bool {
return $this->name != null;
}
public function get_name() : string {
return $this->name != null ? $this->name : "null";
}
//Get and set the number of members of the group
public function set_number_members(int $number_members){
$this->number_members = $number_members;
}
public function has_number_members() : bool {
return $this->number_members > -1;
}
public function get_number_members() : int {
return $this->number_members;
}
}