From e624540c160453525cc1e4830ee0a2e529f6d0ed Mon Sep 17 00:00:00 2001 From: Paminger Date: Tue, 10 May 2016 14:12:33 +0200 Subject: [PATCH] Message --- application/config/fhcomplete.php | 4 +- application/config/migration.php | 4 +- application/controllers/DBTools.php | 3 +- application/controllers/ModelTest.php | 6 +- .../{message => system}/Message.php | 0 application/core/FHC_Model.php | 5 +- application/migrations/008_message.php | 78 +- application/models/system/Message_model.php | 4178 +++-------------- application/models/system/Thread_model.php | 14 + include/studiengang.class.php | 2 +- 10 files changed, 667 insertions(+), 3627 deletions(-) rename application/controllers/{message => system}/Message.php (100%) create mode 100644 application/models/system/Thread_model.php diff --git a/application/config/fhcomplete.php b/application/config/fhcomplete.php index d82f90165..4db7d6fca 100644 --- a/application/config/fhcomplete.php +++ b/application/config/fhcomplete.php @@ -10,5 +10,7 @@ $config['fhc_acl'] = array 'public.tbl_prestudent' => 'basis/person', 'public.tbl_prestudentstatus' => 'basis/person', 'public.tbl_organisationseinheit' => 'basis/organisationseinheit', - 'public.tbl_sprache' => 'admin' + 'public.tbl_sprache' => 'admin', + 'public.tbl_msg_thread' => 'admin', + 'public.tbl_msg_message' => 'admin' ); diff --git a/application/config/migration.php b/application/config/migration.php index b4732b46e..886f95e24 100755 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -23,7 +23,7 @@ $config['migration_enabled'] = TRUE; | be upgraded / downgraded to. | */ -$config['migration_version'] = 008; +$config['migration_version'] = 007; /* |-------------------------------------------------------------------------- @@ -55,7 +55,7 @@ $config['migration_type'] = 'sequential'; | will migrate up. This must be set. | */ -$config['migration_table'] = 'ci_migrations'; +$config['migration_table'] = 'system.ci_migrations'; /* |-------------------------------------------------------------------------- diff --git a/application/controllers/DBTools.php b/application/controllers/DBTools.php index f68c67a3a..ea0b61e6f 100644 --- a/application/controllers/DBTools.php +++ b/application/controllers/DBTools.php @@ -48,6 +48,7 @@ class DBTools extends FHC_Controller if (ENVIRONMENT !== 'development') exit('Wowsers! You don\'t want to do that!'); $this->load->database('system'); //Use the system-Connection for DB-Manipulation + $this->config->load('migration'); $this->load->library('migration'); // If not set, set it @@ -301,7 +302,7 @@ class DBTools extends FHC_Controller */ private function __getVersion() { - $row = $this->db->get('ci_migrations')->row(); + $row = $this->db->get($this->config->item('migration_table'))->row(); return $row ? $row->version : 0; } } diff --git a/application/controllers/ModelTest.php b/application/controllers/ModelTest.php index 430021d26..3e89a3dbc 100755 --- a/application/controllers/ModelTest.php +++ b/application/controllers/ModelTest.php @@ -28,7 +28,7 @@ class ModelTest extends FHC_Controller $res = $this->Person_model->getPerson(null, 'asdf\' OR person_id=1; SELECT 1; --'); var_dump($res->result_object()); - $this->load->model('person/Prestudent_model'); + $this->load->model('crm/Prestudent_model'); $id=null; // Insert PreStudent @@ -79,7 +79,7 @@ class ModelTest extends FHC_Controller var_dump($res->retval); // Insert PreStudentStatus - $this->load->model('person/Prestudentstatus_model'); + $this->load->model('crm/Prestudentstatus_model'); $data = array ( 'prestudent_id' => $id, @@ -155,7 +155,7 @@ class ModelTest extends FHC_Controller else var_dump($res->retval); - $this->load->model('core/Sprache_model'); + $this->load->model('system/Sprache_model'); // Insert Sprache $data = array ( diff --git a/application/controllers/message/Message.php b/application/controllers/system/Message.php similarity index 100% rename from application/controllers/message/Message.php rename to application/controllers/system/Message.php diff --git a/application/core/FHC_Model.php b/application/core/FHC_Model.php index f31ad6b78..8afcbb7cc 100644 --- a/application/core/FHC_Model.php +++ b/application/core/FHC_Model.php @@ -1,5 +1,6 @@ session->uid; else $uid = null; - $this->load->library('FHC_DB_ACL',array('uid' => $uid)); + $this->load->library('FHC_DB_ACL', array('uid' => $uid)); } /** --------------------------------------------------------------- diff --git a/application/migrations/008_message.php b/application/migrations/008_message.php index 64e94b718..b5133a983 100644 --- a/application/migrations/008_message.php +++ b/application/migrations/008_message.php @@ -6,57 +6,59 @@ class Migration_Message extends CI_Migration { public function up() { - if (!$this->db->table_exists('msg_messages')) + if (! $this->db->table_exists('public.tbl_msg_message')) { - $query= ' - CREATE TABLE msg_messages ( - id serial, + $query= " + CREATE TABLE public.tbl_msg_message ( + message_id serial, thread_id bigint NOT NULL, body text NOT NULL, priority smallint NOT NULL DEFAULT 0, - sender_id bigint NOT NULL, + person_id bigint NOT NULL, cdate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (id) + PRIMARY KEY (message_id) ); - GRANT SELECT ON TABLE msg_messages TO web; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE msg_messages TO admin; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE msg_messages TO vilesci; - GRANT SELECT, UPDATE ON SEQUENCE msg_messages_id_seq TO web; - GRANT SELECT, UPDATE ON SEQUENCE msg_messages_id_seq TO admin; - GRANT SELECT, UPDATE ON SEQUENCE msg_messages_id_seq TO vilesci; + COMMENT ON COLUMN public.tbl_msg_message.person_id IS 'Sender'; + COMMENT ON COLUMN public.tbl_msg_message.priority IS 'Codex in config/message.php'; + GRANT SELECT ON TABLE public.tbl_msg_message TO web; + GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_message TO admin; + GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_message TO vilesci; + GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_message_message_id_seq TO web; + GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_message_message_id_seq TO admin; + GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_message_message_id_seq TO vilesci; - CREATE TABLE msg_participants ( - user_id bigint NOT NULL, + CREATE TABLE public.tbl_msg_participant ( + person_id bigint NOT NULL, thread_id bigint NOT NULL, cdate timestamp NOT NULL DEFAULT now(), - PRIMARY KEY (user_id,thread_id) + PRIMARY KEY (person_id,thread_id) ); - GRANT SELECT ON TABLE msg_participants TO web; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE msg_participants TO admin; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE msg_participants TO vilesci; + GRANT SELECT ON TABLE public.tbl_msg_participant TO web; + GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_participant TO admin; + GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_participant TO vilesci; - CREATE TABLE msg_status ( + CREATE TABLE public.tbl_msg_status ( message_id bigint NOT NULL, - user_id bigint NOT NULL, + person_id bigint NOT NULL, status smallint NOT NULL, - PRIMARY KEY (message_id,user_id) + PRIMARY KEY (message_id,person_id) ); - GRANT SELECT ON TABLE msg_status TO web; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE msg_status TO admin; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE msg_status TO vilesci; + GRANT SELECT ON TABLE public.tbl_msg_status TO web; + GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_status TO admin; + GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_status TO vilesci; - CREATE TABLE msg_threads ( - id serial, + CREATE TABLE public.tbl_msg_thread ( + thread_id serial, subject text, - PRIMARY KEY (id) + PRIMARY KEY (thread_id) ); - GRANT SELECT ON TABLE msg_threads TO web; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE msg_threads TO admin; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE msg_threads TO vilesci; - GRANT SELECT, UPDATE ON SEQUENCE msg_threads_id_seq TO web; - GRANT SELECT, UPDATE ON SEQUENCE msg_threads_id_seq TO admin; - GRANT SELECT, UPDATE ON SEQUENCE msg_threads_id_seq TO vilesci; -'; + GRANT SELECT ON TABLE public.tbl_msg_thread TO web; + GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_thread TO admin; + GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_thread TO vilesci; + GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_thread_thread_id_seq TO web; + GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_thread_thread_id_seq TO admin; + GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_thread_thread_id_seq TO vilesci; +"; if (!$this->db->simple_query($query)) { echo "Error creating Basis DB-Schema!"; @@ -66,10 +68,10 @@ class Migration_Message extends CI_Migration { public function down() { - $this->dbforge->drop_table('msg_messages'); - $this->dbforge->drop_table('msg_participants'); - $this->dbforge->drop_table('msg_status'); - $this->dbforge->drop_table('msg_threads'); + $this->dbforge->drop_table('public.tbl_msg_message'); + $this->dbforge->drop_table('public.tbl_msg_participant'); + $this->dbforge->drop_table('public.tbl_msg_status'); + $this->dbforge->drop_table('public.tbl_msg_thread'); } } diff --git a/application/models/system/Message_model.php b/application/models/system/Message_model.php index 059ff2d45..d2f503eea 100644 --- a/application/models/system/Message_model.php +++ b/application/models/system/Message_model.php @@ -1,3582 +1,602 @@ + - - - - - - +class Message_model extends DB_Model +{ + /** + * Constructor + */ + public function __construct() + { + parent::__construct(); + require_once APPPATH.'config/message.php'; + $this->lang->load('message'); + $this->dbTable = 'public.tbl_msg_message'; + $this->pk = 'message_id'; + } + + /** ----------------------------------------------------------------- + * getMessage() - will return a single message, including the status for specified user. + * + * @param integer $msg_id EQUIRED + * @param integer $person_id REQUIRED + * @return array + */ + function getMessage($msg_id, $person_id) + { + // Validate + if (empty($msg_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_MSG_ID); + } + if (empty($person_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_USER_ID); + } + $sql = 'SELECT m.*, s.status, t.subject, ' . "CONCAT(vorname, ' ', nachname) as user_name" . + ' FROM ' . $this->db->dbprefix . 'tbl_msg_message m ' . + ' JOIN ' . $this->db->dbprefix . 'tbl_msg_thread t ON (m.thread_id = t.thread_id) ' . + ' JOIN ' . $this->db->dbprefix . 'public.tbl_person' . ' ON (' . 'tbl_person.person_id' . ' = m.sender_id) '. + ' JOIN ' . $this->db->dbprefix . 'tbl_msg_status s ON (s.message_id = m.message_id AND s.person_id = ? ) ' . + ' WHERE m.message_id = ? ' ; + $result = $this->db->query($sql, array($person_id, $msg_id)); + if ($result) + return $this->_success($result->result_array()); + else + return $this->_general_error(); + } + /** ----------------------------------------------------------------- + * Get a Full Thread + * get_full_thread() - will return a entire thread, including the status for specified user. + * + * @param integer $thread_id REQUIRED + * @param integer $person_id REQUIRED + * @param boolean $full_thread OPTIONAL - If true, user will also see messages from thread posted BEFORE user became participant + * @param string $order_by OPTIONAL + * @return array + */ + function get_full_thread($thread_id, $person_id, $full_thread = FALSE, $order_by = 'ASC') + { + // Validate + if (empty($thread_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID); + } + if (empty($person_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_USER_ID); + } + $sql = 'SELECT m.*, s.status, t.subject, '."CONCAT(vorname, ' ', nachname) as user_name" . + ' FROM ' . $this->db->dbprefix . 'tbl_msg_participant p ' . + ' JOIN ' . $this->db->dbprefix . 'tbl_msg_thread t ON (t.thread_id = p.thread_id) ' . + ' JOIN ' . $this->db->dbprefix . 'tbl_msg_message m ON (m.thread_id = t.thread_id) ' . + ' JOIN ' . $this->db->dbprefix . 'public.tbl_person' . ' ON (' . 'tbl_person.person_id' . ' = m.sender_id) '. + ' JOIN ' . $this->db->dbprefix . 'tbl_msg_status s ON (s.message_id = m.message_id AND s.person_id = ? ) ' . + ' WHERE p.person_id = ? ' . + ' AND p.thread_id = ? '; + if ( ! $full_thread) + { + $sql .= ' AND m.cdate >= p.cdate'; + } + $sql .= ' ORDER BY m.cdate ' . $order_by; + $result = $this->db->query($sql, array($person_id, $person_id, $thread_id)); + if ($result) + return $this->_success($result->result_array()); + else + return $this->_general_error(); + } + /** ----------------------------------------------------------------- + * get_all_threads() - will return all threads for user, including the status for specified user. + * + * @param integer $person_id REQUIRED + * @param boolean $full_thread OPTIONAL - If true, user will also see messages from thread posted BEFORE user became participant + * @param string $order_by OPTIONAL + * @return array + */ + function get_all_threads($person_id, $full_thread = FALSE, $order_by = 'asc') + { + if (empty($person_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_USER_ID); + } + $sql = 'SELECT m.*, s.status, t.subject, '."CONCAT(vorname, ' ', nachname) as user_name" . + ' FROM ' . $this->db->dbprefix . 'tbl_msg_participant p ' . + ' JOIN ' . $this->db->dbprefix . 'tbl_msg_thread t ON (t.thread_id = p.thread_id) ' . + ' JOIN ' . $this->db->dbprefix . 'tbl_msg_message m ON (m.thread_id = t.thread_id) ' . + ' JOIN ' . $this->db->dbprefix . 'public.tbl_person' . ' ON (' . 'tbl_person.person_id' . ' = m.sender_id) '. + ' JOIN ' . $this->db->dbprefix . 'tbl_msg_status s ON (s.message_id = m.message_id AND s.person_id = ? ) ' . + ' WHERE p.person_id = ? ' ; + if (!$full_thread) + { + $sql .= ' AND m.cdate >= p.cdate'; + } + $sql .= ' ORDER BY t.thread_id ' . $order_by. ', m.cdate '. $order_by; + $result = $this->db->query($sql, array($person_id, $person_id)); + if ($result) + return $this->_success($result->result_array()); + else + return $this->_general_error(); + } + /** ----------------------------------------------------------------- + * Get all Threads Grouped + * get_all_threads_grouped() - will return all threads for user, including the status for specified user. + * - messages are grouped in threads. + * + * @param integer $person_id REQUIRED + * @param boolean $full_thread OPTIONAL - If true, user will also see messages from thread posted BEFORE user became participant + * @param string $order_by OPTIONAL + * @return array + */ + function get_all_threads_grouped($person_id, $full_thread = FALSE, $order_by = 'ASC') + { + if (empty($person_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_USER_ID); + } + $message = $this->get_all_threads($person_id, $full_thread, $order_by); + if (is_array($message)) + { + $threads = array(); + foreach ($message as $msg) + { + if ( ! isset($threads[$msg['thread_id']])) + { + $threads[$msg['thread_id']]['thread_id'] = $msg['thread_id']; + $threads[$msg['thread_id']]['messages'] = array($msg); + } + else + { + $threads[$msg['thread_id']]['messages'][] = $msg; + } + } + return $this->_success($threads); + } + // General Error Occurred + return $this->_general_error(); + } + /** ----------------------------------------------------------------- + * Change Message Status + * update_message_status() - will change status on message for particular user + * + * @param integer $msg_id REQUIRED + * @param integer $person_id REQUIRED + * @param integer $status_id REQUIRED - should come from config/message.php list of constants + * @return array + */ + function update_message_status($msg_id, $person_id, $status_id) + { + // Validate + if (empty($msg_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_MSG_ID); + } + if (empty($person_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_USER_ID); + } + if (empty($status_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_STATUS_ID); + } + $this->db->where(array('message_id' => $msg_id, 'person_id' => $person_id )); + $this->db->update('tbl_msg_status', array('status' => $status_id )); + $rows = $this->db->affected_rows(); + if ($rows == 1) + return $this->_success($rows, MSG_STATUS_UPDATE); + else + return $this->_general_error(); + } + /** ----------------------------------------------------------------- + * Add a Participant + * add_participant() - adds user to existing thread + * + * @param integer $thread_id REQUIRED + * @param integer $person_id REQUIRED + * @return array + */ + function addParticipant($thread_id, $person_id) + { + + if (empty($thread_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID); + } + if (empty($person_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_USER_ID); + } + if ( ! $this->valid_new_participant($thread_id, $person_id)) + { + $this->_participant_error(MSG_ERR_PARTICIPANT_EXISTS); + } + if ( ! $this->application_user($person_id)) + { + $this->_participant_error(MSG_ERR_PARTICIPANT_NONSYSTEM); + } + $this->db->trans_start(); + $participants[] = array('thread_id' => $thread_id,'person_id' => $person_id); + $this->_insert_participants($participants); + // Get Messages by Thread + $messages = $this->_get_messages_by_thread_id($thread_id); + foreach ($messages as $message) + { + $statuses[] = array('message_id' => $message['message_id'], 'person_id' => $person_id, 'status' => MSG_STATUS_UNREAD); + } + $this->_insert_statuses($statuses); + $this->db->trans_complete(); + if ($this->db->trans_status() === FALSE) + { + $this->db->trans_rollback(); + return $this->_general_error(); + } + return $this->_success(NULL, MSG_PARTICIPANT_ADDED); + } + /** --------------------------------------------------------------- + * Remove a Participant + * remove_participant() - removes user from existing thread + * + * @param integer $thread_id REQUIRED + * @param integer $person_id REQUIRED + * @return array + */ + function remove_participant($thread_id, $person_id) + { + // Validate + if (empty($thread_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID); + } + if (empty($person_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_USER_ID); + } + $this->db->trans_start(); + $this->_delete_participant($thread_id, $person_id); + $this->_delete_statuses($thread_id, $person_id); + $this->db->trans_complete(); + if ($this->db->trans_status() === FALSE) + { + $this->db->trans_rollback(); + return $this->_success(NULL, MSG_PARTICIPANT_REMOVED); + } + return $this->_general_error(); + } + /** ---------------------------------------------------------------- + * Send a New Message + * send_new_message() - sends new internal message. This function will create a new thread + * + * @param integer $sender_id REQUIRED + * @param mixed $recipients REQUIRED - a single integer or an array of integers, representing person_ids + * @param string $subject + * @param string $body + * @param integer $priority + * @return array + */ + function send_new_message($sender_id, $recipients, $subject, $body, $priority) + { + // Validate + if (empty($sender_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_SENDER_ID); + } + if (empty($recipients)) + { + return array( + 'err' => 1, + 'code' => MSG_ERR_INVALID_RECIPIENTS, + 'msg' => lang('mahana_'.MSG_ERR_INVALID_RECIPIENTS) + ); + } + $this->db->trans_start(); + $thread_id = $this->_insert_thread($subject); + $msg_id = $this->_insert_message($thread_id, $sender_id, $body, $priority); + // Create batch inserts + $participants[] = array('thread_id' => $thread_id,'person_id' => $sender_id); + $statuses[] = array('message_id' => $msg_id, 'person_id' => $sender_id,'status' => MSG_STATUS_READ); + if ( ! is_array($recipients) ) + { + if ($sender_id != $recipients) + { + $participants[] = array('thread_id' => $thread_id,'person_id' => $recipients); + $statuses[] = array('message_id' => $msg_id, 'person_id' => $recipients, 'status' => MSG_STATUS_UNREAD); + } + } + else + { + foreach ($recipients as $recipient) + { + if ($sender_id != $recipient) + { + $participants[] = array('thread_id' => $thread_id,'person_id' => $recipient); + $statuses[] = array('message_id' => $msg_id, 'person_id' => $recipient, 'status' => MSG_STATUS_UNREAD); + } + } + } + $participants=array_unique($participants, SORT_REGULAR); // Clean if sender and recipient is the same + $this->_insert_participants($participants); + $this->_insert_statuses($statuses); + $this->db->trans_complete(); + if ($this->db->trans_status() === FALSE) + { + $this->db->trans_rollback(); + return $this->_general_error(); + } + return $this->_success($thread_id, MSG_MESSAGE_SENT); + } + /** -------------------------------------------------------------- + * Reply to Message + * reply_to_message() - replies to internal message. This function will NOT create a new thread or participant list + * + * @param integer $msg_id REQUIRED + * @param integer $sender_id REQUIRED + * @param string $subject + * @param string $body + * @param integer $priority + * @return array + */ + function reply_to_message($reply_msg_id, $sender_id, $body, $priority) + { + // Validate + if (empty($sender_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_SENDER_ID); + } + if (empty($msg_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_MSG_ID); + } + $this->db->trans_start(); + // Get the thread id to keep messages together + if ( ! $thread_id = $this->_get_thread_id_from_message($reply_msg_id)) + { + return FALSE; + } + // Add this message + $msg_id = $this->_insert_message($thread_id, $sender_id, $body, $priority); + if ($recipients = $this->_get_thread_participants($thread_id, $sender_id)) + { + $statuses[] = array('message_id' => $msg_id, 'person_id' => $sender_id,'status' => MSG_STATUS_READ); + foreach ($recipients as $recipient) + { + $statuses[] = array('message_id' => $msg_id, 'person_id' => $recipient['person_id'], 'status' => MSG_STATUS_UNREAD); + } + $this->_insert_statuses($statuses); + } + $this->db->trans_complete(); + if ($this->db->trans_status() === FALSE) + { + $this->db->trans_rollback(); + return $this->_general_error(); + } + return $this->_success($msg_id, MSG_MESSAGE_SENT); + } + /** ---------------------------------------------------------------- + * Get Participant List + * get_participant_list() - returns list of participants on given thread. If sender_id set, sender_id will be left off list + * + * @param integer $thread_id REQUIRED + * @param integer $sender_id REQUIRED + * @return array + */ + function get_participant_list($thread_id, $sender_id = 0) + { + // Validate + if (empty($thread_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID); + } + + if ($results = $this->_get_thread_participants($thread_id, $sender_id)) + return $this->_success($results); + else + return $this->_general_error(); + } + /** ---------------------------------------------------------------- + * Get Message Count + * get_msg_count() - returns integer with count of message for user, by status. defaults to new messages + * + * @param integer $person_id REQUIRED + * @param integer $status_id OPTIONAL - defaults to "Unread" + * @return array + */ + function get_msg_count($person_id, $status_id = MSG_STATUS_UNREAD) + { + if (empty($person_id)) + { + return $this->_invalid_id(MSG_ERR_INVALID_USER_ID); + } + $result = $this->db->select('COUNT(*) AS msg_count')->where(array('person_id' => $person_id, 'status' => $status_id ))->get('tbl_msg_status'); + $rows = $result->row()->msg_count; + + if (is_numeric($rows)) + return $this->_success($rows); + else + return $this->_general_error(); + } + // ------------------------------------------------------------------------ + /** + * Valid New Participant - because of CodeIgniter's DB Class return style, + * it is safer to check for uniqueness first + * + * @param integer $thread_id + * @param integer $person_id + * @return boolean + */ + function valid_new_participant($thread_id, $person_id) + { + $sql = 'SELECT COUNT(*) AS count ' . + ' FROM ' . $this->db->dbprefix . 'tbl_msg_participant p ' . + ' WHERE p.thread_id = ? ' . + ' AND p.person_id = ? '; + $query = $this->db->query($sql, array($thread_id, $person_id)); + if ($query->row()->count) + { + return FALSE; + } + return TRUE; + } - - - - - - - - - - - - - - FHC-Core/Message_model.php at ci · FH-Complete/FHC-Core - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Skip to content - - - - - - - - - - - - - -
- -
-
- - -
-
-
- -
-
- - - -
    - -
  • -
    - -
    - - - - Unwatch - - - - -
    - -
    -
    -
    -
  • - -
  • - -
    - -
    - - -
    -
    - - -
    - -
  • - -
  • - - - Fork - - - - - -
  • -
