From e2fc4cd6eb3864dbdcf2474e7164c6a7c50488bf Mon Sep 17 00:00:00 2001 From: AdwinTrave Date: Sun, 22 Jun 2014 12:25:34 +0200 Subject: [PATCH] Adding and removing participants from messaging Right now anyone can add more people to thread. If there are more than 2 people on a thread user can remove themselves from the thread. --- application/controllers/pm/Message.php | 89 ++++++++++++++++++ application/controllers/pm/Overview.php | 2 +- application/language/english/pm/pm_lang.php | 8 +- application/libraries/pm/Mahana_messaging.php | 4 +- application/models/pm/Mahana_model.php | 10 +- application/views/forums/talk.php | 38 -------- application/views/pm/message.php | 35 ++++--- system/core/compat/password.php | 5 +- system/libraries/Encryption.php | 15 +++ .../build/doctrees/environment.pickle | Bin 525141 -> 526941 bytes .../doctrees/libraries/encryption.doctree | Bin 158494 -> 159634 bytes user_guide_src/build/html/genindex.html | 2 +- user_guide_src/build/html/index.html | 2 +- .../build/html/libraries/encryption.html | 17 ++-- .../build/html/libraries/index.html | 2 +- user_guide_src/build/html/objects.inv | Bin 4600 -> 4635 bytes user_guide_src/build/html/search.html | 2 +- user_guide_src/build/html/searchindex.js | 2 +- .../source/libraries/encryption.rst | 15 ++- 19 files changed, 175 insertions(+), 73 deletions(-) diff --git a/application/controllers/pm/Message.php b/application/controllers/pm/Message.php index ed190f8..9108865 100644 --- a/application/controllers/pm/Message.php +++ b/application/controllers/pm/Message.php @@ -101,6 +101,95 @@ function index($id = NULL) $data['content'] = $this->load->view('pm/message', isset($data) ? $data : NULL, TRUE); $this->load->view('template', $data); } + + /** + * Add participant to a thread + * @param int $id Thread ID + */ + public function add_participant($id) + { + $data = $this->authentication->initialize(TRUE, 'pm/message/add_participant/'.$id, NULL, 'msg_use'); + + //check that the user can do this + $participants = $this->mahana_messaging->get_participant_list($id)['retval']; + $allowed = FALSE; + foreach($participants AS $participant) + { + if($participant['user_id'] == $data['account']->id) + { + $allowed = TRUE; + } + } + + if($allowed == FALSE) + { + $this->session->set_flashdata(array('message' => lang('pm_action_not_allowed'), 'message_type' => 'danger')); + redirect(base_url('pm/message/'.$id)); + } + + $this->form_validation->set_rules('msg-add-participants', 'lang:pm_participant_wrong', 'required|trim|alpha_dash|xss_clean'); + if($this->form_validation->run()) + { + $users = $this->input->post('msg-add-participants', TRUE); + + //now separate users by ',' into array + $user_array = explode(',', trim($users)); + $user_ids = $this->mahana_messaging->usernames_to_ids($user_array); + if($user_ids) + { + foreach($user_ids AS $user) + { + $this->mahana_messaging->add_participant($id, $user); + } + } + else + { + $this->session->set_flashdata(array('message' => lang('pm_participant_wrong'), 'message_type' => 'danger')); + } + } + else + { + $this->session->set_flashdata(array('message' => lang('pm_participant_wrong'), 'message_type' => 'danger')); + } + redirect(base_url('pm/message/'.$id)); + } + + /** + * Remove a participant from a thread + * @param int $id Thread ID + * @param int $user User ID + */ + public function remove_participant($id, $user) + { + $data = $this->authentication->initialize(TRUE, 'pm/message/remove_participant/'.$id.'/'.$user, NULL, 'msg_use'); + + //check that the user can do this + if($data['account']->id != $user) + { + $this->session->set_flashdata(array('message' => lang('pm_action_not_allowed'), 'message_type' => 'danger')); + redirect(base_url('pm/message/'.$id)); + } + + $error = $this->mahana_messaging->remove_participant($id, $user); + if($error['err'] == 1) + { + $this->session->set_flashdata(array('message' => $error['msg'], 'message_type' => 'danger')); + } + elseif($error['err'] == 0) + { + $this->session->set_flashdata(array('message' => $error['msg'], 'message_type' => 'success')); + } + + if($data['account']->id == $user) + { + redirect(base_url('pm')); + } + else + { + redirect(base_url('pm/message/'.$id)); + } + + } } /* End of file Message.php */ /* Location: ./application/controllers/pm/Message.php */ \ No newline at end of file diff --git a/application/controllers/pm/Overview.php b/application/controllers/pm/Overview.php index 7e8d90f..f2a92d9 100644 --- a/application/controllers/pm/Overview.php +++ b/application/controllers/pm/Overview.php @@ -72,7 +72,7 @@ function index() $text = $this->input->post('msg-text', TRUE); //convert usernames to ids - //@todo check for multiple recipients and convert to array if needed + $recipients = explode(',', $recipients); $recipients = $this->mahana_messaging->usernames_to_ids($recipients); //submit diff --git a/application/language/english/pm/pm_lang.php b/application/language/english/pm/pm_lang.php index f4d7603..b01b2cc 100644 --- a/application/language/english/pm/pm_lang.php +++ b/application/language/english/pm/pm_lang.php @@ -14,4 +14,10 @@ $lang['pm_from_you'] = "You"; $lang['pm_participants'] = "Participants"; $lang['pm_participant_add'] = "Add participant"; -$lang['pm_reply'] = "Reply"; \ No newline at end of file +$lang['pm_reply'] = "Reply"; +$lang['pm_participant_wrong'] = "This user does not exists."; +$lang['pm_add_participants'] = "Add users to this converstaion"; +$lang['pm_add_users'] = "Add users"; +$lang['pm_remove_user'] = "Remove this user from this conversation"; +$lang['pm_remove_participant'] = "Remove from this conversation"; +$lang['pm_action_not_allowed'] = "You are not allowed to perform this action."; \ No newline at end of file diff --git a/application/libraries/pm/Mahana_messaging.php b/application/libraries/pm/Mahana_messaging.php index 4cb5f19..752e261 100644 --- a/application/libraries/pm/Mahana_messaging.php +++ b/application/libraries/pm/Mahana_messaging.php @@ -363,7 +363,7 @@ function get_participant_list($thread_id, $sender_id = 0) return $this->_invalid_id(MSG_ERR_INVALID_THREAD_ID); } - if ($participants = $this->ci->Mahana_model-> get_participant_list($thread_id, $sender_id)) + if ($participants = $this->ci->Mahana_model->get_participant_list($thread_id, $sender_id)) { return $this->_success($participants); } @@ -416,7 +416,7 @@ function usernames_to_ids($usernames) $user = $this->ci->Account_model->get_by_username($username); if(isset($user->id)) { - $return[$user->id]; + $return[$user->id] = $user->id; } } return $return; diff --git a/application/models/pm/Mahana_model.php b/application/models/pm/Mahana_model.php index 95e5bc7..6c13940 100644 --- a/application/models/pm/Mahana_model.php +++ b/application/models/pm/Mahana_model.php @@ -558,8 +558,14 @@ private function _delete_statuses($thread_id, $user_id) { $this->db->join($this->db->dbprefix . 'msg_messages', $this->db->dbprefix . 'msg_messages.id = '.$this->db->dbprefix . 'msg_status.message_id'); - $this->db->delete($this->db->dbprefix . 'msg_status', array($this->db->dbprefix .'msg_messages.thread_id' => $thread_id, $this->db->dbprefix . 'msg_status.user_id' => $user_id)); - + $list = $this->db->get_where($this->db->dbprefix . 'msg_status', array($this->db->dbprefix . 'msg_messages.thread_id' => $thread_id, $this->db->dbprefix . 'msg_status.user_id' => $user_id))->result(); + + foreach($list AS $item) + { + $this->db->where(array('message_id' => $item->message_id, 'user_id' => $item->user_id)); + } + $this->db->delete($this->db->dbprefix . 'msg_status'); + return TRUE; } } diff --git a/application/views/forums/talk.php b/application/views/forums/talk.php index f1a8a3a..3231590 100644 --- a/application/views/forums/talk.php +++ b/application/views/forums/talk.php @@ -34,44 +34,6 @@

title; ?>

-
post; ?>

diff --git a/application/views/pm/message.php b/application/views/pm/message.php index 78f94f6..2d919a6 100644 --- a/application/views/pm/message.php +++ b/application/views/pm/message.php @@ -1,9 +1,9 @@ -
+
-
+
" . lang('pm_reply') . ""; - echo form_open('', array('role' => 'form'), array('msg-subject' => $thread[0]['subject'], 'msg-reply-id' => $thread[0]['id'])); + echo form_open(uri_string(), array('role' => 'form'), array('msg-subject' => $thread[0]['subject'], 'msg-reply-id' => $thread[0]['id'])); echo form_label(lang('pm_text'), 'msg-text'); echo form_error('msg-text', '
', '
'); echo form_textarea(array('name' => 'msg-text', 'id' => 'msg-text', 'class' => 'form-control')); - echo form_submit(array('name' => 'msg-reply', 'class' => "btn btn-success pull-right"), lang('pm_msg_send')); + echo form_submit(array('name' => 'msg-reply', 'class' => "btn btn-lg btn-success pull-right"), lang('pm_msg_send')); echo form_close(); ?>
-
+

@@ -40,25 +40,36 @@ id) + //@todo need to figure out who is going to have the priviledge to remove people from 3+ debates + //everyone should have the option to remove themselves from 3+ debates + if(count($participants) > 2) { - $badge = NULL; + if($participant['user_id'] == $account->id) + { + $badge = anchor(base_url('pm/message/remove_participant/'.$thread[0]['thread_id'].'/'.$participant['user_id']), '', array('class' => 'badge pull-right')); + } + else + { + $badge = NULL; + } } else { - $badge = ''; + $badge = NULL; } - echo '
  • '. $badge . $participant['username'].'
  • '; + echo '
  • ' . $participant['username'] . ' ' . $badge .'
  • '; } ?>