2009-09-29 03:54:14 +00:00
< ? php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Part of the message section of Moodle
*
* @ author Luis Rodrigues and Martin Dougiamas
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
* @ package message
*/
2006-12-25 22:34:05 +00:00
2005-04-14 09:28:07 +00:00
require ( '../config.php' );
require ( 'lib.php' );
require_login ();
2009-09-29 03:54:14 +00:00
if ( has_capability ( 'moodle/legacy:guest' , get_context_instance ( CONTEXT_SYSTEM ), 0 , false )) {
2005-04-14 09:28:07 +00:00
redirect ( $CFG -> wwwroot );
}
if ( empty ( $CFG -> messaging )) {
2008-06-13 06:55:25 +00:00
print_error ( 'disabled' , 'message' );
2005-04-14 09:28:07 +00:00
}
/// Script parameters
2006-12-25 22:34:05 +00:00
$userid = required_param ( 'id' , PARAM_INT );
$noframesjs = optional_param ( 'noframesjs' , 0 , PARAM_BOOL );
2005-04-14 09:28:07 +00:00
2009-09-29 03:54:14 +00:00
$url = new moodle_url ( $CFG -> wwwroot . '/message/discussion.php' , array ( 'id' => $userid ));
if ( $noframesjs !== 0 ) {
$url -> param ( 'noframesjs' , $noframesjs );
}
$PAGE -> set_url ( $url );
2005-04-14 09:28:07 +00:00
/// Check the user we are talking to is valid
2008-06-05 13:26:40 +00:00
if ( ! $user = $DB -> get_record ( 'user' , array ( 'id' => $userid ))) {
2008-06-15 10:13:30 +00:00
print_error ( 'invaliduserid' );
2005-04-14 09:28:07 +00:00
}
2008-07-05 14:52:39 +00:00
if ( $user -> deleted ) {
2009-12-16 18:00:58 +00:00
$PAGE -> set_pagelayout ( 'popup' );
2009-06-30 08:33:29 +00:00
$PAGE -> set_title ( get_string ( 'discussion' , 'message' ) . ': ' . fullname ( $user ));
echo $OUTPUT -> header ();
echo $OUTPUT -> heading ( get_string ( 'userdeleted' ), 1 );
echo $OUTPUT -> footer ();
2008-07-05 14:52:39 +00:00
die ;
}
2006-12-25 22:34:05 +00:00
/// Check if frame&jsless mode selected
if ( ! get_user_preferences ( 'message_noframesjs' , 0 ) and ! $noframesjs ) {
/// Print frameset to contain all the various panes
@ header ( 'Content-Type: text/html; charset=utf-8' );
2005-04-14 09:28:07 +00:00
?>
2005-11-05 10:50:35 +00:00
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Frameset//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd " >
2005-04-14 09:28:07 +00:00
< html >
2005-11-05 10:50:35 +00:00
< head >
2006-11-11 17:23:20 +00:00
< meta http - equiv = " content-type " content = " text/html; charset=utf-8 " />
2005-11-05 10:50:35 +00:00
< title >< ? php echo get_string ( 'discussion' , 'message' ) . ': ' . fullname ( $user ) ?> </title>
2009-12-16 18:00:58 +00:00
< link rel = " shortcut icon " href = " <?php echo $CFG->wwwroot .'/theme/'. $PAGE->theme ->name; ?>/pix/favicon.ico " />
2005-11-05 10:50:35 +00:00
</ head >
2007-08-27 03:22:16 +00:00
< frameset rows = " 110,*,0,220 " >
2006-12-25 22:34:05 +00:00
< noframes >< body >< ? php
2007-09-18 18:24:52 +00:00
echo '<a href="discussion.php?id=' . $userid . '&noframesjs=1">' . get_string ( 'noframesjs' , 'message' ) . '</a>' ;
2006-12-25 22:34:05 +00:00
?> </body></noframes>
2006-12-18 09:04:33 +00:00
2006-12-25 22:34:05 +00:00
< frame src = " user.php?id=<?php p( $user->id )?>&frame=user " name = " user "
2006-12-18 09:04:33 +00:00
scrolling = " no " marginwidth = " 0 " marginheight = " 0 " frameborder = " 0 " />
2006-12-25 22:34:05 +00:00
< frame src = " messages.php " name = " messages "
2006-12-18 09:04:33 +00:00
scrolling = " yes " marginwidth = " 10 " marginheight = " 10 " frameborder = " 0 " />
2006-12-25 22:34:05 +00:00
< frame src = " refresh.php?id=<?php p( $user->id )?>&name=<?php echo urlencode(fullname( $user )) ?> " name = " refresh "
2006-12-18 09:04:33 +00:00
scrolling = " no " marginwidth = " 0 " marginheight = " 0 " frameborder = " 0 " />
2009-11-01 12:22:45 +00:00
2006-12-25 22:34:05 +00:00
< frame src = " send.php?id=<?php p( $user->id )?> " name = " send "
2006-12-18 09:04:33 +00:00
scrolling = " no " marginwidth = " 2 " marginheight = " 2 " frameborder = " 0 " />
2005-04-14 09:28:07 +00:00
</ frameset >
</ html >
2006-12-25 22:34:05 +00:00
< ? php
die ;
}
/// user wants simple frame&js-less mode
$start = optional_param ( 'start' , time (), PARAM_INT );
$message = optional_param ( 'message' , '' , PARAM_CLEAN );
$format = optional_param ( 'format' , FORMAT_MOODLE , PARAM_INT );
$refresh = optional_param ( 'refresh' , '' , PARAM_RAW );
2006-12-27 16:06:17 +00:00
$last = optional_param ( 'last' , 0 , PARAM_INT );
$newonly = optional_param ( 'newonly' , 0 , PARAM_BOOL );
2006-12-25 22:34:05 +00:00
$addcontact = optional_param ( 'addcontact' , 0 , PARAM_INT ); // adding a contact
$removecontact = optional_param ( 'removecontact' , 0 , PARAM_INT ); // removing a contact
$blockcontact = optional_param ( 'blockcontact' , 0 , PARAM_INT ); // blocking a contact
$unblockcontact = optional_param ( 'unblockcontact' , 0 , PARAM_INT ); // unblocking a contact
if ( $addcontact and confirm_sesskey ()) {
add_to_log ( SITEID , 'message' , 'add contact' ,
'discussion.php?user1=' . $addcontact . '&user2=' . $USER -> id , $addcontact );
message_add_contact ( $addcontact );
}
if ( $removecontact and confirm_sesskey ()) {
add_to_log ( SITEID , 'message' , 'remove contact' ,
'discussion.php?user1=' . $removecontact . '&user2=' . $USER -> id , $removecontact );
message_remove_contact ( $removecontact );
}
if ( $blockcontact and confirm_sesskey ()) {
add_to_log ( SITEID , 'message' , 'block contact' ,
'discussion.php?user1=' . $blockcontact . '&user2=' . $USER -> id , $blockcontact );
message_block_contact ( $blockcontact );
}
if ( $unblockcontact and confirm_sesskey ()) {
add_to_log ( SITEID , 'message' , 'unblock contact' ,
'history.php?user1=' . $unblockcontact . '&user2=' . $USER -> id , $unblockcontact );
message_unblock_contact ( $unblockcontact );
}
/// Check that the user is not blocking us!!
2008-06-05 13:26:40 +00:00
if ( $contact = $DB -> get_record ( 'message_contacts' , array ( 'userid' => $user -> id , 'contactid' => $USER -> id ))) {
2008-05-02 04:37:02 +00:00
if ( $contact -> blocked and ! has_capability ( 'moodle/site:readallmessages' , get_context_instance ( CONTEXT_SYSTEM ))) {
2009-08-06 08:25:30 +00:00
echo $OUTPUT -> heading ( get_string ( 'userisblockingyou' , 'message' ));
2006-12-25 22:34:05 +00:00
exit ;
}
}
if ( get_user_preferences ( 'message_blocknoncontacts' , 0 , $user -> id )) { // User is blocking non-contacts
if ( empty ( $contact )) { // We are not a contact!
2009-08-06 08:25:30 +00:00
echo $OUTPUT -> heading ( get_string ( 'userisblockingyounoncontact' , 'message' ));
2006-12-25 22:34:05 +00:00
exit ;
}
}
$refreshedmessage = '' ;
if ( ! empty ( $refresh ) and data_submitted ()) {
$refreshedmessage = $message ;
} else if ( empty ( $refresh ) and data_submitted () and confirm_sesskey ()) {
if ( $message != '' ) {
message_post_message ( $USER , $user , $message , $format , 'direct' );
}
2006-12-27 16:06:17 +00:00
redirect ( 'discussion.php?id=' . $userid . '&start=' . $start . '&noframesjs=' . $noframesjs . '&newonly=' . $newonly . '&last=' . $last );
2006-12-25 22:34:05 +00:00
}
$userfullname = fullname ( $user );
$mefullname = fullname ( $USER );
2009-12-16 18:00:58 +00:00
$PAGE -> set_pagelayout ( 'popup' );
2009-06-30 08:33:29 +00:00
$PAGE -> set_title ( get_string ( 'discussion' , 'message' ) . ': ' . fullname ( $user ));
echo $OUTPUT -> header ();
2006-12-25 22:34:05 +00:00
echo '<div class="message-discussion-noframes">' ;
echo '<div id="userinfo">' ;
2009-12-27 19:47:21 +00:00
echo $OUTPUT -> user_picture ( $user , array ( 'size' => 48 , 'courseid' => SITEID ));
2006-12-27 16:06:17 +00:00
echo '<div class="name"><h1>' . $userfullname . '</h1></div>' ;
2006-12-25 22:34:05 +00:00
echo '<div class="commands"><ul>' ;
2008-06-05 13:26:40 +00:00
if ( $contact = $DB -> get_record ( 'message_contacts' , array ( 'userid' => $USER -> id , 'contactid' => $user -> id ))) {
2006-12-25 22:34:05 +00:00
if ( $contact -> blocked ) {
echo '<li>' ;
2006-12-27 16:06:17 +00:00
message_contact_link ( $user -> id , 'add' , false , 'discussion.php?id=' . $user -> id . '&noframesjs=' . $noframesjs . '&newonly=' . $newonly . '&last=' . $last , true );
2006-12-25 22:34:05 +00:00
echo '</li><li>' ;
2006-12-27 16:06:17 +00:00
message_contact_link ( $user -> id , 'unblock' , false , 'discussion.php?id=' . $user -> id . '&noframesjs=' . $noframesjs . '&newonly=' . $newonly . '&last=' . $last , true );
2006-12-25 22:34:05 +00:00
echo '</li>' ;
} else {
echo '<li>' ;
2006-12-27 16:06:17 +00:00
message_contact_link ( $user -> id , 'remove' , false , 'discussion.php?id=' . $user -> id . '&noframesjs=' . $noframesjs . '&newonly=' . $newonly . '&last=' . $last , true );
2006-12-25 22:34:05 +00:00
echo '</li><li>' ;
2006-12-27 16:06:17 +00:00
message_contact_link ( $user -> id , 'block' , false , 'discussion.php?id=' . $user -> id . '&noframesjs=' . $noframesjs . '&newonly=' . $newonly . '&last=' . $last , true );
2006-12-25 22:34:05 +00:00
echo '</li>' ;
}
} else {
echo '<li>' ;
2006-12-27 16:06:17 +00:00
message_contact_link ( $user -> id , 'add' , false , 'discussion.php?id=' . $user -> id . '&noframesjs=' . $noframesjs . '&newonly=' . $newonly . '&last=' . $last , true );
2006-12-25 22:34:05 +00:00
echo '</li><li>' ;
2006-12-27 16:06:17 +00:00
message_contact_link ( $user -> id , 'block' , false , 'discussion.php?id=' . $user -> id . '&noframesjs=' . $noframesjs . '&newonly=' . $newonly . '&last=' . $last , true );
2006-12-25 22:34:05 +00:00
echo '</li>' ;
}
echo '<li>' ;
message_history_link ( $user -> id , 0 , false , '' , '' , 'both' );
echo '</li>' ;
echo '</ul>' ;
echo '</div>' ;
echo '</div>' ; // class="userinfo"
echo '<div id="send">' ;
2007-01-03 20:35:09 +00:00
echo '<form id="editing" method="post" action="discussion.php">' ;
2006-12-25 22:34:05 +00:00
$usehtmleditor = ( can_use_html_editor () && get_user_preferences ( 'message_usehtmleditor' , 0 ));
echo '<h1><label for="edit-message">' . get_string ( 'sendmessage' , 'message' ) . '</label></h1>' ;
2006-12-27 16:06:17 +00:00
echo '<div>' ;
2006-12-25 22:34:05 +00:00
if ( $usehtmleditor ) {
print_textarea ( true , 8 , 34 , 100 , 100 , 'message' , $refreshedmessage );
echo '<input type="hidden" name="format" value="' . FORMAT_HTML . '" />' ;
} else {
2006-12-25 22:38:56 +00:00
print_textarea ( false , 8 , 50 , 0 , 0 , 'message' , $refreshedmessage );
2006-12-25 22:34:05 +00:00
echo '<input type="hidden" name="format" value="' . FORMAT_MOODLE . '" />' ;
}
2006-12-27 16:06:17 +00:00
echo '</div><div>' ;
2007-01-03 20:35:09 +00:00
echo '<input type="hidden" name="id" value="' . $user -> id . '" />' ;
echo '<input type="hidden" name="start" value="' . $start . '" />' ;
echo '<input type="hidden" name="noframesjs" value="' . $noframesjs . '" />' ;
echo '<input type="hidden" name="last" value="' . time () . '" />' ;
2009-01-02 10:36:25 +00:00
echo '<input type="hidden" name="sesskey" value="' . sesskey () . '" />' ;
2006-12-27 16:06:17 +00:00
echo '<input type="submit" value="' . get_string ( 'sendmessage' , 'message' ) . '" /> ' ;
2006-12-29 18:33:41 +00:00
echo '<input type="submit" name="refresh" value="' . get_string ( 'refresh' ) . '" />' ;
2006-12-27 16:06:17 +00:00
echo '<input type="checkbox" name="newonly" id="newonly" ' . ( $newonly ? 'checked="checked" ' : '' ) . '/><label for="newonly">' . get_string ( 'newonlymsg' , 'message' ) . '</label>' ;
echo '</div>' ;
2006-12-25 22:34:05 +00:00
echo '</form>' ;
echo '</div>' ;
echo '<div id="messages">' ;
echo '<h1>' . get_string ( 'messages' , 'message' ) . '</h1>' ;
$allmessages = array ();
$playbeep = false ;
$options = new object ();
$options -> para = false ;
$options -> newlines = true ;
2009-11-01 12:22:45 +00:00
2008-06-05 13:26:40 +00:00
$params = array ( 'uid1' => $USER -> id , 'userid1' => $userid , 'start1' => $start , 'uid2' => $USER -> id , 'userid2' => $userid , 'start2' => $start );
2006-12-27 16:06:17 +00:00
if ( $newonly ) {
2008-06-05 13:26:40 +00:00
$lastsql1 = " AND timecreated > :last1 " ;
$lastsql2 = " AND timecreated > :last2 " ;
$params [ 'last1' ] = $last ;
$params [ 'last2' ] = $last ;
2006-12-27 16:06:17 +00:00
} else {
2008-06-05 13:26:40 +00:00
$lastsql1 = " " ;
$lastsql2 = " " ;
2006-12-27 16:06:17 +00:00
}
2008-08-30 17:53:30 +00:00
//LR: change here the way to
2008-06-05 13:26:40 +00:00
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 )) {
2006-12-25 22:34:05 +00:00
foreach ( $messages as $message ) {
2008-05-08 03:49:51 +00:00
$time = userdate ( $message -> timecreated , get_string ( 'strftimedatetimeshort' ));
2009-11-01 12:22:45 +00:00
2006-12-25 22:34:05 +00:00
if ( $message -> useridfrom == $USER -> id ) {
$fullname = $mefullname ;
} else {
$fullname = $userfullname ;
}
2009-06-30 08:33:29 +00:00
if ( $message -> fullmessageformat == FORMAT_HTML ){
$printmessage = format_text ( $message -> fullmessagehtml , $message -> fullmessageformat , $options , 0 );
2008-08-30 17:53:30 +00:00
} else {
2009-06-30 08:33:29 +00:00
$printmessage = format_text ( $message -> fullmessage , $message -> fullmessageformat , $options , 0 );
2008-08-30 17:53:30 +00:00
}
2006-12-25 22:34:05 +00:00
$printmessage = '<div class="message other"><span class="author">' . $fullname . '</span> ' .
'<span class="time">[' . $time . ']</span>: ' .
'<span class="content">' . $printmessage . '</span></div>' ;
2006-12-30 10:55:32 +00:00
$i = 0 ;
$sortkey = $message -> timecreated . " $i " ; // we need string bacause we would run out of int range
while ( array_key_exists ( $sortkey , $allmessages )) {
$i ++ ;
$sortkey = $message -> timecreated . " $i " ;
}
$allmessages [ $sortkey ] = $printmessage ;
2006-12-25 22:34:05 +00:00
}
}
2008-06-05 13:26:40 +00:00
if ( $messages = $DB -> get_records_select ( 'message' , " useridto = :userid1 AND useridfrom = :uid1 $lastsql1 " , $params )) {
2006-12-25 22:34:05 +00:00
foreach ( $messages as $message ) {
2008-05-08 03:49:51 +00:00
$time = userdate ( $message -> timecreated , get_string ( 'strftimedatetimeshort' ));
2009-11-01 12:22:45 +00:00
2009-06-30 08:33:29 +00:00
if ( $message -> fullmessageformat == FORMAT_HTML ){
$printmessage = format_text ( $message -> fullmessagehtml , $message -> fullmessageformat , $options , 0 );
2008-08-30 17:53:30 +00:00
} else {
2009-06-30 08:33:29 +00:00
$printmessage = format_text ( $message -> fullmessage , $message -> fullmessageformat , $options , 0 );
2008-08-30 17:53:30 +00:00
}
2006-12-25 22:34:05 +00:00
$printmessage = '<div class="message other"><span class="author">' . $mefullname . '</span> ' .
'<span class="time">[' . $time . ']</span>: ' .
'<span class="content">' . $printmessage . '</span></div>' ;
2006-12-30 10:55:32 +00:00
$i = 0 ;
$sortkey = $message -> timecreated . " $i " ; // we need string bacause we would run out of int range
while ( array_key_exists ( $sortkey , $allmessages )) {
$i ++ ;
$sortkey = $message -> timecreated . " $i " ;
}
$allmessages [ $sortkey ] = $printmessage ;
2006-12-25 22:34:05 +00:00
}
}
2008-08-30 17:53:30 +00:00
/*Get still to be read message, use message/lib.php funtion*/
2009-11-01 12:22:45 +00:00
$messages = message_get_popup_messages ( $USER -> id , $userid );
2008-08-30 17:53:30 +00:00
if ( $messages ) {
foreach ( $messages as $message ) {
$time = userdate ( $message -> timecreated , get_string ( 'strftimedatetimeshort' ));
2009-11-01 12:22:45 +00:00
2009-06-30 08:33:29 +00:00
if ( $message -> fullmessageformat == FORMAT_HTML ){
$printmessage = format_text ( $message -> fullmessagehtml , $message -> fullmessageformat , $options , 0 );
2008-08-30 17:53:30 +00:00
} else {
2009-06-30 08:33:29 +00:00
$printmessage = format_text ( $message -> fullmessage , $message -> fullmessageformat , $options , 0 );
2008-08-30 17:53:30 +00:00
}
$printmessage = '<div class="message other"><span class="author">' . $userfullname . '</span> ' .
'<span class="time">[' . $time . ']</span>: ' .
'<span class="content">' . $printmessage . '</span></div>' ;
$i = 0 ;
$sortkey = $message -> timecreated . " $i " ; // we need string bacause we would run out of int range
while ( array_key_exists ( $sortkey , $allmessages )) {
$i ++ ;
$sortkey = $message -> timecreated . " $i " ;
}
$allmessages [ $sortkey ] = $printmessage ;
2009-11-01 12:22:45 +00:00
2008-08-30 17:53:30 +00:00
if ( $message -> timecreated < $start ) {
$start = $message -> timecreated ; // move start back so that we see all current history
}
}
$playbeep = true ;
}
/* old code , to be deleted
2008-06-05 13:26:40 +00:00
if ( $messages = $DB -> get_records_select ( 'message' , " useridto = :uid2 AND useridfrom = userid2 $lastsql2 " , $params )) {
2006-12-25 22:34:05 +00:00
foreach ( $messages as $message ) {
2008-05-08 03:49:51 +00:00
$time = userdate ( $message -> timecreated , get_string ( 'strftimedatetimeshort' ));
2006-12-25 22:34:05 +00:00
$printmessage = format_text ( $message -> message , $message -> format , $options , 0 );
$printmessage = '<div class="message other"><span class="author">' . $userfullname . '</span> ' .
'<span class="time">[' . $time . ']</span>: ' .
'<span class="content">' . $printmessage . '</span></div>' ;
2006-12-30 10:55:32 +00:00
$i = 0 ;
$sortkey = $message -> timecreated . " $i " ; // we need string bacause we would run out of int range
while ( array_key_exists ( $sortkey , $allmessages )) {
$i ++ ;
$sortkey = $message -> timecreated . " $i " ;
}
$allmessages [ $sortkey ] = $printmessage ;
2006-12-25 22:34:05 +00:00
/// Move the entry to the other table
$messageid = $message -> id ;
unset ( $message -> id );
$message -> timeread = time ();
2008-06-05 13:26:40 +00:00
if ( $DB -> insert_record ( 'message_read' , $message )) {
$DB -> delete_records ( 'message' , array ( 'id' => $messageid ));
2006-12-25 22:34:05 +00:00
}
if ( $message -> timecreated < $start ) {
$start = $message -> timecreated ; // move start back so that we see all current history
}
}
$playbeep = true ;
2008-08-30 17:53:30 +00:00
} */
2006-12-25 22:34:05 +00:00
krsort ( $allmessages );
if ( empty ( $allmessages )) {
echo get_string ( 'nomessagesfound' , 'message' );
} else {
2006-12-30 11:20:26 +00:00
echo '<ul class="messagelist">' ;
2006-12-25 22:34:05 +00:00
foreach ( $allmessages as $message ) {
2006-12-30 11:20:26 +00:00
echo '<li>' ;
2006-12-25 22:34:05 +00:00
echo $message ;
2006-12-30 11:20:26 +00:00
echo '</li>' ;
2006-12-25 22:34:05 +00:00
}
2006-12-30 11:20:26 +00:00
echo '</ul>' ;
2006-12-25 22:34:05 +00:00
if ( $playbeep and get_user_preferences ( 'message_beepnewmessage' , 0 )) {
echo '<embed src="bell.wav" autostart="true" hidden="true" name="bell" />' ;
}
}
echo '</div></div>' ;
2009-06-30 08:33:29 +00:00
echo $OUTPUT -> footer ();
2007-08-27 03:22:16 +00:00
?>