where('recipient_id', $recipientId) ->where('read_status', 0) ->orderBy('sent_datetime', 'DESC') ->findAll(); } /** * Mark a message as read and update the read_datetime. * * @param int $messageId * @return bool */ public function markAsRead($messageId) { return $this->update($messageId, [ 'read_status' => 1, 'read_datetime' => utc_now() // Set the current datetime when marked as read ]); } /** * Get messages by priority. * * @param string $priority * @return array */ public function getMessagesByPriority($priority = 'normal') { return $this->where('priority', $priority) ->orderBy('sent_datetime', 'DESC') ->findAll(); } /** * Get messages by status. * * @param string $status * @return array */ public function getMessagesByStatus($status = 'sent') { return $this->where('status', $status) ->orderBy('sent_datetime', 'DESC') ->findAll(); } /** * Get inbox messages for a user. * * @param int $userId * @return array */ public function getInboxMessages($userId) { return $this->where('recipient_id', $userId) ->where('status', 'received') ->orderBy('sent_datetime', 'DESC') ->findAll(); } /** * Get sent messages for a user. * * @param int $userId * @return array */ public function getSentMessages($userId) { return $this->where('sender_id', $userId) ->where('status', 'sent') ->orderBy('sent_datetime', 'DESC') ->findAll(); } /** * Get draft messages for a user. * * @param int $userId * @return array */ public function getDraftMessages($userId) { return $this->where('sender_id', $userId) ->where('status', 'draft') ->orderBy('sent_datetime', 'DESC') ->findAll(); } /** * Get trashed messages for a user. * * @param int $userId * @return array */ public function getTrashedMessages($userId) { return $this->groupStart() ->where('sender_id', $userId) ->orWhere('recipient_id', $userId) ->groupEnd() ->where('status', 'trashed') ->orderBy('sent_datetime', 'DESC') ->findAll(); } /** * Get messages for a specific semester and school year. * * @param string $semester * @param string|null $school_year * @return array */ public function getMessagesBySemesterAndYear($semester, $school_year = null) { $builder = $this->where('semester', $semester); if ($school_year) { $builder->where('school_year', $school_year); } return $builder->orderBy('sent_datetime', 'DESC')->findAll(); } }