From: poltawski Date: Tue, 15 Jan 2008 13:48:34 +0000 (+0000) Subject: MDL-12990 - clean up message_print_contacts() a lot to: X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f46b6587a6096e7feb300f5d9a5d584f81c5ee95;p=moodle.git MDL-12990 - clean up message_print_contacts() a lot to: * Use less DB queries and avoid getting lots of needless data * Dont do a big php array rearranging loop - database does the work for us instead * split all the duplicated display code into a single function merged from MOODLE_19_STABLE --- diff --git a/message/lib.php b/message/lib.php index 214fe1d721..ac11b18632 100644 --- a/message/lib.php +++ b/message/lib.php @@ -23,59 +23,75 @@ function message_print_contacts() { 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 @@ function message_print_contacts() { echo '
'; } - if(!empty($onlinecontacts) || !empty($offlinecontacts) || !empty($unknownmessages)) { - - echo ''; + echo '
'; - if(!empty($onlinecontacts)) { - /// print out list of online contacts + if($countonlinecontacts) { + /// print out list of online contacts - echo ''; + echo ''; - if (!empty($onlinecontacts)) { - foreach ($onlinecontacts as $contact) { - if ($contact->blocked == 1) continue; - $fullname = fullname($contact); - $fullnamelink = $fullname; - /// are there any unread messages for this contact? - if (($unread = message_count_messages($unreadmessages, 'useridfrom', $contact->id)) > 0) { - $fullnamelink = ''.$fullnamelink.' ('.$unread.')'; - } - /// link to remove from contact list - $strcontact = message_contact_link($contact->id, 'remove', true); - $strhistory = message_history_link($contact->id, 0, true, '', '', 'icon'); - - echo ''; - echo ''; - echo ''; - echo ''; - } - } - echo ''; + foreach ($onlinecontacts as $contact) { + message_print_contactlist_user($contact); } + } + echo ''; - if (!empty($offlinecontacts)) { - /// print out list of offline contacts - - echo ''; - - foreach ($offlinecontacts as $contact) { - if ($contact->blocked == 1) continue; - $fullname = fullname($contact); - $fullnamelink = $fullname; - /// are there any unread messages for this contact? - if (($unread = message_count_messages($unreadmessages, 'useridfrom', $contact->id)) > 0) { - $fullnamelink = ''.$fullnamelink.' ('.$unread.')'; - } - /// link to remove from contact list - $strcontact = message_contact_link($contact->id, 'remove', true); - $strhistory = message_history_link($contact->id, 0, true, '', '', 'icon'); - - echo ''; - echo ''; - echo ''; - echo ''; - } - echo ''; - } - - - /// print out list of incoming contacts - if (!empty($unknownmessages)) { - echo ''; - - foreach ($unknownmessages as $messageuser) { - - // set id to be userid so functions expecting a user id have it - $messageuser->id = $messageuser->useridfrom; - - $fullname = fullname($messageuser); - $fullnamelink = $fullname; - if ($messageuser->count) { - $fullnamelink = ''.$fullnamelink.' ('.$messageuser->count.')'; - } - /// link to add to contact list - - $strcontact = message_contact_link($messageuser->useridfrom, 'add', true); - $strblock = message_contact_link($messageuser->useridfrom, 'block', true); - $strhistory = message_history_link($messageuser->useridfrom, 0, true, '', '', 'icon'); + if ($countofflinecontacts) { + /// print out list of offline contacts - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - } + foreach ($offlinecontacts as $contact) { + message_print_contactlist_user($contact); } + echo ''; + } - echo '
'; - echo get_string('onlinecontacts', 'message', $countonlinecontacts); - echo '
'; + echo get_string('onlinecontacts', 'message', $countonlinecontacts); + echo '
'; - print_user_picture($contact, SITEID, $contact->picture, 20, false, true, 'userwindow'); - 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 get_string('offlinecontacts', 'message', $countofflinecontacts); - echo '
'; - print_user_picture($contact, SITEID, $contact->picture, 20, false, true, 'userwindow'); - 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 get_string('incomingcontacts', 'message', count($unknownmessages)); - echo '
'; - print_user_picture($messageuser, SITEID, $messageuser->picture, 20, false, true, 'userwindow'); - echo ''; - - link_to_popup_window("/message/discussion.php?id=$messageuser->useridfrom", "message_$messageuser->useridfrom", - $fullnamelink, 500, 500, get_string('sendmessageto', 'message', $fullname), - 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); + echo '
'; + echo get_string('offlinecontacts', 'message', $countofflinecontacts); + echo '
 
'; + /// print out list of incoming contacts + if ($countstrangers) { + echo ''; + echo get_string('incomingcontacts', 'message', $countstrangers); + echo ''; - if (!empty($unknownmessages) && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help - echo '
('; - print_string('addsomecontactsincoming', 'message'); - echo ')
'; + foreach ($strangers as $stranger) { + message_print_contactlist_user($stranger, false); } + } + echo ''; + if ($countstrangers && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help + echo '
('; + print_string('addsomecontactsincoming', 'message'); + echo ')
'; } echo '
'; @@ -226,8 +165,6 @@ document.write("'.$autorefresh.'") } - - /// $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 @@ function message_get_participants() { 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 ''; +} + ?>