NOBUG: Added missing set_context call
[moodle.git] / message / discussion.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/**
19 * Part of the message section of Moodle
20 *
21 * @author Luis Rodrigues and Martin Dougiamas
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package message
24 */
531e58f1 25
5d6b319b 26 require('../config.php');
27 require('lib.php');
28
29 require_login();
304d1245 30 $PAGE->set_context(get_system_context());
5d6b319b 31
4f0c2d00 32 if (isguestuser()) {
5d6b319b 33 redirect($CFG->wwwroot);
34 }
35
36 if (empty($CFG->messaging)) {
f857fa9f 37 print_error('disabled', 'message');
5d6b319b 38 }
39
40/// Script parameters
531e58f1 41 $userid = required_param('id', PARAM_INT);
42 $noframesjs = optional_param('noframesjs', 0, PARAM_BOOL);
5d6b319b 43
a6855934 44 $url = new moodle_url('/message/discussion.php', array('id'=>$userid));
eb5334ff 45 if ($noframesjs !== 0) {
46 $url->param('noframesjs', $noframesjs);
47 }
48 $PAGE->set_url($url);
49
5d6b319b 50/// Check the user we are talking to is valid
d756fe69 51 if (! $user = $DB->get_record('user', array('id'=>$userid))) {
4f2a63c8 52 print_error('invaliduserid');
5d6b319b 53 }
54
f5fc83e8 55 if ($user->deleted) {
78946b9b 56 $PAGE->set_pagelayout('popup');
a1b53dcf 57 $PAGE->set_title(get_string('discussion', 'message').': '.fullname($user));
58 echo $OUTPUT->header();
59 echo $OUTPUT->heading(get_string('userdeleted'), 1);
60 echo $OUTPUT->footer();
f5fc83e8 61 die;
62 }
63
531e58f1 64/// Check if frame&jsless mode selected
65 if (!get_user_preferences('message_noframesjs', 0) and !$noframesjs) {
66
67 /// Print frameset to contain all the various panes
68 @header('Content-Type: text/html; charset=utf-8');
69
5d6b319b 70?>
bac472e0 71<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
5d6b319b 72 <html>
bac472e0 73 <head>
810944af 74 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
bac472e0 75 <title><?php echo get_string('discussion', 'message').': '.fullname($user) ?></title>
78946b9b 76 <link rel="shortcut icon" href="<?php echo $CFG->wwwroot.'/theme/'.$PAGE->theme->name; ?>/pix/favicon.ico" />
bac472e0 77 </head>
3aa3cf3d 78 <frameset rows="110,*,0,220">
531e58f1 79 <noframes><body><?php
423c0290 80 echo '<a href="discussion.php?id='.$userid.'&amp;noframesjs=1">'.get_string('noframesjs', 'message').'</a>';
531e58f1 81 ?></body></noframes>
0d3bb48d 82
531e58f1 83 <frame src="user.php?id=<?php p($user->id)?>&amp;frame=user" name="user"
0d3bb48d 84 scrolling="no" marginwidth="0" marginheight="0" frameborder="0" />
531e58f1 85 <frame src="messages.php" name="messages"
0d3bb48d 86 scrolling="yes" marginwidth="10" marginheight="10" frameborder="0" />
531e58f1 87 <frame src="refresh.php?id=<?php p($user->id)?>&amp;name=<?php echo urlencode(fullname($user)) ?>" name="refresh"
0d3bb48d 88 scrolling="no" marginwidth="0" marginheight="0" frameborder="0" />
1d422980 89
531e58f1 90 <frame src="send.php?id=<?php p($user->id)?>" name="send"
0d3bb48d 91 scrolling="no" marginwidth="2" marginheight="2" frameborder="0" />
5d6b319b 92 </frameset>
5d6b319b 93 </html>
531e58f1 94
95<?php
96 die;
97 }
98
99/// user wants simple frame&js-less mode
100
101 $start = optional_param('start', time(), PARAM_INT);
a8320bc4 102 $message = optional_param('message', '', PARAM_CLEAN); //TODO: this is wrong we must use text_format() instead
531e58f1 103 $format = optional_param('format', FORMAT_MOODLE, PARAM_INT);
104 $refresh = optional_param('refresh', '', PARAM_RAW);
adb61bc2 105 $last = optional_param('last', 0, PARAM_INT);
106 $newonly = optional_param('newonly', 0, PARAM_BOOL);
531e58f1 107
108 $addcontact = optional_param('addcontact', 0, PARAM_INT); // adding a contact
109 $removecontact = optional_param('removecontact', 0, PARAM_INT); // removing a contact
110 $blockcontact = optional_param('blockcontact', 0, PARAM_INT); // blocking a contact
111 $unblockcontact = optional_param('unblockcontact', 0, PARAM_INT); // unblocking a contact
112
113 if ($addcontact and confirm_sesskey()) {
114 add_to_log(SITEID, 'message', 'add contact',
115 'discussion.php?user1='.$addcontact.'&amp;user2='.$USER->id, $addcontact);
116 message_add_contact($addcontact);
117 }
118 if ($removecontact and confirm_sesskey()) {
119 add_to_log(SITEID, 'message', 'remove contact',
120 'discussion.php?user1='.$removecontact.'&amp;user2='.$USER->id, $removecontact);
121 message_remove_contact($removecontact);
122 }
123 if ($blockcontact and confirm_sesskey()) {
124 add_to_log(SITEID, 'message', 'block contact',
125 'discussion.php?user1='.$blockcontact.'&amp;user2='.$USER->id, $blockcontact);
126 message_block_contact($blockcontact);
127 }
128 if ($unblockcontact and confirm_sesskey()) {
129 add_to_log(SITEID, 'message', 'unblock contact',
130 'history.php?user1='.$unblockcontact.'&amp;user2='.$USER->id, $unblockcontact);
131 message_unblock_contact($unblockcontact);
132 }
133
134/// Check that the user is not blocking us!!
d756fe69 135 if ($contact = $DB->get_record('message_contacts', array('userid'=>$user->id, 'contactid'=>$USER->id))) {
957f6fc9 136 if ($contact->blocked and !has_capability('moodle/site:readallmessages', get_context_instance(CONTEXT_SYSTEM))) {
f2103096 137 echo $OUTPUT->heading(get_string('userisblockingyou', 'message'));
531e58f1 138 exit;
139 }
140 }
141 if (get_user_preferences('message_blocknoncontacts', 0, $user->id)) { // User is blocking non-contacts
142 if (empty($contact)) { // We are not a contact!
f2103096 143 echo $OUTPUT->heading(get_string('userisblockingyounoncontact', 'message'));
531e58f1 144 exit;
145 }
146 }
147
148 $refreshedmessage = '';
149
150 if (!empty($refresh) and data_submitted()) {
151 $refreshedmessage = $message;
152
153 } else if (empty($refresh) and data_submitted() and confirm_sesskey()) {
154 if ($message!='') {
155 message_post_message($USER, $user, $message, $format, 'direct');
156 }
adb61bc2 157 redirect('discussion.php?id='.$userid.'&amp;start='.$start.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last);
531e58f1 158 }
159
160
161 $userfullname = fullname($user);
162 $mefullname = fullname($USER);
163
78946b9b 164 $PAGE->set_pagelayout('popup');
a1b53dcf 165 $PAGE->set_title(get_string('discussion', 'message').': '.fullname($user));
166 echo $OUTPUT->header();
167
531e58f1 168 echo '<div class="message-discussion-noframes">';
169 echo '<div id="userinfo">';
812dbaf7 170 echo $OUTPUT->user_picture($user, array('size'=>48, 'courseid'=>SITEID));
adb61bc2 171 echo '<div class="name"><h1>'.$userfullname.'</h1></div>';
531e58f1 172 echo '<div class="commands"><ul>';
d756fe69 173 if ($contact = $DB->get_record('message_contacts', array('userid'=>$USER->id, 'contactid'=>$user->id))) {
531e58f1 174 if ($contact->blocked) {
175 echo '<li>';
adb61bc2 176 message_contact_link($user->id, 'add', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
531e58f1 177 echo '</li><li>';
adb61bc2 178 message_contact_link($user->id, 'unblock', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
531e58f1 179 echo '</li>';
180 } else {
181 echo '<li>';
adb61bc2 182 message_contact_link($user->id, 'remove', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
531e58f1 183 echo '</li><li>';
adb61bc2 184 message_contact_link($user->id, 'block', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
531e58f1 185 echo '</li>';
186 }
187 } else {
188 echo '<li>';
adb61bc2 189 message_contact_link($user->id, 'add', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
531e58f1 190 echo '</li><li>';
adb61bc2 191 message_contact_link($user->id, 'block', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
531e58f1 192 echo '</li>';
193 }
194 echo '<li>';
195 message_history_link($user->id, 0, false, '', '', 'both');
196 echo '</li>';
197 echo '</ul>';
198 echo '</div>';
199 echo '</div>'; // class="userinfo"
200
201 echo '<div id="send">';
c7225fe2 202 echo '<form id="editing" method="post" action="discussion.php">';
531e58f1 203
204 $usehtmleditor = (can_use_html_editor() && get_user_preferences('message_usehtmleditor', 0));
205 echo '<h1><label for="edit-message">'.get_string('sendmessage', 'message').'</label></h1>';
adb61bc2 206 echo '<div>';
531e58f1 207 if ($usehtmleditor) {
208 print_textarea(true, 8, 34, 100, 100, 'message', $refreshedmessage);
531e58f1 209 echo '<input type="hidden" name="format" value="'.FORMAT_HTML.'" />';
210 } else {
eb449c58 211 print_textarea(false, 8, 50, 0, 0, 'message', $refreshedmessage);
531e58f1 212 echo '<input type="hidden" name="format" value="'.FORMAT_MOODLE.'" />';
213 }
adb61bc2 214 echo '</div><div>';
c7225fe2 215 echo '<input type="hidden" name="id" value="'.$user->id.'" />';
216 echo '<input type="hidden" name="start" value="'.$start.'" />';
217 echo '<input type="hidden" name="noframesjs" value="'.$noframesjs.'" />';
218 echo '<input type="hidden" name="last" value="'.time().'" />';
d4a1fcaf 219 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
adb61bc2 220 echo '<input type="submit" value="'.get_string('sendmessage', 'message').'" />&nbsp;';
6ee78cee 221 echo '<input type="submit" name="refresh" value="'.get_string('refresh').'" />';
adb61bc2 222 echo '<input type="checkbox" name="newonly" id="newonly" '.($newonly?'checked="checked" ':'').'/><label for="newonly">'.get_string('newonlymsg', 'message').'</label>';
223 echo '</div>';
531e58f1 224 echo '</form>';
225 echo '</div>';
226
227 echo '<div id="messages">';
228 echo '<h1>'.get_string('messages', 'message').'</h1>';
229
230 $allmessages = array();
231 $playbeep = false;
8e803c3f 232 $options = new stdClass();
531e58f1 233 $options->para = false;
234 $options->newlines = true;
1d422980 235
d756fe69 236 $params = array('uid1'=>$USER->id ,'userid1'=>$userid, 'start1'=>$start, 'uid2'=>$USER->id ,'userid2'=>$userid, 'start2'=>$start);
adb61bc2 237 if ($newonly) {
d756fe69 238 $lastsql1 = " AND timecreated > :last1";
239 $lastsql2 = " AND timecreated > :last2";
240 $params['last1'] = $last;
241 $params['last2'] = $last;
adb61bc2 242 } else {
d756fe69 243 $lastsql1 = "";
244 $lastsql2 = "";
adb61bc2 245 }
246
1c50df9f 247 //LR: change here the way to
d756fe69 248 if ($messages = $DB->get_records_select('message_read', "(useridto = :uid1 AND useridfrom = :userid1 AND timeread > :start1 $lastsql1) OR (useridto = :userid2 AND useridfrom = :uid2 AND timeread > :start2 $lastsql2)", $params)) {
531e58f1 249 foreach ($messages as $message) {
54d8f804 250 $time = userdate($message->timecreated, get_string('strftimedatetimeshort'));
1d422980 251
531e58f1 252 if ($message->useridfrom == $USER->id) {
253 $fullname = $mefullname;
254 } else {
255 $fullname = $userfullname;
256 }
257
a1b53dcf 258 if ($message->fullmessageformat == FORMAT_HTML){
259 $printmessage = format_text($message->fullmessagehtml, $message->fullmessageformat, $options, 0);
1c50df9f 260 } else{
a1b53dcf 261 $printmessage = format_text($message->fullmessage, $message->fullmessageformat, $options, 0);
1c50df9f 262 }
531e58f1 263 $printmessage = '<div class="message other"><span class="author">'.$fullname.'</span> '.
264 '<span class="time">['.$time.']</span>: '.
265 '<span class="content">'.$printmessage.'</span></div>';
54bb3458 266 $i=0;
71666cf3 267 $sortkey = $message->timecreated."$i"; // we need string because we would run out of int range
54bb3458 268 while (array_key_exists($sortkey, $allmessages)) {
269 $i++;
270 $sortkey = $message->timecreated."$i";
271 }
272 $allmessages[$sortkey] = $printmessage;
531e58f1 273 }
274 }
275
d756fe69 276 if ($messages = $DB->get_records_select('message', "useridto = :userid1 AND useridfrom = :uid1 $lastsql1", $params)) {
531e58f1 277 foreach ($messages as $message) {
54d8f804 278 $time = userdate($message->timecreated, get_string('strftimedatetimeshort'));
1d422980 279
a1b53dcf 280 if ($message->fullmessageformat == FORMAT_HTML){
281 $printmessage = format_text($message->fullmessagehtml, $message->fullmessageformat, $options, 0);
1c50df9f 282 } else{
a1b53dcf 283 $printmessage = format_text($message->fullmessage, $message->fullmessageformat, $options, 0);
1c50df9f 284 }
531e58f1 285 $printmessage = '<div class="message other"><span class="author">'.$mefullname.'</span> '.
286 '<span class="time">['.$time.']</span>: '.
287 '<span class="content">'.$printmessage.'</span></div>';
54bb3458 288 $i=0;
71666cf3 289 $sortkey = $message->timecreated."$i"; // we need string because we would run out of int range
54bb3458 290 while (array_key_exists($sortkey, $allmessages)) {
291 $i++;
292 $sortkey = $message->timecreated."$i";
293 }
294 $allmessages[$sortkey] = $printmessage;
531e58f1 295 }
296 }
71666cf3 297 /*Get still to be read message, use message/lib.php function*/
1d422980 298 $messages = message_get_popup_messages($USER->id, $userid);
1c50df9f 299 if ($messages) {
300 foreach ($messages as $message) {
301 $time = userdate($message->timecreated, get_string('strftimedatetimeshort'));
1d422980 302
a1b53dcf 303 if ($message->fullmessageformat == FORMAT_HTML){
304 $printmessage = format_text($message->fullmessagehtml, $message->fullmessageformat, $options, 0);
1c50df9f 305 } else{
a1b53dcf 306 $printmessage = format_text($message->fullmessage, $message->fullmessageformat, $options, 0);
1c50df9f 307 }
308 $printmessage = '<div class="message other"><span class="author">'.$userfullname.'</span> '.
309 '<span class="time">['.$time.']</span>: '.
310 '<span class="content">'.$printmessage.'</span></div>';
311 $i=0;
71666cf3 312 $sortkey = $message->timecreated."$i"; // we need string because we would run out of int range
1c50df9f 313 while (array_key_exists($sortkey, $allmessages)) {
314 $i++;
315 $sortkey = $message->timecreated."$i";
316 }
317 $allmessages[$sortkey] = $printmessage;
1d422980 318
1c50df9f 319 if ($message->timecreated < $start) {
320 $start = $message->timecreated; // move start back so that we see all current history
321 }
322 }
323 $playbeep = true;
324 }
325 /* old code, to be deleted
d756fe69 326 if ($messages = $DB->get_records_select('message', "useridto = :uid2 AND useridfrom = userid2 $lastsql2", $params)) {
531e58f1 327 foreach ($messages as $message) {
54d8f804 328 $time = userdate($message->timecreated, get_string('strftimedatetimeshort'));
531e58f1 329
330 $printmessage = format_text($message->message, $message->format, $options, 0);
531e58f1 331 $printmessage = '<div class="message other"><span class="author">'.$userfullname.'</span> '.
332 '<span class="time">['.$time.']</span>: '.
333 '<span class="content">'.$printmessage.'</span></div>';
54bb3458 334 $i=0;
71666cf3 335 $sortkey = $message->timecreated."$i"; // we need string because we would run out of int range
54bb3458 336 while (array_key_exists($sortkey, $allmessages)) {
337 $i++;
338 $sortkey = $message->timecreated."$i";
339 }
340 $allmessages[$sortkey] = $printmessage;
531e58f1 341
342 /// Move the entry to the other table
343
344 $messageid = $message->id;
345 unset($message->id);
346 $message->timeread = time();
9d97f08e
PS
347 $DB->insert_record('message_read', $message);
348 $DB->delete_records('message', array('id'=>$messageid));
531e58f1 349 if ($message->timecreated < $start) {
350 $start = $message->timecreated; // move start back so that we see all current history
351 }
352 }
353 $playbeep = true;
1c50df9f 354 }*/
531e58f1 355
356 krsort($allmessages);
357
358 if (empty($allmessages)) {
359 echo get_string('nomessagesfound', 'message');
360 } else {
d53f1cdb 361 echo '<ul class="messagelist">';
531e58f1 362 foreach ($allmessages as $message) {
d53f1cdb 363 echo '<li>';
531e58f1 364 echo $message;
d53f1cdb 365 echo '</li>';
531e58f1 366 }
d53f1cdb 367 echo '</ul>';
531e58f1 368 if ($playbeep and get_user_preferences('message_beepnewmessage', 0)) {
369 echo '<embed src="bell.wav" autostart="true" hidden="true" name="bell" />';
370 }
371 }
372
373 echo '</div></div>';
374
a1b53dcf 375 echo $OUTPUT->footer();
3aa3cf3d 376?>