--- lib.php 2008-01-14 16:59:47.000000000 +0000
+++ /var/www/moodle/message/lib.php 2008-01-14 22:30:09.000000000 +0000
@@ -23,59 +23,75 @@
if (isset($CFG->block_online_users_timetosee)) {
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
}
- $timefrom = time()-$timetoshowusers;
+ // time which a user is counting as being active since
+ $timefrom = time()-$timetoshowusers;
- /// get lists of contacts and unread messages
- $onlinecontacts = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, u.imagealt, mc.blocked
- FROM {$CFG->prefix}user u, {$CFG->prefix}message_contacts mc
- WHERE mc.userid='$USER->id' AND u.id=mc.contactid AND u.lastaccess>=$timefrom
- AND mc.blocked='0'
- ORDER BY u.firstname ASC");
-
- $offlinecontacts = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, u.imagealt, mc.blocked
- FROM {$CFG->prefix}user u, {$CFG->prefix}message_contacts mc
- WHERE mc.userid='$USER->id' AND u.id=mc.contactid AND u.lastaccess<$timefrom
- AND mc.blocked='0'
- ORDER BY u.firstname ASC");
-
- $unreadmessages = get_records_sql("SELECT m.id, m.useridfrom, u.firstname, u.lastname, u.picture, u.imagealt
- FROM {$CFG->prefix}user u, {$CFG->prefix}message m
- WHERE m.useridto='$USER->id' AND u.id=m.useridfrom");
-
- $blockedcontacts = get_records_select('message_contacts', "userid='$USER->id' AND blocked='1'", '', 'contactid, id');
-
-
- $countonlinecontacts = (is_array($onlinecontacts)) ? count($onlinecontacts) : 0;
- $countofflinecontacts = (is_array($offlinecontacts)) ? count($offlinecontacts) : 0;
-
-/// Cycle through messages and extract those that are from unknown contacts
-/// We can take advantage of the keys for $onlinecontacts and $offlinecontacts
-/// which are set to the userid and therefore we just need to see if the key
-/// exists in either of those arrays
-/// We can also discard any messages from users in our blocked contact list
- $unknownmessages = array();
- if (!empty($unreadmessages)) {
- /// make sure we have valid arrays to test against - they may be boolean false
- if (empty($onlinecontacts)) $onlinecontacts = array();
- if (empty($offlinecontacts)) $offlinecontacts = array();
- if (empty($blockedcontacts)) $blockedcontacts = array();
- foreach ($unreadmessages as $unreadmessage) {
- if (array_key_exists($unreadmessage->useridfrom, $onlinecontacts) or
- array_key_exists($unreadmessage->useridfrom, $offlinecontacts) or
- array_key_exists($unreadmessage->useridfrom, $blockedcontacts) ) {
- continue;
- }
- if (!isset($unknownmessages[$unreadmessage->useridfrom])) {
- $message = $unreadmessage;
- $message->count = 1;
- $unknownmessages[$unreadmessage->useridfrom] = $message;
- } else {
- $unknownmessages[$unreadmessage->useridfrom]->count++;
+ // people in our contactlist who are online
+ $onlinecontacts = array();
+ // people in our contactlist who are offline
+ $offlinecontacts = array();
+ // people who are not in our contactlist but have sent us a message
+ $strangers = array();
+
+
+ // get all in our contactlist who are not blocked in our contact list
+ // and count messages we have waiting from each of them
+ $contactsql = "SELECT u.id, u.firstname, u.lastname, u.picture,
+ u.imagealt, u.lastaccess, count(m.id) as messagecount
+ FROM {$CFG->prefix}message_contacts mc
+ JOIN {$CFG->prefix}user u
+ ON u.id = mc.contactid
+ LEFT OUTER JOIN {$CFG->prefix}message m
+ ON m.useridfrom = mc.contactid
+ WHERE mc.userid = {$USER->id}
+ AND mc.blocked = 0
+ GROUP BY u.id, u.firstname, u.lastname, u.picture,
+ u.imagealt, u.lastaccess
+ ORDER BY u.firstname ASC;";
+
+ if($rs = get_recordset_sql($contactsql)){
+ while($rd = rs_fetch_next_record($rs)){
+
+ if($rd->lastaccess >= $timefrom){
+ // they have been active recently, so are counted online
+ $onlinecontacts[] = $rd;
+ }else{
+ $offlinecontacts[] = $rd;
}
}
+ unset($rd);
+ rs_close($rs);
}
+
+ // get messages from anyone who isn't in our contact list and count the number
+ // of messages we have from each of them
+ $strangersql = "SELECT u.id, u.firstname, u.lastname, u.picture,
+ u.imagealt, u.lastaccess, count(m.id) as messagecount
+ FROM {$CFG->prefix}message m
+ JOIN {$CFG->prefix}user u
+ ON u.id = m.useridfrom
+ LEFT OUTER JOIN {$CFG->prefix}message_contacts mc
+ ON mc.contactid = m.useridfrom AND
+ mc.userid = m.useridto
+ WHERE mc.id IS NULL AND m.useridto = {$USER->id}
+ GROUP BY u.id, u.firstname, u.lastname, u.picture,
+ u.imagealt, u.lastaccess
+ ORDER BY u.firstname ASC;";
+
+ if($rs = get_recordset_sql($strangersql)){
+ while($rd= rs_fetch_next_record($rs)){
+ $strangers[] = $rd;
+ }
+ unset($rd);
+ rs_close($rs);
+ }
+
+ $countonlinecontacts = count($onlinecontacts);
+ $countofflinecontacts = count($offlinecontacts);
+ $countstrangers = count($strangers);
+
if ($countonlinecontacts + $countofflinecontacts == 0) {
echo '
';
print_string('contactlistempty', 'message');
@@ -85,128 +101,51 @@
echo '
';
}
- if(!empty($onlinecontacts) || !empty($offlinecontacts) || !empty($unknownmessages)) {
-
- echo '';
+ if ($countstrangers && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help
+ echo '(';
+ print_string('addsomecontactsincoming', 'message');
+ echo ')
';
}
echo '
';
@@ -226,8 +165,6 @@
}
-
-
/// $messagearray is an array of objects
/// $field is a valid property of object
/// $value is the value $field should equal to be counted
@@ -1072,4 +1009,44 @@
UNION SELECT contactid as id,1 from {$CFG->prefix}message_contacts");
}
+/**
+ * Print a row of contactlist displaying user picture, messages waiting and
+ * block links etc
+ * @param $contact contact object containing all fields required for print_user_picture()
+ * @param $incontactlist is the user a contact of ours?
+ */
+function message_print_contactlist_user($contact, $incontactlist = true){
+ $fullname = fullname($contact);
+ $fullnamelink = $fullname;
+
+ /// are there any unread messages for this contact?
+ if ($contact->messagecount > 0 ){
+ $fullnamelink = ''.$fullnamelink.' ('.$contact->messagecount.')';
+ }
+
+
+ if($incontactlist){
+ $strcontact = message_contact_link($contact->id, 'remove', true);
+ $strblock = '';
+ }else{
+ $strcontact = message_contact_link($contact->id, 'add', true);
+ $strblock = ' '. message_contact_link($contact->id, 'block', true);
+ }
+
+ $strhistory = message_history_link($contact->id, 0, true, '', '', 'icon');
+
+ echo '| ';
+ print_user_picture($contact, SITEID, $contact->picture, 20, false, true, 'userwindow');
+ echo ' | ';
+ echo '';
+
+ link_to_popup_window("/message/discussion.php?id=$contact->id", "message_$contact->id",
+ $fullnamelink, 500, 500, get_string('sendmessageto', 'message', $fullname),
+ 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500');
+
+ echo ' | ';
+ echo ' '.$strcontact.$strblock.' '.$strhistory.' | ';
+ echo '
';
+}
+
?>