HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux ubuntu-8gb-hel1-1 6.8.0-55-generic #57-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 12 23:42:21 UTC 2025 x86_64
User: www-data (33)
PHP: 8.1.32
Disabled: NONE
Upload Files
File: /var/www/html/obaasimaghana/app/ex/models/Discussion.php
<?php
class Discussion {
    private $db;

    public function __construct() {
        $this->db = new Database;
    }

    /**
     * Create a new discussion message
     * @param array $data Discussion data
     * @return bool|int False on failure, message ID on success
     */
    public function create($data) {
        try {
            $sql = "INSERT INTO material_discussions (user_id, material_id, content, parent_id, created_at) 
                    VALUES (?, ?, ?, ?, NOW())";
            
            $parentId = isset($data['parent_id']) ? $data['parent_id'] : null;
            
            $stmt = $this->db->prepare($sql);
            $stmt->bind_param("iisi", 
                $data['user_id'],
                $data['material_id'],
                $data['content'],
                $parentId
            );
            
            if ($stmt->execute()) {
                return $this->db->lastInsertId();
            }
            return false;
        } catch (Exception $e) {
            error_log("Error creating discussion: " . $e->getMessage());
            return false;
        }
    }

    /**
     * Get discussions for a specific material
     * @param int $materialId
     * @return array
     */
    public function getDiscussionsByMaterial($materialId) {
        try {
            $sql = "SELECT d.*, u.full_name as user_name, u.profile_picture as user_avatar,
                           (SELECT COUNT(*) FROM material_discussions WHERE parent_id = d.id) as reply_count
                    FROM material_discussions d
                    JOIN users u ON d.user_id = u.id
                    WHERE d.material_id = ? AND d.parent_id IS NULL
                    ORDER BY d.created_at DESC";
            
            $stmt = $this->db->prepare($sql);
            $stmt->bind_param("i", $materialId);
            $stmt->execute();
            
            $result = $stmt->get_result();
            $discussions = [];
            while ($row = $result->fetch_assoc()) {
                // Get replies for each discussion
                $row['replies'] = $this->getReplies($row['id']);
                $discussions[] = $row;
            }
            
            return $discussions;
        } catch (Exception $e) {
            error_log("Error getting discussions: " . $e->getMessage());
            return [];
        }
    }

    /**
     * Get replies for a discussion
     * @param int $parentId
     * @return array
     */
    private function getReplies($parentId) {
        try {
            $sql = "SELECT d.*, u.full_name as user_name, u.profile_picture as user_avatar
                    FROM material_discussions d
                    JOIN users u ON d.user_id = u.id
                    WHERE d.parent_id = ?
                    ORDER BY d.created_at ASC";
            
            $stmt = $this->db->prepare($sql);
            $stmt->bind_param("i", $parentId);
            $stmt->execute();
            
            $result = $stmt->get_result();
            $replies = [];
            while ($row = $result->fetch_assoc()) {
                $replies[] = $row;
            }
            
            return $replies;
        } catch (Exception $e) {
            error_log("Error getting replies: " . $e->getMessage());
            return [];
        }
    }

    /**
     * Delete a discussion message
     * @param int $messageId
     * @param int $userId Owner verification
     * @return bool
     */
    public function delete($messageId, $userId) {
        try {
            // First delete all replies
            $sql = "DELETE FROM material_discussions WHERE parent_id = ?";
            $stmt = $this->db->prepare($sql);
            $stmt->bind_param("i", $messageId);
            $stmt->execute();

            // Then delete the main message
            $sql = "DELETE FROM material_discussions WHERE id = ? AND user_id = ?";
            $stmt = $this->db->prepare($sql);
            $stmt->bind_param("ii", $messageId, $userId);
            return $stmt->execute();
        } catch (Exception $e) {
            error_log("Error deleting discussion: " . $e->getMessage());
            return false;
        }
    }

    /**
     * Get unread message count for a user in a material
     * @param int $userId
     * @param int $materialId
     * @return int
     */
    public function getUnreadCount($userId, $materialId) {
        try {
            $sql = "SELECT COUNT(*) as count 
                    FROM material_discussions d
                    LEFT JOIN message_reads r ON d.id = r.message_id AND r.user_id = ?
                    WHERE d.material_id = ? AND r.id IS NULL AND d.user_id != ?";
            
            $stmt = $this->db->prepare($sql);
            $stmt->bind_param("iii", $userId, $materialId, $userId);
            $stmt->execute();
            
            $result = $stmt->get_result();
            $row = $result->fetch_assoc();
            return (int)$row['count'];
        } catch (Exception $e) {
            error_log("Error getting unread count: " . $e->getMessage());
            return 0;
        }
    }

    /**
     * Mark messages as read for a user
     * @param int $userId
     * @param int $materialId
     * @return bool
     */
    public function markAsRead($userId, $materialId) {
        try {
            $sql = "INSERT IGNORE INTO message_reads (user_id, message_id, read_at)
                    SELECT ?, id, NOW()
                    FROM material_discussions
                    WHERE material_id = ? AND user_id != ?";
            
            $stmt = $this->db->prepare($sql);
            $stmt->bind_param("iii", $userId, $materialId, $userId);
            return $stmt->execute();
        } catch (Exception $e) {
            error_log("Error marking messages as read: " . $e->getMessage());
            return false;
        }
    }
}