Merge branch 'wip-mdl-32759-m22' of git://github.com/rajeshtaneja/moodle into MOODLE_...
[moodle.git] / message / index.php
blob5dd251f1d4a7aa8e2a81b909db5bd7ed6254a33c
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * A page displaying the user's contacts and messages
21 * @package moodlecore
22 * @copyright 2010 Andrew Davis
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../config.php');
27 require_once('lib.php');
28 require_once('send_form.php');
30 require_login(0, false);
32 if (isguestuser()) {
33 redirect($CFG->wwwroot);
36 if (empty($CFG->messaging)) {
37 print_error('disabled', 'message');
40 //'viewing' is the preferred URL parameter but we'll still accept usergroup in case its referenced externally
41 $usergroup = optional_param('usergroup', MESSAGE_VIEW_UNREAD_MESSAGES, PARAM_ALPHANUMEXT);
42 $viewing = optional_param('viewing', $usergroup, PARAM_ALPHANUMEXT);
44 $history = optional_param('history', MESSAGE_HISTORY_SHORT, PARAM_INT);
45 $search = optional_param('search', '', PARAM_CLEAN); //TODO: use PARAM_RAW, but make sure we use s() and p() properly
47 //the same param as 1.9 and the param we have been logging. Use this parameter.
48 $user1id = optional_param('user1', $USER->id, PARAM_INT);
49 //2.0 shipped using this param. Retaining it only for compatibility. It should be removed.
50 $user1id = optional_param('user', $user1id, PARAM_INT);
52 //the same param as 1.9 and the param we have been logging. Use this parameter.
53 $user2id = optional_param('user2', 0, PARAM_INT);
54 //The class send_form supplies the receiving user id as 'id'
55 $user2id = optional_param('id', $user2id, PARAM_INT);
57 $addcontact = optional_param('addcontact', 0, PARAM_INT); // adding a contact
58 $removecontact = optional_param('removecontact', 0, PARAM_INT); // removing a contact
59 $blockcontact = optional_param('blockcontact', 0, PARAM_INT); // blocking a contact
60 $unblockcontact = optional_param('unblockcontact', 0, PARAM_INT); // unblocking a contact
62 //for search
63 $advancedsearch = optional_param('advanced', 0, PARAM_INT);
65 //if they have numerous contacts or are viewing course participants we might need to page through them
66 $page = optional_param('page', 0, PARAM_INT);
68 $url = new moodle_url('/message/index.php');
70 if ($user2id !== 0) {
71 $url->param('user2', $user2id);
74 if ($user2id !== 0) {
75 //Switch view back to contacts if:
76 //1) theyve searched and selected a user
77 //2) they've viewed recent messages or notifications and clicked through to a user
78 if ($viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) {
79 $viewing = MESSAGE_VIEW_CONTACTS;
82 $url->param('viewing', $viewing);
84 $PAGE->set_url($url);
86 $PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id));
87 $PAGE->navigation->extend_for_user($USER);
88 $PAGE->set_pagelayout('course');
90 // Disable message notification popups while the user is viewing their messages
91 $PAGE->set_popup_notification_allowed(false);
93 $context = get_context_instance(CONTEXT_SYSTEM);
95 $user1 = null;
96 $currentuser = true;
97 $showcontactactionlinks = true;
98 if ($user1id != $USER->id) {
99 $user1 = $DB->get_record('user', array('id' => $user1id));
100 if (!$user1) {
101 print_error('invaliduserid');
103 $currentuser = false;//if we're looking at someone else's messages we need to lock/remove some UI elements
104 $showcontactactionlinks = false;
105 } else {
106 $user1 = $USER;
108 unset($user1id);
110 $user2 = null;
111 if (!empty($user2id)) {
112 $user2 = $DB->get_record("user", array("id" => $user2id));
113 if (!$user2) {
114 print_error('invaliduserid');
117 unset($user2id);
119 // Is the user involved in the conversation?
120 // Do they have the ability to read other user's conversations?
121 if (!message_current_user_is_involved($user1, $user2) && !has_capability('moodle/site:readallmessages', $context)) {
122 print_error('accessdenied','admin');
125 /// Process any contact maintenance requests there may be
126 if ($addcontact and confirm_sesskey()) {
127 add_to_log(SITEID, 'message', 'add contact', 'index.php?user1='.$addcontact.'&amp;user2='.$USER->id, $addcontact);
128 message_add_contact($addcontact);
129 redirect($CFG->wwwroot . '/message/index.php?viewing=contacts&id='.$addcontact);
131 if ($removecontact and confirm_sesskey()) {
132 add_to_log(SITEID, 'message', 'remove contact', 'index.php?user1='.$removecontact.'&amp;user2='.$USER->id, $removecontact);
133 message_remove_contact($removecontact);
135 if ($blockcontact and confirm_sesskey()) {
136 add_to_log(SITEID, 'message', 'block contact', 'index.php?user1='.$blockcontact.'&amp;user2='.$USER->id, $blockcontact);
137 message_block_contact($blockcontact);
139 if ($unblockcontact and confirm_sesskey()) {
140 add_to_log(SITEID, 'message', 'unblock contact', 'index.php?user1='.$unblockcontact.'&amp;user2='.$USER->id, $unblockcontact);
141 message_unblock_contact($unblockcontact);
144 //was a message sent? Do NOT allow someone looking at someone else's messages to send them.
145 $messageerror = null;
146 if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', $context)) {
147 // Check that the user is not blocking us!!
148 if ($contact = $DB->get_record('message_contacts', array('userid' => $user2->id, 'contactid' => $user1->id))) {
149 if ($contact->blocked and !has_capability('moodle/site:readallmessages', $context)) {
150 $messageerror = get_string('userisblockingyou', 'message');
153 $userpreferences = get_user_preferences(NULL, NULL, $user2->id);
155 if (!empty($userpreferences['message_blocknoncontacts'])) { // User is blocking non-contacts
156 if (empty($contact)) { // We are not a contact!
157 $messageerror = get_string('userisblockingyounoncontact', 'message', fullname($user2));
161 if (empty($messageerror)) {
162 $mform = new send_form();
163 $defaultmessage = new stdClass;
164 $defaultmessage->id = $user2->id;
165 $defaultmessage->message = '';
167 //Check if the current user has sent a message
168 $data = $mform->get_data();
169 if (!empty($data) && !empty($data->message)) {
170 if (!confirm_sesskey()) {
171 print_error('invalidsesskey');
173 $messageid = message_post_message($user1, $user2, $data->message, FORMAT_MOODLE);
174 if (!empty($messageid)) {
175 //including the id of the user sending the message in the logged URL so the URL works for admins
176 //note message ID may be misleading as the message may potentially get a different ID when moved from message to message_read
177 add_to_log(SITEID, 'message', 'write', 'index.php?user='.$user1->id.'&id='.$user2->id.'&history=1#m'.$messageid, $user1->id);
178 redirect($CFG->wwwroot . '/message/index.php?viewing='.$viewing.'&id='.$user2->id);
184 $strmessages = get_string('messages', 'message');
185 if (!empty($user2)) {
186 $user2fullname = fullname($user2);
188 $PAGE->set_title("$strmessages: $user2fullname");
189 $PAGE->set_heading("$strmessages: $user2fullname");
190 } else {
191 $PAGE->set_title("{$SITE->shortname}: $strmessages");
192 $PAGE->set_heading("{$SITE->shortname}: $strmessages");
195 //now the page contents
196 echo $OUTPUT->header();
198 echo $OUTPUT->box_start('message');
200 $countunread = 0; //count of unread messages from $user2
201 $countunreadtotal = 0; //count of unread messages from all users
203 //we're dealing with unread messages early so the contact list will accurately reflect what is read/unread
204 $viewingnewmessages = false;
205 if (!empty($user2)) {
206 //are there any unread messages from $user2
207 $countunread = message_count_unread_messages($user1, $user2);
208 if ($countunread>0) {
209 //mark the messages we're going to display as read
210 message_mark_messages_read($user1->id, $user2->id);
211 if($viewing == MESSAGE_VIEW_UNREAD_MESSAGES) {
212 $viewingnewmessages = true;
216 $countunreadtotal = message_count_unread_messages($user1);
218 if ($countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES && empty($user2)) {
219 //default to showing the search
220 $viewing = MESSAGE_VIEW_SEARCH;
223 $blockedusers = message_get_blocked_users($user1, $user2);
224 $countblocked = count($blockedusers);
226 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts($user1, $user2);
228 message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks, $page);
230 echo html_writer::start_tag('div', array('class' => 'messagearea mdl-align'));
231 if (!empty($user2)) {
233 echo html_writer::start_tag('div', array('class' => 'mdl-left messagehistory'));
235 $visible = 'visible';
236 $hidden = 'hiddenelement'; //cant just use hidden as mform adds that class to its fieldset for something else
238 $recentlinkclass = $recentlabelclass = $historylinkclass = $historylabelclass = $visible;
239 if ($history == MESSAGE_HISTORY_ALL) {
240 $displaycount = 0;
242 $recentlabelclass = $historylinkclass = $hidden;
243 } else if($viewingnewmessages) {
244 //if user is viewing new messages only show them the new messages
245 $displaycount = $countunread;
247 $recentlabelclass = $historylabelclass = $hidden;
248 } else {
249 //default to only showing the last few messages
250 $displaycount = MESSAGE_SHORTVIEW_LIMIT;
252 if ($countunread>MESSAGE_SHORTVIEW_LIMIT) {
253 $displaycount = $countunread;
256 $recentlinkclass = $historylabelclass = $hidden;
259 $messagehistorylink = html_writer::start_tag('div', array('class' => 'mdl-align messagehistorytype'));
260 $messagehistorylink .= html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_ALL,
261 get_string('messagehistoryfull','message'),
262 array('class' => $historylinkclass));
264 $messagehistorylink .= html_writer::start_tag('span', array('class' => $historylabelclass));
265 $messagehistorylink .= get_string('messagehistoryfull','message');
266 $messagehistorylink .= html_writer::end_tag('span');
268 $messagehistorylink .= '&nbsp;|&nbsp;'.html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_SHORT,
269 get_string('mostrecent','message'),
270 array('class' => $recentlinkclass));
272 $messagehistorylink .= html_writer::start_tag('span', array('class' => $recentlabelclass));
273 $messagehistorylink .= get_string('mostrecent','message');
274 $messagehistorylink .= html_writer::end_tag('span');
276 if ($viewingnewmessages) {
277 $messagehistorylink .= '&nbsp;|&nbsp;'.html_writer::start_tag('span');//, array('class' => $historyclass)
278 $messagehistorylink .= get_string('unreadnewmessages','message',$displaycount);
279 $messagehistorylink .= html_writer::end_tag('span');
282 $messagehistorylink .= html_writer::end_tag('div');
284 message_print_message_history($user1, $user2, $search, $displaycount, $messagehistorylink, $viewingnewmessages);
285 echo html_writer::end_tag('div');
287 //send message form
288 if ($currentuser && has_capability('moodle/site:sendmessage', $context)) {
289 echo html_writer::start_tag('div', array('class' => 'mdl-align messagesend'));
290 if (!empty($messageerror)) {
291 echo html_writer::tag('span', $messageerror, array('id' => 'messagewarning'));
292 } else {
293 // Display a warning if the current user is blocking non-contacts and is about to message to a non-contact
294 // Otherwise they may wonder why they never get a reply
295 $blocknoncontacts = get_user_preferences('message_blocknoncontacts', '', $user1->id);
296 if (!empty($blocknoncontacts)) {
297 $contact = $DB->get_record('message_contacts', array('userid' => $user1->id, 'contactid' => $user2->id));
298 if (empty($contact)) {
299 $msg = get_string('messagingblockednoncontact', 'message', fullname($user2));
300 echo html_writer::tag('span', $msg, array('id' => 'messagewarning'));
304 $mform = new send_form();
305 $defaultmessage = new stdClass;
306 $defaultmessage->id = $user2->id;
307 $defaultmessage->message = '';
308 //$defaultmessage->messageformat = FORMAT_MOODLE;
309 $mform->set_data($defaultmessage);
310 $mform->display();
312 echo html_writer::end_tag('div');
314 } else if ($viewing == MESSAGE_VIEW_SEARCH) {
315 message_print_search($advancedsearch, $user1);
316 } else if ($viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS) {
317 message_print_recent_conversations($user1);
318 } else if ($viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) {
319 message_print_recent_notifications($user1);
321 echo html_writer::end_tag('div');
323 echo $OUTPUT->box_end();
325 echo $OUTPUT->footer();