Commit f49d814a authored by Frederic Massart's avatar Frederic Massart
Browse files

MDL-40048 mod_chat: Replacing add_to_log with new events

For this to be done properly, a new function to send the message
and trigger the event has been created. Following that a
data generator for chat, and some unit tests.
parent 481753ec
......@@ -77,22 +77,14 @@ case 'chat':
}
if (!empty($chat_message)) {
$message = new stdClass();
$message->chatid = $chatuser->chatid;
$message->userid = $chatuser->userid;
$message->groupid = $chatuser->groupid;
$message->message = $chat_message;
$message->timestamp = time();
chat_send_chatmessage($chatuser, $chat_message, 0, $cm);
$chatuser->lastmessageping = time() - 2;
$DB->update_record('chat_users', $chatuser);
$DB->insert_record('chat_messages', $message);
$DB->insert_record('chat_messages_current', $message);
// response ok message
// Response OK message.
echo json_encode(true);
add_to_log($course->id, 'chat', 'talk', "view.php?id=$cm->id", $chat->id, $cm->id);
ob_end_flush();
}
break;
......
......@@ -345,6 +345,7 @@ EOD;
switch($type) {
case CHAT_SIDEKICK_BEEP:
// Incoming beep
$msg = New stdClass;
$msg->chatid = $this->sets_info[$sessionid]['chatid'];
......@@ -355,8 +356,8 @@ EOD;
$msg->timestamp = time();
// Commit to DB
$DB->insert_record('chat_messages', $msg, false);
$DB->insert_record('chat_messages_current', $msg, false);
chat_send_chatmessage($this->sets_info[$sessionid]['chatuser'], $msg->message, false,
$this->sets_info[$sessionid]['cm']);
// OK, now push it out to all users
$this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
......@@ -450,8 +451,8 @@ EOD;
$msg->message = $msg->message;
// Commit to DB
$DB->insert_record('chat_messages', $msg, false);
$DB->insert_record('chat_messages_current', $msg, false);
chat_send_chatmessage($this->sets_info[$sessionid]['chatuser'], $msg->message, false,
$this->sets_info[$sessionid]['cm']);
// Undo the hack
$msg->message = $origmsg;
......@@ -517,6 +518,10 @@ EOD;
$this->dismiss_half($sessionid);
return false;
}
if (!($cm = get_coursemodule_from_instance('chat', $chat->id, $course->id))) {
$this->dismiss_half($sessionid);
return false;
}
global $CHAT_HTMLHEAD_JS, $CFG;
......@@ -531,6 +536,7 @@ EOD;
'courseid' => $course->id,
'chatuser' => $chatuser,
'chatid' => $chat->id,
'cm' => $cm,
'user' => $user,
'userid' => $user->id,
'groupid' => $chatuser->groupid,
......@@ -573,8 +579,7 @@ EOD;
$msg->message = 'enter';
$msg->timestamp = time();
$DB->insert_record('chat_messages', $msg, false);
$DB->insert_record('chat_messages_current', $msg, false);
chat_send_chatmessage($chatuser, $msg->message, true);
$this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
return true;
......@@ -781,8 +786,7 @@ EOD;
$msg->timestamp = time();
$this->trace('User has disconnected, destroying uid '.$info['userid'].' with SID '.$sessionid, E_USER_WARNING);
$DB->insert_record('chat_messages', $msg, false);
$DB->insert_record('chat_messages_current', $msg, false);
chat_send_chatmessage($info['chatuser'], $msg->message, true);
// *************************** IMPORTANT
//
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_chat instances list viewed event.
*
* @package mod_chat
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_chat instances list viewed event class.
*
* @package mod_chat
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class instances_list_viewed extends \core\event\course_module_instances_list_viewed {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "User $this->userid viewed the list of chat activities in the course $this->courseid.";
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'chat', 'view all', 'index.php?id=' . $this->courseid, '');
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_instances_list_viewed', 'mod_chat');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/chat/index.php', array('id' => $this->courseid));
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_chat message sent event.
*
* @package mod_chat
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_chat message sent event class.
*
* @package mod_chat
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class message_sent extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user $this->relateduserid has sent a message in a chat.";
}
/**
* Return legacy log data.
*
* @return array
*/
protected function get_legacy_logdata() {
$message = $this->get_record_snapshot('chat_messages', $this->objectid);
return array($this->courseid, 'chat', 'talk', 'view.php?id=' . $this->context->instanceid,
$message->chatid, $this->context->instanceid, $this->relateduserid);
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_message_sent', 'mod_chat');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/chat/view.php', array('id' => $this->context->instanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'c';
$this->data['level'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'chat_messages';
}
/**
* Custom validation.
*
* @return void
*/
protected function validate_data() {
if (!isset($this->relateduserid)) {
throw new \coding_exception('The property relateduserid must be set.');
}
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_chat sessions viewed event.
*
* @package mod_chat
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_chat sessions viewed event class.
*
* @package mod_chat
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sessions_viewed extends \core\event\content_viewed {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user {$this->userid} has viewed the sessions of the chat {$this->objectid}.";
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'chat', 'report', 'report.php?id=' . $this->context->instanceid,
$this->objectid, $this->context->instanceid);
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_sessions_viewed', 'mod_chat');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/chat/report.php', array('id' => $this->context->instanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['level'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'chat';
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
// The parent class requires this to be non-empty. We are setting it and ignore the parent validation.
$this->data['other']['content'] = '';
}
}
......@@ -93,19 +93,11 @@ if (!empty($refresh) and data_submitted()) {
} else if (empty($refresh) and data_submitted() and confirm_sesskey()) {
if ($message!='') {
$newmessage = new stdClass();
$newmessage->chatid = $chat->id;
$newmessage->userid = $USER->id;
$newmessage->groupid = $groupid;
$newmessage->systrem = 0;
$newmessage->message = $message;
$newmessage->timestamp = time();
$DB->insert_record('chat_messages', $newmessage);
$DB->insert_record('chat_messages_current', $newmessage);
$DB->set_field('chat_users', 'lastmessageping', time(), array('sid'=>$chat_sid));
$chatuser = $DB->get_record('chat_users', array('sid' => $chat_sid));
chat_send_chatmessage($chatuser, $message, 0, $cm);
add_to_log($course->id, 'chat', 'talk', "view.php?id=$cm->id", $chat->id, $cm->id);
$DB->set_field('chat_users', 'lastmessageping', time(), array('sid'=>$chat_sid));
}
chat_delete_old_users();
......
......@@ -44,22 +44,10 @@ $chat_message = clean_text($chat_message, FORMAT_MOODLE); // Strip bad tags
if (!empty($chat_message)) {
$message = new stdClass();
$message->chatid = $chatuser->chatid;
$message->userid = $chatuser->userid;
$message->groupid = $chatuser->groupid;
$message->message = $chat_message;
$message->timestamp = time();
$DB->insert_record('chat_messages', $message);
$DB->insert_record('chat_messages_current', $message);
chat_send_chatmessage($chatuser, $chat_message, 0, $cm);
$chatuser->lastmessageping = time() - 2;
$DB->update_record('chat_users', $chatuser);
if ($cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) {
add_to_log($course->id, 'chat', 'talk', "view.php?id=$cm->id", $chat->id, $cm->id);
}
}
if ($chatuser->version == 'header_js') {
......
......@@ -39,16 +39,7 @@ if (!$cm = get_coursemodule_from_instance('chat', $chatuser->chatid, $courseid))
}
if ($beep) {
$message->chatid = $chatuser->chatid;
$message->userid = $chatuser->userid;
$message->groupid = $chatuser->groupid;
$message->message = "beep $beep";
$message->system = 0;
$message->timestamp = time();
$DB->insert_record('chat_messages', $message);
$DB->insert_record('chat_messages_current', $message);
chat_send_chatmessage($chatuser, "beep $beep", 0, $cm);
$chatuser->lastmessageping = time(); // A beep is a ping ;-)
}
......
......@@ -14,8 +14,11 @@ if (! $course = $DB->get_record('course', array('id'=>$id))) {
require_course_login($course);
$PAGE->set_pagelayout('incourse');
add_to_log($course->id, 'chat', 'view all', "index.php?id=$course->id", '');
$params = array(
'context' => context_course::instance($id)
);
$event = \mod_chat\event\instances_list_viewed::create($params);
$event->trigger();
/// Get all required strings
......
......@@ -61,6 +61,9 @@ $string['chatreport'] = 'Chat sessions';
$string['chat:talk'] = 'Talk in a chat';
$string['chattime'] = 'Next chat time';
$string['entermessage'] = "Enter your message";
$string['event_instances_list_viewed'] = 'Instances list viewed';
$string['event_message_sent'] = 'Message sent';
$string['event_sessions_viewed'] = 'Sessions viewed';
$string['idle'] = 'Idle';
$string['inputarea'] = 'Input area';
$string['invalidid'] = 'Could not find that chat room!';
......
......@@ -602,16 +602,7 @@ function chat_login_user($chatid, $version, $groupid, $course) {
if ($version == 'sockets') {
// do not send 'enter' message, chatd will do it
} else {
$message = new stdClass();
$message->chatid = $chatuser->chatid;
$message->userid = $chatuser->userid;
$message->groupid = $groupid;
$message->message = 'enter';
$message->system = 1;
$message->timestamp = time();
$DB->insert_record('chat_messages', $message);
$DB->insert_record('chat_messages_current', $message);
chat_send_chatmessage($chatuser, 'enter', true);
}
}
......@@ -637,16 +628,7 @@ function chat_delete_old_users() {
if ($oldusers = $DB->get_records_select('chat_users', $query, $params) ) {
$DB->delete_records_select('chat_users', $query, $params);
foreach ($oldusers as $olduser) {
$message = new stdClass();
$message->chatid = $olduser->chatid;
$message->userid = $olduser->userid;
$message->groupid = $olduser->groupid;
$message->message = 'exit';
$message->system = 1;
$message->timestamp = time();
$DB->insert_record('chat_messages', $message);
$DB->insert_record('chat_messages_current', $message);
chat_send_chatmessage($olduser, 'exit', true);
}
}
}
......@@ -708,6 +690,51 @@ function chat_update_chat_times($chatid=0) {
}
}
/**
* Send a message on the chat.
*
* @param object $chatuser The chat user record.
* @param string $messagetext The message to be sent.
* @param bool $system False for non-system messages, true for system messages.
* @param object $cm The course module object, pass it to save a database query when we trigger the event.
* @return int The message ID.
* @since 2.6
*/
function chat_send_chatmessage($chatuser, $messagetext, $system = false, $cm = null) {
global $DB;
$message = new stdClass();
$message->chatid = $chatuser->chatid;
$message->userid = $chatuser->userid;
$message->groupid = $chatuser->groupid;
$message->message = $messagetext;
$message->system = $system ? 1 : 0;
$message->timestamp = time();
$messageid = $DB->insert_record('chat_messages', $message);
$DB->insert_record('chat_messages_current', $message);
$message->id = $messageid;
if (!$system) {
if (empty($cm)) {
$cm = get_coursemodule_from_instance('chat', $chatuser->chatid, $chatuser->course);
}
$params = array(
'context' => context_module::instance($cm->id),
'objectid' => $message->id,
// We set relateduserid, because when triggered from the chat daemon, the event userid is null.
'relateduserid' => $chatuser->userid
);
$event = \mod_chat\event\message_sent::create($params);
$event->add_record_snapshot('chat_messages', $message);
$event->trigger();
}
return $message->id;
}
/**
* @global object
* @global object
......
......@@ -47,7 +47,17 @@
notice(get_string('nopermissiontoseethechatlog', 'chat'));
}
add_to_log($course->id, 'chat', 'report', "report.php?id=$cm->id", $chat->id, $cm->id);
$params = array(
'context' => $context,
'objectid' => $chat->id,
'other' => array(
'start' => $start,
'end' => $end
)
);
$event = \mod_chat\event\sessions_viewed::create($params);
$event->add_record_snapshot('chat', $chat);
$event->trigger();
$strchats = get_string('modulenameplural', 'chat');
$strchat = get_string('modulename', 'chat');
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Events tests.
*
* @package mod_chat
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/chat/lib.php');
/**
* Events tests class.
*
* @package mod_chat
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_chat_events_testcase extends advanced_testcase {
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<