From bea71d57327ce8c1e4bd434b66d6a8845bc1e27c Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 22 Feb 2017 13:42:07 +0800 Subject: [PATCH] MDL-57731 mod_chat: added action events Part of MDL-55611 epic. --- mod/chat/lib.php | 51 ++++++++++++++- mod/chat/tests/lib_test.php | 153 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 mod/chat/tests/lib_test.php diff --git a/mod/chat/lib.php b/mod/chat/lib.php index 9afa417c45e..042c05eaf8c 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -26,6 +26,9 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot.'/calendar/lib.php'); +// Event types. +define('CHAT_EVENT_TYPE_CHATTIME', 'chattime'); + // The HTML head for the message window to start with ( is used to get some browsers starting with output. global $CHAT_HTMLHEAD; $CHAT_HTMLHEAD = "\n\n\n".padding(200); @@ -115,6 +118,7 @@ function chat_add_instance($chat) { if ($chat->schedule > 0) { $event = new stdClass(); + $event->type = CALENDAR_EVENT_TYPE_ACTION; $event->name = $chat->name; $event->description = format_module_intro('chat', $chat, $chat->coursemodule); $event->courseid = $chat->course; @@ -122,8 +126,9 @@ function chat_add_instance($chat) { $event->userid = 0; $event->modulename = 'chat'; $event->instance = $returnid; - $event->eventtype = 'chattime'; + $event->eventtype = CHAT_EVENT_TYPE_CHATTIME; $event->timestart = $chat->chattime; + $event->timesort = $chat->chattime; $event->timeduration = 0; \core_calendar\event::create($event); @@ -153,9 +158,11 @@ function chat_update_instance($chat) { if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'chat', 'instance' => $chat->id))) { if ($chat->schedule > 0) { + $event->type = CALENDAR_EVENT_TYPE_ACTION; $event->name = $chat->name; $event->description = format_module_intro('chat', $chat, $chat->coursemodule); $event->timestart = $chat->chattime; + $event->timesort = $chat->chattime; $calendarevent = \core_calendar\event::load($event->id); $calendarevent->update($event); @@ -168,6 +175,7 @@ function chat_update_instance($chat) { // No event, do we need to create one? if ($chat->schedule > 0) { $event = new stdClass(); + $event->type = CALENDAR_EVENT_TYPE_ACTION; $event->name = $chat->name; $event->description = format_module_intro('chat', $chat, $chat->coursemodule); $event->courseid = $chat->course; @@ -175,8 +183,9 @@ function chat_update_instance($chat) { $event->userid = 0; $event->modulename = 'chat'; $event->instance = $chat->id; - $event->eventtype = 'chattime'; + $event->eventtype = CHAT_EVENT_TYPE_CHATTIME; $event->timestart = $chat->chattime; + $event->timesort = $chat->chattime; $event->timeduration = 0; \core_calendar\event::create($event); @@ -442,6 +451,7 @@ function chat_refresh_events($courseid = 0) { $cm = get_coursemodule_from_instance('chat', $chat->id, $chat->course); $event = new stdClass(); $event->name = $chat->name; + $event->type = CALENDAR_EVENT_TYPE_ACTION; $event->description = format_module_intro('chat', $chat, $cm->id); $event->timestart = $chat->chattime; @@ -455,7 +465,7 @@ function chat_refresh_events($courseid = 0) { $event->userid = 0; $event->modulename = 'chat'; $event->instance = $chat->id; - $event->eventtype = 'chattime'; + $event->eventtype = CHAT_EVENT_TYPE_CHATTIME; $event->timeduration = 0; $event->visible = $DB->get_field('course_modules', 'visible', array('module' => $moduleid, 'instance' => $chat->id)); @@ -1396,3 +1406,38 @@ function chat_view($chat, $course, $cm, $context) { $completion = new completion_info($course); $completion->set_module_viewed($cm); } + +/** + * Is the event visible? + * + * @param \core_calendar\event $event + * @return bool Returns true if the event is visible to the current user, false otherwise. + */ +function mod_chat_core_calendar_is_event_visible(\core_calendar\event $event) { + $cm = get_fast_modinfo($event->courseid)->instances['chat'][$event->instance]; + $context = context_module::instance($cm->id); + + return has_capability('mod/chat:view', $context); +} + +/** + * Handles creating actions for events. + * + * @param \core_calendar\event $event + * @param \core_calendar\action_factory $factory + * @return \core_calendar\local\event\value_objects\action|\core_calendar\local\interfaces\action_interface|null + */ +function mod_chat_core_calendar_provide_event_action(\core_calendar\event $event, + \core_calendar\action_factory $factory) { + global $DB; + + $cm = get_fast_modinfo($event->courseid)->instances['chat'][$event->instance]; + $chattime = $DB->get_field('chat', 'chattime', array('id' => $event->instance)); + + return $factory->create_instance( + get_string('enterchat', 'chat'), + new \moodle_url('/mod/chat/view.php', array('id' => $cm->id)), + 1, + time() >= $chattime + ); +} diff --git a/mod/chat/tests/lib_test.php b/mod/chat/tests/lib_test.php new file mode 100644 index 00000000000..8b3b25f7da0 --- /dev/null +++ b/mod/chat/tests/lib_test.php @@ -0,0 +1,153 @@ +. + +/** + * Contains class containing unit tests for mod/chat/lib.php. + * + * @package mod_chat + * @category test + * @copyright 2017 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Class containing unit tests for mod/chat/lib.php. + * + * @package mod_chat + * @category test + * @copyright 2017 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_chat_lib_testcase extends advanced_testcase { + + public function setUp() { + $this->resetAfterTest(); + } + + public function test_chat_core_calendar_is_event_visible_chattime_event_as_admin() { + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a chat. + $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME); + + // Check that we can see the event. + $this->assertTrue(mod_chat_core_calendar_is_event_visible($event)); + } + + public function test_chat_core_calendar_is_event_visible_chattime_event_as_non_user() { + global $CFG; + + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a chat. + $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME); + + // Log out the user and set force login to true. + \core\session\manager::init_empty_session(); + $CFG->forcelogin = true; + + // Check that we can't see the event. + $this->assertFalse(mod_chat_core_calendar_is_event_visible($event)); + } + + public function test_chat_core_calendar_provide_event_action_chattime_event() { + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a chat. + $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $actionevent = mod_chat_core_calendar_provide_event_action($event, $factory); + + // Confirm the event was decorated. + $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); + $this->assertEquals(get_string('enterchat', 'chat'), $actionevent->get_name()); + $this->assertInstanceOf('moodle_url', $actionevent->get_url()); + $this->assertEquals(1, $actionevent->get_item_count()); + $this->assertTrue($actionevent->is_actionable()); + } + + public function test_chat_core_calendar_provide_event_action_chattime_event_in_future() { + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a chat. + $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id, + 'chattime' => time() + DAYSECS)); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $actionevent = mod_chat_core_calendar_provide_event_action($event, $factory); + + // Confirm the event was decorated. + $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); + $this->assertEquals(get_string('enterchat', 'chat'), $actionevent->get_name()); + $this->assertInstanceOf('moodle_url', $actionevent->get_url()); + $this->assertEquals(1, $actionevent->get_item_count()); + $this->assertFalse($actionevent->is_actionable()); + } + + /** + * Creates an action event. + * + * @param int $courseid + * @param int $instanceid The chat id. + * @param string $eventtype The event type. eg. ASSIGN_EVENT_TYPE_DUE. + * @return bool|\core_calendar\event + */ + private function create_action_event($courseid, $instanceid, $eventtype) { + $event = new stdClass(); + $event->name = 'Calendar event'; + $event->modulename = 'chat'; + $event->courseid = $courseid; + $event->instance = $instanceid; + $event->type = CALENDAR_EVENT_TYPE_ACTION; + $event->eventtype = $eventtype; + $event->timestart = time(); + + return \core_calendar\event::create($event); + } +} -- 2.11.4.GIT