MDL-29248 backup: take rid of the setting and code handling the 'private' user filearea
[moodle.git] / message / index.php
blobe01a8133a943f3334ca9b307aab82afec84c7716
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 //the current user isnt involved in this discussion at all
120 if ($user1->id != $USER->id && (!empty($user2) && $user2->id != $USER->id) && !has_capability('moodle/site:readallmessages', $context)) {
121 print_error('accessdenied','admin');
124 /// Process any contact maintenance requests there may be
125 if ($addcontact and confirm_sesskey()) {
126 add_to_log(SITEID, 'message', 'add contact', 'index.php?user1='.$addcontact.'&amp;user2='.$USER->id, $addcontact);
127 message_add_contact($addcontact);
128 redirect($CFG->wwwroot . '/message/index.php?viewing=contacts&id='.$addcontact);
130 if ($removecontact and confirm_sesskey()) {
131 add_to_log(SITEID, 'message', 'remove contact', 'index.php?user1='.$removecontact.'&amp;user2='.$USER->id, $removecontact);
132 message_remove_contact($removecontact);
134 if ($blockcontact and confirm_sesskey()) {
135 add_to_log(SITEID, 'message', 'block contact', 'index.php?user1='.$blockcontact.'&amp;user2='.$USER->id, $blockcontact);
136 message_block_contact($blockcontact);
138 if ($unblockcontact and confirm_sesskey()) {
139 add_to_log(SITEID, 'message', 'unblock contact', 'index.php?user1='.$unblockcontact.'&amp;user2='.$USER->id, $unblockcontact);
140 message_unblock_contact($unblockcontact);
143 //was a message sent? Do NOT allow someone looking at someone else's messages to send them.
144 $messageerror = null;
145 if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', $context)) {
146 // Check that the user is not blocking us!!
147 if ($contact = $DB->get_record('message_contacts', array('userid' => $user2->id, 'contactid' => $user1->id))) {
148 if ($contact->blocked and !has_capability('moodle/site:readallmessages', $context)) {
149 $messageerror = get_string('userisblockingyou', 'message');
152 $userpreferences = get_user_preferences(NULL, NULL, $user2->id);
154 if (!empty($userpreferences['message_blocknoncontacts'])) { // User is blocking non-contacts
155 if (empty($contact)) { // We are not a contact!
156 $messageerror = get_string('userisblockingyounoncontact', 'message');
160 if (empty($messageerror)) {
161 $mform = new send_form();
162 $defaultmessage = new stdClass;
163 $defaultmessage->id = $user2->id;
164 $defaultmessage->message = '';
166 //Check if the current user has sent a message
167 $data = $mform->get_data();
168 if (!empty($data) && !empty($data->message)) {
169 if (!confirm_sesskey()) {
170 print_error('invalidsesskey');
172 $messageid = message_post_message($user1, $user2, $data->message, FORMAT_MOODLE);
173 if (!empty($messageid)) {
174 //including the id of the user sending the message in the logged URL so the URL works for admins
175 //note message ID may be misleading as the message may potentially get a different ID when moved from message to message_read
176 add_to_log(SITEID, 'message', 'write', 'index.php?user='.$user1->id.'&id='.$user2->id.'&history=1#m'.$messageid, $user1->id);
177 redirect($CFG->wwwroot . '/message/index.php?viewing='.$viewing.'&id='.$user2->id);
183 $strmessages = get_string('messages', 'message');
184 if (!empty($user2)) {
185 $user2fullname = fullname($user2);
187 $PAGE->set_title("$strmessages: $user2fullname");
188 $PAGE->set_heading("$strmessages: $user2fullname");
189 } else {
190 $PAGE->set_title("{$SITE->shortname}: $strmessages");
191 $PAGE->set_heading("{$SITE->shortname}: $strmessages");
194 //now the page contents
195 echo $OUTPUT->header();
197 echo $OUTPUT->box_start('message');
199 $countunread = 0; //count of unread messages from $user2
200 $countunreadtotal = 0; //count of unread messages from all users
202 //we're dealing with unread messages early so the contact list will accurately reflect what is read/unread
203 $viewingnewmessages = false;
204 if (!empty($user2)) {
205 //are there any unread messages from $user2
206 $countunread = message_count_unread_messages($user1, $user2);
207 if ($countunread>0) {
208 //mark the messages we're going to display as read
209 message_mark_messages_read($user1->id, $user2->id);
210 if($viewing == MESSAGE_VIEW_UNREAD_MESSAGES) {
211 $viewingnewmessages = true;
215 $countunreadtotal = message_count_unread_messages($user1);
217 if ($countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES && empty($user2)) {
218 //default to showing the search
219 $viewing = MESSAGE_VIEW_SEARCH;
222 $blockedusers = message_get_blocked_users($user1, $user2);
223 $countblocked = count($blockedusers);
225 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts($user1, $user2);
227 message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks, $page);
229 echo html_writer::start_tag('div', array('class' => 'messagearea mdl-align'));
230 if (!empty($user2)) {
232 echo html_writer::start_tag('div', array('class' => 'mdl-left messagehistory'));
234 $visible = 'visible';
235 $hidden = 'hiddenelement'; //cant just use hidden as mform adds that class to its fieldset for something else
237 $recentlinkclass = $recentlabelclass = $historylinkclass = $historylabelclass = $visible;
238 if ($history == MESSAGE_HISTORY_ALL) {
239 $displaycount = 0;
241 $recentlabelclass = $historylinkclass = $hidden;
242 } else if($viewingnewmessages) {
243 //if user is viewing new messages only show them the new messages
244 $displaycount = $countunread;
246 $recentlabelclass = $historylabelclass = $hidden;
247 } else {
248 //default to only showing the last few messages
249 $displaycount = MESSAGE_SHORTVIEW_LIMIT;
251 if ($countunread>MESSAGE_SHORTVIEW_LIMIT) {
252 $displaycount = $countunread;
255 $recentlinkclass = $historylabelclass = $hidden;
258 $messagehistorylink = html_writer::start_tag('div', array('class' => 'mdl-align messagehistorytype'));
259 $messagehistorylink .= html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_ALL,
260 get_string('messagehistoryfull','message'),
261 array('class' => $historylinkclass));
263 $messagehistorylink .= html_writer::start_tag('span', array('class' => $historylabelclass));
264 $messagehistorylink .= get_string('messagehistoryfull','message');
265 $messagehistorylink .= html_writer::end_tag('span');
267 $messagehistorylink .= '&nbsp;|&nbsp;'.html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_SHORT,
268 get_string('mostrecent','message'),
269 array('class' => $recentlinkclass));
271 $messagehistorylink .= html_writer::start_tag('span', array('class' => $recentlabelclass));
272 $messagehistorylink .= get_string('mostrecent','message');
273 $messagehistorylink .= html_writer::end_tag('span');
275 if ($viewingnewmessages) {
276 $messagehistorylink .= '&nbsp;|&nbsp;'.html_writer::start_tag('span');//, array('class' => $historyclass)
277 $messagehistorylink .= get_string('unreadnewmessages','message',$displaycount);
278 $messagehistorylink .= html_writer::end_tag('span');
281 $messagehistorylink .= html_writer::end_tag('div');
283 message_print_message_history($user1, $user2, $search, $displaycount, $messagehistorylink, $viewingnewmessages);
284 echo html_writer::end_tag('div');
286 //send message form
287 if ($currentuser && has_capability('moodle/site:sendmessage', $context)) {
288 echo html_writer::start_tag('div', array('class' => 'mdl-align messagesend'));
289 if (!empty($messageerror)) {
290 echo $OUTPUT->heading($messageerror, 3);
291 } else {
292 $mform = new send_form();
293 $defaultmessage = new stdClass;
294 $defaultmessage->id = $user2->id;
295 $defaultmessage->message = '';
296 //$defaultmessage->messageformat = FORMAT_MOODLE;
297 $mform->set_data($defaultmessage);
298 $mform->display();
300 echo html_writer::end_tag('div');
302 } else if ($viewing == MESSAGE_VIEW_SEARCH) {
303 message_print_search($advancedsearch, $user1);
304 } else if ($viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS) {
305 message_print_recent_conversations($user1);
306 } else if ($viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) {
307 message_print_recent_notifications($user1);
309 echo html_writer::end_tag('div');
311 echo $OUTPUT->box_end();
313 echo $OUTPUT->footer();