public function test_message_sent() { global $DB; $this->resetAfterTest(); $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); $cm = $DB->get_record('course_modules', array('id' => $chat->cmid)); // Logging in first user to the chat. $this->setUser($user1->id); $sid1 = chat_login_user($chat->id, 'ajax', 0, $course); // Logging in second user to the chat. $this->setUser($user2->id); $sid2 = chat_login_user($chat->id, 'ajax', 0, $course); // Getting the chatuser record. $chatuser1 = $DB->get_record('chat_users', array('sid' => $sid1)); $chatuser2 = $DB->get_record('chat_users', array('sid' => $sid2)); $sink = $this->redirectEvents(); // Send a messaging from the first user. We pass the CM to chat_send_chatmessage() this time. // This ensures that the event triggered when sending a message is filled with the correct information. $this->setUser($user1->id); $messageid = chat_send_chatmessage($chatuser1, 'Hello!', false, $cm); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); $this->assertInstanceOf('\\mod_chat\\event\\message_sent', $event); $this->assertEquals($messageid, $event->objectid); $this->assertEquals($user1->id, $event->relateduserid); $this->assertEquals($user1->id, $event->userid); $expected = array($course->id, 'chat', 'talk', "view.php?id={$cm->id}", $chat->id, $cm->id, $user1->id); $this->assertEventLegacyLogData($expected, $event); // Send a messaging from the first user. We DO NOT pass the CM to chat_send_chatmessage() this time. // This ensures that the event triggered when sending a message is filled with the correct information. $sink->clear(); $this->setUser($user2->id); $messageid = chat_send_chatmessage($chatuser2, 'Hello!'); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); $this->assertInstanceOf('\\mod_chat\\event\\message_sent', $event); $this->assertEquals($messageid, $event->objectid); $this->assertEquals($user2->id, $event->relateduserid); $this->assertEquals($user2->id, $event->userid); $expected = array($course->id, 'chat', 'talk', "view.php?id={$cm->id}", $chat->id, $cm->id, $user2->id); $this->assertEventLegacyLogData($expected, $event); // Sending a message from the system should not trigger any event. $sink->clear(); $this->setAdminUser(); chat_send_chatmessage($chatuser1, 'enter', true); $this->assertEquals(0, $sink->count()); $sink->close(); }
/** * Delete the old and in the way * * @global object * @global object */ function chat_delete_old_users() { // Delete the old and in the way. global $CFG, $DB; $timeold = time() - $CFG->chat_old_ping; $timeoldext = time() - $CFG->chat_old_ping * 10; // JSless gui_basic needs much longer timeouts. $query = "(version<>'basic' AND lastping<?) OR (version='basic' AND lastping<?)"; $params = array($timeold, $timeoldext); if ($oldusers = $DB->get_records_select('chat_users', $query, $params)) { $DB->delete_records_select('chat_users', $query, $params); foreach ($oldusers as $olduser) { chat_send_chatmessage($olduser, 'exit', true); } } }
function disconnect_session($sessionid) { global $DB; $info = $this->sets_info[$sessionid]; $DB->delete_records('chat_users', array('sid' => $sessionid)); $msg = new stdClass(); $msg->chatid = $info['chatid']; $msg->userid = $info['userid']; $msg->groupid = $info['groupid']; $msg->system = 1; $msg->message = 'exit'; $msg->timestamp = time(); $this->trace('User has disconnected, destroying uid ' . $info['userid'] . ' with SID ' . $sessionid, E_USER_WARNING); chat_send_chatmessage($info['chatuser'], $msg->message, true); // *************************** IMPORTANT // // Kill him BEFORE broadcasting, otherwise we 'll get infinite recursion! // // ********************************************************************** $latesender = $info['user']; $this->dismiss_set($sessionid); $this->message_broadcast($msg, $latesender); }
/** * Send a message on the given chat session. * * @param int $chatsid the chat session id * @param string $messagetext the message text * @param string $beepid the beep message id * @return array of warnings and the new message id (0 if the message was empty) * @since Moodle 3.0 * @throws moodle_exception */ public static function send_chat_message($chatsid, $messagetext, $beepid = '') { global $DB; $params = self::validate_parameters(self::send_chat_message_parameters(), array('chatsid' => $chatsid, 'messagetext' => $messagetext, 'beepid' => $beepid)); $warnings = array(); // Request and permission validation. if (!($chatuser = $DB->get_record('chat_users', array('sid' => $params['chatsid'])))) { throw new moodle_exception('notlogged', 'chat'); } $chat = $DB->get_record('chat', array('id' => $chatuser->chatid), '*', MUST_EXIST); list($course, $cm) = get_course_and_cm_from_instance($chat, 'chat'); $context = context_module::instance($cm->id); self::validate_context($context); require_capability('mod/chat:chat', $context); $chatmessage = clean_text($params['messagetext'], FORMAT_MOODLE); if (!empty($params['beepid'])) { $chatmessage = 'beep ' . $params['beepid']; } if (!empty($chatmessage)) { // Send the message. $messageid = chat_send_chatmessage($chatuser, $chatmessage, 0, $cm); // Update ping time. $chatuser->lastmessageping = time() - 2; $DB->update_record('chat_users', $chatuser); } else { $messageid = 0; } $result = array(); $result['messageid'] = $messageid; $result['warnings'] = $warnings; return $result; }
$SESSION->chatprefs = array(); } if (!isset($SESSION->chatprefs[$chat->id])) { $SESSION->chatprefs[$chat->id] = array(); $SESSION->chatprefs[$chat->id]['chatentered'] = time(); } $chatentered = $SESSION->chatprefs[$chat->id]['chatentered']; $refreshedmessage = ''; if (!empty($refresh) and data_submitted()) { $refreshedmessage = $message; chat_delete_old_users(); } else { if (empty($refresh) and data_submitted() and confirm_sesskey()) { if ($message != '') { $chatuser = $DB->get_record('chat_users', array('sid' => $chat_sid)); chat_send_chatmessage($chatuser, $message, 0, $cm); $DB->set_field('chat_users', 'lastmessageping', time(), array('sid' => $chat_sid)); } chat_delete_old_users(); $url = new moodle_url('/mod/chat/gui_basic/index.php', array('id' => $id, 'newonly' => $newonly, 'last' => $last)); redirect($url); } } $PAGE->set_title("{$strchat}: {$course->shortname}: " . format_string($chat->name, true) . "{$groupname}"); echo $OUTPUT->header(); echo $OUTPUT->container_start(null, 'page-mod-chat-gui_basic'); echo $OUTPUT->heading(format_string($course->shortname), 1); echo $OUTPUT->heading(format_string($chat->name), 2); echo $OUTPUT->heading(get_string('participants'), 3); echo $OUTPUT->box_start('generalbox', 'participants'); echo '<ul>';
} //Get the user theme and enough info to be used in chat_format_message() which passes it along to if (!($user = $DB->get_record('user', array('id' => $chatuser->userid, 'deleted' => 0, 'suspended' => 0)))) { // no optimisation here, it would break again in future! print_error('invaliduser'); } \core\session\manager::set_user($user); $PAGE->set_pagelayout('embedded'); //Setup course, lang and theme $PAGE->set_course($course); $courseid = $chatuser->course; if (!($cm = get_coursemodule_from_instance('chat', $chatuser->chatid, $courseid))) { print_error('invalidcoursemodule'); } if ($beep) { chat_send_chatmessage($chatuser, "beep {$beep}", 0, $cm); $chatuser->lastmessageping = time(); // A beep is a ping ;-) } $chatuser->lastping = time(); $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id' => $chatuser->id)); $refreshurl = "users.php?chat_sid={$chat_sid}"; /// Get list of users if (!($chatusers = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid))) { print_error('errornousers', 'chat'); } $uidles = array(); foreach ($chatusers as $chatuser) { $uidles[] = $chatuser->id; } $module = array('name' => 'mod_chat_header', 'fullpath' => '/mod/chat/gui_header_js/module.js', 'requires' => array('node'));
} \core\session\manager::set_user($user); $PAGE->set_pagelayout('embedded'); // Setup course, lang and theme. $PAGE->set_course($course); $courseid = $chatuser->course; if (!$cm = get_coursemodule_from_instance('chat', $chatuser->chatid, $courseid)) { print_error('invalidcoursemodule'); } if ($beep) { chat_send_chatmessage($chatuser, "beep $beep", 0, $cm); $chatuser->lastmessageping = time(); // A beep is a ping. } $chatuser->lastping = time(); $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id' => $chatuser->id)); $refreshurl = "users.php?chat_sid=$chatsid"; // Get list of users. if (!$chatusers = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid)) { print_error('errornousers', 'chat'); } $uidles = Array();