Skip to content

Commit

Permalink
MDL-16268 User-User send messages now use new messaging system.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfrodrigues committed Aug 30, 2008
1 parent 70d91d9 commit 1c50df9
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 160 deletions.
2 changes: 2 additions & 0 deletions admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@

// Install core message providers
message_update_providers();
message_update_providers('message');

/// This is used to handle any settings that must exist in $CFG but which do not exist in
/// admin_get_root()/$ADMIN as admin_setting objects (there are some exceptions).
Expand Down Expand Up @@ -332,6 +333,7 @@

// Update core message providers
message_update_providers();
message_update_providers('message');

if (set_config("version", $version)) {
remove_dir($CFG->dataroot . '/cache', true); // flush cache
Expand Down
2 changes: 1 addition & 1 deletion lang/en_utf8/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@
$string['providerstag'] = 'Source: ';
$string['processortag'] = 'Destination: ';
$string['cannotsavemessageprefs'] = 'Could not save user messaging preferences';

$string['messageprovider:instantmessage'] = 'User to User Instant Message';

?>
7 changes: 3 additions & 4 deletions lib/messagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function message_send_handler($eventdata){
} else {
$userstate = 'loggedin';
}

/// Create the message object
$savemessage = new object();
$savemessage->useridfrom = $eventdata->userfrom->id;
Expand Down Expand Up @@ -95,9 +95,8 @@ function message_send_handler($eventdata){

} else { // Process the message
/// Store unread message just in case we can not send it
$savemessage->id = $DB->insert_record('message', $savemessage);


$savemessage->id = $DB->insert_record('message', $savemessage);

/// Try to deliver the message to each processor
$processorlist = explode(',', $processor);
foreach ($processorlist as $procname) {
Expand Down
49 changes: 43 additions & 6 deletions message/discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
$options = new object();
$options->para = false;
$options->newlines = true;

$params = array('uid1'=>$USER->id ,'userid1'=>$userid, 'start1'=>$start, 'uid2'=>$USER->id ,'userid2'=>$userid, 'start2'=>$start);
if ($newonly) {
$lastsql1 = " AND timecreated > :last1";
Expand All @@ -209,6 +209,7 @@
$lastsql2 = "";
}

//LR: change here the way to
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)) {
foreach ($messages as $message) {
$time = userdate($message->timecreated, get_string('strftimedatetimeshort'));
Expand All @@ -219,7 +220,11 @@
$fullname = $userfullname;
}

$printmessage = format_text($message->message, $message->format, $options, 0);
if ($message->format == FORMAT_HTML){
$printmessage = format_text($message->fullmessagehtml, $message->format, $options, 0);
} else{
$printmessage = format_text($message->fullmessage, $message->format, $options, 0);
}
$printmessage = '<div class="message other"><span class="author">'.$fullname.'</span> '.
'<span class="time">['.$time.']</span>: '.
'<span class="content">'.$printmessage.'</span></div>';
Expand All @@ -236,8 +241,12 @@
if ($messages = $DB->get_records_select('message', "useridto = :userid1 AND useridfrom = :uid1 $lastsql1", $params)) {
foreach ($messages as $message) {
$time = userdate($message->timecreated, get_string('strftimedatetimeshort'));

$printmessage = format_text($message->message, $message->format, $options, 0);

if ($message->format == FORMAT_HTML){
$printmessage = format_text($message->fullmessagehtml, $message->format, $options, 0);
} else{
$printmessage = format_text($message->fullmessage, $message->format, $options, 0);
}
$printmessage = '<div class="message other"><span class="author">'.$mefullname.'</span> '.
'<span class="time">['.$time.']</span>: '.
'<span class="content">'.$printmessage.'</span></div>';
Expand All @@ -250,7 +259,35 @@
$allmessages[$sortkey] = $printmessage;
}
}

/*Get still to be read message, use message/lib.php funtion*/
$messages = message_get_popup_messages($USER->id, $userid);
if ($messages) {
foreach ($messages as $message) {
$time = userdate($message->timecreated, get_string('strftimedatetimeshort'));

if ($message->format == FORMAT_HTML){
$printmessage = format_text($message->fullmessagehtml, $message->format, $options, 0);
} else{
$printmessage = format_text($message->fullmessage, $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>';
$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;

if ($message->timecreated < $start) {
$start = $message->timecreated; // move start back so that we see all current history
}
}
$playbeep = true;
}
/* old code, to be deleted
if ($messages = $DB->get_records_select('message', "useridto = :uid2 AND useridfrom = userid2 $lastsql2", $params)) {
foreach ($messages as $message) {
$time = userdate($message->timecreated, get_string('strftimedatetimeshort'));
Expand Down Expand Up @@ -280,7 +317,7 @@
}
}
$playbeep = true;
}
}*/

krsort($allmessages);

Expand Down
2 changes: 1 addition & 1 deletion message/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
$preferences = new object();

//get the message general preferences
$preferences->showmessagewindow = get_user_preferences( 'message_showmessagewindow', '', $user->id);
$preferences->showmessagewindow = get_user_preferences( 'message_showmessagewindow', 1, $user->id);
$preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
$preferences->noframesjs = get_user_preferences( 'message_noframesjs', '', $user->id);
Expand Down
114 changes: 37 additions & 77 deletions message/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -946,92 +946,19 @@ function message_format_message(&$message, &$user, $format='', $keywords='', $cl
*/
function message_post_message($userfrom, $userto, $message, $format, $messagetype) {
global $CFG, $SITE, $USER, $DB;

/// Set up current language to suit the receiver of the message
$savelang = $USER->lang;

if (!empty($userto->lang)) {
$USER->lang = $userto->lang;
}

$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->component = 'message';
$eventdata->name = 'instantmessage';
$eventdata->userfrom = $userfrom;
$eventdata->userto = $userto;
$eventdata->subject = "MESSAGE";
$eventdata->subject = "IM";
$eventdata->fullmessage = $message;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);


/// Save the new message in the database

$savemessage = NULL;
$savemessage->useridfrom = $userfrom->id;
$savemessage->useridto = $userto->id;
$savemessage->message = $message;
$savemessage->format = $format;
$savemessage->timecreated = time();
$savemessage->messagetype = 'direct';

if ($CFG->messaging) {
//if (!$savemessage->id = $DB->insert_record('message', $savemessage)) {
// return false;
//}
$emailforced = false;
} else { // $CFG->messaging is not on, we need to force sending of emails
$emailforced = true;
$savemessage->id = true;
}

/// Check to see if anything else needs to be done with it

$preference = (object)get_user_preferences(NULL, NULL, $userto->id);

if ($emailforced || (!isset($preference->message_emailmessages) || $preference->message_emailmessages)) { // Receiver wants mail forwarding
if (!isset($preference->message_emailtimenosee)) {
$preference->message_emailtimenosee = 10;
}
if (!isset($preference->message_emailformat)) {
$preference->message_emailformat = FORMAT_HTML;
}
if ($emailforced || (time() - $userto->lastaccess) > ((int)$preference->message_emailtimenosee * 60)) { // Long enough

$tagline = get_string('emailtagline', 'message', $SITE->shortname);

$messagesubject = preg_replace('/\s+/', ' ', strip_tags($message)); // make sure it's all on one line
$messagesubject = message_shorten_message($messagesubject, 30).'...';

$messagetext = format_text_email($message, $format).
"\n\n--\n".$tagline."\n"."$CFG->wwwroot/message/index.php?popup=1";

if (isset($preference->message_emailformat) and $preference->message_emailformat == FORMAT_HTML) {
$messagehtml = format_text($message, $format);
// MDL-10294, do not print link if messaging is disabled
if ($CFG->messaging) {
$messagehtml .= '<hr /><p><a href="'.$CFG->wwwroot.'/message/index.php?popup=1">'.$tagline.'</a></p>';
}
} else {
$messagehtml = NULL;
}

if (!empty($preference->message_emailaddress)) {
$userto->email = $preference->message_emailaddress; // Use custom messaging address
}

if (email_to_user($userto, $userfrom, $messagesubject, $messagetext, $messagehtml)) {
$CFG->messagewasjustemailed = true;
}

sleep(3);
}
}

$USER->lang = $savelang; // restore original language
return events_trigger('message_send', $eventdata);

return $savemessage->id;
}


Expand Down Expand Up @@ -1091,4 +1018,37 @@ function message_print_contactlist_user($contact, $incontactlist = true){
echo '</tr>';
}

function message_get_popup_messages($destuserid, $fromuserid=NULL){
global $DB;

$processor = $DB->get_record('message_processors', array('name' => 'popup'));

$messagesproc = $DB->get_records('message_working', array('processorid'=>$processor->id));

//for every message to process check if it's for current user and process
$messages = array();
foreach ($messagesproc as $msgp){
$query = array('id'=>$msgp->unreadmessageid, 'useridto'=>$destuserid);
if ($fromuserid){
$query['useridfrom'] = $fromuserid;
}
if ($message = $DB->get_record('message', $query)){
$messages[] = $message;
/// Move the entry to the other table
$message->timeread = time();
$messageid = $message->id;
unset($message->id);

//delete what we've processed and check if can move message
$DB->delete_records('message_working', array('id' => $msgp->id));
if ( $DB->count_records('message_working', array('unreadmessageid'=>$messageid)) == 0){
if ($DB->insert_record('message_read', $message)) {
$DB->delete_records('message', array('id' => $messageid));
}
}
}
}
return $messages;
}

?>
70 changes: 0 additions & 70 deletions message/output/popup/popup_interface.php

This file was deleted.

Loading

0 comments on commit 1c50df9

Please sign in to comment.