diff --git a/mod/forum/lang/en/forum.php b/mod/forum/lang/en/forum.php index 1733e64da63..61494bf0dea 100644 --- a/mod/forum/lang/en/forum.php +++ b/mod/forum/lang/en/forum.php @@ -52,6 +52,7 @@ $string['blockperioddisabled'] = 'Don\'t block'; $string['blogforum'] = 'Standard forum displayed in a blog-like format'; $string['bynameondate'] = 'by {$a->name} - {$a->date}'; $string['cachedef_forum_is_tracked'] = 'Forum tracking status for user'; +$string['calendardue'] = '{$a} is due'; $string['cannotadd'] = 'Could not add the discussion for this forum'; $string['cannotadddiscussion'] = 'Adding discussions to this forum requires group membership.'; $string['cannotadddiscussionall'] = 'You do not have permission to add a new discussion topic for all participants.'; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 361c6e70be4..cdecf8ae582 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -86,6 +86,8 @@ define('FORUM_DISCUSSION_UNPINNED', 0); function forum_add_instance($forum, $mform = null) { global $CFG, $DB; + require_once($CFG->dirroot.'/mod/forum/locallib.php'); + $forum->timemodified = time(); if (empty($forum->assessed)) { @@ -127,6 +129,7 @@ function forum_add_instance($forum, $mform = null) { } } + forum_update_calendar($forum, $forum->coursemodule); forum_grade_item_update($forum); $completiontimeexpected = !empty($forum->completionexpected) ? $forum->completionexpected : null; @@ -162,7 +165,9 @@ function forum_instance_created($context, $forum) { * @return bool success */ function forum_update_instance($forum, $mform) { - global $DB, $OUTPUT, $USER; + global $CFG, $DB, $OUTPUT, $USER; + + require_once($CFG->dirroot.'/mod/forum/locallib.php'); $forum->timemodified = time(); $forum->id = $forum->instance; @@ -249,6 +254,7 @@ function forum_update_instance($forum, $mform) { } } + forum_update_calendar($forum, $forum->coursemodule); forum_grade_item_update($forum); $completiontimeexpected = !empty($forum->completionexpected) ? $forum->completionexpected : null; diff --git a/mod/forum/locallib.php b/mod/forum/locallib.php index 6f57004d28c..ab333a993ae 100644 --- a/mod/forum/locallib.php +++ b/mod/forum/locallib.php @@ -1,5 +1,4 @@ dirroot . '/mod/forum/lib.php'); require_once($CFG->libdir . '/portfolio/caller.php'); @@ -700,3 +706,50 @@ function mod_forum_get_tagged_posts($tag, $exclusivemode = false, $fromctx = 0, $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages); } } + +/** + * Update the calendar entries for this forum activity. + * + * @param stdClass $forum the row from the database table forum. + * @param int $cmid The coursemodule id + * @return bool + */ +function forum_update_calendar($forum, $cmid) { + global $DB, $CFG; + + require_once($CFG->dirroot.'/calendar/lib.php'); + + $event = new stdClass(); + + if (!empty($forum->duedate)) { + $event->name = get_string('calendardue', 'forum', $forum->name); + $event->description = format_module_intro('forum', $forum, $cmid); + $event->courseid = $forum->course; + $event->modulename = 'forum'; + $event->instance = $forum->id; + $event->type = CALENDAR_EVENT_TYPE_ACTION; + $event->eventtype = FORUM_EVENT_TYPE_DUE; + $event->timestart = $forum->duedate; + $event->timesort = $forum->duedate; + $event->visible = instance_is_visible('forum', $forum); + } + + $event->id = $DB->get_field('event', 'id', + array('modulename' => 'forum', 'instance' => $forum->id, 'eventtype' => FORUM_EVENT_TYPE_DUE)); + + if ($event->id) { + $calendarevent = calendar_event::load($event->id); + if (!empty($forum->duedate)) { + // Calendar event exists so update it. + $calendarevent->update($event); + } else { + // Calendar event is no longer needed. + $calendarevent->delete(); + } + } else if (!empty($forum->duedate)) { + // Event doesn't exist so create one. + calendar_event::create($event); + } + + return true; +} \ No newline at end of file diff --git a/mod/forum/tests/locallib_test.php b/mod/forum/tests/locallib_test.php new file mode 100644 index 00000000000..4a03a0d1882 --- /dev/null +++ b/mod/forum/tests/locallib_test.php @@ -0,0 +1,73 @@ +. + +/** + * File containing the forum module local library function tests. + * + * @package mod_forum + * @category test + * @copyright 2018 Shamim Rezaie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/mod/forum/lib.php'); + +/** + * Class mod_forum_locallib_testcase. + * + * @copyright 2018 Shamim Rezaie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_forum_locallib_testcase extends advanced_testcase { + public function test_forum_update_calendar() { + global $DB; + + $this->resetAfterTest(); + + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a forum activity. + $time = time(); + $forum = $this->getDataGenerator()->create_module('forum', + array( + 'course' => $course->id, + 'duedate' => $time + ) + ); + + // Check that there is now an event in the database. + $events = $DB->get_records('event'); + $this->assertCount(1, $events); + + // Get the event. + $event = reset($events); + + // Confirm the event is correct. + $this->assertEquals('forum', $event->modulename); + $this->assertEquals($forum->id, $event->instance); + $this->assertEquals(CALENDAR_EVENT_TYPE_ACTION, $event->type); + $this->assertEquals(FORUM_EVENT_TYPE_DUE, $event->eventtype); + $this->assertEquals($time, $event->timestart); + $this->assertEquals($time, $event->timesort); + } +} \ No newline at end of file