Merge branch 'MDL-58497-master' of git://github.com/ankitagarwal/moodle
authorDavid Monllao <davidm@moodle.com>
Thu, 4 May 2017 02:58:36 +0000 (10:58 +0800)
committerDavid Monllao <davidm@moodle.com>
Thu, 4 May 2017 02:58:36 +0000 (10:58 +0800)
mod/forum/lib.php
mod/forum/tests/lib_test.php

index 4398dcc..9a07d66 100644 (file)
@@ -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);
 
@@ -8219,10 +8222,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
     );
 }
index 4441bf1..69ac1fd 100644 (file)
@@ -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());
     }