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); $this->assertEventContextNotUsed($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(); }
if (!$group = groups_get_group($groupid)) { print_error('invalidgroupid'); } $groupname = ': '.$group->name; } else { $groupname = ': '.get_string('allparticipants'); } } else { $groupid = 0; $groupname = ''; } $strchat = get_string('modulename', 'chat'); // must be before current_language() in chat_login_user() to force course language!!! $strchats = get_string('modulenameplural', 'chat'); $stridle = get_String('idle', 'chat'); if (!$chat_sid = chat_login_user($chat->id, 'basic', $groupid, $course)) { print_error('cantlogin', 'chat'); } if (!$chatusers = chat_get_users($chat->id, $groupid, $cm->groupingid)) { print_error('errornousers', 'chat'); } $DB->set_field('chat_users', 'lastping', time(), array('sid'=>$chat_sid)); if (!isset($SESSION->chatprefs)) { $SESSION->chatprefs = array(); } if (!isset($SESSION->chatprefs[$chat->id])) { $SESSION->chatprefs[$chat->id] = array(); $SESSION->chatprefs[$chat->id]['chatentered'] = time();
// Groups are being used if ($groupid = groups_get_activity_group($cm)) { if (!($group = groups_get_group($groupid, false))) { error("That group (id {$groupid}) doesn't exist!"); } $groupname = ': ' . $group->name; } else { $groupname = ': ' . get_string('allparticipants'); } } else { $groupid = 0; $groupname = ''; } $strchat = get_string('modulename', 'chat'); // must be before current_language() in chat_login_user() to force course language!!! if (!($chat_sid = chat_login_user($chat->id, 'header_js', $groupid, $course))) { error('Could not log in to chat room!!'); } $params = "chat_id={$id}&chat_sid={$chat_sid}"; // fallback to the old jsupdate, but allow other update modes $updatemode = 'jsupdate'; if (!empty($CFG->chat_normal_updatemode)) { $updatemode = $CFG->chat_normal_updatemode; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>
/** * Log the current user into a chat room in the given chat. * * @param int $chatid the chat instance id * @param int $groupid the user group id * @return array of warnings and the chat unique session id * @since Moodle 3.0 * @throws moodle_exception */ public static function login_user($chatid, $groupid = 0) { global $DB; $params = self::validate_parameters(self::login_user_parameters(), array('chatid' => $chatid, 'groupid' => $groupid)); $warnings = array(); // Request and permission validation. $chat = $DB->get_record('chat', array('id' => $params['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); if (!empty($params['groupid'])) { $groupid = $params['groupid']; // Determine is the group is visible to user. if (!groups_group_visible($groupid, $course, $cm)) { throw new moodle_exception('notingroup'); } } else { // Check to see if groups are being used here. if ($groupmode = groups_get_activity_groupmode($cm)) { $groupid = groups_get_activity_group($cm); // Determine is the group is visible to user (this is particullary for the group 0). if (!groups_group_visible($groupid, $course, $cm)) { throw new moodle_exception('notingroup'); } } else { $groupid = 0; } } // Get the unique chat session id. // Since we are going to use the chat via Web Service requests we set the ajax version (since it's the most similar). if (!($chatsid = chat_login_user($chat->id, 'ajax', $groupid, $course))) { throw moodle_exception('cantlogin', 'chat'); } $result = array(); $result['chatsid'] = $chatsid; $result['warnings'] = $warnings; return $result; }
print_error('invalidgroupid'); } $groupname = ': ' . $group->name; } else { $groupname = ': ' . get_string('allparticipants'); } } else { $groupid = 0; $groupname = ''; } // if requested theme doesn't exist, use default 'bubble' theme if (!file_exists(dirname(__FILE__) . '/theme/' . $theme . '/chat.css')) { $theme = 'compact'; } // login chat room if (!($chat_sid = chat_login_user($chat->id, 'ajax', $groupid, $course))) { print_error('cantlogin', 'chat'); } $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); $module = array('name' => 'mod_chat_ajax', 'fullpath' => '/mod/chat/gui_ajax/module.js', 'requires' => array('base', 'dom', 'event', 'event-mouseenter', 'event-key', 'json-parse', 'io', 'overlay', 'yui2-resize', 'yui2-layout', 'yui2-menu'), 'strings' => array(array('send', 'chat'), array('sending', 'chat'), array('inputarea', 'chat'), array('userlist', 'chat'), array('modulename', 'chat'), array('beep', 'chat'), array('talk', 'chat'))); $modulecfg = array('home' => $CFG->httpswwwroot . '/mod/chat/view.php?id=' . $cm->id, 'chaturl' => $CFG->httpswwwroot . '/mod/chat/gui_ajax/index.php?id=' . $id, 'theme' => $theme, 'userid' => $USER->id, 'sid' => $chat_sid, 'timer' => 3000, 'chat_lasttime' => 0, 'chat_lastrow' => null, 'chatroom_name' => $courseshortname . ": " . format_string($chat->name, true) . $groupname); $PAGE->requires->js_init_call('M.mod_chat_ajax.init', array($modulecfg), false, $module); $PAGE->set_title(get_string('modulename', 'chat') . ": {$courseshortname}: " . format_string($chat->name, true) . "{$groupname}"); $PAGE->add_body_class('yui-skin-sam'); $PAGE->set_pagelayout('embedded'); $PAGE->requires->css('/mod/chat/gui_ajax/theme/' . $theme . '/chat.css'); echo $OUTPUT->header(); echo $OUTPUT->box('<ul id="users-list"></ul>', '', 'chat-userlist'); echo $OUTPUT->box('', '', 'chat-options'); echo $OUTPUT->box('<ul id="messages-list"></ul>', '', 'chat-messages'); $table = new html_table();
// Groups are being used if ($groupid = groups_get_activity_group($cm)) { if (!($group = groups_get_group($groupid))) { print_error('invalidgroupid'); } $groupname = ': ' . $group->name; } else { $groupname = ': ' . get_string('allparticipants'); } } else { $groupid = 0; $groupname = ''; } $strchat = get_string('modulename', 'chat'); // must be before current_language() in chat_login_user() to force course language!!! if (!($chat_sid = chat_login_user($chat->id, 'sockets', $groupid, $course))) { print_error('cantlogin'); } $params = "chat_sid={$chat_sid}"; $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title> <?php echo "{$strchat}: " . $courseshortname . ": " . format_string($chat->name, true, array('context' => $context)) . "{$groupname}"; ?> </title> </head>