Merge branch 'MDL-25708' of git://github.com/stronk7/moodle
[moodle.git] / message / index.php
CommitLineData
eb5334ff 1<?php
2
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.
14//
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/>.
17
18/**
affefa8c 19 * A page displaying the user's contacts and messages
eb5334ff 20 *
02f2c7bd
AD
21 * @package moodlecore
22 * @copyright 2010 Andrew Davis
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
eb5334ff 24 */
25
02f2c7bd
AD
26require_once('../config.php');
27require_once('lib.php');
28require_once('send_form.php');
eb5334ff 29
30require_login(0, false);
31
4f0c2d00 32if (isguestuser()) {
eb5334ff 33 redirect($CFG->wwwroot);
34}
35
36if (empty($CFG->messaging)) {
37 print_error('disabled', 'message');
38}
cdf39255 39
02f2c7bd
AD
40$usergroup = optional_param('usergroup', VIEW_UNREAD_MESSAGES, PARAM_ALPHANUMEXT);
41$history = optional_param('history', MESSAGE_HISTORY_SHORT, PARAM_INT);
a8320bc4 42$search = optional_param('search', '', PARAM_CLEAN); //TODO: use PARAM_RAW, but make sure we use s() and p() properly
02f2c7bd
AD
43
44$user1id = optional_param('user', $USER->id, PARAM_INT);
45$user2id = optional_param('id', 0, PARAM_INT);
46
eb5334ff 47$addcontact = optional_param('addcontact', 0, PARAM_INT); // adding a contact
48$removecontact = optional_param('removecontact', 0, PARAM_INT); // removing a contact
49$blockcontact = optional_param('blockcontact', 0, PARAM_INT); // blocking a contact
50$unblockcontact = optional_param('unblockcontact', 0, PARAM_INT); // unblocking a contact
eb5334ff 51
08cd70cf
AD
52//for search
53$advancedsearch = optional_param('advanced', 0, PARAM_INT);
54
d54286e7
AD
55//if they have numerous contacts or are viewing course participants we might need to page through them
56$page = optional_param('page', 0, PARAM_INT);
57
a6855934 58$url = new moodle_url('/message/index.php');
02f2c7bd 59
02f2c7bd
AD
60if ($user2id !== 0) {
61 $url->param('id', $user2id);
62}
63
08cd70cf
AD
64if ($usergroup !== 0) {
65 if ($user2id !== 0 && $usergroup==VIEW_SEARCH) {
66 //if theyve searched and selected a user change the view back to contacts so the search button is displayed
67 $usergroup = VIEW_CONTACTS;
68 }
69 $url->param('usergroup', $usergroup);
eb5334ff 70}
405f01ee 71
02f2c7bd 72$PAGE->set_url($url);
3fca9299 73
02f2c7bd
AD
74$PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id));
75$PAGE->navigation->extend_for_user($USER);
76$PAGE->set_pagelayout('course');
77
78$context = get_context_instance(CONTEXT_SYSTEM);
172186b8 79
02f2c7bd
AD
80$user1 = null;
81$currentuser = true;
82$showcontactactionlinks = SHOW_ACTION_LINKS_IN_CONTACT_LIST;
83if ($user1id!=$USER->id) {
84 $user1 = $DB->get_record('user', array('id'=>$user1id));
85 if (!$user1) {
86 print_error('invaliduserid');
87 }
88 $currentuser = false;//if we're looking at someone else's messages we need to lock/remove some UI elements
89 $showcontactactionlinks = false;
90} else {
91 $user1 = $USER;
eb5334ff 92}
02f2c7bd 93unset($user1id);
e8e2d7f1 94
02f2c7bd
AD
95$user2 = null;
96if (!empty($user2id)) {
97 $user2 = $DB->get_record("user", array("id"=>$user2id));
98 if (!$user2) {
99 print_error('invaliduserid');
100 }
101}
102unset($user2id);
172186b8 103
27a39763
AD
104//the current user isnt involved in this discussion at all
105if ($user1->id!=$USER->id && $user2->id!=$USER->id && !has_capability('moodle/site:readallmessages', $context)) {
106 print_error('accessdenied','admin');
107}
108
109/// Process any contact maintenance requests there may be
110if ($addcontact and confirm_sesskey()) {
ee5ae297 111 add_to_log(SITEID, 'message', 'add contact', 'index.php?user1='.$addcontact.'&amp;user2='.$USER->id, $addcontact);
27a39763
AD
112 message_add_contact($addcontact);
113 redirect($CFG->wwwroot . '/message/index.php?usergroup=contacts&id='.$addcontact);
114}
115if ($removecontact and confirm_sesskey()) {
ee5ae297 116 add_to_log(SITEID, 'message', 'remove contact', 'index.php?user1='.$removecontact.'&amp;user2='.$USER->id, $removecontact);
27a39763
AD
117 message_remove_contact($removecontact);
118}
119if ($blockcontact and confirm_sesskey()) {
ee5ae297 120 add_to_log(SITEID, 'message', 'block contact', 'index.php?user1='.$blockcontact.'&amp;user2='.$USER->id, $blockcontact);
27a39763
AD
121 message_block_contact($blockcontact);
122}
123if ($unblockcontact and confirm_sesskey()) {
ee5ae297 124 add_to_log(SITEID, 'message', 'unblock contact', 'index.php?user1='.$unblockcontact.'&amp;user2='.$USER->id, $unblockcontact);
27a39763
AD
125 message_unblock_contact($unblockcontact);
126}
127
71666cf3 128//was a message sent? Do NOT allow someone looking at someone else's messages to send them.
02f2c7bd
AD
129$messageerror = null;
130if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', $context)) {
eb5334ff 131
02f2c7bd
AD
132 // Check that the user is not blocking us!!
133 if ($contact = $DB->get_record('message_contacts', array('userid'=>$user2->id, 'contactid'=>$user1->id))) {
134 if ($contact->blocked and !has_capability('moodle/site:readallmessages', $context)) {
135 $messageerror = get_string('userisblockingyou', 'message');
136 }
137 }
138 $userpreferences = get_user_preferences(NULL, NULL, $user2->id);
eb5334ff 139
02f2c7bd
AD
140 if (!empty($userpreferences['message_blocknoncontacts'])) { // User is blocking non-contacts
141 if (empty($contact)) { // We are not a contact!
142 $messageerror = get_string('userisblockingyounoncontact', 'message');
143 }
144 }
eb5334ff 145
02f2c7bd
AD
146 if (empty($messageerror)) {
147 $mform = new send_form();
148 $defaultmessage = new stdClass;
149 $defaultmessage->id = $user2->id;
150 $defaultmessage->message = '';
eb5334ff 151
d54286e7 152 //Check if the current user has sent a message
02f2c7bd 153 $data = $mform->get_data();
d54286e7 154 if (!empty($data) && !empty($data->message)) {
02f2c7bd
AD
155 if (!confirm_sesskey()) {
156 print_error('invalidsesskey');
157 }
172186b8 158
323409c5 159 $messageid = message_post_message($user1, $user2, $data->message, FORMAT_MOODLE, 'direct');
02f2c7bd
AD
160 if (!empty($messageid)) {
161 redirect($CFG->wwwroot . '/message/index.php?usergroup='.$usergroup.'&id='.$user2->id);
162 }
163 }
164 }
165}
172186b8 166
02f2c7bd
AD
167$strmessages = get_string('messages', 'message');
168if (!empty($user2)) {
169 $user2fullname = fullname($user2);
170
171 $PAGE->set_title("$strmessages: $user2fullname");
172 $PAGE->set_heading("$strmessages: $user2fullname");
173} else {
174 $PAGE->set_title("{$SITE->shortname}: $strmessages");
175 $PAGE->set_heading("{$SITE->shortname}: $strmessages");
eb5334ff 176}
172186b8 177
02f2c7bd
AD
178//now the page contents
179echo $OUTPUT->header();
180
181echo $OUTPUT->box_start('message');
182
183$countunread = 0; //count of unread messages from $user2
184$countunreadtotal = 0; //count of unread messages from all users
185
186//we're dealing with unread messages early so the contact list will accurately reflect what is read/unread
728114df 187$viewingnewmessages = false;
02f2c7bd
AD
188if (!empty($user2)) {
189 //are there any unread messages from $user2
190 $countunread = message_count_unread_messages($user1, $user2);
191 if ($countunread>0) {
192 //mark the messages we're going to display as read
193 message_mark_messages_read($user1->id, $user2->id);
728114df
AD
194 if($usergroup==VIEW_UNREAD_MESSAGES) {
195 $viewingnewmessages = true;
196 }
02f2c7bd
AD
197 }
198}
199$countunreadtotal = message_count_unread_messages($user1);
200
fe8373bc
AD
201if ($countunreadtotal==0 && $usergroup==VIEW_UNREAD_MESSAGES && empty($user2)) {
202 //default to showing the search
203 $usergroup = VIEW_SEARCH;
204}
205
02f2c7bd
AD
206$blockedusers = message_get_blocked_users($user1, $user2);
207$countblocked = count($blockedusers);
208
209list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts($user1, $user2);
210
d54286e7 211message_print_contact_selector($countunreadtotal, $usergroup, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks, $page);
02f2c7bd
AD
212
213echo html_writer::start_tag('div', array('class'=>'messagearea mdl-align'));
214 if (!empty($user2)) {
215
216 echo html_writer::start_tag('div', array('class'=>'mdl-left messagehistory'));
217
728114df
AD
218 $visible = 'visible';
219 $hidden = 'hiddenelement'; //cant just use hidden as mform adds that class to its fieldset for something else
220
221 $recentlinkclass = $recentlabelclass = $historylinkclass = $historylabelclass = $visible;
02f2c7bd
AD
222 if ($history==MESSAGE_HISTORY_ALL) {
223 $displaycount = 0;
224
728114df
AD
225 $recentlabelclass = $historylinkclass = $hidden;
226 } else if($viewingnewmessages) {
227 //if user is viewing new messages only show them the new messages
228 $displaycount = $countunread;
229
230 $recentlabelclass = $historylabelclass = $hidden;
02f2c7bd 231 } else {
d54286e7 232 //default to only showing the last few messages
02f2c7bd
AD
233 $displaycount = MESSAGE_SHORTVIEW_LIMIT;
234
235 if ($countunread>MESSAGE_SHORTVIEW_LIMIT) {
236 $displaycount = $countunread;
237 }
728114df
AD
238
239 $recentlinkclass = $historylabelclass = $hidden;
02f2c7bd
AD
240 }
241
242 $messagehistorylink = html_writer::start_tag('div', array('class'=>'mdl-align messagehistorytype'));
243 $messagehistorylink .= html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_ALL,
244 get_string('messagehistoryfull','message'),
728114df 245 array('class'=>$historylinkclass));
02f2c7bd 246
728114df 247 $messagehistorylink .= html_writer::start_tag('span', array('class'=>$historylabelclass));
02f2c7bd
AD
248 $messagehistorylink .= get_string('messagehistoryfull','message');
249 $messagehistorylink .= html_writer::end_tag('span');
250
251 $messagehistorylink .= '&nbsp;|&nbsp;'.html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_SHORT,
252 get_string('mostrecent','message'),
728114df 253 array('class'=>$recentlinkclass));
02f2c7bd 254
728114df 255 $messagehistorylink .= html_writer::start_tag('span', array('class'=>$recentlabelclass));
02f2c7bd
AD
256 $messagehistorylink .= get_string('mostrecent','message');
257 $messagehistorylink .= html_writer::end_tag('span');
a8320bc4 258
728114df
AD
259 if ($viewingnewmessages) {
260 $messagehistorylink .= '&nbsp;|&nbsp;'.html_writer::start_tag('span');//, array('class'=>$historyclass)
a813a748 261 $messagehistorylink .= get_string('unreadnewmessages','message',$displaycount);
728114df
AD
262 $messagehistorylink .= html_writer::end_tag('span');
263 }
264
02f2c7bd
AD
265 $messagehistorylink .= html_writer::end_tag('div');
266
a813a748 267 message_print_message_history($user1, $user2, $search, $displaycount, $messagehistorylink, $viewingnewmessages);
02f2c7bd
AD
268 echo html_writer::end_tag('div');
269
270 //send message form
271 if ($currentuser && has_capability('moodle/site:sendmessage', $context)) {
272 echo html_writer::start_tag('div', array('class'=>'mdl-align messagesend'));
e41da832
AD
273 if (!empty($messageerror)) {
274 echo $OUTPUT->heading($messageerror, 3);
275 } else {
276 $mform = new send_form();
277 $defaultmessage = new stdClass;
278 $defaultmessage->id = $user2->id;
279 $defaultmessage->message = '';
280 //$defaultmessage->messageformat = FORMAT_MOODLE;
281 $mform->set_data($defaultmessage);
282 $mform->display();
283 }
02f2c7bd
AD
284 echo html_writer::end_tag('div');
285 }
08cd70cf
AD
286 } else if ($usergroup==VIEW_SEARCH) {
287 message_print_search($advancedsearch, $user1);
02f2c7bd
AD
288 }
289echo html_writer::end_tag('div');
290
291echo $OUTPUT->box_end();
292
eb5334ff 293echo $OUTPUT->footer();
0d3bb48d 294
02f2c7bd 295