mirror of
				https://github.com/pierre42100/ComunicAPI
				synced 2025-11-04 12:14:12 +00:00 
			
		
		
		
	Added group membership notifications
This commit is contained in:
		@@ -382,6 +382,10 @@ class GroupsController {
 | 
			
		||||
		if(!components()->groups->respondInvitation(userID, $groupID, $accept))
 | 
			
		||||
			Rest_fatal_error(500, "An error occurred while trying to respond to membership invitation!");
 | 
			
		||||
		
 | 
			
		||||
		//Push notification
 | 
			
		||||
		create_group_membership_notification(userID, 0, $groupID, 
 | 
			
		||||
			$accept ? Notification::ACCEPTED_GROUP_MEMBERSHIP_INVITATION : Notification::REJECTED_GROUP_MEMBERSHIP_INVITATION);
 | 
			
		||||
 | 
			
		||||
		//Success
 | 
			
		||||
		return array("success" => "The response to the invitation was saved!");
 | 
			
		||||
	}
 | 
			
		||||
@@ -406,6 +410,9 @@ class GroupsController {
 | 
			
		||||
		if(!components()->groups->deleteRequest(userID, $groupID))
 | 
			
		||||
			Rest_fatal_error(500, "An error occurred while trying to cancel membership request!");
 | 
			
		||||
		
 | 
			
		||||
		//Delete group membership notifications
 | 
			
		||||
		delete_notifications_group_membership(userID, $groupID);
 | 
			
		||||
 | 
			
		||||
		return array("success" => "The request has been successfully cancelled!");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -443,6 +450,11 @@ class GroupsController {
 | 
			
		||||
		if(!components()->groups->insertMember($member))
 | 
			
		||||
			Rest_fatal_error(500, "Could not register membership!");
 | 
			
		||||
		
 | 
			
		||||
		//Push notification
 | 
			
		||||
		if($info->get_registration_level() == GroupInfo::MODERATED_REGISTRATION)
 | 
			
		||||
			create_group_membership_notification(userID, 0, $groupID, 
 | 
			
		||||
				Notification::SENT_GROUP_MEMBERSHIP_REQUEST);
 | 
			
		||||
 | 
			
		||||
		//Success
 | 
			
		||||
		return array("success" => "The membership has been successfully saved!");
 | 
			
		||||
	}
 | 
			
		||||
@@ -482,6 +494,9 @@ class GroupsController {
 | 
			
		||||
		if(!components()->groups->deleteMembershipWithStatus($userID, $groupID, $level))
 | 
			
		||||
			Rest_fatal_error(500, "Could not delete membership!");
 | 
			
		||||
		
 | 
			
		||||
		//Delete group membership notifications
 | 
			
		||||
		delete_notifications_group_membership($userID, $groupID);
 | 
			
		||||
 | 
			
		||||
		//Success
 | 
			
		||||
		return array("success" => "The membership has been successfully deleted!");
 | 
			
		||||
	}
 | 
			
		||||
@@ -556,6 +571,10 @@ class GroupsController {
 | 
			
		||||
		if(!components()->groups->respondRequest($userID, $groupID, $accept))
 | 
			
		||||
			Rest_fatal_error(500, "Could not respond to the membership request!");
 | 
			
		||||
		
 | 
			
		||||
		//Push notification
 | 
			
		||||
		create_group_membership_notification($userID, userID, $groupID, 
 | 
			
		||||
			$accept ? Notification::ACCEPTED_GROUP_MEMBERSHIP_REQUEST : Notification::REJECTED_GROUP_MEMBERSHIP_REQUEST);
 | 
			
		||||
 | 
			
		||||
		//Success
 | 
			
		||||
		return array("success" => "The response to the request has been successfully saved!");
 | 
			
		||||
	}
 | 
			
		||||
@@ -605,6 +624,9 @@ class GroupsController {
 | 
			
		||||
		if(!components()->groups->deleteInvitation($userID, $groupID))
 | 
			
		||||
			Rest_fatal_error(500, "Could not cancel membership invitation!");
 | 
			
		||||
 | 
			
		||||
		//Delete group membership notifications
 | 
			
		||||
		delete_notifications_group_membership($userID, $groupID);
 | 
			
		||||
 | 
			
		||||
		//Success
 | 
			
		||||
		return array("success" => "Membership invitation has been cancelled !");
 | 
			
		||||
	}
 | 
			
		||||
@@ -655,6 +677,9 @@ class GroupsController {
 | 
			
		||||
		if(!components()->groups->deleteMembershipWithStatus(userID, $groupID, $level))
 | 
			
		||||
			Rest_fatal_error(500, "An error occurred while trying to delete your membership!");
 | 
			
		||||
		
 | 
			
		||||
		//Delete group membership notifications
 | 
			
		||||
		delete_notifications_group_membership(userID, $groupID);
 | 
			
		||||
 | 
			
		||||
		//Success
 | 
			
		||||
		return array("success" => "Your membership has been successfully deleted!");
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -199,6 +199,28 @@ class notificationComponent {
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//Handles groups membership notifications
 | 
			
		||||
		else if($notification->get_on_elem_type() == Notification::GROUP_MEMBERSHIP){
 | 
			
		||||
 | 
			
		||||
			//Complete the notification
 | 
			
		||||
			$notification->set_from_container_id(0);
 | 
			
		||||
			$notification->set_from_container_type("");
 | 
			
		||||
			
 | 
			
		||||
			//Check whether the notification has to be pushed to a single user
 | 
			
		||||
			//or to all the moderators of the page
 | 
			
		||||
			if($notification->has_dest_user_id())
 | 
			
		||||
 | 
			
		||||
				//Push the notification in private way (if it has a destination,
 | 
			
		||||
				//generally the target of the membership request)
 | 
			
		||||
				return $this->push_private($notification);
 | 
			
		||||
			
 | 
			
		||||
			else {
 | 
			
		||||
				//Push the notification to all the moderators of the group
 | 
			
		||||
				return $this->push_group_moderators($notification, $notification->get_on_elem_id());
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		//Unsupported element
 | 
			
		||||
		else {
 | 
			
		||||
			throw new Exception("The kind of notification ".$notification->get_on_elem_type()." is not currently supported !");
 | 
			
		||||
@@ -206,6 +228,28 @@ class notificationComponent {
 | 
			
		||||
		
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Push a notification to all the moderators of a group
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param Notification $notification The notification to push
 | 
			
		||||
	 * @param int $groupID The ID of the target group
 | 
			
		||||
	 * @return bool TRUE for a success / FALSE else
 | 
			
		||||
	 */
 | 
			
		||||
	private function push_group_moderators(Notification $notification, int $groupID) : bool {
 | 
			
		||||
 | 
			
		||||
		//Get the list of the moderators of the group
 | 
			
		||||
		$members = components()->groups->getListMembers($groupID);
 | 
			
		||||
		$moderators = array();
 | 
			
		||||
 | 
			
		||||
		foreach($members as $member){
 | 
			
		||||
			if($member->get_level() <= GroupMember::MODERATOR)
 | 
			
		||||
				$moderators[] = $member->get_userID();
 | 
			
		||||
			
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return $this->push_public($notification, $moderators);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Push a notification to several users
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
@@ -25,6 +25,7 @@ class Notification {
 | 
			
		||||
	const POST_SURVEY = "post_survey";
 | 
			
		||||
	const COMMENT = "comment";
 | 
			
		||||
	const FRIENDSHIP_REQUEST = "friend_request";
 | 
			
		||||
	const GROUP_MEMBERSHIP = "group_membership";
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Event type
 | 
			
		||||
@@ -35,6 +36,12 @@ class Notification {
 | 
			
		||||
	const REJECTED_FRIEND_REQUEST = "rejected_friend_request";
 | 
			
		||||
	const ELEM_CREATED = "elem_created";
 | 
			
		||||
	const ELEM_UPDATED = "elem_updated";
 | 
			
		||||
	const SENT_GROUP_MEMBERSHIP_INVITATION = "sent_group_membership_invitation";
 | 
			
		||||
	const ACCEPTED_GROUP_MEMBERSHIP_INVITATION = "accepted_group_membership_invitation";
 | 
			
		||||
	const REJECTED_GROUP_MEMBERSHIP_INVITATION = "rejected_group_membership_invitation";
 | 
			
		||||
	const SENT_GROUP_MEMBERSHIP_REQUEST = "sent_group_membership_request";
 | 
			
		||||
	const ACCEPTED_GROUP_MEMBERSHIP_REQUEST = "accepted_group_membership_request";
 | 
			
		||||
	const REJECTED_GROUP_MEMBERSHIP_REQUEST = "rejected_group_membership_request";
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Event visibility
 | 
			
		||||
@@ -159,6 +166,10 @@ class Notification {
 | 
			
		||||
	 */
 | 
			
		||||
	public function set_from_user_id(int $from_user_id){
 | 
			
		||||
		$this->from_user_id = $from_user_id;
 | 
			
		||||
 | 
			
		||||
		//Check if we have to reset the value
 | 
			
		||||
		if($from_user_id < 0)
 | 
			
		||||
			$this->from_user_id = null;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
@@ -186,6 +197,10 @@ class Notification {
 | 
			
		||||
	 */
 | 
			
		||||
	public function set_dest_user_id(int $dest_user_id){
 | 
			
		||||
		$this->dest_user_id = $dest_user_id;
 | 
			
		||||
 | 
			
		||||
		//Reset the value if required
 | 
			
		||||
		if($dest_user_id < 0)
 | 
			
		||||
			$this->dest_user_id = null;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
 
 | 
			
		||||
@@ -151,7 +151,7 @@ CREATE TABLE `comunic_notifications` (
 | 
			
		||||
  `dest_user_id` int(11) DEFAULT NULL,
 | 
			
		||||
  `on_elem_id` int(11) DEFAULT NULL,
 | 
			
		||||
  `on_elem_type` varchar(25) DEFAULT NULL,
 | 
			
		||||
  `type` varchar(25) DEFAULT NULL,
 | 
			
		||||
  `type` varchar(50) DEFAULT NULL,
 | 
			
		||||
  `visibility` varchar(20) DEFAULT NULL,
 | 
			
		||||
  `from_container_id` int(11) DEFAULT NULL,
 | 
			
		||||
  `from_container_type` varchar(25) DEFAULT NULL,
 | 
			
		||||
 
 | 
			
		||||
@@ -57,6 +57,36 @@ function delete_notifications_friendship_request(int $userOne, int $userTwo) : b
 | 
			
		||||
	return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Delete all the notifications related to a group membership
 | 
			
		||||
 * 
 | 
			
		||||
 * @param int $userID The ID of the target user
 | 
			
		||||
 * @param int $groupID The ID of the target group
 | 
			
		||||
 * @return bool TRUE for a success / FALSE else
 | 
			
		||||
 */
 | 
			
		||||
function delete_notifications_group_membership(int $userID, int $groupID) : bool {
 | 
			
		||||
	
 | 
			
		||||
	user_login_required();
 | 
			
		||||
 | 
			
		||||
	//Create notification object
 | 
			
		||||
	$notification = new Notification();
 | 
			
		||||
	$notification->set_on_elem_type(Notification::GROUP_MEMBERSHIP);
 | 
			
		||||
	$notification->set_on_elem_id($groupID);
 | 
			
		||||
 | 
			
		||||
	//Delete notifications
 | 
			
		||||
	$notification->set_dest_user_id($userID);
 | 
			
		||||
	$notification->set_from_user_id(-1);
 | 
			
		||||
	if(!components()->notifications->delete($notification))
 | 
			
		||||
		return false;
 | 
			
		||||
	
 | 
			
		||||
	$notification->set_dest_user_id(-1);
 | 
			
		||||
	$notification->set_from_user_id($userID);
 | 
			
		||||
	if(!components()->notifications->delete($notification))
 | 
			
		||||
		return false;
 | 
			
		||||
 | 
			
		||||
	return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Create and push a friendship request notification
 | 
			
		||||
 * 
 | 
			
		||||
@@ -83,3 +113,43 @@ function create_friendship_notification(int $fromUser, int $destUser, string $ki
 | 
			
		||||
	//Try to push the notification
 | 
			
		||||
	return components()->notifications->push($notif);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Create and push a group membership  notification
 | 
			
		||||
 * 
 | 
			
		||||
 * @param int $userID The ID of the target user for the membershp
 | 
			
		||||
 * @param int $moderatorID The ID of the moderator creating the notification (0 if it is the user)
 | 
			
		||||
 * @param int $groupID The ID of the target group
 | 
			
		||||
 * @param string $kind The kind of notification to create
 | 
			
		||||
 * @return bool TRUE in case of success / FALSE else
 | 
			
		||||
 */
 | 
			
		||||
function create_group_membership_notification(int $userID, int $moderatorID, int $groupID, string $kind) : bool {
 | 
			
		||||
 | 
			
		||||
	//Delete all the previous notifications
 | 
			
		||||
	if(!delete_notifications_group_membership($userID, $groupID))
 | 
			
		||||
		return false;
 | 
			
		||||
	
 | 
			
		||||
	//Create the notification
 | 
			
		||||
	$notif = new Notification();
 | 
			
		||||
	$notif->set_time_create(time());
 | 
			
		||||
	$notif->set_on_elem_id($groupID);
 | 
			
		||||
	$notif->set_on_elem_type(Notification::GROUP_MEMBERSHIP);
 | 
			
		||||
	$notif->set_type($kind);
 | 
			
		||||
 | 
			
		||||
	if($moderatorID < 1){
 | 
			
		||||
 | 
			
		||||
		//The notification must be sent to all the moderators of the group
 | 
			
		||||
		$notif->set_from_user_id($userID);
 | 
			
		||||
		$notif->set_dest_user_id(-1);
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
		//We specify both the source and the destination of the notification not
 | 
			
		||||
		//to broadcast the notification to all the group members
 | 
			
		||||
		$notif->set_from_user_id($moderatorID);
 | 
			
		||||
		$notif->set_dest_user_id($userID);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	//Try to push the notification
 | 
			
		||||
	return components()->notifications->push($notif);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user