help string rewording thanks to Joseph R and Sam H
[moodle.git] / message / discussion.php
blob8e9bf7324540209ce8ab0246c1b86051e9b02dac
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 * Part of the message section of Moodle
21 * @author Luis Rodrigues and Martin Dougiamas
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package message
26 require('../config.php');
27 require('lib.php');
29 require_login();
31 if (isguestuser()) {
32 redirect($CFG->wwwroot);
35 if (empty($CFG->messaging)) {
36 print_error('disabled', 'message');
39 /// Script parameters
40 $userid = required_param('id', PARAM_INT);
41 $noframesjs = optional_param('noframesjs', 0, PARAM_BOOL);
43 $url = new moodle_url('/message/discussion.php', array('id'=>$userid));
44 if ($noframesjs !== 0) {
45 $url->param('noframesjs', $noframesjs);
47 $PAGE->set_url($url);
49 /// Check the user we are talking to is valid
50 if (! $user = $DB->get_record('user', array('id'=>$userid))) {
51 print_error('invaliduserid');
54 if ($user->deleted) {
55 $PAGE->set_pagelayout('popup');
56 $PAGE->set_title(get_string('discussion', 'message').': '.fullname($user));
57 echo $OUTPUT->header();
58 echo $OUTPUT->heading(get_string('userdeleted'), 1);
59 echo $OUTPUT->footer();
60 die;
63 /// Check if frame&jsless mode selected
64 if (!get_user_preferences('message_noframesjs', 0) and !$noframesjs) {
66 /// Print frameset to contain all the various panes
67 @header('Content-Type: text/html; charset=utf-8');
70 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
71 <html>
72 <head>
73 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
74 <title><?php echo get_string('discussion', 'message').': '.fullname($user) ?></title>
75 <link rel="shortcut icon" href="<?php echo $CFG->wwwroot.'/theme/'.$PAGE->theme->name; ?>/pix/favicon.ico" />
76 </head>
77 <frameset rows="110,*,0,220">
78 <noframes><body><?php
79 echo '<a href="discussion.php?id='.$userid.'&amp;noframesjs=1">'.get_string('noframesjs', 'message').'</a>';
80 ?></body></noframes>
82 <frame src="user.php?id=<?php p($user->id)?>&amp;frame=user" name="user"
83 scrolling="no" marginwidth="0" marginheight="0" frameborder="0" />
84 <frame src="messages.php" name="messages"
85 scrolling="yes" marginwidth="10" marginheight="10" frameborder="0" />
86 <frame src="refresh.php?id=<?php p($user->id)?>&amp;name=<?php echo urlencode(fullname($user)) ?>" name="refresh"
87 scrolling="no" marginwidth="0" marginheight="0" frameborder="0" />
89 <frame src="send.php?id=<?php p($user->id)?>" name="send"
90 scrolling="no" marginwidth="2" marginheight="2" frameborder="0" />
91 </frameset>
92 </html>
94 <?php
95 die;
98 /// user wants simple frame&js-less mode
100 $start = optional_param('start', time(), PARAM_INT);
101 $message = optional_param('message', '', PARAM_CLEAN);
102 $format = optional_param('format', FORMAT_MOODLE, PARAM_INT);
103 $refresh = optional_param('refresh', '', PARAM_RAW);
104 $last = optional_param('last', 0, PARAM_INT);
105 $newonly = optional_param('newonly', 0, PARAM_BOOL);
107 $addcontact = optional_param('addcontact', 0, PARAM_INT); // adding a contact
108 $removecontact = optional_param('removecontact', 0, PARAM_INT); // removing a contact
109 $blockcontact = optional_param('blockcontact', 0, PARAM_INT); // blocking a contact
110 $unblockcontact = optional_param('unblockcontact', 0, PARAM_INT); // unblocking a contact
112 if ($addcontact and confirm_sesskey()) {
113 add_to_log(SITEID, 'message', 'add contact',
114 'discussion.php?user1='.$addcontact.'&amp;user2='.$USER->id, $addcontact);
115 message_add_contact($addcontact);
117 if ($removecontact and confirm_sesskey()) {
118 add_to_log(SITEID, 'message', 'remove contact',
119 'discussion.php?user1='.$removecontact.'&amp;user2='.$USER->id, $removecontact);
120 message_remove_contact($removecontact);
122 if ($blockcontact and confirm_sesskey()) {
123 add_to_log(SITEID, 'message', 'block contact',
124 'discussion.php?user1='.$blockcontact.'&amp;user2='.$USER->id, $blockcontact);
125 message_block_contact($blockcontact);
127 if ($unblockcontact and confirm_sesskey()) {
128 add_to_log(SITEID, 'message', 'unblock contact',
129 'history.php?user1='.$unblockcontact.'&amp;user2='.$USER->id, $unblockcontact);
130 message_unblock_contact($unblockcontact);
133 /// Check that the user is not blocking us!!
134 if ($contact = $DB->get_record('message_contacts', array('userid'=>$user->id, 'contactid'=>$USER->id))) {
135 if ($contact->blocked and !has_capability('moodle/site:readallmessages', get_context_instance(CONTEXT_SYSTEM))) {
136 echo $OUTPUT->heading(get_string('userisblockingyou', 'message'));
137 exit;
140 if (get_user_preferences('message_blocknoncontacts', 0, $user->id)) { // User is blocking non-contacts
141 if (empty($contact)) { // We are not a contact!
142 echo $OUTPUT->heading(get_string('userisblockingyounoncontact', 'message'));
143 exit;
147 $refreshedmessage = '';
149 if (!empty($refresh) and data_submitted()) {
150 $refreshedmessage = $message;
152 } else if (empty($refresh) and data_submitted() and confirm_sesskey()) {
153 if ($message!='') {
154 message_post_message($USER, $user, $message, $format, 'direct');
156 redirect('discussion.php?id='.$userid.'&amp;start='.$start.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last);
160 $userfullname = fullname($user);
161 $mefullname = fullname($USER);
163 $PAGE->set_pagelayout('popup');
164 $PAGE->set_title(get_string('discussion', 'message').': '.fullname($user));
165 echo $OUTPUT->header();
167 echo '<div class="message-discussion-noframes">';
168 echo '<div id="userinfo">';
169 echo $OUTPUT->user_picture($user, array('size'=>48, 'courseid'=>SITEID));
170 echo '<div class="name"><h1>'.$userfullname.'</h1></div>';
171 echo '<div class="commands"><ul>';
172 if ($contact = $DB->get_record('message_contacts', array('userid'=>$USER->id, 'contactid'=>$user->id))) {
173 if ($contact->blocked) {
174 echo '<li>';
175 message_contact_link($user->id, 'add', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
176 echo '</li><li>';
177 message_contact_link($user->id, 'unblock', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
178 echo '</li>';
179 } else {
180 echo '<li>';
181 message_contact_link($user->id, 'remove', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
182 echo '</li><li>';
183 message_contact_link($user->id, 'block', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
184 echo '</li>';
186 } else {
187 echo '<li>';
188 message_contact_link($user->id, 'add', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
189 echo '</li><li>';
190 message_contact_link($user->id, 'block', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
191 echo '</li>';
193 echo '<li>';
194 message_history_link($user->id, 0, false, '', '', 'both');
195 echo '</li>';
196 echo '</ul>';
197 echo '</div>';
198 echo '</div>'; // class="userinfo"
200 echo '<div id="send">';
201 echo '<form id="editing" method="post" action="discussion.php">';
203 $usehtmleditor = (can_use_html_editor() && get_user_preferences('message_usehtmleditor', 0));
204 echo '<h1><label for="edit-message">'.get_string('sendmessage', 'message').'</label></h1>';
205 echo '<div>';
206 if ($usehtmleditor) {
207 print_textarea(true, 8, 34, 100, 100, 'message', $refreshedmessage);
208 echo '<input type="hidden" name="format" value="'.FORMAT_HTML.'" />';
209 } else {
210 print_textarea(false, 8, 50, 0, 0, 'message', $refreshedmessage);
211 echo '<input type="hidden" name="format" value="'.FORMAT_MOODLE.'" />';
213 echo '</div><div>';
214 echo '<input type="hidden" name="id" value="'.$user->id.'" />';
215 echo '<input type="hidden" name="start" value="'.$start.'" />';
216 echo '<input type="hidden" name="noframesjs" value="'.$noframesjs.'" />';
217 echo '<input type="hidden" name="last" value="'.time().'" />';
218 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
219 echo '<input type="submit" value="'.get_string('sendmessage', 'message').'" />&nbsp;';
220 echo '<input type="submit" name="refresh" value="'.get_string('refresh').'" />';
221 echo '<input type="checkbox" name="newonly" id="newonly" '.($newonly?'checked="checked" ':'').'/><label for="newonly">'.get_string('newonlymsg', 'message').'</label>';
222 echo '</div>';
223 echo '</form>';
224 echo '</div>';
226 echo '<div id="messages">';
227 echo '<h1>'.get_string('messages', 'message').'</h1>';
229 $allmessages = array();
230 $playbeep = false;
231 $options = new object();
232 $options->para = false;
233 $options->newlines = true;
235 $params = array('uid1'=>$USER->id ,'userid1'=>$userid, 'start1'=>$start, 'uid2'=>$USER->id ,'userid2'=>$userid, 'start2'=>$start);
236 if ($newonly) {
237 $lastsql1 = " AND timecreated > :last1";
238 $lastsql2 = " AND timecreated > :last2";
239 $params['last1'] = $last;
240 $params['last2'] = $last;
241 } else {
242 $lastsql1 = "";
243 $lastsql2 = "";
246 //LR: change here the way to
247 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)) {
248 foreach ($messages as $message) {
249 $time = userdate($message->timecreated, get_string('strftimedatetimeshort'));
251 if ($message->useridfrom == $USER->id) {
252 $fullname = $mefullname;
253 } else {
254 $fullname = $userfullname;
257 if ($message->fullmessageformat == FORMAT_HTML){
258 $printmessage = format_text($message->fullmessagehtml, $message->fullmessageformat, $options, 0);
259 } else{
260 $printmessage = format_text($message->fullmessage, $message->fullmessageformat, $options, 0);
262 $printmessage = '<div class="message other"><span class="author">'.$fullname.'</span> '.
263 '<span class="time">['.$time.']</span>: '.
264 '<span class="content">'.$printmessage.'</span></div>';
265 $i=0;
266 $sortkey = $message->timecreated."$i"; // we need string bacause we would run out of int range
267 while (array_key_exists($sortkey, $allmessages)) {
268 $i++;
269 $sortkey = $message->timecreated."$i";
271 $allmessages[$sortkey] = $printmessage;
275 if ($messages = $DB->get_records_select('message', "useridto = :userid1 AND useridfrom = :uid1 $lastsql1", $params)) {
276 foreach ($messages as $message) {
277 $time = userdate($message->timecreated, get_string('strftimedatetimeshort'));
279 if ($message->fullmessageformat == FORMAT_HTML){
280 $printmessage = format_text($message->fullmessagehtml, $message->fullmessageformat, $options, 0);
281 } else{
282 $printmessage = format_text($message->fullmessage, $message->fullmessageformat, $options, 0);
284 $printmessage = '<div class="message other"><span class="author">'.$mefullname.'</span> '.
285 '<span class="time">['.$time.']</span>: '.
286 '<span class="content">'.$printmessage.'</span></div>';
287 $i=0;
288 $sortkey = $message->timecreated."$i"; // we need string bacause we would run out of int range
289 while (array_key_exists($sortkey, $allmessages)) {
290 $i++;
291 $sortkey = $message->timecreated."$i";
293 $allmessages[$sortkey] = $printmessage;
296 /*Get still to be read message, use message/lib.php funtion*/
297 $messages = message_get_popup_messages($USER->id, $userid);
298 if ($messages) {
299 foreach ($messages as $message) {
300 $time = userdate($message->timecreated, get_string('strftimedatetimeshort'));
302 if ($message->fullmessageformat == FORMAT_HTML){
303 $printmessage = format_text($message->fullmessagehtml, $message->fullmessageformat, $options, 0);
304 } else{
305 $printmessage = format_text($message->fullmessage, $message->fullmessageformat, $options, 0);
307 $printmessage = '<div class="message other"><span class="author">'.$userfullname.'</span> '.
308 '<span class="time">['.$time.']</span>: '.
309 '<span class="content">'.$printmessage.'</span></div>';
310 $i=0;
311 $sortkey = $message->timecreated."$i"; // we need string bacause we would run out of int range
312 while (array_key_exists($sortkey, $allmessages)) {
313 $i++;
314 $sortkey = $message->timecreated."$i";
316 $allmessages[$sortkey] = $printmessage;
318 if ($message->timecreated < $start) {
319 $start = $message->timecreated; // move start back so that we see all current history
322 $playbeep = true;
324 /* old code, to be deleted
325 if ($messages = $DB->get_records_select('message', "useridto = :uid2 AND useridfrom = userid2 $lastsql2", $params)) {
326 foreach ($messages as $message) {
327 $time = userdate($message->timecreated, get_string('strftimedatetimeshort'));
329 $printmessage = format_text($message->message, $message->format, $options, 0);
330 $printmessage = '<div class="message other"><span class="author">'.$userfullname.'</span> '.
331 '<span class="time">['.$time.']</span>: '.
332 '<span class="content">'.$printmessage.'</span></div>';
333 $i=0;
334 $sortkey = $message->timecreated."$i"; // we need string bacause we would run out of int range
335 while (array_key_exists($sortkey, $allmessages)) {
336 $i++;
337 $sortkey = $message->timecreated."$i";
339 $allmessages[$sortkey] = $printmessage;
341 /// Move the entry to the other table
343 $messageid = $message->id;
344 unset($message->id);
345 $message->timeread = time();
346 if ($DB->insert_record('message_read', $message)) {
347 $DB->delete_records('message', array('id'=>$messageid));
349 if ($message->timecreated < $start) {
350 $start = $message->timecreated; // move start back so that we see all current history
353 $playbeep = true;
356 krsort($allmessages);
358 if (empty($allmessages)) {
359 echo get_string('nomessagesfound', 'message');
360 } else {
361 echo '<ul class="messagelist">';
362 foreach ($allmessages as $message) {
363 echo '<li>';
364 echo $message;
365 echo '</li>';
367 echo '</ul>';
368 if ($playbeep and get_user_preferences('message_beepnewmessage', 0)) {
369 echo '<embed src="bell.wav" autostart="true" hidden="true" name="bell" />';
373 echo '</div></div>';
375 echo $OUTPUT->footer();