2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * A page displaying the user's contacts and messages
20 * @package core_message
21 * @copyright 2010 Andrew Davis
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
26 require_once('lib.php');
27 require_once('send_form.php');
29 require_login(0, false);
32 redirect($CFG->wwwroot
);
35 if (empty($CFG->messaging
)) {
36 print_error('disabled', 'message');
39 //'viewing' is the preferred URL parameter but we'll still accept usergroup in case its referenced externally
40 $usergroup = optional_param('usergroup', MESSAGE_VIEW_UNREAD_MESSAGES
, PARAM_ALPHANUMEXT
);
41 $viewing = optional_param('viewing', $usergroup, PARAM_ALPHANUMEXT
);
43 $history = optional_param('history', MESSAGE_HISTORY_SHORT
, PARAM_INT
);
44 $search = optional_param('search', '', PARAM_CLEAN
); //TODO: use PARAM_RAW, but make sure we use s() and p() properly
46 //the same param as 1.9 and the param we have been logging. Use this parameter.
47 $user1id = optional_param('user1', $USER->id
, PARAM_INT
);
48 //2.0 shipped using this param. Retaining it only for compatibility. It should be removed.
49 $user1id = optional_param('user', $user1id, PARAM_INT
);
51 //the same param as 1.9 and the param we have been logging. Use this parameter.
52 $user2id = optional_param('user2', 0, PARAM_INT
);
53 //The class send_form supplies the receiving user id as 'id'
54 $user2id = optional_param('id', $user2id, PARAM_INT
);
56 $addcontact = optional_param('addcontact', 0, PARAM_INT
); // adding a contact
57 $removecontact = optional_param('removecontact', 0, PARAM_INT
); // removing a contact
58 $blockcontact = optional_param('blockcontact', 0, PARAM_INT
); // blocking a contact
59 $unblockcontact = optional_param('unblockcontact', 0, PARAM_INT
); // unblocking a contact
62 $advancedsearch = optional_param('advanced', 0, PARAM_INT
);
64 //if they have numerous contacts or are viewing course participants we might need to page through them
65 $page = optional_param('page', 0, PARAM_INT
);
67 $url = new moodle_url('/message/index.php', array('user1' => $user1id));
70 $url->param('user2', $user2id);
72 //Switch view back to contacts if:
73 //1) theyve searched and selected a user
74 //2) they've viewed recent messages or notifications and clicked through to a user
75 if ($viewing == MESSAGE_VIEW_SEARCH ||
$viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS
) {
76 $viewing = MESSAGE_VIEW_CONTACTS
;
80 if ($viewing != MESSAGE_VIEW_UNREAD_MESSAGES
) {
81 $url->param('viewing', $viewing);
86 // Disable message notification popups while the user is viewing their messages
87 $PAGE->set_popup_notification_allowed(false);
91 $showactionlinks = true;
92 if ($user1id != $USER->id
) {
93 $user1 = $DB->get_record('user', array('id' => $user1id));
95 print_error('invaliduserid');
97 $currentuser = false;//if we're looking at someone else's messages we need to lock/remove some UI elements
98 $showactionlinks = false;
105 if (!empty($user2id)) {
106 $user2 = core_user
::get_user($user2id);
108 print_error('invaliduserid');
113 $user2realuser = !empty($user2) && core_user
::is_real_user($user2->id
);
114 $showactionlinks = $showactionlinks && $user2realuser;
115 $systemcontext = context_system
::instance();
117 if (!empty($user2) && $user1->id
== $user2->id
) {
118 print_error('invaliduserid');
121 // Is the user involved in the conversation?
122 // Do they have the ability to read other user's conversations?
123 if (!message_current_user_is_involved($user1, $user2) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
124 print_error('accessdenied','admin');
127 if (substr($viewing, 0, 7) == MESSAGE_VIEW_COURSE
) {
128 $courseid = intval(substr($viewing, 7));
129 require_login($courseid);
130 require_capability('moodle/course:viewparticipants', context_course
::instance($courseid));
131 $PAGE->set_pagelayout('incourse');
133 $PAGE->set_pagelayout('course');
134 $PAGE->set_context(context_user
::instance($user1->id
));
136 if (!empty($user1->id
) && $user1->id
!= $USER->id
) {
137 $PAGE->navigation
->extend_for_user($user1);
139 if (!empty($user2->id
) && $user2->id
!= $USER->id
) {
140 $PAGE->navigation
->extend_for_user($user2);
143 /// Process any contact maintenance requests there may be
144 if ($addcontact and confirm_sesskey()) {
145 add_to_log(SITEID
, 'message', 'add contact', 'index.php?user1='.$addcontact.'&user2='.$USER->id
, $addcontact);
146 message_add_contact($addcontact);
147 redirect($CFG->wwwroot
. '/message/index.php?viewing=contacts&id='.$addcontact);
149 if ($removecontact and confirm_sesskey()) {
150 add_to_log(SITEID
, 'message', 'remove contact', 'index.php?user1='.$removecontact.'&user2='.$USER->id
, $removecontact);
151 message_remove_contact($removecontact);
153 if ($blockcontact and confirm_sesskey()) {
154 add_to_log(SITEID
, 'message', 'block contact', 'index.php?user1='.$blockcontact.'&user2='.$USER->id
, $blockcontact);
155 message_block_contact($blockcontact);
157 if ($unblockcontact and confirm_sesskey()) {
158 add_to_log(SITEID
, 'message', 'unblock contact', 'index.php?user1='.$unblockcontact.'&user2='.$USER->id
, $unblockcontact);
159 message_unblock_contact($unblockcontact);
162 //was a message sent? Do NOT allow someone looking at someone else's messages to send them.
163 $messageerror = null;
164 if ($currentuser && $user2realuser && has_capability('moodle/site:sendmessage', $systemcontext)) {
165 // Check that the user is not blocking us!!
166 if ($contact = $DB->get_record('message_contacts', array('userid' => $user2->id
, 'contactid' => $user1->id
))) {
167 if ($contact->blocked
and !has_capability('moodle/site:readallmessages', $systemcontext)) {
168 $messageerror = get_string('userisblockingyou', 'message');
171 $userpreferences = get_user_preferences(NULL, NULL, $user2->id
);
173 if (!empty($userpreferences['message_blocknoncontacts'])) { // User is blocking non-contacts
174 if (empty($contact)) { // We are not a contact!
175 $messageerror = get_string('userisblockingyounoncontact', 'message', fullname($user2));
179 if (empty($messageerror)) {
180 $mform = new send_form();
181 $defaultmessage = new stdClass
;
182 $defaultmessage->id
= $user2->id
;
183 $defaultmessage->viewing
= $viewing;
184 $defaultmessage->message
= '';
186 //Check if the current user has sent a message
187 $data = $mform->get_data();
188 if (!empty($data) && !empty($data->message
)) {
189 if (!confirm_sesskey()) {
190 print_error('invalidsesskey');
192 $messageid = message_post_message($user1, $user2, $data->message
, FORMAT_MOODLE
);
193 if (!empty($messageid)) {
194 //including the id of the user sending the message in the logged URL so the URL works for admins
195 //note message ID may be misleading as the message may potentially get a different ID when moved from message to message_read
196 add_to_log(SITEID
, 'message', 'write', 'index.php?user='.$user1->id
.'&id='.$user2->id
.'&history=1#m'.$messageid, $user1->id
);
197 redirect($CFG->wwwroot
. '/message/index.php?viewing='.$viewing.'&id='.$user2->id
);
203 $strmessages = get_string('messages', 'message');
204 if ($user2realuser) {
205 $user2fullname = fullname($user2);
207 $PAGE->set_title("$strmessages: $user2fullname");
208 $PAGE->set_heading("$strmessages: $user2fullname");
210 $PAGE->set_title("{$SITE->shortname}: $strmessages");
211 $PAGE->set_heading("{$SITE->shortname}: $strmessages");
214 //now the page contents
215 echo $OUTPUT->header();
217 echo $OUTPUT->box_start('message');
219 $countunread = 0; //count of unread messages from $user2
220 $countunreadtotal = 0; //count of unread messages from all users
222 //we're dealing with unread messages early so the contact list will accurately reflect what is read/unread
223 $viewingnewmessages = false;
224 if ($user2realuser) {
225 //are there any unread messages from $user2
226 $countunread = message_count_unread_messages($user1, $user2);
227 if ($countunread>0) {
228 //mark the messages we're going to display as read
229 message_mark_messages_read($user1->id
, $user2->id
);
230 if($viewing == MESSAGE_VIEW_UNREAD_MESSAGES
) {
231 $viewingnewmessages = true;
235 $countunreadtotal = message_count_unread_messages($user1);
237 if ($currentuser && $countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES
&& empty($user2)) {
238 // If the user has no unread messages, show the search box.
239 // We don't do this when a user is viewing another user's messages as search doesn't
240 // handle user A searching user B's messages properly.
241 $viewing = MESSAGE_VIEW_SEARCH
;
244 $blockedusers = message_get_blocked_users($user1, $user2);
245 $countblocked = count($blockedusers);
247 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts($user1, $user2);
249 message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showactionlinks, $page);
251 echo html_writer
::start_tag('div', array('class' => 'messagearea mdl-align'));
252 if ($user2realuser) {
254 echo html_writer
::start_tag('div', array('class' => 'mdl-left messagehistory'));
256 $visible = 'visible';
257 $hidden = 'hiddenelement'; //cant just use hidden as mform adds that class to its fieldset for something else
259 $recentlinkclass = $recentlabelclass = $historylinkclass = $historylabelclass = $visible;
260 if ($history == MESSAGE_HISTORY_ALL
) {
263 $recentlabelclass = $historylinkclass = $hidden;
264 } else if($viewingnewmessages) {
265 //if user is viewing new messages only show them the new messages
266 $displaycount = $countunread;
268 $recentlabelclass = $historylabelclass = $hidden;
270 //default to only showing the last few messages
271 $displaycount = MESSAGE_SHORTVIEW_LIMIT
;
273 if ($countunread>MESSAGE_SHORTVIEW_LIMIT
) {
274 $displaycount = $countunread;
277 $recentlinkclass = $historylabelclass = $hidden;
280 $messagehistorylink = html_writer
::start_tag('div', array('class' => 'mdl-align messagehistorytype'));
281 $messagehistorylink .= html_writer
::link($PAGE->url
->out(false).'&history='.MESSAGE_HISTORY_ALL
,
282 get_string('messagehistoryfull','message'),
283 array('class' => $historylinkclass));
285 $messagehistorylink .= html_writer
::start_tag('span', array('class' => $historylabelclass));
286 $messagehistorylink .= get_string('messagehistoryfull','message');
287 $messagehistorylink .= html_writer
::end_tag('span');
289 $messagehistorylink .= ' | '.html_writer
::link($PAGE->url
->out(false).'&history='.MESSAGE_HISTORY_SHORT
,
290 get_string('mostrecent','message'),
291 array('class' => $recentlinkclass));
293 $messagehistorylink .= html_writer
::start_tag('span', array('class' => $recentlabelclass));
294 $messagehistorylink .= get_string('mostrecent','message');
295 $messagehistorylink .= html_writer
::end_tag('span');
297 if ($viewingnewmessages) {
298 $messagehistorylink .= ' | '.html_writer
::start_tag('span');//, array('class' => $historyclass)
299 $messagehistorylink .= get_string('unreadnewmessages','message',$displaycount);
300 $messagehistorylink .= html_writer
::end_tag('span');
303 $messagehistorylink .= html_writer
::end_tag('div');
305 message_print_message_history($user1, $user2, $search, $displaycount, $messagehistorylink, $viewingnewmessages, $showactionlinks);
306 echo html_writer
::end_tag('div');
309 if ($currentuser && has_capability('moodle/site:sendmessage', $systemcontext)) {
310 echo html_writer
::start_tag('div', array('class' => 'mdl-align messagesend'));
311 if (!empty($messageerror)) {
312 echo html_writer
::tag('span', $messageerror, array('id' => 'messagewarning'));
314 // Display a warning if the current user is blocking non-contacts and is about to message to a non-contact
315 // Otherwise they may wonder why they never get a reply
316 $blocknoncontacts = get_user_preferences('message_blocknoncontacts', '', $user1->id
);
317 if (!empty($blocknoncontacts)) {
318 $contact = $DB->get_record('message_contacts', array('userid' => $user1->id
, 'contactid' => $user2->id
));
319 if (empty($contact)) {
320 $msg = get_string('messagingblockednoncontact', 'message', fullname($user2));
321 echo html_writer
::tag('span', $msg, array('id' => 'messagewarning'));
325 $mform = new send_form();
326 $defaultmessage = new stdClass
;
327 $defaultmessage->id
= $user2->id
;
328 $defaultmessage->viewing
= $viewing;
329 $defaultmessage->message
= '';
330 //$defaultmessage->messageformat = FORMAT_MOODLE;
331 $mform->set_data($defaultmessage);
334 echo html_writer
::end_tag('div');
336 } else if ($viewing == MESSAGE_VIEW_SEARCH
) {
337 message_print_search($advancedsearch, $user1);
338 } else if ($viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS
) {
339 message_print_recent_conversations($user1, false, $showactionlinks);
340 } else if ($viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS
) {
341 message_print_recent_notifications($user1);
343 echo html_writer
::end_tag('div');
345 echo $OUTPUT->box_end();
347 echo $OUTPUT->footer();