![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.quebec/private_html/php-migration/models/ |
<?php
/**
* ChatParticipant Model
* Migrated from Prisma ChatParticipant model
*/
require_once __DIR__ . '/../config/database.php';
class ChatParticipant {
private $conn;
private $table_name = "ChatParticipant";
public $id;
public $userId;
public $chatRoomId;
public $role;
public $joinedAt;
public function __construct($db) {
$this->conn = $db;
}
// Create chat participant
public function create() {
$query = "INSERT INTO " . $this->table_name . "
(id, userId, chatRoomId, role, joinedAt)
VALUES
(:id, :userId, :chatRoomId, :role, :joinedAt)";
$stmt = $this->conn->prepare($query);
// Generate UUID for id
$this->id = $this->generateUUID();
$this->joinedAt = date('Y-m-d H:i:s');
// Bind values
$stmt->bindParam(':id', $this->id);
$stmt->bindParam(':userId', $this->userId);
$stmt->bindParam(':chatRoomId', $this->chatRoomId);
$stmt->bindParam(':role', $this->role);
$stmt->bindParam(':joinedAt', $this->joinedAt);
if($stmt->execute()) {
return true;
}
return false;
}
// Read participant by ID
public function findById($id) {
$query = "SELECT * FROM " . $this->table_name . " WHERE id = :id LIMIT 1";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':id', $id);
$stmt->execute();
if($stmt->rowCount() > 0) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$this->id = $row['id'];
$this->userId = $row['userId'];
$this->chatRoomId = $row['chatRoomId'];
$this->role = $row['role'];
$this->joinedAt = $row['joinedAt'];
return true;
}
return false;
}
// Get participants by chat room ID
public function getByChatRoomId($chatRoomId) {
$query = "SELECT cp.*, u.name as userName, u.profilePicture, u.role as userRole
FROM " . $this->table_name . " cp
LEFT JOIN User u ON cp.userId = u.id
WHERE cp.chatRoomId = :chatRoomId
ORDER BY cp.joinedAt ASC";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':chatRoomId', $chatRoomId);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Get chat rooms by user ID
public function getByUserId($userId) {
$query = "SELECT cp.*, cr.name as roomName, cr.type as roomType
FROM " . $this->table_name . " cp
LEFT JOIN ChatRoom cr ON cp.chatRoomId = cr.id
WHERE cp.userId = :userId
ORDER BY cr.updatedAt DESC";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':userId', $userId);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Check if user is participant in room
public function isParticipant($userId, $chatRoomId) {
$query = "SELECT COUNT(*) as count FROM " . $this->table_name . "
WHERE userId = :userId AND chatRoomId = :chatRoomId";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':userId', $userId);
$stmt->bindParam(':chatRoomId', $chatRoomId);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result['count'] > 0;
}
// Add user to chat room
public function addToRoom($userId, $chatRoomId, $role = 'MEMBER') {
// Check if already a participant
if ($this->isParticipant($userId, $chatRoomId)) {
return false;
}
$this->userId = $userId;
$this->chatRoomId = $chatRoomId;
$this->role = $role;
return $this->create();
}
// Remove user from chat room
public function removeFromRoom($userId, $chatRoomId) {
$query = "DELETE FROM " . $this->table_name . "
WHERE userId = :userId AND chatRoomId = :chatRoomId";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':userId', $userId);
$stmt->bindParam(':chatRoomId', $chatRoomId);
if($stmt->execute()) {
return true;
}
return false;
}
// Update participant role
public function updateRole($userId, $chatRoomId, $newRole) {
$query = "UPDATE " . $this->table_name . "
SET role = :role
WHERE userId = :userId AND chatRoomId = :chatRoomId";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':role', $newRole);
$stmt->bindParam(':userId', $userId);
$stmt->bindParam(':chatRoomId', $chatRoomId);
if($stmt->execute()) {
return true;
}
return false;
}
// Generate UUID
private function generateUUID() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
}
?>