Merge branch 'w12_MDL-26842_20_ociparams' of git://github.com/skodak/moodle
[moodle.git] / message / index.php
blob691028226743ab4340681e0db2102a251d9f99dc
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 $context = get_context_instance(CONTEXT_SYSTEM);
92 $user1 = null;
93 $currentuser = true;
94 $showcontactactionlinks = true;
95 if ($user1id != $USER->id) {
96 $user1 = $DB->get_record('user', array('id' => $user1id));
97 if (!$user1) {
98 print_error('invaliduserid');
100 $currentuser = false;//if we're looking at someone else's messages we need to lock/remove some UI elements
101 $showcontactactionlinks = false;
102 } else {
103 $user1 = $USER;
105 unset($user1id);
107 $user2 = null;
108 if (!empty($user2id)) {
109 $user2 = $DB->get_record("user", array("id" => $user2id));
110 if (!$user2) {
111 print_error('invaliduserid');
114 unset($user2id);
116 //the current user isnt involved in this discussion at all
117 if ($user1->id != $USER->id && (!empty($user2) && $user2->id != $USER->id) && !has_capability('moodle/site:readallmessages', $context)) {
118 print_error('accessdenied','admin');
121 /// Process any contact maintenance requests there may be
122 if ($addcontact and confirm_sesskey()) {
123 add_to_log(SITEID, 'message', 'add contact', 'index.php?user1='.$addcontact.'&amp;user2='.$USER->id, $addcontact);
124 message_add_contact($addcontact);
125 redirect($CFG->wwwroot . '/message/index.php?viewing=contacts&id='.$addcontact);
127 if ($removecontact and confirm_sesskey()) {
128 add_to_log(SITEID, 'message', 'remove contact', 'index.php?user1='.$removecontact.'&amp;user2='.$USER->id, $removecontact);
129 message_remove_contact($removecontact);
131 if ($blockcontact and confirm_sesskey()) {
132 add_to_log(SITEID, 'message', 'block contact', 'index.php?user1='.$blockcontact.'&amp;user2='.$USER->id, $blockcontact);
133 message_block_contact($blockcontact);
135 if ($unblockcontact and confirm_sesskey()) {
136 add_to_log(SITEID, 'message', 'unblock contact', 'index.php?user1='.$unblockcontact.'&amp;user2='.$USER->id, $unblockcontact);
137 message_unblock_contact($unblockcontact);
140 //was a message sent? Do NOT allow someone looking at someone else's messages to send them.
141 $messageerror = null;
142 if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', $context)) {
143 // Check that the user is not blocking us!!
144 if ($contact = $DB->get_record('message_contacts', array('userid' => $user2->id, 'contactid' => $user1->id))) {
145 if ($contact->blocked and !has_capability('moodle/site:readallmessages', $context)) {
146 $messageerror = get_string('userisblockingyou', 'message');
149 $userpreferences = get_user_preferences(NULL, NULL, $user2->id);
151 if (!empty($userpreferences['message_blocknoncontacts'])) { // User is blocking non-contacts
152 if (empty($contact)) { // We are not a contact!
153 $messageerror = get_string('userisblockingyounoncontact', 'message');
157 if (empty($messageerror)) {
158 $mform = new send_form();
159 $defaultmessage = new stdClass;
160 $defaultmessage->id = $user2->id;
161 $defaultmessage->message = '';
163 //Check if the current user has sent a message
164 $data = $mform->get_data();
165 if (!empty($data) && !empty($data->message)) {
166 if (!confirm_sesskey()) {
167 print_error('invalidsesskey');
169 $messageid = message_post_message($user1, $user2, $data->message, FORMAT_MOODLE);
170 if (!empty($messageid)) {
171 //including the id of the user sending the message in the logged URL so the URL works for admins
172 //note message ID may be misleading as the message may potentially get a different ID when moved from message to message_read
173 add_to_log(SITEID, 'message', 'write', 'index.php?user='.$user1->id.'&id='.$user2->id.'&history=1#m'.$messageid, $user1->id);
174 redirect($CFG->wwwroot . '/message/index.php?viewing='.$viewing.'&id='.$user2->id);
180 $strmessages = get_string('messages', 'message');
181 if (!empty($user2)) {
182 $user2fullname = fullname($user2);
184 $PAGE->set_title("$strmessages: $user2fullname");
185 $PAGE->set_heading("$strmessages: $user2fullname");
186 } else {
187 $PAGE->set_title("{$SITE->shortname}: $strmessages");
188 $PAGE->set_heading("{$SITE->shortname}: $strmessages");
191 //now the page contents
192 echo $OUTPUT->header();
194 echo $OUTPUT->box_start('message');
196 $countunread = 0; //count of unread messages from $user2
197 $countunreadtotal = 0; //count of unread messages from all users
199 //we're dealing with unread messages early so the contact list will accurately reflect what is read/unread
200 $viewingnewmessages = false;
201 if (!empty($user2)) {
202 //are there any unread messages from $user2
203 $countunread = message_count_unread_messages($user1, $user2);
204 if ($countunread>0) {
205 //mark the messages we're going to display as read
206 message_mark_messages_read($user1->id, $user2->id);
207 if($viewing == MESSAGE_VIEW_UNREAD_MESSAGES) {
208 $viewingnewmessages = true;
212 $countunreadtotal = message_count_unread_messages($user1);
214 if ($countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES && empty($user2)) {
215 //default to showing the search
216 $viewing = MESSAGE_VIEW_SEARCH;
219 $blockedusers = message_get_blocked_users($user1, $user2);
220 $countblocked = count($blockedusers);
222 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts($user1, $user2);
224 message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks, $page);
226 echo html_writer::start_tag('div', array('class' => 'messagearea mdl-align'));
227 if (!empty($user2)) {
229 echo html_writer::start_tag('div', array('class' => 'mdl-left messagehistory'));
231 $visible = 'visible';
232 $hidden = 'hiddenelement'; //cant just use hidden as mform adds that class to its fieldset for something else
234 $recentlinkclass = $recentlabelclass = $historylinkclass = $historylabelclass = $visible;
235 if ($history == MESSAGE_HISTORY_ALL) {
236 $displaycount = 0;
238 $recentlabelclass = $historylinkclass = $hidden;
239 } else if($viewingnewmessages) {
240 //if user is viewing new messages only show them the new messages
241 $displaycount = $countunread;
243 $recentlabelclass = $historylabelclass = $hidden;
244 } else {
245 //default to only showing the last few messages
246 $displaycount = MESSAGE_SHORTVIEW_LIMIT;
248 if ($countunread>MESSAGE_SHORTVIEW_LIMIT) {
249 $displaycount = $countunread;
252 $recentlinkclass = $historylabelclass = $hidden;
255 $messagehistorylink = html_writer::start_tag('div', array('class' => 'mdl-align messagehistorytype'));
256 $messagehistorylink .= html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_ALL,
257 get_string('messagehistoryfull','message'),
258 array('class' => $historylinkclass));
260 $messagehistorylink .= html_writer::start_tag('span', array('class' => $historylabelclass));
261 $messagehistorylink .= get_string('messagehistoryfull','message');
262 $messagehistorylink .= html_writer::end_tag('span');
264 $messagehistorylink .= '&nbsp;|&nbsp;'.html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_SHORT,
265 get_string('mostrecent','message'),
266 array('class' => $recentlinkclass));
268 $messagehistorylink .= html_writer::start_tag('span', array('class' => $recentlabelclass));
269 $messagehistorylink .= get_string('mostrecent','message');
270 $messagehistorylink .= html_writer::end_tag('span');
272 if ($viewingnewmessages) {
273 $messagehistorylink .= '&nbsp;|&nbsp;'.html_writer::start_tag('span');//, array('class' => $historyclass)
274 $messagehistorylink .= get_string('unreadnewmessages','message',$displaycount);
275 $messagehistorylink .= html_writer::end_tag('span');
278 $messagehistorylink .= html_writer::end_tag('div');
280 message_print_message_history($user1, $user2, $search, $displaycount, $messagehistorylink, $viewingnewmessages);
281 echo html_writer::end_tag('div');
283 //send message form
284 if ($currentuser && has_capability('moodle/site:sendmessage', $context)) {
285 echo html_writer::start_tag('div', array('class' => 'mdl-align messagesend'));
286 if (!empty($messageerror)) {
287 echo $OUTPUT->heading($messageerror, 3);
288 } else {
289 $mform = new send_form();
290 $defaultmessage = new stdClass;
291 $defaultmessage->id = $user2->id;
292 $defaultmessage->message = '';
293 //$defaultmessage->messageformat = FORMAT_MOODLE;
294 $mform->set_data($defaultmessage);
295 $mform->display();
297 echo html_writer::end_tag('div');
299 } else if ($viewing == MESSAGE_VIEW_SEARCH) {
300 message_print_search($advancedsearch, $user1);
301 } else if ($viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS) {
302 message_print_recent_conversations($user1);
303 } else if ($viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) {
304 message_print_recent_notifications($user1);
306 echo html_writer::end_tag('div');
308 echo $OUTPUT->box_end();
310 echo $OUTPUT->footer();