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.
This commit is contained in:
Frederic Massart 2013-09-17 17:23:59 +08:00
parent 481753eca6
commit f49d814abc
15 changed files with 668 additions and 76 deletions

View File

@ -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;

View File

@ -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
//

View File

@ -0,0 +1,73 @@
<?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));
}
}

View File

@ -0,0 +1,97 @@
<?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.');
}
}
}

View File

@ -0,0 +1,96 @@
<?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'] = '';
}
}

View File

@ -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);
$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));
add_to_log($course->id, 'chat', 'talk', "view.php?id=$cm->id", $chat->id, $cm->id);
}
chat_delete_old_users();

View File

@ -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') {

View File

@ -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 ;-)
}

View File

@ -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

View File

@ -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!';

View File

@ -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

View File

@ -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');

View File

@ -0,0 +1,161 @@
<?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, '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();
}
public function test_sessions_viewed() {
global $USER;
$this->resetAfterTest();
// Not much can be tested here as the event is only triggered on a page load,
// let's just check that the event contains the expected basic information.
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
$params = array(
'context' => context_module::instance($chat->cmid),
'objectid' => $chat->id,
'other' => array(
'start' => 1234,
'end' => 5678
)
);
$event = \mod_chat\event\sessions_viewed::create($params);
$event->add_record_snapshot('chat', $chat);
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\mod_chat\event\sessions_viewed', $event);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals(context_module::instance($chat->cmid), $event->get_context());
$this->assertEquals(1234, $event->other['start']);
$this->assertEquals(5678, $event->other['end']);
$this->assertEquals($chat->id, $event->objectid);
$this->assertEquals($chat, $event->get_record_snapshot('chat', $chat->id));
$expected = array($course->id, 'chat', 'report', "report.php?id=$chat->cmid", $chat->id, $chat->cmid);
$this->assertEventLegacyLogData($expected, $event);
}
public function test_instances_list_viewed() {
global $USER;
$this->resetAfterTest();
// Not much can be tested here as the event is only triggered on a page load,
// let's just check that the event contains the expected basic information.
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$params = array(
'context' => context_course::instance($course->id)
);
$event = \mod_chat\event\instances_list_viewed::create($params);
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\mod_chat\event\instances_list_viewed', $event);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$expected = array($course->id, 'chat', 'view all', "index.php?id=$course->id", '');
$this->assertEventLegacyLogData($expected, $event);
}
}

View File

@ -0,0 +1,102 @@
<?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 data generator.
*
* @package core
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_chat data generator class.
*
* @package core
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_chat_generator extends testing_module_generator {
/**
* @var int keep track of how many messages have been created.
*/
protected $messagecount = 0;
/**
* To be called from data reset code only,
* do not use in tests.
* @return void
*/
public function reset() {
$this->messagecount = 0;
parent::reset();
}
/**
* Create new chat module instance
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/chat/lib.php");
$this->instancecount++;
$i = $this->instancecount;
$record = (object)(array)$record;
$options = (array)$options;
if (empty($record->course)) {
throw new coding_exception('Module generator requires $record->course.');
}
if (!isset($record->name)) {
$record->name = get_string('pluginname', 'chat') . ' ' . $i;
}
if (!isset($record->intro)) {
$record->intro = 'Test chat ' . $i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->keepdays)) {
$record->keepdays = 0;
}
if (!isset($record->studentlogs)) {
$record->studentlogs = 0;
}
if (!isset($record->chattime)) {
$record->chattime = time() - 2;
}
if (!isset($record->schedule)) {
$record->schedule = 0;
}
if (!isset($record->timemodified)) {
$record->timemodified = time();
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = chat_add_instance($record);
return $this->post_add_instance($id, $record->coursemodule);
}
}

View File

@ -0,0 +1,53 @@
<?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/>.
/**
* Genarator tests.
*
* @package mod_chat
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Genarator 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_genarator_testcase extends advanced_testcase {
public function test_create_instance() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$this->assertFalse($DB->record_exists('chat', array('course' => $course->id)));
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
$this->assertEquals(1, $DB->count_records('chat', array('course' => $course->id)));
$this->assertTrue($DB->record_exists('chat', array('course' => $course->id)));
$this->assertTrue($DB->record_exists('chat', array('id' => $chat->id)));
$params = array('course' => $course->id, 'name' => 'One more chat');
$chat = $this->getDataGenerator()->create_module('chat', $params);
$this->assertEquals(2, $DB->count_records('chat', array('course' => $course->id)));
$this->assertEquals('One more chat', $DB->get_field_select('chat', 'name', 'id = :id', array('id' => $chat->id)));
}
}