From bb40a28d9ba8c2d2cbdb3ea2194bfe7d2220dbbf Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Mon, 1 May 2017 13:28:37 +0530 Subject: [PATCH] MDL-58497 forum: Show correct itemcount on action events --- mod/forum/lib.php | 40 +++++++++++++++++++++++++++++++++--- mod/forum/tests/lib_test.php | 5 +++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/mod/forum/lib.php b/mod/forum/lib.php index be2e73c9526..1ed8ef20caa 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -8188,8 +8188,9 @@ function mod_forum_get_fontawesome_icon_map() { * @return bool */ function mod_forum_core_calendar_event_action_shows_item_count(calendar_event $event, $itemcount = 0) { - // Always show item count for forums if item count is greater than 0. - return $itemcount > 0; + // Always show item count for forums if item count is greater than 1. + // If only one action is required than it is obvious and we don't show it for other modules. + return $itemcount > 1; } /** @@ -8204,6 +8205,8 @@ function mod_forum_core_calendar_event_action_shows_item_count(calendar_event $e */ function mod_forum_core_calendar_provide_event_action(calendar_event $event, \core_calendar\action_factory $factory) { + global $DB, $USER; + $cm = get_fast_modinfo($event->courseid)->instances['forum'][$event->instance]; $context = context_module::instance($cm->id); @@ -8221,10 +8224,41 @@ function mod_forum_core_calendar_provide_event_action(calendar_event $event, return null; } + // Get action itemcount. + $itemcount = 0; + $forum = $DB->get_record('forum', array('id' => $cm->instance)); + $postcountsql = " + SELECT + COUNT(1) + FROM + {forum_posts} fp + 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); + + if ($forum->completiondiscussions) { + $count = $DB->count_records('forum_discussions', array('forum' => $forum->id, 'userid' => $USER->id)); + $itemcount += ($forum->completiondiscussions >= $count) ? ($forum->completiondiscussions - $count) : 0; + } + + if ($forum->completionreplies) { + $count = $DB->get_field_sql( $postcountsql.' AND fp.parent<>0', $postcountparams); + $itemcount += ($forum->completionreplies >= $count) ? ($forum->completionreplies - $count) : 0; + } + + if ($forum->completionposts) { + $count = $DB->get_field_sql($postcountsql, $postcountparams); + $itemcount += ($forum->completionposts >= $count) ? ($forum->completionposts - $count) : 0; + } + + // Well there is always atleast one actionable item (view forum, etc). + $itemcount = $itemcount > 0 ? $itemcount : 1; + return $factory->create_instance( get_string('view'), new \moodle_url('/mod/forum/view.php', ['id' => $cm->id]), - 1, + $itemcount, true ); } diff --git a/mod/forum/tests/lib_test.php b/mod/forum/tests/lib_test.php index 4441bf1d0cf..69ac1fd9a07 100644 --- a/mod/forum/tests/lib_test.php +++ b/mod/forum/tests/lib_test.php @@ -3329,7 +3329,8 @@ class mod_forum_lib_testcase extends advanced_testcase { // Create the activity. $course = $this->getDataGenerator()->create_course(); - $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); + $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, @@ -3345,7 +3346,7 @@ class mod_forum_lib_testcase extends advanced_testcase { $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(1, $actionevent->get_item_count()); + $this->assertEquals(7, $actionevent->get_item_count()); $this->assertTrue($actionevent->is_actionable()); }