MDL-27219 chat: Beep will not display user fullname if user is not in chat
authorRajesh Taneja <rajesh@moodle.org>
Tue, 16 Aug 2011 08:14:58 +0000 (16:14 +0800)
committerRajesh Taneja <rajesh@moodle.org>
Tue, 16 Aug 2011 08:34:16 +0000 (16:34 +0800)
mod/chat/chat_ajax.php
mod/chat/lib.php

index 6d66523..d9e6b37 100644 (file)
@@ -135,7 +135,7 @@ case 'update':
                 $send_user_list = true;
                 $users = chat_format_userlist(chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid), $course);
             }
-            if ($html = chat_format_message_theme($message, $chatuser->course, $USER, $theme)) {
+            if ($html = chat_format_message_theme($message, $chatuser, $USER, $cm->groupingid, $theme)) {
                 $message->mymessage = ($USER->id == $message->userid);
                 $message->message  = $html->html;
                 if (!empty($html->type)) {
index a59a5e2..a462b6e 100644 (file)
@@ -898,13 +898,14 @@ function chat_format_message($message, $courseid, $currentuser, $chat_lastrow=NU
 
 /**
  * @global object
- * @param object $message
- * @param int $courseid
- * @param object $currentuser
- * @param string $chat_lastrow
+ * @param object $message message to be displayed.
+ * @param mixed $chatuser user chat data
+ * @param object $currentuser current user for whom the message should be displayed.
+ * @param int $groupingid course module grouping id
+ * @param string $theme name of the chat theme.
  * @return bool|string Returns HTML or false
  */
-function chat_format_message_theme ($message, $courseid, $currentuser, $theme = 'bubble') {
+function chat_format_message_theme ($message, $chatuser, $currentuser, $groupingid, $theme = 'bubble') {
     global $CFG, $USER, $OUTPUT, $COURSE, $DB;
 
     static $users;     // Cache user lookups
@@ -927,8 +928,10 @@ function chat_format_message_theme ($message, $courseid, $currentuser, $theme =
     $tz = get_user_timezone($currentuser->timezone);
     $USER->timezone = $tz;
 
-    if (empty($courseid)) {
+    if (empty($chatuser->course)) {
         $courseid = $COURSE->id;
+    } else {
+        $courseid = $chatuser->course;
     }
 
     $message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz);
@@ -968,6 +971,9 @@ function chat_format_message_theme ($message, $courseid, $currentuser, $theme =
     $special = false;
     $outtime = $message->strtime;
 
+    //Initilise output variable.
+    $outmain = '';
+
     if (substr($text, 0, 5) == 'beep ') {
         $special = true;
         /// It's a beep!
@@ -979,9 +985,17 @@ function chat_format_message_theme ($message, $courseid, $currentuser, $theme =
         } else if ($beepwho == $currentuser->id) {  // current user
             $outmain = get_string('messagebeepsyou', 'chat', fullname($sender));
         } else if ($sender->id == $currentuser->id) {  //something is not caught?
-
-            $receiver = $DB->get_record('user', array('id'=>$beepwho), 'id,picture,firstname,lastname');
-            $outmain = get_string('messageyoubeep', 'chat', fullname($receiver));
+            //allow beep for a active chat user only, else user can beep anyone and get fullname
+            if (!empty($chatuser) && is_numeric($beepwho)) {
+               $chatusers = chat_get_users($chatuser->chatid, $chatuser->groupid, $groupingid);
+               if (array_key_exists($beepwho, $chatusers)) {
+                   $outmain = get_string('messageyoubeep', 'chat', fullname($chatusers[$beepwho]));
+               } else {
+                   $outmain = get_string('messageyoubeep', 'chat', $beepwho);
+               }
+            } else {
+                $outmain = get_string('messageyoubeep', 'chat', $beepwho);
+            }
         }
     } else if (substr($text, 0, 1) == '/') {     /// It's a user command
         $special = true;
@@ -1043,7 +1057,12 @@ function chat_format_message_theme ($message, $courseid, $currentuser, $theme =
         $result->html = str_replace($patterns, $replacements, $chattheme_cfg->user_message);
     }
 
-    return $result;
+    //When user beeps other user, then don't show any timestamp to other users in chat.
+    if (('' === $outmain) && $special) {
+        return false;
+    } else {
+        return $result;
+    }
 }