//TODO: we need to solve problems with database transactions here somehow, for now we just prevent transactions - sorry
$DB->transactions_forbidden();
+ //after how long inactive should the user be considered logged off?
if (isset($CFG->block_online_users_timetosee)) {
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
} else {
- $timetoshowusers = 300;
+ $timetoshowusers = 300;//5 minutes
}
-/// Work out if the user is logged in or not
- if ((time() - $eventdata->userto->lastaccess) > $timetoshowusers) {
- $userstate = 'loggedoff';
- } else {
+ // Work out if the user is logged in or not
+ if ((time() - $timetoshowusers) < $eventdata->userto->lastaccess) {
$userstate = 'loggedin';
+ } else {
+ $userstate = 'loggedoff';
}
-/// Create the message object
+ // Create the message object
$savemessage = new object();
$savemessage->useridfrom = $eventdata->userfrom->id;
$savemessage->useridto = $eventdata->userto->id;
$savemessage->smallmessage = $eventdata->smallmessage;
$savemessage->timecreated = time();
-/// Find out what processors are defined currently
-/// When a user doesn't have settings none gets return, if he doesn't want contact "" gets returned
- $processor = get_user_preferences('message_provider_'.$eventdata->component.'_'.$eventdata->name.'_'.$userstate, NULL, $eventdata->userto->id);
+ // Find out what processors are defined currently
+ // When a user doesn't have settings none gets return, if he doesn't want contact "" gets returned
+ $preferencename = 'message_provider_'.$eventdata->component.'_'.$eventdata->name.'_'.$userstate;
+ $processor = get_user_preferences($preferencename, NULL, $eventdata->userto->id);
if ($processor == NULL) { //this user never had a preference, save default
if (!message_set_default_message_preferences($eventdata->userto)) {
return false;
}
}
+
+ $savemessage->timeread = time();
+ $messageid = $savemessage->id;
+ unset($savemessage->id);
+
+ //if there is no more processors that want to process this we can move message to message_read
+ if ( $DB->count_records('message_working', array('unreadmessageid' => $messageid)) == 0){
+ if ($DB->insert_record('message_read', $savemessage)) {
+ $DB->delete_records('message', array('id' => $messageid));
+ }
+ }
}
return true;
// Make sure the USER has a sesskey set up. Used for CSRF protection.
sesskey();
- // Do not bother admins with any formalities, no last access updates either
+ // Do not bother admins with any formalities
if (is_siteadmin()) {
+ //set accesstime or the user will appear offline which messes up messaging
+ user_accesstime_log($course->id);
return;
}
global $CFG, $SITE, $USER, $DB;
$eventdata = new object();
- $eventdata->component = 'message';
+ $eventdata->component = 'moodle';
$eventdata->name = 'instantmessage';
$eventdata->userfrom = $userfrom;
$eventdata->userto = $userto;
return $messages;
}
-//marks all messages being sent from $fromuserid to $destuserid as read
-function message_mark_messages_read($destuserid, $fromuserid){
+//marks ALL messages being sent from $fromuserid to $touserid as read
+function message_mark_messages_read($touserid, $fromuserid){
global $DB;
$sql = 'SELECT m.*, mw.id AS mwid FROM {message} m JOIN {message_working} mw ON m.id=mw.unreadmessageid WHERE m.useridto=:useridto AND m.useridfrom=:useridfrom';
- $messages = $DB->get_recordset_sql($sql, array('useridto'=>$destuserid,'useridfrom'=>$fromuserid));
+ $messages = $DB->get_recordset_sql($sql, array('useridto'=>$touserid,'useridfrom'=>$fromuserid));
+
+ //todo surely we can do this with one query rather than with a loop
foreach ($messages as $message) {
$message->timeread = time();
$messageid = $message->id;
unset($message->id);//unset because it will get a new id on insert into message_read
- //delete what we've processed and check if can move message
+ //indicate the message is read
$DB->delete_records('message_working', array('id' => $message->mwid));
+
+ //have all message processors completed dealing with this message?
if ( $DB->count_records('message_working', array('unreadmessageid'=>$messageid)) == 0){
if ($DB->insert_record('message_read', $message)) {
$DB->delete_records('message', array('id' => $messageid));
$usertoemail = $userto->email;
}
$userfrom = $DB->get_record('user', array('id' => $message->useridfrom));
- if ( email_to_user($usertoemail, $userfrom->email,
+ $result = email_to_user($usertoemail, $userfrom->email,
$message->subject, $message->fullmessage,
- $message->fullmessagehtml)
- ){
- /// Move the entry to the other table
- $message->timeread = time();
- $messageid = $message->id;
- unset($message->id);
+ $message->fullmessagehtml);
- //if there is no more processor that want to process this can move message
- if ( $DB->count_records('message_working', array('unreadmessageid' => $messageid)) == 0){
- if ($DB->insert_record('message_read', $message)) {
- $DB->delete_records('message', array('id' => $messageid));
- }
- }
- }else{
- //delete what we've processed and check if can move message
- $messageid = $message->id;
- unset($message->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 true;
+ //return $result===true; //email_to_user() can return true, false or "emailstop"
+ return true;//do we want to report an error if email sending fails?
}
/**