MDL-14406
[moodle-linuxchix.git] / message / discussion.php
blobe1cbaca52435241ba95d053311d0db7e47aa7939
1 <?php // $Id$
3 require('../config.php');
4 require('lib.php');
6 require_login();
8 if (isguest()) {
9 redirect($CFG->wwwroot);
12 if (empty($CFG->messaging)) {
13 error("Messaging is disabled on this site");
16 /// Script parameters
17 $userid = required_param('id', PARAM_INT);
18 $noframesjs = optional_param('noframesjs', 0, PARAM_BOOL);
20 /// Check the user we are talking to is valid
21 if (! $user = get_record('user', 'id', $userid)) {
22 error("User ID was incorrect");
25 /// Check if frame&jsless mode selected
26 if (!get_user_preferences('message_noframesjs', 0) and !$noframesjs) {
28 /// Print frameset to contain all the various panes
29 @header('Content-Type: text/html; charset=utf-8');
32 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
33 <html>
34 <head>
35 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
36 <title><?php echo get_string('discussion', 'message').': '.fullname($user) ?></title>
37 </head>
38 <frameset rows="110,*,0,220">
39 <noframes><body><?php
40 echo '<a href="discussion.php?id='.$userid.'&amp;noframesjs=1">'.get_string('noframesjs', 'message').'</a>';
41 ?></body></noframes>
43 <frame src="user.php?id=<?php p($user->id)?>&amp;frame=user" name="user"
44 scrolling="no" marginwidth="0" marginheight="0" frameborder="0" />
45 <frame src="messages.php" name="messages"
46 scrolling="yes" marginwidth="10" marginheight="10" frameborder="0" />
47 <frame src="refresh.php?id=<?php p($user->id)?>&amp;name=<?php echo urlencode(fullname($user)) ?>" name="refresh"
48 scrolling="no" marginwidth="0" marginheight="0" frameborder="0" />
50 <frame src="send.php?id=<?php p($user->id)?>" name="send"
51 scrolling="no" marginwidth="2" marginheight="2" frameborder="0" />
52 </frameset>
53 </html>
55 <?php
56 die;
59 /// user wants simple frame&js-less mode
61 $start = optional_param('start', time(), PARAM_INT);
62 $message = optional_param('message', '', PARAM_CLEAN);
63 $format = optional_param('format', FORMAT_MOODLE, PARAM_INT);
64 $refresh = optional_param('refresh', '', PARAM_RAW);
65 $last = optional_param('last', 0, PARAM_INT);
66 $newonly = optional_param('newonly', 0, PARAM_BOOL);
68 $addcontact = optional_param('addcontact', 0, PARAM_INT); // adding a contact
69 $removecontact = optional_param('removecontact', 0, PARAM_INT); // removing a contact
70 $blockcontact = optional_param('blockcontact', 0, PARAM_INT); // blocking a contact
71 $unblockcontact = optional_param('unblockcontact', 0, PARAM_INT); // unblocking a contact
73 if ($addcontact and confirm_sesskey()) {
74 add_to_log(SITEID, 'message', 'add contact',
75 'discussion.php?user1='.$addcontact.'&amp;user2='.$USER->id, $addcontact);
76 message_add_contact($addcontact);
78 if ($removecontact and confirm_sesskey()) {
79 add_to_log(SITEID, 'message', 'remove contact',
80 'discussion.php?user1='.$removecontact.'&amp;user2='.$USER->id, $removecontact);
81 message_remove_contact($removecontact);
83 if ($blockcontact and confirm_sesskey()) {
84 add_to_log(SITEID, 'message', 'block contact',
85 'discussion.php?user1='.$blockcontact.'&amp;user2='.$USER->id, $blockcontact);
86 message_block_contact($blockcontact);
88 if ($unblockcontact and confirm_sesskey()) {
89 add_to_log(SITEID, 'message', 'unblock contact',
90 'history.php?user1='.$unblockcontact.'&amp;user2='.$USER->id, $unblockcontact);
91 message_unblock_contact($unblockcontact);
94 /// Check that the user is not blocking us!!
95 if ($contact = get_record('message_contacts', 'userid', $user->id, 'contactid', $USER->id)) {
96 if ($contact->blocked and !has_capability('moodle/site:readallmessages', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
97 print_heading(get_string('userisblockingyou', 'message'));
98 exit;
101 if (get_user_preferences('message_blocknoncontacts', 0, $user->id)) { // User is blocking non-contacts
102 if (empty($contact)) { // We are not a contact!
103 print_heading(get_string('userisblockingyounoncontact', 'message'));
104 exit;
108 $refreshedmessage = '';
110 if (!empty($refresh) and data_submitted()) {
111 $refreshedmessage = $message;
113 } else if (empty($refresh) and data_submitted() and confirm_sesskey()) {
114 if ($message!='') {
115 message_post_message($USER, $user, $message, $format, 'direct');
117 redirect('discussion.php?id='.$userid.'&amp;start='.$start.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last);
121 $userfullname = fullname($user);
122 $mefullname = fullname($USER);
124 print_header(get_string('discussion', 'message').': '.fullname($user), '', '', 'edit-message');
125 echo '<div class="message-discussion-noframes">';
126 echo '<div id="userinfo">';
127 echo print_user_picture($user, SITEID, $user->picture, 48, true, true, 'userwindow');
128 echo '<div class="name"><h1>'.$userfullname.'</h1></div>';
129 echo '<div class="commands"><ul>';
130 if ($contact = get_record('message_contacts', 'userid', $USER->id, 'contactid', $user->id)) {
131 if ($contact->blocked) {
132 echo '<li>';
133 message_contact_link($user->id, 'add', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
134 echo '</li><li>';
135 message_contact_link($user->id, 'unblock', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
136 echo '</li>';
137 } else {
138 echo '<li>';
139 message_contact_link($user->id, 'remove', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
140 echo '</li><li>';
141 message_contact_link($user->id, 'block', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
142 echo '</li>';
144 } else {
145 echo '<li>';
146 message_contact_link($user->id, 'add', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
147 echo '</li><li>';
148 message_contact_link($user->id, 'block', false, 'discussion.php?id='.$user->id.'&amp;noframesjs='.$noframesjs.'&amp;newonly='.$newonly.'&amp;last='.$last, true);
149 echo '</li>';
151 echo '<li>';
152 message_history_link($user->id, 0, false, '', '', 'both');
153 echo '</li>';
154 echo '</ul>';
155 echo '</div>';
156 echo '</div>'; // class="userinfo"
158 echo '<div id="send">';
159 echo '<form id="editing" method="post" action="discussion.php">';
161 $usehtmleditor = (can_use_html_editor() && get_user_preferences('message_usehtmleditor', 0));
162 echo '<h1><label for="edit-message">'.get_string('sendmessage', 'message').'</label></h1>';
163 echo '<div>';
164 if ($usehtmleditor) {
165 print_textarea(true, 8, 34, 100, 100, 'message', $refreshedmessage);
166 use_html_editor('message', 'formatblock subscript superscript copy cut paste clean undo redo justifyleft justifycenter justifyright justifyfull lefttoright righttoleft insertorderedlist insertunorderedlist outdent indent inserthorizontalrule createanchor nolink inserttable');
167 echo '<input type="hidden" name="format" value="'.FORMAT_HTML.'" />';
168 } else {
169 print_textarea(false, 8, 50, 0, 0, 'message', $refreshedmessage);
170 echo '<input type="hidden" name="format" value="'.FORMAT_MOODLE.'" />';
172 echo '</div><div>';
173 echo '<input type="hidden" name="id" value="'.$user->id.'" />';
174 echo '<input type="hidden" name="start" value="'.$start.'" />';
175 echo '<input type="hidden" name="noframesjs" value="'.$noframesjs.'" />';
176 echo '<input type="hidden" name="last" value="'.time().'" />';
177 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
178 echo '<input type="submit" value="'.get_string('sendmessage', 'message').'" />&nbsp;';
179 echo '<input type="submit" name="refresh" value="'.get_string('refresh').'" />';
180 echo '<input type="checkbox" name="newonly" id="newonly" '.($newonly?'checked="checked" ':'').'/><label for="newonly">'.get_string('newonlymsg', 'message').'</label>';
181 echo '</div>';
182 echo '</form>';
183 echo '</div>';
185 echo '<div id="messages">';
186 echo '<h1>'.get_string('messages', 'message').'</h1>';
188 $allmessages = array();
189 $playbeep = false;
190 $options = new object();
191 $options->para = false;
192 $options->newlines = true;
194 if ($newonly) {
195 $lastsql = " AND timecreated > $last";
196 } else {
197 $lastsql = "";
200 if ($messages = get_records_select('message_read', "(useridto = '$USER->id' AND useridfrom = '$userid' AND timeread > '$start' $lastsql) OR (useridto = '$userid' AND useridfrom = '$USER->id' AND timeread > '$start' $lastsql)")) {
201 foreach ($messages as $message) {
202 $time = userdate($message->timecreated, get_string('strftimedaytime'));
204 if ($message->useridfrom == $USER->id) {
205 $fullname = $mefullname;
206 } else {
207 $fullname = $userfullname;
210 $printmessage = format_text($message->message, $message->format, $options, 0);
211 $printmessage = '<div class="message other"><span class="author">'.$fullname.'</span> '.
212 '<span class="time">['.$time.']</span>: '.
213 '<span class="content">'.$printmessage.'</span></div>';
214 $i=0;
215 $sortkey = $message->timecreated."$i"; // we need string bacause we would run out of int range
216 while (array_key_exists($sortkey, $allmessages)) {
217 $i++;
218 $sortkey = $message->timecreated."$i";
220 $allmessages[$sortkey] = $printmessage;
224 if ($messages = get_records_select('message', "useridto = '$userid' AND useridfrom = '$USER->id' $lastsql")) {
225 foreach ($messages as $message) {
226 $time = userdate($message->timecreated, get_string('strftimedaytime'));
228 $printmessage = format_text($message->message, $message->format, $options, 0);
229 $printmessage = '<div class="message other"><span class="author">'.$mefullname.'</span> '.
230 '<span class="time">['.$time.']</span>: '.
231 '<span class="content">'.$printmessage.'</span></div>';
232 $i=0;
233 $sortkey = $message->timecreated."$i"; // we need string bacause we would run out of int range
234 while (array_key_exists($sortkey, $allmessages)) {
235 $i++;
236 $sortkey = $message->timecreated."$i";
238 $allmessages[$sortkey] = $printmessage;
242 if ($messages = get_records_select('message', "useridto = '$USER->id' AND useridfrom = '$userid' $lastsql")) {
243 foreach ($messages as $message) {
244 $time = userdate($message->timecreated, get_string('strftimedaytime'));
246 $printmessage = format_text($message->message, $message->format, $options, 0);
247 $printmessage = '<div class="message other"><span class="author">'.$userfullname.'</span> '.
248 '<span class="time">['.$time.']</span>: '.
249 '<span class="content">'.$printmessage.'</span></div>';
250 $i=0;
251 $sortkey = $message->timecreated."$i"; // we need string bacause we would run out of int range
252 while (array_key_exists($sortkey, $allmessages)) {
253 $i++;
254 $sortkey = $message->timecreated."$i";
256 $allmessages[$sortkey] = $printmessage;
258 /// Move the entry to the other table
260 $messageid = $message->id;
261 unset($message->id);
262 $message->timeread = time();
263 $message = addslashes_object($message);
264 if (insert_record('message_read', $message)) {
265 delete_records('message', 'id', $messageid);
267 if ($message->timecreated < $start) {
268 $start = $message->timecreated; // move start back so that we see all current history
271 $playbeep = true;
274 krsort($allmessages);
276 if (empty($allmessages)) {
277 echo get_string('nomessagesfound', 'message');
278 } else {
279 echo '<ul class="messagelist">';
280 foreach ($allmessages as $message) {
281 echo '<li>';
282 echo $message;
283 echo '</li>';
285 echo '</ul>';
286 if ($playbeep and get_user_preferences('message_beepnewmessage', 0)) {
287 echo '<embed src="bell.wav" autostart="true" hidden="true" name="bell" />';
291 echo '</div></div>';
293 print_footer('none');