diff --git a/mod/forum/lib.php b/mod/forum/lib.php index e97ad191c28..bfd1218b09e 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -8331,22 +8331,28 @@ function mod_forum_core_calendar_event_action_shows_item_count(calendar_event $e * * @param calendar_event $event * @param \core_calendar\action_factory $factory + * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default). * @return \core_calendar\local\event\entities\action_interface|null */ function mod_forum_core_calendar_provide_event_action(calendar_event $event, - \core_calendar\action_factory $factory) { + \core_calendar\action_factory $factory, + int $userid = 0) { global $DB, $USER; - $cm = get_fast_modinfo($event->courseid)->instances['forum'][$event->instance]; + if (!$userid) { + $userid = $USER->id; + } + + $cm = get_fast_modinfo($event->courseid, $userid)->instances['forum'][$event->instance]; $context = context_module::instance($cm->id); - if (!has_capability('mod/forum:viewdiscussion', $context)) { + if (!has_capability('mod/forum:viewdiscussion', $context, $userid)) { return null; } $completion = new \completion_info($cm->get_course()); - $completiondata = $completion->get_data($cm, false); + $completiondata = $completion->get_data($cm, false, $userid); if ($completiondata->completionstate != COMPLETION_INCOMPLETE) { return null; @@ -8363,10 +8369,10 @@ function mod_forum_core_calendar_provide_event_action(calendar_event $event, INNER JOIN {forum_discussions} fd ON fp.discussion=fd.id WHERE fp.userid=:userid AND fd.forum=:forumid"; - $postcountparams = array('userid' => $USER->id, 'forumid' => $forum->id); + $postcountparams = array('userid' => $userid, 'forumid' => $forum->id); if ($forum->completiondiscussions) { - $count = $DB->count_records('forum_discussions', array('forum' => $forum->id, 'userid' => $USER->id)); + $count = $DB->count_records('forum_discussions', array('forum' => $forum->id, 'userid' => $userid)); $itemcount += ($forum->completiondiscussions >= $count) ? ($forum->completiondiscussions - $count) : 0; } diff --git a/mod/forum/tests/lib_test.php b/mod/forum/tests/lib_test.php index 5aee4dca258..2496b786af0 100644 --- a/mod/forum/tests/lib_test.php +++ b/mod/forum/tests/lib_test.php @@ -3350,6 +3350,44 @@ class mod_forum_lib_testcase extends advanced_testcase { $this->assertTrue($actionevent->is_actionable()); } + public function test_forum_core_calendar_provide_event_action_for_user() { + global $CFG; + + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // Create the activity. + $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, + 'completionreplies' => 5, 'completiondiscussions' => 2)); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $forum->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + + // Now log out. + $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. + $this->setUser(); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event for the student. + $actionevent = mod_forum_core_calendar_provide_event_action($event, $factory, $student->id); + + // Confirm the event was decorated. + $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); + $this->assertEquals(get_string('view'), $actionevent->get_name()); + $this->assertInstanceOf('moodle_url', $actionevent->get_url()); + $this->assertEquals(7, $actionevent->get_item_count()); + $this->assertTrue($actionevent->is_actionable()); + } + public function test_forum_core_calendar_provide_event_action_as_non_user() { global $CFG; @@ -3412,6 +3450,45 @@ class mod_forum_lib_testcase extends advanced_testcase { $this->assertNull($actionevent); } + public function test_forum_core_calendar_provide_event_action_already_completed_for_user() { + global $CFG; + + $this->resetAfterTest(); + $this->setAdminUser(); + + $CFG->enablecompletion = 1; + + // Create a course. + $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // Create the activity. + $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id), + array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS)); + + // Get some additional data. + $cm = get_coursemodule_from_instance('forum', $forum->id); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $forum->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + + // Mark the activity as completed for the student. + $completion = new completion_info($course); + $completion->set_module_viewed($cm, $student->id); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $actionevent = mod_forum_core_calendar_provide_event_action($event, $factory, $student->id); + + // Ensure result was null. + $this->assertNull($actionevent); + } + public function test_mod_forum_get_tagged_posts() { global $DB;