- -

- - /FHC-Core - -

- -
- -
- -
-
- - - - - - - -
+ /** --------------------------------------------------------------- + * Application User + * + * @param integer $person_id` + * @return boolean + */ + function application_user($person_id) + { + $sql = 'SELECT COUNT(*) AS count ' . + ' FROM ' . $this->db->dbprefix . 'public.tbl_person' . + ' WHERE ' . 'tbl_person.person_id' . ' = ?' ; + $query = $this->db->query($sql, array($person_id)); + if ($query->row()->count) + { + return TRUE; + } + return FALSE; + } -
- - - -
- -
- - Find file - - -
- -
- - -
- - - 3b39752 - - - - - -
- - -
- - -
- -
-
-
- -
- Raw - Blame - History -
- - - - - -
- -
- -
- -
- 704 lines (589 sloc) - - 21.1 KB -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Message_model extends DB_Model
{
public function __construct()
{
parent::__construct();
require_once APPPATH.'config/message.php';
//$this->load->helper('language');
$this->lang->load('message');
}
/** -----------------------------------------------------------------
* get_message() - will return a single message, including the status for specified user.
*
* @param integer $msg_id EQUIRED
* @param integer $user_id REQUIRED
* @return array
*/
function getMessage($msg_id, $user_id)
{
// Validate
if (empty($msg_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_MSG_ID);
}
if (empty($user_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
}
$sql = 'SELECT m.*, s.status, t.subject, ' . USER_TABLE_USERNAME .
' FROM ' . $this->db->dbprefix . 'msg_messages m ' .
' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (m.thread_id = t.id) ' .
' JOIN ' . $this->db->dbprefix . USER_TABLE_TABLENAME . ' ON (' . USER_TABLE_ID . ' = m.sender_id) '.
' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
' WHERE m.id = ? ' ;
$result = $this->db->query($sql, array($user_id, $msg_id));
if ($result)
return $this->_success($result->result_array());
else
return $this->_general_error();
}
/** -----------------------------------------------------------------
* Get a Full Thread
* get_full_thread() - will return a entire thread, including the status for specified user.
*
* @param integer $thread_id REQUIRED
* @param integer $user_id REQUIRED
* @param boolean $full_thread OPTIONAL - If true, user will also see messages from thread posted BEFORE user became participant
* @param string $order_by OPTIONAL
* @return array
*/
function get_full_thread($thread_id, $user_id, $full_thread = FALSE, $order_by = 'ASC')
{
// Validate
if (empty($thread_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID);
}
if (empty($user_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
}
$sql = 'SELECT m.*, s.status, t.subject, '.USER_TABLE_USERNAME .
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (t.id = p.thread_id) ' .
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.thread_id = t.id) ' .
' JOIN ' . $this->db->dbprefix . USER_TABLE_TABLENAME . ' ON (' . USER_TABLE_ID . ' = m.sender_id) '.
' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
' WHERE p.user_id = ? ' .
' AND p.thread_id = ? ';
if ( ! $full_thread)
{
$sql .= ' AND m.cdate >= p.cdate';
}
$sql .= ' ORDER BY m.cdate ' . $order_by;
$result = $this->db->query($sql, array($user_id, $user_id, $thread_id));
if ($result)
return $this->_success($result->result_array());
else
return $this->_general_error();
}
/** -----------------------------------------------------------------
* get_all_threads() - will return all threads for user, including the status for specified user.
*
* @param integer $user_id REQUIRED
* @param boolean $full_thread OPTIONAL - If true, user will also see messages from thread posted BEFORE user became participant
* @param string $order_by OPTIONAL
* @return array
*/
function get_all_threads($user_id, $full_thread = FALSE, $order_by = 'asc')
{
if (empty($user_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
}
$sql = 'SELECT m.*, s.status, t.subject, '.USER_TABLE_USERNAME .
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (t.id = p.thread_id) ' .
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.thread_id = t.id) ' .
' JOIN ' . $this->db->dbprefix . USER_TABLE_TABLENAME . ' ON (' . USER_TABLE_ID . ' = m.sender_id) '.
' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
' WHERE p.user_id = ? ' ;
if (!$full_thread)
{
$sql .= ' AND m.cdate >= p.cdate';
}
$sql .= ' ORDER BY t.id ' . $order_by. ', m.cdate '. $order_by;
$result = $this->db->query($sql, array($user_id, $user_id));
if ($result)
return $this->_success($result->result_array());
else
return $this->_general_error();
}
/** -----------------------------------------------------------------
* Get all Threads Grouped
* get_all_threads_grouped() - will return all threads for user, including the status for specified user.
* - messages are grouped in threads.
*
* @param integer $user_id REQUIRED
* @param boolean $full_thread OPTIONAL - If true, user will also see messages from thread posted BEFORE user became participant
* @param string $order_by OPTIONAL
* @return array
*/
function get_all_threads_grouped($user_id, $full_thread = FALSE, $order_by = 'ASC')
{
if (empty($user_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
}
$message = $this->get_all_threads($user_id, $full_thread, $order_by);
if (is_array($message))
{
$threads = array();
foreach ($message as $msg)
{
if ( ! isset($threads[$msg['thread_id']]))
{
$threads[$msg['thread_id']]['thread_id'] = $msg['thread_id'];
$threads[$msg['thread_id']]['messages'] = array($msg);
}
else
{
$threads[$msg['thread_id']]['messages'][] = $msg;
}
}
return $this->_success($threads);
}
// General Error Occurred
return $this->_general_error();
}
/** -----------------------------------------------------------------
* Change Message Status
* update_message_status() - will change status on message for particular user
*
* @param integer $msg_id REQUIRED
* @param integer $user_id REQUIRED
* @param integer $status_id REQUIRED - should come from config/message.php list of constants
* @return array
*/
function update_message_status($msg_id, $user_id, $status_id)
{
// Validate
if (empty($msg_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_MSG_ID);
}
if (empty($user_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
}
if (empty($status_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_STATUS_ID);
}
$this->db->where(array('message_id' => $msg_id, 'user_id' => $user_id ));
$this->db->update('msg_status', array('status' => $status_id ));
$rows = $this->db->affected_rows();
if ($rows == 1)
return $this->_success($rows, MSG_STATUS_UPDATE);
else
return $this->_general_error();
}
/** -----------------------------------------------------------------
* Add a Participant
* add_participant() - adds user to existing thread
*
* @param integer $thread_id REQUIRED
* @param integer $user_id REQUIRED
* @return array
*/
function add_participant($thread_id, $user_id)
{
if (empty($thread_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID);
}
if (empty($user_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
}
if ( ! $this->valid_new_participant($thread_id, $user_id))
{
$this->_participant_error(MSG_ERR_PARTICIPANT_EXISTS);
}
if ( ! $this->application_user($user_id))
{
$this->_participant_error(MSG_ERR_PARTICIPANT_NONSYSTEM);
}
$this->db->trans_start();
$participants[] = array('thread_id' => $thread_id,'user_id' => $user_id);
$this->_insert_participants($participants);
// Get Messages by Thread
$messages = $this->_get_messages_by_thread_id($thread_id);
foreach ($messages as $message)
{
$statuses[] = array('message_id' => $message['id'], 'user_id' => $user_id, 'status' => MSG_STATUS_UNREAD);
}
$this->_insert_statuses($statuses);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return $this->_general_error();
}
return $this->_success(NULL, MSG_PARTICIPANT_ADDED);
}
/** ---------------------------------------------------------------
* Remove a Participant
* remove_participant() - removes user from existing thread
*
* @param integer $thread_id REQUIRED
* @param integer $user_id REQUIRED
* @return array
*/
function remove_participant($thread_id, $user_id)
{
// Validate
if (empty($thread_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID);
}
if (empty($user_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
}
$this->db->trans_start();
$this->_delete_participant($thread_id, $user_id);
$this->_delete_statuses($thread_id, $user_id);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return $this->_success(NULL, MSG_PARTICIPANT_REMOVED);
}
return $this->_general_error();
}
/** ----------------------------------------------------------------
* Send a New Message
* send_new_message() - sends new internal message. This function will create a new thread
*
* @param integer $sender_id REQUIRED
* @param mixed $recipients REQUIRED - a single integer or an array of integers, representing user_ids
* @param string $subject
* @param string $body
* @param integer $priority
* @return array
*/
function send_new_message($sender_id, $recipients, $subject, $body, $priority)
{
// Validate
if (empty($sender_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_SENDER_ID);
}
if (empty($recipients))
{
return array(
'err' => 1,
'code' => MSG_ERR_INVALID_RECIPIENTS,
'msg' => lang('mahana_'.MSG_ERR_INVALID_RECIPIENTS)
);
}
$this->db->trans_start();
$thread_id = $this->_insert_thread($subject);
$msg_id = $this->_insert_message($thread_id, $sender_id, $body, $priority);
// Create batch inserts
$participants[] = array('thread_id' => $thread_id,'user_id' => $sender_id);
$statuses[] = array('message_id' => $msg_id, 'user_id' => $sender_id,'status' => MSG_STATUS_READ);
if ( ! is_array($recipients) )
{
if ($sender_id != $recipients)
{
$participants[] = array('thread_id' => $thread_id,'user_id' => $recipients);
$statuses[] = array('message_id' => $msg_id, 'user_id' => $recipients, 'status' => MSG_STATUS_UNREAD);
}
}
else
{
foreach ($recipients as $recipient)
{
if ($sender_id != $recipient)
{
$participants[] = array('thread_id' => $thread_id,'user_id' => $recipient);
$statuses[] = array('message_id' => $msg_id, 'user_id' => $recipient, 'status' => MSG_STATUS_UNREAD);
}
}
}
$participants=array_unique($participants, SORT_REGULAR); // Clean if sender and recipient is the same
$this->_insert_participants($participants);
$this->_insert_statuses($statuses);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return $this->_general_error();
}
return $this->_success($thread_id, MSG_MESSAGE_SENT);
}
/** --------------------------------------------------------------
* Reply to Message
* reply_to_message() - replies to internal message. This function will NOT create a new thread or participant list
*
* @param integer $msg_id REQUIRED
* @param integer $sender_id REQUIRED
* @param string $subject
* @param string $body
* @param integer $priority
* @return array
*/
function reply_to_message($reply_msg_id, $sender_id, $body, $priority)
{
// Validate
if (empty($sender_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_SENDER_ID);
}
if (empty($msg_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_MSG_ID);
}
$this->db->trans_start();
// Get the thread id to keep messages together
if ( ! $thread_id = $this->_get_thread_id_from_message($reply_msg_id))
{
return FALSE;
}
// Add this message
$msg_id = $this->_insert_message($thread_id, $sender_id, $body, $priority);
if ($recipients = $this->_get_thread_participants($thread_id, $sender_id))
{
$statuses[] = array('message_id' => $msg_id, 'user_id' => $sender_id,'status' => MSG_STATUS_READ);
foreach ($recipients as $recipient)
{
$statuses[] = array('message_id' => $msg_id, 'user_id' => $recipient['user_id'], 'status' => MSG_STATUS_UNREAD);
}
$this->_insert_statuses($statuses);
}
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return $this->_general_error();
}
return $this->_success($msg_id, MSG_MESSAGE_SENT);
}
/** ----------------------------------------------------------------
* Get Participant List
* get_participant_list() - returns list of participants on given thread. If sender_id set, sender_id will be left off list
*
* @param integer $thread_id REQUIRED
* @param integer $sender_id REQUIRED
* @return array
*/
function get_participant_list($thread_id, $sender_id = 0)
{
// Validate
if (empty($thread_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID);
}
if ($results = $this->_get_thread_participants($thread_id, $sender_id))
return $this->_success($results);
else
return $this->_general_error();
}
/** ----------------------------------------------------------------
* Get Message Count
* get_msg_count() - returns integer with count of message for user, by status. defaults to new messages
*
* @param integer $user_id REQUIRED
* @param integer $status_id OPTIONAL - defaults to "Unread"
* @return array
*/
function get_msg_count($user_id, $status_id = MSG_STATUS_UNREAD)
{
if (empty($user_id))
{
return $this->_invalid_id(MSG_ERR_INVALID_USER_ID);
}
$result = $this->db->select('COUNT(*) AS msg_count')->where(array('user_id' => $user_id, 'status' => $status_id ))->get('msg_status');
$rows = $result->row()->msg_count;
if (is_numeric($rows))
return $this->_success($rows);
else
return $this->_general_error();
}
// ------------------------------------------------------------------------
/**
* Valid New Participant - because of CodeIgniter's DB Class return style,
* it is safer to check for uniqueness first
*
* @param integer $thread_id
* @param integer $user_id
* @return boolean
*/
function valid_new_participant($thread_id, $user_id)
{
$sql = 'SELECT COUNT(*) AS count ' .
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
' WHERE p.thread_id = ? ' .
' AND p.user_id = ? ';
$query = $this->db->query($sql, array($thread_id, $user_id));
if ($query->row()->count)
{
return FALSE;
}
return TRUE;
}
/** ---------------------------------------------------------------
* Application User
*
* @param integer $user_id`
* @return boolean
*/
function application_user($user_id)
{
$sql = 'SELECT COUNT(*) AS count ' .
' FROM ' . $this->db->dbprefix . USER_TABLE_TABLENAME .
' WHERE ' . USER_TABLE_ID . ' = ?' ;
$query = $this->db->query($sql, array($user_id));
if ($query->row()->count)
{
return TRUE;
}
return FALSE;
}
// ------------------------------------------------------------------------
// Private Functions from here out!
// ------------------------------------------------------------------------
/**
* Insert Thread
*
* @param string $subject
* @return integer
*/
private function _insert_thread($subject)
{
$insert_id = $this->db->insert('msg_threads', array('subject' => $subject));
return $this->db->insert_id();
}
/**
* Insert Message
*
* @param integer $thread_id
* @param integer $sender_id
* @param string $body
* @param integer $priority
* @return integer
*/
private function _insert_message($thread_id, $sender_id, $body, $priority)
{
$insert['thread_id'] = $thread_id;
$insert['sender_id'] = $sender_id;
$insert['body'] = $body;
$insert['priority'] = $priority;
$insert_id = $this->db->insert('msg_messages', $insert);
return $this->db->insert_id();
}
/**
* Insert Participants
*
* @param array $participants
* @return bool
*/
private function _insert_participants($participants)
{
return $this->db->insert_batch('msg_participants', $participants);
}
/**
* Insert Statuses
*
* @param array $statuses
* @return bool
*/
private function _insert_statuses($statuses)
{
return $this->db->insert_batch('msg_status', $statuses);
}
/**
* Get Thread ID from Message
*
* @param integer $msg_id
* @return integer
*/
private function _get_thread_id_from_message($msg_id)
{
$query = $this->db->select('thread_id')->get_where('msg_messages', array('id' => $msg_id));
if ($query->num_rows())
{
return $query->row()->thread_id;
}
return 0;
}
/**
* Get Messages by Thread
*
* @param integer $thread_id
* @return array
*/
private function _get_messages_by_thread_id($thread_id)
{
$query = $this->db->get_where('msg_messages', array('thread_id' => $thread_id));
return $query->result_array();
}
/**
* Get Thread Particpiants
*
* @param integer $thread_id
* @param integer $sender_id
* @return array
*/
private function _get_thread_participants($thread_id, $sender_id = 0)
{
$array['thread_id'] = $thread_id;
if ($sender_id) // If $sender_id 0, no one to exclude
{
$array['msg_participants.user_id != '] = $sender_id;
}
$this->db->select('msg_participants.user_id, '.USER_TABLE_USERNAME, FALSE);
$this->db->join(USER_TABLE_TABLENAME, 'msg_participants.user_id = ' . USER_TABLE_ID);
$query = $this->db->get_where('msg_participants', $array);
return $query->result_array();
}
/**
* Delete Participant
*
* @param integer $thread_id
* @param integer $user_id
* @return boolean
*/
private function _delete_participant($thread_id, $user_id)
{
$this->db->delete('msg_participants', array('thread_id' => $thread_id, 'user_id' => $user_id));
if ($this->db->affected_rows() > 0)
{
return TRUE;
}
return FALSE;
}
/**
* Delete Statuses
*
* @param integer $thread_id
* @param integer $user_id
* @return boolean
*/
private function _delete_statuses($thread_id, $user_id)
{
$sql = 'DELETE s FROM msg_status s ' .
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.id = s.message_id) ' .
' WHERE m.thread_id = ? ' .
' AND s.user_id = ? ';
$query = $this->db->query($sql, array($thread_id, $user_id));
return TRUE;
}
/** ---------------------------------------------------------------
* Error Particpant Exists
*
* @return array
*/
private function _participant_error($error = '')
{
return array(
'err' => 1,
'code' => 1,
'msg' => lang('mahana_' . $error)
);
}
}
/* end of file message_model.php */
- -
- -
- - - - -
- -
- - -
-
- -
- - - - - - - - -
- - - Something went wrong with that request. Please try again. -
- - - - - - - - - - - - - - - - + // ------------------------------------------------------------------------ + // Private Functions from here out! + // ------------------------------------------------------------------------ + /** + * Insert Thread + * + * @param string $subject + * @return integer + */ + private function _insert_thread($subject) + { + $insert_id = $this->db->insert('tbl_msg_thread', array('subject' => $subject)); + return $this->db->insert_id(); + } + /** + * Insert Message + * + * @param integer $thread_id + * @param integer $sender_id + * @param string $body + * @param integer $priority + * @return integer + */ + private function _insert_message($thread_id, $sender_id, $body, $priority) + { + $insert['thread_id'] = $thread_id; + $insert['sender_id'] = $sender_id; + $insert['body'] = $body; + $insert['priority'] = $priority; + $insert_id = $this->db->insert('tbl_msg_message', $insert); + return $this->db->insert_id(); + } + /** + * Insert Participants + * + * @param array $participants + * @return bool + */ + private function _insert_participants($participants) + { + return $this->db->insert_batch('tbl_msg_participant', $participants); + } + /** + * Insert Statuses + * + * @param array $statuses + * @return bool + */ + private function _insert_statuses($statuses) + { + return $this->db->insert_batch('tbl_msg_status', $statuses); + } + /** + * Get Thread ID from Message + * + * @param integer $msg_id + * @return integer + */ + private function _get_thread_id_from_message($msg_id) + { + $query = $this->db->select('thread_id')->get_where('tbl_msg_message', array('id' => $msg_id)); + if ($query->num_rows()) + { + return $query->row()->thread_id; + } + return 0; + } + /** + * Get Messages by Thread + * + * @param integer $thread_id + * @return array + */ + private function _get_messages_by_thread_id($thread_id) + { + $query = $this->db->get_where('tbl_msg_message', array('thread_id' => $thread_id)); + return $query->result_array(); + } + /** + * Get Thread Particpiants + * + * @param integer $thread_id + * @param integer $sender_id + * @return array + */ + private function _get_thread_participants($thread_id, $sender_id = 0) + { + $array['thread_id'] = $thread_id; + if ($sender_id) // If $sender_id 0, no one to exclude + { + $array['tbl_msg_participant.person_id != '] = $sender_id; + } + $this->db->select('tbl_msg_participant.person_id, '."CONCAT(vorname, ' ', nachname) as user_name", FALSE); + $this->db->join('public.tbl_person', 'tbl_msg_participant.person_id = ' . 'tbl_person.person_id'); + $query = $this->db->get_where('tbl_msg_participant', $array); + return $query->result_array(); + } + /** + * Delete Participant + * + * @param integer $thread_id + * @param integer $person_id + * @return boolean + */ + private function _delete_participant($thread_id, $person_id) + { + $this->db->delete('tbl_msg_participant', array('thread_id' => $thread_id, 'person_id' => $person_id)); + if ($this->db->affected_rows() > 0) + { + return TRUE; + } + return FALSE; + } + /** + * Delete Statuses + * + * @param integer $thread_id + * @param integer $person_id + * @return boolean + */ + private function _delete_statuses($thread_id, $person_id) + { + $sql = 'DELETE s FROM tbl_msg_status s ' . + ' JOIN ' . $this->db->dbprefix . 'tbl_msg_message m ON (m.message_id = s.message_id) ' . + ' WHERE m.thread_id = ? ' . + ' AND s.person_id = ? '; + $query = $this->db->query($sql, array($thread_id, $person_id)); + return TRUE; + } + /** --------------------------------------------------------------- + * Error Particpant Exists + * + * @return array + */ + private function _participant_error($error = '') + { + return array( + 'err' => 1, + 'code' => 1, + 'msg' => lang('mahana_' . $error) + ); + } +} +/* end of file message_model.php */ diff --git a/application/models/system/Thread_model.php b/application/models/system/Thread_model.php new file mode 100644 index 000000000..f5f1ee3ca --- /dev/null +++ b/application/models/system/Thread_model.php @@ -0,0 +1,14 @@ +dbTable = 'public.tbl_msg_thread'; + $this->pk = 'thread_id'; + } +} diff --git a/include/studiengang.class.php b/include/studiengang.class.php index f2c8069f2..dfc433110 100755 --- a/include/studiengang.class.php +++ b/include/studiengang.class.php @@ -31,7 +31,7 @@ if (function_exists('get_instance')) else require_once(dirname(__FILE__).'/../ci_hack.php'); -require_once(dirname(__FILE__).'/../application/models/lehre/Studiengang_model.php'); +require_once(dirname(__FILE__).'/../application/models/organisation/Studiengang_model.php'); class studiengang extends Studiengang_model {