MDL-52000 mod_forum: Respect timed posts in course overview block
authorAndrew Nicols <andrew@nicols.co.uk>
Wed, 6 Jan 2016 01:04:41 +0000 (09:04 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 6 Jan 2016 08:20:48 +0000 (16:20 +0800)
mod/forum/lib.php
mod/forum/tests/lib_test.php

index 2af2995..29f4f6d 100644 (file)
@@ -1394,8 +1394,11 @@ function forum_print_overview($courses,&$htmlarray) {
                 .'JOIN {forum_posts} p ON p.discussion = d.id '
                 ."WHERE ($coursessql) "
                 .'AND p.userid != ? '
+                .'AND (d.timestart <= ? AND (d.timeend = 0 OR d.timeend > ?)) '
                 .'GROUP BY d.id, d.forum, d.course, d.groupid '
                 .'ORDER BY d.course, d.forum';
+    $params[] = time();
+    $params[] = time();
 
     // Avoid warnings.
     if (!$discussions = $DB->get_records_sql($sql, $params)) {
@@ -1440,8 +1443,12 @@ function forum_print_overview($courses,&$htmlarray) {
             $params[] = $groupid;
         }
         $sql = substr($sql,0,-3); // take off the last OR
-        $sql .= ') AND p.modified >= ? AND r.id is NULL GROUP BY d.forum,d.course';
+        $sql .= ') AND p.modified >= ? AND r.id is NULL ';
+        $sql .= 'AND (d.timestart < ? AND (d.timeend = 0 OR d.timeend > ?)) ';
+        $sql .= 'GROUP BY d.forum,d.course';
         $params[] = $cutoffdate;
+        $params[] = time();
+        $params[] = time();
 
         if (!$unread = $DB->get_records_sql($sql, $params)) {
             $unread = array();
index 07876b3..ef53341 100644 (file)
@@ -2120,4 +2120,275 @@ class mod_forum_lib_testcase extends advanced_testcase {
         $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
         $this->assertTrue($can);
     }
+
+    public function test_print_overview() {
+        $this->resetAfterTest();
+        $course1 = self::getDataGenerator()->create_course();
+        $course2 = self::getDataGenerator()->create_course();
+
+        // Create an author user.
+        $author = self::getDataGenerator()->create_user();
+        $this->getDataGenerator()->enrol_user($author->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($author->id, $course2->id);
+
+        // Create a viewer user.
+        $viewer = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
+        $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
+
+        // Create two forums - one in each course.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $forum1 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course1->id));
+        $forum2 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course2->id));
+
+        // A standard post in the forum.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $record->userid = $author->id;
+        $record->forum = $forum1->id;
+        $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+
+        $this->setUser($viewer->id);
+        $courses = array(
+            $course1->id => clone $course1,
+            $course2->id => clone $course2,
+        );
+
+        foreach ($courses as $courseid => $course) {
+            $courses[$courseid]->lastaccess = 0;
+        }
+        $results = array();
+        forum_print_overview($courses, $results);
+
+        // There should be one entry for course1, and no others.
+        $this->assertCount(1, $results);
+
+        // There should be one entry for a forum in course1.
+        $this->assertCount(1, $results[$course1->id]);
+        $this->assertArrayHasKey('forum', $results[$course1->id]);
+    }
+
+    public function test_print_overview_groups() {
+        global $SESSION;
+        $this->resetAfterTest();
+        $course1 = self::getDataGenerator()->create_course();
+        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
+        $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
+
+        // Create an author user.
+        $author = self::getDataGenerator()->create_user();
+        $this->getDataGenerator()->enrol_user($author->id, $course1->id);
+
+        // Create two viewer users - one in each group.
+        $viewer1 = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
+        $this->getDataGenerator()->enrol_user($viewer1->id, $course1->id);
+        $this->getDataGenerator()->create_group_member(array('userid' => $viewer1->id, 'groupid' => $group1->id));
+
+        $viewer2 = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
+        $this->getDataGenerator()->enrol_user($viewer2->id, $course1->id);
+        $this->getDataGenerator()->create_group_member(array('userid' => $viewer2->id, 'groupid' => $group2->id));
+
+        // Create a forum.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $forum1 = self::getDataGenerator()->create_module('forum', (object) array(
+            'course'        => $course1->id,
+            'groupmode'     => SEPARATEGROUPS,
+        ));
+
+        // A post in the forum for group1.
+        $record = new stdClass();
+        $record->course     = $course1->id;
+        $record->userid     = $author->id;
+        $record->forum      = $forum1->id;
+        $record->groupid    = $group1->id;
+        $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+
+        $course1->lastaccess = 0;
+        $courses = array($course1->id => $course1);
+
+        // As viewer1 (same group as post).
+        $this->setUser($viewer1->id);
+        $results = array();
+        forum_print_overview($courses, $results);
+
+        // There should be one entry for course1.
+        $this->assertCount(1, $results);
+
+        // There should be one entry for a forum in course1.
+        $this->assertCount(1, $results[$course1->id]);
+        $this->assertArrayHasKey('forum', $results[$course1->id]);
+
+        // As viewer2 (different group to post).
+        // Temporary hack to workaround for MDL-52656.
+        $SESSION->currentgroup = null;
+
+        $this->setUser($viewer2->id);
+        $results = array();
+        forum_print_overview($courses, $results);
+
+        // There should be one entry for course1.
+        $this->assertCount(0, $results);
+    }
+
+    /**
+     * @dataProvider print_overview_timed_provider
+     */
+    public function test_print_overview_timed($config, $hasresult) {
+        $this->resetAfterTest();
+        $course1 = self::getDataGenerator()->create_course();
+
+        // Create an author user.
+        $author = self::getDataGenerator()->create_user();
+        $this->getDataGenerator()->enrol_user($author->id, $course1->id);
+
+        // Create a viewer user.
+        $viewer = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
+        $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
+
+        // Create a forum.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $forum1 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course1->id));
+
+        // A timed post with a timestart in the past (24 hours ago).
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $record->userid = $author->id;
+        $record->forum = $forum1->id;
+        if (isset($config['timestartmodifier'])) {
+            $record->timestart = time() + $config['timestartmodifier'];
+        }
+        if (isset($config['timeendmodifier'])) {
+            $record->timeend = time() + $config['timeendmodifier'];
+        }
+        $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+
+        $course1->lastaccess = 0;
+        $courses = array($course1->id => $course1);
+
+        // As viewer, check the forum_print_overview result.
+        $this->setUser($viewer->id);
+        $results = array();
+        forum_print_overview($courses, $results);
+
+        if ($hasresult) {
+            // There should be one entry for course1.
+            $this->assertCount(1, $results);
+
+            // There should be one entry for a forum in course1.
+            $this->assertCount(1, $results[$course1->id]);
+            $this->assertArrayHasKey('forum', $results[$course1->id]);
+        } else {
+            // There should be no entries for any course.
+            $this->assertCount(0, $results);
+        }
+    }
+
+    /**
+     * @dataProvider print_overview_timed_provider
+     */
+    public function test_print_overview_timed_groups($config, $hasresult) {
+        global $SESSION;
+        $this->resetAfterTest();
+        $course1 = self::getDataGenerator()->create_course();
+        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
+        $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
+
+        // Create an author user.
+        $author = self::getDataGenerator()->create_user();
+        $this->getDataGenerator()->enrol_user($author->id, $course1->id);
+
+        // Create two viewer users - one in each group.
+        $viewer1 = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
+        $this->getDataGenerator()->enrol_user($viewer1->id, $course1->id);
+        $this->getDataGenerator()->create_group_member(array('userid' => $viewer1->id, 'groupid' => $group1->id));
+
+        $viewer2 = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
+        $this->getDataGenerator()->enrol_user($viewer2->id, $course1->id);
+        $this->getDataGenerator()->create_group_member(array('userid' => $viewer2->id, 'groupid' => $group2->id));
+
+        // Create a forum.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $forum1 = self::getDataGenerator()->create_module('forum', (object) array(
+            'course'        => $course1->id,
+            'groupmode'     => SEPARATEGROUPS,
+        ));
+
+        // A post in the forum for group1.
+        $record = new stdClass();
+        $record->course     = $course1->id;
+        $record->userid     = $author->id;
+        $record->forum      = $forum1->id;
+        $record->groupid    = $group1->id;
+        if (isset($config['timestartmodifier'])) {
+            $record->timestart = time() + $config['timestartmodifier'];
+        }
+        if (isset($config['timeendmodifier'])) {
+            $record->timeend = time() + $config['timeendmodifier'];
+        }
+        $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+
+        $course1->lastaccess = 0;
+        $courses = array($course1->id => $course1);
+
+        // As viewer1 (same group as post).
+        $this->setUser($viewer1->id);
+        $results = array();
+        forum_print_overview($courses, $results);
+
+        if ($hasresult) {
+            // There should be one entry for course1.
+            $this->assertCount(1, $results);
+
+            // There should be one entry for a forum in course1.
+            $this->assertCount(1, $results[$course1->id]);
+            $this->assertArrayHasKey('forum', $results[$course1->id]);
+        } else {
+            // There should be no entries for any course.
+            $this->assertCount(0, $results);
+        }
+
+        // As viewer2 (different group to post).
+        // Temporary hack to workaround for MDL-52656.
+        $SESSION->currentgroup = null;
+
+        $this->setUser($viewer2->id);
+        $results = array();
+        forum_print_overview($courses, $results);
+
+        // There should be one entry for course1.
+        $this->assertCount(0, $results);
+    }
+
+    public function print_overview_timed_provider() {
+        return array(
+            'timestart_past' => array(
+                'discussionconfig' => array(
+                    'timestartmodifier' => -86000,
+                ),
+                'hasresult'         => true,
+            ),
+            'timestart_future' => array(
+                'discussionconfig' => array(
+                    'timestartmodifier' => 86000,
+                ),
+                'hasresult'         => false,
+            ),
+            'timeend_past' => array(
+                'discussionconfig' => array(
+                    'timeendmodifier'   => -86000,
+                ),
+                'hasresult'         => false,
+            ),
+            'timeend_future' => array(
+                'discussionconfig' => array(
+                    'timeendmodifier'   => 86000,
+                ),
+                'hasresult'         => true,
+            ),
+        );
+    }
 }