diff --git a/mod/chat/lib.php b/mod/chat/lib.php index e36a70acc82..dd7726f7369 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -113,20 +113,21 @@ function chat_add_instance($chat) { $returnid = $DB->insert_record("chat", $chat); - $event = new stdClass(); - $event->name = $chat->name; - $event->description = format_module_intro('chat', $chat, $chat->coursemodule); - $event->courseid = $chat->course; - $event->groupid = 0; - $event->userid = 0; - $event->modulename = 'chat'; - $event->instance = $returnid; - $event->eventtype = 'chattime'; - $event->timestart = $chat->chattime; - $event->timeduration = 0; - - calendar_event::create($event); + if ($chat->schedule > 0) { + $event = new stdClass(); + $event->name = $chat->name; + $event->description = format_module_intro('chat', $chat, $chat->coursemodule); + $event->courseid = $chat->course; + $event->groupid = 0; + $event->userid = 0; + $event->modulename = 'chat'; + $event->instance = $returnid; + $event->eventtype = 'chattime'; + $event->timestart = $chat->chattime; + $event->timeduration = 0; + calendar_event::create($event); + } return $returnid; } @@ -151,12 +152,35 @@ function chat_update_instance($chat) { if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'chat', 'instance' => $chat->id))) { - $event->name = $chat->name; - $event->description = format_module_intro('chat', $chat, $chat->coursemodule); - $event->timestart = $chat->chattime; + if ($chat->schedule > 0) { + $event->name = $chat->name; + $event->description = format_module_intro('chat', $chat, $chat->coursemodule); + $event->timestart = $chat->chattime; - $calendarevent = calendar_event::load($event->id); - $calendarevent->update($event); + $calendarevent = calendar_event::load($event->id); + $calendarevent->update($event); + } else { + // Do not publish this event, so delete it. + $calendarevent = calendar_event::load($event->id); + $calendarevent->delete(); + } + } else { + // No event, do we need to create one? + if ($chat->schedule > 0) { + $event = new stdClass(); + $event->name = $chat->name; + $event->description = format_module_intro('chat', $chat, $chat->coursemodule); + $event->courseid = $chat->course; + $event->groupid = 0; + $event->userid = 0; + $event->modulename = 'chat'; + $event->instance = $chat->id; + $event->eventtype = 'chattime'; + $event->timestart = $chat->chattime; + $event->timeduration = 0; + + calendar_event::create($event); + } } return true; diff --git a/mod/chat/tests/behat/chat_calendar_events.feature b/mod/chat/tests/behat/chat_calendar_events.feature new file mode 100644 index 00000000000..e91955bd93d --- /dev/null +++ b/mod/chat/tests/behat/chat_calendar_events.feature @@ -0,0 +1,36 @@ +@mod @mod_chat +Feature: Chat calendar entries + In order to notify students of upcoming chat sessons + As a teacher + I need to create a chat activity and publish the event times + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Tina | Teacher1 | teacher1@example.com | + | student1 | Sam | Student1 | student1@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + + Scenario: Create a chat activity and do not publish the start date to the calendar + Given the following "activities" exist: + | activity | name | intro | course | idnumber | schedule | + | chat | Test chat name | Test chat description | C1 | chat1 | 0 | + And I log in as "teacher1" + And I follow "Course 1" + When I navigate to "Calendar" node in "Site pages" + Then I should not see "Test chat name" + + Scenario: Create a chat activity and publish the start date to the calendar + Given the following "activities" exist: + | activity | name | intro | course | idnumber | schedule | + | chat | Test chat name | Test chat description | C1 | chat1 | 1 | + And I log in as "teacher1" + And I follow "Course 1" + When I navigate to "Calendar" node in "Site pages" + Then I should see "Test chat name"